]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php
Fix tests
[github/wallabag/wallabag.git] / tests / Wallabag / AnnotationBundle / Controller / AnnotationControllerTest.php
1 <?php
2
3 namespace Tests\Wallabag\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->assertSame(200, $this->client->getResponse()->getStatusCode());
54
55 $content = json_decode($this->client->getResponse()->getContent(), true);
56 $this->assertGreaterThanOrEqual(1, $content['total']);
57 $this->assertSame($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' => [
88 ['start' => '', 'startOffset' => 24, 'end' => '', 'endOffset' => 31],
89 ],
90 ]);
91 $this->client->request('POST', $prefixUrl . '/' . $entry->getId() . '.json', [], [], $headers, $content);
92
93 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
94
95 $content = json_decode($this->client->getResponse()->getContent(), true);
96
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']);
101
102 /** @var Annotation $annotation */
103 $annotation = $em
104 ->getRepository('WallabagAnnotationBundle:Annotation')
105 ->findLastAnnotationByPageId($entry->getId(), 1);
106
107 $this->assertSame('my annotation', $annotation->getText());
108 }
109
110 public function testAllowEmptyQuote()
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
129 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
130
131 $content = json_decode($this->client->getResponse()->getContent(), true);
132
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']);
165 }
166
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' => [
189 ['start' => '', 'startOffset' => 24, 'end' => '', 'endOffset' => 31],
190 ],
191 ]);
192 $this->client->request('POST', $prefixUrl . '/' . $entry->getId() . '.json', [], [], $headers, $content);
193
194 $this->assertSame(400, $this->client->getResponse()->getStatusCode());
195 }
196
197 /**
198 * Test editing an existing annotation.
199 *
200 * @dataProvider dataForEachAnnotations
201 */
202 public function testEditAnnotation($prefixUrl)
203 {
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');
212
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();
220
221 $headers = ['CONTENT_TYPE' => 'application/json'];
222 $content = json_encode([
223 'text' => 'a modified annotation',
224 ]);
225 $this->client->request('PUT', $prefixUrl . '/' . $annotation->getId() . '.json', [], [], $headers, $content);
226 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
227
228 $content = json_decode($this->client->getResponse()->getContent(), true);
229
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']);
234
235 /** @var Annotation $annotationUpdated */
236 $annotationUpdated = $em
237 ->getRepository('WallabagAnnotationBundle:Annotation')
238 ->findOneById($annotation->getId());
239 $this->assertSame('a modified annotation', $annotationUpdated->getText());
240
241 $em->remove($annotationUpdated);
242 $em->flush();
243 }
244
245 /**
246 * Test deleting an annotation.
247 *
248 * @dataProvider dataForEachAnnotations
249 */
250 public function testDeleteAnnotation($prefixUrl)
251 {
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');
260
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 }
272
273 $headers = ['CONTENT_TYPE' => 'application/json'];
274 $content = json_encode([
275 'text' => 'a modified annotation',
276 ]);
277 $this->client->request('DELETE', $prefixUrl . '/' . $annotation->getId() . '.json', [], [], $headers, $content);
278 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
279
280 $content = json_decode($this->client->getResponse()->getContent(), true);
281
282 $this->assertSame('This is my annotation /o/', $content['text']);
283
284 $annotationDeleted = $em
285 ->getRepository('WallabagAnnotationBundle:Annotation')
286 ->findOneById($annotation->getId());
287
288 $this->assertNull($annotationDeleted);
289 }
290 }