3 namespace Tests\Wallabag\CoreBundle\Controller
;
5 use Symfony\Component\HttpFoundation\File\UploadedFile
;
6 use Tests\Wallabag\CoreBundle\WallabagCoreTestCase
;
7 use Wallabag\AnnotationBundle\Entity\Annotation
;
8 use Wallabag\CoreBundle\Entity\Config
;
9 use Wallabag\CoreBundle\Entity\Entry
;
10 use Wallabag\CoreBundle\Entity\Tag
;
11 use Wallabag\UserBundle\Entity\User
;
13 class ConfigControllerTest
extends WallabagCoreTestCase
15 public function testLogin()
17 $client = $this->getClient();
19 $client->request('GET', '/new');
21 $this->assertSame(302, $client->getResponse()->getStatusCode());
22 $this->assertContains('login', $client->getResponse()->headers
->get('location'));
25 public function testIndex()
27 $this->logInAs('admin');
28 $client = $this->getClient();
30 $crawler = $client->request('GET', '/config');
32 $this->assertSame(200, $client->getResponse()->getStatusCode());
34 $this->assertCount(1, $crawler->filter('button[id=config_save]'));
35 $this->assertCount(1, $crawler->filter('button[id=change_passwd_save]'));
36 $this->assertCount(1, $crawler->filter('button[id=update_user_save]'));
37 $this->assertCount(1, $crawler->filter('button[id=feed_config_save]'));
40 public function testUpdate()
42 $this->logInAs('admin');
43 $client = $this->getClient();
45 $crawler = $client->request('GET', '/config');
47 $this->assertSame(200, $client->getResponse()->getStatusCode());
49 $form = $crawler->filter('button[id=config_save]')->form();
52 'config[theme]' => 'material',
53 'config[items_per_page]' => '30',
54 'config[reading_speed]' => '100',
55 'config[action_mark_as_read]' => '0',
56 'config[language]' => 'en',
59 $client->submit($form, $data);
61 $this->assertSame(302, $client->getResponse()->getStatusCode());
63 $crawler = $client->followRedirect();
65 $this->assertContains('flashes.config.notice.config_saved', $crawler->filter('body')->extract(['_text'])[0]);
68 public function testChangeReadingSpeed()
70 $this->logInAs('admin');
71 $client = $this->getClient();
73 $entry = new Entry($this->getLoggedInUser());
74 $entry->setUrl('http://0.0.0.0/test-entry1')
76 $this->getEntityManager()->persist($entry);
78 $this->getEntityManager()->flush();
79 $this->getEntityManager()->clear();
81 $crawler = $client->request('GET', '/unread/list');
82 $form = $crawler->filter('button[id=submit-filter]')->form();
84 'entry_filter[readingTime][right_number]' => 22,
85 'entry_filter[readingTime][left_number]' => 22,
87 $crawler = $client->submit($form, $dataFilters);
88 $this->assertCount(1, $crawler->filter('div[class=entry]'));
90 // Change reading speed
91 $crawler = $client->request('GET', '/config');
92 $form = $crawler->filter('button[id=config_save]')->form();
94 'config[reading_speed]' => '400',
96 $client->submit($form, $data);
98 // Is the entry still available via filters?
99 $crawler = $client->request('GET', '/unread/list');
100 $form = $crawler->filter('button[id=submit-filter]')->form();
101 $crawler = $client->submit($form, $dataFilters);
102 $this->assertCount(0, $crawler->filter('div[class=entry]'));
104 // Restore old configuration
105 $crawler = $client->request('GET', '/config');
106 $form = $crawler->filter('button[id=config_save]')->form();
108 'config[reading_speed]' => '100',
110 $client->submit($form, $data);
113 public function dataForUpdateFailed()
117 'config[theme]' => 'material',
118 'config[items_per_page]' => '',
119 'config[language]' => 'en',
125 * @dataProvider dataForUpdateFailed
127 public function testUpdateFailed($data)
129 $this->logInAs('admin');
130 $client = $this->getClient();
132 $crawler = $client->request('GET', '/config');
134 $this->assertSame(200, $client->getResponse()->getStatusCode());
136 $form = $crawler->filter('button[id=config_save]')->form();
138 $crawler = $client->submit($form, $data);
140 $this->assertSame(200, $client->getResponse()->getStatusCode());
142 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
143 $this->assertContains('This value should not be blank', $alert[0]);
146 public function dataForChangePasswordFailed()
151 'change_passwd[old_password]' => 'material',
152 'change_passwd[new_password][first]' => '',
153 'change_passwd[new_password][second]' => '',
155 'validator.password_wrong_value',
159 'change_passwd[old_password]' => 'mypassword',
160 'change_passwd[new_password][first]' => '',
161 'change_passwd[new_password][second]' => '',
163 'This value should not be blank',
167 'change_passwd[old_password]' => 'mypassword',
168 'change_passwd[new_password][first]' => 'hop',
169 'change_passwd[new_password][second]' => '',
171 'validator.password_must_match',
175 'change_passwd[old_password]' => 'mypassword',
176 'change_passwd[new_password][first]' => 'hop',
177 'change_passwd[new_password][second]' => 'hop',
179 'validator.password_too_short',
185 * @dataProvider dataForChangePasswordFailed
187 public function testChangePasswordFailed($data, $expectedMessage)
189 $this->logInAs('admin');
190 $client = $this->getClient();
192 $crawler = $client->request('GET', '/config');
194 $this->assertSame(200, $client->getResponse()->getStatusCode());
196 $form = $crawler->filter('button[id=change_passwd_save]')->form();
198 $crawler = $client->submit($form, $data);
200 $this->assertSame(200, $client->getResponse()->getStatusCode());
202 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
203 $this->assertContains($expectedMessage, $alert[0]);
206 public function testChangePassword()
208 $this->logInAs('admin');
209 $client = $this->getClient();
211 $crawler = $client->request('GET', '/config');
213 $this->assertSame(200, $client->getResponse()->getStatusCode());
215 $form = $crawler->filter('button[id=change_passwd_save]')->form();
218 'change_passwd[old_password]' => 'mypassword',
219 'change_passwd[new_password][first]' => 'mypassword',
220 'change_passwd[new_password][second]' => 'mypassword',
223 $client->submit($form, $data);
225 $this->assertSame(302, $client->getResponse()->getStatusCode());
227 $crawler = $client->followRedirect();
229 $this->assertContains('flashes.config.notice.password_updated', $crawler->filter('body')->extract(['_text'])[0]);
232 public function dataForUserFailed()
237 'update_user[name]' => '',
238 'update_user[email]' => '',
240 'fos_user.email.blank',
244 'update_user[name]' => '',
245 'update_user[email]' => 'test',
247 'fos_user.email.invalid',
253 * @dataProvider dataForUserFailed
255 public function testUserFailed($data, $expectedMessage)
257 $this->logInAs('admin');
258 $client = $this->getClient();
260 $crawler = $client->request('GET', '/config');
262 $this->assertSame(200, $client->getResponse()->getStatusCode());
264 $form = $crawler->filter('button[id=update_user_save]')->form();
266 $crawler = $client->submit($form, $data);
268 $this->assertSame(200, $client->getResponse()->getStatusCode());
270 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
271 $this->assertContains($expectedMessage, $alert[0]);
274 public function testUserUpdate()
276 $this->logInAs('admin');
277 $client = $this->getClient();
279 $crawler = $client->request('GET', '/config');
281 $this->assertSame(200, $client->getResponse()->getStatusCode());
283 $form = $crawler->filter('button[id=update_user_save]')->form();
286 'update_user[name]' => 'new name',
287 'update_user[email]' => 'admin@wallabag.io',
290 $client->submit($form, $data);
292 $this->assertSame(302, $client->getResponse()->getStatusCode());
294 $crawler = $client->followRedirect();
296 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
297 $this->assertContains('flashes.config.notice.user_updated', $alert[0]);
300 public function testFeedUpdateResetToken()
302 $this->logInAs('admin');
303 $client = $this->getClient();
306 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
308 ->getRepository('WallabagUserBundle:User')
309 ->findOneByUsername('admin');
312 $this->markTestSkipped('No user found in db.');
315 $config = $user->getConfig();
316 $config->setFeedToken(null);
317 $em->persist($config);
320 $crawler = $client->request('GET', '/config');
322 $this->assertSame(200, $client->getResponse()->getStatusCode());
324 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
325 $this->assertContains('config.form_feed.no_token', $body[0]);
327 $client->request('GET', '/generate-token');
328 $this->assertSame(302, $client->getResponse()->getStatusCode());
330 $crawler = $client->followRedirect();
332 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
333 $this->assertContains('config.form_feed.token_reset', $body[0]);
336 public function testGenerateTokenAjax()
338 $this->logInAs('admin');
339 $client = $this->getClient();
346 ['HTTP_X-Requested-With' => 'XMLHttpRequest']
349 $this->assertSame(200, $client->getResponse()->getStatusCode());
350 $content = json_decode($client->getResponse()->getContent(), true);
351 $this->assertArrayHasKey('token', $content);
354 public function testRevokeTokenAjax()
356 $this->logInAs('admin');
357 $client = $this->getClient();
364 ['HTTP_X-Requested-With' => 'XMLHttpRequest']
367 $this->assertSame(200, $client->getResponse()->getStatusCode());
370 public function testFeedUpdate()
372 $this->logInAs('admin');
373 $client = $this->getClient();
375 $crawler = $client->request('GET', '/config');
377 $this->assertSame(200, $client->getResponse()->getStatusCode());
379 $form = $crawler->filter('button[id=feed_config_save]')->form();
382 'feed_config[feed_limit]' => 12,
385 $client->submit($form, $data);
387 $this->assertSame(302, $client->getResponse()->getStatusCode());
389 $crawler = $client->followRedirect();
391 $this->assertContains('flashes.config.notice.feed_updated', $crawler->filter('body')->extract(['_text'])[0]);
394 public function dataForFeedFailed()
399 'feed_config[feed_limit]' => 0,
401 'This value should be 1 or more.',
405 'feed_config[feed_limit]' => 1000000000000,
407 'validator.feed_limit_too_high',
413 * @dataProvider dataForFeedFailed
415 public function testFeedFailed($data, $expectedMessage)
417 $this->logInAs('admin');
418 $client = $this->getClient();
420 $crawler = $client->request('GET', '/config');
422 $this->assertSame(200, $client->getResponse()->getStatusCode());
424 $form = $crawler->filter('button[id=feed_config_save]')->form();
426 $crawler = $client->submit($form, $data);
428 $this->assertSame(200, $client->getResponse()->getStatusCode());
430 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
431 $this->assertContains($expectedMessage, $alert[0]);
434 public function testTaggingRuleCreation()
436 $this->logInAs('admin');
437 $client = $this->getClient();
439 $crawler = $client->request('GET', '/config');
441 $this->assertSame(200, $client->getResponse()->getStatusCode());
443 $form = $crawler->filter('button[id=tagging_rule_save]')->form();
446 'tagging_rule[rule]' => 'readingTime <= 3',
447 'tagging_rule[tags]' => 'short reading',
450 $client->submit($form, $data);
452 $this->assertSame(302, $client->getResponse()->getStatusCode());
454 $crawler = $client->followRedirect();
456 $this->assertContains('flashes.config.notice.tagging_rules_updated', $crawler->filter('body')->extract(['_text'])[0]);
458 $editLink = $crawler->filter('.edit-rule')->last()->link();
460 $crawler = $client->click($editLink);
461 $this->assertSame(302, $client->getResponse()->getStatusCode());
462 $this->assertContains('?tagging-rule=', $client->getResponse()->headers
->get('location'));
464 $crawler = $client->followRedirect();
466 $form = $crawler->filter('button[id=tagging_rule_save]')->form();
469 'tagging_rule[rule]' => 'readingTime <= 30',
470 'tagging_rule[tags]' => 'short reading',
473 $client->submit($form, $data);
475 $this->assertSame(302, $client->getResponse()->getStatusCode());
477 $crawler = $client->followRedirect();
479 $this->assertContains('flashes.config.notice.tagging_rules_updated', $crawler->filter('body')->extract(['_text'])[0]);
481 $this->assertContains('readingTime <= 30', $crawler->filter('body')->extract(['_text'])[0]);
483 $deleteLink = $crawler->filter('.delete-rule')->last()->link();
485 $crawler = $client->click($deleteLink);
486 $this->assertSame(302, $client->getResponse()->getStatusCode());
488 $crawler = $client->followRedirect();
489 $this->assertContains('flashes.config.notice.tagging_rules_deleted', $crawler->filter('body')->extract(['_text'])[0]);
492 public function dataForTaggingRuleFailed()
497 'tagging_rule[rule]' => 'unknownVar <= 3',
498 'tagging_rule[tags]' => 'cool tag',
507 'tagging_rule[rule]' => 'length(domainName) <= 42',
508 'tagging_rule[tags]' => 'cool tag',
519 * @dataProvider dataForTaggingRuleFailed
521 public function testTaggingRuleCreationFail($data, $messages)
523 $this->logInAs('admin');
524 $client = $this->getClient();
526 $crawler = $client->request('GET', '/config');
528 $this->assertSame(200, $client->getResponse()->getStatusCode());
530 $form = $crawler->filter('button[id=tagging_rule_save]')->form();
532 $crawler = $client->submit($form, $data);
534 $this->assertSame(200, $client->getResponse()->getStatusCode());
536 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
538 foreach ($messages as $message) {
539 $this->assertContains($message, $body[0]);
543 public function testTaggingRuleTooLong()
545 $this->logInAs('admin');
546 $client = $this->getClient();
548 $crawler = $client->request('GET', '/config');
550 $this->assertSame(200, $client->getResponse()->getStatusCode());
552 $form = $crawler->filter('button[id=tagging_rule_save]')->form();
554 $crawler = $client->submit($form, [
555 'tagging_rule[rule]' => str_repeat('title', 60),
556 'tagging_rule[tags]' => 'cool tag',
559 $this->assertSame(200, $client->getResponse()->getStatusCode());
561 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
563 $this->assertContains('255 characters', $body[0]);
566 public function testDeletingTaggingRuleFromAnOtherUser()
568 $this->logInAs('bob');
569 $client = $this->getClient();
571 $rule = $client->getContainer()->get('doctrine.orm.entity_manager')
572 ->getRepository('WallabagCoreBundle:TaggingRule')
575 $crawler = $client->request('GET', '/tagging-rule/edit/' . $rule->getId());
577 $this->assertSame(403, $client->getResponse()->getStatusCode());
578 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
579 $this->assertContains('You can not access this tagging rule', $body[0]);
582 public function testEditingTaggingRuleFromAnOtherUser()
584 $this->logInAs('bob');
585 $client = $this->getClient();
587 $rule = $client->getContainer()->get('doctrine.orm.entity_manager')
588 ->getRepository('WallabagCoreBundle:TaggingRule')
591 $crawler = $client->request('GET', '/tagging-rule/edit/' . $rule->getId());
593 $this->assertSame(403, $client->getResponse()->getStatusCode());
594 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
595 $this->assertContains('You can not access this tagging rule', $body[0]);
598 public function testDemoMode()
600 $this->logInAs('admin');
601 $client = $this->getClient();
603 $config = $client->getContainer()->get('craue_config');
604 $config->set('demo_mode_enabled', 1);
605 $config->set('demo_mode_username', 'admin');
607 $crawler = $client->request('GET', '/config');
609 $this->assertSame(200, $client->getResponse()->getStatusCode());
611 $form = $crawler->filter('button[id=change_passwd_save]')->form();
614 'change_passwd[old_password]' => 'mypassword',
615 'change_passwd[new_password][first]' => 'mypassword',
616 'change_passwd[new_password][second]' => 'mypassword',
619 $client->submit($form, $data);
621 $this->assertSame(302, $client->getResponse()->getStatusCode());
622 $this->assertContains('flashes.config.notice.password_not_updated_demo', $client->getContainer()->get('session')->getFlashBag()->get('notice')[0]);
624 $config->set('demo_mode_enabled', 0);
625 $config->set('demo_mode_username', 'wallabag');
628 public function testDeleteUserButtonVisibility()
630 $this->logInAs('admin');
631 $client = $this->getClient();
633 $crawler = $client->request('GET', '/config');
635 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
636 $this->assertContains('config.form_user.delete.button', $body[0]);
638 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
641 ->getRepository('WallabagUserBundle:User')
642 ->findOneByUsername('empty');
643 $user->setEnabled(false);
647 ->getRepository('WallabagUserBundle:User')
648 ->findOneByUsername('bob');
649 $user->setEnabled(false);
654 $crawler = $client->request('GET', '/config');
656 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
657 $this->assertNotContains('config.form_user.delete.button', $body[0]);
659 $client->request('GET', '/account/delete');
660 $this->assertSame(403, $client->getResponse()->getStatusCode());
663 ->getRepository('WallabagUserBundle:User')
664 ->findOneByUsername('empty');
665 $user->setEnabled(true);
669 ->getRepository('WallabagUserBundle:User')
670 ->findOneByUsername('bob');
671 $user->setEnabled(true);
677 public function testDeleteAccount()
679 $client = $this->getClient();
680 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
683 $user->setName('Wallace');
684 $user->setEmail('wallace@wallabag.org');
685 $user->setUsername('wallace');
686 $user->setPlainPassword('wallace');
687 $user->setEnabled(true);
688 $user->addRole('ROLE_SUPER_ADMIN');
692 $config = new Config($user);
694 $config->setTheme('material');
695 $config->setItemsPerPage(30);
696 $config->setReadingSpeed(200);
697 $config->setLanguage('en');
698 $config->setPocketConsumerKey('xxxxx');
700 $em->persist($config);
703 $this->logInAs('wallace');
704 $loggedInUserId = $this->getLoggedInUserId();
706 // create entry to check after user deletion
707 // that this entry is also deleted
708 $crawler = $client->request('GET', '/new');
710 $this->assertSame(200, $client->getResponse()->getStatusCode());
712 $form = $crawler->filter('form[name=entry]')->form();
714 'entry[url]' => $url = 'https://github.com/wallabag/wallabag',
717 $client->submit($form, $data);
718 $this->assertSame(302, $client->getResponse()->getStatusCode());
720 $crawler = $client->request('GET', '/config');
722 $deleteLink = $crawler->filter('.delete-account')->last()->link();
724 $client->click($deleteLink);
725 $this->assertSame(302, $client->getResponse()->getStatusCode());
727 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
729 ->getRepository('WallabagUserBundle:User')
730 ->createQueryBuilder('u')
731 ->where('u.username = :username')->setParameter('username', 'wallace')
733 ->getOneOrNullResult()
736 $this->assertNull($user);
738 $entries = $client->getContainer()
739 ->get('doctrine.orm.entity_manager')
740 ->getRepository('WallabagCoreBundle:Entry')
741 ->findByUser($loggedInUserId);
743 $this->assertEmpty($entries);
746 public function testReset()
748 $this->logInAs('empty');
749 $client = $this->getClient();
751 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
753 $user = static::$kernel->getContainer()->get('security.token_storage')->getToken()->getUser();
756 $tag->setLabel('super');
759 $entry = new Entry($user);
760 $entry->setUrl('https://www.lemonde.fr/europe/article/2016/10/01/pour-le-psoe-chaque-election-s-est-transformee-en-une-agonie_5006476_3214.html');
761 $entry->setContent('Youhou');
762 $entry->setTitle('Youhou');
763 $entry->addTag($tag);
764 $em->persist($entry);
766 $entry2 = new Entry($user);
767 $entry2->setUrl('http://www.lemonde.de/europe/article/2016/10/01/pour-le-psoe-chaque-election-s-est-transformee-en-une-agonie_5006476_3214.html');
768 $entry2->setContent('Youhou');
769 $entry2->setTitle('Youhou');
770 $entry2->addTag($tag);
771 $em->persist($entry2);
773 $annotation = new Annotation($user);
774 $annotation->setText('annotated');
775 $annotation->setQuote('annotated');
776 $annotation->setRanges([]);
777 $annotation->setEntry($entry);
778 $em->persist($annotation);
783 $crawler = $client->request('GET', '/config#set3');
785 $this->assertSame(200, $client->getResponse()->getStatusCode());
787 $crawler = $client->click($crawler->selectLink('config.reset.annotations')->link());
789 $this->assertSame(302, $client->getResponse()->getStatusCode());
790 $this->assertContains('flashes.config.notice.annotations_reset', $client->getContainer()->get('session')->getFlashBag()->get('notice')[0]);
792 $annotationsReset = $em
793 ->getRepository('WallabagAnnotationBundle:Annotation')
794 ->findAnnotationsByPageId($entry->getId(), $user->getId());
796 $this->assertEmpty($annotationsReset, 'Annotations were reset');
799 $crawler = $client->request('GET', '/config#set3');
801 $this->assertSame(200, $client->getResponse()->getStatusCode());
803 $crawler = $client->click($crawler->selectLink('config.reset.tags')->link());
805 $this->assertSame(302, $client->getResponse()->getStatusCode());
806 $this->assertContains('flashes.config.notice.tags_reset', $client->getContainer()->get('session')->getFlashBag()->get('notice')[0]);
809 ->getRepository('WallabagCoreBundle:Tag')
810 ->countAllTags($user->getId());
812 $this->assertSame(0, $tagReset, 'Tags were reset');
815 $crawler = $client->request('GET', '/config#set3');
817 $this->assertSame(200, $client->getResponse()->getStatusCode());
819 $crawler = $client->click($crawler->selectLink('config.reset.entries')->link());
821 $this->assertSame(302, $client->getResponse()->getStatusCode());
822 $this->assertContains('flashes.config.notice.entries_reset', $client->getContainer()->get('session')->getFlashBag()->get('notice')[0]);
825 ->getRepository('WallabagCoreBundle:Entry')
826 ->countAllEntriesByUser($user->getId());
828 $this->assertSame(0, $entryReset, 'Entries were reset');
831 public function testResetArchivedEntries()
833 $this->logInAs('empty');
834 $client = $this->getClient();
836 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
838 $user = static::$kernel->getContainer()->get('security.token_storage')->getToken()->getUser();
841 $tag->setLabel('super');
844 $entry = new Entry($user);
845 $entry->setUrl('https://www.lemonde.fr/europe/article/2016/10/01/pour-le-psoe-chaque-election-s-est-transformee-en-une-agonie_5006476_3214.html');
846 $entry->setContent('Youhou');
847 $entry->setTitle('Youhou');
848 $entry->addTag($tag);
849 $em->persist($entry);
851 $annotation = new Annotation($user);
852 $annotation->setText('annotated');
853 $annotation->setQuote('annotated');
854 $annotation->setRanges([]);
855 $annotation->setEntry($entry);
856 $em->persist($annotation);
858 $tagArchived = new Tag();
859 $tagArchived->setLabel('super');
860 $em->persist($tagArchived);
862 $entryArchived = new Entry($user);
863 $entryArchived->setUrl('https://www.lemonde.fr/europe/article/2016/10/01/pour-le-psoe-chaque-election-s-est-transformee-en-une-agonie_5006476_3214.html');
864 $entryArchived->setContent('Youhou');
865 $entryArchived->setTitle('Youhou');
866 $entryArchived->addTag($tagArchived);
867 $entryArchived->updateArchived(true);
868 $em->persist($entryArchived);
870 $annotationArchived = new Annotation($user);
871 $annotationArchived->setText('annotated');
872 $annotationArchived->setQuote('annotated');
873 $annotationArchived->setRanges([]);
874 $annotationArchived->setEntry($entryArchived);
875 $em->persist($annotationArchived);
879 $crawler = $client->request('GET', '/config#set3');
881 $this->assertSame(200, $client->getResponse()->getStatusCode());
883 $crawler = $client->click($crawler->selectLink('config.reset.archived')->link());
885 $this->assertSame(302, $client->getResponse()->getStatusCode());
886 $this->assertContains('flashes.config.notice.archived_reset', $client->getContainer()->get('session')->getFlashBag()->get('notice')[0]);
889 ->getRepository('WallabagCoreBundle:Entry')
890 ->countAllEntriesByUser($user->getId());
892 $this->assertSame(1, $entryReset, 'Entries were reset');
895 ->getRepository('WallabagCoreBundle:Tag')
896 ->countAllTags($user->getId());
898 $this->assertSame(1, $tagReset, 'Tags were reset');
900 $annotationsReset = $em
901 ->getRepository('WallabagAnnotationBundle:Annotation')
902 ->findAnnotationsByPageId($annotationArchived->getId(), $user->getId());
904 $this->assertEmpty($annotationsReset, 'Annotations were reset');
907 public function testResetEntriesCascade()
909 $this->logInAs('empty');
910 $client = $this->getClient();
912 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
914 $user = static::$kernel->getContainer()->get('security.token_storage')->getToken()->getUser();
917 $tag->setLabel('super');
920 $entry = new Entry($user);
921 $entry->setUrl('https://www.lemonde.fr/europe/article/2016/10/01/pour-le-psoe-chaque-election-s-est-transformee-en-une-agonie_5006476_3214.html');
922 $entry->setContent('Youhou');
923 $entry->setTitle('Youhou');
924 $entry->addTag($tag);
925 $em->persist($entry);
927 $annotation = new Annotation($user);
928 $annotation->setText('annotated');
929 $annotation->setQuote('annotated');
930 $annotation->setRanges([]);
931 $annotation->setEntry($entry);
932 $em->persist($annotation);
936 $crawler = $client->request('GET', '/config#set3');
938 $this->assertSame(200, $client->getResponse()->getStatusCode());
940 $crawler = $client->click($crawler->selectLink('config.reset.entries')->link());
942 $this->assertSame(302, $client->getResponse()->getStatusCode());
943 $this->assertContains('flashes.config.notice.entries_reset', $client->getContainer()->get('session')->getFlashBag()->get('notice')[0]);
946 ->getRepository('WallabagCoreBundle:Entry')
947 ->countAllEntriesByUser($user->getId());
949 $this->assertSame(0, $entryReset, 'Entries were reset');
952 ->getRepository('WallabagCoreBundle:Tag')
953 ->countAllTags($user->getId());
955 $this->assertSame(0, $tagReset, 'Tags were reset');
957 $annotationsReset = $em
958 ->getRepository('WallabagAnnotationBundle:Annotation')
959 ->findAnnotationsByPageId($entry->getId(), $user->getId());
961 $this->assertEmpty($annotationsReset, 'Annotations were reset');
964 public function testSwitchViewMode()
966 $this->logInAs('admin');
967 $client = $this->getClient();
969 $client->request('GET', '/unread/list');
971 $this->assertNotContains('collection', $client->getResponse()->getContent());
973 $client->request('GET', '/config/view-mode');
974 $crawler = $client->followRedirect();
976 $client->request('GET', '/unread/list');
978 $this->assertContains('collection', $client->getResponse()->getContent());
980 $client->request('GET', '/config/view-mode');
983 public function testChangeLocaleWithoutReferer()
985 $client = $this->getClient();
987 $client->request('GET', '/locale/de');
988 $client->followRedirect();
990 $this->assertSame('de', $client->getRequest()->getLocale());
991 $this->assertSame('de', $client->getContainer()->get('session')->get('_locale'));
994 public function testChangeLocaleWithReferer()
996 $client = $this->getClient();
998 $client->request('GET', '/login');
999 $client->request('GET', '/locale/de');
1000 $client->followRedirect();
1002 $this->assertSame('de', $client->getRequest()->getLocale());
1003 $this->assertSame('de', $client->getContainer()->get('session')->get('_locale'));
1006 public function testChangeLocaleToBadLocale()
1008 $client = $this->getClient();
1010 $client->request('GET', '/login');
1011 $client->request('GET', '/locale/yuyuyuyu');
1012 $client->followRedirect();
1014 $this->assertNotSame('yuyuyuyu', $client->getRequest()->getLocale());
1015 $this->assertNotSame('yuyuyuyu', $client->getContainer()->get('session')->get('_locale'));
1018 public function testUserEnable2faEmail()
1020 $this->logInAs('admin');
1021 $client = $this->getClient();
1023 $crawler = $client->request('GET', '/config/otp/email');
1025 $this->assertSame(302, $client->getResponse()->getStatusCode());
1027 $crawler = $client->followRedirect();
1029 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
1030 $this->assertContains('flashes.config.notice.otp_enabled', $alert[0]);
1033 $em = $this->getEntityManager();
1035 ->getRepository('WallabagUserBundle:User')
1036 ->findOneByUsername('admin');
1038 $this->assertTrue($user->isEmailTwoFactor());
1040 $user->setEmailTwoFactor(false);
1041 $em->persist($user);
1045 public function testUserEnable2faGoogle()
1047 $this->logInAs('admin');
1048 $client = $this->getClient();
1050 $crawler = $client->request('GET', '/config/otp/app');
1052 $this->assertSame(200, $client->getResponse()->getStatusCode());
1055 $em = $this->getEntityManager();
1057 ->getRepository('WallabagUserBundle:User')
1058 ->findOneByUsername('admin');
1060 $this->assertTrue($user->isGoogleTwoFactor());
1061 $this->assertGreaterThan(0, $user->getBackupCodes());
1063 $user->setGoogleAuthenticatorSecret(false);
1064 $user->setBackupCodes(null);
1065 $em->persist($user);
1069 public function testUserEnable2faGoogleCancel()
1071 $this->logInAs('admin');
1072 $client = $this->getClient();
1074 $crawler = $client->request('GET', '/config/otp/app');
1076 $this->assertSame(200, $client->getResponse()->getStatusCode());
1079 $em = $this->getEntityManager();
1081 ->getRepository('WallabagUserBundle:User')
1082 ->findOneByUsername('admin');
1084 $this->assertTrue($user->isGoogleTwoFactor());
1085 $this->assertGreaterThan(0, $user->getBackupCodes());
1087 $crawler = $client->request('GET', '/config/otp/app/cancel');
1089 $this->assertSame(302, $client->getResponse()->getStatusCode());
1092 ->getRepository('WallabagUserBundle:User')
1093 ->findOneByUsername('admin');
1095 $this->assertFalse($user->isGoogleTwoFactor());
1096 $this->assertEmpty($user->getBackupCodes());
1099 public function testExportTaggingRule()
1101 $this->logInAs('admin');
1102 $client = $this->getClient();
1105 $crawler = $client->request('GET', '/tagging-rule/export');
1108 $this->assertSame(200, $client->getResponse()->getStatusCode());
1110 $headers = $client->getResponse()->headers
;
1111 $this->assertSame('application/json', $headers->get('content-type'));
1112 $this->assertSame('attachment; filename="tagging_rules_admin.json"', $headers->get('content-disposition'));
1113 $this->assertSame('UTF-8', $headers->get('content-transfer-encoding'));
1115 $content = json_decode($client->getResponse()->getContent(), true);
1117 $this->assertCount(4, $content);
1118 $this->assertSame('content matches "spurs"', $content[0]['rule']);
1119 $this->assertSame('sport', $content[0]['tags'][0]);
1122 public function testImportTagginfRuleBadFile()
1124 $this->logInAs('admin');
1125 $client = $this->getClient();
1127 $crawler = $client->request('GET', '/config');
1128 $form = $crawler->filter('form[name=upload_tagging_rule_file] > button[type=submit]')->form();
1131 'upload_tagging_rule_file[file]' => '',
1134 $client->submit($form, $data);
1136 $this->assertSame(302, $client->getResponse()->getStatusCode());
1139 public function testImportTagginfRuleFile()
1141 $this->logInAs('admin');
1142 $client = $this->getClient();
1144 $crawler = $client->request('GET', '/config');
1145 $form = $crawler->filter('form[name=upload_tagging_rule_file] > button[type=submit]')->form();
1147 $file = new UploadedFile(__DIR__
. '/../fixtures/tagging_rules_admin.json', 'tagging_rules_admin.json');
1150 'upload_tagging_rule_file[file]' => $file,
1153 $client->submit($form, $data);
1154 $this->assertSame(302, $client->getResponse()->getStatusCode());
1156 $user = $client->getContainer()->get('fos_user.user_manager.test')->findUserBy(['username' => 'admin']);
1157 $taggingRules = $user->getConfig()->getTaggingRules()->toArray();
1158 $this->assertCount(5, $taggingRules);
1159 $this->assertSame('title matches "football"', $taggingRules[4]->getRule());