]>
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 | ||
2c3e148b | 110 | /** |
111 | * @dataProvider dataForEachAnnotations | |
112 | */ | |
113 | public function testSetAnnotationWithQuoteTooLong($prefixUrl) | |
114 | { | |
115 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | |
116 | ||
117 | if ('annotations' === $prefixUrl) { | |
118 | $this->logInAs('admin'); | |
119 | } | |
120 | ||
121 | /** @var Entry $entry */ | |
122 | $entry = $em | |
123 | ->getRepository('WallabagCoreBundle:Entry') | |
124 | ->findOneByUsernameAndNotArchived('admin'); | |
125 | ||
126 | $longQuote = str_repeat('a', 10001); | |
127 | $headers = ['CONTENT_TYPE' => 'application/json']; | |
128 | $content = json_encode([ | |
129 | 'text' => 'my annotation', | |
130 | 'quote' => $longQuote, | |
131 | 'ranges' => [ | |
eb570e49 | 132 | ['start' => '', 'startOffset' => 24, 'end' => '', 'endOffset' => 31], |
2c3e148b | 133 | ], |
134 | ]); | |
f808b016 | 135 | $this->client->request('POST', $prefixUrl . '/' . $entry->getId() . '.json', [], [], $headers, $content); |
2c3e148b | 136 | |
f808b016 | 137 | $this->assertSame(400, $this->client->getResponse()->getStatusCode()); |
2c3e148b | 138 | } |
139 | ||
0c271b9e | 140 | /** |
e5edb6e1 | 141 | * Test editing an existing annotation. |
aa474109 JB |
142 | * |
143 | * @dataProvider dataForEachAnnotations | |
0c271b9e | 144 | */ |
aa474109 | 145 | public function testEditAnnotation($prefixUrl) |
f38e03dc | 146 | { |
aa474109 JB |
147 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); |
148 | ||
149 | $user = $em | |
150 | ->getRepository('WallabagUserBundle:User') | |
151 | ->findOneByUserName('admin'); | |
152 | $entry = $em | |
153 | ->getRepository('WallabagCoreBundle:Entry') | |
154 | ->findOneByUsernameAndNotArchived('admin'); | |
f38e03dc | 155 | |
aa474109 JB |
156 | $annotation = new Annotation($user); |
157 | $annotation->setEntry($entry); | |
158 | $annotation->setText('This is my annotation /o/'); | |
159 | $annotation->setQuote('my quote'); | |
160 | ||
161 | $em->persist($annotation); | |
162 | $em->flush(); | |
f38e03dc | 163 | |
4094ea47 JB |
164 | $headers = ['CONTENT_TYPE' => 'application/json']; |
165 | $content = json_encode([ | |
4dc87223 | 166 | 'text' => 'a modified annotation', |
4094ea47 | 167 | ]); |
f808b016 JB |
168 | $this->client->request('PUT', $prefixUrl . '/' . $annotation->getId() . '.json', [], [], $headers, $content); |
169 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
f38e03dc TC |
170 | |
171 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
172 | ||
f808b016 JB |
173 | $this->assertSame('Big boss', $content['user']); |
174 | $this->assertSame('v1.0', $content['annotator_schema_version']); | |
175 | $this->assertSame('a modified annotation', $content['text']); | |
176 | $this->assertSame('my quote', $content['quote']); | |
f38e03dc | 177 | |
0c271b9e | 178 | /** @var Annotation $annotationUpdated */ |
aa474109 | 179 | $annotationUpdated = $em |
4dc87223 | 180 | ->getRepository('WallabagAnnotationBundle:Annotation') |
09d8bb6f | 181 | ->findOneById($annotation->getId()); |
f808b016 | 182 | $this->assertSame('a modified annotation', $annotationUpdated->getText()); |
aa474109 JB |
183 | |
184 | $em->remove($annotationUpdated); | |
185 | $em->flush(); | |
f38e03dc | 186 | } |
09d8bb6f | 187 | |
0c271b9e | 188 | /** |
e5edb6e1 | 189 | * Test deleting an annotation. |
aa474109 JB |
190 | * |
191 | * @dataProvider dataForEachAnnotations | |
0c271b9e | 192 | */ |
aa474109 | 193 | public function testDeleteAnnotation($prefixUrl) |
09d8bb6f | 194 | { |
aa474109 JB |
195 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); |
196 | ||
197 | $user = $em | |
198 | ->getRepository('WallabagUserBundle:User') | |
199 | ->findOneByUserName('admin'); | |
200 | $entry = $em | |
201 | ->getRepository('WallabagCoreBundle:Entry') | |
202 | ->findOneByUsernameAndNotArchived('admin'); | |
09d8bb6f | 203 | |
aa474109 JB |
204 | $annotation = new Annotation($user); |
205 | $annotation->setEntry($entry); | |
206 | $annotation->setText('This is my annotation /o/'); | |
207 | $annotation->setQuote('my quote'); | |
208 | ||
209 | $em->persist($annotation); | |
210 | $em->flush(); | |
211 | ||
212 | if ('annotations' === $prefixUrl) { | |
213 | $this->logInAs('admin'); | |
214 | } | |
09d8bb6f | 215 | |
4094ea47 JB |
216 | $headers = ['CONTENT_TYPE' => 'application/json']; |
217 | $content = json_encode([ | |
09d8bb6f | 218 | 'text' => 'a modified annotation', |
4094ea47 | 219 | ]); |
f808b016 JB |
220 | $this->client->request('DELETE', $prefixUrl . '/' . $annotation->getId() . '.json', [], [], $headers, $content); |
221 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
09d8bb6f JB |
222 | |
223 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
224 | ||
f808b016 | 225 | $this->assertSame('This is my annotation /o/', $content['text']); |
09d8bb6f | 226 | |
aa474109 | 227 | $annotationDeleted = $em |
09d8bb6f JB |
228 | ->getRepository('WallabagAnnotationBundle:Annotation') |
229 | ->findOneById($annotation->getId()); | |
230 | ||
231 | $this->assertNull($annotationDeleted); | |
232 | } | |
f38e03dc | 233 | } |