]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php
API return an error with empty quote
[github/wallabag/wallabag.git] / tests / Wallabag / AnnotationBundle / Controller / AnnotationControllerTest.php
CommitLineData
f38e03dc
TC
1<?php
2
1e0d8ad7 3namespace Tests\Wallabag\AnnotationBundle\Controller;
f38e03dc 4
23634d5d 5use Tests\Wallabag\AnnotationBundle\WallabagAnnotationTestCase;
0c271b9e
TC
6use Wallabag\AnnotationBundle\Entity\Annotation;
7use Wallabag\CoreBundle\Entity\Entry;
f38e03dc 8
4dc87223 9class 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
8197f082 110 public function testCouldNotSetAnnotationWithoutQuote()
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(400, $this->client->getResponse()->getStatusCode());
130
131 $content = json_decode($this->client->getResponse()->getContent(), true);
132
133 $this->assertCount(
134 1,
135 $content['errors']['children']['quote']['errors'],
136 'The quote field should contains an error'
137 );
138 }
139
2c3e148b 140 /**
141 * @dataProvider dataForEachAnnotations
142 */
143 public function testSetAnnotationWithQuoteTooLong($prefixUrl)
144 {
145 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
146
147 if ('annotations' === $prefixUrl) {
148 $this->logInAs('admin');
149 }
150
151 /** @var Entry $entry */
152 $entry = $em
153 ->getRepository('WallabagCoreBundle:Entry')
154 ->findOneByUsernameAndNotArchived('admin');
155
156 $longQuote = str_repeat('a', 10001);
157 $headers = ['CONTENT_TYPE' => 'application/json'];
158 $content = json_encode([
159 'text' => 'my annotation',
160 'quote' => $longQuote,
161 'ranges' => [
eb570e49 162 ['start' => '', 'startOffset' => 24, 'end' => '', 'endOffset' => 31],
2c3e148b 163 ],
164 ]);
f808b016 165 $this->client->request('POST', $prefixUrl . '/' . $entry->getId() . '.json', [], [], $headers, $content);
2c3e148b 166
f808b016 167 $this->assertSame(400, $this->client->getResponse()->getStatusCode());
2c3e148b 168 }
169
0c271b9e 170 /**
e5edb6e1 171 * Test editing an existing annotation.
aa474109
JB
172 *
173 * @dataProvider dataForEachAnnotations
0c271b9e 174 */
aa474109 175 public function testEditAnnotation($prefixUrl)
f38e03dc 176 {
aa474109
JB
177 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
178
179 $user = $em
180 ->getRepository('WallabagUserBundle:User')
181 ->findOneByUserName('admin');
182 $entry = $em
183 ->getRepository('WallabagCoreBundle:Entry')
184 ->findOneByUsernameAndNotArchived('admin');
f38e03dc 185
aa474109
JB
186 $annotation = new Annotation($user);
187 $annotation->setEntry($entry);
188 $annotation->setText('This is my annotation /o/');
189 $annotation->setQuote('my quote');
190
191 $em->persist($annotation);
192 $em->flush();
f38e03dc 193
4094ea47
JB
194 $headers = ['CONTENT_TYPE' => 'application/json'];
195 $content = json_encode([
4dc87223 196 'text' => 'a modified annotation',
4094ea47 197 ]);
f808b016
JB
198 $this->client->request('PUT', $prefixUrl . '/' . $annotation->getId() . '.json', [], [], $headers, $content);
199 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
f38e03dc
TC
200
201 $content = json_decode($this->client->getResponse()->getContent(), true);
202
f808b016
JB
203 $this->assertSame('Big boss', $content['user']);
204 $this->assertSame('v1.0', $content['annotator_schema_version']);
205 $this->assertSame('a modified annotation', $content['text']);
206 $this->assertSame('my quote', $content['quote']);
f38e03dc 207
0c271b9e 208 /** @var Annotation $annotationUpdated */
aa474109 209 $annotationUpdated = $em
4dc87223 210 ->getRepository('WallabagAnnotationBundle:Annotation')
09d8bb6f 211 ->findOneById($annotation->getId());
f808b016 212 $this->assertSame('a modified annotation', $annotationUpdated->getText());
aa474109
JB
213
214 $em->remove($annotationUpdated);
215 $em->flush();
f38e03dc 216 }
09d8bb6f 217
0c271b9e 218 /**
e5edb6e1 219 * Test deleting an annotation.
aa474109
JB
220 *
221 * @dataProvider dataForEachAnnotations
0c271b9e 222 */
aa474109 223 public function testDeleteAnnotation($prefixUrl)
09d8bb6f 224 {
aa474109
JB
225 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
226
227 $user = $em
228 ->getRepository('WallabagUserBundle:User')
229 ->findOneByUserName('admin');
230 $entry = $em
231 ->getRepository('WallabagCoreBundle:Entry')
232 ->findOneByUsernameAndNotArchived('admin');
09d8bb6f 233
aa474109
JB
234 $annotation = new Annotation($user);
235 $annotation->setEntry($entry);
236 $annotation->setText('This is my annotation /o/');
237 $annotation->setQuote('my quote');
238
239 $em->persist($annotation);
240 $em->flush();
241
242 if ('annotations' === $prefixUrl) {
243 $this->logInAs('admin');
244 }
09d8bb6f 245
4094ea47
JB
246 $headers = ['CONTENT_TYPE' => 'application/json'];
247 $content = json_encode([
09d8bb6f 248 'text' => 'a modified annotation',
4094ea47 249 ]);
f808b016
JB
250 $this->client->request('DELETE', $prefixUrl . '/' . $annotation->getId() . '.json', [], [], $headers, $content);
251 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
09d8bb6f
JB
252
253 $content = json_decode($this->client->getResponse()->getContent(), true);
254
f808b016 255 $this->assertSame('This is my annotation /o/', $content['text']);
09d8bb6f 256
aa474109 257 $annotationDeleted = $em
09d8bb6f
JB
258 ->getRepository('WallabagAnnotationBundle:Annotation')
259 ->findOneById($annotation->getId());
260
261 $this->assertNull($annotationDeleted);
262 }
f38e03dc 263}