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