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