]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Tests\Wallabag\CoreBundle\Controller; | |
4 | ||
5 | use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; | |
6 | use Wallabag\CoreBundle\Entity\Config; | |
7 | use Wallabag\UserBundle\Entity\User; | |
8 | use Wallabag\CoreBundle\Entity\Entry; | |
9 | use Wallabag\CoreBundle\Entity\Tag; | |
10 | use Wallabag\AnnotationBundle\Entity\Annotation; | |
11 | ||
12 | class ConfigControllerTest extends WallabagCoreTestCase | |
13 | { | |
14 | public function testLogin() | |
15 | { | |
16 | $client = $this->getClient(); | |
17 | ||
18 | $client->request('GET', '/new'); | |
19 | ||
20 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | |
21 | $this->assertContains('login', $client->getResponse()->headers->get('location')); | |
22 | } | |
23 | ||
24 | public function testIndex() | |
25 | { | |
26 | $this->logInAs('admin'); | |
27 | $client = $this->getClient(); | |
28 | ||
29 | $crawler = $client->request('GET', '/config'); | |
30 | ||
31 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
32 | ||
33 | $this->assertCount(1, $crawler->filter('button[id=config_save]')); | |
34 | $this->assertCount(1, $crawler->filter('button[id=change_passwd_save]')); | |
35 | $this->assertCount(1, $crawler->filter('button[id=update_user_save]')); | |
36 | $this->assertCount(1, $crawler->filter('button[id=rss_config_save]')); | |
37 | } | |
38 | ||
39 | public function testUpdate() | |
40 | { | |
41 | $this->logInAs('admin'); | |
42 | $client = $this->getClient(); | |
43 | ||
44 | $crawler = $client->request('GET', '/config'); | |
45 | ||
46 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
47 | ||
48 | $form = $crawler->filter('button[id=config_save]')->form(); | |
49 | ||
50 | $data = [ | |
51 | 'config[theme]' => 'baggy', | |
52 | 'config[items_per_page]' => '30', | |
53 | 'config[reading_speed]' => '0.5', | |
54 | 'config[action_mark_as_read]' => '0', | |
55 | 'config[language]' => 'en', | |
56 | ]; | |
57 | ||
58 | $client->submit($form, $data); | |
59 | ||
60 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | |
61 | ||
62 | $crawler = $client->followRedirect(); | |
63 | ||
64 | $this->assertContains('flashes.config.notice.config_saved', $crawler->filter('body')->extract(['_text'])[0]); | |
65 | } | |
66 | ||
67 | public function testChangeReadingSpeed() | |
68 | { | |
69 | $this->logInAs('admin'); | |
70 | $client = $this->getClient(); | |
71 | ||
72 | $crawler = $client->request('GET', '/unread/list'); | |
73 | $form = $crawler->filter('button[id=submit-filter]')->form(); | |
74 | $dataFilters = [ | |
75 | 'entry_filter[readingTime][right_number]' => 22, | |
76 | 'entry_filter[readingTime][left_number]' => 22, | |
77 | ]; | |
78 | $crawler = $client->submit($form, $dataFilters); | |
79 | $this->assertCount(1, $crawler->filter('div[class=entry]')); | |
80 | ||
81 | // Change reading speed | |
82 | $crawler = $client->request('GET', '/config'); | |
83 | $form = $crawler->filter('button[id=config_save]')->form(); | |
84 | $data = [ | |
85 | 'config[reading_speed]' => '2', | |
86 | ]; | |
87 | $client->submit($form, $data); | |
88 | ||
89 | // Is the entry still available via filters? | |
90 | $crawler = $client->request('GET', '/unread/list'); | |
91 | $form = $crawler->filter('button[id=submit-filter]')->form(); | |
92 | $crawler = $client->submit($form, $dataFilters); | |
93 | $this->assertCount(0, $crawler->filter('div[class=entry]')); | |
94 | ||
95 | // Restore old configuration | |
96 | $crawler = $client->request('GET', '/config'); | |
97 | $form = $crawler->filter('button[id=config_save]')->form(); | |
98 | $data = [ | |
99 | 'config[reading_speed]' => '0.5', | |
100 | ]; | |
101 | $client->submit($form, $data); | |
102 | } | |
103 | ||
104 | public function dataForUpdateFailed() | |
105 | { | |
106 | return [ | |
107 | [[ | |
108 | 'config[theme]' => 'baggy', | |
109 | 'config[items_per_page]' => '', | |
110 | 'config[language]' => 'en', | |
111 | ]], | |
112 | ]; | |
113 | } | |
114 | ||
115 | /** | |
116 | * @dataProvider dataForUpdateFailed | |
117 | */ | |
118 | public function testUpdateFailed($data) | |
119 | { | |
120 | $this->logInAs('admin'); | |
121 | $client = $this->getClient(); | |
122 | ||
123 | $crawler = $client->request('GET', '/config'); | |
124 | ||
125 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
126 | ||
127 | $form = $crawler->filter('button[id=config_save]')->form(); | |
128 | ||
129 | $crawler = $client->submit($form, $data); | |
130 | ||
131 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
132 | ||
133 | $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text'])); | |
134 | $this->assertContains('This value should not be blank', $alert[0]); | |
135 | } | |
136 | ||
137 | public function dataForChangePasswordFailed() | |
138 | { | |
139 | return [ | |
140 | [ | |
141 | [ | |
142 | 'change_passwd[old_password]' => 'material', | |
143 | 'change_passwd[new_password][first]' => '', | |
144 | 'change_passwd[new_password][second]' => '', | |
145 | ], | |
146 | 'validator.password_wrong_value', | |
147 | ], | |
148 | [ | |
149 | [ | |
150 | 'change_passwd[old_password]' => 'mypassword', | |
151 | 'change_passwd[new_password][first]' => '', | |
152 | 'change_passwd[new_password][second]' => '', | |
153 | ], | |
154 | 'This value should not be blank', | |
155 | ], | |
156 | [ | |
157 | [ | |
158 | 'change_passwd[old_password]' => 'mypassword', | |
159 | 'change_passwd[new_password][first]' => 'hop', | |
160 | 'change_passwd[new_password][second]' => '', | |
161 | ], | |
162 | 'validator.password_must_match', | |
163 | ], | |
164 | [ | |
165 | [ | |
166 | 'change_passwd[old_password]' => 'mypassword', | |
167 | 'change_passwd[new_password][first]' => 'hop', | |
168 | 'change_passwd[new_password][second]' => 'hop', | |
169 | ], | |
170 | 'validator.password_too_short', | |
171 | ], | |
172 | ]; | |
173 | } | |
174 | ||
175 | /** | |
176 | * @dataProvider dataForChangePasswordFailed | |
177 | */ | |
178 | public function testChangePasswordFailed($data, $expectedMessage) | |
179 | { | |
180 | $this->logInAs('admin'); | |
181 | $client = $this->getClient(); | |
182 | ||
183 | $crawler = $client->request('GET', '/config'); | |
184 | ||
185 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
186 | ||
187 | $form = $crawler->filter('button[id=change_passwd_save]')->form(); | |
188 | ||
189 | $crawler = $client->submit($form, $data); | |
190 | ||
191 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
192 | ||
193 | $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text'])); | |
194 | $this->assertContains($expectedMessage, $alert[0]); | |
195 | } | |
196 | ||
197 | public function testChangePassword() | |
198 | { | |
199 | $this->logInAs('admin'); | |
200 | $client = $this->getClient(); | |
201 | ||
202 | $crawler = $client->request('GET', '/config'); | |
203 | ||
204 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
205 | ||
206 | $form = $crawler->filter('button[id=change_passwd_save]')->form(); | |
207 | ||
208 | $data = [ | |
209 | 'change_passwd[old_password]' => 'mypassword', | |
210 | 'change_passwd[new_password][first]' => 'mypassword', | |
211 | 'change_passwd[new_password][second]' => 'mypassword', | |
212 | ]; | |
213 | ||
214 | $client->submit($form, $data); | |
215 | ||
216 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | |
217 | ||
218 | $crawler = $client->followRedirect(); | |
219 | ||
220 | $this->assertContains('flashes.config.notice.password_updated', $crawler->filter('body')->extract(['_text'])[0]); | |
221 | } | |
222 | ||
223 | public function dataForUserFailed() | |
224 | { | |
225 | return [ | |
226 | [ | |
227 | [ | |
228 | 'update_user[name]' => '', | |
229 | 'update_user[email]' => '', | |
230 | ], | |
231 | 'fos_user.email.blank', | |
232 | ], | |
233 | [ | |
234 | [ | |
235 | 'update_user[name]' => '', | |
236 | 'update_user[email]' => 'test', | |
237 | ], | |
238 | 'fos_user.email.invalid', | |
239 | ], | |
240 | ]; | |
241 | } | |
242 | ||
243 | /** | |
244 | * @dataProvider dataForUserFailed | |
245 | */ | |
246 | public function testUserFailed($data, $expectedMessage) | |
247 | { | |
248 | $this->logInAs('admin'); | |
249 | $client = $this->getClient(); | |
250 | ||
251 | $crawler = $client->request('GET', '/config'); | |
252 | ||
253 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
254 | ||
255 | $form = $crawler->filter('button[id=update_user_save]')->form(); | |
256 | ||
257 | $crawler = $client->submit($form, $data); | |
258 | ||
259 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
260 | ||
261 | $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text'])); | |
262 | $this->assertContains($expectedMessage, $alert[0]); | |
263 | } | |
264 | ||
265 | public function testUserUpdate() | |
266 | { | |
267 | $this->logInAs('admin'); | |
268 | $client = $this->getClient(); | |
269 | ||
270 | $crawler = $client->request('GET', '/config'); | |
271 | ||
272 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
273 | ||
274 | $form = $crawler->filter('button[id=update_user_save]')->form(); | |
275 | ||
276 | $data = [ | |
277 | 'update_user[name]' => 'new name', | |
278 | 'update_user[email]' => 'admin@wallabag.io', | |
279 | ]; | |
280 | ||
281 | $client->submit($form, $data); | |
282 | ||
283 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | |
284 | ||
285 | $crawler = $client->followRedirect(); | |
286 | ||
287 | $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text'])); | |
288 | $this->assertContains('flashes.config.notice.user_updated', $alert[0]); | |
289 | } | |
290 | ||
291 | public function testRssUpdateResetToken() | |
292 | { | |
293 | $this->logInAs('admin'); | |
294 | $client = $this->getClient(); | |
295 | ||
296 | // reset the token | |
297 | $em = $client->getContainer()->get('doctrine.orm.entity_manager'); | |
298 | $user = $em | |
299 | ->getRepository('WallabagUserBundle:User') | |
300 | ->findOneByUsername('admin'); | |
301 | ||
302 | if (!$user) { | |
303 | $this->markTestSkipped('No user found in db.'); | |
304 | } | |
305 | ||
306 | $config = $user->getConfig(); | |
307 | $config->setRssToken(null); | |
308 | $em->persist($config); | |
309 | $em->flush(); | |
310 | ||
311 | $crawler = $client->request('GET', '/config'); | |
312 | ||
313 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
314 | ||
315 | $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text'])); | |
316 | $this->assertContains('config.form_rss.no_token', $body[0]); | |
317 | ||
318 | $client->request('GET', '/generate-token'); | |
319 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | |
320 | ||
321 | $crawler = $client->followRedirect(); | |
322 | ||
323 | $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text'])); | |
324 | $this->assertNotContains('config.form_rss.no_token', $body[0]); | |
325 | } | |
326 | ||
327 | public function testGenerateTokenAjax() | |
328 | { | |
329 | $this->logInAs('admin'); | |
330 | $client = $this->getClient(); | |
331 | ||
332 | $client->request( | |
333 | 'GET', | |
334 | '/generate-token', | |
335 | [], | |
336 | [], | |
337 | ['HTTP_X-Requested-With' => 'XMLHttpRequest'] | |
338 | ); | |
339 | ||
340 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
341 | $content = json_decode($client->getResponse()->getContent(), true); | |
342 | $this->assertArrayHasKey('token', $content); | |
343 | } | |
344 | ||
345 | public function testRssUpdate() | |
346 | { | |
347 | $this->logInAs('admin'); | |
348 | $client = $this->getClient(); | |
349 | ||
350 | $crawler = $client->request('GET', '/config'); | |
351 | ||
352 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
353 | ||
354 | $form = $crawler->filter('button[id=rss_config_save]')->form(); | |
355 | ||
356 | $data = [ | |
357 | 'rss_config[rss_limit]' => 12, | |
358 | ]; | |
359 | ||
360 | $client->submit($form, $data); | |
361 | ||
362 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | |
363 | ||
364 | $crawler = $client->followRedirect(); | |
365 | ||
366 | $this->assertContains('flashes.config.notice.rss_updated', $crawler->filter('body')->extract(['_text'])[0]); | |
367 | } | |
368 | ||
369 | public function dataForRssFailed() | |
370 | { | |
371 | return [ | |
372 | [ | |
373 | [ | |
374 | 'rss_config[rss_limit]' => 0, | |
375 | ], | |
376 | 'This value should be 1 or more.', | |
377 | ], | |
378 | [ | |
379 | [ | |
380 | 'rss_config[rss_limit]' => 1000000000000, | |
381 | ], | |
382 | 'validator.rss_limit_too_high', | |
383 | ], | |
384 | ]; | |
385 | } | |
386 | ||
387 | /** | |
388 | * @dataProvider dataForRssFailed | |
389 | */ | |
390 | public function testRssFailed($data, $expectedMessage) | |
391 | { | |
392 | $this->logInAs('admin'); | |
393 | $client = $this->getClient(); | |
394 | ||
395 | $crawler = $client->request('GET', '/config'); | |
396 | ||
397 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
398 | ||
399 | $form = $crawler->filter('button[id=rss_config_save]')->form(); | |
400 | ||
401 | $crawler = $client->submit($form, $data); | |
402 | ||
403 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
404 | ||
405 | $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text'])); | |
406 | $this->assertContains($expectedMessage, $alert[0]); | |
407 | } | |
408 | ||
409 | public function testTaggingRuleCreation() | |
410 | { | |
411 | $this->logInAs('admin'); | |
412 | $client = $this->getClient(); | |
413 | ||
414 | $crawler = $client->request('GET', '/config'); | |
415 | ||
416 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
417 | ||
418 | $form = $crawler->filter('button[id=tagging_rule_save]')->form(); | |
419 | ||
420 | $data = [ | |
421 | 'tagging_rule[rule]' => 'readingTime <= 3', | |
422 | 'tagging_rule[tags]' => 'short reading', | |
423 | ]; | |
424 | ||
425 | $client->submit($form, $data); | |
426 | ||
427 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | |
428 | ||
429 | $crawler = $client->followRedirect(); | |
430 | ||
431 | $this->assertContains('flashes.config.notice.tagging_rules_updated', $crawler->filter('body')->extract(['_text'])[0]); | |
432 | ||
433 | $editLink = $crawler->filter('.mode_edit')->last()->link(); | |
434 | ||
435 | $crawler = $client->click($editLink); | |
436 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | |
437 | $this->assertContains('?tagging-rule=', $client->getResponse()->headers->get('location')); | |
438 | ||
439 | $crawler = $client->followRedirect(); | |
440 | ||
441 | $form = $crawler->filter('button[id=tagging_rule_save]')->form(); | |
442 | ||
443 | $data = [ | |
444 | 'tagging_rule[rule]' => 'readingTime <= 30', | |
445 | 'tagging_rule[tags]' => 'short reading', | |
446 | ]; | |
447 | ||
448 | $client->submit($form, $data); | |
449 | ||
450 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | |
451 | ||
452 | $crawler = $client->followRedirect(); | |
453 | ||
454 | $this->assertContains('flashes.config.notice.tagging_rules_updated', $crawler->filter('body')->extract(['_text'])[0]); | |
455 | ||
456 | $this->assertContains('readingTime <= 30', $crawler->filter('body')->extract(['_text'])[0]); | |
457 | ||
458 | $deleteLink = $crawler->filter('.delete')->last()->link(); | |
459 | ||
460 | $crawler = $client->click($deleteLink); | |
461 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | |
462 | ||
463 | $crawler = $client->followRedirect(); | |
464 | $this->assertContains('flashes.config.notice.tagging_rules_deleted', $crawler->filter('body')->extract(['_text'])[0]); | |
465 | } | |
466 | ||
467 | public function dataForTaggingRuleFailed() | |
468 | { | |
469 | return [ | |
470 | [ | |
471 | [ | |
472 | 'tagging_rule[rule]' => 'unknownVar <= 3', | |
473 | 'tagging_rule[tags]' => 'cool tag', | |
474 | ], | |
475 | [ | |
476 | 'The variable', | |
477 | 'does not exist.', | |
478 | ], | |
479 | ], | |
480 | [ | |
481 | [ | |
482 | 'tagging_rule[rule]' => 'length(domainName) <= 42', | |
483 | 'tagging_rule[tags]' => 'cool tag', | |
484 | ], | |
485 | [ | |
486 | 'The operator', | |
487 | 'does not exist.', | |
488 | ], | |
489 | ], | |
490 | ]; | |
491 | } | |
492 | ||
493 | /** | |
494 | * @dataProvider dataForTaggingRuleFailed | |
495 | */ | |
496 | public function testTaggingRuleCreationFail($data, $messages) | |
497 | { | |
498 | $this->logInAs('admin'); | |
499 | $client = $this->getClient(); | |
500 | ||
501 | $crawler = $client->request('GET', '/config'); | |
502 | ||
503 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
504 | ||
505 | $form = $crawler->filter('button[id=tagging_rule_save]')->form(); | |
506 | ||
507 | $crawler = $client->submit($form, $data); | |
508 | ||
509 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
510 | ||
511 | $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text'])); | |
512 | ||
513 | foreach ($messages as $message) { | |
514 | $this->assertContains($message, $body[0]); | |
515 | } | |
516 | } | |
517 | ||
518 | public function testDeletingTaggingRuleFromAnOtherUser() | |
519 | { | |
520 | $this->logInAs('bob'); | |
521 | $client = $this->getClient(); | |
522 | ||
523 | $rule = $client->getContainer()->get('doctrine.orm.entity_manager') | |
524 | ->getRepository('WallabagCoreBundle:TaggingRule') | |
525 | ->findAll()[0]; | |
526 | ||
527 | $crawler = $client->request('GET', '/tagging-rule/edit/'.$rule->getId()); | |
528 | ||
529 | $this->assertEquals(403, $client->getResponse()->getStatusCode()); | |
530 | $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text'])); | |
531 | $this->assertContains('You can not access this tagging rule', $body[0]); | |
532 | } | |
533 | ||
534 | public function testEditingTaggingRuleFromAnOtherUser() | |
535 | { | |
536 | $this->logInAs('bob'); | |
537 | $client = $this->getClient(); | |
538 | ||
539 | $rule = $client->getContainer()->get('doctrine.orm.entity_manager') | |
540 | ->getRepository('WallabagCoreBundle:TaggingRule') | |
541 | ->findAll()[0]; | |
542 | ||
543 | $crawler = $client->request('GET', '/tagging-rule/edit/'.$rule->getId()); | |
544 | ||
545 | $this->assertEquals(403, $client->getResponse()->getStatusCode()); | |
546 | $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text'])); | |
547 | $this->assertContains('You can not access this tagging rule', $body[0]); | |
548 | } | |
549 | ||
550 | public function testDemoMode() | |
551 | { | |
552 | $this->logInAs('admin'); | |
553 | $client = $this->getClient(); | |
554 | ||
555 | $config = $client->getContainer()->get('craue_config'); | |
556 | $config->set('demo_mode_enabled', 1); | |
557 | $config->set('demo_mode_username', 'admin'); | |
558 | ||
559 | $crawler = $client->request('GET', '/config'); | |
560 | ||
561 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
562 | ||
563 | $form = $crawler->filter('button[id=change_passwd_save]')->form(); | |
564 | ||
565 | $data = [ | |
566 | 'change_passwd[old_password]' => 'mypassword', | |
567 | 'change_passwd[new_password][first]' => 'mypassword', | |
568 | 'change_passwd[new_password][second]' => 'mypassword', | |
569 | ]; | |
570 | ||
571 | $client->submit($form, $data); | |
572 | ||
573 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | |
574 | $this->assertContains('flashes.config.notice.password_not_updated_demo', $client->getContainer()->get('session')->getFlashBag()->get('notice')[0]); | |
575 | ||
576 | $config->set('demo_mode_enabled', 0); | |
577 | $config->set('demo_mode_username', 'wallabag'); | |
578 | } | |
579 | ||
580 | public function testDeleteUserButtonVisibility() | |
581 | { | |
582 | $this->logInAs('admin'); | |
583 | $client = $this->getClient(); | |
584 | ||
585 | $crawler = $client->request('GET', '/config'); | |
586 | ||
587 | $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text'])); | |
588 | $this->assertContains('config.form_user.delete.button', $body[0]); | |
589 | ||
590 | $em = $client->getContainer()->get('doctrine.orm.entity_manager'); | |
591 | ||
592 | $user = $em | |
593 | ->getRepository('WallabagUserBundle:User') | |
594 | ->findOneByUsername('empty'); | |
595 | $user->setExpired(1); | |
596 | $em->persist($user); | |
597 | ||
598 | $user = $em | |
599 | ->getRepository('WallabagUserBundle:User') | |
600 | ->findOneByUsername('bob'); | |
601 | $user->setExpired(1); | |
602 | $em->persist($user); | |
603 | ||
604 | $em->flush(); | |
605 | ||
606 | $crawler = $client->request('GET', '/config'); | |
607 | ||
608 | $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text'])); | |
609 | $this->assertNotContains('config.form_user.delete.button', $body[0]); | |
610 | ||
611 | $client->request('GET', '/account/delete'); | |
612 | $this->assertEquals(403, $client->getResponse()->getStatusCode()); | |
613 | ||
614 | $user = $em | |
615 | ->getRepository('WallabagUserBundle:User') | |
616 | ->findOneByUsername('empty'); | |
617 | $user->setExpired(0); | |
618 | $em->persist($user); | |
619 | ||
620 | $user = $em | |
621 | ->getRepository('WallabagUserBundle:User') | |
622 | ->findOneByUsername('bob'); | |
623 | $user->setExpired(0); | |
624 | $em->persist($user); | |
625 | ||
626 | $em->flush(); | |
627 | } | |
628 | ||
629 | public function testDeleteAccount() | |
630 | { | |
631 | $client = $this->getClient(); | |
632 | $em = $client->getContainer()->get('doctrine.orm.entity_manager'); | |
633 | ||
634 | $user = new User(); | |
635 | $user->setName('Wallace'); | |
636 | $user->setEmail('wallace@wallabag.org'); | |
637 | $user->setUsername('wallace'); | |
638 | $user->setPlainPassword('wallace'); | |
639 | $user->setEnabled(true); | |
640 | $user->addRole('ROLE_SUPER_ADMIN'); | |
641 | ||
642 | $em->persist($user); | |
643 | ||
644 | $config = new Config($user); | |
645 | ||
646 | $config->setTheme('material'); | |
647 | $config->setItemsPerPage(30); | |
648 | $config->setReadingSpeed(1); | |
649 | $config->setLanguage('en'); | |
650 | $config->setPocketConsumerKey('xxxxx'); | |
651 | ||
652 | $em->persist($config); | |
653 | $em->flush(); | |
654 | ||
655 | $this->logInAs('wallace'); | |
656 | $loggedInUserId = $this->getLoggedInUserId(); | |
657 | ||
658 | // create entry to check after user deletion | |
659 | // that this entry is also deleted | |
660 | $crawler = $client->request('GET', '/new'); | |
661 | ||
662 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
663 | ||
664 | $form = $crawler->filter('form[name=entry]')->form(); | |
665 | $data = [ | |
666 | 'entry[url]' => $url = 'https://github.com/wallabag/wallabag', | |
667 | ]; | |
668 | ||
669 | $client->submit($form, $data); | |
670 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | |
671 | ||
672 | $crawler = $client->request('GET', '/config'); | |
673 | ||
674 | $deleteLink = $crawler->filter('.delete-account')->last()->link(); | |
675 | ||
676 | $client->click($deleteLink); | |
677 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | |
678 | ||
679 | $em = $client->getContainer()->get('doctrine.orm.entity_manager'); | |
680 | $user = $em | |
681 | ->getRepository('WallabagUserBundle:User') | |
682 | ->createQueryBuilder('u') | |
683 | ->where('u.username = :username')->setParameter('username', 'wallace') | |
684 | ->getQuery() | |
685 | ->getOneOrNullResult() | |
686 | ; | |
687 | ||
688 | $this->assertNull($user); | |
689 | ||
690 | $entries = $client->getContainer() | |
691 | ->get('doctrine.orm.entity_manager') | |
692 | ->getRepository('WallabagCoreBundle:Entry') | |
693 | ->findByUser($loggedInUserId); | |
694 | ||
695 | $this->assertEmpty($entries); | |
696 | } | |
697 | ||
698 | public function testReset() | |
699 | { | |
700 | $this->logInAs('empty'); | |
701 | $client = $this->getClient(); | |
702 | ||
703 | $em = $client->getContainer()->get('doctrine.orm.entity_manager'); | |
704 | ||
705 | $user = static::$kernel->getContainer()->get('security.token_storage')->getToken()->getUser(); | |
706 | ||
707 | $tag = new Tag(); | |
708 | $tag->setLabel('super'); | |
709 | $em->persist($tag); | |
710 | ||
711 | $entry = new Entry($user); | |
712 | $entry->setUrl('http://www.lemonde.fr/europe/article/2016/10/01/pour-le-psoe-chaque-election-s-est-transformee-en-une-agonie_5006476_3214.html'); | |
713 | $entry->setContent('Youhou'); | |
714 | $entry->setTitle('Youhou'); | |
715 | $entry->addTag($tag); | |
716 | $em->persist($entry); | |
717 | ||
718 | $entry2 = new Entry($user); | |
719 | $entry2->setUrl('http://www.lemonde.de/europe/article/2016/10/01/pour-le-psoe-chaque-election-s-est-transformee-en-une-agonie_5006476_3214.html'); | |
720 | $entry2->setContent('Youhou'); | |
721 | $entry2->setTitle('Youhou'); | |
722 | $entry2->addTag($tag); | |
723 | $em->persist($entry2); | |
724 | ||
725 | $annotation = new Annotation($user); | |
726 | $annotation->setText('annotated'); | |
727 | $annotation->setQuote('annotated'); | |
728 | $annotation->setRanges([]); | |
729 | $annotation->setEntry($entry); | |
730 | $em->persist($annotation); | |
731 | ||
732 | $em->flush(); | |
733 | ||
734 | // reset annotations | |
735 | $crawler = $client->request('GET', '/config#set3'); | |
736 | ||
737 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
738 | ||
739 | $crawler = $client->click($crawler->selectLink('config.reset.annotations')->link()); | |
740 | ||
741 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | |
742 | $this->assertContains('flashes.config.notice.annotations_reset', $client->getContainer()->get('session')->getFlashBag()->get('notice')[0]); | |
743 | ||
744 | $annotationsReset = $em | |
745 | ->getRepository('WallabagAnnotationBundle:Annotation') | |
746 | ->findAnnotationsByPageId($entry->getId(), $user->getId()); | |
747 | ||
748 | $this->assertEmpty($annotationsReset, 'Annotations were reset'); | |
749 | ||
750 | // reset tags | |
751 | $crawler = $client->request('GET', '/config#set3'); | |
752 | ||
753 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
754 | ||
755 | $crawler = $client->click($crawler->selectLink('config.reset.tags')->link()); | |
756 | ||
757 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | |
758 | $this->assertContains('flashes.config.notice.tags_reset', $client->getContainer()->get('session')->getFlashBag()->get('notice')[0]); | |
759 | ||
760 | $tagReset = $em | |
761 | ->getRepository('WallabagCoreBundle:Tag') | |
762 | ->countAllTags($user->getId()); | |
763 | ||
764 | $this->assertEquals(0, $tagReset, 'Tags were reset'); | |
765 | ||
766 | // reset entries | |
767 | $crawler = $client->request('GET', '/config#set3'); | |
768 | ||
769 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
770 | ||
771 | $crawler = $client->click($crawler->selectLink('config.reset.entries')->link()); | |
772 | ||
773 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | |
774 | $this->assertContains('flashes.config.notice.entries_reset', $client->getContainer()->get('session')->getFlashBag()->get('notice')[0]); | |
775 | ||
776 | $entryReset = $em | |
777 | ->getRepository('WallabagCoreBundle:Entry') | |
778 | ->countAllEntriesByUsername($user->getId()); | |
779 | ||
780 | $this->assertEquals(0, $entryReset, 'Entries were reset'); | |
781 | } | |
782 | ||
783 | public function testResetEntriesCascade() | |
784 | { | |
785 | $this->logInAs('empty'); | |
786 | $client = $this->getClient(); | |
787 | ||
788 | $em = $client->getContainer()->get('doctrine.orm.entity_manager'); | |
789 | ||
790 | $user = static::$kernel->getContainer()->get('security.token_storage')->getToken()->getUser(); | |
791 | ||
792 | $tag = new Tag(); | |
793 | $tag->setLabel('super'); | |
794 | $em->persist($tag); | |
795 | ||
796 | $entry = new Entry($user); | |
797 | $entry->setUrl('http://www.lemonde.fr/europe/article/2016/10/01/pour-le-psoe-chaque-election-s-est-transformee-en-une-agonie_5006476_3214.html'); | |
798 | $entry->setContent('Youhou'); | |
799 | $entry->setTitle('Youhou'); | |
800 | $entry->addTag($tag); | |
801 | $em->persist($entry); | |
802 | ||
803 | $annotation = new Annotation($user); | |
804 | $annotation->setText('annotated'); | |
805 | $annotation->setQuote('annotated'); | |
806 | $annotation->setRanges([]); | |
807 | $annotation->setEntry($entry); | |
808 | $em->persist($annotation); | |
809 | ||
810 | $em->flush(); | |
811 | ||
812 | $crawler = $client->request('GET', '/config#set3'); | |
813 | ||
814 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
815 | ||
816 | $crawler = $client->click($crawler->selectLink('config.reset.entries')->link()); | |
817 | ||
818 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | |
819 | $this->assertContains('flashes.config.notice.entries_reset', $client->getContainer()->get('session')->getFlashBag()->get('notice')[0]); | |
820 | ||
821 | $entryReset = $em | |
822 | ->getRepository('WallabagCoreBundle:Entry') | |
823 | ->countAllEntriesByUsername($user->getId()); | |
824 | ||
825 | $this->assertEquals(0, $entryReset, 'Entries were reset'); | |
826 | ||
827 | $tagReset = $em | |
828 | ->getRepository('WallabagCoreBundle:Tag') | |
829 | ->countAllTags($user->getId()); | |
830 | ||
831 | $this->assertEquals(0, $tagReset, 'Tags were reset'); | |
832 | ||
833 | $annotationsReset = $em | |
834 | ->getRepository('WallabagAnnotationBundle:Annotation') | |
835 | ->findAnnotationsByPageId($entry->getId(), $user->getId()); | |
836 | ||
837 | $this->assertEmpty($annotationsReset, 'Annotations were reset'); | |
838 | } | |
839 | } |