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