]>
Commit | Line | Data |
---|---|---|
f38e03dc TC |
1 | <?php |
2 | ||
1e0d8ad7 | 3 | namespace Tests\Wallabag\AnnotationBundle\Controller; |
f38e03dc | 4 | |
23634d5d | 5 | use Tests\Wallabag\AnnotationBundle\WallabagAnnotationTestCase; |
0c271b9e TC |
6 | use Wallabag\AnnotationBundle\Entity\Annotation; |
7 | use Wallabag\CoreBundle\Entity\Entry; | |
f38e03dc | 8 | |
4dc87223 | 9 | class AnnotationControllerTest extends WallabagAnnotationTestCase |
f38e03dc | 10 | { |
aa474109 JB |
11 | /** |
12 | * This data provider allow to tests annotation from the : | |
13 | * - API POV (when user use the api to manage annotations) | |
f24ea59e | 14 | * - and User POV (when user use the web interface - using javascript - to manage annotations). |
aa474109 JB |
15 | */ |
16 | public function dataForEachAnnotations() | |
17 | { | |
18 | return [ | |
19 | ['/api/annotations'], | |
20 | ['annotations'], | |
21 | ]; | |
22 | } | |
23 | ||
0c271b9e | 24 | /** |
e5edb6e1 | 25 | * Test fetching annotations for an entry. |
aa474109 JB |
26 | * |
27 | * @dataProvider dataForEachAnnotations | |
0c271b9e | 28 | */ |
aa474109 | 29 | public function testGetAnnotations($prefixUrl) |
f38e03dc | 30 | { |
aa474109 JB |
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'); | |
f38e03dc | 44 | |
aa474109 JB |
45 | $em->persist($annotation); |
46 | $em->flush(); | |
47 | ||
48 | if ('annotations' === $prefixUrl) { | |
49 | $this->logInAs('admin'); | |
f38e03dc | 50 | } |
09d8bb6f | 51 | |
f808b016 JB |
52 | $this->client->request('GET', $prefixUrl . '/' . $entry->getId() . '.json'); |
53 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
f38e03dc TC |
54 | |
55 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
aa474109 | 56 | $this->assertGreaterThanOrEqual(1, $content['total']); |
f808b016 | 57 | $this->assertSame($annotation->getText(), $content['rows'][0]['text']); |
aa474109 JB |
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(); | |
f38e03dc TC |
63 | } |
64 | ||
0c271b9e | 65 | /** |
e5edb6e1 | 66 | * Test creating an annotation for an entry. |
aa474109 JB |
67 | * |
68 | * @dataProvider dataForEachAnnotations | |
0c271b9e | 69 | */ |
aa474109 | 70 | public function testSetAnnotation($prefixUrl) |
f38e03dc | 71 | { |
aa474109 JB |
72 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); |
73 | ||
74 | if ('annotations' === $prefixUrl) { | |
75 | $this->logInAs('admin'); | |
76 | } | |
f38e03dc | 77 | |
0c271b9e | 78 | /** @var Entry $entry */ |
aa474109 | 79 | $entry = $em |
f38e03dc | 80 | ->getRepository('WallabagCoreBundle:Entry') |
09d8bb6f | 81 | ->findOneByUsernameAndNotArchived('admin'); |
f38e03dc | 82 | |
4094ea47 JB |
83 | $headers = ['CONTENT_TYPE' => 'application/json']; |
84 | $content = json_encode([ | |
4dc87223 | 85 | 'text' => 'my annotation', |
f38e03dc | 86 | 'quote' => 'my quote', |
2c3e148b | 87 | 'ranges' => [ |
eb570e49 | 88 | ['start' => '', 'startOffset' => 24, 'end' => '', 'endOffset' => 31], |
2c3e148b | 89 | ], |
4094ea47 | 90 | ]); |
f808b016 | 91 | $this->client->request('POST', $prefixUrl . '/' . $entry->getId() . '.json', [], [], $headers, $content); |
f38e03dc | 92 | |
f808b016 | 93 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
f38e03dc | 94 | |
09d8bb6f JB |
95 | $content = json_decode($this->client->getResponse()->getContent(), true); |
96 | ||
f808b016 JB |
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']); | |
09d8bb6f | 101 | |
0c271b9e | 102 | /** @var Annotation $annotation */ |
8f2038e5 | 103 | $annotation = $em |
4dc87223 NL |
104 | ->getRepository('WallabagAnnotationBundle:Annotation') |
105 | ->findLastAnnotationByPageId($entry->getId(), 1); | |
f38e03dc | 106 | |
f808b016 | 107 | $this->assertSame('my annotation', $annotation->getText()); |
f38e03dc TC |
108 | } |
109 | ||
6a0d49ab | 110 | public function testAllowEmptyQuote() |
8197f082 | 111 | { |
112 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | |
113 | ||
114 | /** @var Entry $entry */ | |
115 | $entry = $em | |
116 | ->getRepository('WallabagCoreBundle:Entry') | |
117 | ->findOneByUsernameAndNotArchived('admin'); | |
118 | ||
119 | $headers = ['CONTENT_TYPE' => 'application/json']; | |
120 | $content = json_encode([ | |
121 | 'text' => 'my annotation', | |
122 | 'quote' => null, | |
123 | 'ranges' => [ | |
124 | ['start' => '', 'startOffset' => 24, 'end' => '', 'endOffset' => 31], | |
125 | ], | |
126 | ]); | |
127 | $this->client->request('POST', '/api/annotations/' . $entry->getId() . '.json', [], [], $headers, $content); | |
128 | ||
6a0d49ab | 129 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
8197f082 | 130 | |
131 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
132 | ||
6a0d49ab JB |
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']); | |
137 | } | |
138 | ||
139 | public function testAllowOmmittedQuote() | |
140 | { | |
141 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | |
142 | ||
143 | /** @var Entry $entry */ | |
144 | $entry = $em | |
145 | ->getRepository('WallabagCoreBundle:Entry') | |
146 | ->findOneByUsernameAndNotArchived('admin'); | |
147 | ||
148 | $headers = ['CONTENT_TYPE' => 'application/json']; | |
149 | $content = json_encode([ | |
150 | 'text' => 'my new annotation', | |
151 | 'ranges' => [ | |
152 | ['start' => '', 'startOffset' => 25, 'end' => '', 'endOffset' => 32], | |
153 | ], | |
154 | ]); | |
155 | $this->client->request('POST', '/api/annotations/' . $entry->getId() . '.json', [], [], $headers, $content); | |
156 | ||
157 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
158 | ||
159 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
160 | ||
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']); | |
8197f082 | 165 | } |
166 | ||
2c3e148b | 167 | /** |
168 | * @dataProvider dataForEachAnnotations | |
169 | */ | |
170 | public function testSetAnnotationWithQuoteTooLong($prefixUrl) | |
171 | { | |
172 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | |
173 | ||
174 | if ('annotations' === $prefixUrl) { | |
175 | $this->logInAs('admin'); | |
176 | } | |
177 | ||
178 | /** @var Entry $entry */ | |
179 | $entry = $em | |
180 | ->getRepository('WallabagCoreBundle:Entry') | |
181 | ->findOneByUsernameAndNotArchived('admin'); | |
182 | ||
183 | $longQuote = str_repeat('a', 10001); | |
184 | $headers = ['CONTENT_TYPE' => 'application/json']; | |
185 | $content = json_encode([ | |
186 | 'text' => 'my annotation', | |
187 | 'quote' => $longQuote, | |
188 | 'ranges' => [ | |
eb570e49 | 189 | ['start' => '', 'startOffset' => 24, 'end' => '', 'endOffset' => 31], |
2c3e148b | 190 | ], |
191 | ]); | |
f808b016 | 192 | $this->client->request('POST', $prefixUrl . '/' . $entry->getId() . '.json', [], [], $headers, $content); |
2c3e148b | 193 | |
f808b016 | 194 | $this->assertSame(400, $this->client->getResponse()->getStatusCode()); |
2c3e148b | 195 | } |
196 | ||
0c271b9e | 197 | /** |
e5edb6e1 | 198 | * Test editing an existing annotation. |
aa474109 JB |
199 | * |
200 | * @dataProvider dataForEachAnnotations | |
0c271b9e | 201 | */ |
aa474109 | 202 | public function testEditAnnotation($prefixUrl) |
f38e03dc | 203 | { |
aa474109 JB |
204 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); |
205 | ||
206 | $user = $em | |
207 | ->getRepository('WallabagUserBundle:User') | |
208 | ->findOneByUserName('admin'); | |
209 | $entry = $em | |
210 | ->getRepository('WallabagCoreBundle:Entry') | |
211 | ->findOneByUsernameAndNotArchived('admin'); | |
f38e03dc | 212 | |
aa474109 JB |
213 | $annotation = new Annotation($user); |
214 | $annotation->setEntry($entry); | |
215 | $annotation->setText('This is my annotation /o/'); | |
216 | $annotation->setQuote('my quote'); | |
217 | ||
218 | $em->persist($annotation); | |
219 | $em->flush(); | |
f38e03dc | 220 | |
4094ea47 JB |
221 | $headers = ['CONTENT_TYPE' => 'application/json']; |
222 | $content = json_encode([ | |
4dc87223 | 223 | 'text' => 'a modified annotation', |
4094ea47 | 224 | ]); |
f808b016 JB |
225 | $this->client->request('PUT', $prefixUrl . '/' . $annotation->getId() . '.json', [], [], $headers, $content); |
226 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
f38e03dc TC |
227 | |
228 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
229 | ||
f808b016 JB |
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']); | |
f38e03dc | 234 | |
0c271b9e | 235 | /** @var Annotation $annotationUpdated */ |
aa474109 | 236 | $annotationUpdated = $em |
4dc87223 | 237 | ->getRepository('WallabagAnnotationBundle:Annotation') |
09d8bb6f | 238 | ->findOneById($annotation->getId()); |
f808b016 | 239 | $this->assertSame('a modified annotation', $annotationUpdated->getText()); |
aa474109 JB |
240 | |
241 | $em->remove($annotationUpdated); | |
242 | $em->flush(); | |
f38e03dc | 243 | } |
09d8bb6f | 244 | |
0c271b9e | 245 | /** |
e5edb6e1 | 246 | * Test deleting an annotation. |
aa474109 JB |
247 | * |
248 | * @dataProvider dataForEachAnnotations | |
0c271b9e | 249 | */ |
aa474109 | 250 | public function testDeleteAnnotation($prefixUrl) |
09d8bb6f | 251 | { |
aa474109 JB |
252 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); |
253 | ||
254 | $user = $em | |
255 | ->getRepository('WallabagUserBundle:User') | |
256 | ->findOneByUserName('admin'); | |
257 | $entry = $em | |
258 | ->getRepository('WallabagCoreBundle:Entry') | |
259 | ->findOneByUsernameAndNotArchived('admin'); | |
09d8bb6f | 260 | |
aa474109 JB |
261 | $annotation = new Annotation($user); |
262 | $annotation->setEntry($entry); | |
263 | $annotation->setText('This is my annotation /o/'); | |
264 | $annotation->setQuote('my quote'); | |
265 | ||
266 | $em->persist($annotation); | |
267 | $em->flush(); | |
268 | ||
269 | if ('annotations' === $prefixUrl) { | |
270 | $this->logInAs('admin'); | |
271 | } | |
09d8bb6f | 272 | |
4094ea47 JB |
273 | $headers = ['CONTENT_TYPE' => 'application/json']; |
274 | $content = json_encode([ | |
09d8bb6f | 275 | 'text' => 'a modified annotation', |
4094ea47 | 276 | ]); |
f808b016 JB |
277 | $this->client->request('DELETE', $prefixUrl . '/' . $annotation->getId() . '.json', [], [], $headers, $content); |
278 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
09d8bb6f JB |
279 | |
280 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
281 | ||
f808b016 | 282 | $this->assertSame('This is my annotation /o/', $content['text']); |
09d8bb6f | 283 | |
aa474109 | 284 | $annotationDeleted = $em |
09d8bb6f JB |
285 | ->getRepository('WallabagAnnotationBundle:Annotation') |
286 | ->findOneById($annotation->getId()); | |
287 | ||
288 | $this->assertNull($annotationDeleted); | |
289 | } | |
f38e03dc | 290 | } |