]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php
40a1aa97879199e0c0e7b469436cc7236f95a402
[github/wallabag/wallabag.git] / tests / Wallabag / CoreBundle / Controller / ConfigControllerTest.php
1 <?php
2
3 namespace Tests\Wallabag\CoreBundle\Controller;
4
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;
12
13 class ConfigControllerTest extends WallabagCoreTestCase
14 {
15 public function testLogin()
16 {
17 $client = $this->getClient();
18
19 $client->request('GET', '/new');
20
21 $this->assertSame(302, $client->getResponse()->getStatusCode());
22 $this->assertContains('login', $client->getResponse()->headers->get('location'));
23 }
24
25 public function testIndex()
26 {
27 $this->logInAs('admin');
28 $client = $this->getClient();
29
30 $crawler = $client->request('GET', '/config');
31
32 $this->assertSame(200, $client->getResponse()->getStatusCode());
33
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]'));
38 }
39
40 public function testUpdate()
41 {
42 $this->logInAs('admin');
43 $client = $this->getClient();
44
45 $crawler = $client->request('GET', '/config');
46
47 $this->assertSame(200, $client->getResponse()->getStatusCode());
48
49 $form = $crawler->filter('button[id=config_save]')->form();
50
51 $data = [
52 'config[theme]' => 'baggy',
53 'config[items_per_page]' => '30',
54 'config[reading_speed]' => '100',
55 'config[action_mark_as_read]' => '0',
56 'config[language]' => 'en',
57 ];
58
59 $client->submit($form, $data);
60
61 $this->assertSame(302, $client->getResponse()->getStatusCode());
62
63 $crawler = $client->followRedirect();
64
65 $this->assertContains('flashes.config.notice.config_saved', $crawler->filter('body')->extract(['_text'])[0]);
66 }
67
68 public function testChangeReadingSpeed()
69 {
70 $this->logInAs('admin');
71 $this->useTheme('baggy');
72 $client = $this->getClient();
73
74 $entry = new Entry($this->getLoggedInUser());
75 $entry->setUrl('http://0.0.0.0/test-entry1')
76 ->setReadingTime(22);
77 $this->getEntityManager()->persist($entry);
78
79 $this->getEntityManager()->flush();
80 $this->getEntityManager()->clear();
81
82 $crawler = $client->request('GET', '/unread/list');
83 $form = $crawler->filter('button[id=submit-filter]')->form();
84 $dataFilters = [
85 'entry_filter[readingTime][right_number]' => 22,
86 'entry_filter[readingTime][left_number]' => 22,
87 ];
88 $crawler = $client->submit($form, $dataFilters);
89 $this->assertCount(1, $crawler->filter('div[class=entry]'));
90
91 // Change reading speed
92 $crawler = $client->request('GET', '/config');
93 $form = $crawler->filter('button[id=config_save]')->form();
94 $data = [
95 'config[reading_speed]' => '400',
96 ];
97 $client->submit($form, $data);
98
99 // Is the entry still available via filters?
100 $crawler = $client->request('GET', '/unread/list');
101 $form = $crawler->filter('button[id=submit-filter]')->form();
102 $crawler = $client->submit($form, $dataFilters);
103 $this->assertCount(0, $crawler->filter('div[class=entry]'));
104
105 // Restore old configuration
106 $crawler = $client->request('GET', '/config');
107 $form = $crawler->filter('button[id=config_save]')->form();
108 $data = [
109 'config[reading_speed]' => '100',
110 ];
111 $client->submit($form, $data);
112 }
113
114 public function dataForUpdateFailed()
115 {
116 return [
117 [[
118 'config[theme]' => 'baggy',
119 'config[items_per_page]' => '',
120 'config[language]' => 'en',
121 ]],
122 ];
123 }
124
125 /**
126 * @dataProvider dataForUpdateFailed
127 */
128 public function testUpdateFailed($data)
129 {
130 $this->logInAs('admin');
131 $client = $this->getClient();
132
133 $crawler = $client->request('GET', '/config');
134
135 $this->assertSame(200, $client->getResponse()->getStatusCode());
136
137 $form = $crawler->filter('button[id=config_save]')->form();
138
139 $crawler = $client->submit($form, $data);
140
141 $this->assertSame(200, $client->getResponse()->getStatusCode());
142
143 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
144 $this->assertContains('This value should not be blank', $alert[0]);
145 }
146
147 public function dataForChangePasswordFailed()
148 {
149 return [
150 [
151 [
152 'change_passwd[old_password]' => 'material',
153 'change_passwd[new_password][first]' => '',
154 'change_passwd[new_password][second]' => '',
155 ],
156 'validator.password_wrong_value',
157 ],
158 [
159 [
160 'change_passwd[old_password]' => 'mypassword',
161 'change_passwd[new_password][first]' => '',
162 'change_passwd[new_password][second]' => '',
163 ],
164 'This value should not be blank',
165 ],
166 [
167 [
168 'change_passwd[old_password]' => 'mypassword',
169 'change_passwd[new_password][first]' => 'hop',
170 'change_passwd[new_password][second]' => '',
171 ],
172 'validator.password_must_match',
173 ],
174 [
175 [
176 'change_passwd[old_password]' => 'mypassword',
177 'change_passwd[new_password][first]' => 'hop',
178 'change_passwd[new_password][second]' => 'hop',
179 ],
180 'validator.password_too_short',
181 ],
182 ];
183 }
184
185 /**
186 * @dataProvider dataForChangePasswordFailed
187 */
188 public function testChangePasswordFailed($data, $expectedMessage)
189 {
190 $this->logInAs('admin');
191 $client = $this->getClient();
192
193 $crawler = $client->request('GET', '/config');
194
195 $this->assertSame(200, $client->getResponse()->getStatusCode());
196
197 $form = $crawler->filter('button[id=change_passwd_save]')->form();
198
199 $crawler = $client->submit($form, $data);
200
201 $this->assertSame(200, $client->getResponse()->getStatusCode());
202
203 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
204 $this->assertContains($expectedMessage, $alert[0]);
205 }
206
207 public function testChangePassword()
208 {
209 $this->logInAs('admin');
210 $client = $this->getClient();
211
212 $crawler = $client->request('GET', '/config');
213
214 $this->assertSame(200, $client->getResponse()->getStatusCode());
215
216 $form = $crawler->filter('button[id=change_passwd_save]')->form();
217
218 $data = [
219 'change_passwd[old_password]' => 'mypassword',
220 'change_passwd[new_password][first]' => 'mypassword',
221 'change_passwd[new_password][second]' => 'mypassword',
222 ];
223
224 $client->submit($form, $data);
225
226 $this->assertSame(302, $client->getResponse()->getStatusCode());
227
228 $crawler = $client->followRedirect();
229
230 $this->assertContains('flashes.config.notice.password_updated', $crawler->filter('body')->extract(['_text'])[0]);
231 }
232
233 public function dataForUserFailed()
234 {
235 return [
236 [
237 [
238 'update_user[name]' => '',
239 'update_user[email]' => '',
240 ],
241 'fos_user.email.blank',
242 ],
243 [
244 [
245 'update_user[name]' => '',
246 'update_user[email]' => 'test',
247 ],
248 'fos_user.email.invalid',
249 ],
250 ];
251 }
252
253 /**
254 * @dataProvider dataForUserFailed
255 */
256 public function testUserFailed($data, $expectedMessage)
257 {
258 $this->logInAs('admin');
259 $client = $this->getClient();
260
261 $crawler = $client->request('GET', '/config');
262
263 $this->assertSame(200, $client->getResponse()->getStatusCode());
264
265 $form = $crawler->filter('button[id=update_user_save]')->form();
266
267 $crawler = $client->submit($form, $data);
268
269 $this->assertSame(200, $client->getResponse()->getStatusCode());
270
271 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
272 $this->assertContains($expectedMessage, $alert[0]);
273 }
274
275 public function testUserUpdate()
276 {
277 $this->logInAs('admin');
278 $client = $this->getClient();
279
280 $crawler = $client->request('GET', '/config');
281
282 $this->assertSame(200, $client->getResponse()->getStatusCode());
283
284 $form = $crawler->filter('button[id=update_user_save]')->form();
285
286 $data = [
287 'update_user[name]' => 'new name',
288 'update_user[email]' => 'admin@wallabag.io',
289 ];
290
291 $client->submit($form, $data);
292
293 $this->assertSame(302, $client->getResponse()->getStatusCode());
294
295 $crawler = $client->followRedirect();
296
297 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
298 $this->assertContains('flashes.config.notice.user_updated', $alert[0]);
299 }
300
301 public function testFeedUpdateResetToken()
302 {
303 $this->logInAs('admin');
304 $client = $this->getClient();
305
306 // reset the token
307 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
308 $user = $em
309 ->getRepository('WallabagUserBundle:User')
310 ->findOneByUsername('admin');
311
312 if (!$user) {
313 $this->markTestSkipped('No user found in db.');
314 }
315
316 $config = $user->getConfig();
317 $config->setFeedToken(null);
318 $em->persist($config);
319 $em->flush();
320
321 $crawler = $client->request('GET', '/config');
322
323 $this->assertSame(200, $client->getResponse()->getStatusCode());
324
325 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
326 $this->assertContains('config.form_feed.no_token', $body[0]);
327
328 $client->request('GET', '/generate-token');
329 $this->assertSame(302, $client->getResponse()->getStatusCode());
330
331 $crawler = $client->followRedirect();
332
333 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
334 $this->assertContains('config.form_feed.token_reset', $body[0]);
335 }
336
337 public function testGenerateTokenAjax()
338 {
339 $this->logInAs('admin');
340 $client = $this->getClient();
341
342 $client->request(
343 'GET',
344 '/generate-token',
345 [],
346 [],
347 ['HTTP_X-Requested-With' => 'XMLHttpRequest']
348 );
349
350 $this->assertSame(200, $client->getResponse()->getStatusCode());
351 $content = json_decode($client->getResponse()->getContent(), true);
352 $this->assertArrayHasKey('token', $content);
353 }
354
355 public function testRevokeTokenAjax()
356 {
357 $this->logInAs('admin');
358 $client = $this->getClient();
359
360 $client->request(
361 'GET',
362 '/revoke-token',
363 [],
364 [],
365 ['HTTP_X-Requested-With' => 'XMLHttpRequest']
366 );
367
368 $this->assertSame(200, $client->getResponse()->getStatusCode());
369 }
370
371 public function testFeedUpdate()
372 {
373 $this->logInAs('admin');
374 $client = $this->getClient();
375
376 $crawler = $client->request('GET', '/config');
377
378 $this->assertSame(200, $client->getResponse()->getStatusCode());
379
380 $form = $crawler->filter('button[id=feed_config_save]')->form();
381
382 $data = [
383 'feed_config[feed_limit]' => 12,
384 ];
385
386 $client->submit($form, $data);
387
388 $this->assertSame(302, $client->getResponse()->getStatusCode());
389
390 $crawler = $client->followRedirect();
391
392 $this->assertContains('flashes.config.notice.feed_updated', $crawler->filter('body')->extract(['_text'])[0]);
393 }
394
395 public function dataForFeedFailed()
396 {
397 return [
398 [
399 [
400 'feed_config[feed_limit]' => 0,
401 ],
402 'This value should be 1 or more.',
403 ],
404 [
405 [
406 'feed_config[feed_limit]' => 1000000000000,
407 ],
408 'validator.feed_limit_too_high',
409 ],
410 ];
411 }
412
413 /**
414 * @dataProvider dataForFeedFailed
415 */
416 public function testFeedFailed($data, $expectedMessage)
417 {
418 $this->logInAs('admin');
419 $client = $this->getClient();
420
421 $crawler = $client->request('GET', '/config');
422
423 $this->assertSame(200, $client->getResponse()->getStatusCode());
424
425 $form = $crawler->filter('button[id=feed_config_save]')->form();
426
427 $crawler = $client->submit($form, $data);
428
429 $this->assertSame(200, $client->getResponse()->getStatusCode());
430
431 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
432 $this->assertContains($expectedMessage, $alert[0]);
433 }
434
435 public function testTaggingRuleCreation()
436 {
437 $this->logInAs('admin');
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('div[id=set5] a.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('div[id=set5] a.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/delete/' . $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 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 rule', $body[0]);
597 }
598
599 public function testIgnoreOriginRuleCreation()
600 {
601 $this->logInAs('admin');
602 $client = $this->getClient();
603
604 $crawler = $client->request('GET', '/config');
605
606 $this->assertSame(200, $client->getResponse()->getStatusCode());
607
608 $form = $crawler->filter('button[id=ignore_origin_user_rule_save]')->form();
609
610 $data = [
611 'ignore_origin_user_rule[rule]' => 'host = "example.com"',
612 ];
613
614 $client->submit($form, $data);
615
616 $this->assertSame(302, $client->getResponse()->getStatusCode());
617
618 $crawler = $client->followRedirect();
619
620 $this->assertContains('flashes.config.notice.ignore_origin_rules_updated', $crawler->filter('body')->extract(['_text'])[0]);
621
622 $editLink = $crawler->filter('div[id=set6] a.mode_edit')->last()->link();
623
624 $crawler = $client->click($editLink);
625 $this->assertSame(302, $client->getResponse()->getStatusCode());
626 $this->assertContains('?ignore-origin-user-rule=', $client->getResponse()->headers->get('location'));
627
628 $crawler = $client->followRedirect();
629
630 $form = $crawler->filter('button[id=ignore_origin_user_rule_save]')->form();
631
632 $data = [
633 'ignore_origin_user_rule[rule]' => 'host = "example.org"',
634 ];
635
636 $client->submit($form, $data);
637
638 $this->assertSame(302, $client->getResponse()->getStatusCode());
639
640 $crawler = $client->followRedirect();
641
642 $this->assertContains('flashes.config.notice.ignore_origin_rules_updated', $crawler->filter('body')->extract(['_text'])[0]);
643
644 $this->assertContains('host = "example.org"', $crawler->filter('body')->extract(['_text'])[0]);
645
646 $deleteLink = $crawler->filter('div[id=set6] a.delete')->last()->link();
647
648 $crawler = $client->click($deleteLink);
649 $this->assertSame(302, $client->getResponse()->getStatusCode());
650
651 $crawler = $client->followRedirect();
652 $this->assertContains('flashes.config.notice.ignore_origin_rules_deleted', $crawler->filter('body')->extract(['_text'])[0]);
653 }
654
655 public function dataForIgnoreOriginRuleCreationFail()
656 {
657 return [
658 [
659 [
660 'ignore_origin_user_rule[rule]' => 'foo = "bar"',
661 ],
662 [
663 'The variable',
664 'does not exist.',
665 ],
666 ],
667 [
668 [
669 'ignore_origin_user_rule[rule]' => '_all != "none"',
670 ],
671 [
672 'The operator',
673 'does not exist.',
674 ],
675 ],
676 ];
677 }
678
679 /**
680 * @dataProvider dataForIgnoreOriginRuleCreationFail
681 */
682 public function testIgnoreOriginRuleCreationFail($data, $messages)
683 {
684 $this->logInAs('admin');
685 $client = $this->getClient();
686
687 $crawler = $client->request('GET', '/config');
688
689 $this->assertSame(200, $client->getResponse()->getStatusCode());
690
691 $form = $crawler->filter('button[id=ignore_origin_user_rule_save]')->form();
692
693 $crawler = $client->submit($form, $data);
694
695 $this->assertSame(200, $client->getResponse()->getStatusCode());
696
697 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
698
699 foreach ($messages as $message) {
700 $this->assertContains($message, $body[0]);
701 }
702 }
703
704 public function testDeletingIgnoreOriginRuleFromAnOtherUser()
705 {
706 $this->logInAs('bob');
707 $client = $this->getClient();
708
709 $rule = $client->getContainer()->get('doctrine.orm.entity_manager')
710 ->getRepository('WallabagCoreBundle:IgnoreOriginUserRule')
711 ->findAll()[0];
712
713 $crawler = $client->request('GET', '/ignore-origin-user-rule/edit/' . $rule->getId());
714
715 $this->assertSame(403, $client->getResponse()->getStatusCode());
716 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
717 $this->assertContains('You can not access this rule', $body[0]);
718 }
719
720 public function testEditingIgnoreOriginRuleFromAnOtherUser()
721 {
722 $this->logInAs('bob');
723 $client = $this->getClient();
724
725 $rule = $client->getContainer()->get('doctrine.orm.entity_manager')
726 ->getRepository('WallabagCoreBundle:IgnoreOriginUserRule')
727 ->findAll()[0];
728
729 $crawler = $client->request('GET', '/ignore-origin-user-rule/edit/' . $rule->getId());
730
731 $this->assertSame(403, $client->getResponse()->getStatusCode());
732 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
733 $this->assertContains('You can not access this rule', $body[0]);
734 }
735
736 public function testDemoMode()
737 {
738 $this->logInAs('admin');
739 $client = $this->getClient();
740
741 $config = $client->getContainer()->get('craue_config');
742 $config->set('demo_mode_enabled', 1);
743 $config->set('demo_mode_username', 'admin');
744
745 $crawler = $client->request('GET', '/config');
746
747 $this->assertSame(200, $client->getResponse()->getStatusCode());
748
749 $form = $crawler->filter('button[id=change_passwd_save]')->form();
750
751 $data = [
752 'change_passwd[old_password]' => 'mypassword',
753 'change_passwd[new_password][first]' => 'mypassword',
754 'change_passwd[new_password][second]' => 'mypassword',
755 ];
756
757 $client->submit($form, $data);
758
759 $this->assertSame(302, $client->getResponse()->getStatusCode());
760 $this->assertContains('flashes.config.notice.password_not_updated_demo', $client->getContainer()->get('session')->getFlashBag()->get('notice')[0]);
761
762 $config->set('demo_mode_enabled', 0);
763 $config->set('demo_mode_username', 'wallabag');
764 }
765
766 public function testDeleteUserButtonVisibility()
767 {
768 $this->logInAs('admin');
769 $client = $this->getClient();
770
771 $crawler = $client->request('GET', '/config');
772
773 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
774 $this->assertContains('config.form_user.delete.button', $body[0]);
775
776 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
777
778 $user = $em
779 ->getRepository('WallabagUserBundle:User')
780 ->findOneByUsername('empty');
781 $user->setEnabled(false);
782 $em->persist($user);
783
784 $user = $em
785 ->getRepository('WallabagUserBundle:User')
786 ->findOneByUsername('bob');
787 $user->setEnabled(false);
788 $em->persist($user);
789
790 $em->flush();
791
792 $crawler = $client->request('GET', '/config');
793
794 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
795 $this->assertNotContains('config.form_user.delete.button', $body[0]);
796
797 $client->request('GET', '/account/delete');
798 $this->assertSame(403, $client->getResponse()->getStatusCode());
799
800 $user = $em
801 ->getRepository('WallabagUserBundle:User')
802 ->findOneByUsername('empty');
803 $user->setEnabled(true);
804 $em->persist($user);
805
806 $user = $em
807 ->getRepository('WallabagUserBundle:User')
808 ->findOneByUsername('bob');
809 $user->setEnabled(true);
810 $em->persist($user);
811
812 $em->flush();
813 }
814
815 public function testDeleteAccount()
816 {
817 $client = $this->getClient();
818 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
819
820 $user = new User();
821 $user->setName('Wallace');
822 $user->setEmail('wallace@wallabag.org');
823 $user->setUsername('wallace');
824 $user->setPlainPassword('wallace');
825 $user->setEnabled(true);
826 $user->addRole('ROLE_SUPER_ADMIN');
827
828 $em->persist($user);
829
830 $config = new Config($user);
831
832 $config->setTheme('material');
833 $config->setItemsPerPage(30);
834 $config->setReadingSpeed(200);
835 $config->setLanguage('en');
836 $config->setPocketConsumerKey('xxxxx');
837
838 $em->persist($config);
839 $em->flush();
840
841 $this->logInAs('wallace');
842 $loggedInUserId = $this->getLoggedInUserId();
843
844 // create entry to check after user deletion
845 // that this entry is also deleted
846 $crawler = $client->request('GET', '/new');
847
848 $this->assertSame(200, $client->getResponse()->getStatusCode());
849
850 $form = $crawler->filter('form[name=entry]')->form();
851 $data = [
852 'entry[url]' => $url = 'https://github.com/wallabag/wallabag',
853 ];
854
855 $client->submit($form, $data);
856 $this->assertSame(302, $client->getResponse()->getStatusCode());
857
858 $crawler = $client->request('GET', '/config');
859
860 $deleteLink = $crawler->filter('.delete-account')->last()->link();
861
862 $client->click($deleteLink);
863 $this->assertSame(302, $client->getResponse()->getStatusCode());
864
865 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
866 $user = $em
867 ->getRepository('WallabagUserBundle:User')
868 ->createQueryBuilder('u')
869 ->where('u.username = :username')->setParameter('username', 'wallace')
870 ->getQuery()
871 ->getOneOrNullResult()
872 ;
873
874 $this->assertNull($user);
875
876 $entries = $client->getContainer()
877 ->get('doctrine.orm.entity_manager')
878 ->getRepository('WallabagCoreBundle:Entry')
879 ->findByUser($loggedInUserId);
880
881 $this->assertEmpty($entries);
882 }
883
884 public function testReset()
885 {
886 $this->logInAs('empty');
887 $client = $this->getClient();
888
889 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
890
891 $user = static::$kernel->getContainer()->get('security.token_storage')->getToken()->getUser();
892
893 $tag = new Tag();
894 $tag->setLabel('super');
895 $em->persist($tag);
896
897 $entry = new Entry($user);
898 $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');
899 $entry->setContent('Youhou');
900 $entry->setTitle('Youhou');
901 $entry->addTag($tag);
902 $em->persist($entry);
903
904 $entry2 = new Entry($user);
905 $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');
906 $entry2->setContent('Youhou');
907 $entry2->setTitle('Youhou');
908 $entry2->addTag($tag);
909 $em->persist($entry2);
910
911 $annotation = new Annotation($user);
912 $annotation->setText('annotated');
913 $annotation->setQuote('annotated');
914 $annotation->setRanges([]);
915 $annotation->setEntry($entry);
916 $em->persist($annotation);
917
918 $em->flush();
919
920 // reset annotations
921 $crawler = $client->request('GET', '/config#set3');
922
923 $this->assertSame(200, $client->getResponse()->getStatusCode());
924
925 $crawler = $client->click($crawler->selectLink('config.reset.annotations')->link());
926
927 $this->assertSame(302, $client->getResponse()->getStatusCode());
928 $this->assertContains('flashes.config.notice.annotations_reset', $client->getContainer()->get('session')->getFlashBag()->get('notice')[0]);
929
930 $annotationsReset = $em
931 ->getRepository('WallabagAnnotationBundle:Annotation')
932 ->findAnnotationsByPageId($entry->getId(), $user->getId());
933
934 $this->assertEmpty($annotationsReset, 'Annotations were reset');
935
936 // reset tags
937 $crawler = $client->request('GET', '/config#set3');
938
939 $this->assertSame(200, $client->getResponse()->getStatusCode());
940
941 $crawler = $client->click($crawler->selectLink('config.reset.tags')->link());
942
943 $this->assertSame(302, $client->getResponse()->getStatusCode());
944 $this->assertContains('flashes.config.notice.tags_reset', $client->getContainer()->get('session')->getFlashBag()->get('notice')[0]);
945
946 $tagReset = $em
947 ->getRepository('WallabagCoreBundle:Tag')
948 ->countAllTags($user->getId());
949
950 $this->assertSame(0, $tagReset, 'Tags were reset');
951
952 // reset entries
953 $crawler = $client->request('GET', '/config#set3');
954
955 $this->assertSame(200, $client->getResponse()->getStatusCode());
956
957 $crawler = $client->click($crawler->selectLink('config.reset.entries')->link());
958
959 $this->assertSame(302, $client->getResponse()->getStatusCode());
960 $this->assertContains('flashes.config.notice.entries_reset', $client->getContainer()->get('session')->getFlashBag()->get('notice')[0]);
961
962 $entryReset = $em
963 ->getRepository('WallabagCoreBundle:Entry')
964 ->countAllEntriesByUser($user->getId());
965
966 $this->assertSame(0, $entryReset, 'Entries were reset');
967 }
968
969 public function testResetArchivedEntries()
970 {
971 $this->logInAs('empty');
972 $client = $this->getClient();
973
974 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
975
976 $user = static::$kernel->getContainer()->get('security.token_storage')->getToken()->getUser();
977
978 $tag = new Tag();
979 $tag->setLabel('super');
980 $em->persist($tag);
981
982 $entry = new Entry($user);
983 $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');
984 $entry->setContent('Youhou');
985 $entry->setTitle('Youhou');
986 $entry->addTag($tag);
987 $em->persist($entry);
988
989 $annotation = new Annotation($user);
990 $annotation->setText('annotated');
991 $annotation->setQuote('annotated');
992 $annotation->setRanges([]);
993 $annotation->setEntry($entry);
994 $em->persist($annotation);
995
996 $tagArchived = new Tag();
997 $tagArchived->setLabel('super');
998 $em->persist($tagArchived);
999
1000 $entryArchived = new Entry($user);
1001 $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');
1002 $entryArchived->setContent('Youhou');
1003 $entryArchived->setTitle('Youhou');
1004 $entryArchived->addTag($tagArchived);
1005 $entryArchived->updateArchived(true);
1006 $em->persist($entryArchived);
1007
1008 $annotationArchived = new Annotation($user);
1009 $annotationArchived->setText('annotated');
1010 $annotationArchived->setQuote('annotated');
1011 $annotationArchived->setRanges([]);
1012 $annotationArchived->setEntry($entryArchived);
1013 $em->persist($annotationArchived);
1014
1015 $em->flush();
1016
1017 $crawler = $client->request('GET', '/config#set3');
1018
1019 $this->assertSame(200, $client->getResponse()->getStatusCode());
1020
1021 $crawler = $client->click($crawler->selectLink('config.reset.archived')->link());
1022
1023 $this->assertSame(302, $client->getResponse()->getStatusCode());
1024 $this->assertContains('flashes.config.notice.archived_reset', $client->getContainer()->get('session')->getFlashBag()->get('notice')[0]);
1025
1026 $entryReset = $em
1027 ->getRepository('WallabagCoreBundle:Entry')
1028 ->countAllEntriesByUser($user->getId());
1029
1030 $this->assertSame(1, $entryReset, 'Entries were reset');
1031
1032 $tagReset = $em
1033 ->getRepository('WallabagCoreBundle:Tag')
1034 ->countAllTags($user->getId());
1035
1036 $this->assertSame(1, $tagReset, 'Tags were reset');
1037
1038 $annotationsReset = $em
1039 ->getRepository('WallabagAnnotationBundle:Annotation')
1040 ->findAnnotationsByPageId($annotationArchived->getId(), $user->getId());
1041
1042 $this->assertEmpty($annotationsReset, 'Annotations were reset');
1043 }
1044
1045 public function testResetEntriesCascade()
1046 {
1047 $this->logInAs('empty');
1048 $client = $this->getClient();
1049
1050 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
1051
1052 $user = static::$kernel->getContainer()->get('security.token_storage')->getToken()->getUser();
1053
1054 $tag = new Tag();
1055 $tag->setLabel('super');
1056 $em->persist($tag);
1057
1058 $entry = new Entry($user);
1059 $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');
1060 $entry->setContent('Youhou');
1061 $entry->setTitle('Youhou');
1062 $entry->addTag($tag);
1063 $em->persist($entry);
1064
1065 $annotation = new Annotation($user);
1066 $annotation->setText('annotated');
1067 $annotation->setQuote('annotated');
1068 $annotation->setRanges([]);
1069 $annotation->setEntry($entry);
1070 $em->persist($annotation);
1071
1072 $em->flush();
1073
1074 $crawler = $client->request('GET', '/config#set3');
1075
1076 $this->assertSame(200, $client->getResponse()->getStatusCode());
1077
1078 $crawler = $client->click($crawler->selectLink('config.reset.entries')->link());
1079
1080 $this->assertSame(302, $client->getResponse()->getStatusCode());
1081 $this->assertContains('flashes.config.notice.entries_reset', $client->getContainer()->get('session')->getFlashBag()->get('notice')[0]);
1082
1083 $entryReset = $em
1084 ->getRepository('WallabagCoreBundle:Entry')
1085 ->countAllEntriesByUser($user->getId());
1086
1087 $this->assertSame(0, $entryReset, 'Entries were reset');
1088
1089 $tagReset = $em
1090 ->getRepository('WallabagCoreBundle:Tag')
1091 ->countAllTags($user->getId());
1092
1093 $this->assertSame(0, $tagReset, 'Tags were reset');
1094
1095 $annotationsReset = $em
1096 ->getRepository('WallabagAnnotationBundle:Annotation')
1097 ->findAnnotationsByPageId($entry->getId(), $user->getId());
1098
1099 $this->assertEmpty($annotationsReset, 'Annotations were reset');
1100 }
1101
1102 public function testSwitchViewMode()
1103 {
1104 $this->logInAs('admin');
1105 $this->useTheme('baggy');
1106 $client = $this->getClient();
1107
1108 $client->request('GET', '/unread/list');
1109
1110 $this->assertNotContains('listmode', $client->getResponse()->getContent());
1111
1112 $client->request('GET', '/config/view-mode');
1113 $crawler = $client->followRedirect();
1114
1115 $client->request('GET', '/unread/list');
1116
1117 $this->assertContains('listmode', $client->getResponse()->getContent());
1118
1119 $client->request('GET', '/config/view-mode');
1120 }
1121
1122 public function testChangeLocaleWithoutReferer()
1123 {
1124 $client = $this->getClient();
1125
1126 $client->request('GET', '/locale/de');
1127 $client->followRedirect();
1128
1129 $this->assertSame('de', $client->getRequest()->getLocale());
1130 $this->assertSame('de', $client->getContainer()->get('session')->get('_locale'));
1131 }
1132
1133 public function testChangeLocaleWithReferer()
1134 {
1135 $client = $this->getClient();
1136
1137 $client->request('GET', '/login');
1138 $client->request('GET', '/locale/de');
1139 $client->followRedirect();
1140
1141 $this->assertSame('de', $client->getRequest()->getLocale());
1142 $this->assertSame('de', $client->getContainer()->get('session')->get('_locale'));
1143 }
1144
1145 public function testChangeLocaleToBadLocale()
1146 {
1147 $client = $this->getClient();
1148
1149 $client->request('GET', '/login');
1150 $client->request('GET', '/locale/yuyuyuyu');
1151 $client->followRedirect();
1152
1153 $this->assertNotSame('yuyuyuyu', $client->getRequest()->getLocale());
1154 $this->assertNotSame('yuyuyuyu', $client->getContainer()->get('session')->get('_locale'));
1155 }
1156
1157 public function testUserEnable2faEmail()
1158 {
1159 $this->logInAs('admin');
1160 $client = $this->getClient();
1161
1162 $crawler = $client->request('GET', '/config/otp/email');
1163
1164 $this->assertSame(302, $client->getResponse()->getStatusCode());
1165
1166 $crawler = $client->followRedirect();
1167
1168 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
1169 $this->assertContains('flashes.config.notice.otp_enabled', $alert[0]);
1170
1171 // restore user
1172 $em = $this->getEntityManager();
1173 $user = $em
1174 ->getRepository('WallabagUserBundle:User')
1175 ->findOneByUsername('admin');
1176
1177 $this->assertTrue($user->isEmailTwoFactor());
1178
1179 $user->setEmailTwoFactor(false);
1180 $em->persist($user);
1181 $em->flush();
1182 }
1183
1184 public function testUserDisable2faEmail()
1185 {
1186 $this->logInAs('admin');
1187 $client = $this->getClient();
1188
1189 $crawler = $client->request('GET', '/config/otp/email/disable');
1190
1191 $this->assertSame(302, $client->getResponse()->getStatusCode());
1192
1193 $crawler = $client->followRedirect();
1194
1195 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
1196 $this->assertContains('flashes.config.notice.otp_disabled', $alert[0]);
1197
1198 // restore user
1199 $em = $this->getEntityManager();
1200 $user = $em
1201 ->getRepository('WallabagUserBundle:User')
1202 ->findOneByUsername('admin');
1203
1204 $this->assertFalse($user->isEmailTwoFactor());
1205 }
1206
1207 public function testUserEnable2faGoogle()
1208 {
1209 $this->logInAs('admin');
1210 $client = $this->getClient();
1211
1212 $crawler = $client->request('GET', '/config/otp/app');
1213
1214 $this->assertSame(200, $client->getResponse()->getStatusCode());
1215
1216 // restore user
1217 $em = $this->getEntityManager();
1218 $user = $em
1219 ->getRepository('WallabagUserBundle:User')
1220 ->findOneByUsername('admin');
1221
1222 $this->assertTrue($user->isGoogleTwoFactor());
1223 $this->assertGreaterThan(0, $user->getBackupCodes());
1224
1225 $user->setGoogleAuthenticatorSecret(false);
1226 $user->setBackupCodes(null);
1227 $em->persist($user);
1228 $em->flush();
1229 }
1230
1231 public function testUserEnable2faGoogleCancel()
1232 {
1233 $this->logInAs('admin');
1234 $client = $this->getClient();
1235
1236 $crawler = $client->request('GET', '/config/otp/app');
1237
1238 $this->assertSame(200, $client->getResponse()->getStatusCode());
1239
1240 // restore user
1241 $em = $this->getEntityManager();
1242 $user = $em
1243 ->getRepository('WallabagUserBundle:User')
1244 ->findOneByUsername('admin');
1245
1246 $this->assertTrue($user->isGoogleTwoFactor());
1247 $this->assertGreaterThan(0, $user->getBackupCodes());
1248
1249 $crawler = $client->request('GET', '/config/otp/app/cancel');
1250
1251 $this->assertSame(302, $client->getResponse()->getStatusCode());
1252
1253 $user = $em
1254 ->getRepository('WallabagUserBundle:User')
1255 ->findOneByUsername('admin');
1256
1257 $this->assertFalse($user->isGoogleTwoFactor());
1258 $this->assertEmpty($user->getBackupCodes());
1259 }
1260
1261 public function testUserDisable2faGoogle()
1262 {
1263 $this->logInAs('admin');
1264 $client = $this->getClient();
1265
1266 $crawler = $client->request('GET', '/config/otp/app/disable');
1267
1268 $this->assertSame(302, $client->getResponse()->getStatusCode());
1269
1270 $crawler = $client->followRedirect();
1271
1272 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
1273 $this->assertContains('flashes.config.notice.otp_disabled', $alert[0]);
1274
1275 // restore user
1276 $em = $this->getEntityManager();
1277 $user = $em
1278 ->getRepository('WallabagUserBundle:User')
1279 ->findOneByUsername('admin');
1280
1281 $this->assertEmpty($user->getGoogleAuthenticatorSecret());
1282 $this->assertEmpty($user->getBackupCodes());
1283 }
1284
1285 public function testExportTaggingRule()
1286 {
1287 $this->logInAs('admin');
1288 $client = $this->getClient();
1289
1290 ob_start();
1291 $crawler = $client->request('GET', '/tagging-rule/export');
1292 ob_end_clean();
1293
1294 $this->assertSame(200, $client->getResponse()->getStatusCode());
1295
1296 $headers = $client->getResponse()->headers;
1297 $this->assertSame('application/json', $headers->get('content-type'));
1298 $this->assertSame('attachment; filename="tagging_rules_admin.json"', $headers->get('content-disposition'));
1299 $this->assertSame('UTF-8', $headers->get('content-transfer-encoding'));
1300
1301 $content = json_decode($client->getResponse()->getContent(), true);
1302
1303 $this->assertCount(4, $content);
1304 $this->assertSame('content matches "spurs"', $content[0]['rule']);
1305 $this->assertSame('sport', $content[0]['tags'][0]);
1306 }
1307
1308 public function testImportTagginfRuleBadFile()
1309 {
1310 $this->logInAs('admin');
1311 $client = $this->getClient();
1312
1313 $crawler = $client->request('GET', '/config');
1314 $form = $crawler->filter('form[name=upload_tagging_rule_file] > button[type=submit]')->form();
1315
1316 $data = [
1317 'upload_tagging_rule_file[file]' => '',
1318 ];
1319
1320 $client->submit($form, $data);
1321
1322 $this->assertSame(302, $client->getResponse()->getStatusCode());
1323 }
1324
1325 public function testImportTagginfRuleFile()
1326 {
1327 $this->logInAs('admin');
1328 $client = $this->getClient();
1329
1330 $crawler = $client->request('GET', '/config');
1331 $form = $crawler->filter('form[name=upload_tagging_rule_file] > button[type=submit]')->form();
1332
1333 $file = new UploadedFile(__DIR__ . '/../fixtures/tagging_rules_admin.json', 'tagging_rules_admin.json');
1334
1335 $data = [
1336 'upload_tagging_rule_file[file]' => $file,
1337 ];
1338
1339 $client->submit($form, $data);
1340 $this->assertSame(302, $client->getResponse()->getStatusCode());
1341
1342 $user = $client->getContainer()->get('fos_user.user_manager.test')->findUserBy(['username' => 'admin']);
1343 $taggingRules = $user->getConfig()->getTaggingRules()->toArray();
1344 $this->assertCount(5, $taggingRules);
1345 $this->assertSame('title matches "football"', $taggingRules[4]->getRule());
1346 }
1347 }