aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php
diff options
context:
space:
mode:
authorJeremy Benoist <j0k3r@users.noreply.github.com>2017-01-27 09:34:32 +0100
committerGitHub <noreply@github.com>2017-01-27 09:34:32 +0100
commit6fb06904ecde15b1b07d0a2af945338b416cf0e2 (patch)
treee76f3e8142399316ec5660fab8c646b2c34b8336 /tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php
parent05fa529bcfde01be5d320cb532900d72cf4b0830 (diff)
parent78295b99dd1721c613f1ce52e2debbe6f6db7753 (diff)
downloadwallabag-6fb06904ecde15b1b07d0a2af945338b416cf0e2.tar.gz
wallabag-6fb06904ecde15b1b07d0a2af945338b416cf0e2.tar.zst
wallabag-6fb06904ecde15b1b07d0a2af945338b416cf0e2.zip
Merge pull request #2416 from wallabag/2.2
wallabag 2.2.0
Diffstat (limited to 'tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php')
-rw-r--r--tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php150
1 files changed, 116 insertions, 34 deletions
diff --git a/tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php b/tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php
index 70849f74..81f9e9ec 100644
--- a/tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php
+++ b/tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php
@@ -3,35 +3,80 @@
3namespace Tests\AnnotationBundle\Controller; 3namespace Tests\AnnotationBundle\Controller;
4 4
5use Tests\Wallabag\AnnotationBundle\WallabagAnnotationTestCase; 5use Tests\Wallabag\AnnotationBundle\WallabagAnnotationTestCase;
6use Wallabag\AnnotationBundle\Entity\Annotation;
7use Wallabag\CoreBundle\Entity\Entry;
6 8
7class AnnotationControllerTest extends WallabagAnnotationTestCase 9class AnnotationControllerTest extends WallabagAnnotationTestCase
8{ 10{
9 public function testGetAnnotations() 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()
10 { 17 {
11 $annotation = $this->client->getContainer() 18 return [
12 ->get('doctrine.orm.entity_manager') 19 ['/api/annotations'],
13 ->getRepository('WallabagAnnotationBundle:Annotation') 20 ['annotations'],
14 ->findOneByUsername('admin'); 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');
15 39
16 if (!$annotation) { 40 $annotation = new Annotation($user);
17 $this->markTestSkipped('No content found in db.'); 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');
18 } 50 }
19 51
20 $this->logInAs('admin'); 52 $this->client->request('GET', $prefixUrl.'/'.$entry->getId().'.json');
21 $crawler = $this->client->request('GET', 'annotations/'.$annotation->getEntry()->getId().'.json');
22 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 53 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
23 54
24 $content = json_decode($this->client->getResponse()->getContent(), true); 55 $content = json_decode($this->client->getResponse()->getContent(), true);
25 $this->assertEquals(1, $content['total']); 56 $this->assertGreaterThanOrEqual(1, $content['total']);
26 $this->assertEquals($annotation->getText(), $content['rows'][0]['text']); 57 $this->assertEquals($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();
27 } 63 }
28 64
29 public function testSetAnnotation() 65 /**
66 * Test creating an annotation for an entry.
67 *
68 * @dataProvider dataForEachAnnotations
69 */
70 public function testSetAnnotation($prefixUrl)
30 { 71 {
31 $this->logInAs('admin'); 72 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
32 73
33 $entry = $this->client->getContainer() 74 if ('annotations' === $prefixUrl) {
34 ->get('doctrine.orm.entity_manager') 75 $this->logInAs('admin');
76 }
77
78 /** @var Entry $entry */
79 $entry = $em
35 ->getRepository('WallabagCoreBundle:Entry') 80 ->getRepository('WallabagCoreBundle:Entry')
36 ->findOneByUsernameAndNotArchived('admin'); 81 ->findOneByUsernameAndNotArchived('admin');
37 82
@@ -41,7 +86,7 @@ class AnnotationControllerTest extends WallabagAnnotationTestCase
41 'quote' => 'my quote', 86 'quote' => 'my quote',
42 'ranges' => ['start' => '', 'startOffset' => 24, 'end' => '', 'endOffset' => 31], 87 'ranges' => ['start' => '', 'startOffset' => 24, 'end' => '', 'endOffset' => 31],
43 ]); 88 ]);
44 $crawler = $this->client->request('POST', 'annotations/'.$entry->getId().'.json', [], [], $headers, $content); 89 $this->client->request('POST', $prefixUrl.'/'.$entry->getId().'.json', [], [], $headers, $content);
45 90
46 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 91 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
47 92
@@ -52,6 +97,7 @@ class AnnotationControllerTest extends WallabagAnnotationTestCase
52 $this->assertEquals('my annotation', $content['text']); 97 $this->assertEquals('my annotation', $content['text']);
53 $this->assertEquals('my quote', $content['quote']); 98 $this->assertEquals('my quote', $content['quote']);
54 99
100 /** @var Annotation $annotation */
55 $annotation = $this->client->getContainer() 101 $annotation = $this->client->getContainer()
56 ->get('doctrine.orm.entity_manager') 102 ->get('doctrine.orm.entity_manager')
57 ->getRepository('WallabagAnnotationBundle:Annotation') 103 ->getRepository('WallabagAnnotationBundle:Annotation')
@@ -60,20 +106,35 @@ class AnnotationControllerTest extends WallabagAnnotationTestCase
60 $this->assertEquals('my annotation', $annotation->getText()); 106 $this->assertEquals('my annotation', $annotation->getText());
61 } 107 }
62 108
63 public function testEditAnnotation() 109 /**
110 * Test editing an existing annotation.
111 *
112 * @dataProvider dataForEachAnnotations
113 */
114 public function testEditAnnotation($prefixUrl)
64 { 115 {
65 $annotation = $this->client->getContainer() 116 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
66 ->get('doctrine.orm.entity_manager')
67 ->getRepository('WallabagAnnotationBundle:Annotation')
68 ->findOneByUsername('admin');
69 117
70 $this->logInAs('admin'); 118 $user = $em
119 ->getRepository('WallabagUserBundle:User')
120 ->findOneByUserName('admin');
121 $entry = $em
122 ->getRepository('WallabagCoreBundle:Entry')
123 ->findOneByUsernameAndNotArchived('admin');
124
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();
71 132
72 $headers = ['CONTENT_TYPE' => 'application/json']; 133 $headers = ['CONTENT_TYPE' => 'application/json'];
73 $content = json_encode([ 134 $content = json_encode([
74 'text' => 'a modified annotation', 135 'text' => 'a modified annotation',
75 ]); 136 ]);
76 $crawler = $this->client->request('PUT', 'annotations/'.$annotation->getId().'.json', [], [], $headers, $content); 137 $this->client->request('PUT', $prefixUrl.'/'.$annotation->getId().'.json', [], [], $headers, $content);
77 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 138 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
78 139
79 $content = json_decode($this->client->getResponse()->getContent(), true); 140 $content = json_decode($this->client->getResponse()->getContent(), true);
@@ -83,35 +144,56 @@ class AnnotationControllerTest extends WallabagAnnotationTestCase
83 $this->assertEquals('a modified annotation', $content['text']); 144 $this->assertEquals('a modified annotation', $content['text']);
84 $this->assertEquals('my quote', $content['quote']); 145 $this->assertEquals('my quote', $content['quote']);
85 146
86 $annotationUpdated = $this->client->getContainer() 147 /** @var Annotation $annotationUpdated */
87 ->get('doctrine.orm.entity_manager') 148 $annotationUpdated = $em
88 ->getRepository('WallabagAnnotationBundle:Annotation') 149 ->getRepository('WallabagAnnotationBundle:Annotation')
89 ->findOneById($annotation->getId()); 150 ->findOneById($annotation->getId());
90 $this->assertEquals('a modified annotation', $annotationUpdated->getText()); 151 $this->assertEquals('a modified annotation', $annotationUpdated->getText());
152
153 $em->remove($annotationUpdated);
154 $em->flush();
91 } 155 }
92 156
93 public function testDeleteAnnotation() 157 /**
158 * Test deleting an annotation.
159 *
160 * @dataProvider dataForEachAnnotations
161 */
162 public function testDeleteAnnotation($prefixUrl)
94 { 163 {
95 $annotation = $this->client->getContainer() 164 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
96 ->get('doctrine.orm.entity_manager')
97 ->getRepository('WallabagAnnotationBundle:Annotation')
98 ->findOneByUsername('admin');
99 165
100 $this->logInAs('admin'); 166 $user = $em
167 ->getRepository('WallabagUserBundle:User')
168 ->findOneByUserName('admin');
169 $entry = $em
170 ->getRepository('WallabagCoreBundle:Entry')
171 ->findOneByUsernameAndNotArchived('admin');
172
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 }
101 184
102 $headers = ['CONTENT_TYPE' => 'application/json']; 185 $headers = ['CONTENT_TYPE' => 'application/json'];
103 $content = json_encode([ 186 $content = json_encode([
104 'text' => 'a modified annotation', 187 'text' => 'a modified annotation',
105 ]); 188 ]);
106 $crawler = $this->client->request('DELETE', 'annotations/'.$annotation->getId().'.json', [], [], $headers, $content); 189 $this->client->request('DELETE', $prefixUrl.'/'.$annotation->getId().'.json', [], [], $headers, $content);
107 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 190 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
108 191
109 $content = json_decode($this->client->getResponse()->getContent(), true); 192 $content = json_decode($this->client->getResponse()->getContent(), true);
110 193
111 $this->assertEquals('a modified annotation', $content['text']); 194 $this->assertEquals('This is my annotation /o/', $content['text']);
112 195
113 $annotationDeleted = $this->client->getContainer() 196 $annotationDeleted = $em
114 ->get('doctrine.orm.entity_manager')
115 ->getRepository('WallabagAnnotationBundle:Annotation') 197 ->getRepository('WallabagAnnotationBundle:Annotation')
116 ->findOneById($annotation->getId()); 198 ->findOneById($annotation->getId());
117 199