3 namespace Tests\Wallabag\CoreBundle\Controller
;
5 use Tests\Wallabag\CoreBundle\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=rss_config_save]'));
34 public function testUpdate()
36 $this->logInAs('admin');
37 $client = $this->getClient();
39 $crawler = $client->request('GET', '/config');
41 $this->assertEquals(200, $client->getResponse()->getStatusCode());
43 $form = $crawler->filter('button[id=config_save]')->form();
46 'config[theme]' => 'baggy',
47 'config[items_per_page]' => '30',
48 'config[reading_speed]' => '0.5',
49 'config[language]' => 'en',
52 $client->submit($form, $data);
54 $this->assertEquals(302, $client->getResponse()->getStatusCode());
56 $crawler = $client->followRedirect();
58 $this->assertContains('flashes.config.notice.config_saved', $crawler->filter('body')->extract(['_text'])[0]);
61 public function testChangeReadingSpeed()
63 $this->logInAs('admin');
64 $client = $this->getClient();
66 $crawler = $client->request('GET', '/unread/list');
67 $form = $crawler->filter('button[id=submit-filter]')->form();
69 'entry_filter[readingTime][right_number]' => 22,
70 'entry_filter[readingTime][left_number]' => 22,
72 $crawler = $client->submit($form, $dataFilters);
73 $this->assertCount(1, $crawler->filter('div[class=entry]'));
75 // Change reading speed
76 $crawler = $client->request('GET', '/config');
77 $form = $crawler->filter('button[id=config_save]')->form();
79 'config[reading_speed]' => '2',
81 $client->submit($form, $data);
83 // Is the entry still available via filters?
84 $crawler = $client->request('GET', '/unread/list');
85 $form = $crawler->filter('button[id=submit-filter]')->form();
86 $crawler = $client->submit($form, $dataFilters);
87 $this->assertCount(0, $crawler->filter('div[class=entry]'));
89 // Restore old configuration
90 $crawler = $client->request('GET', '/config');
91 $form = $crawler->filter('button[id=config_save]')->form();
93 'config[reading_speed]' => '0.5',
95 $client->submit($form, $data);
98 public function dataForUpdateFailed()
102 'config[theme]' => 'baggy',
103 'config[items_per_page]' => '',
104 'config[language]' => 'en',
110 * @dataProvider dataForUpdateFailed
112 public function testUpdateFailed($data)
114 $this->logInAs('admin');
115 $client = $this->getClient();
117 $crawler = $client->request('GET', '/config');
119 $this->assertEquals(200, $client->getResponse()->getStatusCode());
121 $form = $crawler->filter('button[id=config_save]')->form();
123 $crawler = $client->submit($form, $data);
125 $this->assertEquals(200, $client->getResponse()->getStatusCode());
127 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
128 $this->assertContains('This value should not be blank', $alert[0]);
131 public function dataForChangePasswordFailed()
136 'change_passwd[old_password]' => 'material',
137 'change_passwd[new_password][first]' => '',
138 'change_passwd[new_password][second]' => '',
140 'validator.password_wrong_value',
144 'change_passwd[old_password]' => 'mypassword',
145 'change_passwd[new_password][first]' => '',
146 'change_passwd[new_password][second]' => '',
148 'This value should not be blank',
152 'change_passwd[old_password]' => 'mypassword',
153 'change_passwd[new_password][first]' => 'hop',
154 'change_passwd[new_password][second]' => '',
156 'validator.password_must_match',
160 'change_passwd[old_password]' => 'mypassword',
161 'change_passwd[new_password][first]' => 'hop',
162 'change_passwd[new_password][second]' => 'hop',
164 'validator.password_too_short',
170 * @dataProvider dataForChangePasswordFailed
172 public function testChangePasswordFailed($data, $expectedMessage)
174 $this->logInAs('admin');
175 $client = $this->getClient();
177 $crawler = $client->request('GET', '/config');
179 $this->assertEquals(200, $client->getResponse()->getStatusCode());
181 $form = $crawler->filter('button[id=change_passwd_save]')->form();
183 $crawler = $client->submit($form, $data);
185 $this->assertEquals(200, $client->getResponse()->getStatusCode());
187 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
188 $this->assertContains($expectedMessage, $alert[0]);
191 public function testChangePassword()
193 $this->logInAs('admin');
194 $client = $this->getClient();
196 $crawler = $client->request('GET', '/config');
198 $this->assertEquals(200, $client->getResponse()->getStatusCode());
200 $form = $crawler->filter('button[id=change_passwd_save]')->form();
203 'change_passwd[old_password]' => 'mypassword',
204 'change_passwd[new_password][first]' => 'mypassword',
205 'change_passwd[new_password][second]' => 'mypassword',
208 $client->submit($form, $data);
210 $this->assertEquals(302, $client->getResponse()->getStatusCode());
212 $crawler = $client->followRedirect();
214 $this->assertContains('flashes.config.notice.password_updated', $crawler->filter('body')->extract(['_text'])[0]);
217 public function dataForUserFailed()
222 'update_user[name]' => '',
223 'update_user[email]' => '',
225 'fos_user.email.blank',
229 'update_user[name]' => '',
230 'update_user[email]' => 'test',
232 'fos_user.email.invalid',
238 * @dataProvider dataForUserFailed
240 public function testUserFailed($data, $expectedMessage)
242 $this->logInAs('admin');
243 $client = $this->getClient();
245 $crawler = $client->request('GET', '/config');
247 $this->assertEquals(200, $client->getResponse()->getStatusCode());
249 $form = $crawler->filter('button[id=update_user_save]')->form();
251 $crawler = $client->submit($form, $data);
253 $this->assertEquals(200, $client->getResponse()->getStatusCode());
255 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
256 $this->assertContains($expectedMessage, $alert[0]);
259 public function testUserUpdate()
261 $this->logInAs('admin');
262 $client = $this->getClient();
264 $crawler = $client->request('GET', '/config');
266 $this->assertEquals(200, $client->getResponse()->getStatusCode());
268 $form = $crawler->filter('button[id=update_user_save]')->form();
271 'update_user[name]' => 'new name',
272 'update_user[email]' => 'admin@wallabag.io',
275 $client->submit($form, $data);
277 $this->assertEquals(302, $client->getResponse()->getStatusCode());
279 $crawler = $client->followRedirect();
281 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
282 $this->assertContains('flashes.config.notice.user_updated', $alert[0]);
285 public function testRssUpdateResetToken()
287 $this->logInAs('admin');
288 $client = $this->getClient();
291 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
293 ->getRepository('WallabagUserBundle:User')
294 ->findOneByUsername('admin');
297 $this->markTestSkipped('No user found in db.');
300 $config = $user->getConfig();
301 $config->setRssToken(null);
302 $em->persist($config);
305 $crawler = $client->request('GET', '/config');
307 $this->assertEquals(200, $client->getResponse()->getStatusCode());
309 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
310 $this->assertContains('config.form_rss.no_token', $body[0]);
312 $client->request('GET', '/generate-token');
313 $this->assertEquals(302, $client->getResponse()->getStatusCode());
315 $crawler = $client->followRedirect();
317 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
318 $this->assertNotContains('config.form_rss.no_token', $body[0]);
321 public function testGenerateTokenAjax()
323 $this->logInAs('admin');
324 $client = $this->getClient();
331 ['HTTP_X-Requested-With' => 'XMLHttpRequest']
334 $this->assertEquals(200, $client->getResponse()->getStatusCode());
335 $content = json_decode($client->getResponse()->getContent(), true);
336 $this->assertArrayHasKey('token', $content);
339 public function testRssUpdate()
341 $this->logInAs('admin');
342 $client = $this->getClient();
344 $crawler = $client->request('GET', '/config');
346 $this->assertEquals(200, $client->getResponse()->getStatusCode());
348 $form = $crawler->filter('button[id=rss_config_save]')->form();
351 'rss_config[rss_limit]' => 12,
354 $client->submit($form, $data);
356 $this->assertEquals(302, $client->getResponse()->getStatusCode());
358 $crawler = $client->followRedirect();
360 $this->assertContains('flashes.config.notice.rss_updated', $crawler->filter('body')->extract(['_text'])[0]);
363 public function dataForRssFailed()
368 'rss_config[rss_limit]' => 0,
370 'This value should be 1 or more.',
374 'rss_config[rss_limit]' => 1000000000000,
376 'validator.rss_limit_too_high',
382 * @dataProvider dataForRssFailed
384 public function testRssFailed($data, $expectedMessage)
386 $this->logInAs('admin');
387 $client = $this->getClient();
389 $crawler = $client->request('GET', '/config');
391 $this->assertEquals(200, $client->getResponse()->getStatusCode());
393 $form = $crawler->filter('button[id=rss_config_save]')->form();
395 $crawler = $client->submit($form, $data);
397 $this->assertEquals(200, $client->getResponse()->getStatusCode());
399 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
400 $this->assertContains($expectedMessage, $alert[0]);
403 public function testTaggingRuleCreation()
405 $this->logInAs('admin');
406 $client = $this->getClient();
408 $crawler = $client->request('GET', '/config');
410 $this->assertTrue($client->getResponse()->isSuccessful());
412 $form = $crawler->filter('button[id=tagging_rule_save]')->form();
415 'tagging_rule[rule]' => 'readingTime <= 3',
416 'tagging_rule[tags]' => 'short reading',
419 $client->submit($form, $data);
421 $this->assertEquals(302, $client->getResponse()->getStatusCode());
423 $crawler = $client->followRedirect();
425 $this->assertContains('flashes.config.notice.tagging_rules_updated', $crawler->filter('body')->extract(['_text'])[0]);
427 $editLink = $crawler->filter('.mode_edit')->last()->link();
429 $crawler = $client->click($editLink);
430 $this->assertEquals(302, $client->getResponse()->getStatusCode());
431 $this->assertContains('?tagging-rule=', $client->getResponse()->headers
->get('location'));
433 $crawler = $client->followRedirect();
435 $form = $crawler->filter('button[id=tagging_rule_save]')->form();
438 'tagging_rule[rule]' => 'readingTime <= 30',
439 'tagging_rule[tags]' => 'short reading',
442 $client->submit($form, $data);
444 $this->assertEquals(302, $client->getResponse()->getStatusCode());
446 $crawler = $client->followRedirect();
448 $this->assertContains('flashes.config.notice.tagging_rules_updated', $crawler->filter('body')->extract(['_text'])[0]);
450 $this->assertContains('readingTime <= 30', $crawler->filter('body')->extract(['_text'])[0]);
452 $deleteLink = $crawler->filter('.delete')->last()->link();
454 $crawler = $client->click($deleteLink);
455 $this->assertEquals(302, $client->getResponse()->getStatusCode());
457 $crawler = $client->followRedirect();
458 $this->assertContains('flashes.config.notice.tagging_rules_deleted', $crawler->filter('body')->extract(['_text'])[0]);
461 public function dataForTaggingRuleFailed()
466 'tagging_rule[rule]' => 'unknownVar <= 3',
467 'tagging_rule[tags]' => 'cool tag',
476 'tagging_rule[rule]' => 'length(domainName) <= 42',
477 'tagging_rule[tags]' => 'cool tag',
488 * @dataProvider dataForTaggingRuleFailed
490 public function testTaggingRuleCreationFail($data, $messages)
492 $this->logInAs('admin');
493 $client = $this->getClient();
495 $crawler = $client->request('GET', '/config');
497 $this->assertTrue($client->getResponse()->isSuccessful());
499 $form = $crawler->filter('button[id=tagging_rule_save]')->form();
501 $crawler = $client->submit($form, $data);
503 $this->assertEquals(200, $client->getResponse()->getStatusCode());
505 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
507 foreach ($messages as $message) {
508 $this->assertContains($message, $body[0]);
512 public function testDeletingTaggingRuleFromAnOtherUser()
514 $this->logInAs('bob');
515 $client = $this->getClient();
517 $rule = $client->getContainer()->get('doctrine.orm.entity_manager')
518 ->getRepository('WallabagCoreBundle:TaggingRule')
521 $crawler = $client->request('GET', '/tagging-rule/edit/'.$rule->getId());
523 $this->assertEquals(403, $client->getResponse()->getStatusCode());
524 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
525 $this->assertContains('You can not access this tagging rule', $body[0]);
528 public function testEditingTaggingRuleFromAnOtherUser()
530 $this->logInAs('bob');
531 $client = $this->getClient();
533 $rule = $client->getContainer()->get('doctrine.orm.entity_manager')
534 ->getRepository('WallabagCoreBundle:TaggingRule')
537 $crawler = $client->request('GET', '/tagging-rule/edit/'.$rule->getId());
539 $this->assertEquals(403, $client->getResponse()->getStatusCode());
540 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
541 $this->assertContains('You can not access this tagging rule', $body[0]);
544 public function testDemoMode()
546 $this->logInAs('admin');
547 $client = $this->getClient();
549 $config = $client->getContainer()->get('craue_config');
550 $config->set('demo_mode_enabled', 1);
551 $config->set('demo_mode_username', 'admin');
553 $crawler = $client->request('GET', '/config');
555 $this->assertEquals(200, $client->getResponse()->getStatusCode());
557 $form = $crawler->filter('button[id=change_passwd_save]')->form();
560 'change_passwd[old_password]' => 'mypassword',
561 'change_passwd[new_password][first]' => 'mypassword',
562 'change_passwd[new_password][second]' => 'mypassword',
565 $client->submit($form, $data);
567 $this->assertEquals(302, $client->getResponse()->getStatusCode());
568 $this->assertContains('flashes.config.notice.password_not_updated_demo', $client->getContainer()->get('session')->getFlashBag()->get('notice')[0]);
570 $config->set('demo_mode_enabled', 0);
571 $config->set('demo_mode_username', 'wallabag');