]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php
Removed old, not so maintained and buggy baggy theme
[github/wallabag/wallabag.git] / tests / Wallabag / CoreBundle / Controller / ConfigControllerTest.php
CommitLineData
4d85d7e9
J
1<?php
2
1e0d8ad7 3namespace Tests\Wallabag\CoreBundle\Controller;
4d85d7e9 4
34be2d5d 5use Symfony\Component\HttpFoundation\File\UploadedFile;
23634d5d 6use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
f808b016 7use Wallabag\AnnotationBundle\Entity\Annotation;
c3396c65 8use Wallabag\CoreBundle\Entity\Config;
206bade5
JB
9use Wallabag\CoreBundle\Entity\Entry;
10use Wallabag\CoreBundle\Entity\Tag;
f808b016 11use Wallabag\UserBundle\Entity\User;
4d85d7e9 12
769e19dc 13class ConfigControllerTest extends WallabagCoreTestCase
4d85d7e9
J
14{
15 public function testLogin()
16 {
17 $client = $this->getClient();
18
19 $client->request('GET', '/new');
20
f808b016 21 $this->assertSame(302, $client->getResponse()->getStatusCode());
4d85d7e9
J
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
f808b016 32 $this->assertSame(200, $client->getResponse()->getStatusCode());
4d85d7e9 33
d9085c63
J
34 $this->assertCount(1, $crawler->filter('button[id=config_save]'));
35 $this->assertCount(1, $crawler->filter('button[id=change_passwd_save]'));
c844dc0c 36 $this->assertCount(1, $crawler->filter('button[id=update_user_save]'));
531c8d0a 37 $this->assertCount(1, $crawler->filter('button[id=feed_config_save]'));
4d85d7e9
J
38 }
39
40 public function testUpdate()
41 {
42 $this->logInAs('admin');
43 $client = $this->getClient();
44
45 $crawler = $client->request('GET', '/config');
46
f808b016 47 $this->assertSame(200, $client->getResponse()->getStatusCode());
4d85d7e9 48
d9085c63 49 $form = $crawler->filter('button[id=config_save]')->form();
4d85d7e9 50
4094ea47 51 $data = [
39676caf 52 'config[theme]' => 'material',
4d85d7e9 53 'config[items_per_page]' => '30',
41022cb2 54 'config[reading_speed]' => '100',
a42f38d9 55 'config[action_mark_as_read]' => '0',
c89d35e8 56 'config[language]' => 'en',
4094ea47 57 ];
4d85d7e9
J
58
59 $client->submit($form, $data);
60
f808b016 61 $this->assertSame(302, $client->getResponse()->getStatusCode());
4d85d7e9
J
62
63 $crawler = $client->followRedirect();
64
bf3dc999 65 $this->assertContains('flashes.config.notice.config_saved', $crawler->filter('body')->extract(['_text'])[0]);
4d85d7e9
J
66 }
67
c4c062cc
NL
68 public function testChangeReadingSpeed()
69 {
70 $this->logInAs('admin');
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 = [
41022cb2 94 'config[reading_speed]' => '400',
c4c062cc
NL
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 = [
41022cb2 108 'config[reading_speed]' => '100',
c4c062cc
NL
109 ];
110 $client->submit($form, $data);
111 }
112
4d85d7e9
J
113 public function dataForUpdateFailed()
114 {
4094ea47
JB
115 return [
116 [[
39676caf 117 'config[theme]' => 'material',
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
531c8d0a 300 public function testFeedUpdateResetToken()
371ac69a
J
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
1210dae1 308 ->getRepository('WallabagUserBundle:User')
371ac69a
J
309 ->findOneByUsername('admin');
310
311 if (!$user) {
312 $this->markTestSkipped('No user found in db.');
313 }
314
315 $config = $user->getConfig();
531c8d0a 316 $config->setFeedToken(null);
371ac69a
J
317 $em->persist($config);
318 $em->flush();
319
320 $crawler = $client->request('GET', '/config');
321
f808b016 322 $this->assertSame(200, $client->getResponse()->getStatusCode());
371ac69a 323
4094ea47 324 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
531c8d0a 325 $this->assertContains('config.form_feed.no_token', $body[0]);
371ac69a
J
326
327 $client->request('GET', '/generate-token');
f808b016 328 $this->assertSame(302, $client->getResponse()->getStatusCode());
371ac69a
J
329
330 $crawler = $client->followRedirect();
331
4094ea47 332 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
c4bf12aa 333 $this->assertContains('config.form_feed.token_reset', $body[0]);
371ac69a
J
334 }
335
336 public function testGenerateTokenAjax()
337 {
338 $this->logInAs('admin');
339 $client = $this->getClient();
340
341 $client->request(
342 'GET',
343 '/generate-token',
4094ea47
JB
344 [],
345 [],
346 ['HTTP_X-Requested-With' => 'XMLHttpRequest']
371ac69a
J
347 );
348
f808b016 349 $this->assertSame(200, $client->getResponse()->getStatusCode());
9744e971 350 $content = json_decode($client->getResponse()->getContent(), true);
371ac69a
J
351 $this->assertArrayHasKey('token', $content);
352 }
353
c4bf12aa
JB
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
531c8d0a 370 public function testFeedUpdate()
371ac69a
J
371 {
372 $this->logInAs('admin');
373 $client = $this->getClient();
374
375 $crawler = $client->request('GET', '/config');
376
f808b016 377 $this->assertSame(200, $client->getResponse()->getStatusCode());
371ac69a 378
531c8d0a 379 $form = $crawler->filter('button[id=feed_config_save]')->form();
371ac69a 380
4094ea47 381 $data = [
531c8d0a 382 'feed_config[feed_limit]' => 12,
4094ea47 383 ];
371ac69a
J
384
385 $client->submit($form, $data);
386
f808b016 387 $this->assertSame(302, $client->getResponse()->getStatusCode());
371ac69a
J
388
389 $crawler = $client->followRedirect();
390
531c8d0a 391 $this->assertContains('flashes.config.notice.feed_updated', $crawler->filter('body')->extract(['_text'])[0]);
371ac69a
J
392 }
393
531c8d0a 394 public function dataForFeedFailed()
371ac69a 395 {
4094ea47
JB
396 return [
397 [
398 [
531c8d0a 399 'feed_config[feed_limit]' => 0,
4094ea47 400 ],
371ac69a 401 'This value should be 1 or more.',
4094ea47
JB
402 ],
403 [
404 [
531c8d0a 405 'feed_config[feed_limit]' => 1000000000000,
4094ea47 406 ],
531c8d0a 407 'validator.feed_limit_too_high',
4094ea47
JB
408 ],
409 ];
371ac69a
J
410 }
411
412 /**
531c8d0a 413 * @dataProvider dataForFeedFailed
371ac69a 414 */
531c8d0a 415 public function testFeedFailed($data, $expectedMessage)
371ac69a
J
416 {
417 $this->logInAs('admin');
418 $client = $this->getClient();
419
420 $crawler = $client->request('GET', '/config');
421
f808b016 422 $this->assertSame(200, $client->getResponse()->getStatusCode());
371ac69a 423
531c8d0a 424 $form = $crawler->filter('button[id=feed_config_save]')->form();
371ac69a
J
425
426 $crawler = $client->submit($form, $data);
427
f808b016 428 $this->assertSame(200, $client->getResponse()->getStatusCode());
371ac69a 429
4094ea47 430 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
371ac69a
J
431 $this->assertContains($expectedMessage, $alert[0]);
432 }
8a99c7a8
KG
433
434 public function testTaggingRuleCreation()
435 {
436 $this->logInAs('admin');
437 $client = $this->getClient();
438
439 $crawler = $client->request('GET', '/config');
440
f808b016 441 $this->assertSame(200, $client->getResponse()->getStatusCode());
8a99c7a8
KG
442
443 $form = $crawler->filter('button[id=tagging_rule_save]')->form();
444
4094ea47 445 $data = [
8a99c7a8
KG
446 'tagging_rule[rule]' => 'readingTime <= 3',
447 'tagging_rule[tags]' => 'short reading',
4094ea47 448 ];
8a99c7a8
KG
449
450 $client->submit($form, $data);
451
f808b016 452 $this->assertSame(302, $client->getResponse()->getStatusCode());
8a99c7a8
KG
453
454 $crawler = $client->followRedirect();
455
bf3dc999
JB
456 $this->assertContains('flashes.config.notice.tagging_rules_updated', $crawler->filter('body')->extract(['_text'])[0]);
457
39676caf 458 $editLink = $crawler->filter('.edit-rule')->last()->link();
bf3dc999
JB
459
460 $crawler = $client->click($editLink);
f808b016 461 $this->assertSame(302, $client->getResponse()->getStatusCode());
bf3dc999
JB
462 $this->assertContains('?tagging-rule=', $client->getResponse()->headers->get('location'));
463
464 $crawler = $client->followRedirect();
465
466 $form = $crawler->filter('button[id=tagging_rule_save]')->form();
467
468 $data = [
469 'tagging_rule[rule]' => 'readingTime <= 30',
470 'tagging_rule[tags]' => 'short reading',
471 ];
472
473 $client->submit($form, $data);
474
f808b016 475 $this->assertSame(302, $client->getResponse()->getStatusCode());
bf3dc999
JB
476
477 $crawler = $client->followRedirect();
478
479 $this->assertContains('flashes.config.notice.tagging_rules_updated', $crawler->filter('body')->extract(['_text'])[0]);
480
481 $this->assertContains('readingTime <= 30', $crawler->filter('body')->extract(['_text'])[0]);
8a99c7a8 482
39676caf 483 $deleteLink = $crawler->filter('.delete-rule')->last()->link();
8a99c7a8
KG
484
485 $crawler = $client->click($deleteLink);
f808b016 486 $this->assertSame(302, $client->getResponse()->getStatusCode());
8a99c7a8
KG
487
488 $crawler = $client->followRedirect();
bf3dc999 489 $this->assertContains('flashes.config.notice.tagging_rules_deleted', $crawler->filter('body')->extract(['_text'])[0]);
8a99c7a8
KG
490 }
491
492 public function dataForTaggingRuleFailed()
493 {
4094ea47
JB
494 return [
495 [
496 [
a3cac44c
JB
497 'tagging_rule[rule]' => 'unknownVar <= 3',
498 'tagging_rule[tags]' => 'cool tag',
4094ea47
JB
499 ],
500 [
a3cac44c
JB
501 'The variable',
502 'does not exist.',
4094ea47
JB
503 ],
504 ],
505 [
506 [
a3cac44c
JB
507 'tagging_rule[rule]' => 'length(domainName) <= 42',
508 'tagging_rule[tags]' => 'cool tag',
4094ea47
JB
509 ],
510 [
a3cac44c
JB
511 'The operator',
512 'does not exist.',
4094ea47
JB
513 ],
514 ],
515 ];
8a99c7a8 516 }
a3cac44c
JB
517
518 /**
519 * @dataProvider dataForTaggingRuleFailed
520 */
521 public function testTaggingRuleCreationFail($data, $messages)
522 {
523 $this->logInAs('admin');
524 $client = $this->getClient();
525
526 $crawler = $client->request('GET', '/config');
527
f808b016 528 $this->assertSame(200, $client->getResponse()->getStatusCode());
a3cac44c
JB
529
530 $form = $crawler->filter('button[id=tagging_rule_save]')->form();
531
4f9cf232 532 $crawler = $client->submit($form, $data);
a3cac44c 533
f808b016 534 $this->assertSame(200, $client->getResponse()->getStatusCode());
a3cac44c 535
4094ea47 536 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
4f9cf232 537
a3cac44c 538 foreach ($messages as $message) {
4f9cf232 539 $this->assertContains($message, $body[0]);
a3cac44c
JB
540 }
541 }
542
5aa0294c
JB
543 public function testTaggingRuleTooLong()
544 {
545 $this->logInAs('admin');
546 $client = $this->getClient();
547
548 $crawler = $client->request('GET', '/config');
549
f808b016 550 $this->assertSame(200, $client->getResponse()->getStatusCode());
5aa0294c
JB
551
552 $form = $crawler->filter('button[id=tagging_rule_save]')->form();
553
554 $crawler = $client->submit($form, [
555 'tagging_rule[rule]' => str_repeat('title', 60),
556 'tagging_rule[tags]' => 'cool tag',
557 ]);
558
f808b016 559 $this->assertSame(200, $client->getResponse()->getStatusCode());
5aa0294c
JB
560
561 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
562
563 $this->assertContains('255 characters', $body[0]);
564 }
565
a3cac44c
JB
566 public function testDeletingTaggingRuleFromAnOtherUser()
567 {
568 $this->logInAs('bob');
569 $client = $this->getClient();
570
571 $rule = $client->getContainer()->get('doctrine.orm.entity_manager')
572 ->getRepository('WallabagCoreBundle:TaggingRule')
573 ->findAll()[0];
574
f808b016 575 $crawler = $client->request('GET', '/tagging-rule/edit/' . $rule->getId());
bf3dc999 576
f808b016 577 $this->assertSame(403, $client->getResponse()->getStatusCode());
bf3dc999
JB
578 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
579 $this->assertContains('You can not access this tagging rule', $body[0]);
580 }
581
582 public function testEditingTaggingRuleFromAnOtherUser()
583 {
584 $this->logInAs('bob');
585 $client = $this->getClient();
586
587 $rule = $client->getContainer()->get('doctrine.orm.entity_manager')
588 ->getRepository('WallabagCoreBundle:TaggingRule')
589 ->findAll()[0];
590
f808b016 591 $crawler = $client->request('GET', '/tagging-rule/edit/' . $rule->getId());
4f9cf232 592
f808b016 593 $this->assertSame(403, $client->getResponse()->getStatusCode());
4094ea47 594 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
4f9cf232 595 $this->assertContains('You can not access this tagging rule', $body[0]);
a3cac44c 596 }
b6c00b0b
JB
597
598 public function testDemoMode()
599 {
600 $this->logInAs('admin');
601 $client = $this->getClient();
602
603 $config = $client->getContainer()->get('craue_config');
604 $config->set('demo_mode_enabled', 1);
605 $config->set('demo_mode_username', 'admin');
606
607 $crawler = $client->request('GET', '/config');
608
f808b016 609 $this->assertSame(200, $client->getResponse()->getStatusCode());
b6c00b0b
JB
610
611 $form = $crawler->filter('button[id=change_passwd_save]')->form();
612
4094ea47 613 $data = [
b6c00b0b
JB
614 'change_passwd[old_password]' => 'mypassword',
615 'change_passwd[new_password][first]' => 'mypassword',
616 'change_passwd[new_password][second]' => 'mypassword',
4094ea47 617 ];
b6c00b0b
JB
618
619 $client->submit($form, $data);
620
f808b016 621 $this->assertSame(302, $client->getResponse()->getStatusCode());
4204a06b 622 $this->assertContains('flashes.config.notice.password_not_updated_demo', $client->getContainer()->get('session')->getFlashBag()->get('notice')[0]);
b6c00b0b
JB
623
624 $config->set('demo_mode_enabled', 0);
625 $config->set('demo_mode_username', 'wallabag');
626 }
821bb876
NL
627
628 public function testDeleteUserButtonVisibility()
629 {
630 $this->logInAs('admin');
631 $client = $this->getClient();
632
633 $crawler = $client->request('GET', '/config');
634
635 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
876d77a6 636 $this->assertContains('config.form_user.delete.button', $body[0]);
821bb876
NL
637
638 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
639
821bb876
NL
640 $user = $em
641 ->getRepository('WallabagUserBundle:User')
642 ->findOneByUsername('empty');
5066c3e0 643 $user->setEnabled(false);
821bb876
NL
644 $em->persist($user);
645
646 $user = $em
647 ->getRepository('WallabagUserBundle:User')
648 ->findOneByUsername('bob');
5066c3e0 649 $user->setEnabled(false);
821bb876
NL
650 $em->persist($user);
651
652 $em->flush();
653
654 $crawler = $client->request('GET', '/config');
655
656 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
876d77a6 657 $this->assertNotContains('config.form_user.delete.button', $body[0]);
821bb876
NL
658
659 $client->request('GET', '/account/delete');
f808b016 660 $this->assertSame(403, $client->getResponse()->getStatusCode());
821bb876 661
821bb876
NL
662 $user = $em
663 ->getRepository('WallabagUserBundle:User')
664 ->findOneByUsername('empty');
5066c3e0 665 $user->setEnabled(true);
821bb876
NL
666 $em->persist($user);
667
668 $user = $em
669 ->getRepository('WallabagUserBundle:User')
670 ->findOneByUsername('bob');
5066c3e0 671 $user->setEnabled(true);
821bb876
NL
672 $em->persist($user);
673
674 $em->flush();
675 }
676
677 public function testDeleteAccount()
678 {
821bb876 679 $client = $this->getClient();
c3396c65
JB
680 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
681
682 $user = new User();
683 $user->setName('Wallace');
684 $user->setEmail('wallace@wallabag.org');
685 $user->setUsername('wallace');
686 $user->setPlainPassword('wallace');
687 $user->setEnabled(true);
688 $user->addRole('ROLE_SUPER_ADMIN');
689
690 $em->persist($user);
691
692 $config = new Config($user);
693
694 $config->setTheme('material');
695 $config->setItemsPerPage(30);
af7b22a3 696 $config->setReadingSpeed(200);
c3396c65
JB
697 $config->setLanguage('en');
698 $config->setPocketConsumerKey('xxxxx');
699
700 $em->persist($config);
701 $em->flush();
702
703 $this->logInAs('wallace');
876d77a6 704 $loggedInUserId = $this->getLoggedInUserId();
821bb876 705
b8402687
NL
706 // create entry to check after user deletion
707 // that this entry is also deleted
708 $crawler = $client->request('GET', '/new');
709
f808b016 710 $this->assertSame(200, $client->getResponse()->getStatusCode());
b8402687
NL
711
712 $form = $crawler->filter('form[name=entry]')->form();
713 $data = [
714 'entry[url]' => $url = 'https://github.com/wallabag/wallabag',
715 ];
716
717 $client->submit($form, $data);
f808b016 718 $this->assertSame(302, $client->getResponse()->getStatusCode());
b8402687 719
821bb876
NL
720 $crawler = $client->request('GET', '/config');
721
71254701 722 $deleteLink = $crawler->filter('.delete-account')->last()->link();
821bb876
NL
723
724 $client->click($deleteLink);
f808b016 725 $this->assertSame(302, $client->getResponse()->getStatusCode());
821bb876
NL
726
727 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
728 $user = $em
729 ->getRepository('WallabagUserBundle:User')
730 ->createQueryBuilder('u')
731 ->where('u.username = :username')->setParameter('username', 'wallace')
732 ->getQuery()
733 ->getOneOrNullResult()
734 ;
735
71254701 736 $this->assertNull($user);
b8402687
NL
737
738 $entries = $client->getContainer()
739 ->get('doctrine.orm.entity_manager')
740 ->getRepository('WallabagCoreBundle:Entry')
876d77a6 741 ->findByUser($loggedInUserId);
b8402687
NL
742
743 $this->assertEmpty($entries);
821bb876 744 }
206bade5
JB
745
746 public function testReset()
747 {
748 $this->logInAs('empty');
749 $client = $this->getClient();
750
751 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
752
753 $user = static::$kernel->getContainer()->get('security.token_storage')->getToken()->getUser();
754
755 $tag = new Tag();
756 $tag->setLabel('super');
757 $em->persist($tag);
758
759 $entry = new Entry($user);
77854331 760 $entry->setUrl('https://www.lemonde.fr/europe/article/2016/10/01/pour-le-psoe-chaque-election-s-est-transformee-en-une-agonie_5006476_3214.html');
206bade5
JB
761 $entry->setContent('Youhou');
762 $entry->setTitle('Youhou');
763 $entry->addTag($tag);
764 $em->persist($entry);
765
766 $entry2 = new Entry($user);
767 $entry2->setUrl('http://www.lemonde.de/europe/article/2016/10/01/pour-le-psoe-chaque-election-s-est-transformee-en-une-agonie_5006476_3214.html');
768 $entry2->setContent('Youhou');
769 $entry2->setTitle('Youhou');
770 $entry2->addTag($tag);
771 $em->persist($entry2);
772
773 $annotation = new Annotation($user);
774 $annotation->setText('annotated');
775 $annotation->setQuote('annotated');
776 $annotation->setRanges([]);
777 $annotation->setEntry($entry);
778 $em->persist($annotation);
779
780 $em->flush();
781
782 // reset annotations
783 $crawler = $client->request('GET', '/config#set3');
784
f808b016 785 $this->assertSame(200, $client->getResponse()->getStatusCode());
206bade5
JB
786
787 $crawler = $client->click($crawler->selectLink('config.reset.annotations')->link());
788
f808b016 789 $this->assertSame(302, $client->getResponse()->getStatusCode());
206bade5
JB
790 $this->assertContains('flashes.config.notice.annotations_reset', $client->getContainer()->get('session')->getFlashBag()->get('notice')[0]);
791
792 $annotationsReset = $em
793 ->getRepository('WallabagAnnotationBundle:Annotation')
794 ->findAnnotationsByPageId($entry->getId(), $user->getId());
795
796 $this->assertEmpty($annotationsReset, 'Annotations were reset');
797
798 // reset tags
799 $crawler = $client->request('GET', '/config#set3');
800
f808b016 801 $this->assertSame(200, $client->getResponse()->getStatusCode());
206bade5
JB
802
803 $crawler = $client->click($crawler->selectLink('config.reset.tags')->link());
804
f808b016 805 $this->assertSame(302, $client->getResponse()->getStatusCode());
206bade5
JB
806 $this->assertContains('flashes.config.notice.tags_reset', $client->getContainer()->get('session')->getFlashBag()->get('notice')[0]);
807
808 $tagReset = $em
809 ->getRepository('WallabagCoreBundle:Tag')
810 ->countAllTags($user->getId());
811
f808b016 812 $this->assertSame(0, $tagReset, 'Tags were reset');
206bade5
JB
813
814 // reset entries
815 $crawler = $client->request('GET', '/config#set3');
816
f808b016 817 $this->assertSame(200, $client->getResponse()->getStatusCode());
206bade5
JB
818
819 $crawler = $client->click($crawler->selectLink('config.reset.entries')->link());
820
f808b016 821 $this->assertSame(302, $client->getResponse()->getStatusCode());
206bade5
JB
822 $this->assertContains('flashes.config.notice.entries_reset', $client->getContainer()->get('session')->getFlashBag()->get('notice')[0]);
823
824 $entryReset = $em
825 ->getRepository('WallabagCoreBundle:Entry')
273b6f06 826 ->countAllEntriesByUser($user->getId());
206bade5 827
f808b016 828 $this->assertSame(0, $entryReset, 'Entries were reset');
206bade5
JB
829 }
830
6da1aebc
TC
831 public function testResetArchivedEntries()
832 {
833 $this->logInAs('empty');
834 $client = $this->getClient();
835
836 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
837
838 $user = static::$kernel->getContainer()->get('security.token_storage')->getToken()->getUser();
839
840 $tag = new Tag();
841 $tag->setLabel('super');
842 $em->persist($tag);
843
844 $entry = new Entry($user);
77854331 845 $entry->setUrl('https://www.lemonde.fr/europe/article/2016/10/01/pour-le-psoe-chaque-election-s-est-transformee-en-une-agonie_5006476_3214.html');
6da1aebc
TC
846 $entry->setContent('Youhou');
847 $entry->setTitle('Youhou');
848 $entry->addTag($tag);
849 $em->persist($entry);
850
851 $annotation = new Annotation($user);
852 $annotation->setText('annotated');
853 $annotation->setQuote('annotated');
854 $annotation->setRanges([]);
855 $annotation->setEntry($entry);
856 $em->persist($annotation);
857
858 $tagArchived = new Tag();
859 $tagArchived->setLabel('super');
860 $em->persist($tagArchived);
861
862 $entryArchived = new Entry($user);
77854331 863 $entryArchived->setUrl('https://www.lemonde.fr/europe/article/2016/10/01/pour-le-psoe-chaque-election-s-est-transformee-en-une-agonie_5006476_3214.html');
6da1aebc
TC
864 $entryArchived->setContent('Youhou');
865 $entryArchived->setTitle('Youhou');
866 $entryArchived->addTag($tagArchived);
7975395d 867 $entryArchived->updateArchived(true);
6da1aebc
TC
868 $em->persist($entryArchived);
869
870 $annotationArchived = new Annotation($user);
871 $annotationArchived->setText('annotated');
872 $annotationArchived->setQuote('annotated');
873 $annotationArchived->setRanges([]);
874 $annotationArchived->setEntry($entryArchived);
875 $em->persist($annotationArchived);
876
877 $em->flush();
878
879 $crawler = $client->request('GET', '/config#set3');
880
f808b016 881 $this->assertSame(200, $client->getResponse()->getStatusCode());
6da1aebc
TC
882
883 $crawler = $client->click($crawler->selectLink('config.reset.archived')->link());
884
f808b016 885 $this->assertSame(302, $client->getResponse()->getStatusCode());
6da1aebc
TC
886 $this->assertContains('flashes.config.notice.archived_reset', $client->getContainer()->get('session')->getFlashBag()->get('notice')[0]);
887
888 $entryReset = $em
889 ->getRepository('WallabagCoreBundle:Entry')
73f28afb 890 ->countAllEntriesByUser($user->getId());
6da1aebc 891
f808b016 892 $this->assertSame(1, $entryReset, 'Entries were reset');
6da1aebc
TC
893
894 $tagReset = $em
895 ->getRepository('WallabagCoreBundle:Tag')
896 ->countAllTags($user->getId());
897
f808b016 898 $this->assertSame(1, $tagReset, 'Tags were reset');
6da1aebc
TC
899
900 $annotationsReset = $em
901 ->getRepository('WallabagAnnotationBundle:Annotation')
902 ->findAnnotationsByPageId($annotationArchived->getId(), $user->getId());
903
904 $this->assertEmpty($annotationsReset, 'Annotations were reset');
905 }
906
206bade5
JB
907 public function testResetEntriesCascade()
908 {
909 $this->logInAs('empty');
910 $client = $this->getClient();
911
912 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
913
914 $user = static::$kernel->getContainer()->get('security.token_storage')->getToken()->getUser();
915
916 $tag = new Tag();
917 $tag->setLabel('super');
918 $em->persist($tag);
919
920 $entry = new Entry($user);
77854331 921 $entry->setUrl('https://www.lemonde.fr/europe/article/2016/10/01/pour-le-psoe-chaque-election-s-est-transformee-en-une-agonie_5006476_3214.html');
206bade5
JB
922 $entry->setContent('Youhou');
923 $entry->setTitle('Youhou');
924 $entry->addTag($tag);
925 $em->persist($entry);
926
927 $annotation = new Annotation($user);
928 $annotation->setText('annotated');
929 $annotation->setQuote('annotated');
930 $annotation->setRanges([]);
931 $annotation->setEntry($entry);
932 $em->persist($annotation);
933
934 $em->flush();
935
936 $crawler = $client->request('GET', '/config#set3');
937
f808b016 938 $this->assertSame(200, $client->getResponse()->getStatusCode());
206bade5
JB
939
940 $crawler = $client->click($crawler->selectLink('config.reset.entries')->link());
941
f808b016 942 $this->assertSame(302, $client->getResponse()->getStatusCode());
206bade5
JB
943 $this->assertContains('flashes.config.notice.entries_reset', $client->getContainer()->get('session')->getFlashBag()->get('notice')[0]);
944
945 $entryReset = $em
946 ->getRepository('WallabagCoreBundle:Entry')
273b6f06 947 ->countAllEntriesByUser($user->getId());
206bade5 948
f808b016 949 $this->assertSame(0, $entryReset, 'Entries were reset');
206bade5
JB
950
951 $tagReset = $em
952 ->getRepository('WallabagCoreBundle:Tag')
953 ->countAllTags($user->getId());
954
f808b016 955 $this->assertSame(0, $tagReset, 'Tags were reset');
206bade5
JB
956
957 $annotationsReset = $em
958 ->getRepository('WallabagAnnotationBundle:Annotation')
959 ->findAnnotationsByPageId($entry->getId(), $user->getId());
960
961 $this->assertEmpty($annotationsReset, 'Annotations were reset');
962 }
8f3ff39c
NL
963
964 public function testSwitchViewMode()
965 {
966 $this->logInAs('admin');
967 $client = $this->getClient();
968
969 $client->request('GET', '/unread/list');
970
39676caf 971 $this->assertNotContains('collection', $client->getResponse()->getContent());
8f3ff39c
NL
972
973 $client->request('GET', '/config/view-mode');
974 $crawler = $client->followRedirect();
975
976 $client->request('GET', '/unread/list');
977
39676caf 978 $this->assertContains('collection', $client->getResponse()->getContent());
56a7ce17
NL
979
980 $client->request('GET', '/config/view-mode');
8f3ff39c 981 }
4d4147b2
JB
982
983 public function testChangeLocaleWithoutReferer()
984 {
985 $client = $this->getClient();
986
987 $client->request('GET', '/locale/de');
988 $client->followRedirect();
989
990 $this->assertSame('de', $client->getRequest()->getLocale());
991 $this->assertSame('de', $client->getContainer()->get('session')->get('_locale'));
992 }
993
994 public function testChangeLocaleWithReferer()
995 {
996 $client = $this->getClient();
997
998 $client->request('GET', '/login');
999 $client->request('GET', '/locale/de');
1000 $client->followRedirect();
1001
1002 $this->assertSame('de', $client->getRequest()->getLocale());
1003 $this->assertSame('de', $client->getContainer()->get('session')->get('_locale'));
1004 }
1005
1006 public function testChangeLocaleToBadLocale()
1007 {
1008 $client = $this->getClient();
1009
1010 $client->request('GET', '/login');
1011 $client->request('GET', '/locale/yuyuyuyu');
1012 $client->followRedirect();
1013
1014 $this->assertNotSame('yuyuyuyu', $client->getRequest()->getLocale());
1015 $this->assertNotSame('yuyuyuyu', $client->getContainer()->get('session')->get('_locale'));
1016 }
a0c5eb00
JB
1017
1018 public function testUserEnable2faEmail()
1019 {
1020 $this->logInAs('admin');
1021 $client = $this->getClient();
1022
1023 $crawler = $client->request('GET', '/config/otp/email');
1024
1025 $this->assertSame(302, $client->getResponse()->getStatusCode());
1026
1027 $crawler = $client->followRedirect();
1028
1029 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
1030 $this->assertContains('flashes.config.notice.otp_enabled', $alert[0]);
1031
1032 // restore user
1033 $em = $this->getEntityManager();
1034 $user = $em
1035 ->getRepository('WallabagUserBundle:User')
1036 ->findOneByUsername('admin');
1037
1038 $this->assertTrue($user->isEmailTwoFactor());
1039
1040 $user->setEmailTwoFactor(false);
1041 $em->persist($user);
1042 $em->flush();
1043 }
1044
1045 public function testUserEnable2faGoogle()
1046 {
1047 $this->logInAs('admin');
1048 $client = $this->getClient();
1049
1050 $crawler = $client->request('GET', '/config/otp/app');
1051
1052 $this->assertSame(200, $client->getResponse()->getStatusCode());
1053
1054 // restore user
1055 $em = $this->getEntityManager();
1056 $user = $em
1057 ->getRepository('WallabagUserBundle:User')
1058 ->findOneByUsername('admin');
1059
1060 $this->assertTrue($user->isGoogleTwoFactor());
1061 $this->assertGreaterThan(0, $user->getBackupCodes());
1062
1063 $user->setGoogleAuthenticatorSecret(false);
1064 $user->setBackupCodes(null);
1065 $em->persist($user);
1066 $em->flush();
1067 }
1068
1069 public function testUserEnable2faGoogleCancel()
1070 {
1071 $this->logInAs('admin');
1072 $client = $this->getClient();
1073
1074 $crawler = $client->request('GET', '/config/otp/app');
1075
1076 $this->assertSame(200, $client->getResponse()->getStatusCode());
1077
1078 // restore user
1079 $em = $this->getEntityManager();
1080 $user = $em
1081 ->getRepository('WallabagUserBundle:User')
1082 ->findOneByUsername('admin');
1083
1084 $this->assertTrue($user->isGoogleTwoFactor());
1085 $this->assertGreaterThan(0, $user->getBackupCodes());
1086
1087 $crawler = $client->request('GET', '/config/otp/app/cancel');
1088
1089 $this->assertSame(302, $client->getResponse()->getStatusCode());
1090
1091 $user = $em
1092 ->getRepository('WallabagUserBundle:User')
1093 ->findOneByUsername('admin');
1094
1095 $this->assertFalse($user->isGoogleTwoFactor());
1096 $this->assertEmpty($user->getBackupCodes());
1097 }
34be2d5d
JB
1098
1099 public function testExportTaggingRule()
1100 {
1101 $this->logInAs('admin');
1102 $client = $this->getClient();
1103
1104 ob_start();
1105 $crawler = $client->request('GET', '/tagging-rule/export');
1106 ob_end_clean();
1107
1108 $this->assertSame(200, $client->getResponse()->getStatusCode());
1109
1110 $headers = $client->getResponse()->headers;
1111 $this->assertSame('application/json', $headers->get('content-type'));
1112 $this->assertSame('attachment; filename="tagging_rules_admin.json"', $headers->get('content-disposition'));
1113 $this->assertSame('UTF-8', $headers->get('content-transfer-encoding'));
1114
1115 $content = json_decode($client->getResponse()->getContent(), true);
1116
1117 $this->assertCount(4, $content);
1118 $this->assertSame('content matches "spurs"', $content[0]['rule']);
1119 $this->assertSame('sport', $content[0]['tags'][0]);
1120 }
1121
1122 public function testImportTagginfRuleBadFile()
1123 {
1124 $this->logInAs('admin');
1125 $client = $this->getClient();
1126
1127 $crawler = $client->request('GET', '/config');
1128 $form = $crawler->filter('form[name=upload_tagging_rule_file] > button[type=submit]')->form();
1129
1130 $data = [
1131 'upload_tagging_rule_file[file]' => '',
1132 ];
1133
1134 $client->submit($form, $data);
1135
1136 $this->assertSame(302, $client->getResponse()->getStatusCode());
1137 }
1138
1139 public function testImportTagginfRuleFile()
1140 {
1141 $this->logInAs('admin');
1142 $client = $this->getClient();
1143
1144 $crawler = $client->request('GET', '/config');
1145 $form = $crawler->filter('form[name=upload_tagging_rule_file] > button[type=submit]')->form();
1146
1147 $file = new UploadedFile(__DIR__ . '/../fixtures/tagging_rules_admin.json', 'tagging_rules_admin.json');
1148
1149 $data = [
1150 'upload_tagging_rule_file[file]' => $file,
1151 ];
1152
1153 $client->submit($form, $data);
1154 $this->assertSame(302, $client->getResponse()->getStatusCode());
1155
1156 $user = $client->getContainer()->get('fos_user.user_manager.test')->findUserBy(['username' => 'admin']);
1157 $taggingRules = $user->getConfig()->getTaggingRules()->toArray();
1158 $this->assertCount(5, $taggingRules);
1159 $this->assertSame('title matches "football"', $taggingRules[4]->getRule());
1160 }
4d85d7e9 1161}