]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Tests/Controller/ConfigControllerTest.php
Avoid multiple tag creation
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Tests / Controller / ConfigControllerTest.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Tests\Controller;
4
5 use Wallabag\CoreBundle\Tests\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=new_user_save]'));
32 $this->assertCount(1, $crawler->filter('button[id=rss_config_save]'));
33 }
34
35 public function testUpdate()
36 {
37 $this->logInAs('admin');
38 $client = $this->getClient();
39
40 $crawler = $client->request('GET', '/config');
41
42 $this->assertEquals(200, $client->getResponse()->getStatusCode());
43
44 $form = $crawler->filter('button[id=config_save]')->form();
45
46 $data = array(
47 'config[theme]' => 0,
48 'config[items_per_page]' => '30',
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->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(array('_text')));
59 $this->assertContains('Config saved', $alert[0]);
60 }
61
62 public function dataForUpdateFailed()
63 {
64 return array(
65 array(array(
66 'config[theme]' => 0,
67 'config[items_per_page]' => '',
68 'config[language]' => 'en',
69 )),
70 );
71 }
72
73 /**
74 * @dataProvider dataForUpdateFailed
75 */
76 public function testUpdateFailed($data)
77 {
78 $this->logInAs('admin');
79 $client = $this->getClient();
80
81 $crawler = $client->request('GET', '/config');
82
83 $this->assertEquals(200, $client->getResponse()->getStatusCode());
84
85 $form = $crawler->filter('button[id=config_save]')->form();
86
87 $crawler = $client->submit($form, $data);
88
89 $this->assertEquals(200, $client->getResponse()->getStatusCode());
90
91 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(array('_text')));
92 $this->assertContains('This value should not be blank', $alert[0]);
93 }
94
95 public function dataForChangePasswordFailed()
96 {
97 return array(
98 array(
99 array(
100 'change_passwd[old_password]' => 'material',
101 'change_passwd[new_password][first]' => '',
102 'change_passwd[new_password][second]' => '',
103 ),
104 'Wrong value for your current password',
105 ),
106 array(
107 array(
108 'change_passwd[old_password]' => 'mypassword',
109 'change_passwd[new_password][first]' => '',
110 'change_passwd[new_password][second]' => '',
111 ),
112 'This value should not be blank',
113 ),
114 array(
115 array(
116 'change_passwd[old_password]' => 'mypassword',
117 'change_passwd[new_password][first]' => 'hop',
118 'change_passwd[new_password][second]' => '',
119 ),
120 'The password fields must match',
121 ),
122 array(
123 array(
124 'change_passwd[old_password]' => 'mypassword',
125 'change_passwd[new_password][first]' => 'hop',
126 'change_passwd[new_password][second]' => 'hop',
127 ),
128 'Password should by at least',
129 ),
130 );
131 }
132
133 /**
134 * @dataProvider dataForChangePasswordFailed
135 */
136 public function testChangePasswordFailed($data, $expectedMessage)
137 {
138 $this->logInAs('admin');
139 $client = $this->getClient();
140
141 $crawler = $client->request('GET', '/config');
142
143 $this->assertEquals(200, $client->getResponse()->getStatusCode());
144
145 $form = $crawler->filter('button[id=change_passwd_save]')->form();
146
147 $crawler = $client->submit($form, $data);
148
149 $this->assertEquals(200, $client->getResponse()->getStatusCode());
150
151 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(array('_text')));
152 $this->assertContains($expectedMessage, $alert[0]);
153 }
154
155 public function testChangePassword()
156 {
157 $this->logInAs('admin');
158 $client = $this->getClient();
159
160 $crawler = $client->request('GET', '/config');
161
162 $this->assertEquals(200, $client->getResponse()->getStatusCode());
163
164 $form = $crawler->filter('button[id=change_passwd_save]')->form();
165
166 $data = array(
167 'change_passwd[old_password]' => 'mypassword',
168 'change_passwd[new_password][first]' => 'mypassword',
169 'change_passwd[new_password][second]' => 'mypassword',
170 );
171
172 $client->submit($form, $data);
173
174 $this->assertEquals(302, $client->getResponse()->getStatusCode());
175
176 $crawler = $client->followRedirect();
177
178 $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(array('_text')));
179 $this->assertContains('Password updated', $alert[0]);
180 }
181
182 public function dataForUserFailed()
183 {
184 return array(
185 array(
186 array(
187 'update_user[name]' => '',
188 'update_user[email]' => '',
189 ),
190 'Please enter an email',
191 ),
192 array(
193 array(
194 'update_user[name]' => '',
195 'update_user[email]' => 'test',
196 ),
197 'The email is not valid',
198 ),
199 );
200 }
201
202 /**
203 * @dataProvider dataForUserFailed
204 */
205 public function testUserFailed($data, $expectedMessage)
206 {
207 $this->logInAs('admin');
208 $client = $this->getClient();
209
210 $crawler = $client->request('GET', '/config');
211
212 $this->assertEquals(200, $client->getResponse()->getStatusCode());
213
214 $form = $crawler->filter('button[id=update_user_save]')->form();
215
216 $crawler = $client->submit($form, $data);
217
218 $this->assertEquals(200, $client->getResponse()->getStatusCode());
219
220 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(array('_text')));
221 $this->assertContains($expectedMessage, $alert[0]);
222 }
223
224 public function testUserUpdate()
225 {
226 $this->logInAs('admin');
227 $client = $this->getClient();
228
229 $crawler = $client->request('GET', '/config');
230
231 $this->assertEquals(200, $client->getResponse()->getStatusCode());
232
233 $form = $crawler->filter('button[id=update_user_save]')->form();
234
235 $data = array(
236 'update_user[name]' => 'new name',
237 'update_user[email]' => 'admin@wallabag.io',
238 );
239
240 $client->submit($form, $data);
241
242 $this->assertEquals(302, $client->getResponse()->getStatusCode());
243
244 $crawler = $client->followRedirect();
245
246 $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(array('_text')));
247 $this->assertContains('Information updated', $alert[0]);
248 }
249
250 public function dataForNewUserFailed()
251 {
252 return array(
253 array(
254 array(
255 'new_user[username]' => '',
256 'new_user[plainPassword][first]' => '',
257 'new_user[plainPassword][second]' => '',
258 'new_user[email]' => '',
259 ),
260 'Please enter a username',
261 ),
262 array(
263 array(
264 'new_user[username]' => 'a',
265 'new_user[plainPassword][first]' => 'mypassword',
266 'new_user[plainPassword][second]' => 'mypassword',
267 'new_user[email]' => '',
268 ),
269 'The username is too short',
270 ),
271 array(
272 array(
273 'new_user[username]' => 'wallace',
274 'new_user[plainPassword][first]' => 'mypassword',
275 'new_user[plainPassword][second]' => 'mypassword',
276 'new_user[email]' => 'test',
277 ),
278 'The email is not valid',
279 ),
280 array(
281 array(
282 'new_user[username]' => 'admin',
283 'new_user[plainPassword][first]' => 'wallacewallace',
284 'new_user[plainPassword][second]' => 'wallacewallace',
285 'new_user[email]' => 'wallace@wallace.me',
286 ),
287 'The username is already used',
288 ),
289 array(
290 array(
291 'new_user[username]' => 'wallace',
292 'new_user[plainPassword][first]' => 'mypassword1',
293 'new_user[plainPassword][second]' => 'mypassword2',
294 'new_user[email]' => 'wallace@wallace.me',
295 ),
296 'This value is not valid',
297 ),
298 );
299 }
300
301 /**
302 * @dataProvider dataForNewUserFailed
303 */
304 public function testNewUserFailed($data, $expectedMessage)
305 {
306 $this->logInAs('admin');
307 $client = $this->getClient();
308
309 $crawler = $client->request('GET', '/config');
310
311 $this->assertEquals(200, $client->getResponse()->getStatusCode());
312
313 $form = $crawler->filter('button[id=new_user_save]')->form();
314
315 $crawler = $client->submit($form, $data);
316
317 $this->assertEquals(200, $client->getResponse()->getStatusCode());
318
319 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(array('_text')));
320 $this->assertContains($expectedMessage, $alert[0]);
321 }
322
323 public function testNewUserCreated()
324 {
325 $this->logInAs('admin');
326 $client = $this->getClient();
327
328 $crawler = $client->request('GET', '/config');
329
330 $this->assertEquals(200, $client->getResponse()->getStatusCode());
331
332 $form = $crawler->filter('button[id=new_user_save]')->form();
333
334 $data = array(
335 'new_user[username]' => 'wallace',
336 'new_user[plainPassword][first]' => 'wallace1',
337 'new_user[plainPassword][second]' => 'wallace1',
338 'new_user[email]' => 'wallace@wallace.me',
339 );
340
341 $client->submit($form, $data);
342
343 $this->assertEquals(302, $client->getResponse()->getStatusCode());
344
345 $crawler = $client->followRedirect();
346
347 $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(array('_text')));
348 $this->assertContains('User "wallace" added', $alert[0]);
349
350 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
351 $user = $em
352 ->getRepository('WallabagUserBundle:User')
353 ->findOneByUsername('wallace');
354
355 $this->assertTrue(false !== $user);
356 $this->assertTrue($user->isEnabled());
357 }
358
359 public function testRssUpdateResetToken()
360 {
361 $this->logInAs('admin');
362 $client = $this->getClient();
363
364 // reset the token
365 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
366 $user = $em
367 ->getRepository('WallabagUserBundle:User')
368 ->findOneByUsername('admin');
369
370 if (!$user) {
371 $this->markTestSkipped('No user found in db.');
372 }
373
374 $config = $user->getConfig();
375 $config->setRssToken(null);
376 $em->persist($config);
377 $em->flush();
378
379 $crawler = $client->request('GET', '/config');
380
381 $this->assertEquals(200, $client->getResponse()->getStatusCode());
382
383 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(array('_text')));
384 $this->assertContains('You need to generate a token first.', $body[0]);
385
386 $client->request('GET', '/generate-token');
387 $this->assertEquals(302, $client->getResponse()->getStatusCode());
388
389 $crawler = $client->followRedirect();
390
391 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(array('_text')));
392 $this->assertNotContains('You need to generate a token first.', $body[0]);
393 }
394
395 public function testGenerateTokenAjax()
396 {
397 $this->logInAs('admin');
398 $client = $this->getClient();
399
400 $client->request(
401 'GET',
402 '/generate-token',
403 array(),
404 array(),
405 array('HTTP_X-Requested-With' => 'XMLHttpRequest')
406 );
407
408 $this->assertEquals(200, $client->getResponse()->getStatusCode());
409 $content = json_decode($client->getResponse()->getContent(), true);
410 $this->assertArrayHasKey('token', $content);
411 }
412
413 public function testRssUpdate()
414 {
415 $this->logInAs('admin');
416 $client = $this->getClient();
417
418 $crawler = $client->request('GET', '/config');
419
420 if (500 == $client->getResponse()->getStatusCode()) {
421 var_export($client->getResponse()->getContent());
422 die();
423 }
424
425 $this->assertEquals(200, $client->getResponse()->getStatusCode());
426
427 $form = $crawler->filter('button[id=rss_config_save]')->form();
428
429 $data = array(
430 'rss_config[rss_limit]' => 12,
431 );
432
433 $client->submit($form, $data);
434
435 $this->assertEquals(302, $client->getResponse()->getStatusCode());
436
437 $crawler = $client->followRedirect();
438
439 $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(array('_text')));
440 $this->assertContains('RSS information updated', $alert[0]);
441 }
442
443 public function dataForRssFailed()
444 {
445 return array(
446 array(
447 array(
448 'rss_config[rss_limit]' => 0,
449 ),
450 'This value should be 1 or more.',
451 ),
452 array(
453 array(
454 'rss_config[rss_limit]' => 1000000000000,
455 ),
456 'This will certainly kill the app',
457 ),
458 );
459 }
460
461 /**
462 * @dataProvider dataForRssFailed
463 */
464 public function testRssFailed($data, $expectedMessage)
465 {
466 $this->logInAs('admin');
467 $client = $this->getClient();
468
469 $crawler = $client->request('GET', '/config');
470
471 $this->assertEquals(200, $client->getResponse()->getStatusCode());
472
473 $form = $crawler->filter('button[id=rss_config_save]')->form();
474
475 $crawler = $client->submit($form, $data);
476
477 $this->assertEquals(200, $client->getResponse()->getStatusCode());
478
479 $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(array('_text')));
480 $this->assertContains($expectedMessage, $alert[0]);
481 }
482
483 public function testTaggingRuleCreation()
484 {
485 $this->logInAs('admin');
486 $client = $this->getClient();
487
488 $crawler = $client->request('GET', '/config');
489
490 $this->assertTrue($client->getResponse()->isSuccessful());
491
492 $form = $crawler->filter('button[id=tagging_rule_save]')->form();
493
494 $data = array(
495 'tagging_rule[rule]' => 'readingTime <= 3',
496 'tagging_rule[tags]' => 'short reading',
497 );
498
499 $client->submit($form, $data);
500
501 $this->assertEquals(302, $client->getResponse()->getStatusCode());
502
503 $crawler = $client->followRedirect();
504
505 $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(array('_text')));
506 $this->assertContains('Tagging rules updated', $alert[0]);
507
508 $deleteLink = $crawler->filter('.delete')->last()->link();
509
510 $crawler = $client->click($deleteLink);
511 $this->assertEquals(302, $client->getResponse()->getStatusCode());
512
513 $crawler = $client->followRedirect();
514 $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(array('_text')));
515 $this->assertContains('Tagging rule deleted', $alert[0]);
516 }
517
518 public function dataForTaggingRuleFailed()
519 {
520 return array(
521 array(
522 array(
523 'rss_config[rule]' => 'unknownVar <= 3',
524 'rss_config[tags]' => 'cool tag',
525 ),
526 'The variable « unknownVar » does not exist.',
527 ),
528 array(
529 array(
530 'rss_config[rule]' => 'length(domainName) <= 42',
531 'rss_config[tags]' => 'cool tag',
532 ),
533 'The operator « length » does not exist.',
534 ),
535 );
536 }
537 }