3 namespace Tests\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 */
103 $annotation = $this->client
->getContainer()
104 ->get('doctrine.orm.entity_manager')
105 ->getRepository('WallabagAnnotationBundle:Annotation')
106 ->findLastAnnotationByPageId($entry->getId(), 1);
108 $this->assertSame('my annotation', $annotation->getText());
112 * @dataProvider dataForEachAnnotations
114 public function testSetAnnotationWithQuoteTooLong($prefixUrl)
116 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
118 if ('annotations' === $prefixUrl) {
119 $this->logInAs('admin');
122 /** @var Entry $entry */
124 ->getRepository('WallabagCoreBundle:Entry')
125 ->findOneByUsernameAndNotArchived('admin');
127 $longQuote = str_repeat('a', 10001);
128 $headers = ['CONTENT_TYPE' => 'application/json'];
129 $content = json_encode([
130 'text' => 'my annotation',
131 'quote' => $longQuote,
133 ['start' => '', 'startOffset' => 24, 'end' => '', 'endOffset' => 31],
136 $this->client
->request('POST', $prefixUrl . '/' . $entry->getId() . '.json', [], [], $headers, $content);
138 $this->assertSame(400, $this->client
->getResponse()->getStatusCode());
142 * Test editing an existing annotation.
144 * @dataProvider dataForEachAnnotations
146 public function testEditAnnotation($prefixUrl)
148 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
151 ->getRepository('WallabagUserBundle:User')
152 ->findOneByUserName('admin');
154 ->getRepository('WallabagCoreBundle:Entry')
155 ->findOneByUsernameAndNotArchived('admin');
157 $annotation = new Annotation($user);
158 $annotation->setEntry($entry);
159 $annotation->setText('This is my annotation /o/');
160 $annotation->setQuote('my quote');
162 $em->persist($annotation);
165 $headers = ['CONTENT_TYPE' => 'application/json'];
166 $content = json_encode([
167 'text' => 'a modified annotation',
169 $this->client
->request('PUT', $prefixUrl . '/' . $annotation->getId() . '.json', [], [], $headers, $content);
170 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
172 $content = json_decode($this->client
->getResponse()->getContent(), true);
174 $this->assertSame('Big boss', $content['user']);
175 $this->assertSame('v1.0', $content['annotator_schema_version']);
176 $this->assertSame('a modified annotation', $content['text']);
177 $this->assertSame('my quote', $content['quote']);
179 /** @var Annotation $annotationUpdated */
180 $annotationUpdated = $em
181 ->getRepository('WallabagAnnotationBundle:Annotation')
182 ->findOneById($annotation->getId());
183 $this->assertSame('a modified annotation', $annotationUpdated->getText());
185 $em->remove($annotationUpdated);
190 * Test deleting an annotation.
192 * @dataProvider dataForEachAnnotations
194 public function testDeleteAnnotation($prefixUrl)
196 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
199 ->getRepository('WallabagUserBundle:User')
200 ->findOneByUserName('admin');
202 ->getRepository('WallabagCoreBundle:Entry')
203 ->findOneByUsernameAndNotArchived('admin');
205 $annotation = new Annotation($user);
206 $annotation->setEntry($entry);
207 $annotation->setText('This is my annotation /o/');
208 $annotation->setQuote('my quote');
210 $em->persist($annotation);
213 if ('annotations' === $prefixUrl) {
214 $this->logInAs('admin');
217 $headers = ['CONTENT_TYPE' => 'application/json'];
218 $content = json_encode([
219 'text' => 'a modified annotation',
221 $this->client
->request('DELETE', $prefixUrl . '/' . $annotation->getId() . '.json', [], [], $headers, $content);
222 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
224 $content = json_decode($this->client
->getResponse()->getContent(), true);
226 $this->assertSame('This is my annotation /o/', $content['text']);
228 $annotationDeleted = $em
229 ->getRepository('WallabagAnnotationBundle:Annotation')
230 ->findOneById($annotation->getId());
232 $this->assertNull($annotationDeleted);