]>
Commit | Line | Data |
---|---|---|
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]' => 'fr_FR', | |
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]' => 'fr_FR', | |
69 | )), | |
70 | array(array( | |
71 | 'config[theme]' => 0, | |
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 | ||
90 | $form = $crawler->filter('button[id=config_save]')->form(); | |
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 | } | |
99 | ||
100 | public function dataForChangePasswordFailed() | |
101 | { | |
102 | return array( | |
103 | array( | |
104 | array( | |
105 | 'change_passwd[old_password]' => 'material', | |
106 | 'change_passwd[new_password][first]' => '', | |
107 | 'change_passwd[new_password][second]' => '', | |
108 | ), | |
109 | 'Wrong value for your current password', | |
110 | ), | |
111 | array( | |
112 | array( | |
113 | 'change_passwd[old_password]' => 'mypassword', | |
114 | 'change_passwd[new_password][first]' => '', | |
115 | 'change_passwd[new_password][second]' => '', | |
116 | ), | |
117 | 'This value should not be blank', | |
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 | ), | |
125 | 'The password fields must match', | |
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 | ), | |
133 | 'Password should by at least', | |
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 | ||
183 | $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(array('_text'))); | |
184 | $this->assertContains('Password updated', $alert[0]); | |
185 | } | |
186 | ||
187 | public function dataForUserFailed() | |
188 | { | |
189 | return array( | |
190 | array( | |
191 | array( | |
192 | 'update_user[name]' => '', | |
193 | 'update_user[email]' => '', | |
194 | ), | |
195 | 'This value should not be blank.', | |
196 | ), | |
197 | array( | |
198 | array( | |
199 | 'update_user[name]' => '', | |
200 | 'update_user[email]' => 'test', | |
201 | ), | |
202 | 'This value is not a valid email address.', | |
203 | ), | |
204 | ); | |
205 | } | |
206 | ||
207 | /** | |
208 | * @dataProvider dataForUserFailed | |
209 | */ | |
210 | public function testUserFailed($data, $expectedMessage) | |
211 | { | |
212 | $this->logInAs('admin'); | |
213 | $client = $this->getClient(); | |
214 | ||
215 | $crawler = $client->request('GET', '/config'); | |
216 | ||
217 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
218 | ||
219 | $form = $crawler->filter('button[id=update_user_save]')->form(); | |
220 | ||
221 | $crawler = $client->submit($form, $data); | |
222 | ||
223 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
224 | ||
225 | $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(array('_text'))); | |
226 | $this->assertContains($expectedMessage, $alert[0]); | |
227 | } | |
228 | ||
229 | public function testUserUpdate() | |
230 | { | |
231 | $this->logInAs('admin'); | |
232 | $client = $this->getClient(); | |
233 | ||
234 | $crawler = $client->request('GET', '/config'); | |
235 | ||
236 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
237 | ||
238 | $form = $crawler->filter('button[id=update_user_save]')->form(); | |
239 | ||
240 | $data = array( | |
241 | 'update_user[name]' => 'new name', | |
242 | 'update_user[email]' => 'admin@wallabag.io', | |
243 | ); | |
244 | ||
245 | $client->submit($form, $data); | |
246 | ||
247 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | |
248 | ||
249 | $crawler = $client->followRedirect(); | |
250 | ||
251 | $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(array('_text'))); | |
252 | $this->assertContains('Information updated', $alert[0]); | |
253 | } | |
254 | ||
255 | public function dataForNewUserFailed() | |
256 | { | |
257 | return array( | |
258 | array( | |
259 | array( | |
260 | 'new_user[username]' => '', | |
261 | 'new_user[password]' => '', | |
262 | 'new_user[email]' => '', | |
263 | ), | |
264 | 'This value should not be blank.', | |
265 | ), | |
266 | array( | |
267 | array( | |
268 | 'new_user[username]' => 'ad', | |
269 | 'new_user[password]' => '', | |
270 | 'new_user[email]' => '', | |
271 | ), | |
272 | 'This value is too short.', | |
273 | ), | |
274 | array( | |
275 | array( | |
276 | 'new_user[username]' => 'wallace', | |
277 | 'new_user[password]' => '', | |
278 | 'new_user[email]' => 'test', | |
279 | ), | |
280 | 'This value is not a valid email address.', | |
281 | ), | |
282 | array( | |
283 | array( | |
284 | 'new_user[username]' => 'wallace', | |
285 | 'new_user[password]' => 'admin', | |
286 | 'new_user[email]' => 'wallace@wallace.me', | |
287 | ), | |
288 | 'Password should by at least', | |
289 | ), | |
290 | array( | |
291 | array( | |
292 | 'new_user[username]' => 'admin', | |
293 | 'new_user[password]' => 'wallacewallace', | |
294 | 'new_user[email]' => 'wallace@wallace.me', | |
295 | ), | |
296 | 'This value is already used', | |
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[password]' => 'wallace1', | |
337 | 'new_user[email]' => 'wallace@wallace.me', | |
338 | ); | |
339 | ||
340 | $client->submit($form, $data); | |
341 | ||
342 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | |
343 | ||
344 | $crawler = $client->followRedirect(); | |
345 | ||
346 | $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(array('_text'))); | |
347 | $this->assertContains('User "wallace" added', $alert[0]); | |
348 | } | |
349 | ||
350 | public function testRssUpdateResetToken() | |
351 | { | |
352 | $this->logInAs('admin'); | |
353 | $client = $this->getClient(); | |
354 | ||
355 | // reset the token | |
356 | $em = $client->getContainer()->get('doctrine.orm.entity_manager'); | |
357 | $user = $em | |
358 | ->getRepository('WallabagCoreBundle:User') | |
359 | ->findOneByUsername('admin'); | |
360 | ||
361 | if (!$user) { | |
362 | $this->markTestSkipped('No user found in db.'); | |
363 | } | |
364 | ||
365 | $config = $user->getConfig(); | |
366 | $config->setRssToken(null); | |
367 | $em->persist($config); | |
368 | $em->flush(); | |
369 | ||
370 | $crawler = $client->request('GET', '/config'); | |
371 | ||
372 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
373 | ||
374 | $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(array('_text'))); | |
375 | $this->assertContains('You need to generate a token first.', $body[0]); | |
376 | ||
377 | $client->request('GET', '/generate-token'); | |
378 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | |
379 | ||
380 | $crawler = $client->followRedirect(); | |
381 | ||
382 | $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(array('_text'))); | |
383 | $this->assertNotContains('You need to generate a token first.', $body[0]); | |
384 | } | |
385 | ||
386 | public function testGenerateTokenAjax() | |
387 | { | |
388 | $this->logInAs('admin'); | |
389 | $client = $this->getClient(); | |
390 | ||
391 | $client->request( | |
392 | 'GET', | |
393 | '/generate-token', | |
394 | array(), | |
395 | array(), | |
396 | array('HTTP_X-Requested-With' => 'XMLHttpRequest') | |
397 | ); | |
398 | ||
399 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
400 | $content = json_decode($client->getResponse()->getContent(), true); | |
401 | $this->assertArrayHasKey('token', $content); | |
402 | } | |
403 | ||
404 | public function testRssUpdate() | |
405 | { | |
406 | $this->logInAs('admin'); | |
407 | $client = $this->getClient(); | |
408 | ||
409 | $crawler = $client->request('GET', '/config'); | |
410 | ||
411 | if (500 == $client->getResponse()->getStatusCode()) { | |
412 | var_export($client->getResponse()->getContent()); | |
413 | die(); | |
414 | } | |
415 | ||
416 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
417 | ||
418 | $form = $crawler->filter('button[id=rss_config_save]')->form(); | |
419 | ||
420 | $data = array( | |
421 | 'rss_config[rss_limit]' => 12, | |
422 | ); | |
423 | ||
424 | $client->submit($form, $data); | |
425 | ||
426 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | |
427 | ||
428 | $crawler = $client->followRedirect(); | |
429 | ||
430 | $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(array('_text'))); | |
431 | $this->assertContains('RSS information updated', $alert[0]); | |
432 | } | |
433 | ||
434 | public function dataForRssFailed() | |
435 | { | |
436 | return array( | |
437 | array( | |
438 | array( | |
439 | 'rss_config[rss_limit]' => 0, | |
440 | ), | |
441 | 'This value should be 1 or more.', | |
442 | ), | |
443 | array( | |
444 | array( | |
445 | 'rss_config[rss_limit]' => 1000000000000, | |
446 | ), | |
447 | 'This will certainly kill the app', | |
448 | ), | |
449 | ); | |
450 | } | |
451 | ||
452 | /** | |
453 | * @dataProvider dataForRssFailed | |
454 | */ | |
455 | public function testRssFailed($data, $expectedMessage) | |
456 | { | |
457 | $this->logInAs('admin'); | |
458 | $client = $this->getClient(); | |
459 | ||
460 | $crawler = $client->request('GET', '/config'); | |
461 | ||
462 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
463 | ||
464 | $form = $crawler->filter('button[id=rss_config_save]')->form(); | |
465 | ||
466 | $crawler = $client->submit($form, $data); | |
467 | ||
468 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
469 | ||
470 | $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(array('_text'))); | |
471 | $this->assertContains($expectedMessage, $alert[0]); | |
472 | } | |
473 | } |