]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php
Crypt site credential password
[github/wallabag/wallabag.git] / tests / Wallabag / CoreBundle / Controller / EntryControllerTest.php
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\CoreBundle\Entity\Entry;
8 use Wallabag\CoreBundle\Entity\SiteCredential;
9
10 class EntryControllerTest extends WallabagCoreTestCase
11 {
12 public $downloadImagesEnabled = false;
13 public $url = 'http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html';
14
15 /**
16 * @after
17 *
18 * Ensure download_images_enabled is disabled after each script
19 */
20 public function tearDownImagesEnabled()
21 {
22 if ($this->downloadImagesEnabled) {
23 $client = static::createClient();
24 $client->getContainer()->get('craue_config')->set('download_images_enabled', 0);
25
26 $this->downloadImagesEnabled = false;
27 }
28 }
29
30 public function testLogin()
31 {
32 $client = $this->getClient();
33
34 $client->request('GET', '/new');
35
36 $this->assertEquals(302, $client->getResponse()->getStatusCode());
37 $this->assertContains('login', $client->getResponse()->headers->get('location'));
38 }
39
40 public function testQuickstart()
41 {
42 $this->logInAs('empty');
43 $client = $this->getClient();
44
45 $client->request('GET', '/unread/list');
46 $crawler = $client->followRedirect();
47
48 $this->assertEquals(200, $client->getResponse()->getStatusCode());
49 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
50 $this->assertContains('quickstart.intro.title', $body[0]);
51
52 // Test if quickstart is disabled when user has 1 entry
53 $crawler = $client->request('GET', '/new');
54
55 $this->assertEquals(200, $client->getResponse()->getStatusCode());
56
57 $form = $crawler->filter('form[name=entry]')->form();
58
59 $data = [
60 'entry[url]' => $this->url,
61 ];
62
63 $client->submit($form, $data);
64 $this->assertEquals(302, $client->getResponse()->getStatusCode());
65 $client->followRedirect();
66
67 $crawler = $client->request('GET', '/unread/list');
68 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
69 $this->assertContains('entry.list.number_on_the_page', $body[0]);
70 }
71
72 public function testGetNew()
73 {
74 $this->logInAs('admin');
75 $this->useTheme('baggy');
76 $client = $this->getClient();
77
78 $crawler = $client->request('GET', '/new');
79
80 $this->assertEquals(200, $client->getResponse()->getStatusCode());
81
82 $this->assertCount(1, $crawler->filter('input[type=url]'));
83 $this->assertCount(1, $crawler->filter('form[name=entry]'));
84 }
85
86 public function testPostNewViaBookmarklet()
87 {
88 $this->logInAs('admin');
89 $this->useTheme('baggy');
90 $client = $this->getClient();
91
92 $crawler = $client->request('GET', '/');
93
94 $this->assertCount(4, $crawler->filter('div[class=entry]'));
95
96 // Good URL
97 $client->request('GET', '/bookmarklet', ['url' => $this->url]);
98 $this->assertEquals(302, $client->getResponse()->getStatusCode());
99 $client->followRedirect();
100 $crawler = $client->request('GET', '/');
101 $this->assertCount(5, $crawler->filter('div[class=entry]'));
102
103 $em = $client->getContainer()
104 ->get('doctrine.orm.entity_manager');
105 $entry = $em
106 ->getRepository('WallabagCoreBundle:Entry')
107 ->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
108 $em->remove($entry);
109 $em->flush();
110 }
111
112 public function testPostNewEmpty()
113 {
114 $this->logInAs('admin');
115 $client = $this->getClient();
116
117 $crawler = $client->request('GET', '/new');
118
119 $this->assertEquals(200, $client->getResponse()->getStatusCode());
120
121 $form = $crawler->filter('form[name=entry]')->form();
122
123 $crawler = $client->submit($form);
124
125 $this->assertEquals(200, $client->getResponse()->getStatusCode());
126 $this->assertCount(1, $alert = $crawler->filter('form ul li')->extract(['_text']));
127 $this->assertEquals('This value should not be blank.', $alert[0]);
128 }
129
130 /**
131 * This test will require an internet connection.
132 */
133 public function testPostNewOk()
134 {
135 $this->logInAs('admin');
136 $client = $this->getClient();
137
138 $crawler = $client->request('GET', '/new');
139
140 $this->assertEquals(200, $client->getResponse()->getStatusCode());
141
142 $form = $crawler->filter('form[name=entry]')->form();
143
144 $data = [
145 'entry[url]' => $this->url,
146 ];
147
148 $client->submit($form, $data);
149
150 $this->assertEquals(302, $client->getResponse()->getStatusCode());
151
152 $content = $client->getContainer()
153 ->get('doctrine.orm.entity_manager')
154 ->getRepository('WallabagCoreBundle:Entry')
155 ->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
156
157 $author = $content->getPublishedBy();
158
159 $this->assertInstanceOf('Wallabag\CoreBundle\Entity\Entry', $content);
160 $this->assertEquals($this->url, $content->getUrl());
161 $this->assertContains('Google', $content->getTitle());
162 $this->assertEquals('fr', $content->getLanguage());
163 $this->assertEquals('2015-03-28 15:37:39', $content->getPublishedAt()->format('Y-m-d H:i:s'));
164 $this->assertEquals('Morgane Tual', $author[0]);
165 $this->assertArrayHasKey('x-varnish1', $content->getHeaders());
166 }
167
168 public function testPostWithMultipleAuthors()
169 {
170 $url = 'http://www.liberation.fr/planete/2017/04/05/donald-trump-et-xi-jinping-tentative-de-flirt-en-floride_1560768';
171 $this->logInAs('admin');
172 $client = $this->getClient();
173
174 $crawler = $client->request('GET', '/new');
175
176 $this->assertEquals(200, $client->getResponse()->getStatusCode());
177
178 $form = $crawler->filter('form[name=entry]')->form();
179
180 $data = [
181 'entry[url]' => $url,
182 ];
183
184 $client->submit($form, $data);
185
186 $this->assertEquals(302, $client->getResponse()->getStatusCode());
187
188 $content = $client->getContainer()
189 ->get('doctrine.orm.entity_manager')
190 ->getRepository('WallabagCoreBundle:Entry')
191 ->findByUrlAndUserId($url, $this->getLoggedInUserId());
192
193 $authors = $content->getPublishedBy();
194 $this->assertEquals('2017-04-05 19:26:13', $content->getPublishedAt()->format('Y-m-d H:i:s'));
195 $this->assertEquals('fr', $content->getLanguage());
196 $this->assertEquals('Raphaël Balenieri, correspondant à Pékin', $authors[0]);
197 $this->assertEquals('Frédéric Autran, correspondant à New York', $authors[1]);
198 }
199
200 public function testPostNewOkUrlExist()
201 {
202 $this->logInAs('admin');
203
204 $entry = new Entry($this->getLoggedInUser());
205 $entry->setUrl($this->url);
206 $this->getEntityManager()->persist($entry);
207 $this->getEntityManager()->flush();
208
209 $client = $this->getClient();
210
211 $crawler = $client->request('GET', '/new');
212
213 $this->assertEquals(200, $client->getResponse()->getStatusCode());
214
215 $form = $crawler->filter('form[name=entry]')->form();
216
217 $data = [
218 'entry[url]' => $this->url,
219 ];
220
221 $client->submit($form, $data);
222
223 $this->assertEquals(302, $client->getResponse()->getStatusCode());
224 $this->assertContains('/view/', $client->getResponse()->getTargetUrl());
225 }
226
227 public function testPostNewOkUrlExistWithAccent()
228 {
229 $this->logInAs('admin');
230 $client = $this->getClient();
231
232 $url = 'http://www.aritylabs.com/post/106091708292/des-contr%C3%B4leurs-optionnels-gr%C3%A2ce-%C3%A0-constmissing';
233
234 $crawler = $client->request('GET', '/new');
235
236 $this->assertEquals(200, $client->getResponse()->getStatusCode());
237
238 $form = $crawler->filter('form[name=entry]')->form();
239
240 $data = [
241 'entry[url]' => $url,
242 ];
243
244 $client->submit($form, $data);
245
246 $crawler = $client->request('GET', '/new');
247
248 $this->assertEquals(200, $client->getResponse()->getStatusCode());
249
250 $form = $crawler->filter('form[name=entry]')->form();
251
252 $data = [
253 'entry[url]' => $url,
254 ];
255
256 $client->submit($form, $data);
257
258 $this->assertEquals(302, $client->getResponse()->getStatusCode());
259 $this->assertContains('/view/', $client->getResponse()->getTargetUrl());
260 }
261
262 /**
263 * This test will require an internet connection.
264 */
265 public function testPostNewThatWillBeTagged()
266 {
267 $this->logInAs('admin');
268 $client = $this->getClient();
269
270 $crawler = $client->request('GET', '/new');
271
272 $this->assertEquals(200, $client->getResponse()->getStatusCode());
273
274 $form = $crawler->filter('form[name=entry]')->form();
275
276 $data = [
277 'entry[url]' => $url = 'https://github.com/wallabag/wallabag',
278 ];
279
280 $client->submit($form, $data);
281
282 $this->assertEquals(302, $client->getResponse()->getStatusCode());
283 $this->assertContains('/', $client->getResponse()->getTargetUrl());
284
285 $em = $client->getContainer()
286 ->get('doctrine.orm.entity_manager');
287 $entry = $em
288 ->getRepository('WallabagCoreBundle:Entry')
289 ->findOneByUrl($url);
290 $tags = $entry->getTags();
291
292 $this->assertCount(2, $tags);
293 $this->assertContains('wallabag', $tags);
294 $this->assertEquals('en', $entry->getLanguage());
295
296 $em->remove($entry);
297 $em->flush();
298
299 // and now re-submit it to test the cascade persistence for tags after entry removal
300 // related https://github.com/wallabag/wallabag/issues/2121
301 $crawler = $client->request('GET', '/new');
302
303 $this->assertEquals(200, $client->getResponse()->getStatusCode());
304
305 $form = $crawler->filter('form[name=entry]')->form();
306
307 $data = [
308 'entry[url]' => $url = 'https://github.com/wallabag/wallabag/tree/master',
309 ];
310
311 $client->submit($form, $data);
312
313 $this->assertEquals(302, $client->getResponse()->getStatusCode());
314 $this->assertContains('/', $client->getResponse()->getTargetUrl());
315
316 $entry = $em
317 ->getRepository('WallabagCoreBundle:Entry')
318 ->findOneByUrl($url);
319
320 $tags = $entry->getTags();
321
322 $this->assertCount(2, $tags);
323 $this->assertContains('wallabag', $tags);
324
325 $em->remove($entry);
326 $em->flush();
327 }
328
329 public function testArchive()
330 {
331 $this->logInAs('admin');
332 $client = $this->getClient();
333
334 $client->request('GET', '/archive/list');
335
336 $this->assertEquals(200, $client->getResponse()->getStatusCode());
337 }
338
339 public function testUntagged()
340 {
341 $this->logInAs('admin');
342 $client = $this->getClient();
343
344 $client->request('GET', '/untagged/list');
345
346 $this->assertEquals(200, $client->getResponse()->getStatusCode());
347 }
348
349 public function testStarred()
350 {
351 $this->logInAs('admin');
352 $client = $this->getClient();
353
354 $client->request('GET', '/starred/list');
355
356 $this->assertEquals(200, $client->getResponse()->getStatusCode());
357 }
358
359 public function testRangeException()
360 {
361 $this->logInAs('admin');
362 $client = $this->getClient();
363
364 $client->request('GET', '/all/list/900');
365
366 $this->assertEquals(302, $client->getResponse()->getStatusCode());
367 $this->assertEquals('/all/list', $client->getResponse()->getTargetUrl());
368 }
369
370 public function testView()
371 {
372 $this->logInAs('admin');
373 $client = $this->getClient();
374
375 $entry = new Entry($this->getLoggedInUser());
376 $entry->setUrl('http://example.com/foo');
377 $entry->setTitle('title foo');
378 $entry->setContent('foo bar baz');
379 $this->getEntityManager()->persist($entry);
380 $this->getEntityManager()->flush();
381
382 $crawler = $client->request('GET', '/view/'.$entry->getId());
383
384 $this->assertEquals(200, $client->getResponse()->getStatusCode());
385 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
386 $this->assertContains($entry->getTitle(), $body[0]);
387 }
388
389 /**
390 * This test will require an internet connection.
391 */
392 public function testReload()
393 {
394 $this->logInAs('admin');
395 $client = $this->getClient();
396
397 $entry = new Entry($this->getLoggedInUser());
398 $entry->setUrl($this->url);
399 $entry->setTitle('title foo');
400 $entry->setContent('');
401 $this->getEntityManager()->persist($entry);
402 $this->getEntityManager()->flush();
403 $this->getEntityManager()->clear();
404
405 $client->request('GET', '/reload/'.$entry->getId());
406
407 $this->assertEquals(302, $client->getResponse()->getStatusCode());
408
409 $entry = $this->getEntityManager()
410 ->getRepository('WallabagCoreBundle:Entry')
411 ->find($entry->getId());
412
413 $this->assertNotEmpty($entry->getContent());
414 }
415
416 public function testReloadWithFetchingFailed()
417 {
418 $this->logInAs('admin');
419 $client = $this->getClient();
420
421 $entry = new Entry($this->getLoggedInUser());
422 $entry->setUrl('http://0.0.0.0/failed.html');
423 $this->getEntityManager()->persist($entry);
424 $this->getEntityManager()->flush();
425
426 $client->request('GET', '/reload/'.$entry->getId());
427
428 $this->assertEquals(302, $client->getResponse()->getStatusCode());
429
430 // force EntityManager to clear previous entity
431 // otherwise, retrieve the same entity will retrieve change from the previous request :0
432 $this->getEntityManager()->clear();
433 $newContent = $this->getEntityManager()
434 ->getRepository('WallabagCoreBundle:Entry')
435 ->find($entry->getId());
436
437 $this->assertNotEquals($client->getContainer()->getParameter('wallabag_core.fetching_error_message'), $newContent->getContent());
438 }
439
440 public function testEdit()
441 {
442 $this->logInAs('admin');
443 $client = $this->getClient();
444
445 $entry = new Entry($this->getLoggedInUser());
446 $entry->setUrl($this->url);
447 $this->getEntityManager()->persist($entry);
448 $this->getEntityManager()->flush();
449
450 $crawler = $client->request('GET', '/edit/'.$entry->getId());
451
452 $this->assertEquals(200, $client->getResponse()->getStatusCode());
453
454 $this->assertCount(1, $crawler->filter('input[id=entry_title]'));
455 $this->assertCount(1, $crawler->filter('button[id=entry_save]'));
456 }
457
458 public function testEditUpdate()
459 {
460 $this->logInAs('admin');
461 $client = $this->getClient();
462
463 $entry = new Entry($this->getLoggedInUser());
464 $entry->setUrl($this->url);
465 $this->getEntityManager()->persist($entry);
466 $this->getEntityManager()->flush();
467
468 $crawler = $client->request('GET', '/edit/'.$entry->getId());
469
470 $this->assertEquals(200, $client->getResponse()->getStatusCode());
471
472 $form = $crawler->filter('button[type=submit]')->form();
473
474 $data = [
475 'entry[title]' => 'My updated title hehe :)',
476 ];
477
478 $client->submit($form, $data);
479
480 $this->assertEquals(302, $client->getResponse()->getStatusCode());
481
482 $crawler = $client->followRedirect();
483
484 $this->assertGreaterThan(1, $alert = $crawler->filter('div[id=article] h1')->extract(['_text']));
485 $this->assertContains('My updated title hehe :)', $alert[0]);
486 }
487
488 public function testToggleArchive()
489 {
490 $this->logInAs('admin');
491 $client = $this->getClient();
492
493 $entry = new Entry($this->getLoggedInUser());
494 $entry->setUrl($this->url);
495 $this->getEntityManager()->persist($entry);
496 $this->getEntityManager()->flush();
497 $this->getEntityManager()->clear();
498
499 $client->request('GET', '/archive/'.$entry->getId());
500
501 $this->assertEquals(302, $client->getResponse()->getStatusCode());
502
503 $res = $client->getContainer()
504 ->get('doctrine.orm.entity_manager')
505 ->getRepository('WallabagCoreBundle:Entry')
506 ->find($entry->getId());
507
508 $this->assertEquals($res->isArchived(), true);
509 }
510
511 public function testToggleStar()
512 {
513 $this->logInAs('admin');
514 $client = $this->getClient();
515
516 $entry = new Entry($this->getLoggedInUser());
517 $entry->setUrl($this->url);
518 $this->getEntityManager()->persist($entry);
519 $this->getEntityManager()->flush();
520 $this->getEntityManager()->clear();
521
522 $client->request('GET', '/star/'.$entry->getId());
523
524 $this->assertEquals(302, $client->getResponse()->getStatusCode());
525
526 $res = $client->getContainer()
527 ->get('doctrine.orm.entity_manager')
528 ->getRepository('WallabagCoreBundle:Entry')
529 ->findOneById($entry->getId());
530
531 $this->assertEquals($res->isStarred(), true);
532 }
533
534 public function testDelete()
535 {
536 $this->logInAs('admin');
537 $client = $this->getClient();
538
539 $entry = new Entry($this->getLoggedInUser());
540 $entry->setUrl($this->url);
541 $this->getEntityManager()->persist($entry);
542 $this->getEntityManager()->flush();
543
544 $client->request('GET', '/delete/'.$entry->getId());
545
546 $this->assertEquals(302, $client->getResponse()->getStatusCode());
547
548 $client->request('GET', '/delete/'.$entry->getId());
549
550 $this->assertEquals(404, $client->getResponse()->getStatusCode());
551 }
552
553 /**
554 * It will create a new entry.
555 * Browse to it.
556 * Then remove it.
557 *
558 * And it'll check that user won't be redirected to the view page of the content when it had been removed
559 */
560 public function testViewAndDelete()
561 {
562 $this->logInAs('admin');
563 $client = $this->getClient();
564
565 $em = $client->getContainer()
566 ->get('doctrine.orm.entity_manager');
567
568 // add a new content to be removed later
569 $user = $em
570 ->getRepository('WallabagUserBundle:User')
571 ->findOneByUserName('admin');
572
573 $content = new Entry($user);
574 $content->setUrl('http://1.1.1.1/entry');
575 $content->setReadingTime(12);
576 $content->setDomainName('domain.io');
577 $content->setMimetype('text/html');
578 $content->setTitle('test title entry');
579 $content->setContent('This is my content /o/');
580 $content->setArchived(true);
581 $content->setLanguage('fr');
582
583 $em->persist($content);
584 $em->flush();
585
586 $client->request('GET', '/view/'.$content->getId());
587 $this->assertEquals(200, $client->getResponse()->getStatusCode());
588
589 $client->request('GET', '/delete/'.$content->getId());
590 $this->assertEquals(302, $client->getResponse()->getStatusCode());
591
592 $client->followRedirect();
593 $this->assertEquals(200, $client->getResponse()->getStatusCode());
594 }
595
596 public function testViewOtherUserEntry()
597 {
598 $this->logInAs('admin');
599 $client = $this->getClient();
600
601 $content = $client->getContainer()
602 ->get('doctrine.orm.entity_manager')
603 ->getRepository('WallabagCoreBundle:Entry')
604 ->findOneByUsernameAndNotArchived('bob');
605
606 $client->request('GET', '/view/'.$content->getId());
607
608 $this->assertEquals(403, $client->getResponse()->getStatusCode());
609 }
610
611 public function testFilterOnReadingTime()
612 {
613 $this->logInAs('admin');
614 $this->useTheme('baggy');
615 $client = $this->getClient();
616 $entry = new Entry($this->getLoggedInUser());
617 $entry->setUrl($this->url);
618 $entry->setReadingTime(22);
619 $this->getEntityManager()->persist($entry);
620 $this->getEntityManager()->flush();
621
622 $crawler = $client->request('GET', '/unread/list');
623
624 $form = $crawler->filter('button[id=submit-filter]')->form();
625
626 $data = [
627 'entry_filter[readingTime][right_number]' => 22,
628 'entry_filter[readingTime][left_number]' => 22,
629 ];
630
631 $crawler = $client->submit($form, $data);
632
633 $this->assertCount(1, $crawler->filter('div[class=entry]'));
634 }
635
636 public function testFilterOnReadingTimeWithNegativeValue()
637 {
638 $this->logInAs('admin');
639 $client = $this->getClient();
640
641 $crawler = $client->request('GET', '/unread/list');
642
643 $form = $crawler->filter('button[id=submit-filter]')->form();
644
645 $data = [
646 'entry_filter[readingTime][right_number]' => -22,
647 'entry_filter[readingTime][left_number]' => -22,
648 ];
649
650 $crawler = $client->submit($form, $data);
651
652 // forcing negative value results in no entry displayed
653 $this->assertCount(0, $crawler->filter('div[class=entry]'));
654 }
655
656 public function testFilterOnReadingTimeOnlyUpper()
657 {
658 $this->logInAs('admin');
659 $this->useTheme('baggy');
660 $client = $this->getClient();
661
662 $crawler = $client->request('GET', '/all/list');
663 $this->assertCount(5, $crawler->filter('div[class=entry]'));
664
665 $entry = new Entry($this->getLoggedInUser());
666 $entry->setUrl($this->url);
667 $entry->setReadingTime(23);
668 $this->getEntityManager()->persist($entry);
669 $this->getEntityManager()->flush();
670
671 $crawler = $client->request('GET', '/all/list');
672 $this->assertCount(6, $crawler->filter('div[class=entry]'));
673
674 $form = $crawler->filter('button[id=submit-filter]')->form();
675
676 $data = [
677 'entry_filter[readingTime][right_number]' => 22,
678 ];
679
680 $crawler = $client->submit($form, $data);
681
682 $this->assertCount(5, $crawler->filter('div[class=entry]'));
683 }
684
685 public function testFilterOnReadingTimeOnlyLower()
686 {
687 $this->logInAs('admin');
688 $this->useTheme('baggy');
689 $client = $this->getClient();
690
691 $crawler = $client->request('GET', '/unread/list');
692
693 $form = $crawler->filter('button[id=submit-filter]')->form();
694
695 $data = [
696 'entry_filter[readingTime][left_number]' => 22,
697 ];
698
699 $crawler = $client->submit($form, $data);
700
701 $this->assertCount(0, $crawler->filter('div[class=entry]'));
702
703 $entry = new Entry($this->getLoggedInUser());
704 $entry->setUrl($this->url);
705 $entry->setReadingTime(23);
706 $this->getEntityManager()->persist($entry);
707 $this->getEntityManager()->flush();
708
709 $crawler = $client->submit($form, $data);
710 $this->assertCount(1, $crawler->filter('div[class=entry]'));
711 }
712
713 public function testFilterOnUnreadStatus()
714 {
715 $this->logInAs('admin');
716 $this->useTheme('baggy');
717 $client = $this->getClient();
718
719 $crawler = $client->request('GET', '/all/list');
720
721 $form = $crawler->filter('button[id=submit-filter]')->form();
722
723 $data = [
724 'entry_filter[isUnread]' => true,
725 ];
726
727 $crawler = $client->submit($form, $data);
728
729 $this->assertCount(4, $crawler->filter('div[class=entry]'));
730
731 $entry = new Entry($this->getLoggedInUser());
732 $entry->setUrl($this->url);
733 $entry->setArchived(false);
734 $this->getEntityManager()->persist($entry);
735 $this->getEntityManager()->flush();
736
737 $crawler = $client->submit($form, $data);
738
739 $this->assertCount(5, $crawler->filter('div[class=entry]'));
740 }
741
742 public function testFilterOnCreationDate()
743 {
744 $this->logInAs('admin');
745 $this->useTheme('baggy');
746 $client = $this->getClient();
747
748 $crawler = $client->request('GET', '/unread/list');
749
750 $form = $crawler->filter('button[id=submit-filter]')->form();
751
752 $data = [
753 'entry_filter[createdAt][left_date]' => date('d/m/Y'),
754 'entry_filter[createdAt][right_date]' => date('d/m/Y', strtotime('+1 day')),
755 ];
756
757 $crawler = $client->submit($form, $data);
758
759 $this->assertCount(5, $crawler->filter('div[class=entry]'));
760
761 $data = [
762 'entry_filter[createdAt][left_date]' => date('d/m/Y'),
763 'entry_filter[createdAt][right_date]' => date('d/m/Y'),
764 ];
765
766 $crawler = $client->submit($form, $data);
767
768 $this->assertCount(5, $crawler->filter('div[class=entry]'));
769
770 $data = [
771 'entry_filter[createdAt][left_date]' => '01/01/1970',
772 'entry_filter[createdAt][right_date]' => '01/01/1970',
773 ];
774
775 $crawler = $client->submit($form, $data);
776
777 $this->assertCount(0, $crawler->filter('div[class=entry]'));
778 }
779
780 public function testPaginationWithFilter()
781 {
782 $this->logInAs('admin');
783 $client = $this->getClient();
784 $crawler = $client->request('GET', '/config');
785
786 $form = $crawler->filter('button[id=config_save]')->form();
787
788 $data = [
789 'config[items_per_page]' => '1',
790 ];
791
792 $client->submit($form, $data);
793
794 $parameters = '?entry_filter%5BreadingTime%5D%5Bleft_number%5D=&entry_filter%5BreadingTime%5D%5Bright_number%5D=';
795
796 $client->request('GET', 'unread/list'.$parameters);
797
798 $this->assertContains($parameters, $client->getResponse()->getContent());
799
800 // reset pagination
801 $crawler = $client->request('GET', '/config');
802 $form = $crawler->filter('button[id=config_save]')->form();
803 $data = [
804 'config[items_per_page]' => '12',
805 ];
806 $client->submit($form, $data);
807 }
808
809 public function testFilterOnDomainName()
810 {
811 $this->logInAs('admin');
812 $this->useTheme('baggy');
813 $client = $this->getClient();
814
815 $crawler = $client->request('GET', '/unread/list');
816 $form = $crawler->filter('button[id=submit-filter]')->form();
817 $data = [
818 'entry_filter[domainName]' => 'domain',
819 ];
820
821 $crawler = $client->submit($form, $data);
822 $this->assertCount(5, $crawler->filter('div[class=entry]'));
823
824 $crawler = $client->request('GET', '/unread/list');
825 $form = $crawler->filter('button[id=submit-filter]')->form();
826 $data = [
827 'entry_filter[domainName]' => 'dOmain',
828 ];
829
830 $crawler = $client->submit($form, $data);
831 $this->assertCount(5, $crawler->filter('div[class=entry]'));
832
833 $form = $crawler->filter('button[id=submit-filter]')->form();
834 $data = [
835 'entry_filter[domainName]' => 'wallabag',
836 ];
837
838 $crawler = $client->submit($form, $data);
839 $this->assertCount(0, $crawler->filter('div[class=entry]'));
840 }
841
842 public function testFilterOnStatus()
843 {
844 $this->logInAs('admin');
845 $this->useTheme('baggy');
846 $client = $this->getClient();
847
848 $crawler = $client->request('GET', '/unread/list');
849 $form = $crawler->filter('button[id=submit-filter]')->form();
850 $form['entry_filter[isArchived]']->tick();
851 $form['entry_filter[isStarred]']->untick();
852
853 $crawler = $client->submit($form);
854 $this->assertCount(1, $crawler->filter('div[class=entry]'));
855
856 $form = $crawler->filter('button[id=submit-filter]')->form();
857 $form['entry_filter[isArchived]']->untick();
858 $form['entry_filter[isStarred]']->tick();
859
860 $crawler = $client->submit($form);
861 $this->assertCount(1, $crawler->filter('div[class=entry]'));
862 }
863
864 public function testPreviewPictureFilter()
865 {
866 $this->logInAs('admin');
867 $this->useTheme('baggy');
868 $client = $this->getClient();
869
870 $crawler = $client->request('GET', '/unread/list');
871 $form = $crawler->filter('button[id=submit-filter]')->form();
872 $form['entry_filter[previewPicture]']->tick();
873
874 $crawler = $client->submit($form);
875 $this->assertCount(1, $crawler->filter('div[class=entry]'));
876 }
877
878 public function testFilterOnLanguage()
879 {
880 $this->logInAs('admin');
881 $this->useTheme('baggy');
882 $client = $this->getClient();
883
884 $entry = new Entry($this->getLoggedInUser());
885 $entry->setUrl($this->url);
886 $entry->setLanguage('fr');
887 $this->getEntityManager()->persist($entry);
888 $this->getEntityManager()->flush();
889
890 $crawler = $client->request('GET', '/unread/list');
891 $form = $crawler->filter('button[id=submit-filter]')->form();
892 $data = [
893 'entry_filter[language]' => 'fr',
894 ];
895
896 $crawler = $client->submit($form, $data);
897 $this->assertCount(3, $crawler->filter('div[class=entry]'));
898
899 $form = $crawler->filter('button[id=submit-filter]')->form();
900 $data = [
901 'entry_filter[language]' => 'en',
902 ];
903
904 $crawler = $client->submit($form, $data);
905 $this->assertCount(2, $crawler->filter('div[class=entry]'));
906 }
907
908 public function testShareEntryPublicly()
909 {
910 $this->logInAs('admin');
911 $client = $this->getClient();
912
913 // sharing is enabled
914 $client->getContainer()->get('craue_config')->set('share_public', 1);
915
916 $content = new Entry($this->getLoggedInUser());
917 $content->setUrl($this->url);
918 $this->getEntityManager()->persist($content);
919 $this->getEntityManager()->flush();
920 $this->getEntityManager()->clear();
921
922 // no uid
923 $client->request('GET', '/share/'.$content->getUid());
924 $this->assertEquals(404, $client->getResponse()->getStatusCode());
925
926 // generating the uid
927 $client->request('GET', '/share/'.$content->getId());
928 $this->assertEquals(302, $client->getResponse()->getStatusCode());
929
930 // follow link with uid
931 $crawler = $client->followRedirect();
932 $this->assertEquals(200, $client->getResponse()->getStatusCode());
933 $this->assertContains('max-age=25200', $client->getResponse()->headers->get('cache-control'));
934 $this->assertContains('public', $client->getResponse()->headers->get('cache-control'));
935 $this->assertContains('s-maxage=25200', $client->getResponse()->headers->get('cache-control'));
936 $this->assertNotContains('no-cache', $client->getResponse()->headers->get('cache-control'));
937 $this->assertContains('og:title', $client->getResponse()->getContent());
938 $this->assertContains('og:type', $client->getResponse()->getContent());
939 $this->assertContains('og:url', $client->getResponse()->getContent());
940 $this->assertContains('og:image', $client->getResponse()->getContent());
941
942 // sharing is now disabled
943 $client->getContainer()->get('craue_config')->set('share_public', 0);
944 $client->request('GET', '/share/'.$content->getUid());
945 $this->assertEquals(404, $client->getResponse()->getStatusCode());
946
947 $client->request('GET', '/view/'.$content->getId());
948 $this->assertContains('no-cache', $client->getResponse()->headers->get('cache-control'));
949
950 // removing the share
951 $client->request('GET', '/share/delete/'.$content->getId());
952 $this->assertEquals(302, $client->getResponse()->getStatusCode());
953
954 // share is now disable
955 $client->request('GET', '/share/'.$content->getUid());
956 $this->assertEquals(404, $client->getResponse()->getStatusCode());
957 }
958
959 public function testNewEntryWithDownloadImagesEnabled()
960 {
961 $this->downloadImagesEnabled = true;
962 $this->logInAs('admin');
963 $client = $this->getClient();
964
965 $url = 'http://www.20minutes.fr/montpellier/1952003-20161030-video-car-tombe-panne-rugbymen-perpignan-improvisent-melee-route';
966 $client->getContainer()->get('craue_config')->set('download_images_enabled', 1);
967
968 $crawler = $client->request('GET', '/new');
969
970 $this->assertEquals(200, $client->getResponse()->getStatusCode());
971
972 $form = $crawler->filter('form[name=entry]')->form();
973
974 $data = [
975 'entry[url]' => $url,
976 ];
977
978 $client->submit($form, $data);
979
980 $this->assertEquals(302, $client->getResponse()->getStatusCode());
981
982 $em = $client->getContainer()
983 ->get('doctrine.orm.entity_manager');
984
985 $entry = $em
986 ->getRepository('WallabagCoreBundle:Entry')
987 ->findByUrlAndUserId($url, $this->getLoggedInUserId());
988
989 $this->assertInstanceOf('Wallabag\CoreBundle\Entity\Entry', $entry);
990 $this->assertEquals($url, $entry->getUrl());
991 $this->assertContains('Perpignan', $entry->getTitle());
992 // instead of checking for the filename (which might change) check that the image is now local
993 $this->assertContains('https://your-wallabag-url-instance.com/assets/images/', $entry->getContent());
994
995 $client->getContainer()->get('craue_config')->set('download_images_enabled', 0);
996 }
997
998 /**
999 * @depends testNewEntryWithDownloadImagesEnabled
1000 */
1001 public function testRemoveEntryWithDownloadImagesEnabled()
1002 {
1003 $this->downloadImagesEnabled = true;
1004 $this->logInAs('admin');
1005 $client = $this->getClient();
1006
1007 $url = 'http://www.20minutes.fr/montpellier/1952003-20161030-video-car-tombe-panne-rugbymen-perpignan-improvisent-melee-route';
1008 $client->getContainer()->get('craue_config')->set('download_images_enabled', 1);
1009
1010 $crawler = $client->request('GET', '/new');
1011
1012 $this->assertEquals(200, $client->getResponse()->getStatusCode());
1013
1014 $form = $crawler->filter('form[name=entry]')->form();
1015
1016 $data = [
1017 'entry[url]' => $url,
1018 ];
1019
1020 $client->submit($form, $data);
1021
1022 $this->assertEquals(302, $client->getResponse()->getStatusCode());
1023
1024 $content = $client->getContainer()
1025 ->get('doctrine.orm.entity_manager')
1026 ->getRepository('WallabagCoreBundle:Entry')
1027 ->findByUrlAndUserId($url, $this->getLoggedInUserId());
1028
1029 $client->request('GET', '/delete/'.$content->getId());
1030
1031 $this->assertEquals(302, $client->getResponse()->getStatusCode());
1032
1033 $client->getContainer()->get('craue_config')->set('download_images_enabled', 0);
1034 }
1035
1036 public function testRedirectToHomepage()
1037 {
1038 $this->logInAs('empty');
1039 $client = $this->getClient();
1040
1041 // Redirect to homepage
1042 $config = $this->getLoggedInUser()->getConfig();
1043 $config->setActionMarkAsRead(Config::REDIRECT_TO_HOMEPAGE);
1044 $this->getEntityManager()->persist($config);
1045
1046 $entry = new Entry($this->getLoggedInUser());
1047 $entry->setUrl($this->url);
1048 $this->getEntityManager()->persist($entry);
1049
1050 $this->getEntityManager()->flush();
1051
1052 $client->request('GET', '/view/'.$entry->getId());
1053 $client->request('GET', '/archive/'.$entry->getId());
1054
1055 $this->assertEquals(302, $client->getResponse()->getStatusCode());
1056 $this->assertEquals('/', $client->getResponse()->headers->get('location'));
1057 }
1058
1059 public function testRedirectToCurrentPage()
1060 {
1061 $this->logInAs('empty');
1062 $client = $this->getClient();
1063
1064 // Redirect to current page
1065 $config = $this->getLoggedInUser()->getConfig();
1066 $config->setActionMarkAsRead(Config::REDIRECT_TO_CURRENT_PAGE);
1067 $this->getEntityManager()->persist($config);
1068
1069 $entry = new Entry($this->getLoggedInUser());
1070 $entry->setUrl($this->url);
1071 $this->getEntityManager()->persist($entry);
1072
1073 $this->getEntityManager()->flush();
1074
1075 $client->request('GET', '/view/'.$entry->getId());
1076 $client->request('GET', '/archive/'.$entry->getId());
1077
1078 $this->assertEquals(302, $client->getResponse()->getStatusCode());
1079 $this->assertContains('/view/'.$entry->getId(), $client->getResponse()->headers->get('location'));
1080 }
1081
1082 public function testFilterOnHttpStatus()
1083 {
1084 $this->logInAs('admin');
1085 $this->useTheme('baggy');
1086 $client = $this->getClient();
1087
1088 $entry = new Entry($this->getLoggedInUser());
1089 $entry->setUrl('http://www.lemonde.fr/incorrect-url/');
1090 $entry->setHttpStatus(404);
1091 $this->getEntityManager()->persist($entry);
1092
1093 $this->getEntityManager()->flush();
1094
1095 $crawler = $client->request('GET', '/all/list');
1096 $form = $crawler->filter('button[id=submit-filter]')->form();
1097
1098 $data = [
1099 'entry_filter[httpStatus]' => 404,
1100 ];
1101
1102 $crawler = $client->submit($form, $data);
1103
1104 $this->assertCount(1, $crawler->filter('div[class=entry]'));
1105
1106 $entry = new Entry($this->getLoggedInUser());
1107 $entry->setUrl($this->url);
1108 $entry->setHttpStatus(200);
1109 $this->getEntityManager()->persist($entry);
1110
1111 $entry = new Entry($this->getLoggedInUser());
1112 $entry->setUrl('http://www.nextinpact.com/news/101235-wallabag-alternative-libre-a-pocket-creuse-petit-a-petit-son-nid.htm');
1113 $entry->setHttpStatus(200);
1114 $this->getEntityManager()->persist($entry);
1115
1116 $this->getEntityManager()->flush();
1117
1118 $crawler = $client->request('GET', '/all/list');
1119 $form = $crawler->filter('button[id=submit-filter]')->form();
1120
1121 $data = [
1122 'entry_filter[httpStatus]' => 200,
1123 ];
1124
1125 $crawler = $client->submit($form, $data);
1126
1127 $this->assertCount(2, $crawler->filter('div[class=entry]'));
1128
1129 $crawler = $client->request('GET', '/all/list');
1130 $form = $crawler->filter('button[id=submit-filter]')->form();
1131
1132 $data = [
1133 'entry_filter[httpStatus]' => 1024,
1134 ];
1135
1136 $crawler = $client->submit($form, $data);
1137
1138 $this->assertCount(8, $crawler->filter('div[class=entry]'));
1139 }
1140
1141 public function testSearch()
1142 {
1143 $this->logInAs('admin');
1144 $this->useTheme('baggy');
1145 $client = $this->getClient();
1146
1147 $entry = new Entry($this->getLoggedInUser());
1148 $entry->setUrl($this->url);
1149 $entry->setTitle('test');
1150 $this->getEntityManager()->persist($entry);
1151 $this->getEntityManager()->flush();
1152
1153 // Search on unread list
1154 $crawler = $client->request('GET', '/unread/list');
1155
1156 $form = $crawler->filter('form[name=search]')->form();
1157 $data = [
1158 'search_entry[term]' => 'title',
1159 ];
1160
1161 $crawler = $client->submit($form, $data);
1162
1163 $this->assertCount(4, $crawler->filter('div[class=entry]'));
1164
1165 // Search on starred list
1166 $crawler = $client->request('GET', '/starred/list');
1167
1168 $entry = new Entry($this->getLoggedInUser());
1169 $entry->setUrl('http://localhost/foo/bar');
1170 $entry->setTitle('testeur');
1171 $entry->setStarred(true);
1172 $this->getEntityManager()->persist($entry);
1173 $this->getEntityManager()->flush();
1174
1175 $form = $crawler->filter('form[name=search]')->form();
1176 $data = [
1177 'search_entry[term]' => 'testeur',
1178 ];
1179
1180 $crawler = $client->submit($form, $data);
1181
1182 $this->assertCount(1, $crawler->filter('div[class=entry]'));
1183
1184 $crawler = $client->request('GET', '/archive/list');
1185
1186 // Added new article to test on archive list
1187 $entry = new Entry($this->getLoggedInUser());
1188 $entry->setUrl('http://0.0.0.0/foo/baz/qux');
1189 $entry->setTitle('Le manège');
1190 $entry->setArchived(true);
1191 $this->getEntityManager()->persist($entry);
1192 $this->getEntityManager()->flush();
1193
1194 $form = $crawler->filter('form[name=search]')->form();
1195 $data = [
1196 'search_entry[term]' => 'manège',
1197 ];
1198
1199 $crawler = $client->submit($form, $data);
1200
1201 $this->assertCount(1, $crawler->filter('div[class=entry]'));
1202 $client->request('GET', '/delete/'.$entry->getId());
1203
1204 // test on list of all articles
1205 $crawler = $client->request('GET', '/all/list');
1206
1207 $form = $crawler->filter('form[name=search]')->form();
1208 $data = [
1209 'search_entry[term]' => 'wxcvbnqsdf', // a string not available in the database
1210 ];
1211
1212 $crawler = $client->submit($form, $data);
1213
1214 $this->assertCount(0, $crawler->filter('div[class=entry]'));
1215
1216 // test url search on list of all articles
1217 $entry = new Entry($this->getLoggedInUser());
1218 $entry->setUrl('http://domain/qux');
1219 $entry->setTitle('Le manège');
1220 $entry->setArchived(true);
1221 $this->getEntityManager()->persist($entry);
1222 $this->getEntityManager()->flush();
1223
1224 $crawler = $client->request('GET', '/all/list');
1225
1226 $form = $crawler->filter('form[name=search]')->form();
1227 $data = [
1228 'search_entry[term]' => 'domain', // the search will match an entry with 'domain' in its url
1229 ];
1230
1231 $crawler = $client->submit($form, $data);
1232
1233 $this->assertCount(1, $crawler->filter('div[class=entry]'));
1234
1235 // same as previous test but for case-sensitivity
1236 $crawler = $client->request('GET', '/all/list');
1237
1238 $form = $crawler->filter('form[name=search]')->form();
1239 $data = [
1240 'search_entry[term]' => 'doMain', // the search will match an entry with 'domain' in its url
1241 ];
1242
1243 $crawler = $client->submit($form, $data);
1244
1245 $this->assertCount(1, $crawler->filter('div[class=entry]'));
1246 }
1247
1248 public function dataForLanguage()
1249 {
1250 return [
1251 'ru' => [
1252 'https://www.pravda.ru/world/09-06-2017/1337283-qatar-0/',
1253 'ru',
1254 ],
1255 'fr-FR' => [
1256 'http://www.zataz.com/90-des-dossiers-medicaux-des-coreens-du-sud-vendus-a-des-entreprises-privees/',
1257 'fr_FR',
1258 ],
1259 'de' => [
1260 'http://www.bild.de/politik/ausland/theresa-may/wahlbeben-grossbritannien-analyse-52108924.bild.html',
1261 'de',
1262 ],
1263 'it' => [
1264 'http://www.ansa.it/sito/notizie/mondo/europa/2017/06/08/voto-gb-seggi-aperti-misure-sicurezza-rafforzate_0cb71f7f-e23b-4d5f-95ca-bc12296419f0.html',
1265 'it',
1266 ],
1267 'zh_CN' => [
1268 'http://www.hao123.com/shequ?__noscript__-=1',
1269 'zh_CN',
1270 ],
1271 'de_AT' => [
1272 'https://buy.garmin.com/de-AT/AT/catalog/product/compareResult.ep?compareProduct=112885&compareProduct=36728',
1273 'de_AT',
1274 ],
1275 'ru_RU' => [
1276 'http://netler.ru/ikt/windows-error-reporting.htm',
1277 'ru_RU',
1278 ],
1279 'pt_BR' => [
1280 'http://precodoscombustiveis.com.br/postos/cidade/4121/pr/maringa',
1281 'pt_BR',
1282 ],
1283 'fucked_list_of_languages' => [
1284 'http://geocatalog.webservice-energy.org/geonetwork/srv/eng/main.home',
1285 '',
1286 ],
1287 'es-ES' => [
1288 'http://www.muylinux.com/2015/04/17/odf-reino-unido-microsoft-google',
1289 'es_ES',
1290 ],
1291 ];
1292 }
1293
1294 /**
1295 * @dataProvider dataForLanguage
1296 */
1297 public function testLanguageValidation($url, $expectedLanguage)
1298 {
1299 $this->logInAs('admin');
1300 $client = $this->getClient();
1301
1302 $crawler = $client->request('GET', '/new');
1303
1304 $this->assertEquals(200, $client->getResponse()->getStatusCode());
1305
1306 $form = $crawler->filter('form[name=entry]')->form();
1307
1308 $data = [
1309 'entry[url]' => $url,
1310 ];
1311
1312 $client->submit($form, $data);
1313
1314 $this->assertEquals(302, $client->getResponse()->getStatusCode());
1315
1316 $content = $client->getContainer()
1317 ->get('doctrine.orm.entity_manager')
1318 ->getRepository('WallabagCoreBundle:Entry')
1319 ->findByUrlAndUserId($url, $this->getLoggedInUserId());
1320
1321 $this->assertInstanceOf('Wallabag\CoreBundle\Entity\Entry', $content);
1322 $this->assertEquals($url, $content->getUrl());
1323 $this->assertEquals($expectedLanguage, $content->getLanguage());
1324 }
1325
1326 /**
1327 * This test will require an internet connection.
1328 */
1329 public function testRestrictedArticle()
1330 {
1331 $url = 'http://www.monde-diplomatique.fr/2017/05/BONNET/57475';
1332 $this->logInAs('admin');
1333 $client = $this->getClient();
1334 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
1335
1336 // enable restricted access
1337 $client->getContainer()->get('craue_config')->set('restricted_access', 1);
1338
1339 // create a new site_credential
1340 $user = $client->getContainer()->get('security.token_storage')->getToken()->getUser();
1341 $credential = new SiteCredential($user);
1342 $credential->setHost('monde-diplomatique.fr');
1343 $credential->setUsername('foo');
1344 $credential->setPassword($client->getContainer()->get('wallabag_core.helper.crypto_proxy')->crypt('bar'));
1345
1346 $em->persist($credential);
1347 $em->flush();
1348
1349 $crawler = $client->request('GET', '/new');
1350
1351 $this->assertEquals(200, $client->getResponse()->getStatusCode());
1352
1353 $form = $crawler->filter('form[name=entry]')->form();
1354
1355 $data = [
1356 'entry[url]' => $url,
1357 ];
1358
1359 $client->submit($form, $data);
1360
1361 $this->assertEquals(302, $client->getResponse()->getStatusCode());
1362
1363 $crawler = $client->followRedirect();
1364
1365 $this->assertEquals(200, $client->getResponse()->getStatusCode());
1366 $this->assertContains('flashes.entry.notice.entry_saved', $crawler->filter('body')->extract(['_text'])[0]);
1367
1368 $content = $em
1369 ->getRepository('WallabagCoreBundle:Entry')
1370 ->findByUrlAndUserId($url, $this->getLoggedInUserId());
1371
1372 $this->assertInstanceOf('Wallabag\CoreBundle\Entity\Entry', $content);
1373 $this->assertSame('Crimes et réformes aux Philippines', $content->getTitle());
1374
1375 $client->getContainer()->get('craue_config')->set('restricted_access', 0);
1376 }
1377 }