3 namespace Tests\Wallabag\AnnotationBundle\Controller
;
5 use Tests\Wallabag\AnnotationBundle\WallabagAnnotationTestCase
;
6 use Wallabag\AnnotationBundle\Entity\Annotation
;
7 use Wallabag\CoreBundle\Entity\Entry
;
9 class AnnotationControllerTest
extends WallabagAnnotationTestCase
12 * This data provider allow to tests annotation from the :
13 * - API POV (when user use the api to manage annotations)
14 * - and User POV (when user use the web interface - using javascript - to manage annotations).
16 public function dataForEachAnnotations()
25 * Test fetching annotations for an entry.
27 * @dataProvider dataForEachAnnotations
29 public function testGetAnnotations($prefixUrl)
31 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
34 ->getRepository('WallabagUserBundle:User')
35 ->findOneByUserName('admin');
37 ->getRepository('WallabagCoreBundle:Entry')
38 ->findOneByUsernameAndNotArchived('admin');
40 $annotation = new Annotation($user);
41 $annotation->setEntry($entry);
42 $annotation->setText('This is my annotation /o/');
43 $annotation->setQuote('content');
45 $em->persist($annotation);
48 if ('annotations' === $prefixUrl) {
49 $this->logInAs('admin');
52 $this->client
->request('GET', $prefixUrl . '/' . $entry->getId() . '.json');
53 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
55 $content = json_decode($this->client
->getResponse()->getContent(), true);
56 $this->assertGreaterThanOrEqual(1, $content['total']);
57 $this->assertSame($annotation->getText(), $content['rows'][0]['text']);
59 // we need to re-fetch the annotation becase after the flush, it has been "detached" from the entity manager
60 $annotation = $em->getRepository('WallabagAnnotationBundle:Annotation')->findAnnotationById($annotation->getId());
61 $em->remove($annotation);
66 * Test creating an annotation for an entry.
68 * @dataProvider dataForEachAnnotations
70 public function testSetAnnotation($prefixUrl)
72 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
74 if ('annotations' === $prefixUrl) {
75 $this->logInAs('admin');
78 /** @var Entry $entry */
80 ->getRepository('WallabagCoreBundle:Entry')
81 ->findOneByUsernameAndNotArchived('admin');
83 $headers = ['CONTENT_TYPE' => 'application/json'];
84 $content = json_encode([
85 'text' => 'my annotation',
86 'quote' => 'my quote',
88 ['start' => '', 'startOffset' => 24, 'end' => '', 'endOffset' => 31],
91 $this->client
->request('POST', $prefixUrl . '/' . $entry->getId() . '.json', [], [], $headers, $content);
93 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
95 $content = json_decode($this->client
->getResponse()->getContent(), true);
97 $this->assertSame('Big boss', $content['user']);
98 $this->assertSame('v1.0', $content['annotator_schema_version']);
99 $this->assertSame('my annotation', $content['text']);
100 $this->assertSame('my quote', $content['quote']);
102 /** @var Annotation $annotation */
104 ->getRepository('WallabagAnnotationBundle:Annotation')
105 ->findLastAnnotationByPageId($entry->getId(), 1);
107 $this->assertSame('my annotation', $annotation->getText());
110 public function testCouldNotSetAnnotationWithoutQuote()
112 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
114 /** @var Entry $entry */
116 ->getRepository('WallabagCoreBundle:Entry')
117 ->findOneByUsernameAndNotArchived('admin');
119 $headers = ['CONTENT_TYPE' => 'application/json'];
120 $content = json_encode([
121 'text' => 'my annotation',
124 ['start' => '', 'startOffset' => 24, 'end' => '', 'endOffset' => 31],
127 $this->client
->request('POST', '/api/annotations/' . $entry->getId() . '.json', [], [], $headers, $content);
129 $this->assertSame(400, $this->client
->getResponse()->getStatusCode());
131 $content = json_decode($this->client
->getResponse()->getContent(), true);
135 $content['errors']['children']['quote']['errors'],
136 'The quote field should contains an error'
141 * @dataProvider dataForEachAnnotations
143 public function testSetAnnotationWithQuoteTooLong($prefixUrl)
145 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
147 if ('annotations' === $prefixUrl) {
148 $this->logInAs('admin');
151 /** @var Entry $entry */
153 ->getRepository('WallabagCoreBundle:Entry')
154 ->findOneByUsernameAndNotArchived('admin');
156 $longQuote = str_repeat('a', 10001);
157 $headers = ['CONTENT_TYPE' => 'application/json'];
158 $content = json_encode([
159 'text' => 'my annotation',
160 'quote' => $longQuote,
162 ['start' => '', 'startOffset' => 24, 'end' => '', 'endOffset' => 31],
165 $this->client
->request('POST', $prefixUrl . '/' . $entry->getId() . '.json', [], [], $headers, $content);
167 $this->assertSame(400, $this->client
->getResponse()->getStatusCode());
171 * Test editing an existing annotation.
173 * @dataProvider dataForEachAnnotations
175 public function testEditAnnotation($prefixUrl)
177 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
180 ->getRepository('WallabagUserBundle:User')
181 ->findOneByUserName('admin');
183 ->getRepository('WallabagCoreBundle:Entry')
184 ->findOneByUsernameAndNotArchived('admin');
186 $annotation = new Annotation($user);
187 $annotation->setEntry($entry);
188 $annotation->setText('This is my annotation /o/');
189 $annotation->setQuote('my quote');
191 $em->persist($annotation);
194 $headers = ['CONTENT_TYPE' => 'application/json'];
195 $content = json_encode([
196 'text' => 'a modified annotation',
198 $this->client
->request('PUT', $prefixUrl . '/' . $annotation->getId() . '.json', [], [], $headers, $content);
199 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
201 $content = json_decode($this->client
->getResponse()->getContent(), true);
203 $this->assertSame('Big boss', $content['user']);
204 $this->assertSame('v1.0', $content['annotator_schema_version']);
205 $this->assertSame('a modified annotation', $content['text']);
206 $this->assertSame('my quote', $content['quote']);
208 /** @var Annotation $annotationUpdated */
209 $annotationUpdated = $em
210 ->getRepository('WallabagAnnotationBundle:Annotation')
211 ->findOneById($annotation->getId());
212 $this->assertSame('a modified annotation', $annotationUpdated->getText());
214 $em->remove($annotationUpdated);
219 * Test deleting an annotation.
221 * @dataProvider dataForEachAnnotations
223 public function testDeleteAnnotation($prefixUrl)
225 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
228 ->getRepository('WallabagUserBundle:User')
229 ->findOneByUserName('admin');
231 ->getRepository('WallabagCoreBundle:Entry')
232 ->findOneByUsernameAndNotArchived('admin');
234 $annotation = new Annotation($user);
235 $annotation->setEntry($entry);
236 $annotation->setText('This is my annotation /o/');
237 $annotation->setQuote('my quote');
239 $em->persist($annotation);
242 if ('annotations' === $prefixUrl) {
243 $this->logInAs('admin');
246 $headers = ['CONTENT_TYPE' => 'application/json'];
247 $content = json_encode([
248 'text' => 'a modified annotation',
250 $this->client
->request('DELETE', $prefixUrl . '/' . $annotation->getId() . '.json', [], [], $headers, $content);
251 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
253 $content = json_decode($this->client
->getResponse()->getContent(), true);
255 $this->assertSame('This is my annotation /o/', $content['text']);
257 $annotationDeleted = $em
258 ->getRepository('WallabagAnnotationBundle:Annotation')
259 ->findOneById($annotation->getId());
261 $this->assertNull($annotationDeleted);