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