diff options
author | Jérémy Benoist <j0k3r@users.noreply.github.com> | 2017-10-23 11:09:17 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-23 11:09:17 +0200 |
commit | 1953a872932a63792293b4aec087880265ba89f7 (patch) | |
tree | fd16599e737fcdaf193c933ef3ec4a4ee248b117 /tests/Wallabag/ApiBundle | |
parent | d83d25dadec2c38460a32d96f5d2903426fec9d3 (diff) | |
parent | 702f2d67d60ca963492b90dad74cb5f8dcc84e51 (diff) | |
download | wallabag-1953a872932a63792293b4aec087880265ba89f7.tar.gz wallabag-1953a872932a63792293b4aec087880265ba89f7.tar.zst wallabag-1953a872932a63792293b4aec087880265ba89f7.zip |
Merge pull request #3011 from wallabag/2.3
wallabag 2.3.0
Diffstat (limited to 'tests/Wallabag/ApiBundle')
6 files changed, 755 insertions, 199 deletions
diff --git a/tests/Wallabag/ApiBundle/Controller/DeveloperControllerTest.php b/tests/Wallabag/ApiBundle/Controller/DeveloperControllerTest.php index 6659443b..e9e5ee3b 100644 --- a/tests/Wallabag/ApiBundle/Controller/DeveloperControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/DeveloperControllerTest.php | |||
@@ -3,6 +3,7 @@ | |||
3 | namespace Tests\Wallabag\ApiBundle\Controller; | 3 | namespace Tests\Wallabag\ApiBundle\Controller; |
4 | 4 | ||
5 | use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; | 5 | use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; |
6 | use Wallabag\ApiBundle\Entity\Client; | ||
6 | 7 | ||
7 | class DeveloperControllerTest extends WallabagCoreTestCase | 8 | class DeveloperControllerTest extends WallabagCoreTestCase |
8 | { | 9 | { |
@@ -14,7 +15,7 @@ class DeveloperControllerTest extends WallabagCoreTestCase | |||
14 | $nbClients = $em->getRepository('WallabagApiBundle:Client')->findAll(); | 15 | $nbClients = $em->getRepository('WallabagApiBundle:Client')->findAll(); |
15 | 16 | ||
16 | $crawler = $client->request('GET', '/developer/client/create'); | 17 | $crawler = $client->request('GET', '/developer/client/create'); |
17 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | 18 | $this->assertSame(200, $client->getResponse()->getStatusCode()); |
18 | 19 | ||
19 | $form = $crawler->filter('button[type=submit]')->form(); | 20 | $form = $crawler->filter('button[type=submit]')->form(); |
20 | 21 | ||
@@ -24,7 +25,7 @@ class DeveloperControllerTest extends WallabagCoreTestCase | |||
24 | 25 | ||
25 | $crawler = $client->submit($form, $data); | 26 | $crawler = $client->submit($form, $data); |
26 | 27 | ||
27 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | 28 | $this->assertSame(200, $client->getResponse()->getStatusCode()); |
28 | 29 | ||
29 | $newNbClients = $em->getRepository('WallabagApiBundle:Client')->findAll(); | 30 | $newNbClients = $em->getRepository('WallabagApiBundle:Client')->findAll(); |
30 | $this->assertGreaterThan(count($nbClients), count($newNbClients)); | 31 | $this->assertGreaterThan(count($nbClients), count($newNbClients)); |
@@ -33,14 +34,10 @@ class DeveloperControllerTest extends WallabagCoreTestCase | |||
33 | $this->assertContains('My app', $alert[0]); | 34 | $this->assertContains('My app', $alert[0]); |
34 | } | 35 | } |
35 | 36 | ||
36 | /** | ||
37 | * @depends testCreateClient | ||
38 | */ | ||
39 | public function testCreateToken() | 37 | public function testCreateToken() |
40 | { | 38 | { |
41 | $client = $this->getClient(); | 39 | $client = $this->getClient(); |
42 | $em = $client->getContainer()->get('doctrine.orm.entity_manager'); | 40 | $apiClient = $this->createApiClientForUser('admin'); |
43 | $apiClient = $em->getRepository('WallabagApiBundle:Client')->findOneByName('My app'); | ||
44 | 41 | ||
45 | $client->request('POST', '/oauth/v2/token', [ | 42 | $client->request('POST', '/oauth/v2/token', [ |
46 | 'grant_type' => 'password', | 43 | 'grant_type' => 'password', |
@@ -50,7 +47,7 @@ class DeveloperControllerTest extends WallabagCoreTestCase | |||
50 | 'password' => 'mypassword', | 47 | 'password' => 'mypassword', |
51 | ]); | 48 | ]); |
52 | 49 | ||
53 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | 50 | $this->assertSame(200, $client->getResponse()->getStatusCode()); |
54 | 51 | ||
55 | $data = json_decode($client->getResponse()->getContent(), true); | 52 | $data = json_decode($client->getResponse()->getContent(), true); |
56 | $this->assertArrayHasKey('access_token', $data); | 53 | $this->assertArrayHasKey('access_token', $data); |
@@ -67,8 +64,8 @@ class DeveloperControllerTest extends WallabagCoreTestCase | |||
67 | $nbClients = $em->getRepository('WallabagApiBundle:Client')->findAll(); | 64 | $nbClients = $em->getRepository('WallabagApiBundle:Client')->findAll(); |
68 | 65 | ||
69 | $crawler = $client->request('GET', '/developer'); | 66 | $crawler = $client->request('GET', '/developer'); |
70 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | 67 | $this->assertSame(200, $client->getResponse()->getStatusCode()); |
71 | $this->assertEquals(count($nbClients), $crawler->filter('ul[class=collapsible] li')->count()); | 68 | $this->assertSame(count($nbClients), $crawler->filter('ul[class=collapsible] li')->count()); |
72 | } | 69 | } |
73 | 70 | ||
74 | public function testDeveloperHowto() | 71 | public function testDeveloperHowto() |
@@ -77,12 +74,13 @@ class DeveloperControllerTest extends WallabagCoreTestCase | |||
77 | $client = $this->getClient(); | 74 | $client = $this->getClient(); |
78 | 75 | ||
79 | $crawler = $client->request('GET', '/developer/howto/first-app'); | 76 | $crawler = $client->request('GET', '/developer/howto/first-app'); |
80 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | 77 | $this->assertSame(200, $client->getResponse()->getStatusCode()); |
81 | } | 78 | } |
82 | 79 | ||
83 | public function testRemoveClient() | 80 | public function testRemoveClient() |
84 | { | 81 | { |
85 | $client = $this->getClient(); | 82 | $client = $this->getClient(); |
83 | $adminApiClient = $this->createApiClientForUser('admin'); | ||
86 | $em = $client->getContainer()->get('doctrine.orm.entity_manager'); | 84 | $em = $client->getContainer()->get('doctrine.orm.entity_manager'); |
87 | 85 | ||
88 | // Try to remove an admin's client with a wrong user | 86 | // Try to remove an admin's client with a wrong user |
@@ -90,13 +88,9 @@ class DeveloperControllerTest extends WallabagCoreTestCase | |||
90 | $client->request('GET', '/developer'); | 88 | $client->request('GET', '/developer'); |
91 | $this->assertContains('no_client', $client->getResponse()->getContent()); | 89 | $this->assertContains('no_client', $client->getResponse()->getContent()); |
92 | 90 | ||
93 | // get an ID of a admin's client | ||
94 | $this->logInAs('admin'); | ||
95 | $nbClients = $em->getRepository('WallabagApiBundle:Client')->findByUser($this->getLoggedInUserId()); | ||
96 | |||
97 | $this->logInAs('bob'); | 91 | $this->logInAs('bob'); |
98 | $client->request('GET', '/developer/client/delete/'.$nbClients[0]->getId()); | 92 | $client->request('GET', '/developer/client/delete/' . $adminApiClient->getId()); |
99 | $this->assertEquals(403, $client->getResponse()->getStatusCode()); | 93 | $this->assertSame(403, $client->getResponse()->getStatusCode()); |
100 | 94 | ||
101 | // Try to remove the admin's client with the good user | 95 | // Try to remove the admin's client with the good user |
102 | $this->logInAs('admin'); | 96 | $this->logInAs('admin'); |
@@ -109,9 +103,32 @@ class DeveloperControllerTest extends WallabagCoreTestCase | |||
109 | ; | 103 | ; |
110 | 104 | ||
111 | $client->click($link); | 105 | $client->click($link); |
112 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | 106 | $this->assertSame(302, $client->getResponse()->getStatusCode()); |
107 | |||
108 | $this->assertNull( | ||
109 | $em->getRepository('WallabagApiBundle:Client')->find($adminApiClient->getId()), | ||
110 | 'The client should have been removed' | ||
111 | ); | ||
112 | } | ||
113 | 113 | ||
114 | $newNbClients = $em->getRepository('WallabagApiBundle:Client')->findByUser($this->getLoggedInUserId()); | 114 | /** |
115 | $this->assertGreaterThan(count($newNbClients), count($nbClients)); | 115 | * @param string $username |
116 | * @param array $grantTypes | ||
117 | * | ||
118 | * @return Client | ||
119 | */ | ||
120 | private function createApiClientForUser($username, $grantTypes = ['password']) | ||
121 | { | ||
122 | $client = $this->getClient(); | ||
123 | $em = $client->getContainer()->get('doctrine.orm.entity_manager'); | ||
124 | $userManager = $client->getContainer()->get('fos_user.user_manager'); | ||
125 | $user = $userManager->findUserBy(['username' => $username]); | ||
126 | $apiClient = new Client($user); | ||
127 | $apiClient->setName('My app'); | ||
128 | $apiClient->setAllowedGrantTypes($grantTypes); | ||
129 | $em->persist($apiClient); | ||
130 | $em->flush(); | ||
131 | |||
132 | return $apiClient; | ||
116 | } | 133 | } |
117 | } | 134 | } |
diff --git a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php index 0a65f9ce..95c64501 100644 --- a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php | |||
@@ -3,8 +3,10 @@ | |||
3 | namespace Tests\Wallabag\ApiBundle\Controller; | 3 | namespace Tests\Wallabag\ApiBundle\Controller; |
4 | 4 | ||
5 | use Tests\Wallabag\ApiBundle\WallabagApiTestCase; | 5 | use Tests\Wallabag\ApiBundle\WallabagApiTestCase; |
6 | use Wallabag\CoreBundle\Entity\Entry; | ||
6 | use Wallabag\CoreBundle\Entity\Tag; | 7 | use Wallabag\CoreBundle\Entity\Tag; |
7 | use Wallabag\CoreBundle\Helper\ContentProxy; | 8 | use Wallabag\CoreBundle\Helper\ContentProxy; |
9 | use Wallabag\UserBundle\Entity\User; | ||
8 | 10 | ||
9 | class EntryRestControllerTest extends WallabagApiTestCase | 11 | class EntryRestControllerTest extends WallabagApiTestCase |
10 | { | 12 | { |
@@ -19,19 +21,19 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
19 | $this->markTestSkipped('No content found in db.'); | 21 | $this->markTestSkipped('No content found in db.'); |
20 | } | 22 | } |
21 | 23 | ||
22 | $this->client->request('GET', '/api/entries/'.$entry->getId().'.json'); | 24 | $this->client->request('GET', '/api/entries/' . $entry->getId() . '.json'); |
23 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | 25 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
24 | 26 | ||
25 | $content = json_decode($this->client->getResponse()->getContent(), true); | 27 | $content = json_decode($this->client->getResponse()->getContent(), true); |
26 | 28 | ||
27 | $this->assertEquals($entry->getTitle(), $content['title']); | 29 | $this->assertSame($entry->getTitle(), $content['title']); |
28 | $this->assertEquals($entry->getUrl(), $content['url']); | 30 | $this->assertSame($entry->getUrl(), $content['url']); |
29 | $this->assertCount(count($entry->getTags()), $content['tags']); | 31 | $this->assertCount(count($entry->getTags()), $content['tags']); |
30 | $this->assertEquals($entry->getUserName(), $content['user_name']); | 32 | $this->assertSame($entry->getUserName(), $content['user_name']); |
31 | $this->assertEquals($entry->getUserEmail(), $content['user_email']); | 33 | $this->assertSame($entry->getUserEmail(), $content['user_email']); |
32 | $this->assertEquals($entry->getUserId(), $content['user_id']); | 34 | $this->assertSame($entry->getUserId(), $content['user_id']); |
33 | 35 | ||
34 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); | 36 | $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
35 | } | 37 | } |
36 | 38 | ||
37 | public function testExportEntry() | 39 | public function testExportEntry() |
@@ -45,39 +47,39 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
45 | $this->markTestSkipped('No content found in db.'); | 47 | $this->markTestSkipped('No content found in db.'); |
46 | } | 48 | } |
47 | 49 | ||
48 | $this->client->request('GET', '/api/entries/'.$entry->getId().'/export.epub'); | 50 | $this->client->request('GET', '/api/entries/' . $entry->getId() . '/export.epub'); |
49 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | 51 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
50 | 52 | ||
51 | // epub format got the content type in the content | 53 | // epub format got the content type in the content |
52 | $this->assertContains('application/epub', $this->client->getResponse()->getContent()); | 54 | $this->assertContains('application/epub', $this->client->getResponse()->getContent()); |
53 | $this->assertEquals('application/epub+zip', $this->client->getResponse()->headers->get('Content-Type')); | 55 | $this->assertSame('application/epub+zip', $this->client->getResponse()->headers->get('Content-Type')); |
54 | 56 | ||
55 | // re-auth client for mobi | 57 | // re-auth client for mobi |
56 | $client = $this->createAuthorizedClient(); | 58 | $client = $this->createAuthorizedClient(); |
57 | $client->request('GET', '/api/entries/'.$entry->getId().'/export.mobi'); | 59 | $client->request('GET', '/api/entries/' . $entry->getId() . '/export.mobi'); |
58 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | 60 | $this->assertSame(200, $client->getResponse()->getStatusCode()); |
59 | 61 | ||
60 | $this->assertEquals('application/x-mobipocket-ebook', $client->getResponse()->headers->get('Content-Type')); | 62 | $this->assertSame('application/x-mobipocket-ebook', $client->getResponse()->headers->get('Content-Type')); |
61 | 63 | ||
62 | // re-auth client for pdf | 64 | // re-auth client for pdf |
63 | $client = $this->createAuthorizedClient(); | 65 | $client = $this->createAuthorizedClient(); |
64 | $client->request('GET', '/api/entries/'.$entry->getId().'/export.pdf'); | 66 | $client->request('GET', '/api/entries/' . $entry->getId() . '/export.pdf'); |
65 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | 67 | $this->assertSame(200, $client->getResponse()->getStatusCode()); |
66 | 68 | ||
67 | $this->assertContains('PDF-', $client->getResponse()->getContent()); | 69 | $this->assertContains('PDF-', $client->getResponse()->getContent()); |
68 | $this->assertEquals('application/pdf', $client->getResponse()->headers->get('Content-Type')); | 70 | $this->assertSame('application/pdf', $client->getResponse()->headers->get('Content-Type')); |
69 | 71 | ||
70 | // re-auth client for pdf | 72 | // re-auth client for pdf |
71 | $client = $this->createAuthorizedClient(); | 73 | $client = $this->createAuthorizedClient(); |
72 | $client->request('GET', '/api/entries/'.$entry->getId().'/export.txt'); | 74 | $client->request('GET', '/api/entries/' . $entry->getId() . '/export.txt'); |
73 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | 75 | $this->assertSame(200, $client->getResponse()->getStatusCode()); |
74 | 76 | ||
75 | $this->assertContains('text/plain', $client->getResponse()->headers->get('Content-Type')); | 77 | $this->assertContains('text/plain', $client->getResponse()->headers->get('Content-Type')); |
76 | 78 | ||
77 | // re-auth client for pdf | 79 | // re-auth client for pdf |
78 | $client = $this->createAuthorizedClient(); | 80 | $client = $this->createAuthorizedClient(); |
79 | $client->request('GET', '/api/entries/'.$entry->getId().'/export.csv'); | 81 | $client->request('GET', '/api/entries/' . $entry->getId() . '/export.csv'); |
80 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | 82 | $this->assertSame(200, $client->getResponse()->getStatusCode()); |
81 | 83 | ||
82 | $this->assertContains('application/csv', $client->getResponse()->headers->get('Content-Type')); | 84 | $this->assertContains('application/csv', $client->getResponse()->headers->get('Content-Type')); |
83 | } | 85 | } |
@@ -93,26 +95,26 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
93 | $this->markTestSkipped('No content found in db.'); | 95 | $this->markTestSkipped('No content found in db.'); |
94 | } | 96 | } |
95 | 97 | ||
96 | $this->client->request('GET', '/api/entries/'.$entry->getId().'.json'); | 98 | $this->client->request('GET', '/api/entries/' . $entry->getId() . '.json'); |
97 | 99 | ||
98 | $this->assertEquals(403, $this->client->getResponse()->getStatusCode()); | 100 | $this->assertSame(403, $this->client->getResponse()->getStatusCode()); |
99 | } | 101 | } |
100 | 102 | ||
101 | public function testGetEntries() | 103 | public function testGetEntries() |
102 | { | 104 | { |
103 | $this->client->request('GET', '/api/entries'); | 105 | $this->client->request('GET', '/api/entries'); |
104 | 106 | ||
105 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | 107 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
106 | 108 | ||
107 | $content = json_decode($this->client->getResponse()->getContent(), true); | 109 | $content = json_decode($this->client->getResponse()->getContent(), true); |
108 | 110 | ||
109 | $this->assertGreaterThanOrEqual(1, count($content)); | 111 | $this->assertGreaterThanOrEqual(1, count($content)); |
110 | $this->assertNotEmpty($content['_embedded']['items']); | 112 | $this->assertNotEmpty($content['_embedded']['items']); |
111 | $this->assertGreaterThanOrEqual(1, $content['total']); | 113 | $this->assertGreaterThanOrEqual(1, $content['total']); |
112 | $this->assertEquals(1, $content['page']); | 114 | $this->assertSame(1, $content['page']); |
113 | $this->assertGreaterThanOrEqual(1, $content['pages']); | 115 | $this->assertGreaterThanOrEqual(1, $content['pages']); |
114 | 116 | ||
115 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); | 117 | $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
116 | } | 118 | } |
117 | 119 | ||
118 | public function testGetEntriesWithFullOptions() | 120 | public function testGetEntriesWithFullOptions() |
@@ -126,17 +128,18 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
126 | 'perPage' => 2, | 128 | 'perPage' => 2, |
127 | 'tags' => 'foo', | 129 | 'tags' => 'foo', |
128 | 'since' => 1443274283, | 130 | 'since' => 1443274283, |
131 | 'public' => 0, | ||
129 | ]); | 132 | ]); |
130 | 133 | ||
131 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | 134 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
132 | 135 | ||
133 | $content = json_decode($this->client->getResponse()->getContent(), true); | 136 | $content = json_decode($this->client->getResponse()->getContent(), true); |
134 | 137 | ||
135 | $this->assertGreaterThanOrEqual(1, count($content)); | 138 | $this->assertGreaterThanOrEqual(1, count($content)); |
136 | $this->assertArrayHasKey('items', $content['_embedded']); | 139 | $this->assertArrayHasKey('items', $content['_embedded']); |
137 | $this->assertGreaterThanOrEqual(0, $content['total']); | 140 | $this->assertGreaterThanOrEqual(0, $content['total']); |
138 | $this->assertEquals(1, $content['page']); | 141 | $this->assertSame(1, $content['page']); |
139 | $this->assertEquals(2, $content['limit']); | 142 | $this->assertSame(2, $content['limit']); |
140 | $this->assertGreaterThanOrEqual(1, $content['pages']); | 143 | $this->assertGreaterThanOrEqual(1, $content['pages']); |
141 | 144 | ||
142 | $this->assertArrayHasKey('_links', $content); | 145 | $this->assertArrayHasKey('_links', $content); |
@@ -152,9 +155,56 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
152 | $this->assertContains('order=asc', $content['_links'][$link]['href']); | 155 | $this->assertContains('order=asc', $content['_links'][$link]['href']); |
153 | $this->assertContains('tags=foo', $content['_links'][$link]['href']); | 156 | $this->assertContains('tags=foo', $content['_links'][$link]['href']); |
154 | $this->assertContains('since=1443274283', $content['_links'][$link]['href']); | 157 | $this->assertContains('since=1443274283', $content['_links'][$link]['href']); |
158 | $this->assertContains('public=0', $content['_links'][$link]['href']); | ||
155 | } | 159 | } |
156 | 160 | ||
157 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); | 161 | $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
162 | } | ||
163 | |||
164 | public function testGetEntriesPublicOnly() | ||
165 | { | ||
166 | $entry = $this->client->getContainer() | ||
167 | ->get('doctrine.orm.entity_manager') | ||
168 | ->getRepository('WallabagCoreBundle:Entry') | ||
169 | ->findOneByUser(1); | ||
170 | |||
171 | if (!$entry) { | ||
172 | $this->markTestSkipped('No content found in db.'); | ||
173 | } | ||
174 | |||
175 | // generate at least one public entry | ||
176 | $entry->generateUid(); | ||
177 | |||
178 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | ||
179 | $em->persist($entry); | ||
180 | $em->flush(); | ||
181 | |||
182 | $this->client->request('GET', '/api/entries', [ | ||
183 | 'public' => 1, | ||
184 | ]); | ||
185 | |||
186 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | ||
187 | |||
188 | $content = json_decode($this->client->getResponse()->getContent(), true); | ||
189 | |||
190 | $this->assertGreaterThanOrEqual(1, count($content)); | ||
191 | $this->assertArrayHasKey('items', $content['_embedded']); | ||
192 | $this->assertGreaterThanOrEqual(1, $content['total']); | ||
193 | $this->assertSame(1, $content['page']); | ||
194 | $this->assertSame(30, $content['limit']); | ||
195 | $this->assertGreaterThanOrEqual(1, $content['pages']); | ||
196 | |||
197 | $this->assertArrayHasKey('_links', $content); | ||
198 | $this->assertArrayHasKey('self', $content['_links']); | ||
199 | $this->assertArrayHasKey('first', $content['_links']); | ||
200 | $this->assertArrayHasKey('last', $content['_links']); | ||
201 | |||
202 | foreach (['self', 'first', 'last'] as $link) { | ||
203 | $this->assertArrayHasKey('href', $content['_links'][$link]); | ||
204 | $this->assertContains('public=1', $content['_links'][$link]['href']); | ||
205 | } | ||
206 | |||
207 | $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type')); | ||
158 | } | 208 | } |
159 | 209 | ||
160 | public function testGetEntriesOnPageTwo() | 210 | public function testGetEntriesOnPageTwo() |
@@ -164,27 +214,27 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
164 | 'perPage' => 2, | 214 | 'perPage' => 2, |
165 | ]); | 215 | ]); |
166 | 216 | ||
167 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | 217 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
168 | 218 | ||
169 | $content = json_decode($this->client->getResponse()->getContent(), true); | 219 | $content = json_decode($this->client->getResponse()->getContent(), true); |
170 | 220 | ||
171 | $this->assertGreaterThanOrEqual(0, $content['total']); | 221 | $this->assertGreaterThanOrEqual(0, $content['total']); |
172 | $this->assertEquals(2, $content['page']); | 222 | $this->assertSame(2, $content['page']); |
173 | $this->assertEquals(2, $content['limit']); | 223 | $this->assertSame(2, $content['limit']); |
174 | } | 224 | } |
175 | 225 | ||
176 | public function testGetStarredEntries() | 226 | public function testGetStarredEntries() |
177 | { | 227 | { |
178 | $this->client->request('GET', '/api/entries', ['starred' => 1, 'sort' => 'updated']); | 228 | $this->client->request('GET', '/api/entries', ['starred' => 1, 'sort' => 'updated']); |
179 | 229 | ||
180 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | 230 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
181 | 231 | ||
182 | $content = json_decode($this->client->getResponse()->getContent(), true); | 232 | $content = json_decode($this->client->getResponse()->getContent(), true); |
183 | 233 | ||
184 | $this->assertGreaterThanOrEqual(1, count($content)); | 234 | $this->assertGreaterThanOrEqual(1, count($content)); |
185 | $this->assertNotEmpty($content['_embedded']['items']); | 235 | $this->assertNotEmpty($content['_embedded']['items']); |
186 | $this->assertGreaterThanOrEqual(1, $content['total']); | 236 | $this->assertGreaterThanOrEqual(1, $content['total']); |
187 | $this->assertEquals(1, $content['page']); | 237 | $this->assertSame(1, $content['page']); |
188 | $this->assertGreaterThanOrEqual(1, $content['pages']); | 238 | $this->assertGreaterThanOrEqual(1, $content['pages']); |
189 | 239 | ||
190 | $this->assertArrayHasKey('_links', $content); | 240 | $this->assertArrayHasKey('_links', $content); |
@@ -198,21 +248,21 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
198 | $this->assertContains('sort=updated', $content['_links'][$link]['href']); | 248 | $this->assertContains('sort=updated', $content['_links'][$link]['href']); |
199 | } | 249 | } |
200 | 250 | ||
201 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); | 251 | $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
202 | } | 252 | } |
203 | 253 | ||
204 | public function testGetArchiveEntries() | 254 | public function testGetArchiveEntries() |
205 | { | 255 | { |
206 | $this->client->request('GET', '/api/entries', ['archive' => 1]); | 256 | $this->client->request('GET', '/api/entries', ['archive' => 1]); |
207 | 257 | ||
208 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | 258 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
209 | 259 | ||
210 | $content = json_decode($this->client->getResponse()->getContent(), true); | 260 | $content = json_decode($this->client->getResponse()->getContent(), true); |
211 | 261 | ||
212 | $this->assertGreaterThanOrEqual(1, count($content)); | 262 | $this->assertGreaterThanOrEqual(1, count($content)); |
213 | $this->assertNotEmpty($content['_embedded']['items']); | 263 | $this->assertNotEmpty($content['_embedded']['items']); |
214 | $this->assertGreaterThanOrEqual(1, $content['total']); | 264 | $this->assertGreaterThanOrEqual(1, $content['total']); |
215 | $this->assertEquals(1, $content['page']); | 265 | $this->assertSame(1, $content['page']); |
216 | $this->assertGreaterThanOrEqual(1, $content['pages']); | 266 | $this->assertGreaterThanOrEqual(1, $content['pages']); |
217 | 267 | ||
218 | $this->assertArrayHasKey('_links', $content); | 268 | $this->assertArrayHasKey('_links', $content); |
@@ -225,23 +275,26 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
225 | $this->assertContains('archive=1', $content['_links'][$link]['href']); | 275 | $this->assertContains('archive=1', $content['_links'][$link]['href']); |
226 | } | 276 | } |
227 | 277 | ||
228 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); | 278 | $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
229 | } | 279 | } |
230 | 280 | ||
231 | public function testGetTaggedEntries() | 281 | public function testGetTaggedEntries() |
232 | { | 282 | { |
233 | $this->client->request('GET', '/api/entries', ['tags' => 'foo,bar']); | 283 | $this->client->request('GET', '/api/entries', ['tags' => 'foo,bar']); |
234 | 284 | ||
235 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | 285 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
236 | 286 | ||
237 | $content = json_decode($this->client->getResponse()->getContent(), true); | 287 | $content = json_decode($this->client->getResponse()->getContent(), true); |
238 | 288 | ||
239 | $this->assertGreaterThanOrEqual(1, count($content)); | 289 | $this->assertGreaterThanOrEqual(1, count($content)); |
240 | $this->assertNotEmpty($content['_embedded']['items']); | 290 | $this->assertNotEmpty($content['_embedded']['items']); |
241 | $this->assertGreaterThanOrEqual(1, $content['total']); | 291 | $this->assertGreaterThanOrEqual(1, $content['total']); |
242 | $this->assertEquals(1, $content['page']); | 292 | $this->assertSame(1, $content['page']); |
243 | $this->assertGreaterThanOrEqual(1, $content['pages']); | 293 | $this->assertGreaterThanOrEqual(1, $content['pages']); |
244 | 294 | ||
295 | $this->assertContains('foo', array_column($content['_embedded']['items'][0]['tags'], 'label'), 'Entries tags should have "foo" tag'); | ||
296 | $this->assertContains('bar', array_column($content['_embedded']['items'][0]['tags'], 'label'), 'Entries tags should have "bar" tag'); | ||
297 | |||
245 | $this->assertArrayHasKey('_links', $content); | 298 | $this->assertArrayHasKey('_links', $content); |
246 | $this->assertArrayHasKey('self', $content['_links']); | 299 | $this->assertArrayHasKey('self', $content['_links']); |
247 | $this->assertArrayHasKey('first', $content['_links']); | 300 | $this->assertArrayHasKey('first', $content['_links']); |
@@ -249,24 +302,31 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
249 | 302 | ||
250 | foreach (['self', 'first', 'last'] as $link) { | 303 | foreach (['self', 'first', 'last'] as $link) { |
251 | $this->assertArrayHasKey('href', $content['_links'][$link]); | 304 | $this->assertArrayHasKey('href', $content['_links'][$link]); |
252 | $this->assertContains('tags='.urlencode('foo,bar'), $content['_links'][$link]['href']); | 305 | $this->assertContains('tags=' . urlencode('foo,bar'), $content['_links'][$link]['href']); |
253 | } | 306 | } |
254 | 307 | ||
255 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); | 308 | $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
309 | } | ||
310 | |||
311 | public function testGetTaggedEntriesWithBadParams() | ||
312 | { | ||
313 | $this->client->request('GET', '/api/entries', ['tags' => ['foo', 'bar']]); | ||
314 | |||
315 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | ||
256 | } | 316 | } |
257 | 317 | ||
258 | public function testGetDatedEntries() | 318 | public function testGetDatedEntries() |
259 | { | 319 | { |
260 | $this->client->request('GET', '/api/entries', ['since' => 1443274283]); | 320 | $this->client->request('GET', '/api/entries', ['since' => 1443274283]); |
261 | 321 | ||
262 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | 322 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
263 | 323 | ||
264 | $content = json_decode($this->client->getResponse()->getContent(), true); | 324 | $content = json_decode($this->client->getResponse()->getContent(), true); |
265 | 325 | ||
266 | $this->assertGreaterThanOrEqual(1, count($content)); | 326 | $this->assertGreaterThanOrEqual(1, count($content)); |
267 | $this->assertNotEmpty($content['_embedded']['items']); | 327 | $this->assertNotEmpty($content['_embedded']['items']); |
268 | $this->assertGreaterThanOrEqual(1, $content['total']); | 328 | $this->assertGreaterThanOrEqual(1, $content['total']); |
269 | $this->assertEquals(1, $content['page']); | 329 | $this->assertSame(1, $content['page']); |
270 | $this->assertGreaterThanOrEqual(1, $content['pages']); | 330 | $this->assertGreaterThanOrEqual(1, $content['pages']); |
271 | 331 | ||
272 | $this->assertArrayHasKey('_links', $content); | 332 | $this->assertArrayHasKey('_links', $content); |
@@ -279,7 +339,7 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
279 | $this->assertContains('since=1443274283', $content['_links'][$link]['href']); | 339 | $this->assertContains('since=1443274283', $content['_links'][$link]['href']); |
280 | } | 340 | } |
281 | 341 | ||
282 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); | 342 | $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
283 | } | 343 | } |
284 | 344 | ||
285 | public function testGetDatedSupEntries() | 345 | public function testGetDatedSupEntries() |
@@ -287,15 +347,15 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
287 | $future = new \DateTime(date('Y-m-d H:i:s')); | 347 | $future = new \DateTime(date('Y-m-d H:i:s')); |
288 | $this->client->request('GET', '/api/entries', ['since' => $future->getTimestamp() + 1000]); | 348 | $this->client->request('GET', '/api/entries', ['since' => $future->getTimestamp() + 1000]); |
289 | 349 | ||
290 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | 350 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
291 | 351 | ||
292 | $content = json_decode($this->client->getResponse()->getContent(), true); | 352 | $content = json_decode($this->client->getResponse()->getContent(), true); |
293 | 353 | ||
294 | $this->assertGreaterThanOrEqual(1, count($content)); | 354 | $this->assertGreaterThanOrEqual(1, count($content)); |
295 | $this->assertEmpty($content['_embedded']['items']); | 355 | $this->assertEmpty($content['_embedded']['items']); |
296 | $this->assertEquals(0, $content['total']); | 356 | $this->assertSame(0, $content['total']); |
297 | $this->assertEquals(1, $content['page']); | 357 | $this->assertSame(1, $content['page']); |
298 | $this->assertEquals(1, $content['pages']); | 358 | $this->assertSame(1, $content['pages']); |
299 | 359 | ||
300 | $this->assertArrayHasKey('_links', $content); | 360 | $this->assertArrayHasKey('_links', $content); |
301 | $this->assertArrayHasKey('self', $content['_links']); | 361 | $this->assertArrayHasKey('self', $content['_links']); |
@@ -304,10 +364,10 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
304 | 364 | ||
305 | foreach (['self', 'first', 'last'] as $link) { | 365 | foreach (['self', 'first', 'last'] as $link) { |
306 | $this->assertArrayHasKey('href', $content['_links'][$link]); | 366 | $this->assertArrayHasKey('href', $content['_links'][$link]); |
307 | $this->assertContains('since='.($future->getTimestamp() + 1000), $content['_links'][$link]['href']); | 367 | $this->assertContains('since=' . ($future->getTimestamp() + 1000), $content['_links'][$link]['href']); |
308 | } | 368 | } |
309 | 369 | ||
310 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); | 370 | $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
311 | } | 371 | } |
312 | 372 | ||
313 | public function testDeleteEntry() | 373 | public function testDeleteEntry() |
@@ -315,25 +375,25 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
315 | $entry = $this->client->getContainer() | 375 | $entry = $this->client->getContainer() |
316 | ->get('doctrine.orm.entity_manager') | 376 | ->get('doctrine.orm.entity_manager') |
317 | ->getRepository('WallabagCoreBundle:Entry') | 377 | ->getRepository('WallabagCoreBundle:Entry') |
318 | ->findOneByUser(1); | 378 | ->findOneByUser(1, ['id' => 'asc']); |
319 | 379 | ||
320 | if (!$entry) { | 380 | if (!$entry) { |
321 | $this->markTestSkipped('No content found in db.'); | 381 | $this->markTestSkipped('No content found in db.'); |
322 | } | 382 | } |
323 | 383 | ||
324 | $this->client->request('DELETE', '/api/entries/'.$entry->getId().'.json'); | 384 | $this->client->request('DELETE', '/api/entries/' . $entry->getId() . '.json'); |
325 | 385 | ||
326 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | 386 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
327 | 387 | ||
328 | $content = json_decode($this->client->getResponse()->getContent(), true); | 388 | $content = json_decode($this->client->getResponse()->getContent(), true); |
329 | 389 | ||
330 | $this->assertEquals($entry->getTitle(), $content['title']); | 390 | $this->assertSame($entry->getTitle(), $content['title']); |
331 | $this->assertEquals($entry->getUrl(), $content['url']); | 391 | $this->assertSame($entry->getUrl(), $content['url']); |
332 | 392 | ||
333 | // We'll try to delete this entry again | 393 | // We'll try to delete this entry again |
334 | $this->client->request('DELETE', '/api/entries/'.$entry->getId().'.json'); | 394 | $this->client->request('DELETE', '/api/entries/' . $entry->getId() . '.json'); |
335 | 395 | ||
336 | $this->assertEquals(404, $this->client->getResponse()->getStatusCode()); | 396 | $this->assertSame(404, $this->client->getResponse()->getStatusCode()); |
337 | } | 397 | } |
338 | 398 | ||
339 | public function testPostEntry() | 399 | public function testPostEntry() |
@@ -342,38 +402,61 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
342 | 'url' => 'http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html', | 402 | 'url' => 'http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html', |
343 | 'tags' => 'google', | 403 | 'tags' => 'google', |
344 | 'title' => 'New title for my article', | 404 | 'title' => 'New title for my article', |
405 | 'content' => 'my content', | ||
406 | 'language' => 'de', | ||
407 | 'published_at' => '2016-09-08T11:55:58+0200', | ||
408 | 'authors' => 'bob,helen', | ||
409 | 'public' => 1, | ||
345 | ]); | 410 | ]); |
346 | 411 | ||
347 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | 412 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
348 | 413 | ||
349 | $content = json_decode($this->client->getResponse()->getContent(), true); | 414 | $content = json_decode($this->client->getResponse()->getContent(), true); |
350 | 415 | ||
351 | $this->assertGreaterThan(0, $content['id']); | 416 | $this->assertGreaterThan(0, $content['id']); |
352 | $this->assertEquals('http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html', $content['url']); | 417 | $this->assertSame('http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html', $content['url']); |
353 | $this->assertEquals(false, $content['is_archived']); | 418 | $this->assertSame(0, $content['is_archived']); |
354 | $this->assertEquals(false, $content['is_starred']); | 419 | $this->assertSame(0, $content['is_starred']); |
355 | $this->assertEquals('New title for my article', $content['title']); | 420 | $this->assertNull($content['starred_at']); |
356 | $this->assertEquals(1, $content['user_id']); | 421 | $this->assertSame('New title for my article', $content['title']); |
357 | $this->assertCount(1, $content['tags']); | 422 | $this->assertSame(1, $content['user_id']); |
423 | $this->assertCount(2, $content['tags']); | ||
424 | $this->assertSame('my content', $content['content']); | ||
425 | $this->assertSame('de', $content['language']); | ||
426 | $this->assertSame('2016-09-08T11:55:58+0200', $content['published_at']); | ||
427 | $this->assertCount(2, $content['published_by']); | ||
428 | $this->assertContains('bob', $content['published_by']); | ||
429 | $this->assertContains('helen', $content['published_by']); | ||
430 | $this->assertTrue($content['is_public'], 'A public link has been generated for that entry'); | ||
358 | } | 431 | } |
359 | 432 | ||
360 | public function testPostSameEntry() | 433 | public function testPostSameEntry() |
361 | { | 434 | { |
435 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | ||
436 | $entry = new Entry($em->getReference(User::class, 1)); | ||
437 | $entry->setUrl('http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html'); | ||
438 | $entry->setArchived(true); | ||
439 | $entry->addTag((new Tag())->setLabel('google')); | ||
440 | $entry->addTag((new Tag())->setLabel('apple')); | ||
441 | $em->persist($entry); | ||
442 | $em->flush(); | ||
443 | $em->clear(); | ||
444 | |||
362 | $this->client->request('POST', '/api/entries.json', [ | 445 | $this->client->request('POST', '/api/entries.json', [ |
363 | 'url' => 'http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html', | 446 | 'url' => 'http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html', |
364 | 'archive' => '1', | 447 | 'archive' => '1', |
365 | 'tags' => 'google, apple', | 448 | 'tags' => 'google, apple', |
366 | ]); | 449 | ]); |
367 | 450 | ||
368 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | 451 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
369 | 452 | ||
370 | $content = json_decode($this->client->getResponse()->getContent(), true); | 453 | $content = json_decode($this->client->getResponse()->getContent(), true); |
371 | 454 | ||
372 | $this->assertGreaterThan(0, $content['id']); | 455 | $this->assertGreaterThan(0, $content['id']); |
373 | $this->assertEquals('http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html', $content['url']); | 456 | $this->assertSame('http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html', $content['url']); |
374 | $this->assertEquals(true, $content['is_archived']); | 457 | $this->assertSame(1, $content['is_archived']); |
375 | $this->assertEquals(false, $content['is_starred']); | 458 | $this->assertSame(0, $content['is_starred']); |
376 | $this->assertCount(2, $content['tags']); | 459 | $this->assertCount(3, $content['tags']); |
377 | } | 460 | } |
378 | 461 | ||
379 | public function testPostEntryWhenFetchContentFails() | 462 | public function testPostEntryWhenFetchContentFails() |
@@ -394,10 +477,10 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
394 | 'url' => 'http://www.example.com/', | 477 | 'url' => 'http://www.example.com/', |
395 | ]); | 478 | ]); |
396 | 479 | ||
397 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | 480 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
398 | $content = json_decode($this->client->getResponse()->getContent(), true); | 481 | $content = json_decode($this->client->getResponse()->getContent(), true); |
399 | $this->assertGreaterThan(0, $content['id']); | 482 | $this->assertGreaterThan(0, $content['id']); |
400 | $this->assertEquals('http://www.example.com/', $content['url']); | 483 | $this->assertSame('http://www.example.com/', $content['url']); |
401 | } finally { | 484 | } finally { |
402 | // Remove the created entry to avoid side effects on other tests | 485 | // Remove the created entry to avoid side effects on other tests |
403 | if (isset($content['id'])) { | 486 | if (isset($content['id'])) { |
@@ -411,21 +494,23 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
411 | 494 | ||
412 | public function testPostArchivedAndStarredEntry() | 495 | public function testPostArchivedAndStarredEntry() |
413 | { | 496 | { |
497 | $now = new \DateTime(); | ||
414 | $this->client->request('POST', '/api/entries.json', [ | 498 | $this->client->request('POST', '/api/entries.json', [ |
415 | 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', | 499 | 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', |
416 | 'archive' => '1', | 500 | 'archive' => '1', |
417 | 'starred' => '1', | 501 | 'starred' => '1', |
418 | ]); | 502 | ]); |
419 | 503 | ||
420 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | 504 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
421 | 505 | ||
422 | $content = json_decode($this->client->getResponse()->getContent(), true); | 506 | $content = json_decode($this->client->getResponse()->getContent(), true); |
423 | 507 | ||
424 | $this->assertGreaterThan(0, $content['id']); | 508 | $this->assertGreaterThan(0, $content['id']); |
425 | $this->assertEquals('http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', $content['url']); | 509 | $this->assertSame('http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', $content['url']); |
426 | $this->assertEquals(true, $content['is_archived']); | 510 | $this->assertSame(1, $content['is_archived']); |
427 | $this->assertEquals(true, $content['is_starred']); | 511 | $this->assertSame(1, $content['is_starred']); |
428 | $this->assertEquals(1, $content['user_id']); | 512 | $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \DateTime($content['starred_at']))->getTimestamp()); |
513 | $this->assertSame(1, $content['user_id']); | ||
429 | } | 514 | } |
430 | 515 | ||
431 | public function testPostArchivedAndStarredEntryWithoutQuotes() | 516 | public function testPostArchivedAndStarredEntryWithoutQuotes() |
@@ -436,14 +521,14 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
436 | 'starred' => 1, | 521 | 'starred' => 1, |
437 | ]); | 522 | ]); |
438 | 523 | ||
439 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | 524 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
440 | 525 | ||
441 | $content = json_decode($this->client->getResponse()->getContent(), true); | 526 | $content = json_decode($this->client->getResponse()->getContent(), true); |
442 | 527 | ||
443 | $this->assertGreaterThan(0, $content['id']); | 528 | $this->assertGreaterThan(0, $content['id']); |
444 | $this->assertEquals('http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', $content['url']); | 529 | $this->assertSame('http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', $content['url']); |
445 | $this->assertEquals(false, $content['is_archived']); | 530 | $this->assertSame(0, $content['is_archived']); |
446 | $this->assertEquals(true, $content['is_starred']); | 531 | $this->assertSame(1, $content['is_starred']); |
447 | } | 532 | } |
448 | 533 | ||
449 | public function testPatchEntry() | 534 | public function testPatchEntry() |
@@ -457,25 +542,35 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
457 | $this->markTestSkipped('No content found in db.'); | 542 | $this->markTestSkipped('No content found in db.'); |
458 | } | 543 | } |
459 | 544 | ||
460 | // hydrate the tags relations | 545 | $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [ |
461 | $nbTags = count($entry->getTags()); | ||
462 | |||
463 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [ | ||
464 | 'title' => 'New awesome title', | 546 | 'title' => 'New awesome title', |
465 | 'tags' => 'new tag '.uniqid(), | 547 | 'tags' => 'new tag ' . uniqid(), |
466 | 'starred' => '1', | 548 | 'starred' => '1', |
467 | 'archive' => '0', | 549 | 'archive' => '0', |
550 | 'language' => 'de_AT', | ||
551 | 'preview_picture' => 'http://preview.io/picture.jpg', | ||
552 | 'authors' => 'bob,sponge', | ||
553 | 'content' => 'awesome', | ||
554 | 'public' => 0, | ||
555 | 'published_at' => 1488833381, | ||
468 | ]); | 556 | ]); |
469 | 557 | ||
470 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | 558 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
471 | 559 | ||
472 | $content = json_decode($this->client->getResponse()->getContent(), true); | 560 | $content = json_decode($this->client->getResponse()->getContent(), true); |
473 | 561 | ||
474 | $this->assertEquals($entry->getId(), $content['id']); | 562 | $this->assertSame($entry->getId(), $content['id']); |
475 | $this->assertEquals($entry->getUrl(), $content['url']); | 563 | $this->assertSame($entry->getUrl(), $content['url']); |
476 | $this->assertEquals('New awesome title', $content['title']); | 564 | $this->assertSame('New awesome title', $content['title']); |
477 | $this->assertGreaterThan($nbTags, count($content['tags'])); | 565 | $this->assertGreaterThanOrEqual(1, count($content['tags']), 'We force only one tag'); |
478 | $this->assertEquals(1, $content['user_id']); | 566 | $this->assertSame(1, $content['user_id']); |
567 | $this->assertSame('de_AT', $content['language']); | ||
568 | $this->assertSame('http://preview.io/picture.jpg', $content['preview_picture']); | ||
569 | $this->assertContains('sponge', $content['published_by']); | ||
570 | $this->assertContains('bob', $content['published_by']); | ||
571 | $this->assertSame('awesome', $content['content']); | ||
572 | $this->assertFalse($content['is_public'], 'Entry is no more shared'); | ||
573 | $this->assertContains('2017-03-06', $content['published_at']); | ||
479 | } | 574 | } |
480 | 575 | ||
481 | public function testPatchEntryWithoutQuotes() | 576 | public function testPatchEntryWithoutQuotes() |
@@ -489,24 +584,27 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
489 | $this->markTestSkipped('No content found in db.'); | 584 | $this->markTestSkipped('No content found in db.'); |
490 | } | 585 | } |
491 | 586 | ||
492 | // hydrate the tags relations | 587 | $previousContent = $entry->getContent(); |
493 | $nbTags = count($entry->getTags()); | 588 | $previousLanguage = $entry->getLanguage(); |
494 | 589 | ||
495 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [ | 590 | $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [ |
496 | 'title' => 'New awesome title', | 591 | 'title' => 'New awesome title', |
497 | 'tags' => 'new tag '.uniqid(), | 592 | 'tags' => 'new tag ' . uniqid(), |
498 | 'starred' => 1, | 593 | 'starred' => 1, |
499 | 'archive' => 0, | 594 | 'archive' => 0, |
595 | 'authors' => ['bob', 'sponge'], | ||
500 | ]); | 596 | ]); |
501 | 597 | ||
502 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | 598 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
503 | 599 | ||
504 | $content = json_decode($this->client->getResponse()->getContent(), true); | 600 | $content = json_decode($this->client->getResponse()->getContent(), true); |
505 | 601 | ||
506 | $this->assertEquals($entry->getId(), $content['id']); | 602 | $this->assertSame($entry->getId(), $content['id']); |
507 | $this->assertEquals($entry->getUrl(), $content['url']); | 603 | $this->assertSame($entry->getUrl(), $content['url']); |
508 | $this->assertEquals('New awesome title', $content['title']); | 604 | $this->assertGreaterThanOrEqual(1, count($content['tags']), 'We force only one tag'); |
509 | $this->assertGreaterThan($nbTags, count($content['tags'])); | 605 | $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string'); |
606 | $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved'); | ||
607 | $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved'); | ||
510 | } | 608 | } |
511 | 609 | ||
512 | public function testGetTagsEntry() | 610 | public function testGetTagsEntry() |
@@ -527,9 +625,9 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
527 | $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()]; | 625 | $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()]; |
528 | } | 626 | } |
529 | 627 | ||
530 | $this->client->request('GET', '/api/entries/'.$entry->getId().'/tags'); | 628 | $this->client->request('GET', '/api/entries/' . $entry->getId() . '/tags'); |
531 | 629 | ||
532 | $this->assertEquals(json_encode($tags, JSON_HEX_QUOT), $this->client->getResponse()->getContent()); | 630 | $this->assertSame(json_encode($tags, JSON_HEX_QUOT), $this->client->getResponse()->getContent()); |
533 | } | 631 | } |
534 | 632 | ||
535 | public function testPostTagsOnEntry() | 633 | public function testPostTagsOnEntry() |
@@ -547,14 +645,14 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
547 | 645 | ||
548 | $newTags = 'tag1,tag2,tag3'; | 646 | $newTags = 'tag1,tag2,tag3'; |
549 | 647 | ||
550 | $this->client->request('POST', '/api/entries/'.$entry->getId().'/tags', ['tags' => $newTags]); | 648 | $this->client->request('POST', '/api/entries/' . $entry->getId() . '/tags', ['tags' => $newTags]); |
551 | 649 | ||
552 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | 650 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
553 | 651 | ||
554 | $content = json_decode($this->client->getResponse()->getContent(), true); | 652 | $content = json_decode($this->client->getResponse()->getContent(), true); |
555 | 653 | ||
556 | $this->assertArrayHasKey('tags', $content); | 654 | $this->assertArrayHasKey('tags', $content); |
557 | $this->assertEquals($nbTags + 3, count($content['tags'])); | 655 | $this->assertSame($nbTags + 3, count($content['tags'])); |
558 | 656 | ||
559 | $entryDB = $this->client->getContainer() | 657 | $entryDB = $this->client->getContainer() |
560 | ->get('doctrine.orm.entity_manager') | 658 | ->get('doctrine.orm.entity_manager') |
@@ -587,14 +685,14 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
587 | $nbTags = count($entry->getTags()); | 685 | $nbTags = count($entry->getTags()); |
588 | $tag = $entry->getTags()[0]; | 686 | $tag = $entry->getTags()[0]; |
589 | 687 | ||
590 | $this->client->request('DELETE', '/api/entries/'.$entry->getId().'/tags/'.$tag->getId().'.json'); | 688 | $this->client->request('DELETE', '/api/entries/' . $entry->getId() . '/tags/' . $tag->getId() . '.json'); |
591 | 689 | ||
592 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | 690 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
593 | 691 | ||
594 | $content = json_decode($this->client->getResponse()->getContent(), true); | 692 | $content = json_decode($this->client->getResponse()->getContent(), true); |
595 | 693 | ||
596 | $this->assertArrayHasKey('tags', $content); | 694 | $this->assertArrayHasKey('tags', $content); |
597 | $this->assertEquals($nbTags - 1, count($content['tags'])); | 695 | $this->assertSame($nbTags - 1, count($content['tags'])); |
598 | } | 696 | } |
599 | 697 | ||
600 | public function testSaveIsArchivedAfterPost() | 698 | public function testSaveIsArchivedAfterPost() |
@@ -612,11 +710,11 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
612 | 'url' => $entry->getUrl(), | 710 | 'url' => $entry->getUrl(), |
613 | ]); | 711 | ]); |
614 | 712 | ||
615 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | 713 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
616 | 714 | ||
617 | $content = json_decode($this->client->getResponse()->getContent(), true); | 715 | $content = json_decode($this->client->getResponse()->getContent(), true); |
618 | 716 | ||
619 | $this->assertEquals(true, $content['is_archived']); | 717 | $this->assertSame(1, $content['is_archived']); |
620 | } | 718 | } |
621 | 719 | ||
622 | public function testSaveIsStarredAfterPost() | 720 | public function testSaveIsStarredAfterPost() |
@@ -634,11 +732,11 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
634 | 'url' => $entry->getUrl(), | 732 | 'url' => $entry->getUrl(), |
635 | ]); | 733 | ]); |
636 | 734 | ||
637 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | 735 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
638 | 736 | ||
639 | $content = json_decode($this->client->getResponse()->getContent(), true); | 737 | $content = json_decode($this->client->getResponse()->getContent(), true); |
640 | 738 | ||
641 | $this->assertEquals(true, $content['is_starred']); | 739 | $this->assertSame(1, $content['is_starred']); |
642 | } | 740 | } |
643 | 741 | ||
644 | public function testSaveIsArchivedAfterPatch() | 742 | public function testSaveIsArchivedAfterPatch() |
@@ -652,19 +750,23 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
652 | $this->markTestSkipped('No content found in db.'); | 750 | $this->markTestSkipped('No content found in db.'); |
653 | } | 751 | } |
654 | 752 | ||
655 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [ | 753 | $previousTitle = $entry->getTitle(); |
656 | 'title' => $entry->getTitle().'++', | 754 | |
755 | $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [ | ||
756 | 'title' => $entry->getTitle() . '++', | ||
657 | ]); | 757 | ]); |
658 | 758 | ||
659 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | 759 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
660 | 760 | ||
661 | $content = json_decode($this->client->getResponse()->getContent(), true); | 761 | $content = json_decode($this->client->getResponse()->getContent(), true); |
662 | 762 | ||
663 | $this->assertEquals(true, $content['is_archived']); | 763 | $this->assertSame(1, $content['is_archived']); |
764 | $this->assertSame($previousTitle . '++', $content['title']); | ||
664 | } | 765 | } |
665 | 766 | ||
666 | public function testSaveIsStarredAfterPatch() | 767 | public function testSaveIsStarredAfterPatch() |
667 | { | 768 | { |
769 | $now = new \DateTime(); | ||
668 | $entry = $this->client->getContainer() | 770 | $entry = $this->client->getContainer() |
669 | ->get('doctrine.orm.entity_manager') | 771 | ->get('doctrine.orm.entity_manager') |
670 | ->getRepository('WallabagCoreBundle:Entry') | 772 | ->getRepository('WallabagCoreBundle:Entry') |
@@ -673,60 +775,94 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
673 | if (!$entry) { | 775 | if (!$entry) { |
674 | $this->markTestSkipped('No content found in db.'); | 776 | $this->markTestSkipped('No content found in db.'); |
675 | } | 777 | } |
676 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [ | 778 | $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [ |
677 | 'title' => $entry->getTitle().'++', | 779 | 'title' => $entry->getTitle() . '++', |
678 | ]); | 780 | ]); |
679 | 781 | ||
680 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | 782 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
681 | 783 | ||
682 | $content = json_decode($this->client->getResponse()->getContent(), true); | 784 | $content = json_decode($this->client->getResponse()->getContent(), true); |
683 | 785 | ||
684 | $this->assertEquals(true, $content['is_starred']); | 786 | $this->assertSame(1, $content['is_starred']); |
787 | $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \DateTime($content['starred_at']))->getTimestamp()); | ||
685 | } | 788 | } |
686 | 789 | ||
687 | public function testGetEntriesExists() | 790 | public function dataForEntriesExistWithUrl() |
688 | { | 791 | { |
689 | $this->client->request('GET', '/api/entries/exists?url=http://0.0.0.0/entry2'); | 792 | return [ |
793 | 'with_id' => [ | ||
794 | 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2&return_id=1', | ||
795 | 'expectedValue' => 2, | ||
796 | ], | ||
797 | 'without_id' => [ | ||
798 | 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2', | ||
799 | 'expectedValue' => true, | ||
800 | ], | ||
801 | ]; | ||
802 | } | ||
690 | 803 | ||
691 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | 804 | /** |
805 | * @dataProvider dataForEntriesExistWithUrl | ||
806 | */ | ||
807 | public function testGetEntriesExists($url, $expectedValue) | ||
808 | { | ||
809 | $this->client->request('GET', $url); | ||
810 | |||
811 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | ||
692 | 812 | ||
693 | $content = json_decode($this->client->getResponse()->getContent(), true); | 813 | $content = json_decode($this->client->getResponse()->getContent(), true); |
694 | 814 | ||
695 | $this->assertEquals(true, $content['exists']); | 815 | $this->assertSame($expectedValue, $content['exists']); |
696 | } | 816 | } |
697 | 817 | ||
698 | public function testGetEntriesExistsWithManyUrls() | 818 | public function testGetEntriesExistsWithManyUrls() |
699 | { | 819 | { |
700 | $url1 = 'http://0.0.0.0/entry2'; | 820 | $url1 = 'http://0.0.0.0/entry2'; |
701 | $url2 = 'http://0.0.0.0/entry10'; | 821 | $url2 = 'http://0.0.0.0/entry10'; |
702 | $this->client->request('GET', '/api/entries/exists?urls[]='.$url1.'&urls[]='.$url2); | 822 | $this->client->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2 . '&return_id=1'); |
823 | |||
824 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | ||
825 | |||
826 | $content = json_decode($this->client->getResponse()->getContent(), true); | ||
827 | |||
828 | $this->assertArrayHasKey($url1, $content); | ||
829 | $this->assertArrayHasKey($url2, $content); | ||
830 | $this->assertSame(2, $content[$url1]); | ||
831 | $this->assertNull($content[$url2]); | ||
832 | } | ||
833 | |||
834 | public function testGetEntriesExistsWithManyUrlsReturnBool() | ||
835 | { | ||
836 | $url1 = 'http://0.0.0.0/entry2'; | ||
837 | $url2 = 'http://0.0.0.0/entry10'; | ||
838 | $this->client->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2); | ||
703 | 839 | ||
704 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | 840 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
705 | 841 | ||
706 | $content = json_decode($this->client->getResponse()->getContent(), true); | 842 | $content = json_decode($this->client->getResponse()->getContent(), true); |
707 | 843 | ||
708 | $this->assertArrayHasKey($url1, $content); | 844 | $this->assertArrayHasKey($url1, $content); |
709 | $this->assertArrayHasKey($url2, $content); | 845 | $this->assertArrayHasKey($url2, $content); |
710 | $this->assertEquals(true, $content[$url1]); | 846 | $this->assertTrue($content[$url1]); |
711 | $this->assertEquals(false, $content[$url2]); | 847 | $this->assertFalse($content[$url2]); |
712 | } | 848 | } |
713 | 849 | ||
714 | public function testGetEntriesExistsWhichDoesNotExists() | 850 | public function testGetEntriesExistsWhichDoesNotExists() |
715 | { | 851 | { |
716 | $this->client->request('GET', '/api/entries/exists?url=http://google.com/entry2'); | 852 | $this->client->request('GET', '/api/entries/exists?url=http://google.com/entry2'); |
717 | 853 | ||
718 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | 854 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
719 | 855 | ||
720 | $content = json_decode($this->client->getResponse()->getContent(), true); | 856 | $content = json_decode($this->client->getResponse()->getContent(), true); |
721 | 857 | ||
722 | $this->assertEquals(false, $content['exists']); | 858 | $this->assertSame(false, $content['exists']); |
723 | } | 859 | } |
724 | 860 | ||
725 | public function testGetEntriesExistsWithNoUrl() | 861 | public function testGetEntriesExistsWithNoUrl() |
726 | { | 862 | { |
727 | $this->client->request('GET', '/api/entries/exists?url='); | 863 | $this->client->request('GET', '/api/entries/exists?url='); |
728 | 864 | ||
729 | $this->assertEquals(403, $this->client->getResponse()->getStatusCode()); | 865 | $this->assertSame(403, $this->client->getResponse()->getStatusCode()); |
730 | } | 866 | } |
731 | 867 | ||
732 | public function testReloadEntryErrorWhileFetching() | 868 | public function testReloadEntryErrorWhileFetching() |
@@ -739,8 +875,8 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
739 | $this->markTestSkipped('No content found in db.'); | 875 | $this->markTestSkipped('No content found in db.'); |
740 | } | 876 | } |
741 | 877 | ||
742 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'/reload.json'); | 878 | $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '/reload.json'); |
743 | $this->assertEquals(304, $this->client->getResponse()->getStatusCode()); | 879 | $this->assertSame(304, $this->client->getResponse()->getStatusCode()); |
744 | } | 880 | } |
745 | 881 | ||
746 | public function testReloadEntry() | 882 | public function testReloadEntry() |
@@ -755,13 +891,208 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
755 | 891 | ||
756 | $this->setUp(); | 892 | $this->setUp(); |
757 | 893 | ||
758 | $this->client->request('PATCH', '/api/entries/'.$json['id'].'/reload.json'); | 894 | $this->client->request('PATCH', '/api/entries/' . $json['id'] . '/reload.json'); |
759 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | 895 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
760 | 896 | ||
761 | $content = json_decode($this->client->getResponse()->getContent(), true); | 897 | $content = json_decode($this->client->getResponse()->getContent(), true); |
762 | 898 | ||
763 | $this->assertNotEmpty($content['title']); | 899 | $this->assertNotEmpty($content['title']); |
764 | 900 | ||
765 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); | 901 | $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
902 | } | ||
903 | |||
904 | public function testPostEntriesTagsListAction() | ||
905 | { | ||
906 | $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager') | ||
907 | ->getRepository('WallabagCoreBundle:Entry') | ||
908 | ->findByUrlAndUserId('http://0.0.0.0/entry4', 1); | ||
909 | |||
910 | $tags = $entry->getTags(); | ||
911 | |||
912 | $this->assertCount(2, $tags); | ||
913 | |||
914 | $list = [ | ||
915 | [ | ||
916 | 'url' => 'http://0.0.0.0/entry4', | ||
917 | 'tags' => 'new tag 1, new tag 2', | ||
918 | ], | ||
919 | ]; | ||
920 | |||
921 | $this->client->request('POST', '/api/entries/tags/lists?list=' . json_encode($list)); | ||
922 | |||
923 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | ||
924 | |||
925 | $content = json_decode($this->client->getResponse()->getContent(), true); | ||
926 | |||
927 | $this->assertInternalType('int', $content[0]['entry']); | ||
928 | $this->assertSame('http://0.0.0.0/entry4', $content[0]['url']); | ||
929 | |||
930 | $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager') | ||
931 | ->getRepository('WallabagCoreBundle:Entry') | ||
932 | ->findByUrlAndUserId('http://0.0.0.0/entry4', 1); | ||
933 | |||
934 | $tags = $entry->getTags(); | ||
935 | $this->assertCount(4, $tags); | ||
936 | } | ||
937 | |||
938 | public function testPostEntriesTagsListActionNoList() | ||
939 | { | ||
940 | $this->client->request('POST', '/api/entries/tags/lists?list=' . json_encode([])); | ||
941 | |||
942 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | ||
943 | |||
944 | $content = json_decode($this->client->getResponse()->getContent(), true); | ||
945 | |||
946 | $this->assertEmpty($content); | ||
947 | } | ||
948 | |||
949 | public function testDeleteEntriesTagsListAction() | ||
950 | { | ||
951 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | ||
952 | $entry = new Entry($em->getReference(User::class, 1)); | ||
953 | $entry->setUrl('http://0.0.0.0/test-entry'); | ||
954 | $entry->addTag((new Tag())->setLabel('foo-tag')); | ||
955 | $entry->addTag((new Tag())->setLabel('bar-tag')); | ||
956 | $em->persist($entry); | ||
957 | $em->flush(); | ||
958 | |||
959 | $em->clear(); | ||
960 | |||
961 | $list = [ | ||
962 | [ | ||
963 | 'url' => 'http://0.0.0.0/test-entry', | ||
964 | 'tags' => 'foo-tag, bar-tag', | ||
965 | ], | ||
966 | ]; | ||
967 | |||
968 | $this->client->request('DELETE', '/api/entries/tags/list?list=' . json_encode($list)); | ||
969 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | ||
970 | |||
971 | $entry = $em->getRepository('WallabagCoreBundle:Entry')->find($entry->getId()); | ||
972 | $this->assertCount(0, $entry->getTags()); | ||
973 | } | ||
974 | |||
975 | public function testDeleteEntriesTagsListActionNoList() | ||
976 | { | ||
977 | $this->client->request('DELETE', '/api/entries/tags/list?list=' . json_encode([])); | ||
978 | |||
979 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | ||
980 | |||
981 | $content = json_decode($this->client->getResponse()->getContent(), true); | ||
982 | |||
983 | $this->assertEmpty($content); | ||
984 | } | ||
985 | |||
986 | public function testPostEntriesListAction() | ||
987 | { | ||
988 | $list = [ | ||
989 | 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', | ||
990 | 'http://0.0.0.0/entry2', | ||
991 | ]; | ||
992 | |||
993 | $this->client->request('POST', '/api/entries/lists?urls=' . json_encode($list)); | ||
994 | |||
995 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | ||
996 | |||
997 | $content = json_decode($this->client->getResponse()->getContent(), true); | ||
998 | |||
999 | $this->assertInternalType('int', $content[0]['entry']); | ||
1000 | $this->assertSame('http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', $content[0]['url']); | ||
1001 | |||
1002 | $this->assertInternalType('int', $content[1]['entry']); | ||
1003 | $this->assertSame('http://0.0.0.0/entry2', $content[1]['url']); | ||
1004 | } | ||
1005 | |||
1006 | public function testPostEntriesListActionWithNoUrls() | ||
1007 | { | ||
1008 | $this->client->request('POST', '/api/entries/lists?urls=' . json_encode([])); | ||
1009 | |||
1010 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | ||
1011 | |||
1012 | $content = json_decode($this->client->getResponse()->getContent(), true); | ||
1013 | |||
1014 | $this->assertEmpty($content); | ||
1015 | } | ||
1016 | |||
1017 | public function testDeleteEntriesListAction() | ||
1018 | { | ||
1019 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | ||
1020 | $em->persist((new Entry($em->getReference(User::class, 1)))->setUrl('http://0.0.0.0/test-entry1')); | ||
1021 | |||
1022 | $em->flush(); | ||
1023 | $em->clear(); | ||
1024 | $list = [ | ||
1025 | 'http://0.0.0.0/test-entry1', | ||
1026 | 'http://0.0.0.0/test-entry-not-exist', | ||
1027 | ]; | ||
1028 | |||
1029 | $this->client->request('DELETE', '/api/entries/list?urls=' . json_encode($list)); | ||
1030 | |||
1031 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | ||
1032 | |||
1033 | $content = json_decode($this->client->getResponse()->getContent(), true); | ||
1034 | |||
1035 | $this->assertTrue($content[0]['entry']); | ||
1036 | $this->assertSame('http://0.0.0.0/test-entry1', $content[0]['url']); | ||
1037 | |||
1038 | $this->assertFalse($content[1]['entry']); | ||
1039 | $this->assertSame('http://0.0.0.0/test-entry-not-exist', $content[1]['url']); | ||
1040 | } | ||
1041 | |||
1042 | public function testDeleteEntriesListActionWithNoUrls() | ||
1043 | { | ||
1044 | $this->client->request('DELETE', '/api/entries/list?urls=' . json_encode([])); | ||
1045 | |||
1046 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | ||
1047 | |||
1048 | $content = json_decode($this->client->getResponse()->getContent(), true); | ||
1049 | |||
1050 | $this->assertEmpty($content); | ||
1051 | } | ||
1052 | |||
1053 | public function testLimitBulkAction() | ||
1054 | { | ||
1055 | $list = [ | ||
1056 | 'http://0.0.0.0/entry1', | ||
1057 | 'http://0.0.0.0/entry1', | ||
1058 | 'http://0.0.0.0/entry1', | ||
1059 | 'http://0.0.0.0/entry1', | ||
1060 | 'http://0.0.0.0/entry1', | ||
1061 | 'http://0.0.0.0/entry1', | ||
1062 | 'http://0.0.0.0/entry1', | ||
1063 | 'http://0.0.0.0/entry1', | ||
1064 | 'http://0.0.0.0/entry1', | ||
1065 | 'http://0.0.0.0/entry1', | ||
1066 | 'http://0.0.0.0/entry1', | ||
1067 | ]; | ||
1068 | |||
1069 | $this->client->request('POST', '/api/entries/lists?urls=' . json_encode($list)); | ||
1070 | |||
1071 | $this->assertSame(400, $this->client->getResponse()->getStatusCode()); | ||
1072 | $this->assertContains('API limit reached', $this->client->getResponse()->getContent()); | ||
1073 | } | ||
1074 | |||
1075 | public function testRePostEntryAndReUsePublishedAt() | ||
1076 | { | ||
1077 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | ||
1078 | $entry = new Entry($em->getReference(User::class, 1)); | ||
1079 | $entry->setTitle('Antoine de Caunes : « Je veux avoir le droit de tâtonner »'); | ||
1080 | $entry->setContent('hihi'); | ||
1081 | $entry->setUrl('http://www.lemonde.fr/m-perso/article/2017/06/25/antoine-de-caunes-je-veux-avoir-le-droit-de-tatonner_5150728_4497916.html'); | ||
1082 | $entry->setPublishedAt(new \DateTime('2017-06-26T07:46:02+0200')); | ||
1083 | $em->persist($entry); | ||
1084 | $em->flush(); | ||
1085 | $em->clear(); | ||
1086 | |||
1087 | $this->client->request('POST', '/api/entries.json', [ | ||
1088 | 'url' => 'http://www.lemonde.fr/m-perso/article/2017/06/25/antoine-de-caunes-je-veux-avoir-le-droit-de-tatonner_5150728_4497916.html', | ||
1089 | ]); | ||
1090 | |||
1091 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | ||
1092 | |||
1093 | $content = json_decode($this->client->getResponse()->getContent(), true); | ||
1094 | |||
1095 | $this->assertGreaterThan(0, $content['id']); | ||
1096 | $this->assertSame('http://www.lemonde.fr/m-perso/article/2017/06/25/antoine-de-caunes-je-veux-avoir-le-droit-de-tatonner_5150728_4497916.html', $content['url']); | ||
766 | } | 1097 | } |
767 | } | 1098 | } |
diff --git a/tests/Wallabag/ApiBundle/Controller/TagRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/TagRestControllerTest.php index bde5251f..430e548d 100644 --- a/tests/Wallabag/ApiBundle/Controller/TagRestControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/TagRestControllerTest.php | |||
@@ -11,7 +11,7 @@ class TagRestControllerTest extends WallabagApiTestCase | |||
11 | { | 11 | { |
12 | $this->client->request('GET', '/api/tags.json'); | 12 | $this->client->request('GET', '/api/tags.json'); |
13 | 13 | ||
14 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | 14 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
15 | 15 | ||
16 | $content = json_decode($this->client->getResponse()->getContent(), true); | 16 | $content = json_decode($this->client->getResponse()->getContent(), true); |
17 | 17 | ||
@@ -22,39 +22,49 @@ class TagRestControllerTest extends WallabagApiTestCase | |||
22 | return end($content); | 22 | return end($content); |
23 | } | 23 | } |
24 | 24 | ||
25 | /** | 25 | public function testDeleteUserTag() |
26 | * @depends testGetUserTags | ||
27 | */ | ||
28 | public function testDeleteUserTag($tag) | ||
29 | { | 26 | { |
30 | $tagName = $tag['label']; | 27 | $tagLabel = 'tagtest'; |
28 | $tag = new Tag(); | ||
29 | $tag->setLabel($tagLabel); | ||
30 | |||
31 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | ||
32 | $em->persist($tag); | ||
33 | $em->flush(); | ||
34 | $em->clear(); | ||
31 | 35 | ||
32 | $this->client->request('DELETE', '/api/tags/'.$tag['id'].'.json'); | 36 | $this->client->request('DELETE', '/api/tags/' . $tag->getId() . '.json'); |
33 | 37 | ||
34 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | 38 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
35 | 39 | ||
36 | $content = json_decode($this->client->getResponse()->getContent(), true); | 40 | $content = json_decode($this->client->getResponse()->getContent(), true); |
37 | 41 | ||
38 | $this->assertArrayHasKey('label', $content); | 42 | $this->assertArrayHasKey('label', $content); |
39 | $this->assertEquals($tag['label'], $content['label']); | 43 | $this->assertSame($tag->getLabel(), $content['label']); |
40 | $this->assertEquals($tag['slug'], $content['slug']); | 44 | $this->assertSame($tag->getSlug(), $content['slug']); |
41 | 45 | ||
42 | $entries = $this->client->getContainer() | 46 | $entries = $em->getRepository('WallabagCoreBundle:Entry') |
43 | ->get('doctrine.orm.entity_manager') | 47 | ->findAllByTagId($this->user->getId(), $tag->getId()); |
44 | ->getRepository('WallabagCoreBundle:Entry') | ||
45 | ->findAllByTagId($this->user->getId(), $tag['id']); | ||
46 | 48 | ||
47 | $this->assertCount(0, $entries); | 49 | $this->assertCount(0, $entries); |
48 | 50 | ||
49 | $tag = $this->client->getContainer() | 51 | $tag = $em->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($tagLabel); |
50 | ->get('doctrine.orm.entity_manager') | ||
51 | ->getRepository('WallabagCoreBundle:Tag') | ||
52 | ->findOneByLabel($tagName); | ||
53 | 52 | ||
54 | $this->assertNull($tag, $tagName.' was removed because it begun an orphan tag'); | 53 | $this->assertNull($tag, $tagLabel . ' was removed because it begun an orphan tag'); |
55 | } | 54 | } |
56 | 55 | ||
57 | public function testDeleteTagByLabel() | 56 | public function dataForDeletingTagByLabel() |
57 | { | ||
58 | return [ | ||
59 | 'by_query' => [true], | ||
60 | 'by_body' => [false], | ||
61 | ]; | ||
62 | } | ||
63 | |||
64 | /** | ||
65 | * @dataProvider dataForDeletingTagByLabel | ||
66 | */ | ||
67 | public function testDeleteTagByLabel($useQueryString) | ||
58 | { | 68 | { |
59 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | 69 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); |
60 | $entry = $this->client->getContainer() | 70 | $entry = $this->client->getContainer() |
@@ -73,15 +83,19 @@ class TagRestControllerTest extends WallabagApiTestCase | |||
73 | $em->persist($entry); | 83 | $em->persist($entry); |
74 | $em->flush(); | 84 | $em->flush(); |
75 | 85 | ||
76 | $this->client->request('DELETE', '/api/tag/label.json', ['tag' => $tag->getLabel()]); | 86 | if ($useQueryString) { |
87 | $this->client->request('DELETE', '/api/tag/label.json?tag=' . $tag->getLabel()); | ||
88 | } else { | ||
89 | $this->client->request('DELETE', '/api/tag/label.json', ['tag' => $tag->getLabel()]); | ||
90 | } | ||
77 | 91 | ||
78 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | 92 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
79 | 93 | ||
80 | $content = json_decode($this->client->getResponse()->getContent(), true); | 94 | $content = json_decode($this->client->getResponse()->getContent(), true); |
81 | 95 | ||
82 | $this->assertArrayHasKey('label', $content); | 96 | $this->assertArrayHasKey('label', $content); |
83 | $this->assertEquals($tag->getLabel(), $content['label']); | 97 | $this->assertSame($tag->getLabel(), $content['label']); |
84 | $this->assertEquals($tag->getSlug(), $content['slug']); | 98 | $this->assertSame($tag->getSlug(), $content['slug']); |
85 | 99 | ||
86 | $entries = $this->client->getContainer() | 100 | $entries = $this->client->getContainer() |
87 | ->get('doctrine.orm.entity_manager') | 101 | ->get('doctrine.orm.entity_manager') |
@@ -95,10 +109,13 @@ class TagRestControllerTest extends WallabagApiTestCase | |||
95 | { | 109 | { |
96 | $this->client->request('DELETE', '/api/tag/label.json', ['tag' => 'does not exist']); | 110 | $this->client->request('DELETE', '/api/tag/label.json', ['tag' => 'does not exist']); |
97 | 111 | ||
98 | $this->assertEquals(404, $this->client->getResponse()->getStatusCode()); | 112 | $this->assertSame(404, $this->client->getResponse()->getStatusCode()); |
99 | } | 113 | } |
100 | 114 | ||
101 | public function testDeleteTagsByLabel() | 115 | /** |
116 | * @dataProvider dataForDeletingTagByLabel | ||
117 | */ | ||
118 | public function testDeleteTagsByLabel($useQueryString) | ||
102 | { | 119 | { |
103 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | 120 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); |
104 | $entry = $this->client->getContainer() | 121 | $entry = $this->client->getContainer() |
@@ -122,21 +139,25 @@ class TagRestControllerTest extends WallabagApiTestCase | |||
122 | $em->persist($entry); | 139 | $em->persist($entry); |
123 | $em->flush(); | 140 | $em->flush(); |
124 | 141 | ||
125 | $this->client->request('DELETE', '/api/tags/label.json', ['tags' => $tag->getLabel().','.$tag2->getLabel()]); | 142 | if ($useQueryString) { |
143 | $this->client->request('DELETE', '/api/tags/label.json?tags=' . $tag->getLabel() . ',' . $tag2->getLabel()); | ||
144 | } else { | ||
145 | $this->client->request('DELETE', '/api/tags/label.json', ['tags' => $tag->getLabel() . ',' . $tag2->getLabel()]); | ||
146 | } | ||
126 | 147 | ||
127 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | 148 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
128 | 149 | ||
129 | $content = json_decode($this->client->getResponse()->getContent(), true); | 150 | $content = json_decode($this->client->getResponse()->getContent(), true); |
130 | 151 | ||
131 | $this->assertCount(2, $content); | 152 | $this->assertCount(2, $content); |
132 | 153 | ||
133 | $this->assertArrayHasKey('label', $content[0]); | 154 | $this->assertArrayHasKey('label', $content[0]); |
134 | $this->assertEquals($tag->getLabel(), $content[0]['label']); | 155 | $this->assertSame($tag->getLabel(), $content[0]['label']); |
135 | $this->assertEquals($tag->getSlug(), $content[0]['slug']); | 156 | $this->assertSame($tag->getSlug(), $content[0]['slug']); |
136 | 157 | ||
137 | $this->assertArrayHasKey('label', $content[1]); | 158 | $this->assertArrayHasKey('label', $content[1]); |
138 | $this->assertEquals($tag2->getLabel(), $content[1]['label']); | 159 | $this->assertSame($tag2->getLabel(), $content[1]['label']); |
139 | $this->assertEquals($tag2->getSlug(), $content[1]['slug']); | 160 | $this->assertSame($tag2->getSlug(), $content[1]['slug']); |
140 | 161 | ||
141 | $entries = $this->client->getContainer() | 162 | $entries = $this->client->getContainer() |
142 | ->get('doctrine.orm.entity_manager') | 163 | ->get('doctrine.orm.entity_manager') |
@@ -157,6 +178,6 @@ class TagRestControllerTest extends WallabagApiTestCase | |||
157 | { | 178 | { |
158 | $this->client->request('DELETE', '/api/tags/label.json', ['tags' => 'does not exist']); | 179 | $this->client->request('DELETE', '/api/tags/label.json', ['tags' => 'does not exist']); |
159 | 180 | ||
160 | $this->assertEquals(404, $this->client->getResponse()->getStatusCode()); | 181 | $this->assertSame(404, $this->client->getResponse()->getStatusCode()); |
161 | } | 182 | } |
162 | } | 183 | } |
diff --git a/tests/Wallabag/ApiBundle/Controller/UserRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/UserRestControllerTest.php new file mode 100644 index 00000000..51fac2bd --- /dev/null +++ b/tests/Wallabag/ApiBundle/Controller/UserRestControllerTest.php | |||
@@ -0,0 +1,185 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Tests\Wallabag\ApiBundle\Controller; | ||
4 | |||
5 | use Tests\Wallabag\ApiBundle\WallabagApiTestCase; | ||
6 | |||
7 | class UserRestControllerTest extends WallabagApiTestCase | ||
8 | { | ||
9 | public function testGetUser() | ||
10 | { | ||
11 | $this->client->request('GET', '/api/user.json'); | ||
12 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | ||
13 | |||
14 | $content = json_decode($this->client->getResponse()->getContent(), true); | ||
15 | |||
16 | $this->assertArrayHasKey('id', $content); | ||
17 | $this->assertArrayHasKey('email', $content); | ||
18 | $this->assertArrayHasKey('name', $content); | ||
19 | $this->assertArrayHasKey('username', $content); | ||
20 | $this->assertArrayHasKey('created_at', $content); | ||
21 | $this->assertArrayHasKey('updated_at', $content); | ||
22 | |||
23 | $this->assertSame('bigboss@wallabag.org', $content['email']); | ||
24 | $this->assertSame('Big boss', $content['name']); | ||
25 | $this->assertSame('admin', $content['username']); | ||
26 | |||
27 | $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type')); | ||
28 | } | ||
29 | |||
30 | public function testGetUserWithoutAuthentication() | ||
31 | { | ||
32 | $client = static::createClient(); | ||
33 | $client->request('GET', '/api/user.json'); | ||
34 | $this->assertSame(401, $client->getResponse()->getStatusCode()); | ||
35 | |||
36 | $content = json_decode($client->getResponse()->getContent(), true); | ||
37 | |||
38 | $this->assertArrayHasKey('error', $content); | ||
39 | $this->assertArrayHasKey('error_description', $content); | ||
40 | |||
41 | $this->assertSame('access_denied', $content['error']); | ||
42 | |||
43 | $this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type')); | ||
44 | } | ||
45 | |||
46 | public function testCreateNewUser() | ||
47 | { | ||
48 | $this->client->getContainer()->get('craue_config')->set('api_user_registration', 1); | ||
49 | $this->client->request('PUT', '/api/user.json', [ | ||
50 | 'username' => 'google', | ||
51 | 'password' => 'googlegoogle', | ||
52 | 'email' => 'wallabag@google.com', | ||
53 | ]); | ||
54 | |||
55 | $this->assertSame(201, $this->client->getResponse()->getStatusCode()); | ||
56 | |||
57 | $content = json_decode($this->client->getResponse()->getContent(), true); | ||
58 | |||
59 | $this->assertArrayHasKey('id', $content); | ||
60 | $this->assertArrayHasKey('email', $content); | ||
61 | $this->assertArrayHasKey('username', $content); | ||
62 | $this->assertArrayHasKey('created_at', $content); | ||
63 | $this->assertArrayHasKey('updated_at', $content); | ||
64 | $this->assertArrayHasKey('default_client', $content); | ||
65 | |||
66 | $this->assertSame('wallabag@google.com', $content['email']); | ||
67 | $this->assertSame('google', $content['username']); | ||
68 | |||
69 | $this->assertArrayHasKey('client_secret', $content['default_client']); | ||
70 | $this->assertArrayHasKey('client_id', $content['default_client']); | ||
71 | |||
72 | $this->assertSame('Default client', $content['default_client']['name']); | ||
73 | |||
74 | $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type')); | ||
75 | |||
76 | $this->client->getContainer()->get('craue_config')->set('api_user_registration', 0); | ||
77 | } | ||
78 | |||
79 | public function testCreateNewUserWithoutAuthentication() | ||
80 | { | ||
81 | // create a new client instead of using $this->client to be sure client isn't authenticated | ||
82 | $client = static::createClient(); | ||
83 | $client->getContainer()->get('craue_config')->set('api_user_registration', 1); | ||
84 | $client->request('PUT', '/api/user.json', [ | ||
85 | 'username' => 'google', | ||
86 | 'password' => 'googlegoogle', | ||
87 | 'email' => 'wallabag@google.com', | ||
88 | 'client_name' => 'My client name !!', | ||
89 | ]); | ||
90 | |||
91 | $this->assertSame(201, $client->getResponse()->getStatusCode()); | ||
92 | |||
93 | $content = json_decode($client->getResponse()->getContent(), true); | ||
94 | |||
95 | $this->assertArrayHasKey('id', $content); | ||
96 | $this->assertArrayHasKey('email', $content); | ||
97 | $this->assertArrayHasKey('username', $content); | ||
98 | $this->assertArrayHasKey('created_at', $content); | ||
99 | $this->assertArrayHasKey('updated_at', $content); | ||
100 | $this->assertArrayHasKey('default_client', $content); | ||
101 | |||
102 | $this->assertSame('wallabag@google.com', $content['email']); | ||
103 | $this->assertSame('google', $content['username']); | ||
104 | |||
105 | $this->assertArrayHasKey('client_secret', $content['default_client']); | ||
106 | $this->assertArrayHasKey('client_id', $content['default_client']); | ||
107 | |||
108 | $this->assertSame('My client name !!', $content['default_client']['name']); | ||
109 | |||
110 | $this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type')); | ||
111 | |||
112 | $client->getContainer()->get('craue_config')->set('api_user_registration', 0); | ||
113 | } | ||
114 | |||
115 | public function testCreateNewUserWithExistingEmail() | ||
116 | { | ||
117 | $client = static::createClient(); | ||
118 | $client->getContainer()->get('craue_config')->set('api_user_registration', 1); | ||
119 | $client->request('PUT', '/api/user.json', [ | ||
120 | 'username' => 'admin', | ||
121 | 'password' => 'googlegoogle', | ||
122 | 'email' => 'bigboss@wallabag.org', | ||
123 | ]); | ||
124 | |||
125 | $this->assertSame(400, $client->getResponse()->getStatusCode()); | ||
126 | |||
127 | $content = json_decode($client->getResponse()->getContent(), true); | ||
128 | |||
129 | $this->assertArrayHasKey('error', $content); | ||
130 | $this->assertArrayHasKey('username', $content['error']); | ||
131 | $this->assertArrayHasKey('email', $content['error']); | ||
132 | |||
133 | // $this->assertEquals('fos_user.username.already_used', $content['error']['username'][0]); | ||
134 | // $this->assertEquals('fos_user.email.already_used', $content['error']['email'][0]); | ||
135 | // This shouldn't be translated ... | ||
136 | $this->assertSame('This value is already used.', $content['error']['username'][0]); | ||
137 | $this->assertSame('This value is already used.', $content['error']['email'][0]); | ||
138 | |||
139 | $this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type')); | ||
140 | |||
141 | $client->getContainer()->get('craue_config')->set('api_user_registration', 0); | ||
142 | } | ||
143 | |||
144 | public function testCreateNewUserWithTooShortPassword() | ||
145 | { | ||
146 | $client = static::createClient(); | ||
147 | $client->getContainer()->get('craue_config')->set('api_user_registration', 1); | ||
148 | $client->request('PUT', '/api/user.json', [ | ||
149 | 'username' => 'facebook', | ||
150 | 'password' => 'face', | ||
151 | 'email' => 'facebook@wallabag.org', | ||
152 | ]); | ||
153 | |||
154 | $this->assertSame(400, $client->getResponse()->getStatusCode()); | ||
155 | |||
156 | $content = json_decode($client->getResponse()->getContent(), true); | ||
157 | |||
158 | $this->assertArrayHasKey('error', $content); | ||
159 | $this->assertArrayHasKey('password', $content['error']); | ||
160 | |||
161 | $this->assertSame('validator.password_too_short', $content['error']['password'][0]); | ||
162 | |||
163 | $this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type')); | ||
164 | |||
165 | $client->getContainer()->get('craue_config')->set('api_user_registration', 0); | ||
166 | } | ||
167 | |||
168 | public function testCreateNewUserWhenRegistrationIsDisabled() | ||
169 | { | ||
170 | $client = static::createClient(); | ||
171 | $client->request('PUT', '/api/user.json', [ | ||
172 | 'username' => 'facebook', | ||
173 | 'password' => 'face', | ||
174 | 'email' => 'facebook@wallabag.org', | ||
175 | ]); | ||
176 | |||
177 | $this->assertSame(403, $client->getResponse()->getStatusCode()); | ||
178 | |||
179 | $content = json_decode($client->getResponse()->getContent(), true); | ||
180 | |||
181 | $this->assertArrayHasKey('error', $content); | ||
182 | |||
183 | $this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type')); | ||
184 | } | ||
185 | } | ||
diff --git a/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php index c87e58de..ac4d6cdc 100644 --- a/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php | |||
@@ -8,12 +8,14 @@ class WallabagRestControllerTest extends WallabagApiTestCase | |||
8 | { | 8 | { |
9 | public function testGetVersion() | 9 | public function testGetVersion() |
10 | { | 10 | { |
11 | $this->client->request('GET', '/api/version'); | 11 | // create a new client instead of using $this->client to be sure client isn't authenticated |
12 | $client = static::createClient(); | ||
13 | $client->request('GET', '/api/version'); | ||
12 | 14 | ||
13 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | 15 | $this->assertSame(200, $client->getResponse()->getStatusCode()); |
14 | 16 | ||
15 | $content = json_decode($this->client->getResponse()->getContent(), true); | 17 | $content = json_decode($client->getResponse()->getContent(), true); |
16 | 18 | ||
17 | $this->assertEquals($this->client->getContainer()->getParameter('wallabag_core.version'), $content); | 19 | $this->assertSame($client->getContainer()->getParameter('wallabag_core.version'), $content); |
18 | } | 20 | } |
19 | } | 21 | } |
diff --git a/tests/Wallabag/ApiBundle/WallabagApiTestCase.php b/tests/Wallabag/ApiBundle/WallabagApiTestCase.php index cf9b3347..8a188e1c 100644 --- a/tests/Wallabag/ApiBundle/WallabagApiTestCase.php +++ b/tests/Wallabag/ApiBundle/WallabagApiTestCase.php | |||
@@ -8,7 +8,7 @@ use Symfony\Component\BrowserKit\Cookie; | |||
8 | abstract class WallabagApiTestCase extends WebTestCase | 8 | abstract class WallabagApiTestCase 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 | ||
@@ -23,7 +23,7 @@ abstract class WallabagApiTestCase extends WebTestCase | |||
23 | } | 23 | } |
24 | 24 | ||
25 | /** | 25 | /** |
26 | * @return Client | 26 | * @return \Symfony\Bundle\FrameworkBundle\Client |
27 | */ | 27 | */ |
28 | protected function createAuthorizedClient() | 28 | protected function createAuthorizedClient() |
29 | { | 29 | { |
@@ -37,10 +37,10 @@ abstract class WallabagApiTestCase extends WebTestCase | |||
37 | $firewallName = $container->getParameter('fos_user.firewall_name'); | 37 | $firewallName = $container->getParameter('fos_user.firewall_name'); |
38 | 38 | ||
39 | $this->user = $userManager->findUserBy(['username' => 'admin']); | 39 | $this->user = $userManager->findUserBy(['username' => 'admin']); |
40 | $loginManager->loginUser($firewallName, $this->user); | 40 | $loginManager->logInUser($firewallName, $this->user); |
41 | 41 | ||
42 | // save the login token into the session and put it in a cookie | 42 | // save the login token into the session and put it in a cookie |
43 | $container->get('session')->set('_security_'.$firewallName, serialize($container->get('security.token_storage')->getToken())); | 43 | $container->get('session')->set('_security_' . $firewallName, serialize($container->get('security.token_storage')->getToken())); |
44 | $container->get('session')->save(); | 44 | $container->get('session')->save(); |
45 | 45 | ||
46 | $session = $container->get('session'); | 46 | $session = $container->get('session'); |