]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Tests\AnnotationBundle\Controller; | |
4 | ||
5 | use Tests\Wallabag\AnnotationBundle\WallabagAnnotationTestCase; | |
6 | use Wallabag\AnnotationBundle\Entity\Annotation; | |
7 | use Wallabag\CoreBundle\Entity\Entry; | |
8 | ||
9 | class AnnotationControllerTest extends WallabagAnnotationTestCase | |
10 | { | |
11 | /** | |
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). | |
15 | */ | |
16 | public function dataForEachAnnotations() | |
17 | { | |
18 | return [ | |
19 | ['/api/annotations'], | |
20 | ['annotations'], | |
21 | ]; | |
22 | } | |
23 | ||
24 | /** | |
25 | * Test fetching annotations for an entry. | |
26 | * | |
27 | * @dataProvider dataForEachAnnotations | |
28 | */ | |
29 | public function testGetAnnotations($prefixUrl) | |
30 | { | |
31 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | |
32 | ||
33 | $user = $em | |
34 | ->getRepository('WallabagUserBundle:User') | |
35 | ->findOneByUserName('admin'); | |
36 | $entry = $em | |
37 | ->getRepository('WallabagCoreBundle:Entry') | |
38 | ->findOneByUsernameAndNotArchived('admin'); | |
39 | ||
40 | $annotation = new Annotation($user); | |
41 | $annotation->setEntry($entry); | |
42 | $annotation->setText('This is my annotation /o/'); | |
43 | $annotation->setQuote('content'); | |
44 | ||
45 | $em->persist($annotation); | |
46 | $em->flush(); | |
47 | ||
48 | if ('annotations' === $prefixUrl) { | |
49 | $this->logInAs('admin'); | |
50 | } | |
51 | ||
52 | $this->client->request('GET', $prefixUrl.'/'.$entry->getId().'.json'); | |
53 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
54 | ||
55 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
56 | $this->assertGreaterThanOrEqual(1, $content['total']); | |
57 | $this->assertEquals($annotation->getText(), $content['rows'][0]['text']); | |
58 | ||
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); | |
62 | $em->flush(); | |
63 | } | |
64 | ||
65 | /** | |
66 | * Test creating an annotation for an entry. | |
67 | * | |
68 | * @dataProvider dataForEachAnnotations | |
69 | */ | |
70 | public function testSetAnnotation($prefixUrl) | |
71 | { | |
72 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | |
73 | ||
74 | if ('annotations' === $prefixUrl) { | |
75 | $this->logInAs('admin'); | |
76 | } | |
77 | ||
78 | /** @var Entry $entry */ | |
79 | $entry = $em | |
80 | ->getRepository('WallabagCoreBundle:Entry') | |
81 | ->findOneByUsernameAndNotArchived('admin'); | |
82 | ||
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], | |
88 | ]); | |
89 | $this->client->request('POST', $prefixUrl.'/'.$entry->getId().'.json', [], [], $headers, $content); | |
90 | ||
91 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
92 | ||
93 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
94 | ||
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']); | |
99 | ||
100 | /** @var Annotation $annotation */ | |
101 | $annotation = $this->client->getContainer() | |
102 | ->get('doctrine.orm.entity_manager') | |
103 | ->getRepository('WallabagAnnotationBundle:Annotation') | |
104 | ->findLastAnnotationByPageId($entry->getId(), 1); | |
105 | ||
106 | $this->assertEquals('my annotation', $annotation->getText()); | |
107 | } | |
108 | ||
109 | /** | |
110 | * Test editing an existing annotation. | |
111 | * | |
112 | * @dataProvider dataForEachAnnotations | |
113 | */ | |
114 | public function testEditAnnotation($prefixUrl) | |
115 | { | |
116 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | |
117 | ||
118 | $user = $em | |
119 | ->getRepository('WallabagUserBundle:User') | |
120 | ->findOneByUserName('admin'); | |
121 | $entry = $em | |
122 | ->getRepository('WallabagCoreBundle:Entry') | |
123 | ->findOneByUsernameAndNotArchived('admin'); | |
124 | ||
125 | $annotation = new Annotation($user); | |
126 | $annotation->setEntry($entry); | |
127 | $annotation->setText('This is my annotation /o/'); | |
128 | $annotation->setQuote('my quote'); | |
129 | ||
130 | $em->persist($annotation); | |
131 | $em->flush(); | |
132 | ||
133 | $headers = ['CONTENT_TYPE' => 'application/json']; | |
134 | $content = json_encode([ | |
135 | 'text' => 'a modified annotation', | |
136 | ]); | |
137 | $this->client->request('PUT', $prefixUrl.'/'.$annotation->getId().'.json', [], [], $headers, $content); | |
138 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
139 | ||
140 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
141 | ||
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']); | |
146 | ||
147 | /** @var Annotation $annotationUpdated */ | |
148 | $annotationUpdated = $em | |
149 | ->getRepository('WallabagAnnotationBundle:Annotation') | |
150 | ->findOneById($annotation->getId()); | |
151 | $this->assertEquals('a modified annotation', $annotationUpdated->getText()); | |
152 | ||
153 | $em->remove($annotationUpdated); | |
154 | $em->flush(); | |
155 | } | |
156 | ||
157 | /** | |
158 | * Test deleting an annotation. | |
159 | * | |
160 | * @dataProvider dataForEachAnnotations | |
161 | */ | |
162 | public function testDeleteAnnotation($prefixUrl) | |
163 | { | |
164 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | |
165 | ||
166 | $user = $em | |
167 | ->getRepository('WallabagUserBundle:User') | |
168 | ->findOneByUserName('admin'); | |
169 | $entry = $em | |
170 | ->getRepository('WallabagCoreBundle:Entry') | |
171 | ->findOneByUsernameAndNotArchived('admin'); | |
172 | ||
173 | $annotation = new Annotation($user); | |
174 | $annotation->setEntry($entry); | |
175 | $annotation->setText('This is my annotation /o/'); | |
176 | $annotation->setQuote('my quote'); | |
177 | ||
178 | $em->persist($annotation); | |
179 | $em->flush(); | |
180 | ||
181 | if ('annotations' === $prefixUrl) { | |
182 | $this->logInAs('admin'); | |
183 | } | |
184 | ||
185 | $headers = ['CONTENT_TYPE' => 'application/json']; | |
186 | $content = json_encode([ | |
187 | 'text' => 'a modified annotation', | |
188 | ]); | |
189 | $this->client->request('DELETE', $prefixUrl.'/'.$annotation->getId().'.json', [], [], $headers, $content); | |
190 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
191 | ||
192 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
193 | ||
194 | $this->assertEquals('This is my annotation /o/', $content['text']); | |
195 | ||
196 | $annotationDeleted = $em | |
197 | ->getRepository('WallabagAnnotationBundle:Annotation') | |
198 | ->findOneById($annotation->getId()); | |
199 | ||
200 | $this->assertNull($annotationDeleted); | |
201 | } | |
202 | } |