3 namespace Wallabag\CoreBundle\Tests\Controller
;
5 use Wallabag\CoreBundle\Tests\WallabagCoreTestCase
;
7 class ConfigControllerTest
extends WallabagCoreTestCase
9 public function testLogin()
11 $client = $this->getClient();
13 $client->request('GET', '/new');
15 $this->assertEquals(302, $client->getResponse()->getStatusCode());
16 $this->assertContains('login', $client->getResponse()->headers
->get('location'));
19 public function testIndex()
21 $this->logInAs('admin');
22 $client = $this->getClient();
24 $crawler = $client->request('GET', '/config');
26 $this->assertEquals(200, $client->getResponse()->getStatusCode());
28 $this->assertCount(1, $crawler->filter('button[id=config_save]'));
29 $this->assertCount(1, $crawler->filter('button[id=change_passwd_save]'));
30 $this->assertCount(1, $crawler->filter('button[id=update_user_save]'));
31 $this->assertCount(1, $crawler->filter('button[id=new_user_save]'));
32 $this->assertCount(1, $crawler->filter('button[id=rss_config_save]'));
35 public function testUpdate()
37 $this->logInAs('admin');
38 $client = $this->getClient();
40 $crawler = $client->request('GET', '/config');
42 $this->assertEquals(200, $client->getResponse()->getStatusCode());
44 $form = $crawler->filter('button[id=config_save]')->form();
48 'config[items_per_page]' => '30',
49 'config[language]' => 'fr_FR',
52 $client->submit($form, $data);
54 $this->assertEquals(302, $client->getResponse()->getStatusCode());
56 $crawler = $client->followRedirect();
58 $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(array('_text')));
59 $this->assertContains('Config saved', $alert[0]);
62 public function dataForUpdateFailed()
67 'config[items_per_page]' => '',
68 'config[language]' => 'fr_FR',
72 'config[items_per_page]' => '12',
73 'config[language]' => '',
79 * @dataProvider dataForUpdateFailed
81 public function testUpdateFailed($data)
83 $this->logInAs('admin');
84 $client = $this->getClient();
86 $crawler = $client->request('GET', '/config');
88 $this->assertEquals(200, $client->getResponse()->getStatusCode());
90 $form = $crawler->filter('button[id=config_save]')->form();
92 $crawler = $client->submit($form, $data);
94 $this->assertEquals(200, $client->getResponse()->getStatusCode());
96 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(array('_text')));
97 $this->assertContains('This value should not be blank', $alert[0]);
100 public function dataForChangePasswordFailed()
105 'change_passwd[old_password]' => 'material',
106 'change_passwd[new_password][first]' => '',
107 'change_passwd[new_password][second]' => '',
109 'Wrong value for your current password',
113 'change_passwd[old_password]' => 'mypassword',
114 'change_passwd[new_password][first]' => '',
115 'change_passwd[new_password][second]' => '',
117 'This value should not be blank',
121 'change_passwd[old_password]' => 'mypassword',
122 'change_passwd[new_password][first]' => 'hop',
123 'change_passwd[new_password][second]' => '',
125 'The password fields must match',
129 'change_passwd[old_password]' => 'mypassword',
130 'change_passwd[new_password][first]' => 'hop',
131 'change_passwd[new_password][second]' => 'hop',
133 'Password should by at least',
139 * @dataProvider dataForChangePasswordFailed
141 public function testChangePasswordFailed($data, $expectedMessage)
143 $this->logInAs('admin');
144 $client = $this->getClient();
146 $crawler = $client->request('GET', '/config');
148 $this->assertEquals(200, $client->getResponse()->getStatusCode());
150 $form = $crawler->filter('button[id=change_passwd_save]')->form();
152 $crawler = $client->submit($form, $data);
154 $this->assertEquals(200, $client->getResponse()->getStatusCode());
156 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(array('_text')));
157 $this->assertContains($expectedMessage, $alert[0]);
160 public function testChangePassword()
162 $this->logInAs('admin');
163 $client = $this->getClient();
165 $crawler = $client->request('GET', '/config');
167 $this->assertEquals(200, $client->getResponse()->getStatusCode());
169 $form = $crawler->filter('button[id=change_passwd_save]')->form();
172 'change_passwd[old_password]' => 'mypassword',
173 'change_passwd[new_password][first]' => 'mypassword',
174 'change_passwd[new_password][second]' => 'mypassword',
177 $client->submit($form, $data);
179 $this->assertEquals(302, $client->getResponse()->getStatusCode());
181 $crawler = $client->followRedirect();
183 $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(array('_text')));
184 $this->assertContains('Password updated', $alert[0]);
187 public function dataForUserFailed()
192 'update_user[name]' => '',
193 'update_user[email]' => '',
195 'This value should not be blank.',
199 'update_user[name]' => '',
200 'update_user[email]' => 'test',
202 'This value is not a valid email address.',
208 * @dataProvider dataForUserFailed
210 public function testUserFailed($data, $expectedMessage)
212 $this->logInAs('admin');
213 $client = $this->getClient();
215 $crawler = $client->request('GET', '/config');
217 $this->assertEquals(200, $client->getResponse()->getStatusCode());
219 $form = $crawler->filter('button[id=update_user_save]')->form();
221 $crawler = $client->submit($form, $data);
223 $this->assertEquals(200, $client->getResponse()->getStatusCode());
225 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(array('_text')));
226 $this->assertContains($expectedMessage, $alert[0]);
229 public function testUserUpdate()
231 $this->logInAs('admin');
232 $client = $this->getClient();
234 $crawler = $client->request('GET', '/config');
236 $this->assertEquals(200, $client->getResponse()->getStatusCode());
238 $form = $crawler->filter('button[id=update_user_save]')->form();
241 'update_user[name]' => 'new name',
242 'update_user[email]' => 'admin@wallabag.io',
245 $client->submit($form, $data);
247 $this->assertEquals(302, $client->getResponse()->getStatusCode());
249 $crawler = $client->followRedirect();
251 $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(array('_text')));
252 $this->assertContains('Information updated', $alert[0]);
255 public function dataForNewUserFailed()
260 'new_user[username]' => '',
261 'new_user[password]' => '',
262 'new_user[email]' => '',
264 'This value should not be blank.',
268 'new_user[username]' => 'ad',
269 'new_user[password]' => '',
270 'new_user[email]' => '',
272 'This value is too short.',
276 'new_user[username]' => 'wallace',
277 'new_user[password]' => '',
278 'new_user[email]' => 'test',
280 'This value is not a valid email address.',
284 'new_user[username]' => 'wallace',
285 'new_user[password]' => 'admin',
286 'new_user[email]' => 'wallace@wallace.me',
288 'Password should by at least',
292 'new_user[username]' => 'admin',
293 'new_user[password]' => 'wallacewallace',
294 'new_user[email]' => 'wallace@wallace.me',
296 'This value is already used',
302 * @dataProvider dataForNewUserFailed
304 public function testNewUserFailed($data, $expectedMessage)
306 $this->logInAs('admin');
307 $client = $this->getClient();
309 $crawler = $client->request('GET', '/config');
311 $this->assertEquals(200, $client->getResponse()->getStatusCode());
313 $form = $crawler->filter('button[id=new_user_save]')->form();
315 $crawler = $client->submit($form, $data);
317 $this->assertEquals(200, $client->getResponse()->getStatusCode());
319 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(array('_text')));
320 $this->assertContains($expectedMessage, $alert[0]);
323 public function testNewUserCreated()
325 $this->logInAs('admin');
326 $client = $this->getClient();
328 $crawler = $client->request('GET', '/config');
330 $this->assertEquals(200, $client->getResponse()->getStatusCode());
332 $form = $crawler->filter('button[id=new_user_save]')->form();
335 'new_user[username]' => 'wallace',
336 'new_user[password]' => 'wallace1',
337 'new_user[email]' => 'wallace@wallace.me',
340 $client->submit($form, $data);
342 $this->assertEquals(302, $client->getResponse()->getStatusCode());
344 $crawler = $client->followRedirect();
346 $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(array('_text')));
347 $this->assertContains('User "wallace" added', $alert[0]);
350 public function testRssUpdateResetToken()
352 $this->logInAs('admin');
353 $client = $this->getClient();
356 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
358 ->getRepository('WallabagCoreBundle:User')
359 ->findOneByUsername('admin');
362 $this->markTestSkipped('No user found in db.');
365 $config = $user->getConfig();
366 $config->setRssToken(null);
367 $em->persist($config);
370 $crawler = $client->request('GET', '/config');
372 $this->assertEquals(200, $client->getResponse()->getStatusCode());
374 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(array('_text')));
375 $this->assertContains('You need to generate a token first.', $body[0]);
377 $client->request('GET', '/generate-token');
378 $this->assertEquals(302, $client->getResponse()->getStatusCode());
380 $crawler = $client->followRedirect();
382 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(array('_text')));
383 $this->assertNotContains('You need to generate a token first.', $body[0]);
386 public function testGenerateTokenAjax()
388 $this->logInAs('admin');
389 $client = $this->getClient();
396 array('HTTP_X-Requested-With' => 'XMLHttpRequest')
399 $this->assertEquals(200, $client->getResponse()->getStatusCode());
400 $content = json_decode($client->getResponse()->getContent(), true);
401 $this->assertArrayHasKey('token', $content);
404 public function testRssUpdate()
406 $this->logInAs('admin');
407 $client = $this->getClient();
409 $crawler = $client->request('GET', '/config');
411 if (500 == $client->getResponse()->getStatusCode()) {
412 var_export($client->getResponse()->getContent());
416 $this->assertEquals(200, $client->getResponse()->getStatusCode());
418 $form = $crawler->filter('button[id=rss_config_save]')->form();
421 'rss_config[rss_limit]' => 12,
424 $client->submit($form, $data);
426 $this->assertEquals(302, $client->getResponse()->getStatusCode());
428 $crawler = $client->followRedirect();
430 $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(array('_text')));
431 $this->assertContains('RSS information updated', $alert[0]);
434 public function dataForRssFailed()
439 'rss_config[rss_limit]' => 0,
441 'This value should be 1 or more.',
445 'rss_config[rss_limit]' => 1000000000000,
447 'This will certainly kill the app',
453 * @dataProvider dataForRssFailed
455 public function testRssFailed($data, $expectedMessage)
457 $this->logInAs('admin');
458 $client = $this->getClient();
460 $crawler = $client->request('GET', '/config');
462 $this->assertEquals(200, $client->getResponse()->getStatusCode());
464 $form = $crawler->filter('button[id=rss_config_save]')->form();
466 $crawler = $client->submit($form, $data);
468 $this->assertEquals(200, $client->getResponse()->getStatusCode());
470 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(array('_text')));
471 $this->assertContains($expectedMessage, $alert[0]);