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