]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php
Added relation between API Client and User
[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)
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
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
aa474109 52 $this->client->request('GET', $prefixUrl.'/'.$entry->getId().'.json');
f38e03dc
TC
53 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
54
55 $content = json_decode($this->client->getResponse()->getContent(), true);
aa474109 56 $this->assertGreaterThanOrEqual(1, $content['total']);
4dc87223 57 $this->assertEquals($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',
4094ea47
JB
87 'ranges' => ['start' => '', 'startOffset' => 24, 'end' => '', 'endOffset' => 31],
88 ]);
aa474109 89 $this->client->request('POST', $prefixUrl.'/'.$entry->getId().'.json', [], [], $headers, $content);
f38e03dc
TC
90
91 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
92
09d8bb6f
JB
93 $content = json_decode($this->client->getResponse()->getContent(), true);
94
95 $this->assertEquals('Big boss', $content['user']);
96 $this->assertEquals('v1.0', $content['annotator_schema_version']);
97 $this->assertEquals('my annotation', $content['text']);
98 $this->assertEquals('my quote', $content['quote']);
99
0c271b9e 100 /** @var Annotation $annotation */
4dc87223 101 $annotation = $this->client->getContainer()
f38e03dc 102 ->get('doctrine.orm.entity_manager')
4dc87223
NL
103 ->getRepository('WallabagAnnotationBundle:Annotation')
104 ->findLastAnnotationByPageId($entry->getId(), 1);
f38e03dc 105
4dc87223 106 $this->assertEquals('my annotation', $annotation->getText());
f38e03dc
TC
107 }
108
0c271b9e 109 /**
e5edb6e1 110 * Test editing an existing annotation.
aa474109
JB
111 *
112 * @dataProvider dataForEachAnnotations
0c271b9e 113 */
aa474109 114 public function testEditAnnotation($prefixUrl)
f38e03dc 115 {
aa474109
JB
116 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
117
118 $user = $em
119 ->getRepository('WallabagUserBundle:User')
120 ->findOneByUserName('admin');
121 $entry = $em
122 ->getRepository('WallabagCoreBundle:Entry')
123 ->findOneByUsernameAndNotArchived('admin');
f38e03dc 124
aa474109
JB
125 $annotation = new Annotation($user);
126 $annotation->setEntry($entry);
127 $annotation->setText('This is my annotation /o/');
128 $annotation->setQuote('my quote');
129
130 $em->persist($annotation);
131 $em->flush();
f38e03dc 132
4094ea47
JB
133 $headers = ['CONTENT_TYPE' => 'application/json'];
134 $content = json_encode([
4dc87223 135 'text' => 'a modified annotation',
4094ea47 136 ]);
aa474109 137 $this->client->request('PUT', $prefixUrl.'/'.$annotation->getId().'.json', [], [], $headers, $content);
f38e03dc
TC
138 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
139
140 $content = json_decode($this->client->getResponse()->getContent(), true);
141
09d8bb6f
JB
142 $this->assertEquals('Big boss', $content['user']);
143 $this->assertEquals('v1.0', $content['annotator_schema_version']);
4dc87223 144 $this->assertEquals('a modified annotation', $content['text']);
b95ffda2 145 $this->assertEquals('my quote', $content['quote']);
f38e03dc 146
0c271b9e 147 /** @var Annotation $annotationUpdated */
aa474109 148 $annotationUpdated = $em
4dc87223 149 ->getRepository('WallabagAnnotationBundle:Annotation')
09d8bb6f 150 ->findOneById($annotation->getId());
4dc87223 151 $this->assertEquals('a modified annotation', $annotationUpdated->getText());
aa474109
JB
152
153 $em->remove($annotationUpdated);
154 $em->flush();
f38e03dc 155 }
09d8bb6f 156
0c271b9e 157 /**
e5edb6e1 158 * Test deleting an annotation.
aa474109
JB
159 *
160 * @dataProvider dataForEachAnnotations
0c271b9e 161 */
aa474109 162 public function testDeleteAnnotation($prefixUrl)
09d8bb6f 163 {
aa474109
JB
164 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
165
166 $user = $em
167 ->getRepository('WallabagUserBundle:User')
168 ->findOneByUserName('admin');
169 $entry = $em
170 ->getRepository('WallabagCoreBundle:Entry')
171 ->findOneByUsernameAndNotArchived('admin');
09d8bb6f 172
aa474109
JB
173 $annotation = new Annotation($user);
174 $annotation->setEntry($entry);
175 $annotation->setText('This is my annotation /o/');
176 $annotation->setQuote('my quote');
177
178 $em->persist($annotation);
179 $em->flush();
180
181 if ('annotations' === $prefixUrl) {
182 $this->logInAs('admin');
183 }
09d8bb6f 184
4094ea47
JB
185 $headers = ['CONTENT_TYPE' => 'application/json'];
186 $content = json_encode([
09d8bb6f 187 'text' => 'a modified annotation',
4094ea47 188 ]);
aa474109 189 $this->client->request('DELETE', $prefixUrl.'/'.$annotation->getId().'.json', [], [], $headers, $content);
09d8bb6f
JB
190 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
191
192 $content = json_decode($this->client->getResponse()->getContent(), true);
193
aa474109 194 $this->assertEquals('This is my annotation /o/', $content['text']);
09d8bb6f 195
aa474109 196 $annotationDeleted = $em
09d8bb6f
JB
197 ->getRepository('WallabagAnnotationBundle:Annotation')
198 ->findOneById($annotation->getId());
199
200 $this->assertNull($annotationDeleted);
201 }
f38e03dc 202}