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]' => 'en',
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]' => 'en',
74 * @dataProvider dataForUpdateFailed
76 public function testUpdateFailed($data)
78 $this->logInAs('admin');
79 $client = $this->getClient();
81 $crawler = $client->request('GET', '/config');
83 $this->assertEquals(200, $client->getResponse()->getStatusCode());
85 $form = $crawler->filter('button[id=config_save]')->form();
87 $crawler = $client->submit($form, $data);
89 $this->assertEquals(200, $client->getResponse()->getStatusCode());
91 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(array('_text')));
92 $this->assertContains('This value should not be blank', $alert[0]);
95 public function dataForChangePasswordFailed()
100 'change_passwd[old_password]' => 'material',
101 'change_passwd[new_password][first]' => '',
102 'change_passwd[new_password][second]' => '',
104 'Wrong value for your current password',
108 'change_passwd[old_password]' => 'mypassword',
109 'change_passwd[new_password][first]' => '',
110 'change_passwd[new_password][second]' => '',
112 'This value should not be blank',
116 'change_passwd[old_password]' => 'mypassword',
117 'change_passwd[new_password][first]' => 'hop',
118 'change_passwd[new_password][second]' => '',
120 'The password fields must match',
124 'change_passwd[old_password]' => 'mypassword',
125 'change_passwd[new_password][first]' => 'hop',
126 'change_passwd[new_password][second]' => 'hop',
128 'Password should by at least',
134 * @dataProvider dataForChangePasswordFailed
136 public function testChangePasswordFailed($data, $expectedMessage)
138 $this->logInAs('admin');
139 $client = $this->getClient();
141 $crawler = $client->request('GET', '/config');
143 $this->assertEquals(200, $client->getResponse()->getStatusCode());
145 $form = $crawler->filter('button[id=change_passwd_save]')->form();
147 $crawler = $client->submit($form, $data);
149 $this->assertEquals(200, $client->getResponse()->getStatusCode());
151 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(array('_text')));
152 $this->assertContains($expectedMessage, $alert[0]);
155 public function testChangePassword()
157 $this->logInAs('admin');
158 $client = $this->getClient();
160 $crawler = $client->request('GET', '/config');
162 $this->assertEquals(200, $client->getResponse()->getStatusCode());
164 $form = $crawler->filter('button[id=change_passwd_save]')->form();
167 'change_passwd[old_password]' => 'mypassword',
168 'change_passwd[new_password][first]' => 'mypassword',
169 'change_passwd[new_password][second]' => 'mypassword',
172 $client->submit($form, $data);
174 $this->assertEquals(302, $client->getResponse()->getStatusCode());
176 $crawler = $client->followRedirect();
178 $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(array('_text')));
179 $this->assertContains('Password updated', $alert[0]);
182 public function dataForUserFailed()
187 'update_user[name]' => '',
188 'update_user[email]' => '',
190 'Please enter an email',
194 'update_user[name]' => '',
195 'update_user[email]' => 'test',
197 'The email is not valid',
203 * @dataProvider dataForUserFailed
205 public function testUserFailed($data, $expectedMessage)
207 $this->logInAs('admin');
208 $client = $this->getClient();
210 $crawler = $client->request('GET', '/config');
212 $this->assertEquals(200, $client->getResponse()->getStatusCode());
214 $form = $crawler->filter('button[id=update_user_save]')->form();
216 $crawler = $client->submit($form, $data);
218 $this->assertEquals(200, $client->getResponse()->getStatusCode());
220 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(array('_text')));
221 $this->assertContains($expectedMessage, $alert[0]);
224 public function testUserUpdate()
226 $this->logInAs('admin');
227 $client = $this->getClient();
229 $crawler = $client->request('GET', '/config');
231 $this->assertEquals(200, $client->getResponse()->getStatusCode());
233 $form = $crawler->filter('button[id=update_user_save]')->form();
236 'update_user[name]' => 'new name',
237 'update_user[email]' => 'admin@wallabag.io',
240 $client->submit($form, $data);
242 $this->assertEquals(302, $client->getResponse()->getStatusCode());
244 $crawler = $client->followRedirect();
246 $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(array('_text')));
247 $this->assertContains('Information updated', $alert[0]);
250 public function dataForNewUserFailed()
255 'new_user[username]' => '',
256 'new_user[plainPassword][first]' => '',
257 'new_user[plainPassword][second]' => '',
258 'new_user[email]' => '',
260 'Please enter a username',
264 'new_user[username]' => 'a',
265 'new_user[plainPassword][first]' => 'mypassword',
266 'new_user[plainPassword][second]' => 'mypassword',
267 'new_user[email]' => '',
269 'The username is too short',
273 'new_user[username]' => 'wallace',
274 'new_user[plainPassword][first]' => 'mypassword',
275 'new_user[plainPassword][second]' => 'mypassword',
276 'new_user[email]' => 'test',
278 'The email is not valid',
282 'new_user[username]' => 'admin',
283 'new_user[plainPassword][first]' => 'wallacewallace',
284 'new_user[plainPassword][second]' => 'wallacewallace',
285 'new_user[email]' => 'wallace@wallace.me',
287 'The username is already used',
291 'new_user[username]' => 'wallace',
292 'new_user[plainPassword][first]' => 'mypassword1',
293 'new_user[plainPassword][second]' => 'mypassword2',
294 'new_user[email]' => 'wallace@wallace.me',
296 'This value is not valid',
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[plainPassword][first]' => 'wallace1',
337 'new_user[plainPassword][second]' => 'wallace1',
338 'new_user[email]' => 'wallace@wallace.me',
341 $client->submit($form, $data);
343 $this->assertEquals(302, $client->getResponse()->getStatusCode());
345 $crawler = $client->followRedirect();
347 $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(array('_text')));
348 $this->assertContains('User "wallace" added', $alert[0]);
350 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
352 ->getRepository('WallabagUserBundle:User')
353 ->findOneByUsername('wallace');
355 $this->assertTrue(false !== $user);
356 $this->assertTrue($user->isEnabled());
359 public function testRssUpdateResetToken()
361 $this->logInAs('admin');
362 $client = $this->getClient();
365 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
367 ->getRepository('WallabagUserBundle:User')
368 ->findOneByUsername('admin');
371 $this->markTestSkipped('No user found in db.');
374 $config = $user->getConfig();
375 $config->setRssToken(null);
376 $em->persist($config);
379 $crawler = $client->request('GET', '/config');
381 $this->assertEquals(200, $client->getResponse()->getStatusCode());
383 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(array('_text')));
384 $this->assertContains('You need to generate a token first.', $body[0]);
386 $client->request('GET', '/generate-token');
387 $this->assertEquals(302, $client->getResponse()->getStatusCode());
389 $crawler = $client->followRedirect();
391 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(array('_text')));
392 $this->assertNotContains('You need to generate a token first.', $body[0]);
395 public function testGenerateTokenAjax()
397 $this->logInAs('admin');
398 $client = $this->getClient();
405 array('HTTP_X-Requested-With' => 'XMLHttpRequest')
408 $this->assertEquals(200, $client->getResponse()->getStatusCode());
409 $content = json_decode($client->getResponse()->getContent(), true);
410 $this->assertArrayHasKey('token', $content);
413 public function testRssUpdate()
415 $this->logInAs('admin');
416 $client = $this->getClient();
418 $crawler = $client->request('GET', '/config');
420 if (500 == $client->getResponse()->getStatusCode()) {
421 var_export($client->getResponse()->getContent());
425 $this->assertEquals(200, $client->getResponse()->getStatusCode());
427 $form = $crawler->filter('button[id=rss_config_save]')->form();
430 'rss_config[rss_limit]' => 12,
433 $client->submit($form, $data);
435 $this->assertEquals(302, $client->getResponse()->getStatusCode());
437 $crawler = $client->followRedirect();
439 $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(array('_text')));
440 $this->assertContains('RSS information updated', $alert[0]);
443 public function dataForRssFailed()
448 'rss_config[rss_limit]' => 0,
450 'This value should be 1 or more.',
454 'rss_config[rss_limit]' => 1000000000000,
456 'This will certainly kill the app',
462 * @dataProvider dataForRssFailed
464 public function testRssFailed($data, $expectedMessage)
466 $this->logInAs('admin');
467 $client = $this->getClient();
469 $crawler = $client->request('GET', '/config');
471 $this->assertEquals(200, $client->getResponse()->getStatusCode());
473 $form = $crawler->filter('button[id=rss_config_save]')->form();
475 $crawler = $client->submit($form, $data);
477 $this->assertEquals(200, $client->getResponse()->getStatusCode());
479 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(array('_text')));
480 $this->assertContains($expectedMessage, $alert[0]);
483 public function testTaggingRuleCreation()
485 $this->logInAs('admin');
486 $client = $this->getClient();
488 $crawler = $client->request('GET', '/config');
490 $this->assertTrue($client->getResponse()->isSuccessful());
492 $form = $crawler->filter('button[id=tagging_rule_save]')->form();
495 'tagging_rule[rule]' => 'readingTime <= 3',
496 'tagging_rule[tags]' => 'short reading',
499 $client->submit($form, $data);
501 $this->assertEquals(302, $client->getResponse()->getStatusCode());
503 $crawler = $client->followRedirect();
505 $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(array('_text')));
506 $this->assertContains('Tagging rules updated', $alert[0]);
508 $deleteLink = $crawler->filter('.delete')->last()->link();
510 $crawler = $client->click($deleteLink);
511 $this->assertEquals(302, $client->getResponse()->getStatusCode());
513 $crawler = $client->followRedirect();
514 $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(array('_text')));
515 $this->assertContains('Tagging rule deleted', $alert[0]);
518 public function dataForTaggingRuleFailed()
523 'rss_config[rule]' => 'unknownVar <= 3',
524 'rss_config[tags]' => 'cool tag',
526 'The variable « unknownVar » does not exist.',
530 'rss_config[rule]' => 'length(domainName) <= 42',
531 'rss_config[tags]' => 'cool tag',
533 'The operator « length » does not exist.',