aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/Wallabag/ApiBundle
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Wallabag/ApiBundle')
-rw-r--r--tests/Wallabag/ApiBundle/Controller/DeveloperControllerTest.php57
-rw-r--r--tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php592
-rw-r--r--tests/Wallabag/ApiBundle/Controller/TagRestControllerTest.php87
-rw-r--r--tests/Wallabag/ApiBundle/Controller/UserRestControllerTest.php185
-rw-r--r--tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php10
-rw-r--r--tests/Wallabag/ApiBundle/WallabagApiTestCase.php8
6 files changed, 740 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 @@
3namespace Tests\Wallabag\ApiBundle\Controller; 3namespace Tests\Wallabag\ApiBundle\Controller;
4 4
5use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; 5use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
6use Wallabag\ApiBundle\Entity\Client;
6 7
7class DeveloperControllerTest extends WallabagCoreTestCase 8class 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..2dc08be2 100644
--- a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
+++ b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
@@ -3,8 +3,10 @@
3namespace Tests\Wallabag\ApiBundle\Controller; 3namespace Tests\Wallabag\ApiBundle\Controller;
4 4
5use Tests\Wallabag\ApiBundle\WallabagApiTestCase; 5use Tests\Wallabag\ApiBundle\WallabagApiTestCase;
6use Wallabag\CoreBundle\Entity\Entry;
6use Wallabag\CoreBundle\Entity\Tag; 7use Wallabag\CoreBundle\Entity\Tag;
7use Wallabag\CoreBundle\Helper\ContentProxy; 8use Wallabag\CoreBundle\Helper\ContentProxy;
9use Wallabag\UserBundle\Entity\User;
8 10
9class EntryRestControllerTest extends WallabagApiTestCase 11class 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,21 +275,21 @@ 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
245 $this->assertArrayHasKey('_links', $content); 295 $this->assertArrayHasKey('_links', $content);
@@ -249,24 +299,24 @@ class EntryRestControllerTest extends WallabagApiTestCase
249 299
250 foreach (['self', 'first', 'last'] as $link) { 300 foreach (['self', 'first', 'last'] as $link) {
251 $this->assertArrayHasKey('href', $content['_links'][$link]); 301 $this->assertArrayHasKey('href', $content['_links'][$link]);
252 $this->assertContains('tags='.urlencode('foo,bar'), $content['_links'][$link]['href']); 302 $this->assertContains('tags=' . urlencode('foo,bar'), $content['_links'][$link]['href']);
253 } 303 }
254 304
255 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); 305 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
256 } 306 }
257 307
258 public function testGetDatedEntries() 308 public function testGetDatedEntries()
259 { 309 {
260 $this->client->request('GET', '/api/entries', ['since' => 1443274283]); 310 $this->client->request('GET', '/api/entries', ['since' => 1443274283]);
261 311
262 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 312 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
263 313
264 $content = json_decode($this->client->getResponse()->getContent(), true); 314 $content = json_decode($this->client->getResponse()->getContent(), true);
265 315
266 $this->assertGreaterThanOrEqual(1, count($content)); 316 $this->assertGreaterThanOrEqual(1, count($content));
267 $this->assertNotEmpty($content['_embedded']['items']); 317 $this->assertNotEmpty($content['_embedded']['items']);
268 $this->assertGreaterThanOrEqual(1, $content['total']); 318 $this->assertGreaterThanOrEqual(1, $content['total']);
269 $this->assertEquals(1, $content['page']); 319 $this->assertSame(1, $content['page']);
270 $this->assertGreaterThanOrEqual(1, $content['pages']); 320 $this->assertGreaterThanOrEqual(1, $content['pages']);
271 321
272 $this->assertArrayHasKey('_links', $content); 322 $this->assertArrayHasKey('_links', $content);
@@ -279,7 +329,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
279 $this->assertContains('since=1443274283', $content['_links'][$link]['href']); 329 $this->assertContains('since=1443274283', $content['_links'][$link]['href']);
280 } 330 }
281 331
282 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); 332 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
283 } 333 }
284 334
285 public function testGetDatedSupEntries() 335 public function testGetDatedSupEntries()
@@ -287,15 +337,15 @@ class EntryRestControllerTest extends WallabagApiTestCase
287 $future = new \DateTime(date('Y-m-d H:i:s')); 337 $future = new \DateTime(date('Y-m-d H:i:s'));
288 $this->client->request('GET', '/api/entries', ['since' => $future->getTimestamp() + 1000]); 338 $this->client->request('GET', '/api/entries', ['since' => $future->getTimestamp() + 1000]);
289 339
290 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 340 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
291 341
292 $content = json_decode($this->client->getResponse()->getContent(), true); 342 $content = json_decode($this->client->getResponse()->getContent(), true);
293 343
294 $this->assertGreaterThanOrEqual(1, count($content)); 344 $this->assertGreaterThanOrEqual(1, count($content));
295 $this->assertEmpty($content['_embedded']['items']); 345 $this->assertEmpty($content['_embedded']['items']);
296 $this->assertEquals(0, $content['total']); 346 $this->assertSame(0, $content['total']);
297 $this->assertEquals(1, $content['page']); 347 $this->assertSame(1, $content['page']);
298 $this->assertEquals(1, $content['pages']); 348 $this->assertSame(1, $content['pages']);
299 349
300 $this->assertArrayHasKey('_links', $content); 350 $this->assertArrayHasKey('_links', $content);
301 $this->assertArrayHasKey('self', $content['_links']); 351 $this->assertArrayHasKey('self', $content['_links']);
@@ -304,10 +354,10 @@ class EntryRestControllerTest extends WallabagApiTestCase
304 354
305 foreach (['self', 'first', 'last'] as $link) { 355 foreach (['self', 'first', 'last'] as $link) {
306 $this->assertArrayHasKey('href', $content['_links'][$link]); 356 $this->assertArrayHasKey('href', $content['_links'][$link]);
307 $this->assertContains('since='.($future->getTimestamp() + 1000), $content['_links'][$link]['href']); 357 $this->assertContains('since=' . ($future->getTimestamp() + 1000), $content['_links'][$link]['href']);
308 } 358 }
309 359
310 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); 360 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
311 } 361 }
312 362
313 public function testDeleteEntry() 363 public function testDeleteEntry()
@@ -315,25 +365,25 @@ class EntryRestControllerTest extends WallabagApiTestCase
315 $entry = $this->client->getContainer() 365 $entry = $this->client->getContainer()
316 ->get('doctrine.orm.entity_manager') 366 ->get('doctrine.orm.entity_manager')
317 ->getRepository('WallabagCoreBundle:Entry') 367 ->getRepository('WallabagCoreBundle:Entry')
318 ->findOneByUser(1); 368 ->findOneByUser(1, ['id' => 'asc']);
319 369
320 if (!$entry) { 370 if (!$entry) {
321 $this->markTestSkipped('No content found in db.'); 371 $this->markTestSkipped('No content found in db.');
322 } 372 }
323 373
324 $this->client->request('DELETE', '/api/entries/'.$entry->getId().'.json'); 374 $this->client->request('DELETE', '/api/entries/' . $entry->getId() . '.json');
325 375
326 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 376 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
327 377
328 $content = json_decode($this->client->getResponse()->getContent(), true); 378 $content = json_decode($this->client->getResponse()->getContent(), true);
329 379
330 $this->assertEquals($entry->getTitle(), $content['title']); 380 $this->assertSame($entry->getTitle(), $content['title']);
331 $this->assertEquals($entry->getUrl(), $content['url']); 381 $this->assertSame($entry->getUrl(), $content['url']);
332 382
333 // We'll try to delete this entry again 383 // We'll try to delete this entry again
334 $this->client->request('DELETE', '/api/entries/'.$entry->getId().'.json'); 384 $this->client->request('DELETE', '/api/entries/' . $entry->getId() . '.json');
335 385
336 $this->assertEquals(404, $this->client->getResponse()->getStatusCode()); 386 $this->assertSame(404, $this->client->getResponse()->getStatusCode());
337 } 387 }
338 388
339 public function testPostEntry() 389 public function testPostEntry()
@@ -342,38 +392,60 @@ 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', 392 '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', 393 'tags' => 'google',
344 'title' => 'New title for my article', 394 'title' => 'New title for my article',
395 'content' => 'my content',
396 'language' => 'de',
397 'published_at' => '2016-09-08T11:55:58+0200',
398 'authors' => 'bob,helen',
399 'public' => 1,
345 ]); 400 ]);
346 401
347 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 402 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
348 403
349 $content = json_decode($this->client->getResponse()->getContent(), true); 404 $content = json_decode($this->client->getResponse()->getContent(), true);
350 405
351 $this->assertGreaterThan(0, $content['id']); 406 $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']); 407 $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']); 408 $this->assertSame(0, $content['is_archived']);
354 $this->assertEquals(false, $content['is_starred']); 409 $this->assertSame(0, $content['is_starred']);
355 $this->assertEquals('New title for my article', $content['title']); 410 $this->assertSame('New title for my article', $content['title']);
356 $this->assertEquals(1, $content['user_id']); 411 $this->assertSame(1, $content['user_id']);
357 $this->assertCount(1, $content['tags']); 412 $this->assertCount(2, $content['tags']);
413 $this->assertSame('my content', $content['content']);
414 $this->assertSame('de', $content['language']);
415 $this->assertSame('2016-09-08T11:55:58+0200', $content['published_at']);
416 $this->assertCount(2, $content['published_by']);
417 $this->assertContains('bob', $content['published_by']);
418 $this->assertContains('helen', $content['published_by']);
419 $this->assertTrue($content['is_public'], 'A public link has been generated for that entry');
358 } 420 }
359 421
360 public function testPostSameEntry() 422 public function testPostSameEntry()
361 { 423 {
424 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
425 $entry = new Entry($em->getReference(User::class, 1));
426 $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');
427 $entry->setArchived(true);
428 $entry->addTag((new Tag())->setLabel('google'));
429 $entry->addTag((new Tag())->setLabel('apple'));
430 $em->persist($entry);
431 $em->flush();
432 $em->clear();
433
362 $this->client->request('POST', '/api/entries.json', [ 434 $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', 435 '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', 436 'archive' => '1',
365 'tags' => 'google, apple', 437 'tags' => 'google, apple',
366 ]); 438 ]);
367 439
368 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 440 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
369 441
370 $content = json_decode($this->client->getResponse()->getContent(), true); 442 $content = json_decode($this->client->getResponse()->getContent(), true);
371 443
372 $this->assertGreaterThan(0, $content['id']); 444 $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']); 445 $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']); 446 $this->assertSame(1, $content['is_archived']);
375 $this->assertEquals(false, $content['is_starred']); 447 $this->assertSame(0, $content['is_starred']);
376 $this->assertCount(2, $content['tags']); 448 $this->assertCount(3, $content['tags']);
377 } 449 }
378 450
379 public function testPostEntryWhenFetchContentFails() 451 public function testPostEntryWhenFetchContentFails()
@@ -394,10 +466,10 @@ class EntryRestControllerTest extends WallabagApiTestCase
394 'url' => 'http://www.example.com/', 466 'url' => 'http://www.example.com/',
395 ]); 467 ]);
396 468
397 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 469 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
398 $content = json_decode($this->client->getResponse()->getContent(), true); 470 $content = json_decode($this->client->getResponse()->getContent(), true);
399 $this->assertGreaterThan(0, $content['id']); 471 $this->assertGreaterThan(0, $content['id']);
400 $this->assertEquals('http://www.example.com/', $content['url']); 472 $this->assertSame('http://www.example.com/', $content['url']);
401 } finally { 473 } finally {
402 // Remove the created entry to avoid side effects on other tests 474 // Remove the created entry to avoid side effects on other tests
403 if (isset($content['id'])) { 475 if (isset($content['id'])) {
@@ -417,15 +489,15 @@ class EntryRestControllerTest extends WallabagApiTestCase
417 'starred' => '1', 489 'starred' => '1',
418 ]); 490 ]);
419 491
420 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 492 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
421 493
422 $content = json_decode($this->client->getResponse()->getContent(), true); 494 $content = json_decode($this->client->getResponse()->getContent(), true);
423 495
424 $this->assertGreaterThan(0, $content['id']); 496 $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']); 497 $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']); 498 $this->assertSame(1, $content['is_archived']);
427 $this->assertEquals(true, $content['is_starred']); 499 $this->assertSame(1, $content['is_starred']);
428 $this->assertEquals(1, $content['user_id']); 500 $this->assertSame(1, $content['user_id']);
429 } 501 }
430 502
431 public function testPostArchivedAndStarredEntryWithoutQuotes() 503 public function testPostArchivedAndStarredEntryWithoutQuotes()
@@ -436,14 +508,14 @@ class EntryRestControllerTest extends WallabagApiTestCase
436 'starred' => 1, 508 'starred' => 1,
437 ]); 509 ]);
438 510
439 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 511 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
440 512
441 $content = json_decode($this->client->getResponse()->getContent(), true); 513 $content = json_decode($this->client->getResponse()->getContent(), true);
442 514
443 $this->assertGreaterThan(0, $content['id']); 515 $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']); 516 $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']); 517 $this->assertSame(0, $content['is_archived']);
446 $this->assertEquals(true, $content['is_starred']); 518 $this->assertSame(1, $content['is_starred']);
447 } 519 }
448 520
449 public function testPatchEntry() 521 public function testPatchEntry()
@@ -457,25 +529,35 @@ class EntryRestControllerTest extends WallabagApiTestCase
457 $this->markTestSkipped('No content found in db.'); 529 $this->markTestSkipped('No content found in db.');
458 } 530 }
459 531
460 // hydrate the tags relations 532 $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', 533 'title' => 'New awesome title',
465 'tags' => 'new tag '.uniqid(), 534 'tags' => 'new tag ' . uniqid(),
466 'starred' => '1', 535 'starred' => '1',
467 'archive' => '0', 536 'archive' => '0',
537 'language' => 'de_AT',
538 'preview_picture' => 'http://preview.io/picture.jpg',
539 'authors' => 'bob,sponge',
540 'content' => 'awesome',
541 'public' => 0,
542 'published_at' => 1488833381,
468 ]); 543 ]);
469 544
470 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 545 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
471 546
472 $content = json_decode($this->client->getResponse()->getContent(), true); 547 $content = json_decode($this->client->getResponse()->getContent(), true);
473 548
474 $this->assertEquals($entry->getId(), $content['id']); 549 $this->assertSame($entry->getId(), $content['id']);
475 $this->assertEquals($entry->getUrl(), $content['url']); 550 $this->assertSame($entry->getUrl(), $content['url']);
476 $this->assertEquals('New awesome title', $content['title']); 551 $this->assertSame('New awesome title', $content['title']);
477 $this->assertGreaterThan($nbTags, count($content['tags'])); 552 $this->assertGreaterThanOrEqual(1, count($content['tags']), 'We force only one tag');
478 $this->assertEquals(1, $content['user_id']); 553 $this->assertSame(1, $content['user_id']);
554 $this->assertSame('de_AT', $content['language']);
555 $this->assertSame('http://preview.io/picture.jpg', $content['preview_picture']);
556 $this->assertContains('sponge', $content['published_by']);
557 $this->assertContains('bob', $content['published_by']);
558 $this->assertSame('awesome', $content['content']);
559 $this->assertFalse($content['is_public'], 'Entry is no more shared');
560 $this->assertContains('2017-03-06', $content['published_at']);
479 } 561 }
480 562
481 public function testPatchEntryWithoutQuotes() 563 public function testPatchEntryWithoutQuotes()
@@ -489,24 +571,27 @@ class EntryRestControllerTest extends WallabagApiTestCase
489 $this->markTestSkipped('No content found in db.'); 571 $this->markTestSkipped('No content found in db.');
490 } 572 }
491 573
492 // hydrate the tags relations 574 $previousContent = $entry->getContent();
493 $nbTags = count($entry->getTags()); 575 $previousLanguage = $entry->getLanguage();
494 576
495 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [ 577 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
496 'title' => 'New awesome title', 578 'title' => 'New awesome title',
497 'tags' => 'new tag '.uniqid(), 579 'tags' => 'new tag ' . uniqid(),
498 'starred' => 1, 580 'starred' => 1,
499 'archive' => 0, 581 'archive' => 0,
582 'authors' => ['bob', 'sponge'],
500 ]); 583 ]);
501 584
502 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 585 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
503 586
504 $content = json_decode($this->client->getResponse()->getContent(), true); 587 $content = json_decode($this->client->getResponse()->getContent(), true);
505 588
506 $this->assertEquals($entry->getId(), $content['id']); 589 $this->assertSame($entry->getId(), $content['id']);
507 $this->assertEquals($entry->getUrl(), $content['url']); 590 $this->assertSame($entry->getUrl(), $content['url']);
508 $this->assertEquals('New awesome title', $content['title']); 591 $this->assertGreaterThanOrEqual(1, count($content['tags']), 'We force only one tag');
509 $this->assertGreaterThan($nbTags, count($content['tags'])); 592 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
593 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
594 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
510 } 595 }
511 596
512 public function testGetTagsEntry() 597 public function testGetTagsEntry()
@@ -527,9 +612,9 @@ class EntryRestControllerTest extends WallabagApiTestCase
527 $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()]; 612 $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()];
528 } 613 }
529 614
530 $this->client->request('GET', '/api/entries/'.$entry->getId().'/tags'); 615 $this->client->request('GET', '/api/entries/' . $entry->getId() . '/tags');
531 616
532 $this->assertEquals(json_encode($tags, JSON_HEX_QUOT), $this->client->getResponse()->getContent()); 617 $this->assertSame(json_encode($tags, JSON_HEX_QUOT), $this->client->getResponse()->getContent());
533 } 618 }
534 619
535 public function testPostTagsOnEntry() 620 public function testPostTagsOnEntry()
@@ -547,14 +632,14 @@ class EntryRestControllerTest extends WallabagApiTestCase
547 632
548 $newTags = 'tag1,tag2,tag3'; 633 $newTags = 'tag1,tag2,tag3';
549 634
550 $this->client->request('POST', '/api/entries/'.$entry->getId().'/tags', ['tags' => $newTags]); 635 $this->client->request('POST', '/api/entries/' . $entry->getId() . '/tags', ['tags' => $newTags]);
551 636
552 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 637 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
553 638
554 $content = json_decode($this->client->getResponse()->getContent(), true); 639 $content = json_decode($this->client->getResponse()->getContent(), true);
555 640
556 $this->assertArrayHasKey('tags', $content); 641 $this->assertArrayHasKey('tags', $content);
557 $this->assertEquals($nbTags + 3, count($content['tags'])); 642 $this->assertSame($nbTags + 3, count($content['tags']));
558 643
559 $entryDB = $this->client->getContainer() 644 $entryDB = $this->client->getContainer()
560 ->get('doctrine.orm.entity_manager') 645 ->get('doctrine.orm.entity_manager')
@@ -587,14 +672,14 @@ class EntryRestControllerTest extends WallabagApiTestCase
587 $nbTags = count($entry->getTags()); 672 $nbTags = count($entry->getTags());
588 $tag = $entry->getTags()[0]; 673 $tag = $entry->getTags()[0];
589 674
590 $this->client->request('DELETE', '/api/entries/'.$entry->getId().'/tags/'.$tag->getId().'.json'); 675 $this->client->request('DELETE', '/api/entries/' . $entry->getId() . '/tags/' . $tag->getId() . '.json');
591 676
592 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 677 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
593 678
594 $content = json_decode($this->client->getResponse()->getContent(), true); 679 $content = json_decode($this->client->getResponse()->getContent(), true);
595 680
596 $this->assertArrayHasKey('tags', $content); 681 $this->assertArrayHasKey('tags', $content);
597 $this->assertEquals($nbTags - 1, count($content['tags'])); 682 $this->assertSame($nbTags - 1, count($content['tags']));
598 } 683 }
599 684
600 public function testSaveIsArchivedAfterPost() 685 public function testSaveIsArchivedAfterPost()
@@ -612,11 +697,11 @@ class EntryRestControllerTest extends WallabagApiTestCase
612 'url' => $entry->getUrl(), 697 'url' => $entry->getUrl(),
613 ]); 698 ]);
614 699
615 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 700 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
616 701
617 $content = json_decode($this->client->getResponse()->getContent(), true); 702 $content = json_decode($this->client->getResponse()->getContent(), true);
618 703
619 $this->assertEquals(true, $content['is_archived']); 704 $this->assertSame(1, $content['is_archived']);
620 } 705 }
621 706
622 public function testSaveIsStarredAfterPost() 707 public function testSaveIsStarredAfterPost()
@@ -634,11 +719,11 @@ class EntryRestControllerTest extends WallabagApiTestCase
634 'url' => $entry->getUrl(), 719 'url' => $entry->getUrl(),
635 ]); 720 ]);
636 721
637 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 722 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
638 723
639 $content = json_decode($this->client->getResponse()->getContent(), true); 724 $content = json_decode($this->client->getResponse()->getContent(), true);
640 725
641 $this->assertEquals(true, $content['is_starred']); 726 $this->assertSame(1, $content['is_starred']);
642 } 727 }
643 728
644 public function testSaveIsArchivedAfterPatch() 729 public function testSaveIsArchivedAfterPatch()
@@ -652,15 +737,18 @@ class EntryRestControllerTest extends WallabagApiTestCase
652 $this->markTestSkipped('No content found in db.'); 737 $this->markTestSkipped('No content found in db.');
653 } 738 }
654 739
655 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [ 740 $previousTitle = $entry->getTitle();
656 'title' => $entry->getTitle().'++', 741
742 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
743 'title' => $entry->getTitle() . '++',
657 ]); 744 ]);
658 745
659 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 746 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
660 747
661 $content = json_decode($this->client->getResponse()->getContent(), true); 748 $content = json_decode($this->client->getResponse()->getContent(), true);
662 749
663 $this->assertEquals(true, $content['is_archived']); 750 $this->assertSame(1, $content['is_archived']);
751 $this->assertSame($previousTitle . '++', $content['title']);
664 } 752 }
665 753
666 public function testSaveIsStarredAfterPatch() 754 public function testSaveIsStarredAfterPatch()
@@ -673,60 +761,93 @@ class EntryRestControllerTest extends WallabagApiTestCase
673 if (!$entry) { 761 if (!$entry) {
674 $this->markTestSkipped('No content found in db.'); 762 $this->markTestSkipped('No content found in db.');
675 } 763 }
676 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [ 764 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
677 'title' => $entry->getTitle().'++', 765 'title' => $entry->getTitle() . '++',
678 ]); 766 ]);
679 767
680 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 768 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
681 769
682 $content = json_decode($this->client->getResponse()->getContent(), true); 770 $content = json_decode($this->client->getResponse()->getContent(), true);
683 771
684 $this->assertEquals(true, $content['is_starred']); 772 $this->assertSame(1, $content['is_starred']);
773 }
774
775 public function dataForEntriesExistWithUrl()
776 {
777 return [
778 'with_id' => [
779 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2&return_id=1',
780 'expectedValue' => 2,
781 ],
782 'without_id' => [
783 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2',
784 'expectedValue' => true,
785 ],
786 ];
685 } 787 }
686 788
687 public function testGetEntriesExists() 789 /**
790 * @dataProvider dataForEntriesExistWithUrl
791 */
792 public function testGetEntriesExists($url, $expectedValue)
688 { 793 {
689 $this->client->request('GET', '/api/entries/exists?url=http://0.0.0.0/entry2'); 794 $this->client->request('GET', $url);
690 795
691 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 796 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
692 797
693 $content = json_decode($this->client->getResponse()->getContent(), true); 798 $content = json_decode($this->client->getResponse()->getContent(), true);
694 799
695 $this->assertEquals(true, $content['exists']); 800 $this->assertSame($expectedValue, $content['exists']);
696 } 801 }
697 802
698 public function testGetEntriesExistsWithManyUrls() 803 public function testGetEntriesExistsWithManyUrls()
699 { 804 {
700 $url1 = 'http://0.0.0.0/entry2'; 805 $url1 = 'http://0.0.0.0/entry2';
701 $url2 = 'http://0.0.0.0/entry10'; 806 $url2 = 'http://0.0.0.0/entry10';
702 $this->client->request('GET', '/api/entries/exists?urls[]='.$url1.'&urls[]='.$url2); 807 $this->client->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2 . '&return_id=1');
703 808
704 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 809 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
705 810
706 $content = json_decode($this->client->getResponse()->getContent(), true); 811 $content = json_decode($this->client->getResponse()->getContent(), true);
707 812
708 $this->assertArrayHasKey($url1, $content); 813 $this->assertArrayHasKey($url1, $content);
709 $this->assertArrayHasKey($url2, $content); 814 $this->assertArrayHasKey($url2, $content);
710 $this->assertEquals(true, $content[$url1]); 815 $this->assertSame(2, $content[$url1]);
711 $this->assertEquals(false, $content[$url2]); 816 $this->assertNull($content[$url2]);
817 }
818
819 public function testGetEntriesExistsWithManyUrlsReturnBool()
820 {
821 $url1 = 'http://0.0.0.0/entry2';
822 $url2 = 'http://0.0.0.0/entry10';
823 $this->client->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2);
824
825 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
826
827 $content = json_decode($this->client->getResponse()->getContent(), true);
828
829 $this->assertArrayHasKey($url1, $content);
830 $this->assertArrayHasKey($url2, $content);
831 $this->assertTrue($content[$url1]);
832 $this->assertFalse($content[$url2]);
712 } 833 }
713 834
714 public function testGetEntriesExistsWhichDoesNotExists() 835 public function testGetEntriesExistsWhichDoesNotExists()
715 { 836 {
716 $this->client->request('GET', '/api/entries/exists?url=http://google.com/entry2'); 837 $this->client->request('GET', '/api/entries/exists?url=http://google.com/entry2');
717 838
718 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 839 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
719 840
720 $content = json_decode($this->client->getResponse()->getContent(), true); 841 $content = json_decode($this->client->getResponse()->getContent(), true);
721 842
722 $this->assertEquals(false, $content['exists']); 843 $this->assertSame(false, $content['exists']);
723 } 844 }
724 845
725 public function testGetEntriesExistsWithNoUrl() 846 public function testGetEntriesExistsWithNoUrl()
726 { 847 {
727 $this->client->request('GET', '/api/entries/exists?url='); 848 $this->client->request('GET', '/api/entries/exists?url=');
728 849
729 $this->assertEquals(403, $this->client->getResponse()->getStatusCode()); 850 $this->assertSame(403, $this->client->getResponse()->getStatusCode());
730 } 851 }
731 852
732 public function testReloadEntryErrorWhileFetching() 853 public function testReloadEntryErrorWhileFetching()
@@ -739,8 +860,8 @@ class EntryRestControllerTest extends WallabagApiTestCase
739 $this->markTestSkipped('No content found in db.'); 860 $this->markTestSkipped('No content found in db.');
740 } 861 }
741 862
742 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'/reload.json'); 863 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '/reload.json');
743 $this->assertEquals(304, $this->client->getResponse()->getStatusCode()); 864 $this->assertSame(304, $this->client->getResponse()->getStatusCode());
744 } 865 }
745 866
746 public function testReloadEntry() 867 public function testReloadEntry()
@@ -755,13 +876,208 @@ class EntryRestControllerTest extends WallabagApiTestCase
755 876
756 $this->setUp(); 877 $this->setUp();
757 878
758 $this->client->request('PATCH', '/api/entries/'.$json['id'].'/reload.json'); 879 $this->client->request('PATCH', '/api/entries/' . $json['id'] . '/reload.json');
759 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 880 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
760 881
761 $content = json_decode($this->client->getResponse()->getContent(), true); 882 $content = json_decode($this->client->getResponse()->getContent(), true);
762 883
763 $this->assertNotEmpty($content['title']); 884 $this->assertNotEmpty($content['title']);
764 885
765 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); 886 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
887 }
888
889 public function testPostEntriesTagsListAction()
890 {
891 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
892 ->getRepository('WallabagCoreBundle:Entry')
893 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
894
895 $tags = $entry->getTags();
896
897 $this->assertCount(2, $tags);
898
899 $list = [
900 [
901 'url' => 'http://0.0.0.0/entry4',
902 'tags' => 'new tag 1, new tag 2',
903 ],
904 ];
905
906 $this->client->request('POST', '/api/entries/tags/lists?list=' . json_encode($list));
907
908 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
909
910 $content = json_decode($this->client->getResponse()->getContent(), true);
911
912 $this->assertInternalType('int', $content[0]['entry']);
913 $this->assertSame('http://0.0.0.0/entry4', $content[0]['url']);
914
915 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
916 ->getRepository('WallabagCoreBundle:Entry')
917 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
918
919 $tags = $entry->getTags();
920 $this->assertCount(4, $tags);
921 }
922
923 public function testPostEntriesTagsListActionNoList()
924 {
925 $this->client->request('POST', '/api/entries/tags/lists?list=' . json_encode([]));
926
927 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
928
929 $content = json_decode($this->client->getResponse()->getContent(), true);
930
931 $this->assertEmpty($content);
932 }
933
934 public function testDeleteEntriesTagsListAction()
935 {
936 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
937 $entry = new Entry($em->getReference(User::class, 1));
938 $entry->setUrl('http://0.0.0.0/test-entry');
939 $entry->addTag((new Tag())->setLabel('foo-tag'));
940 $entry->addTag((new Tag())->setLabel('bar-tag'));
941 $em->persist($entry);
942 $em->flush();
943
944 $em->clear();
945
946 $list = [
947 [
948 'url' => 'http://0.0.0.0/test-entry',
949 'tags' => 'foo-tag, bar-tag',
950 ],
951 ];
952
953 $this->client->request('DELETE', '/api/entries/tags/list?list=' . json_encode($list));
954 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
955
956 $entry = $em->getRepository('WallabagCoreBundle:Entry')->find($entry->getId());
957 $this->assertCount(0, $entry->getTags());
958 }
959
960 public function testDeleteEntriesTagsListActionNoList()
961 {
962 $this->client->request('DELETE', '/api/entries/tags/list?list=' . json_encode([]));
963
964 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
965
966 $content = json_decode($this->client->getResponse()->getContent(), true);
967
968 $this->assertEmpty($content);
969 }
970
971 public function testPostEntriesListAction()
972 {
973 $list = [
974 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html',
975 'http://0.0.0.0/entry2',
976 ];
977
978 $this->client->request('POST', '/api/entries/lists?urls=' . json_encode($list));
979
980 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
981
982 $content = json_decode($this->client->getResponse()->getContent(), true);
983
984 $this->assertInternalType('int', $content[0]['entry']);
985 $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']);
986
987 $this->assertInternalType('int', $content[1]['entry']);
988 $this->assertSame('http://0.0.0.0/entry2', $content[1]['url']);
989 }
990
991 public function testPostEntriesListActionWithNoUrls()
992 {
993 $this->client->request('POST', '/api/entries/lists?urls=' . json_encode([]));
994
995 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
996
997 $content = json_decode($this->client->getResponse()->getContent(), true);
998
999 $this->assertEmpty($content);
1000 }
1001
1002 public function testDeleteEntriesListAction()
1003 {
1004 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
1005 $em->persist((new Entry($em->getReference(User::class, 1)))->setUrl('http://0.0.0.0/test-entry1'));
1006
1007 $em->flush();
1008 $em->clear();
1009 $list = [
1010 'http://0.0.0.0/test-entry1',
1011 'http://0.0.0.0/test-entry-not-exist',
1012 ];
1013
1014 $this->client->request('DELETE', '/api/entries/list?urls=' . json_encode($list));
1015
1016 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1017
1018 $content = json_decode($this->client->getResponse()->getContent(), true);
1019
1020 $this->assertTrue($content[0]['entry']);
1021 $this->assertSame('http://0.0.0.0/test-entry1', $content[0]['url']);
1022
1023 $this->assertFalse($content[1]['entry']);
1024 $this->assertSame('http://0.0.0.0/test-entry-not-exist', $content[1]['url']);
1025 }
1026
1027 public function testDeleteEntriesListActionWithNoUrls()
1028 {
1029 $this->client->request('DELETE', '/api/entries/list?urls=' . json_encode([]));
1030
1031 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1032
1033 $content = json_decode($this->client->getResponse()->getContent(), true);
1034
1035 $this->assertEmpty($content);
1036 }
1037
1038 public function testLimitBulkAction()
1039 {
1040 $list = [
1041 'http://0.0.0.0/entry1',
1042 'http://0.0.0.0/entry1',
1043 'http://0.0.0.0/entry1',
1044 'http://0.0.0.0/entry1',
1045 'http://0.0.0.0/entry1',
1046 'http://0.0.0.0/entry1',
1047 'http://0.0.0.0/entry1',
1048 'http://0.0.0.0/entry1',
1049 'http://0.0.0.0/entry1',
1050 'http://0.0.0.0/entry1',
1051 'http://0.0.0.0/entry1',
1052 ];
1053
1054 $this->client->request('POST', '/api/entries/lists?urls=' . json_encode($list));
1055
1056 $this->assertSame(400, $this->client->getResponse()->getStatusCode());
1057 $this->assertContains('API limit reached', $this->client->getResponse()->getContent());
1058 }
1059
1060 public function testRePostEntryAndReUsePublishedAt()
1061 {
1062 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
1063 $entry = new Entry($em->getReference(User::class, 1));
1064 $entry->setTitle('Antoine de Caunes : « Je veux avoir le droit de tâtonner »');
1065 $entry->setContent('hihi');
1066 $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');
1067 $entry->setPublishedAt(new \DateTime('2017-06-26T07:46:02+0200'));
1068 $em->persist($entry);
1069 $em->flush();
1070 $em->clear();
1071
1072 $this->client->request('POST', '/api/entries.json', [
1073 'url' => 'http://www.lemonde.fr/m-perso/article/2017/06/25/antoine-de-caunes-je-veux-avoir-le-droit-de-tatonner_5150728_4497916.html',
1074 ]);
1075
1076 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1077
1078 $content = json_decode($this->client->getResponse()->getContent(), true);
1079
1080 $this->assertGreaterThan(0, $content['id']);
1081 $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 } 1082 }
767} 1083}
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
3namespace Tests\Wallabag\ApiBundle\Controller;
4
5use Tests\Wallabag\ApiBundle\WallabagApiTestCase;
6
7class 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;
8abstract class WallabagApiTestCase extends WebTestCase 8abstract 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');