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 testAllowEmptyQuote()
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(200, $this->client
->getResponse()->getStatusCode());
131 $content = json_decode($this->client
->getResponse()->getContent(), true);
133 $this->assertSame('Big boss', $content['user']);
134 $this->assertSame('v1.0', $content['annotator_schema_version']);
135 $this->assertSame('my annotation', $content['text']);
136 $this->assertSame('', $content['quote']);
139 public function testAllowOmmittedQuote()
141 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
143 /** @var Entry $entry */
145 ->getRepository('WallabagCoreBundle:Entry')
146 ->findOneByUsernameAndNotArchived('admin');
148 $headers = ['CONTENT_TYPE' => 'application/json'];
149 $content = json_encode([
150 'text' => 'my new annotation',
152 ['start' => '', 'startOffset' => 25, 'end' => '', 'endOffset' => 32],
155 $this->client
->request('POST', '/api/annotations/' . $entry->getId() . '.json', [], [], $headers, $content);
157 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
159 $content = json_decode($this->client
->getResponse()->getContent(), true);
161 $this->assertSame('Big boss', $content['user']);
162 $this->assertSame('v1.0', $content['annotator_schema_version']);
163 $this->assertSame('my new annotation', $content['text']);
164 $this->assertSame('', $content['quote']);
168 * @dataProvider dataForEachAnnotations
170 public function testSetAnnotationWithQuoteTooLong($prefixUrl)
172 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
174 if ('annotations' === $prefixUrl) {
175 $this->logInAs('admin');
178 /** @var Entry $entry */
180 ->getRepository('WallabagCoreBundle:Entry')
181 ->findOneByUsernameAndNotArchived('admin');
183 $longQuote = str_repeat('a', 10001);
184 $headers = ['CONTENT_TYPE' => 'application/json'];
185 $content = json_encode([
186 'text' => 'my annotation',
187 'quote' => $longQuote,
189 ['start' => '', 'startOffset' => 24, 'end' => '', 'endOffset' => 31],
192 $this->client
->request('POST', $prefixUrl . '/' . $entry->getId() . '.json', [], [], $headers, $content);
194 $this->assertSame(400, $this->client
->getResponse()->getStatusCode());
198 * Test editing an existing annotation.
200 * @dataProvider dataForEachAnnotations
202 public function testEditAnnotation($prefixUrl)
204 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
207 ->getRepository('WallabagUserBundle:User')
208 ->findOneByUserName('admin');
210 ->getRepository('WallabagCoreBundle:Entry')
211 ->findOneByUsernameAndNotArchived('admin');
213 $annotation = new Annotation($user);
214 $annotation->setEntry($entry);
215 $annotation->setText('This is my annotation /o/');
216 $annotation->setQuote('my quote');
218 $em->persist($annotation);
221 $headers = ['CONTENT_TYPE' => 'application/json'];
222 $content = json_encode([
223 'text' => 'a modified annotation',
225 $this->client
->request('PUT', $prefixUrl . '/' . $annotation->getId() . '.json', [], [], $headers, $content);
226 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
228 $content = json_decode($this->client
->getResponse()->getContent(), true);
230 $this->assertSame('Big boss', $content['user']);
231 $this->assertSame('v1.0', $content['annotator_schema_version']);
232 $this->assertSame('a modified annotation', $content['text']);
233 $this->assertSame('my quote', $content['quote']);
235 /** @var Annotation $annotationUpdated */
236 $annotationUpdated = $em
237 ->getRepository('WallabagAnnotationBundle:Annotation')
238 ->findOneById($annotation->getId());
239 $this->assertSame('a modified annotation', $annotationUpdated->getText());
241 $em->remove($annotationUpdated);
246 * Test deleting an annotation.
248 * @dataProvider dataForEachAnnotations
250 public function testDeleteAnnotation($prefixUrl)
252 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
255 ->getRepository('WallabagUserBundle:User')
256 ->findOneByUserName('admin');
258 ->getRepository('WallabagCoreBundle:Entry')
259 ->findOneByUsernameAndNotArchived('admin');
261 $annotation = new Annotation($user);
262 $annotation->setEntry($entry);
263 $annotation->setText('This is my annotation /o/');
264 $annotation->setQuote('my quote');
266 $em->persist($annotation);
269 if ('annotations' === $prefixUrl) {
270 $this->logInAs('admin');
273 $headers = ['CONTENT_TYPE' => 'application/json'];
274 $content = json_encode([
275 'text' => 'a modified annotation',
277 $this->client
->request('DELETE', $prefixUrl . '/' . $annotation->getId() . '.json', [], [], $headers, $content);
278 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
280 $content = json_decode($this->client
->getResponse()->getContent(), true);
282 $this->assertSame('This is my annotation /o/', $content['text']);
284 $annotationDeleted = $em
285 ->getRepository('WallabagAnnotationBundle:Annotation')
286 ->findOneById($annotation->getId());
288 $this->assertNull($annotationDeleted);