]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php
Add ability to manually define the reading speed
[github/wallabag/wallabag.git] / tests / Wallabag / CoreBundle / Controller / ConfigControllerTest.php
1 <?php
2
3 namespace Tests\Wallabag\CoreBundle\Controller;
4
5 use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
6 use Wallabag\AnnotationBundle\Entity\Annotation;
7 use Wallabag\CoreBundle\Entity\Config;
8 use Wallabag\CoreBundle\Entity\Entry;
9 use Wallabag\CoreBundle\Entity\Tag;
10 use Wallabag\UserBundle\Entity\User;
11
12 class ConfigControllerTest extends WallabagCoreTestCase
13 {
14 public function testLogin()
15 {
16 $client = $this->getClient();
17
18 $client->request('GET', '/new');
19
20 $this->assertSame(302, $client->getResponse()->getStatusCode());
21 $this->assertContains('login', $client->getResponse()->headers->get('location'));
22 }
23
24 public function testIndex()
25 {
26 $this->logInAs('admin');
27 $client = $this->getClient();
28
29 $crawler = $client->request('GET', '/config');
30
31 $this->assertSame(200, $client->getResponse()->getStatusCode());
32
33 $this->assertCount(1, $crawler->filter('button[id=config_save]'));
34 $this->assertCount(1, $crawler->filter('button[id=change_passwd_save]'));
35 $this->assertCount(1, $crawler->filter('button[id=update_user_save]'));
36 $this->assertCount(1, $crawler->filter('button[id=feed_config_save]'));
37 }
38
39 public function testUpdate()
40 {
41 $this->logInAs('admin');
42 $client = $this->getClient();
43
44 $crawler = $client->request('GET', '/config');
45
46 $this->assertSame(200, $client->getResponse()->getStatusCode());
47
48 $form = $crawler->filter('button[id=config_save]')->form();
49
50 $data = [
51 'config[theme]' => 'baggy',
52 'config[items_per_page]' => '30',
53 'config[reading_speed]' => '100',
54 'config[action_mark_as_read]' => '0',
55 'config[language]' => 'en',
56 ];
57
58 $client->submit($form, $data);
59
60 $this->assertSame(302, $client->getResponse()->getStatusCode());
61
62 $crawler = $client->followRedirect();
63
64 $this->assertContains('flashes.config.notice.config_saved', $crawler->filter('body')->extract(['_text'])[0]);
65 }
66
67 public function testChangeReadingSpeed()
68 {
69 $this->logInAs('admin');
70 $this->useTheme('baggy');
71 $client = $this->getClient();
72
73 $entry = new Entry($this->getLoggedInUser());
74 $entry->setUrl('http://0.0.0.0/test-entry1')
75 ->setReadingTime(22);
76 $this->getEntityManager()->persist($entry);
77
78 $this->getEntityManager()->flush();
79 $this->getEntityManager()->clear();
80
81 $crawler = $client->request('GET', '/unread/list');
82 $form = $crawler->filter('button[id=submit-filter]')->form();
83 $dataFilters = [
84 'entry_filter[readingTime][right_number]' => 22,
85 'entry_filter[readingTime][left_number]' => 22,
86 ];
87 $crawler = $client->submit($form, $dataFilters);
88 $this->assertCount(1, $crawler->filter('div[class=entry]'));
89
90 // Change reading speed
91 $crawler = $client->request('GET', '/config');
92 $form = $crawler->filter('button[id=config_save]')->form();
93 $data = [
94 'config[reading_speed]' => '400',
95 ];
96 $client->submit($form, $data);
97
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]'));
103
104 // Restore old configuration
105 $crawler = $client->request('GET', '/config');
106 $form = $crawler->filter('button[id=config_save]')->form();
107 $data = [
108 'config[reading_speed]' => '100',
109 ];
110 $client->submit($form, $data);
111 }
112
113 public function dataForUpdateFailed()
114 {
115 return [
116 [[
117 'config[theme]' => 'baggy',
118 'config[items_per_page]' => '',
119 'config[language]' => 'en',
120 ]],
121 ];
122 }
123
124 /**
125 * @dataProvider dataForUpdateFailed
126 */
127 public function testUpdateFailed($data)
128 {
129 $this->logInAs('admin');
130 $client = $this->getClient();
131
132 $crawler = $client->request('GET', '/config');
133
134 $this->assertSame(200, $client->getResponse()->getStatusCode());
135
136 $form = $crawler->filter('button[id=config_save]')->form();
137
138 $crawler = $client->submit($form, $data);
139
140 $this->assertSame(200, $client->getResponse()->getStatusCode());
141
142 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
143 $this->assertContains('This value should not be blank', $alert[0]);
144 }
145
146 public function dataForChangePasswordFailed()
147 {
148 return [
149 [
150 [
151 'change_passwd[old_password]' => 'material',
152 'change_passwd[new_password][first]' => '',
153 'change_passwd[new_password][second]' => '',
154 ],
155 'validator.password_wrong_value',
156 ],
157 [
158 [
159 'change_passwd[old_password]' => 'mypassword',
160 'change_passwd[new_password][first]' => '',
161 'change_passwd[new_password][second]' => '',
162 ],
163 'This value should not be blank',
164 ],
165 [
166 [
167 'change_passwd[old_password]' => 'mypassword',
168 'change_passwd[new_password][first]' => 'hop',
169 'change_passwd[new_password][second]' => '',
170 ],
171 'validator.password_must_match',
172 ],
173 [
174 [
175 'change_passwd[old_password]' => 'mypassword',
176 'change_passwd[new_password][first]' => 'hop',
177 'change_passwd[new_password][second]' => 'hop',
178 ],
179 'validator.password_too_short',
180 ],
181 ];
182 }
183
184 /**
185 * @dataProvider dataForChangePasswordFailed
186 */
187 public function testChangePasswordFailed($data, $expectedMessage)
188 {
189 $this->logInAs('admin');
190 $client = $this->getClient();
191
192 $crawler = $client->request('GET', '/config');
193
194 $this->assertSame(200, $client->getResponse()->getStatusCode());
195
196 $form = $crawler->filter('button[id=change_passwd_save]')->form();
197
198 $crawler = $client->submit($form, $data);
199
200 $this->assertSame(200, $client->getResponse()->getStatusCode());
201
202 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
203 $this->assertContains($expectedMessage, $alert[0]);
204 }
205
206 public function testChangePassword()
207 {
208 $this->logInAs('admin');
209 $client = $this->getClient();
210
211 $crawler = $client->request('GET', '/config');
212
213 $this->assertSame(200, $client->getResponse()->getStatusCode());
214
215 $form = $crawler->filter('button[id=change_passwd_save]')->form();
216
217 $data = [
218 'change_passwd[old_password]' => 'mypassword',
219 'change_passwd[new_password][first]' => 'mypassword',
220 'change_passwd[new_password][second]' => 'mypassword',
221 ];
222
223 $client->submit($form, $data);
224
225 $this->assertSame(302, $client->getResponse()->getStatusCode());
226
227 $crawler = $client->followRedirect();
228
229 $this->assertContains('flashes.config.notice.password_updated', $crawler->filter('body')->extract(['_text'])[0]);
230 }
231
232 public function dataForUserFailed()
233 {
234 return [
235 [
236 [
237 'update_user[name]' => '',
238 'update_user[email]' => '',
239 ],
240 'fos_user.email.blank',
241 ],
242 [
243 [
244 'update_user[name]' => '',
245 'update_user[email]' => 'test',
246 ],
247 'fos_user.email.invalid',
248 ],
249 ];
250 }
251
252 /**
253 * @dataProvider dataForUserFailed
254 */
255 public function testUserFailed($data, $expectedMessage)
256 {
257 $this->logInAs('admin');
258 $client = $this->getClient();
259
260 $crawler = $client->request('GET', '/config');
261
262 $this->assertSame(200, $client->getResponse()->getStatusCode());
263
264 $form = $crawler->filter('button[id=update_user_save]')->form();
265
266 $crawler = $client->submit($form, $data);
267
268 $this->assertSame(200, $client->getResponse()->getStatusCode());
269
270 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
271 $this->assertContains($expectedMessage, $alert[0]);
272 }
273
274 public function testUserUpdate()
275 {
276 $this->logInAs('admin');
277 $client = $this->getClient();
278
279 $crawler = $client->request('GET', '/config');
280
281 $this->assertSame(200, $client->getResponse()->getStatusCode());
282
283 $form = $crawler->filter('button[id=update_user_save]')->form();
284
285 $data = [
286 'update_user[name]' => 'new name',
287 'update_user[email]' => 'admin@wallabag.io',
288 ];
289
290 $client->submit($form, $data);
291
292 $this->assertSame(302, $client->getResponse()->getStatusCode());
293
294 $crawler = $client->followRedirect();
295
296 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
297 $this->assertContains('flashes.config.notice.user_updated', $alert[0]);
298 }
299
300 public function testFeedUpdateResetToken()
301 {
302 $this->logInAs('admin');
303 $client = $this->getClient();
304
305 // reset the token
306 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
307 $user = $em
308 ->getRepository('WallabagUserBundle:User')
309 ->findOneByUsername('admin');
310
311 if (!$user) {
312 $this->markTestSkipped('No user found in db.');
313 }
314
315 $config = $user->getConfig();
316 $config->setFeedToken(null);
317 $em->persist($config);
318 $em->flush();
319
320 $crawler = $client->request('GET', '/config');
321
322 $this->assertSame(200, $client->getResponse()->getStatusCode());
323
324 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
325 $this->assertContains('config.form_feed.no_token', $body[0]);
326
327 $client->request('GET', '/generate-token');
328 $this->assertSame(302, $client->getResponse()->getStatusCode());
329
330 $crawler = $client->followRedirect();
331
332 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
333 $this->assertContains('config.form_feed.token_reset', $body[0]);
334 }
335
336 public function testGenerateTokenAjax()
337 {
338 $this->logInAs('admin');
339 $client = $this->getClient();
340
341 $client->request(
342 'GET',
343 '/generate-token',
344 [],
345 [],
346 ['HTTP_X-Requested-With' => 'XMLHttpRequest']
347 );
348
349 $this->assertSame(200, $client->getResponse()->getStatusCode());
350 $content = json_decode($client->getResponse()->getContent(), true);
351 $this->assertArrayHasKey('token', $content);
352 }
353
354 public function testRevokeTokenAjax()
355 {
356 $this->logInAs('admin');
357 $client = $this->getClient();
358
359 $client->request(
360 'GET',
361 '/revoke-token',
362 [],
363 [],
364 ['HTTP_X-Requested-With' => 'XMLHttpRequest']
365 );
366
367 $this->assertSame(200, $client->getResponse()->getStatusCode());
368 }
369
370 public function testFeedUpdate()
371 {
372 $this->logInAs('admin');
373 $client = $this->getClient();
374
375 $crawler = $client->request('GET', '/config');
376
377 $this->assertSame(200, $client->getResponse()->getStatusCode());
378
379 $form = $crawler->filter('button[id=feed_config_save]')->form();
380
381 $data = [
382 'feed_config[feed_limit]' => 12,
383 ];
384
385 $client->submit($form, $data);
386
387 $this->assertSame(302, $client->getResponse()->getStatusCode());
388
389 $crawler = $client->followRedirect();
390
391 $this->assertContains('flashes.config.notice.feed_updated', $crawler->filter('body')->extract(['_text'])[0]);
392 }
393
394 public function dataForFeedFailed()
395 {
396 return [
397 [
398 [
399 'feed_config[feed_limit]' => 0,
400 ],
401 'This value should be 1 or more.',
402 ],
403 [
404 [
405 'feed_config[feed_limit]' => 1000000000000,
406 ],
407 'validator.feed_limit_too_high',
408 ],
409 ];
410 }
411
412 /**
413 * @dataProvider dataForFeedFailed
414 */
415 public function testFeedFailed($data, $expectedMessage)
416 {
417 $this->logInAs('admin');
418 $client = $this->getClient();
419
420 $crawler = $client->request('GET', '/config');
421
422 $this->assertSame(200, $client->getResponse()->getStatusCode());
423
424 $form = $crawler->filter('button[id=feed_config_save]')->form();
425
426 $crawler = $client->submit($form, $data);
427
428 $this->assertSame(200, $client->getResponse()->getStatusCode());
429
430 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
431 $this->assertContains($expectedMessage, $alert[0]);
432 }
433
434 public function testTaggingRuleCreation()
435 {
436 $this->logInAs('admin');
437 $this->useTheme('baggy');
438 $client = $this->getClient();
439
440 $crawler = $client->request('GET', '/config');
441
442 $this->assertSame(200, $client->getResponse()->getStatusCode());
443
444 $form = $crawler->filter('button[id=tagging_rule_save]')->form();
445
446 $data = [
447 'tagging_rule[rule]' => 'readingTime <= 3',
448 'tagging_rule[tags]' => 'short reading',
449 ];
450
451 $client->submit($form, $data);
452
453 $this->assertSame(302, $client->getResponse()->getStatusCode());
454
455 $crawler = $client->followRedirect();
456
457 $this->assertContains('flashes.config.notice.tagging_rules_updated', $crawler->filter('body')->extract(['_text'])[0]);
458
459 $editLink = $crawler->filter('.mode_edit')->last()->link();
460
461 $crawler = $client->click($editLink);
462 $this->assertSame(302, $client->getResponse()->getStatusCode());
463 $this->assertContains('?tagging-rule=', $client->getResponse()->headers->get('location'));
464
465 $crawler = $client->followRedirect();
466
467 $form = $crawler->filter('button[id=tagging_rule_save]')->form();
468
469 $data = [
470 'tagging_rule[rule]' => 'readingTime <= 30',
471 'tagging_rule[tags]' => 'short reading',
472 ];
473
474 $client->submit($form, $data);
475
476 $this->assertSame(302, $client->getResponse()->getStatusCode());
477
478 $crawler = $client->followRedirect();
479
480 $this->assertContains('flashes.config.notice.tagging_rules_updated', $crawler->filter('body')->extract(['_text'])[0]);
481
482 $this->assertContains('readingTime <= 30', $crawler->filter('body')->extract(['_text'])[0]);
483
484 $deleteLink = $crawler->filter('.delete')->last()->link();
485
486 $crawler = $client->click($deleteLink);
487 $this->assertSame(302, $client->getResponse()->getStatusCode());
488
489 $crawler = $client->followRedirect();
490 $this->assertContains('flashes.config.notice.tagging_rules_deleted', $crawler->filter('body')->extract(['_text'])[0]);
491 }
492
493 public function dataForTaggingRuleFailed()
494 {
495 return [
496 [
497 [
498 'tagging_rule[rule]' => 'unknownVar <= 3',
499 'tagging_rule[tags]' => 'cool tag',
500 ],
501 [
502 'The variable',
503 'does not exist.',
504 ],
505 ],
506 [
507 [
508 'tagging_rule[rule]' => 'length(domainName) <= 42',
509 'tagging_rule[tags]' => 'cool tag',
510 ],
511 [
512 'The operator',
513 'does not exist.',
514 ],
515 ],
516 ];
517 }
518
519 /**
520 * @dataProvider dataForTaggingRuleFailed
521 */
522 public function testTaggingRuleCreationFail($data, $messages)
523 {
524 $this->logInAs('admin');
525 $client = $this->getClient();
526
527 $crawler = $client->request('GET', '/config');
528
529 $this->assertSame(200, $client->getResponse()->getStatusCode());
530
531 $form = $crawler->filter('button[id=tagging_rule_save]')->form();
532
533 $crawler = $client->submit($form, $data);
534
535 $this->assertSame(200, $client->getResponse()->getStatusCode());
536
537 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
538
539 foreach ($messages as $message) {
540 $this->assertContains($message, $body[0]);
541 }
542 }
543
544 public function testTaggingRuleTooLong()
545 {
546 $this->logInAs('admin');
547 $client = $this->getClient();
548
549 $crawler = $client->request('GET', '/config');
550
551 $this->assertSame(200, $client->getResponse()->getStatusCode());
552
553 $form = $crawler->filter('button[id=tagging_rule_save]')->form();
554
555 $crawler = $client->submit($form, [
556 'tagging_rule[rule]' => str_repeat('title', 60),
557 'tagging_rule[tags]' => 'cool tag',
558 ]);
559
560 $this->assertSame(200, $client->getResponse()->getStatusCode());
561
562 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
563
564 $this->assertContains('255 characters', $body[0]);
565 }
566
567 public function testDeletingTaggingRuleFromAnOtherUser()
568 {
569 $this->logInAs('bob');
570 $client = $this->getClient();
571
572 $rule = $client->getContainer()->get('doctrine.orm.entity_manager')
573 ->getRepository('WallabagCoreBundle:TaggingRule')
574 ->findAll()[0];
575
576 $crawler = $client->request('GET', '/tagging-rule/edit/' . $rule->getId());
577
578 $this->assertSame(403, $client->getResponse()->getStatusCode());
579 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
580 $this->assertContains('You can not access this tagging rule', $body[0]);
581 }
582
583 public function testEditingTaggingRuleFromAnOtherUser()
584 {
585 $this->logInAs('bob');
586 $client = $this->getClient();
587
588 $rule = $client->getContainer()->get('doctrine.orm.entity_manager')
589 ->getRepository('WallabagCoreBundle:TaggingRule')
590 ->findAll()[0];
591
592 $crawler = $client->request('GET', '/tagging-rule/edit/' . $rule->getId());
593
594 $this->assertSame(403, $client->getResponse()->getStatusCode());
595 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
596 $this->assertContains('You can not access this tagging rule', $body[0]);
597 }
598
599 public function testDemoMode()
600 {
601 $this->logInAs('admin');
602 $client = $this->getClient();
603
604 $config = $client->getContainer()->get('craue_config');
605 $config->set('demo_mode_enabled', 1);
606 $config->set('demo_mode_username', 'admin');
607
608 $crawler = $client->request('GET', '/config');
609
610 $this->assertSame(200, $client->getResponse()->getStatusCode());
611
612 $form = $crawler->filter('button[id=change_passwd_save]')->form();
613
614 $data = [
615 'change_passwd[old_password]' => 'mypassword',
616 'change_passwd[new_password][first]' => 'mypassword',
617 'change_passwd[new_password][second]' => 'mypassword',
618 ];
619
620 $client->submit($form, $data);
621
622 $this->assertSame(302, $client->getResponse()->getStatusCode());
623 $this->assertContains('flashes.config.notice.password_not_updated_demo', $client->getContainer()->get('session')->getFlashBag()->get('notice')[0]);
624
625 $config->set('demo_mode_enabled', 0);
626 $config->set('demo_mode_username', 'wallabag');
627 }
628
629 public function testDeleteUserButtonVisibility()
630 {
631 $this->logInAs('admin');
632 $client = $this->getClient();
633
634 $crawler = $client->request('GET', '/config');
635
636 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
637 $this->assertContains('config.form_user.delete.button', $body[0]);
638
639 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
640
641 $user = $em
642 ->getRepository('WallabagUserBundle:User')
643 ->findOneByUsername('empty');
644 $user->setEnabled(false);
645 $em->persist($user);
646
647 $user = $em
648 ->getRepository('WallabagUserBundle:User')
649 ->findOneByUsername('bob');
650 $user->setEnabled(false);
651 $em->persist($user);
652
653 $em->flush();
654
655 $crawler = $client->request('GET', '/config');
656
657 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
658 $this->assertNotContains('config.form_user.delete.button', $body[0]);
659
660 $client->request('GET', '/account/delete');
661 $this->assertSame(403, $client->getResponse()->getStatusCode());
662
663 $user = $em
664 ->getRepository('WallabagUserBundle:User')
665 ->findOneByUsername('empty');
666 $user->setEnabled(true);
667 $em->persist($user);
668
669 $user = $em
670 ->getRepository('WallabagUserBundle:User')
671 ->findOneByUsername('bob');
672 $user->setEnabled(true);
673 $em->persist($user);
674
675 $em->flush();
676 }
677
678 public function testDeleteAccount()
679 {
680 $client = $this->getClient();
681 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
682
683 $user = new User();
684 $user->setName('Wallace');
685 $user->setEmail('wallace@wallabag.org');
686 $user->setUsername('wallace');
687 $user->setPlainPassword('wallace');
688 $user->setEnabled(true);
689 $user->addRole('ROLE_SUPER_ADMIN');
690
691 $em->persist($user);
692
693 $config = new Config($user);
694
695 $config->setTheme('material');
696 $config->setItemsPerPage(30);
697 $config->setReadingSpeed(1);
698 $config->setLanguage('en');
699 $config->setPocketConsumerKey('xxxxx');
700
701 $em->persist($config);
702 $em->flush();
703
704 $this->logInAs('wallace');
705 $loggedInUserId = $this->getLoggedInUserId();
706
707 // create entry to check after user deletion
708 // that this entry is also deleted
709 $crawler = $client->request('GET', '/new');
710
711 $this->assertSame(200, $client->getResponse()->getStatusCode());
712
713 $form = $crawler->filter('form[name=entry]')->form();
714 $data = [
715 'entry[url]' => $url = 'https://github.com/wallabag/wallabag',
716 ];
717
718 $client->submit($form, $data);
719 $this->assertSame(302, $client->getResponse()->getStatusCode());
720
721 $crawler = $client->request('GET', '/config');
722
723 $deleteLink = $crawler->filter('.delete-account')->last()->link();
724
725 $client->click($deleteLink);
726 $this->assertSame(302, $client->getResponse()->getStatusCode());
727
728 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
729 $user = $em
730 ->getRepository('WallabagUserBundle:User')
731 ->createQueryBuilder('u')
732 ->where('u.username = :username')->setParameter('username', 'wallace')
733 ->getQuery()
734 ->getOneOrNullResult()
735 ;
736
737 $this->assertNull($user);
738
739 $entries = $client->getContainer()
740 ->get('doctrine.orm.entity_manager')
741 ->getRepository('WallabagCoreBundle:Entry')
742 ->findByUser($loggedInUserId);
743
744 $this->assertEmpty($entries);
745 }
746
747 public function testReset()
748 {
749 $this->logInAs('empty');
750 $client = $this->getClient();
751
752 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
753
754 $user = static::$kernel->getContainer()->get('security.token_storage')->getToken()->getUser();
755
756 $tag = new Tag();
757 $tag->setLabel('super');
758 $em->persist($tag);
759
760 $entry = new Entry($user);
761 $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');
762 $entry->setContent('Youhou');
763 $entry->setTitle('Youhou');
764 $entry->addTag($tag);
765 $em->persist($entry);
766
767 $entry2 = new Entry($user);
768 $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');
769 $entry2->setContent('Youhou');
770 $entry2->setTitle('Youhou');
771 $entry2->addTag($tag);
772 $em->persist($entry2);
773
774 $annotation = new Annotation($user);
775 $annotation->setText('annotated');
776 $annotation->setQuote('annotated');
777 $annotation->setRanges([]);
778 $annotation->setEntry($entry);
779 $em->persist($annotation);
780
781 $em->flush();
782
783 // reset annotations
784 $crawler = $client->request('GET', '/config#set3');
785
786 $this->assertSame(200, $client->getResponse()->getStatusCode());
787
788 $crawler = $client->click($crawler->selectLink('config.reset.annotations')->link());
789
790 $this->assertSame(302, $client->getResponse()->getStatusCode());
791 $this->assertContains('flashes.config.notice.annotations_reset', $client->getContainer()->get('session')->getFlashBag()->get('notice')[0]);
792
793 $annotationsReset = $em
794 ->getRepository('WallabagAnnotationBundle:Annotation')
795 ->findAnnotationsByPageId($entry->getId(), $user->getId());
796
797 $this->assertEmpty($annotationsReset, 'Annotations were reset');
798
799 // reset tags
800 $crawler = $client->request('GET', '/config#set3');
801
802 $this->assertSame(200, $client->getResponse()->getStatusCode());
803
804 $crawler = $client->click($crawler->selectLink('config.reset.tags')->link());
805
806 $this->assertSame(302, $client->getResponse()->getStatusCode());
807 $this->assertContains('flashes.config.notice.tags_reset', $client->getContainer()->get('session')->getFlashBag()->get('notice')[0]);
808
809 $tagReset = $em
810 ->getRepository('WallabagCoreBundle:Tag')
811 ->countAllTags($user->getId());
812
813 $this->assertSame(0, $tagReset, 'Tags were reset');
814
815 // reset entries
816 $crawler = $client->request('GET', '/config#set3');
817
818 $this->assertSame(200, $client->getResponse()->getStatusCode());
819
820 $crawler = $client->click($crawler->selectLink('config.reset.entries')->link());
821
822 $this->assertSame(302, $client->getResponse()->getStatusCode());
823 $this->assertContains('flashes.config.notice.entries_reset', $client->getContainer()->get('session')->getFlashBag()->get('notice')[0]);
824
825 $entryReset = $em
826 ->getRepository('WallabagCoreBundle:Entry')
827 ->countAllEntriesByUser($user->getId());
828
829 $this->assertSame(0, $entryReset, 'Entries were reset');
830 }
831
832 public function testResetArchivedEntries()
833 {
834 $this->logInAs('empty');
835 $client = $this->getClient();
836
837 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
838
839 $user = static::$kernel->getContainer()->get('security.token_storage')->getToken()->getUser();
840
841 $tag = new Tag();
842 $tag->setLabel('super');
843 $em->persist($tag);
844
845 $entry = new Entry($user);
846 $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');
847 $entry->setContent('Youhou');
848 $entry->setTitle('Youhou');
849 $entry->addTag($tag);
850 $em->persist($entry);
851
852 $annotation = new Annotation($user);
853 $annotation->setText('annotated');
854 $annotation->setQuote('annotated');
855 $annotation->setRanges([]);
856 $annotation->setEntry($entry);
857 $em->persist($annotation);
858
859 $tagArchived = new Tag();
860 $tagArchived->setLabel('super');
861 $em->persist($tagArchived);
862
863 $entryArchived = new Entry($user);
864 $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');
865 $entryArchived->setContent('Youhou');
866 $entryArchived->setTitle('Youhou');
867 $entryArchived->addTag($tagArchived);
868 $entryArchived->updateArchived(true);
869 $em->persist($entryArchived);
870
871 $annotationArchived = new Annotation($user);
872 $annotationArchived->setText('annotated');
873 $annotationArchived->setQuote('annotated');
874 $annotationArchived->setRanges([]);
875 $annotationArchived->setEntry($entryArchived);
876 $em->persist($annotationArchived);
877
878 $em->flush();
879
880 $crawler = $client->request('GET', '/config#set3');
881
882 $this->assertSame(200, $client->getResponse()->getStatusCode());
883
884 $crawler = $client->click($crawler->selectLink('config.reset.archived')->link());
885
886 $this->assertSame(302, $client->getResponse()->getStatusCode());
887 $this->assertContains('flashes.config.notice.archived_reset', $client->getContainer()->get('session')->getFlashBag()->get('notice')[0]);
888
889 $entryReset = $em
890 ->getRepository('WallabagCoreBundle:Entry')
891 ->countAllEntriesByUser($user->getId());
892
893 $this->assertSame(1, $entryReset, 'Entries were reset');
894
895 $tagReset = $em
896 ->getRepository('WallabagCoreBundle:Tag')
897 ->countAllTags($user->getId());
898
899 $this->assertSame(1, $tagReset, 'Tags were reset');
900
901 $annotationsReset = $em
902 ->getRepository('WallabagAnnotationBundle:Annotation')
903 ->findAnnotationsByPageId($annotationArchived->getId(), $user->getId());
904
905 $this->assertEmpty($annotationsReset, 'Annotations were reset');
906 }
907
908 public function testResetEntriesCascade()
909 {
910 $this->logInAs('empty');
911 $client = $this->getClient();
912
913 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
914
915 $user = static::$kernel->getContainer()->get('security.token_storage')->getToken()->getUser();
916
917 $tag = new Tag();
918 $tag->setLabel('super');
919 $em->persist($tag);
920
921 $entry = new Entry($user);
922 $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');
923 $entry->setContent('Youhou');
924 $entry->setTitle('Youhou');
925 $entry->addTag($tag);
926 $em->persist($entry);
927
928 $annotation = new Annotation($user);
929 $annotation->setText('annotated');
930 $annotation->setQuote('annotated');
931 $annotation->setRanges([]);
932 $annotation->setEntry($entry);
933 $em->persist($annotation);
934
935 $em->flush();
936
937 $crawler = $client->request('GET', '/config#set3');
938
939 $this->assertSame(200, $client->getResponse()->getStatusCode());
940
941 $crawler = $client->click($crawler->selectLink('config.reset.entries')->link());
942
943 $this->assertSame(302, $client->getResponse()->getStatusCode());
944 $this->assertContains('flashes.config.notice.entries_reset', $client->getContainer()->get('session')->getFlashBag()->get('notice')[0]);
945
946 $entryReset = $em
947 ->getRepository('WallabagCoreBundle:Entry')
948 ->countAllEntriesByUser($user->getId());
949
950 $this->assertSame(0, $entryReset, 'Entries were reset');
951
952 $tagReset = $em
953 ->getRepository('WallabagCoreBundle:Tag')
954 ->countAllTags($user->getId());
955
956 $this->assertSame(0, $tagReset, 'Tags were reset');
957
958 $annotationsReset = $em
959 ->getRepository('WallabagAnnotationBundle:Annotation')
960 ->findAnnotationsByPageId($entry->getId(), $user->getId());
961
962 $this->assertEmpty($annotationsReset, 'Annotations were reset');
963 }
964
965 public function testSwitchViewMode()
966 {
967 $this->logInAs('admin');
968 $this->useTheme('baggy');
969 $client = $this->getClient();
970
971 $client->request('GET', '/unread/list');
972
973 $this->assertNotContains('listmode', $client->getResponse()->getContent());
974
975 $client->request('GET', '/config/view-mode');
976 $crawler = $client->followRedirect();
977
978 $client->request('GET', '/unread/list');
979
980 $this->assertContains('listmode', $client->getResponse()->getContent());
981
982 $client->request('GET', '/config/view-mode');
983 }
984
985 public function testChangeLocaleWithoutReferer()
986 {
987 $client = $this->getClient();
988
989 $client->request('GET', '/locale/de');
990 $client->followRedirect();
991
992 $this->assertSame('de', $client->getRequest()->getLocale());
993 $this->assertSame('de', $client->getContainer()->get('session')->get('_locale'));
994 }
995
996 public function testChangeLocaleWithReferer()
997 {
998 $client = $this->getClient();
999
1000 $client->request('GET', '/login');
1001 $client->request('GET', '/locale/de');
1002 $client->followRedirect();
1003
1004 $this->assertSame('de', $client->getRequest()->getLocale());
1005 $this->assertSame('de', $client->getContainer()->get('session')->get('_locale'));
1006 }
1007
1008 public function testChangeLocaleToBadLocale()
1009 {
1010 $client = $this->getClient();
1011
1012 $client->request('GET', '/login');
1013 $client->request('GET', '/locale/yuyuyuyu');
1014 $client->followRedirect();
1015
1016 $this->assertNotSame('yuyuyuyu', $client->getRequest()->getLocale());
1017 $this->assertNotSame('yuyuyuyu', $client->getContainer()->get('session')->get('_locale'));
1018 }
1019
1020 public function testUserEnable2faEmail()
1021 {
1022 $this->logInAs('admin');
1023 $client = $this->getClient();
1024
1025 $crawler = $client->request('GET', '/config/otp/email');
1026
1027 $this->assertSame(302, $client->getResponse()->getStatusCode());
1028
1029 $crawler = $client->followRedirect();
1030
1031 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
1032 $this->assertContains('flashes.config.notice.otp_enabled', $alert[0]);
1033
1034 // restore user
1035 $em = $this->getEntityManager();
1036 $user = $em
1037 ->getRepository('WallabagUserBundle:User')
1038 ->findOneByUsername('admin');
1039
1040 $this->assertTrue($user->isEmailTwoFactor());
1041
1042 $user->setEmailTwoFactor(false);
1043 $em->persist($user);
1044 $em->flush();
1045 }
1046
1047 public function testUserEnable2faGoogle()
1048 {
1049 $this->logInAs('admin');
1050 $client = $this->getClient();
1051
1052 $crawler = $client->request('GET', '/config/otp/app');
1053
1054 $this->assertSame(200, $client->getResponse()->getStatusCode());
1055
1056 // restore user
1057 $em = $this->getEntityManager();
1058 $user = $em
1059 ->getRepository('WallabagUserBundle:User')
1060 ->findOneByUsername('admin');
1061
1062 $this->assertTrue($user->isGoogleTwoFactor());
1063 $this->assertGreaterThan(0, $user->getBackupCodes());
1064
1065 $user->setGoogleAuthenticatorSecret(false);
1066 $user->setBackupCodes(null);
1067 $em->persist($user);
1068 $em->flush();
1069 }
1070
1071 public function testUserEnable2faGoogleCancel()
1072 {
1073 $this->logInAs('admin');
1074 $client = $this->getClient();
1075
1076 $crawler = $client->request('GET', '/config/otp/app');
1077
1078 $this->assertSame(200, $client->getResponse()->getStatusCode());
1079
1080 // restore user
1081 $em = $this->getEntityManager();
1082 $user = $em
1083 ->getRepository('WallabagUserBundle:User')
1084 ->findOneByUsername('admin');
1085
1086 $this->assertTrue($user->isGoogleTwoFactor());
1087 $this->assertGreaterThan(0, $user->getBackupCodes());
1088
1089 $crawler = $client->request('GET', '/config/otp/app/cancel');
1090
1091 $this->assertSame(302, $client->getResponse()->getStatusCode());
1092
1093 $user = $em
1094 ->getRepository('WallabagUserBundle:User')
1095 ->findOneByUsername('admin');
1096
1097 $this->assertFalse($user->isGoogleTwoFactor());
1098 $this->assertEmpty($user->getBackupCodes());
1099 }
1100 }