diff options
author | Thomas Citharel <tcit@tcit.fr> | 2016-02-07 16:52:59 +0100 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-02-26 18:13:17 +0100 |
commit | f38e03dc02c96344677fd2720912605b21c90b5d (patch) | |
tree | 2028cecf5d0930e1569ddb977e245ea44bedc38a /src/Wallabag/CommentBundle | |
parent | d3f1a9dc1ac181033ec54f9149d2807199fa8f17 (diff) | |
download | wallabag-f38e03dc02c96344677fd2720912605b21c90b5d.tar.gz wallabag-f38e03dc02c96344677fd2720912605b21c90b5d.tar.zst wallabag-f38e03dc02c96344677fd2720912605b21c90b5d.zip |
Comment work with annotator v2
- add missing annotator.js file and fix typo
- edit & delete routes, started tests
- basic tests
Diffstat (limited to 'src/Wallabag/CommentBundle')
12 files changed, 759 insertions, 0 deletions
diff --git a/src/Wallabag/CommentBundle/Controller/WallabagCommentController.php b/src/Wallabag/CommentBundle/Controller/WallabagCommentController.php new file mode 100644 index 00000000..be11f226 --- /dev/null +++ b/src/Wallabag/CommentBundle/Controller/WallabagCommentController.php | |||
@@ -0,0 +1,146 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CommentBundle\Controller; | ||
4 | |||
5 | use FOS\RestBundle\Controller\FOSRestController; | ||
6 | use Nelmio\ApiDocBundle\Annotation\ApiDoc; | ||
7 | use Symfony\Component\HttpFoundation\Request; | ||
8 | use Symfony\Component\HttpFoundation\Response; | ||
9 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; | ||
10 | use Wallabag\CommentBundle\Entity\Comment; | ||
11 | use Wallabag\CoreBundle\Entity\Entry; | ||
12 | |||
13 | class WallabagCommentController extends FOSRestController | ||
14 | { | ||
15 | /** | ||
16 | * Retrieve comments for an entry. | ||
17 | * | ||
18 | * @ApiDoc( | ||
19 | * requirements={ | ||
20 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | ||
21 | * } | ||
22 | * ) | ||
23 | * | ||
24 | * @return Response | ||
25 | */ | ||
26 | public function getAnnotationsAction(Entry $entry) | ||
27 | { | ||
28 | $commentRows = $this | ||
29 | ->getDoctrine() | ||
30 | ->getRepository('WallabagCommentBundle:Comment') | ||
31 | ->findCommentsByPageId($entry->getId(), $this->getUser()->getId()); | ||
32 | $total = count($commentRows); | ||
33 | $comments = array('total' => $total, 'rows' => $commentRows); | ||
34 | |||
35 | $json = $this->get('serializer')->serialize($comments, 'json'); | ||
36 | |||
37 | return $this->renderJsonResponse($json); | ||
38 | } | ||
39 | |||
40 | /** | ||
41 | * Creates a new comment. | ||
42 | * | ||
43 | * @param Entry $entry | ||
44 | * | ||
45 | * @ApiDoc( | ||
46 | * requirements={ | ||
47 | * {"name"="ranges", "dataType"="array", "requirement"="\w+", "description"="The range array for the annotation"}, | ||
48 | * {"name"="quote", "dataType"="string", "required"=false, "description"="Optional, quote for the comment"}, | ||
49 | * {"name"="text", "dataType"="string", "required"=true, "description"=""}, | ||
50 | * } | ||
51 | * ) | ||
52 | * | ||
53 | * @return Response | ||
54 | */ | ||
55 | public function postAnnotationAction(Request $request, Entry $entry) | ||
56 | { | ||
57 | $data = json_decode($request->getContent(), true); | ||
58 | |||
59 | $em = $this->getDoctrine()->getManager(); | ||
60 | |||
61 | $comment = new Comment($this->getUser()); | ||
62 | |||
63 | $comment->setText($data['text']); | ||
64 | if (array_key_exists('quote', $data)) { | ||
65 | $comment->setQuote($data['quote']); | ||
66 | } | ||
67 | if (array_key_exists('ranges', $data)) { | ||
68 | $comment->setRanges($data['ranges']); | ||
69 | } | ||
70 | |||
71 | $comment->setEntry($entry); | ||
72 | |||
73 | $em->persist($comment); | ||
74 | $em->flush(); | ||
75 | |||
76 | $json = $this->get('serializer')->serialize($comment, 'json'); | ||
77 | |||
78 | return $this->renderJsonResponse($json); | ||
79 | } | ||
80 | |||
81 | /** | ||
82 | * Updates a comment. | ||
83 | * | ||
84 | * @ApiDoc( | ||
85 | * requirements={ | ||
86 | * {"name"="comment", "dataType"="string", "requirement"="\w+", "description"="The comment ID"} | ||
87 | * } | ||
88 | * ) | ||
89 | * | ||
90 | * @ParamConverter("comment", class="WallabagCommentBundle:Comment") | ||
91 | * | ||
92 | * @return Response | ||
93 | */ | ||
94 | public function putAnnotationAction(Comment $comment, Request $request) | ||
95 | { | ||
96 | $data = json_decode($request->getContent(), true); | ||
97 | |||
98 | if (!is_null($data['text'])) { | ||
99 | $comment->setText($data['text']); | ||
100 | } | ||
101 | |||
102 | $em = $this->getDoctrine()->getManager(); | ||
103 | $em->flush(); | ||
104 | |||
105 | $json = $this->get('serializer')->serialize($comment, 'json'); | ||
106 | |||
107 | return $this->renderJsonResponse($json); | ||
108 | } | ||
109 | |||
110 | /** | ||
111 | * Removes a comment. | ||
112 | * | ||
113 | * @ApiDoc( | ||
114 | * requirements={ | ||
115 | * {"name"="comment", "dataType"="string", "requirement"="\w+", "description"="The comment ID"} | ||
116 | * } | ||
117 | * ) | ||
118 | * | ||
119 | * @ParamConverter("comment", class="WallabagCommentBundle:Comment") | ||
120 | * | ||
121 | * @return Response | ||
122 | */ | ||
123 | public function deleteAnnotationAction(Comment $comment) | ||
124 | { | ||
125 | $em = $this->getDoctrine()->getManager(); | ||
126 | $em->remove($comment); | ||
127 | $em->flush(); | ||
128 | |||
129 | $json = $this->get('serializer')->serialize($comment, 'json'); | ||
130 | |||
131 | return $this->renderJsonResponse($json); | ||
132 | } | ||
133 | |||
134 | /** | ||
135 | * Send a JSON Response. | ||
136 | * We don't use the Symfony JsonRespone, because it takes an array as parameter instead of a JSON string. | ||
137 | * | ||
138 | * @param string $json | ||
139 | * | ||
140 | * @return Response | ||
141 | */ | ||
142 | private function renderJsonResponse($json, $code = 200) | ||
143 | { | ||
144 | return new Response($json, $code, array('application/json')); | ||
145 | } | ||
146 | } | ||
diff --git a/src/Wallabag/CommentBundle/DataFixtures/ORM/LoadCommentData.php b/src/Wallabag/CommentBundle/DataFixtures/ORM/LoadCommentData.php new file mode 100644 index 00000000..717f4863 --- /dev/null +++ b/src/Wallabag/CommentBundle/DataFixtures/ORM/LoadCommentData.php | |||
@@ -0,0 +1,45 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\DataFixtures\ORM; | ||
4 | |||
5 | use Doctrine\Common\DataFixtures\AbstractFixture; | ||
6 | use Doctrine\Common\DataFixtures\OrderedFixtureInterface; | ||
7 | use Doctrine\Common\Persistence\ObjectManager; | ||
8 | use Wallabag\CommentBundle\Entity\Comment; | ||
9 | |||
10 | class LoadCommentData extends AbstractFixture implements OrderedFixtureInterface | ||
11 | { | ||
12 | /** | ||
13 | * {@inheritdoc} | ||
14 | */ | ||
15 | public function load(ObjectManager $manager) | ||
16 | { | ||
17 | $comment1 = new Comment($this->getReference('admin-user')); | ||
18 | $comment1->setEntry($this->getReference('entry1')); | ||
19 | $comment1->setText('This is my comment /o/'); | ||
20 | $comment1->setQuote('content'); | ||
21 | |||
22 | $manager->persist($comment1); | ||
23 | |||
24 | $this->addReference('comment1', $comment1); | ||
25 | |||
26 | $comment2 = new Comment($this->getReference('admin-user')); | ||
27 | $comment2->setEntry($this->getReference('entry2')); | ||
28 | $comment2->setText('This is my 2nd comment /o/'); | ||
29 | $comment2->setQuote('content'); | ||
30 | |||
31 | $manager->persist($comment2); | ||
32 | |||
33 | $this->addReference('comment2', $comment2); | ||
34 | |||
35 | $manager->flush(); | ||
36 | } | ||
37 | |||
38 | /** | ||
39 | * {@inheritdoc} | ||
40 | */ | ||
41 | public function getOrder() | ||
42 | { | ||
43 | return 35; | ||
44 | } | ||
45 | } | ||
diff --git a/src/Wallabag/CommentBundle/DependencyInjection/Configuration.php b/src/Wallabag/CommentBundle/DependencyInjection/Configuration.php new file mode 100644 index 00000000..bc8d0615 --- /dev/null +++ b/src/Wallabag/CommentBundle/DependencyInjection/Configuration.php | |||
@@ -0,0 +1,20 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CommentBundle\DependencyInjection; | ||
4 | |||
5 | use Symfony\Component\Config\Definition\Builder\TreeBuilder; | ||
6 | use Symfony\Component\Config\Definition\ConfigurationInterface; | ||
7 | |||
8 | class Configuration implements ConfigurationInterface | ||
9 | { | ||
10 | /** | ||
11 | * {@inheritdoc} | ||
12 | */ | ||
13 | public function getConfigTreeBuilder() | ||
14 | { | ||
15 | $treeBuilder = new TreeBuilder(); | ||
16 | $rootNode = $treeBuilder->root('wallabag_comment'); | ||
17 | |||
18 | return $treeBuilder; | ||
19 | } | ||
20 | } | ||
diff --git a/src/Wallabag/CommentBundle/DependencyInjection/WallabagCommentExtension.php b/src/Wallabag/CommentBundle/DependencyInjection/WallabagCommentExtension.php new file mode 100644 index 00000000..b58a17a0 --- /dev/null +++ b/src/Wallabag/CommentBundle/DependencyInjection/WallabagCommentExtension.php | |||
@@ -0,0 +1,23 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CommentBundle\DependencyInjection; | ||
4 | |||
5 | use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
6 | use Symfony\Component\Config\FileLocator; | ||
7 | use Symfony\Component\HttpKernel\DependencyInjection\Extension; | ||
8 | use Symfony\Component\DependencyInjection\Loader; | ||
9 | |||
10 | class WallabagCommentExtension extends Extension | ||
11 | { | ||
12 | /** | ||
13 | * {@inheritdoc} | ||
14 | */ | ||
15 | public function load(array $configs, ContainerBuilder $container) | ||
16 | { | ||
17 | $configuration = new Configuration(); | ||
18 | $config = $this->processConfiguration($configuration, $configs); | ||
19 | |||
20 | $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); | ||
21 | $loader->load('services.yml'); | ||
22 | } | ||
23 | } | ||
diff --git a/src/Wallabag/CommentBundle/Entity/Comment.php b/src/Wallabag/CommentBundle/Entity/Comment.php new file mode 100644 index 00000000..a89a2cb3 --- /dev/null +++ b/src/Wallabag/CommentBundle/Entity/Comment.php | |||
@@ -0,0 +1,270 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CommentBundle\Entity; | ||
4 | |||
5 | use Doctrine\ORM\Mapping as ORM; | ||
6 | use JMS\Serializer\Annotation\ExclusionPolicy; | ||
7 | use JMS\Serializer\Annotation\Exclude; | ||
8 | use JMS\Serializer\Annotation\VirtualProperty; | ||
9 | use JMS\Serializer\Annotation\SerializedName; | ||
10 | use Wallabag\UserBundle\Entity\User; | ||
11 | use Wallabag\CoreBundle\Entity\Entry; | ||
12 | |||
13 | /** | ||
14 | * Comment. | ||
15 | * | ||
16 | * @ORM\Table(name="comment") | ||
17 | * @ORM\Entity(repositoryClass="Wallabag\CommentBundle\Repository\CommentRepository") | ||
18 | * @ORM\HasLifecycleCallbacks() | ||
19 | * @ExclusionPolicy("none") | ||
20 | */ | ||
21 | class Comment | ||
22 | { | ||
23 | /** | ||
24 | * @var int | ||
25 | * | ||
26 | * @ORM\Column(name="id", type="integer") | ||
27 | * @ORM\Id | ||
28 | * @ORM\GeneratedValue(strategy="AUTO") | ||
29 | */ | ||
30 | private $id; | ||
31 | |||
32 | /** | ||
33 | * @var string | ||
34 | * | ||
35 | * @ORM\Column(name="text", type="text") | ||
36 | */ | ||
37 | private $text; | ||
38 | |||
39 | /** | ||
40 | * @var \DateTime | ||
41 | * | ||
42 | * @ORM\Column(name="created_at", type="datetime") | ||
43 | */ | ||
44 | private $createdAt; | ||
45 | |||
46 | /** | ||
47 | * @var \DateTime | ||
48 | * | ||
49 | * @ORM\Column(name="updated_at", type="datetime") | ||
50 | */ | ||
51 | private $updatedAt; | ||
52 | |||
53 | /** | ||
54 | * @var string | ||
55 | * | ||
56 | * @ORM\Column(name="quote", type="string") | ||
57 | */ | ||
58 | private $quote; | ||
59 | |||
60 | /** | ||
61 | * @var array | ||
62 | * | ||
63 | * @ORM\Column(name="ranges", type="array") | ||
64 | */ | ||
65 | private $ranges; | ||
66 | |||
67 | /** | ||
68 | * @Exclude | ||
69 | * | ||
70 | * @ORM\ManyToOne(targetEntity="Wallabag\UserBundle\Entity\User") | ||
71 | */ | ||
72 | private $user; | ||
73 | |||
74 | /** | ||
75 | * @Exclude | ||
76 | * | ||
77 | * @ORM\ManyToOne(targetEntity="Wallabag\CoreBundle\Entity\Entry", inversedBy="comments") | ||
78 | * @ORM\JoinColumn(name="entry_id", referencedColumnName="id") | ||
79 | */ | ||
80 | private $entry; | ||
81 | |||
82 | /* | ||
83 | * @param User $user | ||
84 | */ | ||
85 | public function __construct(\Wallabag\UserBundle\Entity\User $user) | ||
86 | { | ||
87 | $this->user = $user; | ||
88 | } | ||
89 | |||
90 | /** | ||
91 | * Get id. | ||
92 | * | ||
93 | * @return int | ||
94 | */ | ||
95 | public function getId() | ||
96 | { | ||
97 | return $this->id; | ||
98 | } | ||
99 | |||
100 | /** | ||
101 | * Set text. | ||
102 | * | ||
103 | * @param string $text | ||
104 | * | ||
105 | * @return Comment | ||
106 | */ | ||
107 | public function setText($text) | ||
108 | { | ||
109 | $this->text = $text; | ||
110 | |||
111 | return $this; | ||
112 | } | ||
113 | |||
114 | /** | ||
115 | * Get text. | ||
116 | * | ||
117 | * @return string | ||
118 | */ | ||
119 | public function getText() | ||
120 | { | ||
121 | return $this->text; | ||
122 | } | ||
123 | |||
124 | /** | ||
125 | * @ORM\PrePersist | ||
126 | * @ORM\PreUpdate | ||
127 | */ | ||
128 | public function timestamps() | ||
129 | { | ||
130 | if (is_null($this->createdAt)) { | ||
131 | $this->createdAt = new \DateTime(); | ||
132 | } | ||
133 | $this->updatedAt = new \DateTime(); | ||
134 | } | ||
135 | |||
136 | /** | ||
137 | * Get created. | ||
138 | * | ||
139 | * @return \DateTime | ||
140 | */ | ||
141 | public function getCreatedAt() | ||
142 | { | ||
143 | return $this->createdAt; | ||
144 | } | ||
145 | |||
146 | /** | ||
147 | * Get updated. | ||
148 | * | ||
149 | * @return \DateTime | ||
150 | */ | ||
151 | public function getUpdatedAt() | ||
152 | { | ||
153 | return $this->updatedAt; | ||
154 | } | ||
155 | |||
156 | /** | ||
157 | * Get quote. | ||
158 | * | ||
159 | * @return string | ||
160 | */ | ||
161 | public function getQuote() | ||
162 | { | ||
163 | return $this->quote; | ||
164 | } | ||
165 | |||
166 | /** | ||
167 | * Set quote. | ||
168 | * | ||
169 | * @param string $quote | ||
170 | * | ||
171 | * @return Comment | ||
172 | */ | ||
173 | public function setQuote($quote) | ||
174 | { | ||
175 | $this->quote = $quote; | ||
176 | |||
177 | return $this; | ||
178 | } | ||
179 | |||
180 | /** | ||
181 | * Get ranges. | ||
182 | * | ||
183 | * @return array | ||
184 | */ | ||
185 | public function getRanges() | ||
186 | { | ||
187 | return $this->ranges; | ||
188 | } | ||
189 | |||
190 | /** | ||
191 | * Set ranges. | ||
192 | * | ||
193 | * @param array $ranges | ||
194 | * | ||
195 | * @return Comment | ||
196 | */ | ||
197 | public function setRanges($ranges) | ||
198 | { | ||
199 | $this->ranges = $ranges; | ||
200 | |||
201 | return $this; | ||
202 | } | ||
203 | |||
204 | /** | ||
205 | * Set user. | ||
206 | * | ||
207 | * @param string $user | ||
208 | * | ||
209 | * @return Comment | ||
210 | */ | ||
211 | public function setUser($user) | ||
212 | { | ||
213 | $this->user = $user; | ||
214 | |||
215 | return $this; | ||
216 | } | ||
217 | |||
218 | /** | ||
219 | * Get user. | ||
220 | * | ||
221 | * @return string | ||
222 | */ | ||
223 | public function getUser() | ||
224 | { | ||
225 | return $this->user; | ||
226 | } | ||
227 | |||
228 | /** | ||
229 | * @VirtualProperty | ||
230 | * @SerializedName("user") | ||
231 | */ | ||
232 | public function getUserName() | ||
233 | { | ||
234 | return $this->user->getName(); | ||
235 | } | ||
236 | |||
237 | /** | ||
238 | * Set entry. | ||
239 | * | ||
240 | * @param Entry $entry | ||
241 | * | ||
242 | * @return Comment | ||
243 | */ | ||
244 | public function setEntry($entry) | ||
245 | { | ||
246 | $this->entry = $entry; | ||
247 | $entry->setComment($this); | ||
248 | |||
249 | return $this; | ||
250 | } | ||
251 | |||
252 | /** | ||
253 | * Get entry. | ||
254 | * | ||
255 | * @return Entry | ||
256 | */ | ||
257 | public function getEntry() | ||
258 | { | ||
259 | return $this->entry; | ||
260 | } | ||
261 | |||
262 | /** | ||
263 | * @VirtualProperty | ||
264 | * @SerializedName("annotator_schema_version") | ||
265 | */ | ||
266 | public function getVersion() | ||
267 | { | ||
268 | return 'v1.0'; | ||
269 | } | ||
270 | } | ||
diff --git a/src/Wallabag/CommentBundle/Repository/CommentRepository.php b/src/Wallabag/CommentBundle/Repository/CommentRepository.php new file mode 100644 index 00000000..15acffbf --- /dev/null +++ b/src/Wallabag/CommentBundle/Repository/CommentRepository.php | |||
@@ -0,0 +1,94 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CommentBundle\Repository; | ||
4 | |||
5 | use Doctrine\ORM\EntityRepository; | ||
6 | |||
7 | /** | ||
8 | * CommentRepository. | ||
9 | * | ||
10 | * This class was generated by the Doctrine ORM. Add your own custom | ||
11 | * repository methods below. | ||
12 | */ | ||
13 | class CommentRepository extends EntityRepository | ||
14 | { | ||
15 | /** | ||
16 | * Return a query builder to used by other getBuilderFor* method. | ||
17 | * | ||
18 | * @param int $userId | ||
19 | * | ||
20 | * @return QueryBuilder | ||
21 | */ | ||
22 | private function getBuilderByUser($userId) | ||
23 | { | ||
24 | return $this->createQueryBuilder('c') | ||
25 | ->leftJoin('c.user', 'u') | ||
26 | ->andWhere('u.id = :userId')->setParameter('userId', $userId) | ||
27 | ->orderBy('c.id', 'desc') | ||
28 | ; | ||
29 | } | ||
30 | |||
31 | /** | ||
32 | * Retrieves all comments for a user. | ||
33 | * | ||
34 | * @param int $userId | ||
35 | * | ||
36 | * @return QueryBuilder | ||
37 | */ | ||
38 | public function getBuilderForAllByUser($userId) | ||
39 | { | ||
40 | return $this | ||
41 | ->getBuilderByUser($userId) | ||
42 | ; | ||
43 | } | ||
44 | |||
45 | /** | ||
46 | * Get comment for this id. | ||
47 | * | ||
48 | * @param int $commentId | ||
49 | * | ||
50 | * @return array | ||
51 | */ | ||
52 | public function findCommentById($commentId) | ||
53 | { | ||
54 | return $this->createQueryBuilder('c') | ||
55 | ->andWhere('c.id = :commentId')->setParameter('commentId', $commentId) | ||
56 | ->getQuery()->getSingleResult() | ||
57 | ; | ||
58 | } | ||
59 | |||
60 | /** | ||
61 | * Find comments for entry id. | ||
62 | * | ||
63 | * @param int $entryId | ||
64 | * @param int $userId | ||
65 | * | ||
66 | * @return array | ||
67 | */ | ||
68 | public function findCommentsByPageId($entryId, $userId) | ||
69 | { | ||
70 | return $this->createQueryBuilder('c') | ||
71 | ->where('c.entry = :entryId')->setParameter('entryId', $entryId) | ||
72 | ->andwhere('c.user = :userId')->setParameter('userId', $userId) | ||
73 | ->getQuery()->getResult() | ||
74 | ; | ||
75 | } | ||
76 | |||
77 | /** | ||
78 | * Find last comment for a given entry id. Used only for tests. | ||
79 | * | ||
80 | * @param int $entryId | ||
81 | * | ||
82 | * @return array | ||
83 | */ | ||
84 | public function findLastCommentByPageId($entryId, $userId) | ||
85 | { | ||
86 | return $this->createQueryBuilder('c') | ||
87 | ->where('c.entry = :entryId')->setParameter('entryId', $entryId) | ||
88 | ->andwhere('c.user = :userId')->setParameter('userId', $userId) | ||
89 | ->orderBy('c.id', 'DESC') | ||
90 | ->setMaxResults(1) | ||
91 | ->getQuery() | ||
92 | ->getOneOrNullResult(); | ||
93 | } | ||
94 | } | ||
diff --git a/src/Wallabag/CommentBundle/Resources/config/routing.yml b/src/Wallabag/CommentBundle/Resources/config/routing.yml new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/src/Wallabag/CommentBundle/Resources/config/routing.yml | |||
diff --git a/src/Wallabag/CommentBundle/Resources/config/routing_comments.yml b/src/Wallabag/CommentBundle/Resources/config/routing_comments.yml new file mode 100644 index 00000000..1d3893d3 --- /dev/null +++ b/src/Wallabag/CommentBundle/Resources/config/routing_comments.yml | |||
@@ -0,0 +1,4 @@ | |||
1 | annotations: | ||
2 | type: rest | ||
3 | resource: "WallabagCommentBundle:WallabagComment" | ||
4 | name_prefix: annotations_ | ||
diff --git a/src/Wallabag/CommentBundle/Resources/config/services.yml b/src/Wallabag/CommentBundle/Resources/config/services.yml new file mode 100644 index 00000000..0e7972a4 --- /dev/null +++ b/src/Wallabag/CommentBundle/Resources/config/services.yml | |||
@@ -0,0 +1,4 @@ | |||
1 | services: | ||
2 | # wallabag_comment.example: | ||
3 | # class: Wallabag\CommentBundle\Example | ||
4 | # arguments: ["@service_id", "plain_value", %parameter%] | ||
diff --git a/src/Wallabag/CommentBundle/Tests/Controller/CommentControllerTest.php b/src/Wallabag/CommentBundle/Tests/Controller/CommentControllerTest.php new file mode 100644 index 00000000..f8b2a56f --- /dev/null +++ b/src/Wallabag/CommentBundle/Tests/Controller/CommentControllerTest.php | |||
@@ -0,0 +1,81 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CommentBundle\Tests\Controller; | ||
4 | |||
5 | use Wallabag\CommentBundle\Tests\WallabagCommentTestCase; | ||
6 | |||
7 | class CommentControllerTest extends WallabagCommentTestCase | ||
8 | { | ||
9 | public function testGetComments() | ||
10 | { | ||
11 | $comment = $this->client->getContainer() | ||
12 | ->get('doctrine.orm.entity_manager') | ||
13 | ->getRepository('WallabagCommentBundle:Comment') | ||
14 | ->findOneBy(array('user' => 1)); | ||
15 | |||
16 | if (!$comment) { | ||
17 | $this->markTestSkipped('No content found in db.'); | ||
18 | } | ||
19 | $this->logInAs('admin'); | ||
20 | $crawler = $this->client->request('GET', 'annotations/'.$comment->getEntry()->getId().'.json'); | ||
21 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | ||
22 | |||
23 | $content = json_decode($this->client->getResponse()->getContent(), true); | ||
24 | $this->assertEquals(1, $content['total']); | ||
25 | $this->assertEquals($comment->getText(), $content['rows'][0]['text']); | ||
26 | } | ||
27 | |||
28 | public function testSetcomment() | ||
29 | { | ||
30 | $this->logInAs('admin'); | ||
31 | |||
32 | $entry = $this->client->getContainer() | ||
33 | ->get('doctrine.orm.entity_manager') | ||
34 | ->getRepository('WallabagCoreBundle:Entry') | ||
35 | ->findOneBy(array('user' => 1)); | ||
36 | |||
37 | $headers = array('CONTENT_TYPE' => 'application/json'); | ||
38 | $content = json_encode(array( | ||
39 | 'text' => 'my comment', | ||
40 | 'quote' => 'my quote', | ||
41 | 'range' => '[{"start":"","startOffset":24,"end":"","endOffset":31}]', | ||
42 | )); | ||
43 | $crawler = $this->client->request('POST', 'annotations/'.$entry->getId().'.json', array(), array(), $headers, $content); | ||
44 | |||
45 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | ||
46 | |||
47 | $comment = $this->client->getContainer() | ||
48 | ->get('doctrine.orm.entity_manager') | ||
49 | ->getRepository('WallabagCommentBundle:Comment') | ||
50 | ->findLastCommentByPageId($entry->getId(), 1); | ||
51 | |||
52 | $this->assertEquals('my comment', $comment->getText()); | ||
53 | } | ||
54 | |||
55 | public function testEditcomment() | ||
56 | { | ||
57 | $comment = $this->client->getContainer() | ||
58 | ->get('doctrine.orm.entity_manager') | ||
59 | ->getRepository('WallabagCommentBundle:Comment') | ||
60 | ->findOneBy(array('user' => 1)); | ||
61 | |||
62 | $this->logInAs('admin'); | ||
63 | |||
64 | $headers = array('CONTENT_TYPE' => 'application/json'); | ||
65 | $content = json_encode(array( | ||
66 | 'text' => 'a modified comment', | ||
67 | )); | ||
68 | $crawler = $this->client->request('PUT', 'annotations/'.$comment->getId().'.json', array(), array(), $headers, $content); | ||
69 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | ||
70 | |||
71 | $content = json_decode($this->client->getResponse()->getContent(), true); | ||
72 | |||
73 | $this->assertEquals('a modified comment', $content['text']); | ||
74 | |||
75 | $commentUpdated = $this->client->getContainer() | ||
76 | ->get('doctrine.orm.entity_manager') | ||
77 | ->getRepository('WallabagCommentBundle:Comment') | ||
78 | ->findCommentById($comment->getId()); | ||
79 | $this->assertEquals('a modified comment', $commentUpdated->getText()); | ||
80 | } | ||
81 | } | ||
diff --git a/src/Wallabag/CommentBundle/Tests/WallabagCommentTestCase.php b/src/Wallabag/CommentBundle/Tests/WallabagCommentTestCase.php new file mode 100644 index 00000000..f4a2ae6c --- /dev/null +++ b/src/Wallabag/CommentBundle/Tests/WallabagCommentTestCase.php | |||
@@ -0,0 +1,63 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CommentBundle\Tests; | ||
4 | |||
5 | use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | ||
6 | use Symfony\Component\BrowserKit\Cookie; | ||
7 | |||
8 | abstract class WallabagCommentTestCase extends WebTestCase | ||
9 | { | ||
10 | /** | ||
11 | * @var Client | ||
12 | */ | ||
13 | protected $client = null; | ||
14 | |||
15 | /** | ||
16 | * @var \FOS\UserBundle\Model\UserInterface | ||
17 | */ | ||
18 | protected $user; | ||
19 | |||
20 | public function setUp() | ||
21 | { | ||
22 | $this->client = $this->createAuthorizedClient(); | ||
23 | } | ||
24 | |||
25 | public function logInAs($username) | ||
26 | { | ||
27 | $crawler = $this->client->request('GET', '/login'); | ||
28 | $form = $crawler->filter('button[type=submit]')->form(); | ||
29 | $data = array( | ||
30 | '_username' => $username, | ||
31 | '_password' => 'mypassword', | ||
32 | ); | ||
33 | |||
34 | $this->client->submit($form, $data); | ||
35 | } | ||
36 | |||
37 | /** | ||
38 | * @return Client | ||
39 | */ | ||
40 | protected function createAuthorizedClient() | ||
41 | { | ||
42 | $client = static::createClient(); | ||
43 | $container = $client->getContainer(); | ||
44 | |||
45 | /** @var $userManager \FOS\UserBundle\Doctrine\UserManager */ | ||
46 | $userManager = $container->get('fos_user.user_manager'); | ||
47 | /** @var $loginManager \FOS\UserBundle\Security\LoginManager */ | ||
48 | $loginManager = $container->get('fos_user.security.login_manager'); | ||
49 | $firewallName = $container->getParameter('fos_user.firewall_name'); | ||
50 | |||
51 | $this->user = $userManager->findUserBy(array('username' => 'admin')); | ||
52 | $loginManager->loginUser($firewallName, $this->user); | ||
53 | |||
54 | // save the login token into the session and put it in a cookie | ||
55 | $container->get('session')->set('_security_'.$firewallName, serialize($container->get('security.token_storage')->getToken())); | ||
56 | $container->get('session')->save(); | ||
57 | |||
58 | $session = $container->get('session'); | ||
59 | $client->getCookieJar()->set(new Cookie($session->getName(), $session->getId())); | ||
60 | |||
61 | return $client; | ||
62 | } | ||
63 | } | ||
diff --git a/src/Wallabag/CommentBundle/WallabagCommentBundle.php b/src/Wallabag/CommentBundle/WallabagCommentBundle.php new file mode 100644 index 00000000..8150034d --- /dev/null +++ b/src/Wallabag/CommentBundle/WallabagCommentBundle.php | |||
@@ -0,0 +1,9 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CommentBundle; | ||
4 | |||
5 | use Symfony\Component\HttpKernel\Bundle\Bundle; | ||
6 | |||
7 | class WallabagCommentBundle extends Bundle | ||
8 | { | ||
9 | } | ||