]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php
Added help on config screen
[github/wallabag/wallabag.git] / tests / Wallabag / CoreBundle / Controller / ConfigControllerTest.php
CommitLineData
4d85d7e9
J
1<?php
2
23634d5d 3namespace Tests\Wallabag\CoreBundle\Controller;
4d85d7e9 4
23634d5d 5use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
4d85d7e9 6
769e19dc 7class ConfigControllerTest extends WallabagCoreTestCase
4d85d7e9
J
8{
9 public function testLogin()
10 {
11 $client = $this->getClient();
12
13 $client->request('GET', '/new');
14
15 $this->assertEquals(302, $client->getResponse()->getStatusCode());
16 $this->assertContains('login', $client->getResponse()->headers->get('location'));
17 }
18
19 public function testIndex()
20 {
21 $this->logInAs('admin');
22 $client = $this->getClient();
23
24 $crawler = $client->request('GET', '/config');
25
26 $this->assertEquals(200, $client->getResponse()->getStatusCode());
27
d9085c63
J
28 $this->assertCount(1, $crawler->filter('button[id=config_save]'));
29 $this->assertCount(1, $crawler->filter('button[id=change_passwd_save]'));
c844dc0c 30 $this->assertCount(1, $crawler->filter('button[id=update_user_save]'));
371ac69a 31 $this->assertCount(1, $crawler->filter('button[id=rss_config_save]'));
4d85d7e9
J
32 }
33
34 public function testUpdate()
35 {
36 $this->logInAs('admin');
37 $client = $this->getClient();
38
39 $crawler = $client->request('GET', '/config');
40
41 $this->assertEquals(200, $client->getResponse()->getStatusCode());
42
d9085c63 43 $form = $crawler->filter('button[id=config_save]')->form();
4d85d7e9 44
4094ea47 45 $data = [
23ff8d36 46 'config[theme]' => 'baggy',
4d85d7e9 47 'config[items_per_page]' => '30',
1b64a84b 48 'config[reading_speed]' => '0.5',
c89d35e8 49 'config[language]' => 'en',
4094ea47 50 ];
4d85d7e9
J
51
52 $client->submit($form, $data);
53
54 $this->assertEquals(302, $client->getResponse()->getStatusCode());
55
56 $crawler = $client->followRedirect();
57
bf3dc999 58 $this->assertContains('flashes.config.notice.config_saved', $crawler->filter('body')->extract(['_text'])[0]);
4d85d7e9
J
59 }
60
c4c062cc
NL
61 public function testChangeReadingSpeed()
62 {
63 $this->logInAs('admin');
64 $client = $this->getClient();
65
66 $crawler = $client->request('GET', '/unread/list');
67 $form = $crawler->filter('button[id=submit-filter]')->form();
68 $dataFilters = [
69 'entry_filter[readingTime][right_number]' => 22,
70 'entry_filter[readingTime][left_number]' => 22,
71 ];
72 $crawler = $client->submit($form, $dataFilters);
73 $this->assertCount(1, $crawler->filter('div[class=entry]'));
74
75 // Change reading speed
76 $crawler = $client->request('GET', '/config');
77 $form = $crawler->filter('button[id=config_save]')->form();
78 $data = [
79 'config[reading_speed]' => '2',
80 ];
81 $client->submit($form, $data);
82
83 // Is the entry still available via filters?
84 $crawler = $client->request('GET', '/unread/list');
85 $form = $crawler->filter('button[id=submit-filter]')->form();
86 $crawler = $client->submit($form, $dataFilters);
87 $this->assertCount(0, $crawler->filter('div[class=entry]'));
88
89 // Restore old configuration
90 $crawler = $client->request('GET', '/config');
91 $form = $crawler->filter('button[id=config_save]')->form();
92 $data = [
93 'config[reading_speed]' => '0.5',
94 ];
95 $client->submit($form, $data);
96 }
97
4d85d7e9
J
98 public function dataForUpdateFailed()
99 {
4094ea47
JB
100 return [
101 [[
23ff8d36 102 'config[theme]' => 'baggy',
4d85d7e9 103 'config[items_per_page]' => '',
c89d35e8 104 'config[language]' => 'en',
4094ea47
JB
105 ]],
106 ];
4d85d7e9
J
107 }
108
109 /**
110 * @dataProvider dataForUpdateFailed
111 */
112 public function testUpdateFailed($data)
113 {
114 $this->logInAs('admin');
115 $client = $this->getClient();
116
117 $crawler = $client->request('GET', '/config');
118
119 $this->assertEquals(200, $client->getResponse()->getStatusCode());
120
d9085c63 121 $form = $crawler->filter('button[id=config_save]')->form();
4d85d7e9
J
122
123 $crawler = $client->submit($form, $data);
124
125 $this->assertEquals(200, $client->getResponse()->getStatusCode());
126
4094ea47 127 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
4d85d7e9
J
128 $this->assertContains('This value should not be blank', $alert[0]);
129 }
d9085c63
J
130
131 public function dataForChangePasswordFailed()
132 {
4094ea47
JB
133 return [
134 [
135 [
4ab58dcf 136 'change_passwd[old_password]' => 'material',
d9085c63
J
137 'change_passwd[new_password][first]' => '',
138 'change_passwd[new_password][second]' => '',
4094ea47 139 ],
0d42217e 140 'validator.password_wrong_value',
4094ea47
JB
141 ],
142 [
143 [
d9085c63
J
144 'change_passwd[old_password]' => 'mypassword',
145 'change_passwd[new_password][first]' => '',
146 'change_passwd[new_password][second]' => '',
4094ea47 147 ],
c0d9eba0 148 'This value should not be blank',
4094ea47
JB
149 ],
150 [
151 [
d9085c63
J
152 'change_passwd[old_password]' => 'mypassword',
153 'change_passwd[new_password][first]' => 'hop',
154 'change_passwd[new_password][second]' => '',
4094ea47 155 ],
0d42217e 156 'validator.password_must_match',
4094ea47
JB
157 ],
158 [
159 [
d9085c63
J
160 'change_passwd[old_password]' => 'mypassword',
161 'change_passwd[new_password][first]' => 'hop',
162 'change_passwd[new_password][second]' => 'hop',
4094ea47 163 ],
0d42217e 164 'validator.password_too_short',
4094ea47
JB
165 ],
166 ];
d9085c63
J
167 }
168
169 /**
170 * @dataProvider dataForChangePasswordFailed
171 */
172 public function testChangePasswordFailed($data, $expectedMessage)
173 {
174 $this->logInAs('admin');
175 $client = $this->getClient();
176
177 $crawler = $client->request('GET', '/config');
178
179 $this->assertEquals(200, $client->getResponse()->getStatusCode());
180
181 $form = $crawler->filter('button[id=change_passwd_save]')->form();
182
183 $crawler = $client->submit($form, $data);
184
185 $this->assertEquals(200, $client->getResponse()->getStatusCode());
186
4094ea47 187 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
d9085c63
J
188 $this->assertContains($expectedMessage, $alert[0]);
189 }
190
191 public function testChangePassword()
192 {
193 $this->logInAs('admin');
194 $client = $this->getClient();
195
196 $crawler = $client->request('GET', '/config');
197
198 $this->assertEquals(200, $client->getResponse()->getStatusCode());
199
200 $form = $crawler->filter('button[id=change_passwd_save]')->form();
201
4094ea47 202 $data = [
d9085c63
J
203 'change_passwd[old_password]' => 'mypassword',
204 'change_passwd[new_password][first]' => 'mypassword',
205 'change_passwd[new_password][second]' => 'mypassword',
4094ea47 206 ];
d9085c63
J
207
208 $client->submit($form, $data);
209
210 $this->assertEquals(302, $client->getResponse()->getStatusCode());
211
212 $crawler = $client->followRedirect();
213
bf3dc999 214 $this->assertContains('flashes.config.notice.password_updated', $crawler->filter('body')->extract(['_text'])[0]);
d9085c63 215 }
c0d9eba0
J
216
217 public function dataForUserFailed()
218 {
4094ea47
JB
219 return [
220 [
221 [
c844dc0c
J
222 'update_user[name]' => '',
223 'update_user[email]' => '',
4094ea47 224 ],
0d42217e 225 'fos_user.email.blank',
4094ea47
JB
226 ],
227 [
228 [
c844dc0c
J
229 'update_user[name]' => '',
230 'update_user[email]' => 'test',
4094ea47 231 ],
0d42217e 232 'fos_user.email.invalid',
4094ea47
JB
233 ],
234 ];
c0d9eba0
J
235 }
236
237 /**
238 * @dataProvider dataForUserFailed
239 */
240 public function testUserFailed($data, $expectedMessage)
241 {
242 $this->logInAs('admin');
243 $client = $this->getClient();
244
245 $crawler = $client->request('GET', '/config');
246
247 $this->assertEquals(200, $client->getResponse()->getStatusCode());
248
c844dc0c 249 $form = $crawler->filter('button[id=update_user_save]')->form();
c0d9eba0
J
250
251 $crawler = $client->submit($form, $data);
252
253 $this->assertEquals(200, $client->getResponse()->getStatusCode());
254
4094ea47 255 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
c0d9eba0
J
256 $this->assertContains($expectedMessage, $alert[0]);
257 }
258
259 public function testUserUpdate()
260 {
261 $this->logInAs('admin');
262 $client = $this->getClient();
263
264 $crawler = $client->request('GET', '/config');
265
266 $this->assertEquals(200, $client->getResponse()->getStatusCode());
267
c844dc0c 268 $form = $crawler->filter('button[id=update_user_save]')->form();
c0d9eba0 269
4094ea47 270 $data = [
c844dc0c
J
271 'update_user[name]' => 'new name',
272 'update_user[email]' => 'admin@wallabag.io',
4094ea47 273 ];
c0d9eba0
J
274
275 $client->submit($form, $data);
276
277 $this->assertEquals(302, $client->getResponse()->getStatusCode());
278
279 $crawler = $client->followRedirect();
280
4094ea47 281 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
4204a06b 282 $this->assertContains('flashes.config.notice.user_updated', $alert[0]);
c0d9eba0 283 }
e4977b8a 284
371ac69a
J
285 public function testRssUpdateResetToken()
286 {
287 $this->logInAs('admin');
288 $client = $this->getClient();
289
290 // reset the token
291 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
292 $user = $em
1210dae1 293 ->getRepository('WallabagUserBundle:User')
371ac69a
J
294 ->findOneByUsername('admin');
295
296 if (!$user) {
297 $this->markTestSkipped('No user found in db.');
298 }
299
300 $config = $user->getConfig();
301 $config->setRssToken(null);
302 $em->persist($config);
303 $em->flush();
304
305 $crawler = $client->request('GET', '/config');
306
307 $this->assertEquals(200, $client->getResponse()->getStatusCode());
308
4094ea47 309 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
0d42217e 310 $this->assertContains('config.form_rss.no_token', $body[0]);
371ac69a
J
311
312 $client->request('GET', '/generate-token');
313 $this->assertEquals(302, $client->getResponse()->getStatusCode());
314
315 $crawler = $client->followRedirect();
316
4094ea47 317 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
0d42217e 318 $this->assertNotContains('config.form_rss.no_token', $body[0]);
371ac69a
J
319 }
320
321 public function testGenerateTokenAjax()
322 {
323 $this->logInAs('admin');
324 $client = $this->getClient();
325
326 $client->request(
327 'GET',
328 '/generate-token',
4094ea47
JB
329 [],
330 [],
331 ['HTTP_X-Requested-With' => 'XMLHttpRequest']
371ac69a
J
332 );
333
334 $this->assertEquals(200, $client->getResponse()->getStatusCode());
9744e971 335 $content = json_decode($client->getResponse()->getContent(), true);
371ac69a
J
336 $this->assertArrayHasKey('token', $content);
337 }
338
339 public function testRssUpdate()
340 {
341 $this->logInAs('admin');
342 $client = $this->getClient();
343
344 $crawler = $client->request('GET', '/config');
345
371ac69a
J
346 $this->assertEquals(200, $client->getResponse()->getStatusCode());
347
348 $form = $crawler->filter('button[id=rss_config_save]')->form();
349
4094ea47 350 $data = [
371ac69a 351 'rss_config[rss_limit]' => 12,
4094ea47 352 ];
371ac69a
J
353
354 $client->submit($form, $data);
355
356 $this->assertEquals(302, $client->getResponse()->getStatusCode());
357
358 $crawler = $client->followRedirect();
359
bf3dc999 360 $this->assertContains('flashes.config.notice.rss_updated', $crawler->filter('body')->extract(['_text'])[0]);
371ac69a
J
361 }
362
363 public function dataForRssFailed()
364 {
4094ea47
JB
365 return [
366 [
367 [
371ac69a 368 'rss_config[rss_limit]' => 0,
4094ea47 369 ],
371ac69a 370 'This value should be 1 or more.',
4094ea47
JB
371 ],
372 [
373 [
371ac69a 374 'rss_config[rss_limit]' => 1000000000000,
4094ea47 375 ],
540a9bc4 376 'validator.rss_limit_too_high',
4094ea47
JB
377 ],
378 ];
371ac69a
J
379 }
380
381 /**
382 * @dataProvider dataForRssFailed
383 */
384 public function testRssFailed($data, $expectedMessage)
385 {
386 $this->logInAs('admin');
387 $client = $this->getClient();
388
389 $crawler = $client->request('GET', '/config');
390
391 $this->assertEquals(200, $client->getResponse()->getStatusCode());
392
393 $form = $crawler->filter('button[id=rss_config_save]')->form();
394
395 $crawler = $client->submit($form, $data);
396
397 $this->assertEquals(200, $client->getResponse()->getStatusCode());
398
4094ea47 399 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
371ac69a
J
400 $this->assertContains($expectedMessage, $alert[0]);
401 }
8a99c7a8
KG
402
403 public function testTaggingRuleCreation()
404 {
405 $this->logInAs('admin');
406 $client = $this->getClient();
407
408 $crawler = $client->request('GET', '/config');
409
410 $this->assertTrue($client->getResponse()->isSuccessful());
411
412 $form = $crawler->filter('button[id=tagging_rule_save]')->form();
413
4094ea47 414 $data = [
8a99c7a8
KG
415 'tagging_rule[rule]' => 'readingTime <= 3',
416 'tagging_rule[tags]' => 'short reading',
4094ea47 417 ];
8a99c7a8
KG
418
419 $client->submit($form, $data);
420
421 $this->assertEquals(302, $client->getResponse()->getStatusCode());
422
423 $crawler = $client->followRedirect();
424
bf3dc999
JB
425 $this->assertContains('flashes.config.notice.tagging_rules_updated', $crawler->filter('body')->extract(['_text'])[0]);
426
427 $editLink = $crawler->filter('.mode_edit')->last()->link();
428
429 $crawler = $client->click($editLink);
430 $this->assertEquals(302, $client->getResponse()->getStatusCode());
431 $this->assertContains('?tagging-rule=', $client->getResponse()->headers->get('location'));
432
433 $crawler = $client->followRedirect();
434
435 $form = $crawler->filter('button[id=tagging_rule_save]')->form();
436
437 $data = [
438 'tagging_rule[rule]' => 'readingTime <= 30',
439 'tagging_rule[tags]' => 'short reading',
440 ];
441
442 $client->submit($form, $data);
443
444 $this->assertEquals(302, $client->getResponse()->getStatusCode());
445
446 $crawler = $client->followRedirect();
447
448 $this->assertContains('flashes.config.notice.tagging_rules_updated', $crawler->filter('body')->extract(['_text'])[0]);
449
450 $this->assertContains('readingTime <= 30', $crawler->filter('body')->extract(['_text'])[0]);
8a99c7a8 451
958671a7 452 $deleteLink = $crawler->filter('.delete')->last()->link();
8a99c7a8
KG
453
454 $crawler = $client->click($deleteLink);
455 $this->assertEquals(302, $client->getResponse()->getStatusCode());
456
457 $crawler = $client->followRedirect();
bf3dc999 458 $this->assertContains('flashes.config.notice.tagging_rules_deleted', $crawler->filter('body')->extract(['_text'])[0]);
8a99c7a8
KG
459 }
460
461 public function dataForTaggingRuleFailed()
462 {
4094ea47
JB
463 return [
464 [
465 [
a3cac44c
JB
466 'tagging_rule[rule]' => 'unknownVar <= 3',
467 'tagging_rule[tags]' => 'cool tag',
4094ea47
JB
468 ],
469 [
a3cac44c
JB
470 'The variable',
471 'does not exist.',
4094ea47
JB
472 ],
473 ],
474 [
475 [
a3cac44c
JB
476 'tagging_rule[rule]' => 'length(domainName) <= 42',
477 'tagging_rule[tags]' => 'cool tag',
4094ea47
JB
478 ],
479 [
a3cac44c
JB
480 'The operator',
481 'does not exist.',
4094ea47
JB
482 ],
483 ],
484 ];
8a99c7a8 485 }
a3cac44c
JB
486
487 /**
488 * @dataProvider dataForTaggingRuleFailed
489 */
490 public function testTaggingRuleCreationFail($data, $messages)
491 {
492 $this->logInAs('admin');
493 $client = $this->getClient();
494
495 $crawler = $client->request('GET', '/config');
496
497 $this->assertTrue($client->getResponse()->isSuccessful());
498
499 $form = $crawler->filter('button[id=tagging_rule_save]')->form();
500
4f9cf232 501 $crawler = $client->submit($form, $data);
a3cac44c
JB
502
503 $this->assertEquals(200, $client->getResponse()->getStatusCode());
504
4094ea47 505 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
4f9cf232 506
a3cac44c 507 foreach ($messages as $message) {
4f9cf232 508 $this->assertContains($message, $body[0]);
a3cac44c
JB
509 }
510 }
511
512 public function testDeletingTaggingRuleFromAnOtherUser()
513 {
514 $this->logInAs('bob');
515 $client = $this->getClient();
516
517 $rule = $client->getContainer()->get('doctrine.orm.entity_manager')
518 ->getRepository('WallabagCoreBundle:TaggingRule')
519 ->findAll()[0];
520
bf3dc999
JB
521 $crawler = $client->request('GET', '/tagging-rule/edit/'.$rule->getId());
522
523 $this->assertEquals(403, $client->getResponse()->getStatusCode());
524 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
525 $this->assertContains('You can not access this tagging rule', $body[0]);
526 }
527
528 public function testEditingTaggingRuleFromAnOtherUser()
529 {
530 $this->logInAs('bob');
531 $client = $this->getClient();
532
533 $rule = $client->getContainer()->get('doctrine.orm.entity_manager')
534 ->getRepository('WallabagCoreBundle:TaggingRule')
535 ->findAll()[0];
536
537 $crawler = $client->request('GET', '/tagging-rule/edit/'.$rule->getId());
4f9cf232 538
a3cac44c 539 $this->assertEquals(403, $client->getResponse()->getStatusCode());
4094ea47 540 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
4f9cf232 541 $this->assertContains('You can not access this tagging rule', $body[0]);
a3cac44c 542 }
b6c00b0b
JB
543
544 public function testDemoMode()
545 {
546 $this->logInAs('admin');
547 $client = $this->getClient();
548
549 $config = $client->getContainer()->get('craue_config');
550 $config->set('demo_mode_enabled', 1);
551 $config->set('demo_mode_username', 'admin');
552
553 $crawler = $client->request('GET', '/config');
554
555 $this->assertEquals(200, $client->getResponse()->getStatusCode());
556
557 $form = $crawler->filter('button[id=change_passwd_save]')->form();
558
4094ea47 559 $data = [
b6c00b0b
JB
560 'change_passwd[old_password]' => 'mypassword',
561 'change_passwd[new_password][first]' => 'mypassword',
562 'change_passwd[new_password][second]' => 'mypassword',
4094ea47 563 ];
b6c00b0b
JB
564
565 $client->submit($form, $data);
566
567 $this->assertEquals(302, $client->getResponse()->getStatusCode());
4204a06b 568 $this->assertContains('flashes.config.notice.password_not_updated_demo', $client->getContainer()->get('session')->getFlashBag()->get('notice')[0]);
b6c00b0b
JB
569
570 $config->set('demo_mode_enabled', 0);
571 $config->set('demo_mode_username', 'wallabag');
572 }
4d85d7e9 573}