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->assertEquals(200, $this->client
->getResponse()->getStatusCode());
55 $content = json_decode($this->client
->getResponse()->getContent(), true);
56 $this->assertGreaterThanOrEqual(1, $content['total']);
57 $this->assertEquals($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',
87 'ranges' => ['start' => '', 'startOffset' => 24, 'end' => '', 'endOffset' => 31],
89 $this->client
->request('POST', $prefixUrl.'/'.$entry->getId().'.json', [], [], $headers, $content);
91 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
93 $content = json_decode($this->client
->getResponse()->getContent(), true);
95 $this->assertEquals('Big boss', $content['user']);
96 $this->assertEquals('v1.0', $content['annotator_schema_version']);
97 $this->assertEquals('my annotation', $content['text']);
98 $this->assertEquals('my quote', $content['quote']);
100 /** @var Annotation $annotation */
101 $annotation = $this->client
->getContainer()
102 ->get('doctrine.orm.entity_manager')
103 ->getRepository('WallabagAnnotationBundle:Annotation')
104 ->findLastAnnotationByPageId($entry->getId(), 1);
106 $this->assertEquals('my annotation', $annotation->getText());
110 * Test editing an existing annotation.
112 * @dataProvider dataForEachAnnotations
114 public function testEditAnnotation($prefixUrl)
116 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
119 ->getRepository('WallabagUserBundle:User')
120 ->findOneByUserName('admin');
122 ->getRepository('WallabagCoreBundle:Entry')
123 ->findOneByUsernameAndNotArchived('admin');
125 $annotation = new Annotation($user);
126 $annotation->setEntry($entry);
127 $annotation->setText('This is my annotation /o/');
128 $annotation->setQuote('my quote');
130 $em->persist($annotation);
133 $headers = ['CONTENT_TYPE' => 'application/json'];
134 $content = json_encode([
135 'text' => 'a modified annotation',
137 $this->client
->request('PUT', $prefixUrl.'/'.$annotation->getId().'.json', [], [], $headers, $content);
138 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
140 $content = json_decode($this->client
->getResponse()->getContent(), true);
142 $this->assertEquals('Big boss', $content['user']);
143 $this->assertEquals('v1.0', $content['annotator_schema_version']);
144 $this->assertEquals('a modified annotation', $content['text']);
145 $this->assertEquals('my quote', $content['quote']);
147 /** @var Annotation $annotationUpdated */
148 $annotationUpdated = $em
149 ->getRepository('WallabagAnnotationBundle:Annotation')
150 ->findOneById($annotation->getId());
151 $this->assertEquals('a modified annotation', $annotationUpdated->getText());
153 $em->remove($annotationUpdated);
158 * Test deleting an annotation.
160 * @dataProvider dataForEachAnnotations
162 public function testDeleteAnnotation($prefixUrl)
164 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
167 ->getRepository('WallabagUserBundle:User')
168 ->findOneByUserName('admin');
170 ->getRepository('WallabagCoreBundle:Entry')
171 ->findOneByUsernameAndNotArchived('admin');
173 $annotation = new Annotation($user);
174 $annotation->setEntry($entry);
175 $annotation->setText('This is my annotation /o/');
176 $annotation->setQuote('my quote');
178 $em->persist($annotation);
181 if ('annotations' === $prefixUrl) {
182 $this->logInAs('admin');
185 $headers = ['CONTENT_TYPE' => 'application/json'];
186 $content = json_encode([
187 'text' => 'a modified annotation',
189 $this->client
->request('DELETE', $prefixUrl.'/'.$annotation->getId().'.json', [], [], $headers, $content);
190 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
192 $content = json_decode($this->client
->getResponse()->getContent(), true);
194 $this->assertEquals('This is my annotation /o/', $content['text']);
196 $annotationDeleted = $em
197 ->getRepository('WallabagAnnotationBundle:Annotation')
198 ->findOneById($annotation->getId());
200 $this->assertNull($annotationDeleted);