diff options
author | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2016-10-24 10:06:12 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-24 10:06:12 +0200 |
commit | a1c18418288a9521c980c96fd5defffc757a81c6 (patch) | |
tree | 7331a401bec4f3a4d2a8afc6462ac4fe2eea32f9 /tests/Wallabag | |
parent | 1e7807e703fd75eceff3df512308b56e452d6dc6 (diff) | |
parent | aa4741091f3f2cc7e4a7ef061a04678597ddeca8 (diff) | |
download | wallabag-a1c18418288a9521c980c96fd5defffc757a81c6.tar.gz wallabag-a1c18418288a9521c980c96fd5defffc757a81c6.tar.zst wallabag-a1c18418288a9521c980c96fd5defffc757a81c6.zip |
Merge pull request #1890 from wallabag/v2-api-annotation-switched
bring annotations to API
Diffstat (limited to 'tests/Wallabag')
-rw-r--r-- | tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php | 150 | ||||
-rw-r--r-- | tests/Wallabag/AnnotationBundle/WallabagAnnotationTestCase.php | 6 |
2 files changed, 119 insertions, 37 deletions
diff --git a/tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php b/tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php index 70849f74..cee0b847 100644 --- a/tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php +++ b/tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php | |||
@@ -3,35 +3,80 @@ | |||
3 | namespace Tests\AnnotationBundle\Controller; | 3 | namespace Tests\AnnotationBundle\Controller; |
4 | 4 | ||
5 | use Tests\Wallabag\AnnotationBundle\WallabagAnnotationTestCase; | 5 | use Tests\Wallabag\AnnotationBundle\WallabagAnnotationTestCase; |
6 | use Wallabag\AnnotationBundle\Entity\Annotation; | ||
7 | use Wallabag\CoreBundle\Entity\Entry; | ||
6 | 8 | ||
7 | class AnnotationControllerTest extends WallabagAnnotationTestCase | 9 | class 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 | ||
diff --git a/tests/Wallabag/AnnotationBundle/WallabagAnnotationTestCase.php b/tests/Wallabag/AnnotationBundle/WallabagAnnotationTestCase.php index 82790a5c..ef3f1324 100644 --- a/tests/Wallabag/AnnotationBundle/WallabagAnnotationTestCase.php +++ b/tests/Wallabag/AnnotationBundle/WallabagAnnotationTestCase.php | |||
@@ -8,7 +8,7 @@ use Symfony\Component\BrowserKit\Cookie; | |||
8 | abstract class WallabagAnnotationTestCase extends WebTestCase | 8 | abstract class WallabagAnnotationTestCase extends WebTestCase |
9 | { | 9 | { |
10 | /** | 10 | /** |
11 | * @var Client | 11 | * @var \Symfony\Bundle\FrameworkBundle\Client |
12 | */ | 12 | */ |
13 | protected $client = null; | 13 | protected $client = null; |
14 | 14 | ||
@@ -35,7 +35,7 @@ abstract class WallabagAnnotationTestCase extends WebTestCase | |||
35 | } | 35 | } |
36 | 36 | ||
37 | /** | 37 | /** |
38 | * @return Client | 38 | * @return \Symfony\Bundle\FrameworkBundle\Client |
39 | */ | 39 | */ |
40 | protected function createAuthorizedClient() | 40 | protected function createAuthorizedClient() |
41 | { | 41 | { |
@@ -49,7 +49,7 @@ abstract class WallabagAnnotationTestCase extends WebTestCase | |||
49 | $firewallName = $container->getParameter('fos_user.firewall_name'); | 49 | $firewallName = $container->getParameter('fos_user.firewall_name'); |
50 | 50 | ||
51 | $this->user = $userManager->findUserBy(['username' => 'admin']); | 51 | $this->user = $userManager->findUserBy(['username' => 'admin']); |
52 | $loginManager->loginUser($firewallName, $this->user); | 52 | $loginManager->logInUser($firewallName, $this->user); |
53 | 53 | ||
54 | // save the login token into the session and put it in a cookie | 54 | // save the login token into the session and put it in a cookie |
55 | $container->get('session')->set('_security_'.$firewallName, serialize($container->get('security.token_storage')->getToken())); | 55 | $container->get('session')->set('_security_'.$firewallName, serialize($container->get('security.token_storage')->getToken())); |