3 namespace Wallabag\CoreBundle\Tests\Controller
;
5 use Wallabag\CoreBundle\Tests\WallabagTestCase
;
7 class ConfigControllerTest
extends WallabagTestCase
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=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();
47 'config[theme]' => 'baggy',
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()
66 'config[theme]' => 'baggy',
67 'config[items_per_page]' => '',
68 'config[language]' => 'fr_FR',
71 'config[theme]' => 'baggy',
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]' => 'baggy',
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 'user[username]' => '',
196 'This value should not be blank.',
200 'user[username]' => 'ad',
204 'This value is too short.',
208 'user[username]' => 'admin',
210 'user[email]' => 'test',
212 'This value is not a valid email address.',
218 * @dataProvider dataForUserFailed
220 public function testUserFailed($data, $expectedMessage)
222 $this->logInAs('admin');
223 $client = $this->getClient();
225 $crawler = $client->request('GET', '/config');
227 $this->assertEquals(200, $client->getResponse()->getStatusCode());
229 $form = $crawler->filter('button[id=user_save]')->form();
231 $crawler = $client->submit($form, $data);
233 $this->assertEquals(200, $client->getResponse()->getStatusCode());
235 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(array('_text')));
236 $this->assertContains($expectedMessage, $alert[0]);
239 public function testUserUpdate()
241 $this->logInAs('admin');
242 $client = $this->getClient();
244 $crawler = $client->request('GET', '/config');
246 $this->assertEquals(200, $client->getResponse()->getStatusCode());
248 $form = $crawler->filter('button[id=user_save]')->form();
251 'user[username]' => 'admin',
252 'user[name]' => 'new name',
253 'user[email]' => 'admin@wallabag.io',
256 $client->submit($form, $data);
258 $this->assertEquals(302, $client->getResponse()->getStatusCode());
260 $crawler = $client->followRedirect();
262 $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(array('_text')));
263 $this->assertContains('Information updated', $alert[0]);
266 public function dataForNewUserFailed()
271 'new_user[username]' => '',
272 'new_user[password]' => '',
273 'new_user[email]' => '',
275 'This value should not be blank.',
279 'new_user[username]' => 'ad',
280 'new_user[password]' => '',
281 'new_user[email]' => '',
283 'This value is too short.',
287 'new_user[username]' => 'wallace',
288 'new_user[password]' => '',
289 'new_user[email]' => 'test',
291 'This value is not a valid email address.',
295 'new_user[username]' => 'wallace',
296 'new_user[password]' => 'admin',
297 'new_user[email]' => 'wallace@wallace.me',
299 'Password should by at least',
305 * @dataProvider dataForNewUserFailed
307 public function testNewUserFailed($data, $expectedMessage)
309 $this->logInAs('admin');
310 $client = $this->getClient();
312 $crawler = $client->request('GET', '/config');
314 $this->assertEquals(200, $client->getResponse()->getStatusCode());
316 $form = $crawler->filter('button[id=new_user_save]')->form();
318 $crawler = $client->submit($form, $data);
320 $this->assertEquals(200, $client->getResponse()->getStatusCode());
322 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(array('_text')));
323 $this->assertContains($expectedMessage, $alert[0]);
326 public function testNewUserCreated()
328 $this->logInAs('admin');
329 $client = $this->getClient();
331 $crawler = $client->request('GET', '/config');
333 $this->assertEquals(200, $client->getResponse()->getStatusCode());
335 $form = $crawler->filter('button[id=new_user_save]')->form();
338 'new_user[username]' => 'wallace',
339 'new_user[password]' => 'wallace1',
340 'new_user[email]' => 'wallace@wallace.me',
343 $client->submit($form, $data);
345 $this->assertEquals(302, $client->getResponse()->getStatusCode());
347 $crawler = $client->followRedirect();
349 $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(array('_text')));
350 $this->assertContains('User "wallace" added', $alert[0]);
353 public function testRssUpdateResetToken()
355 $this->logInAs('admin');
356 $client = $this->getClient();
359 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
361 ->getRepository('WallabagCoreBundle:User')
362 ->findOneByUsername('admin');
365 $this->markTestSkipped('No user found in db.');
368 $config = $user->getConfig();
369 $config->setRssToken(null);
370 $em->persist($config);
373 $crawler = $client->request('GET', '/config');
375 $this->assertEquals(200, $client->getResponse()->getStatusCode());
377 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(array('_text')));
378 $this->assertContains('You need to generate a token first.', $body[0]);
380 $client->request('GET', '/generate-token');
381 $this->assertEquals(302, $client->getResponse()->getStatusCode());
383 $crawler = $client->followRedirect();
385 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(array('_text')));
386 $this->assertNotContains('You need to generate a token first.', $body[0]);
389 public function testGenerateTokenAjax()
391 $this->logInAs('admin');
392 $client = $this->getClient();
399 array('HTTP_X-Requested-With' => 'XMLHttpRequest')
402 $this->assertEquals(200, $client->getResponse()->getStatusCode());
403 $content = json_decode($client->getResponse()->getContent(), true);;
404 $this->assertArrayHasKey('token', $content);
407 public function testRssUpdate()
409 $this->logInAs('admin');
410 $client = $this->getClient();
412 $crawler = $client->request('GET', '/config');
414 if (500 == $client->getResponse()->getStatusCode()) {
415 var_export($client->getResponse()->getContent());
419 $this->assertEquals(200, $client->getResponse()->getStatusCode());
421 $form = $crawler->filter('button[id=rss_config_save]')->form();
424 'rss_config[rss_limit]' => 12,
427 $client->submit($form, $data);
429 $this->assertEquals(302, $client->getResponse()->getStatusCode());
431 $crawler = $client->followRedirect();
433 $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(array('_text')));
434 $this->assertContains('RSS information updated', $alert[0]);
437 public function dataForRssFailed()
442 'rss_config[rss_limit]' => 0,
444 'This value should be 1 or more.',
448 'rss_config[rss_limit]' => 1000000000000,
450 'This will certainly kill the app',
456 * @dataProvider dataForRssFailed
458 public function testRssFailed($data, $expectedMessage)
460 $this->logInAs('admin');
461 $client = $this->getClient();
463 $crawler = $client->request('GET', '/config');
465 $this->assertEquals(200, $client->getResponse()->getStatusCode());
467 $form = $crawler->filter('button[id=rss_config_save]')->form();
469 $crawler = $client->submit($form, $data);
471 $this->assertEquals(200, $client->getResponse()->getStatusCode());
473 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(array('_text')));
474 $this->assertContains($expectedMessage, $alert[0]);