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();
47 'config[theme]' => 'baggy',
48 'config[items_per_page]' => '30',
49 'config[reading_speed]' => '0.5',
50 'config[language]' => 'en',
53 $client->submit($form, $data);
55 $this->assertEquals(302, $client->getResponse()->getStatusCode());
57 $crawler = $client->followRedirect();
59 $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(['_text']));
60 $this->assertContains('flashes.config.notice.config_saved', $alert[0]);
63 public function dataForUpdateFailed()
67 'config[theme]' => 'baggy',
68 'config[items_per_page]' => '',
69 'config[language]' => 'en',
75 * @dataProvider dataForUpdateFailed
77 public function testUpdateFailed($data)
79 $this->logInAs('admin');
80 $client = $this->getClient();
82 $crawler = $client->request('GET', '/config');
84 $this->assertEquals(200, $client->getResponse()->getStatusCode());
86 $form = $crawler->filter('button[id=config_save]')->form();
88 $crawler = $client->submit($form, $data);
90 $this->assertEquals(200, $client->getResponse()->getStatusCode());
92 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
93 $this->assertContains('This value should not be blank', $alert[0]);
96 public function dataForChangePasswordFailed()
101 'change_passwd[old_password]' => 'material',
102 'change_passwd[new_password][first]' => '',
103 'change_passwd[new_password][second]' => '',
105 'validator.password_wrong_value',
109 'change_passwd[old_password]' => 'mypassword',
110 'change_passwd[new_password][first]' => '',
111 'change_passwd[new_password][second]' => '',
113 'This value should not be blank',
117 'change_passwd[old_password]' => 'mypassword',
118 'change_passwd[new_password][first]' => 'hop',
119 'change_passwd[new_password][second]' => '',
121 'validator.password_must_match',
125 'change_passwd[old_password]' => 'mypassword',
126 'change_passwd[new_password][first]' => 'hop',
127 'change_passwd[new_password][second]' => 'hop',
129 'validator.password_too_short',
135 * @dataProvider dataForChangePasswordFailed
137 public function testChangePasswordFailed($data, $expectedMessage)
139 $this->logInAs('admin');
140 $client = $this->getClient();
142 $crawler = $client->request('GET', '/config');
144 $this->assertEquals(200, $client->getResponse()->getStatusCode());
146 $form = $crawler->filter('button[id=change_passwd_save]')->form();
148 $crawler = $client->submit($form, $data);
150 $this->assertEquals(200, $client->getResponse()->getStatusCode());
152 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
153 $this->assertContains($expectedMessage, $alert[0]);
156 public function testChangePassword()
158 $this->logInAs('admin');
159 $client = $this->getClient();
161 $crawler = $client->request('GET', '/config');
163 $this->assertEquals(200, $client->getResponse()->getStatusCode());
165 $form = $crawler->filter('button[id=change_passwd_save]')->form();
168 'change_passwd[old_password]' => 'mypassword',
169 'change_passwd[new_password][first]' => 'mypassword',
170 'change_passwd[new_password][second]' => 'mypassword',
173 $client->submit($form, $data);
175 $this->assertEquals(302, $client->getResponse()->getStatusCode());
177 $crawler = $client->followRedirect();
179 $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(['_text']));
180 $this->assertContains('flashes.config.notice.password_updated', $alert[0]);
183 public function dataForUserFailed()
188 'update_user[name]' => '',
189 'update_user[email]' => '',
191 'fos_user.email.blank',
195 'update_user[name]' => '',
196 'update_user[email]' => 'test',
198 'fos_user.email.invalid',
204 * @dataProvider dataForUserFailed
206 public function testUserFailed($data, $expectedMessage)
208 $this->logInAs('admin');
209 $client = $this->getClient();
211 $crawler = $client->request('GET', '/config');
213 $this->assertEquals(200, $client->getResponse()->getStatusCode());
215 $form = $crawler->filter('button[id=update_user_save]')->form();
217 $crawler = $client->submit($form, $data);
219 $this->assertEquals(200, $client->getResponse()->getStatusCode());
221 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
222 $this->assertContains($expectedMessage, $alert[0]);
225 public function testUserUpdate()
227 $this->logInAs('admin');
228 $client = $this->getClient();
230 $crawler = $client->request('GET', '/config');
232 $this->assertEquals(200, $client->getResponse()->getStatusCode());
234 $form = $crawler->filter('button[id=update_user_save]')->form();
237 'update_user[name]' => 'new name',
238 'update_user[email]' => 'admin@wallabag.io',
241 $client->submit($form, $data);
243 $this->assertEquals(302, $client->getResponse()->getStatusCode());
245 $crawler = $client->followRedirect();
247 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
248 $this->assertContains('flashes.config.notice.user_updated', $alert[0]);
251 public function dataForNewUserFailed()
256 'new_user[username]' => '',
257 'new_user[plainPassword][first]' => '',
258 'new_user[plainPassword][second]' => '',
259 'new_user[email]' => '',
261 'fos_user.username.blank',
265 'new_user[username]' => 'a',
266 'new_user[plainPassword][first]' => 'mypassword',
267 'new_user[plainPassword][second]' => 'mypassword',
268 'new_user[email]' => '',
270 'fos_user.username.short',
274 'new_user[username]' => 'wallace',
275 'new_user[plainPassword][first]' => 'mypassword',
276 'new_user[plainPassword][second]' => 'mypassword',
277 'new_user[email]' => 'test',
279 'fos_user.email.invalid',
283 'new_user[username]' => 'admin',
284 'new_user[plainPassword][first]' => 'wallacewallace',
285 'new_user[plainPassword][second]' => 'wallacewallace',
286 'new_user[email]' => 'wallace@wallace.me',
288 'fos_user.username.already_used',
292 'new_user[username]' => 'wallace',
293 'new_user[plainPassword][first]' => 'mypassword1',
294 'new_user[plainPassword][second]' => 'mypassword2',
295 'new_user[email]' => 'wallace@wallace.me',
297 'validator.password_must_match',
303 * @dataProvider dataForNewUserFailed
305 public function testNewUserFailed($data, $expectedMessage)
307 $this->logInAs('admin');
308 $client = $this->getClient();
310 $crawler = $client->request('GET', '/config');
312 $this->assertEquals(200, $client->getResponse()->getStatusCode());
314 $form = $crawler->filter('button[id=new_user_save]')->form();
316 $crawler = $client->submit($form, $data);
318 $this->assertEquals(200, $client->getResponse()->getStatusCode());
320 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
321 $this->assertContains($expectedMessage, $alert[0]);
324 public function testNewUserCreated()
326 $this->logInAs('admin');
327 $client = $this->getClient();
329 $crawler = $client->request('GET', '/config');
331 $this->assertEquals(200, $client->getResponse()->getStatusCode());
333 $form = $crawler->filter('button[id=new_user_save]')->form();
336 'new_user[username]' => 'wallace',
337 'new_user[plainPassword][first]' => 'wallace1',
338 'new_user[plainPassword][second]' => 'wallace1',
339 'new_user[email]' => 'wallace@wallace.me',
342 $client->submit($form, $data);
344 $this->assertEquals(302, $client->getResponse()->getStatusCode());
346 $crawler = $client->followRedirect();
348 $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(['_text']));
349 $this->assertContains('flashes.config.notice.user_added', $alert[0]);
351 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
353 ->getRepository('WallabagUserBundle:User')
354 ->findOneByUsername('wallace');
356 $this->assertTrue(false !== $user);
357 $this->assertTrue($user->isEnabled());
358 $this->assertEquals('material', $user->getConfig()->getTheme());
359 $this->assertEquals(12, $user->getConfig()->getItemsPerPage());
360 $this->assertEquals(50, $user->getConfig()->getRssLimit());
361 $this->assertEquals('en', $user->getConfig()->getLanguage());
362 $this->assertEquals(1, $user->getConfig()->getReadingSpeed());
365 public function testRssUpdateResetToken()
367 $this->logInAs('admin');
368 $client = $this->getClient();
371 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
373 ->getRepository('WallabagUserBundle:User')
374 ->findOneByUsername('admin');
377 $this->markTestSkipped('No user found in db.');
380 $config = $user->getConfig();
381 $config->setRssToken(null);
382 $em->persist($config);
385 $crawler = $client->request('GET', '/config');
387 $this->assertEquals(200, $client->getResponse()->getStatusCode());
389 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
390 $this->assertContains('config.form_rss.no_token', $body[0]);
392 $client->request('GET', '/generate-token');
393 $this->assertEquals(302, $client->getResponse()->getStatusCode());
395 $crawler = $client->followRedirect();
397 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
398 $this->assertNotContains('config.form_rss.no_token', $body[0]);
401 public function testGenerateTokenAjax()
403 $this->logInAs('admin');
404 $client = $this->getClient();
411 ['HTTP_X-Requested-With' => 'XMLHttpRequest']
414 $this->assertEquals(200, $client->getResponse()->getStatusCode());
415 $content = json_decode($client->getResponse()->getContent(), true);
416 $this->assertArrayHasKey('token', $content);
419 public function testRssUpdate()
421 $this->logInAs('admin');
422 $client = $this->getClient();
424 $crawler = $client->request('GET', '/config');
426 $this->assertEquals(200, $client->getResponse()->getStatusCode());
428 $form = $crawler->filter('button[id=rss_config_save]')->form();
431 'rss_config[rss_limit]' => 12,
434 $client->submit($form, $data);
436 $this->assertEquals(302, $client->getResponse()->getStatusCode());
438 $crawler = $client->followRedirect();
440 $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(['_text']));
441 $this->assertContains('flashes.config.notice.rss_updated', $alert[0]);
444 public function dataForRssFailed()
449 'rss_config[rss_limit]' => 0,
451 'This value should be 1 or more.',
455 'rss_config[rss_limit]' => 1000000000000,
457 'validator.rss_limit_too_hight',
463 * @dataProvider dataForRssFailed
465 public function testRssFailed($data, $expectedMessage)
467 $this->logInAs('admin');
468 $client = $this->getClient();
470 $crawler = $client->request('GET', '/config');
472 $this->assertEquals(200, $client->getResponse()->getStatusCode());
474 $form = $crawler->filter('button[id=rss_config_save]')->form();
476 $crawler = $client->submit($form, $data);
478 $this->assertEquals(200, $client->getResponse()->getStatusCode());
480 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
481 $this->assertContains($expectedMessage, $alert[0]);
484 public function testTaggingRuleCreation()
486 $this->logInAs('admin');
487 $client = $this->getClient();
489 $crawler = $client->request('GET', '/config');
491 $this->assertTrue($client->getResponse()->isSuccessful());
493 $form = $crawler->filter('button[id=tagging_rule_save]')->form();
496 'tagging_rule[rule]' => 'readingTime <= 3',
497 'tagging_rule[tags]' => 'short reading',
500 $client->submit($form, $data);
502 $this->assertEquals(302, $client->getResponse()->getStatusCode());
504 $crawler = $client->followRedirect();
506 $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(['_text']));
507 $this->assertContains('flashes.config.notice.tagging_rules_updated', $alert[0]);
509 $deleteLink = $crawler->filter('.delete')->last()->link();
511 $crawler = $client->click($deleteLink);
512 $this->assertEquals(302, $client->getResponse()->getStatusCode());
514 $crawler = $client->followRedirect();
515 $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(['_text']));
516 $this->assertContains('flashes.config.notice.tagging_rules_deleted', $alert[0]);
519 public function dataForTaggingRuleFailed()
524 'tagging_rule[rule]' => 'unknownVar <= 3',
525 'tagging_rule[tags]' => 'cool tag',
534 'tagging_rule[rule]' => 'length(domainName) <= 42',
535 'tagging_rule[tags]' => 'cool tag',
546 * @dataProvider dataForTaggingRuleFailed
548 public function testTaggingRuleCreationFail($data, $messages)
550 $this->logInAs('admin');
551 $client = $this->getClient();
553 $crawler = $client->request('GET', '/config');
555 $this->assertTrue($client->getResponse()->isSuccessful());
557 $form = $crawler->filter('button[id=tagging_rule_save]')->form();
559 $crawler = $client->submit($form, $data);
561 $this->assertEquals(200, $client->getResponse()->getStatusCode());
563 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
565 foreach ($messages as $message) {
566 $this->assertContains($message, $body[0]);
570 public function testDeletingTaggingRuleFromAnOtherUser()
572 $this->logInAs('bob');
573 $client = $this->getClient();
575 $rule = $client->getContainer()->get('doctrine.orm.entity_manager')
576 ->getRepository('WallabagCoreBundle:TaggingRule')
579 $crawler = $client->request('GET', '/tagging-rule/delete/'.$rule->getId());
581 $this->assertEquals(403, $client->getResponse()->getStatusCode());
582 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
583 $this->assertContains('You can not access this tagging rule', $body[0]);
586 public function testDemoMode()
588 $this->logInAs('admin');
589 $client = $this->getClient();
591 $config = $client->getContainer()->get('craue_config');
592 $config->set('demo_mode_enabled', 1);
593 $config->set('demo_mode_username', 'admin');
595 $crawler = $client->request('GET', '/config');
597 $this->assertEquals(200, $client->getResponse()->getStatusCode());
599 $form = $crawler->filter('button[id=change_passwd_save]')->form();
602 'change_passwd[old_password]' => 'mypassword',
603 'change_passwd[new_password][first]' => 'mypassword',
604 'change_passwd[new_password][second]' => 'mypassword',
607 $client->submit($form, $data);
609 $this->assertEquals(302, $client->getResponse()->getStatusCode());
610 $this->assertContains('flashes.config.notice.password_not_updated_demo', $client->getContainer()->get('session')->getFlashBag()->get('notice')[0]);
612 $config->set('demo_mode_enabled', 0);
613 $config->set('demo_mode_username', 'wallabag');