]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php
Added help on config screen
[github/wallabag/wallabag.git] / tests / Wallabag / CoreBundle / Controller / ConfigControllerTest.php
1 <?php
2
3 namespace Tests\Wallabag\CoreBundle\Controller;
4
5 use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
6
7 class ConfigControllerTest extends WallabagCoreTestCase
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
28 $this->assertCount(1, $crawler->filter('button[id=config_save]'));
29 $this->assertCount(1, $crawler->filter('button[id=change_passwd_save]'));
30 $this->assertCount(1, $crawler->filter('button[id=update_user_save]'));
31 $this->assertCount(1, $crawler->filter('button[id=rss_config_save]'));
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
43 $form = $crawler->filter('button[id=config_save]')->form();
44
45 $data = [
46 'config[theme]' => 'baggy',
47 'config[items_per_page]' => '30',
48 'config[reading_speed]' => '0.5',
49 'config[language]' => 'en',
50 ];
51
52 $client->submit($form, $data);
53
54 $this->assertEquals(302, $client->getResponse()->getStatusCode());
55
56 $crawler = $client->followRedirect();
57
58 $this->assertContains('flashes.config.notice.config_saved', $crawler->filter('body')->extract(['_text'])[0]);
59 }
60
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
98 public function dataForUpdateFailed()
99 {
100 return [
101 [[
102 'config[theme]' => 'baggy',
103 'config[items_per_page]' => '',
104 'config[language]' => 'en',
105 ]],
106 ];
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
121 $form = $crawler->filter('button[id=config_save]')->form();
122
123 $crawler = $client->submit($form, $data);
124
125 $this->assertEquals(200, $client->getResponse()->getStatusCode());
126
127 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
128 $this->assertContains('This value should not be blank', $alert[0]);
129 }
130
131 public function dataForChangePasswordFailed()
132 {
133 return [
134 [
135 [
136 'change_passwd[old_password]' => 'material',
137 'change_passwd[new_password][first]' => '',
138 'change_passwd[new_password][second]' => '',
139 ],
140 'validator.password_wrong_value',
141 ],
142 [
143 [
144 'change_passwd[old_password]' => 'mypassword',
145 'change_passwd[new_password][first]' => '',
146 'change_passwd[new_password][second]' => '',
147 ],
148 'This value should not be blank',
149 ],
150 [
151 [
152 'change_passwd[old_password]' => 'mypassword',
153 'change_passwd[new_password][first]' => 'hop',
154 'change_passwd[new_password][second]' => '',
155 ],
156 'validator.password_must_match',
157 ],
158 [
159 [
160 'change_passwd[old_password]' => 'mypassword',
161 'change_passwd[new_password][first]' => 'hop',
162 'change_passwd[new_password][second]' => 'hop',
163 ],
164 'validator.password_too_short',
165 ],
166 ];
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
187 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
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
202 $data = [
203 'change_passwd[old_password]' => 'mypassword',
204 'change_passwd[new_password][first]' => 'mypassword',
205 'change_passwd[new_password][second]' => 'mypassword',
206 ];
207
208 $client->submit($form, $data);
209
210 $this->assertEquals(302, $client->getResponse()->getStatusCode());
211
212 $crawler = $client->followRedirect();
213
214 $this->assertContains('flashes.config.notice.password_updated', $crawler->filter('body')->extract(['_text'])[0]);
215 }
216
217 public function dataForUserFailed()
218 {
219 return [
220 [
221 [
222 'update_user[name]' => '',
223 'update_user[email]' => '',
224 ],
225 'fos_user.email.blank',
226 ],
227 [
228 [
229 'update_user[name]' => '',
230 'update_user[email]' => 'test',
231 ],
232 'fos_user.email.invalid',
233 ],
234 ];
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
249 $form = $crawler->filter('button[id=update_user_save]')->form();
250
251 $crawler = $client->submit($form, $data);
252
253 $this->assertEquals(200, $client->getResponse()->getStatusCode());
254
255 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
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
268 $form = $crawler->filter('button[id=update_user_save]')->form();
269
270 $data = [
271 'update_user[name]' => 'new name',
272 'update_user[email]' => 'admin@wallabag.io',
273 ];
274
275 $client->submit($form, $data);
276
277 $this->assertEquals(302, $client->getResponse()->getStatusCode());
278
279 $crawler = $client->followRedirect();
280
281 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
282 $this->assertContains('flashes.config.notice.user_updated', $alert[0]);
283 }
284
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
293 ->getRepository('WallabagUserBundle:User')
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
309 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
310 $this->assertContains('config.form_rss.no_token', $body[0]);
311
312 $client->request('GET', '/generate-token');
313 $this->assertEquals(302, $client->getResponse()->getStatusCode());
314
315 $crawler = $client->followRedirect();
316
317 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
318 $this->assertNotContains('config.form_rss.no_token', $body[0]);
319 }
320
321 public function testGenerateTokenAjax()
322 {
323 $this->logInAs('admin');
324 $client = $this->getClient();
325
326 $client->request(
327 'GET',
328 '/generate-token',
329 [],
330 [],
331 ['HTTP_X-Requested-With' => 'XMLHttpRequest']
332 );
333
334 $this->assertEquals(200, $client->getResponse()->getStatusCode());
335 $content = json_decode($client->getResponse()->getContent(), true);
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
346 $this->assertEquals(200, $client->getResponse()->getStatusCode());
347
348 $form = $crawler->filter('button[id=rss_config_save]')->form();
349
350 $data = [
351 'rss_config[rss_limit]' => 12,
352 ];
353
354 $client->submit($form, $data);
355
356 $this->assertEquals(302, $client->getResponse()->getStatusCode());
357
358 $crawler = $client->followRedirect();
359
360 $this->assertContains('flashes.config.notice.rss_updated', $crawler->filter('body')->extract(['_text'])[0]);
361 }
362
363 public function dataForRssFailed()
364 {
365 return [
366 [
367 [
368 'rss_config[rss_limit]' => 0,
369 ],
370 'This value should be 1 or more.',
371 ],
372 [
373 [
374 'rss_config[rss_limit]' => 1000000000000,
375 ],
376 'validator.rss_limit_too_high',
377 ],
378 ];
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
399 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
400 $this->assertContains($expectedMessage, $alert[0]);
401 }
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
414 $data = [
415 'tagging_rule[rule]' => 'readingTime <= 3',
416 'tagging_rule[tags]' => 'short reading',
417 ];
418
419 $client->submit($form, $data);
420
421 $this->assertEquals(302, $client->getResponse()->getStatusCode());
422
423 $crawler = $client->followRedirect();
424
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]);
451
452 $deleteLink = $crawler->filter('.delete')->last()->link();
453
454 $crawler = $client->click($deleteLink);
455 $this->assertEquals(302, $client->getResponse()->getStatusCode());
456
457 $crawler = $client->followRedirect();
458 $this->assertContains('flashes.config.notice.tagging_rules_deleted', $crawler->filter('body')->extract(['_text'])[0]);
459 }
460
461 public function dataForTaggingRuleFailed()
462 {
463 return [
464 [
465 [
466 'tagging_rule[rule]' => 'unknownVar <= 3',
467 'tagging_rule[tags]' => 'cool tag',
468 ],
469 [
470 'The variable',
471 'does not exist.',
472 ],
473 ],
474 [
475 [
476 'tagging_rule[rule]' => 'length(domainName) <= 42',
477 'tagging_rule[tags]' => 'cool tag',
478 ],
479 [
480 'The operator',
481 'does not exist.',
482 ],
483 ],
484 ];
485 }
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
501 $crawler = $client->submit($form, $data);
502
503 $this->assertEquals(200, $client->getResponse()->getStatusCode());
504
505 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
506
507 foreach ($messages as $message) {
508 $this->assertContains($message, $body[0]);
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
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());
538
539 $this->assertEquals(403, $client->getResponse()->getStatusCode());
540 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
541 $this->assertContains('You can not access this tagging rule', $body[0]);
542 }
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
559 $data = [
560 'change_passwd[old_password]' => 'mypassword',
561 'change_passwd[new_password][first]' => 'mypassword',
562 'change_passwd[new_password][second]' => 'mypassword',
563 ];
564
565 $client->submit($form, $data);
566
567 $this->assertEquals(302, $client->getResponse()->getStatusCode());
568 $this->assertContains('flashes.config.notice.password_not_updated_demo', $client->getContainer()->get('session')->getFlashBag()->get('notice')[0]);
569
570 $config->set('demo_mode_enabled', 0);
571 $config->set('demo_mode_username', 'wallabag');
572 }
573 }