]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php
First draft for tests
[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 use Wallabag\CoreBundle\Entity\Tag;
10 use Wallabag\CoreBundle\Helper\ContentProxy;
11
12 class EntryControllerTest extends WallabagCoreTestCase
13 {
14 const AN_URL_CONTAINING_AN_ARTICLE_WITH_IMAGE = 'https://www.lemonde.fr/judo/article/2017/11/11/judo-la-decima-de-teddy-riner_5213605_1556020.html';
15 public $downloadImagesEnabled = false;
16 public $url = 'https://www.lemonde.fr/pixels/article/2019/06/18/ce-qu-il-faut-savoir-sur-le-libra-la-cryptomonnaie-de-facebook_5477887_4408996.html';
17
18 /**
19 * @after
20 *
21 * Ensure download_images_enabled is disabled after each script
22 */
23 public function tearDownImagesEnabled()
24 {
25 if ($this->downloadImagesEnabled) {
26 $client = static::createClient();
27 $client->getContainer()->get('craue_config')->set('download_images_enabled', 0);
28
29 $this->downloadImagesEnabled = false;
30 }
31 }
32
33 public function testLogin()
34 {
35 $client = $this->getClient();
36
37 $client->request('GET', '/new');
38
39 $this->assertSame(302, $client->getResponse()->getStatusCode());
40 $this->assertContains('login', $client->getResponse()->headers->get('location'));
41 }
42
43 /**
44 * @group NetworkCalls
45 */
46 public function testQuickstart()
47 {
48 $this->logInAs('empty');
49 $client = $this->getClient();
50
51 $client->request('GET', '/unread/list');
52 $this->assertSame(302, $client->getResponse()->getStatusCode());
53 $crawler = $client->followRedirect();
54
55 $this->assertSame(200, $client->getResponse()->getStatusCode());
56 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
57 $this->assertContains('quickstart.intro.title', $body[0]);
58
59 // Test if quickstart is disabled when user has 1 entry
60 $crawler = $client->request('GET', '/new');
61
62 $this->assertSame(200, $client->getResponse()->getStatusCode());
63
64 $form = $crawler->filter('form[name=entry]')->form();
65
66 $data = [
67 'entry[url]' => $this->url,
68 ];
69
70 $client->submit($form, $data);
71 $this->assertSame(302, $client->getResponse()->getStatusCode());
72 $client->followRedirect();
73
74 $crawler = $client->request('GET', '/unread/list');
75 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
76 $this->assertContains('entry.list.number_on_the_page', $body[0]);
77 }
78
79 public function testGetNew()
80 {
81 $this->logInAs('admin');
82 $this->useTheme('baggy');
83 $client = $this->getClient();
84
85 $crawler = $client->request('GET', '/new');
86
87 $this->assertSame(200, $client->getResponse()->getStatusCode());
88
89 $this->assertCount(1, $crawler->filter('input[type=url]'));
90 $this->assertCount(1, $crawler->filter('form[name=entry]'));
91 }
92
93 /**
94 * @group NetworkCalls
95 */
96 public function testPostNewViaBookmarklet()
97 {
98 $this->logInAs('admin');
99 $client = $this->getClient();
100
101 $crawler = $client->request('GET', '/');
102
103 $this->assertCount(4, $crawler->filter('li.entry'));
104
105 // Good URL
106 $client->request('GET', '/bookmarklet', ['url' => $this->url]);
107 $this->assertSame(302, $client->getResponse()->getStatusCode());
108 $client->followRedirect();
109 $crawler = $client->request('GET', '/');
110 $this->assertCount(5, $crawler->filter('li.entry'));
111
112 $em = $client->getContainer()
113 ->get('doctrine.orm.entity_manager');
114 $entry = $em
115 ->getRepository('WallabagCoreBundle:Entry')
116 ->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
117 $em->remove($entry);
118 $em->flush();
119 }
120
121 public function testPostNewEmpty()
122 {
123 $this->logInAs('admin');
124 $client = $this->getClient();
125
126 $crawler = $client->request('GET', '/new');
127
128 $this->assertSame(200, $client->getResponse()->getStatusCode());
129
130 $form = $crawler->filter('form[name=entry]')->form();
131
132 $crawler = $client->submit($form);
133
134 $this->assertSame(200, $client->getResponse()->getStatusCode());
135 $this->assertCount(1, $alert = $crawler->filter('form ul li')->extract(['_text']));
136 $this->assertSame('This value should not be blank.', $alert[0]);
137 }
138
139 /**
140 * @group NetworkCalls
141 */
142 public function testPostNewOk()
143 {
144 $this->logInAs('admin');
145 $client = $this->getClient();
146
147 $client->getContainer()->get('craue_config')->set('store_article_headers', 1);
148
149 $crawler = $client->request('GET', '/new');
150
151 $this->assertSame(200, $client->getResponse()->getStatusCode());
152
153 $form = $crawler->filter('form[name=entry]')->form();
154
155 $data = [
156 'entry[url]' => $this->url,
157 ];
158
159 $client->submit($form, $data);
160
161 $this->assertSame(302, $client->getResponse()->getStatusCode());
162
163 $content = $client->getContainer()
164 ->get('doctrine.orm.entity_manager')
165 ->getRepository('WallabagCoreBundle:Entry')
166 ->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
167
168 $author = $content->getPublishedBy();
169
170 $this->assertInstanceOf('Wallabag\CoreBundle\Entity\Entry', $content);
171 $this->assertSame($this->url, $content->getUrl());
172 $this->assertContains('la cryptomonnaie de Facebook', $content->getTitle());
173 $this->assertSame('fr', $content->getLanguage());
174 $this->assertArrayHasKey('x-frame-options', $content->getHeaders());
175 $client->getContainer()->get('craue_config')->set('store_article_headers', 0);
176 }
177
178 /**
179 * @group NetworkCalls
180 */
181 public function testPostWithMultipleAuthors()
182 {
183 $url = 'https://www.liberation.fr/planete/2017/04/05/donald-trump-et-xi-jinping-tentative-de-flirt-en-floride_1560768';
184 $this->logInAs('admin');
185 $client = $this->getClient();
186
187 $crawler = $client->request('GET', '/new');
188
189 $this->assertSame(200, $client->getResponse()->getStatusCode());
190
191 $form = $crawler->filter('form[name=entry]')->form();
192
193 $data = [
194 'entry[url]' => $url,
195 ];
196
197 $client->submit($form, $data);
198
199 $this->assertSame(302, $client->getResponse()->getStatusCode());
200
201 $content = $client->getContainer()
202 ->get('doctrine.orm.entity_manager')
203 ->getRepository('WallabagCoreBundle:Entry')
204 ->findByUrlAndUserId($url, $this->getLoggedInUserId());
205
206 $this->assertInstanceOf('Wallabag\CoreBundle\Entity\Entry', $content);
207 $authors = $content->getPublishedBy();
208 $this->assertSame('2017-04-05 19:26:13', $content->getPublishedAt()->format('Y-m-d H:i:s'));
209 $this->assertSame('fr', $content->getLanguage());
210 $this->assertSame('Raphaël Balenieri', $authors[0]);
211 $this->assertSame('Frédéric Autran', $authors[1]);
212 }
213
214 public function testPostNewOkUrlExist()
215 {
216 $this->logInAs('admin');
217
218 $entry = new Entry($this->getLoggedInUser());
219 $entry->setUrl($this->url);
220 $this->getEntityManager()->persist($entry);
221 $this->getEntityManager()->flush();
222
223 $client = $this->getClient();
224
225 $crawler = $client->request('GET', '/new');
226
227 $this->assertSame(200, $client->getResponse()->getStatusCode());
228
229 $form = $crawler->filter('form[name=entry]')->form();
230
231 $data = [
232 'entry[url]' => $this->url,
233 ];
234
235 $client->submit($form, $data);
236
237 $this->assertSame(302, $client->getResponse()->getStatusCode());
238 $this->assertContains('/view/', $client->getResponse()->getTargetUrl());
239 }
240
241 /**
242 * @group NetworkCalls
243 */
244 public function testPostNewOkUrlExistWithAccent()
245 {
246 $this->logInAs('admin');
247 $client = $this->getClient();
248
249 $url = 'https://www.aritylabs.com/post/106091708292/des-contr%C3%B4leurs-optionnels-gr%C3%A2ce-%C3%A0-constmissing';
250
251 $crawler = $client->request('GET', '/new');
252
253 $this->assertSame(200, $client->getResponse()->getStatusCode());
254
255 $form = $crawler->filter('form[name=entry]')->form();
256
257 $data = [
258 'entry[url]' => $url,
259 ];
260
261 $client->submit($form, $data);
262
263 $crawler = $client->request('GET', '/new');
264
265 $this->assertSame(200, $client->getResponse()->getStatusCode());
266
267 $form = $crawler->filter('form[name=entry]')->form();
268
269 $data = [
270 'entry[url]' => $url,
271 ];
272
273 $client->submit($form, $data);
274
275 $this->assertSame(302, $client->getResponse()->getStatusCode());
276 $this->assertContains('/view/', $client->getResponse()->getTargetUrl());
277 }
278
279 /**
280 * @group NetworkCalls
281 */
282 public function testPostNewOkUrlExistWithRedirection()
283 {
284 $this->logInAs('admin');
285 $client = $this->getClient();
286
287 $url = 'https://wllbg.org/test-redirect/c51c';
288
289 $crawler = $client->request('GET', '/new');
290
291 $this->assertSame(200, $client->getResponse()->getStatusCode());
292
293 $form = $crawler->filter('form[name=entry]')->form();
294
295 $data = [
296 'entry[url]' => $url,
297 ];
298
299 $client->submit($form, $data);
300
301 $crawler = $client->request('GET', '/new');
302
303 $this->assertSame(200, $client->getResponse()->getStatusCode());
304
305 $form = $crawler->filter('form[name=entry]')->form();
306
307 $data = [
308 'entry[url]' => $url,
309 ];
310
311 $client->submit($form, $data);
312
313 $this->assertSame(302, $client->getResponse()->getStatusCode());
314 $this->assertContains('/view/', $client->getResponse()->getTargetUrl());
315 }
316
317 /**
318 * @group NetworkCalls
319 */
320 public function testPostNewThatWillBeTagged()
321 {
322 $this->logInAs('admin');
323 $client = $this->getClient();
324
325 $crawler = $client->request('GET', '/new');
326
327 $this->assertSame(200, $client->getResponse()->getStatusCode());
328
329 $form = $crawler->filter('form[name=entry]')->form();
330
331 $data = [
332 'entry[url]' => $url = 'https://github.com/wallabag/wallabag',
333 ];
334
335 $client->submit($form, $data);
336
337 $this->assertSame(302, $client->getResponse()->getStatusCode());
338 $this->assertContains('/', $client->getResponse()->getTargetUrl());
339
340 $em = $client->getContainer()
341 ->get('doctrine.orm.entity_manager');
342 $entry = $em
343 ->getRepository('WallabagCoreBundle:Entry')
344 ->findOneByUrl($url);
345 $tags = $entry->getTags();
346
347 $this->assertCount(2, $tags);
348 $this->assertContains('wallabag', $tags);
349 $this->assertSame('en', $entry->getLanguage());
350
351 $em->remove($entry);
352 $em->flush();
353
354 // and now re-submit it to test the cascade persistence for tags after entry removal
355 // related https://github.com/wallabag/wallabag/issues/2121
356 $crawler = $client->request('GET', '/new');
357
358 $this->assertSame(200, $client->getResponse()->getStatusCode());
359
360 $form = $crawler->filter('form[name=entry]')->form();
361
362 $data = [
363 'entry[url]' => $url = 'https://github.com/wallabag/wallabag/tree/master',
364 ];
365
366 $client->submit($form, $data);
367
368 $this->assertSame(302, $client->getResponse()->getStatusCode());
369 $this->assertContains('/', $client->getResponse()->getTargetUrl());
370
371 $entry = $em
372 ->getRepository('WallabagCoreBundle:Entry')
373 ->findOneByUrl($url);
374
375 $tags = $entry->getTags();
376
377 $this->assertCount(2, $tags);
378 $this->assertContains('wallabag', $tags);
379
380 $em->remove($entry);
381 $em->flush();
382 }
383
384 public function testArchive()
385 {
386 $this->logInAs('admin');
387 $client = $this->getClient();
388
389 $client->request('GET', '/archive/list');
390
391 $this->assertSame(200, $client->getResponse()->getStatusCode());
392 }
393
394 public function testUntagged()
395 {
396 $this->logInAs('admin');
397 $client = $this->getClient();
398
399 $client->request('GET', '/untagged/list');
400
401 $this->assertSame(200, $client->getResponse()->getStatusCode());
402 }
403
404 public function testStarred()
405 {
406 $this->logInAs('admin');
407 $client = $this->getClient();
408
409 $client->request('GET', '/starred/list');
410
411 $this->assertSame(200, $client->getResponse()->getStatusCode());
412 }
413
414 public function testRangeException()
415 {
416 $this->logInAs('admin');
417 $client = $this->getClient();
418
419 $client->request('GET', '/all/list/900');
420
421 $this->assertSame(302, $client->getResponse()->getStatusCode());
422 $this->assertSame('/all/list', $client->getResponse()->getTargetUrl());
423 }
424
425 public function testView()
426 {
427 $this->logInAs('admin');
428 $client = $this->getClient();
429
430 $entry = new Entry($this->getLoggedInUser());
431 $entry->setUrl('http://example.com/foo');
432 $entry->setTitle('title foo');
433 $entry->setContent('foo bar baz');
434 $this->getEntityManager()->persist($entry);
435 $this->getEntityManager()->flush();
436
437 $crawler = $client->request('GET', '/view/' . $entry->getId());
438
439 $this->assertSame(200, $client->getResponse()->getStatusCode());
440 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
441 $this->assertContains($entry->getTitle(), $body[0]);
442 }
443
444 /**
445 * @group NetworkCalls
446 */
447 public function testReload()
448 {
449 $this->logInAs('admin');
450 $client = $this->getClient();
451
452 $entry = new Entry($this->getLoggedInUser());
453 $entry->setUrl($this->url);
454 $entry->setTitle('title foo');
455 $entry->setContent('');
456 $this->getEntityManager()->persist($entry);
457 $this->getEntityManager()->flush();
458 $this->getEntityManager()->clear();
459
460 $client->request('GET', '/reload/' . $entry->getId());
461
462 $this->assertSame(302, $client->getResponse()->getStatusCode());
463
464 $entry = $this->getEntityManager()
465 ->getRepository('WallabagCoreBundle:Entry')
466 ->find($entry->getId());
467
468 $this->assertNotEmpty($entry->getContent());
469 }
470
471 public function testReloadWithFetchingFailed()
472 {
473 $this->logInAs('admin');
474 $client = $this->getClient();
475
476 $entry = new Entry($this->getLoggedInUser());
477 $entry->setUrl('http://0.0.0.0/failed.html');
478 $this->getEntityManager()->persist($entry);
479 $this->getEntityManager()->flush();
480
481 $client->request('GET', '/reload/' . $entry->getId());
482
483 $this->assertSame(302, $client->getResponse()->getStatusCode());
484
485 // force EntityManager to clear previous entity
486 // otherwise, retrieve the same entity will retrieve change from the previous request :0
487 $this->getEntityManager()->clear();
488 $newContent = $this->getEntityManager()
489 ->getRepository('WallabagCoreBundle:Entry')
490 ->find($entry->getId());
491
492 $this->assertNotSame($client->getContainer()->getParameter('wallabag_core.fetching_error_message'), $newContent->getContent());
493 }
494
495 public function testEdit()
496 {
497 $this->logInAs('admin');
498 $client = $this->getClient();
499
500 $entry = new Entry($this->getLoggedInUser());
501 $entry->setUrl($this->url);
502 $this->getEntityManager()->persist($entry);
503 $this->getEntityManager()->flush();
504
505 $crawler = $client->request('GET', '/edit/' . $entry->getId());
506
507 $this->assertSame(200, $client->getResponse()->getStatusCode());
508
509 $this->assertCount(1, $crawler->filter('input[id=entry_title]'));
510 $this->assertCount(1, $crawler->filter('button[id=entry_save]'));
511 }
512
513 public function testEditUpdate()
514 {
515 $this->logInAs('admin');
516 $client = $this->getClient();
517
518 $entry = new Entry($this->getLoggedInUser());
519 $entry->setUrl($this->url);
520 $this->getEntityManager()->persist($entry);
521 $this->getEntityManager()->flush();
522
523 $crawler = $client->request('GET', '/edit/' . $entry->getId());
524
525 $this->assertSame(200, $client->getResponse()->getStatusCode());
526
527 $form = $crawler->filter('button[id=entry_save]')->form();
528
529 $data = [
530 'entry[title]' => 'My updated title hehe :)',
531 'entry[origin_url]' => 'https://example.io',
532 ];
533
534 $client->submit($form, $data);
535
536 $this->assertSame(302, $client->getResponse()->getStatusCode());
537
538 $crawler = $client->followRedirect();
539
540 $this->assertGreaterThan(1, $title = $crawler->filter('div[id=article] h1')->extract(['_text']));
541 $this->assertContains('My updated title hehe :)', $title[0]);
542 $this->assertGreaterThan(1, $stats = $crawler->filter('div[class="tools grey-text"] ul[class=stats] li a[class="tool grey-text"]')->extract(['_text']));
543 $this->assertContains('example.io', trim($stats[1]));
544 }
545
546 public function testEditRemoveOriginUrl()
547 {
548 $this->logInAs('admin');
549 $client = $this->getClient();
550
551 $entry = new Entry($this->getLoggedInUser());
552 $entry->setUrl($this->url);
553 $this->getEntityManager()->persist($entry);
554 $this->getEntityManager()->flush();
555
556 $crawler = $client->request('GET', '/edit/' . $entry->getId());
557
558 $this->assertSame(200, $client->getResponse()->getStatusCode());
559
560 $form = $crawler->filter('button[id=entry_save]')->form();
561
562 $data = [
563 'entry[title]' => 'My updated title hehe :)',
564 'entry[origin_url]' => '',
565 ];
566
567 $client->submit($form, $data);
568
569 $this->assertSame(302, $client->getResponse()->getStatusCode());
570
571 $crawler = $client->followRedirect();
572
573 $title = $crawler->filter('div[id=article] h1')->extract(['_text']);
574 $this->assertGreaterThan(1, $title);
575 $this->assertContains('My updated title hehe :)', $title[0]);
576
577 $stats = $crawler->filter('div[class="tools grey-text"] ul[class=stats] li a[class="tool grey-text"]')->extract(['_text']);
578 $this->assertCount(1, $stats);
579 $this->assertNotContains('example.io', trim($stats[0]));
580 }
581
582 public function testToggleArchive()
583 {
584 $this->logInAs('admin');
585 $client = $this->getClient();
586
587 $entry = new Entry($this->getLoggedInUser());
588 $entry->setUrl($this->url);
589 $this->getEntityManager()->persist($entry);
590 $this->getEntityManager()->flush();
591 $this->getEntityManager()->clear();
592
593 $client->request('GET', '/archive/' . $entry->getId());
594
595 $this->assertSame(302, $client->getResponse()->getStatusCode());
596
597 $res = $client->getContainer()
598 ->get('doctrine.orm.entity_manager')
599 ->getRepository('WallabagCoreBundle:Entry')
600 ->find($entry->getId());
601
602 $this->assertSame(1, $res->isArchived());
603 }
604
605 public function testToggleStar()
606 {
607 $this->logInAs('admin');
608 $client = $this->getClient();
609
610 $entry = new Entry($this->getLoggedInUser());
611 $entry->setUrl($this->url);
612 $this->getEntityManager()->persist($entry);
613 $this->getEntityManager()->flush();
614 $this->getEntityManager()->clear();
615
616 $client->request('GET', '/star/' . $entry->getId());
617
618 $this->assertSame(302, $client->getResponse()->getStatusCode());
619
620 $res = $client->getContainer()
621 ->get('doctrine.orm.entity_manager')
622 ->getRepository('WallabagCoreBundle:Entry')
623 ->findOneById($entry->getId());
624
625 $this->assertSame(1, $res->isStarred());
626 }
627
628 public function testDelete()
629 {
630 $this->logInAs('admin');
631 $client = $this->getClient();
632
633 $entry = new Entry($this->getLoggedInUser());
634 $entry->setUrl($this->url);
635 $this->getEntityManager()->persist($entry);
636 $this->getEntityManager()->flush();
637
638 $client->request('GET', '/delete/' . $entry->getId());
639
640 $this->assertSame(302, $client->getResponse()->getStatusCode());
641
642 $client->request('GET', '/delete/' . $entry->getId());
643
644 $this->assertSame(404, $client->getResponse()->getStatusCode());
645 }
646
647 /**
648 * It will create a new entry.
649 * Browse to it.
650 * Then remove it.
651 *
652 * And it'll check that user won't be redirected to the view page of the content when it had been removed
653 */
654 public function testViewAndDelete()
655 {
656 $this->logInAs('admin');
657 $client = $this->getClient();
658
659 $em = $client->getContainer()
660 ->get('doctrine.orm.entity_manager');
661
662 // add a new content to be removed later
663 $user = $em
664 ->getRepository('WallabagUserBundle:User')
665 ->findOneByUserName('admin');
666
667 $content = new Entry($user);
668 $content->setUrl('http://1.1.1.1/entry');
669 $content->setReadingTime(12);
670 $content->setDomainName('domain.io');
671 $content->setMimetype('text/html');
672 $content->setTitle('test title entry');
673 $content->setContent('This is my content /o/');
674 $content->updateArchived(true);
675 $content->setLanguage('fr');
676
677 $em->persist($content);
678 $em->flush();
679
680 $client->request('GET', '/view/' . $content->getId());
681 $this->assertSame(200, $client->getResponse()->getStatusCode());
682
683 $client->request('GET', '/delete/' . $content->getId());
684 $this->assertSame(302, $client->getResponse()->getStatusCode());
685
686 $client->followRedirect();
687 $this->assertSame(200, $client->getResponse()->getStatusCode());
688 }
689
690 public function testViewOtherUserEntry()
691 {
692 $this->logInAs('admin');
693 $client = $this->getClient();
694
695 $content = $client->getContainer()
696 ->get('doctrine.orm.entity_manager')
697 ->getRepository('WallabagCoreBundle:Entry')
698 ->findOneByUsernameAndNotArchived('bob');
699
700 $client->request('GET', '/view/' . $content->getId());
701
702 $this->assertSame(403, $client->getResponse()->getStatusCode());
703 }
704
705 public function testFilterOnReadingTime()
706 {
707 $this->logInAs('admin');
708 $client = $this->getClient();
709 $entry = new Entry($this->getLoggedInUser());
710 $entry->setUrl($this->url);
711 $entry->setReadingTime(22);
712 $this->getEntityManager()->persist($entry);
713 $this->getEntityManager()->flush();
714
715 $crawler = $client->request('GET', '/unread/list');
716
717 $form = $crawler->filter('button[id=submit-filter]')->form();
718
719 $data = [
720 'entry_filter[readingTime][right_number]' => 22,
721 'entry_filter[readingTime][left_number]' => 22,
722 ];
723
724 $crawler = $client->submit($form, $data);
725
726 $this->assertCount(1, $crawler->filter('li.entry'));
727 }
728
729 public function testFilterOnReadingTimeWithNegativeValue()
730 {
731 $this->logInAs('admin');
732 $client = $this->getClient();
733
734 $crawler = $client->request('GET', '/unread/list');
735
736 $form = $crawler->filter('button[id=submit-filter]')->form();
737
738 $data = [
739 'entry_filter[readingTime][right_number]' => -22,
740 'entry_filter[readingTime][left_number]' => -22,
741 ];
742
743 $crawler = $client->submit($form, $data);
744
745 // forcing negative value results in no entry displayed
746 $this->assertCount(0, $crawler->filter('li.entry'));
747 }
748
749 public function testFilterOnReadingTimeOnlyUpper()
750 {
751 $this->logInAs('admin');
752 $client = $this->getClient();
753
754 $crawler = $client->request('GET', '/all/list');
755 $this->assertCount(5, $crawler->filter('li.entry'));
756
757 $entry = new Entry($this->getLoggedInUser());
758 $entry->setUrl($this->url);
759 $entry->setReadingTime(23);
760 $this->getEntityManager()->persist($entry);
761 $this->getEntityManager()->flush();
762
763 $crawler = $client->request('GET', '/all/list');
764 $this->assertCount(6, $crawler->filter('li.entry'));
765
766 $form = $crawler->filter('button[id=submit-filter]')->form();
767
768 $data = [
769 'entry_filter[readingTime][right_number]' => 22,
770 ];
771
772 $crawler = $client->submit($form, $data);
773
774 $this->assertCount(5, $crawler->filter('li.entry'));
775 }
776
777 public function testFilterOnReadingTimeOnlyLower()
778 {
779 $this->logInAs('admin');
780 $client = $this->getClient();
781
782 $crawler = $client->request('GET', '/unread/list');
783
784 $form = $crawler->filter('button[id=submit-filter]')->form();
785
786 $data = [
787 'entry_filter[readingTime][left_number]' => 22,
788 ];
789
790 $crawler = $client->submit($form, $data);
791
792 $this->assertCount(0, $crawler->filter('li.entry'));
793
794 $entry = new Entry($this->getLoggedInUser());
795 $entry->setUrl($this->url);
796 $entry->setReadingTime(23);
797 $this->getEntityManager()->persist($entry);
798 $this->getEntityManager()->flush();
799
800 $crawler = $client->submit($form, $data);
801 $this->assertCount(1, $crawler->filter('li.entry'));
802 }
803
804 public function testFilterOnUnreadStatus()
805 {
806 $this->logInAs('admin');
807 $client = $this->getClient();
808
809 $crawler = $client->request('GET', '/all/list');
810
811 $form = $crawler->filter('button[id=submit-filter]')->form();
812
813 $data = [
814 'entry_filter[isUnread]' => true,
815 ];
816
817 $crawler = $client->submit($form, $data);
818
819 $this->assertCount(4, $crawler->filter('li.entry'));
820
821 $entry = new Entry($this->getLoggedInUser());
822 $entry->setUrl($this->url);
823 $entry->updateArchived(false);
824 $this->getEntityManager()->persist($entry);
825 $this->getEntityManager()->flush();
826
827 $crawler = $client->submit($form, $data);
828
829 $this->assertCount(5, $crawler->filter('li.entry'));
830 }
831
832 public function testFilterOnCreationDate()
833 {
834 $this->logInAs('admin');
835 $client = $this->getClient();
836
837 $em = $this->getEntityManager();
838
839 $today = new \DateTimeImmutable();
840 $tomorrow = $today->add(new \DateInterval('P1D'));
841 $yesterday = $today->sub(new \DateInterval('P1D'));
842
843 $entry = new Entry($this->getLoggedInUser());
844 $entry->setUrl('http://0.0.0.0/testFilterOnCreationDate');
845 $entry->setCreatedAt($yesterday);
846 $em->persist($entry);
847 $em->flush();
848
849 $crawler = $client->request('GET', '/unread/list');
850
851 $form = $crawler->filter('button[id=submit-filter]')->form();
852
853 $data = [
854 'entry_filter[createdAt][left_date]' => $today->format('Y-m-d'),
855 'entry_filter[createdAt][right_date]' => $tomorrow->format('Y-m-d'),
856 ];
857
858 $crawler = $client->submit($form, $data);
859
860 $this->assertCount(5, $crawler->filter('li.entry'));
861
862 $data = [
863 'entry_filter[createdAt][left_date]' => $today->format('Y-m-d'),
864 'entry_filter[createdAt][right_date]' => $today->format('Y-m-d'),
865 ];
866
867 $crawler = $client->submit($form, $data);
868
869 $this->assertCount(5, $crawler->filter('li.entry'));
870
871 $data = [
872 'entry_filter[createdAt][left_date]' => '1970-01-01',
873 'entry_filter[createdAt][right_date]' => '1970-01-01',
874 ];
875
876 $crawler = $client->submit($form, $data);
877
878 $this->assertCount(0, $crawler->filter('li.entry'));
879 }
880
881 public function testPaginationWithFilter()
882 {
883 $this->logInAs('admin');
884 $client = $this->getClient();
885 $crawler = $client->request('GET', '/config');
886
887 $form = $crawler->filter('button[id=config_save]')->form();
888
889 $data = [
890 'config[items_per_page]' => '1',
891 ];
892
893 $client->submit($form, $data);
894
895 $parameters = '?entry_filter%5BreadingTime%5D%5Bleft_number%5D=&entry_filter%5BreadingTime%5D%5Bright_number%5D=';
896
897 $client->request('GET', 'unread/list' . $parameters);
898
899 $this->assertContains($parameters, $client->getResponse()->getContent());
900
901 // reset pagination
902 $crawler = $client->request('GET', '/config');
903 $form = $crawler->filter('button[id=config_save]')->form();
904 $data = [
905 'config[items_per_page]' => '12',
906 ];
907 $client->submit($form, $data);
908 }
909
910 public function testFilterOnDomainName()
911 {
912 $this->logInAs('admin');
913 $client = $this->getClient();
914
915 $crawler = $client->request('GET', '/unread/list');
916 $form = $crawler->filter('button[id=submit-filter]')->form();
917 $data = [
918 'entry_filter[domainName]' => 'domain',
919 ];
920
921 $crawler = $client->submit($form, $data);
922 $this->assertCount(5, $crawler->filter('li.entry'));
923
924 $crawler = $client->request('GET', '/unread/list');
925 $form = $crawler->filter('button[id=submit-filter]')->form();
926 $data = [
927 'entry_filter[domainName]' => 'dOmain',
928 ];
929
930 $crawler = $client->submit($form, $data);
931 $this->assertCount(5, $crawler->filter('li.entry'));
932
933 $form = $crawler->filter('button[id=submit-filter]')->form();
934 $data = [
935 'entry_filter[domainName]' => 'wallabag',
936 ];
937
938 $crawler = $client->submit($form, $data);
939 $this->assertCount(0, $crawler->filter('li.entry'));
940 }
941
942 public function testFilterOnStatus()
943 {
944 $this->logInAs('admin');
945 $client = $this->getClient();
946
947 $crawler = $client->request('GET', '/unread/list');
948 $form = $crawler->filter('button[id=submit-filter]')->form();
949 $form['entry_filter[isArchived]']->tick();
950 $form['entry_filter[isStarred]']->untick();
951
952 $crawler = $client->submit($form);
953 $this->assertCount(1, $crawler->filter('li.entry'));
954
955 $form = $crawler->filter('button[id=submit-filter]')->form();
956 $form['entry_filter[isArchived]']->untick();
957 $form['entry_filter[isStarred]']->tick();
958
959 $crawler = $client->submit($form);
960 $this->assertCount(1, $crawler->filter('li.entry'));
961 }
962
963 public function testFilterOnIsPublic()
964 {
965 $this->logInAs('admin');
966 $client = $this->getClient();
967
968 $crawler = $client->request('GET', '/unread/list');
969 $form = $crawler->filter('button[id=submit-filter]')->form();
970 $form['entry_filter[isPublic]']->tick();
971
972 $crawler = $client->submit($form);
973 $this->assertCount(0, $crawler->filter('li.entry'));
974 }
975
976 public function testPreviewPictureFilter()
977 {
978 $this->logInAs('admin');
979 $client = $this->getClient();
980
981 $crawler = $client->request('GET', '/unread/list');
982 $form = $crawler->filter('button[id=submit-filter]')->form();
983 $form['entry_filter[previewPicture]']->tick();
984
985 $crawler = $client->submit($form);
986 $this->assertCount(1, $crawler->filter('li.entry'));
987 }
988
989 public function testFilterOnLanguage()
990 {
991 $this->logInAs('admin');
992 $client = $this->getClient();
993
994 $entry = new Entry($this->getLoggedInUser());
995 $entry->setUrl($this->url);
996 $entry->setLanguage('fr');
997 $this->getEntityManager()->persist($entry);
998 $this->getEntityManager()->flush();
999
1000 $crawler = $client->request('GET', '/unread/list');
1001 $form = $crawler->filter('button[id=submit-filter]')->form();
1002 $data = [
1003 'entry_filter[language]' => 'fr',
1004 ];
1005
1006 $crawler = $client->submit($form, $data);
1007 $this->assertCount(3, $crawler->filter('li.entry'));
1008
1009 $form = $crawler->filter('button[id=submit-filter]')->form();
1010 $data = [
1011 'entry_filter[language]' => 'en',
1012 ];
1013
1014 $crawler = $client->submit($form, $data);
1015 $this->assertCount(2, $crawler->filter('li.entry'));
1016 }
1017
1018 public function testSortOnTitle()
1019 {
1020 $this->logInAs('admin');
1021 $client = $this->getClient();
1022
1023 $crawler = $client->request('GET', '/unread/list');
1024 $form = $crawler->filter('button[id=submit-sort]')->form();
1025 $data = [
1026 'entry_sort[sortType]' => 'title',
1027 'entry_sort[sortOrder]' => 'asc',
1028 ];
1029 $crawler = $client->submit($form, $data);
1030
1031 $this->assertCount(4, $crawler->filter('li.entry'));
1032
1033 $matches = [];
1034 preg_match_all('/test title entry([0-9])/', $client->getResponse()->getContent(), $matches);
1035
1036 $results = array_values(array_unique($matches[0]));
1037
1038 $ids = [1, 2, 4, 5];
1039
1040 foreach ($results as $key => $result) {
1041 $this->assertSame('test title entry' . $ids[$key], $result);
1042 }
1043
1044 rsort($ids);
1045
1046 $crawler = $client->request('GET', '/unread/list');
1047 $form = $crawler->filter('button[id=submit-sort]')->form();
1048 $data = [
1049 'entry_sort[sortType]' => 'title',
1050 'entry_sort[sortOrder]' => 'desc',
1051 ];
1052 $crawler = $client->submit($form, $data);
1053
1054 $matches = [];
1055 preg_match_all('/test title entry([0-9])/', $client->getResponse()->getContent(), $matches);
1056
1057 $results = array_values(array_unique($matches[0]));
1058
1059 foreach ($results as $key => $result) {
1060 $this->assertSame('test title entry' . $ids[$key], $result);
1061 }
1062 }
1063
1064 public function testShareEntryPublicly()
1065 {
1066 $this->logInAs('admin');
1067 $client = $this->getClient();
1068
1069 // sharing is enabled
1070 $client->getContainer()->get('craue_config')->set('share_public', 1);
1071
1072 $content = new Entry($this->getLoggedInUser());
1073 $content->setUrl($this->url);
1074 $this->getEntityManager()->persist($content);
1075 $this->getEntityManager()->flush();
1076 $this->getEntityManager()->clear();
1077
1078 // no uid
1079 $client->request('GET', '/share/' . $content->getUid());
1080 $this->assertSame(404, $client->getResponse()->getStatusCode());
1081
1082 // generating the uid
1083 $client->request('GET', '/share/' . $content->getId());
1084 $this->assertSame(302, $client->getResponse()->getStatusCode());
1085
1086 $shareUrl = $client->getResponse()->getTargetUrl();
1087
1088 // use a new client to have a fresh empty session (instead of a logged one from the previous client)
1089 $client->restart();
1090
1091 $client->request('GET', $shareUrl);
1092
1093 $this->assertSame(200, $client->getResponse()->getStatusCode());
1094 $this->assertContains('max-age=25200', $client->getResponse()->headers->get('cache-control'));
1095 $this->assertContains('public', $client->getResponse()->headers->get('cache-control'));
1096 $this->assertContains('s-maxage=25200', $client->getResponse()->headers->get('cache-control'));
1097 $this->assertNotContains('no-cache', $client->getResponse()->headers->get('cache-control'));
1098 $this->assertContains('og:title', $client->getResponse()->getContent());
1099 $this->assertContains('og:type', $client->getResponse()->getContent());
1100 $this->assertContains('og:url', $client->getResponse()->getContent());
1101 $this->assertContains('og:image', $client->getResponse()->getContent());
1102
1103 // sharing is now disabled
1104 $client->getContainer()->get('craue_config')->set('share_public', 0);
1105 $client->request('GET', '/share/' . $content->getUid());
1106 $this->assertSame(404, $client->getResponse()->getStatusCode());
1107
1108 // removing the share
1109 $client->request('GET', '/share/delete/' . $content->getId());
1110 $this->assertSame(302, $client->getResponse()->getStatusCode());
1111
1112 // share is now disable
1113 $client->request('GET', '/share/' . $content->getUid());
1114 $this->assertSame(404, $client->getResponse()->getStatusCode());
1115 }
1116
1117 /**
1118 * @group NetworkCalls
1119 */
1120 public function testNewEntryWithDownloadImagesEnabled()
1121 {
1122 $this->downloadImagesEnabled = true;
1123 $this->logInAs('admin');
1124 $client = $this->getClient();
1125
1126 $url = self::AN_URL_CONTAINING_AN_ARTICLE_WITH_IMAGE;
1127 $client->getContainer()->get('craue_config')->set('download_images_enabled', 1);
1128
1129 $crawler = $client->request('GET', '/new');
1130
1131 $this->assertSame(200, $client->getResponse()->getStatusCode());
1132
1133 $form = $crawler->filter('form[name=entry]')->form();
1134
1135 $data = [
1136 'entry[url]' => $url,
1137 ];
1138
1139 $client->submit($form, $data);
1140
1141 $this->assertSame(302, $client->getResponse()->getStatusCode());
1142
1143 $em = $client->getContainer()
1144 ->get('doctrine.orm.entity_manager');
1145
1146 $entry = $em
1147 ->getRepository('WallabagCoreBundle:Entry')
1148 ->findByUrlAndUserId($url, $this->getLoggedInUserId());
1149
1150 $this->assertInstanceOf('Wallabag\CoreBundle\Entity\Entry', $entry);
1151 $this->assertSame($url, $entry->getUrl());
1152 $this->assertContains('Judo', $entry->getTitle());
1153 // instead of checking for the filename (which might change) check that the image is now local
1154 $this->assertContains(rtrim($client->getContainer()->getParameter('domain_name'), '/') . '/assets/images/', $entry->getContent());
1155
1156 $client->getContainer()->get('craue_config')->set('download_images_enabled', 0);
1157 }
1158
1159 /**
1160 * @depends testNewEntryWithDownloadImagesEnabled
1161 */
1162 public function testRemoveEntryWithDownloadImagesEnabled()
1163 {
1164 $this->downloadImagesEnabled = true;
1165 $this->logInAs('admin');
1166 $client = $this->getClient();
1167
1168 $url = self::AN_URL_CONTAINING_AN_ARTICLE_WITH_IMAGE;
1169 $client->getContainer()->get('craue_config')->set('download_images_enabled', 1);
1170
1171 $crawler = $client->request('GET', '/new');
1172
1173 $this->assertSame(200, $client->getResponse()->getStatusCode());
1174
1175 $form = $crawler->filter('form[name=entry]')->form();
1176
1177 $data = [
1178 'entry[url]' => $url,
1179 ];
1180
1181 $client->submit($form, $data);
1182
1183 $this->assertSame(302, $client->getResponse()->getStatusCode());
1184
1185 $content = $client->getContainer()
1186 ->get('doctrine.orm.entity_manager')
1187 ->getRepository('WallabagCoreBundle:Entry')
1188 ->findByUrlAndUserId($url, $this->getLoggedInUserId());
1189
1190 $client->request('GET', '/delete/' . $content->getId());
1191
1192 $this->assertSame(302, $client->getResponse()->getStatusCode());
1193
1194 $client->getContainer()->get('craue_config')->set('download_images_enabled', 0);
1195 }
1196
1197 public function testRedirectToHomepage()
1198 {
1199 $this->logInAs('empty');
1200 $client = $this->getClient();
1201
1202 // Redirect to homepage
1203 $config = $this->getLoggedInUser()->getConfig();
1204 $config->setActionMarkAsRead(Config::REDIRECT_TO_HOMEPAGE);
1205 $this->getEntityManager()->persist($config);
1206
1207 $entry = new Entry($this->getLoggedInUser());
1208 $entry->setUrl($this->url);
1209 $this->getEntityManager()->persist($entry);
1210
1211 $this->getEntityManager()->flush();
1212
1213 $client->request('GET', '/view/' . $entry->getId());
1214 $client->request('GET', '/archive/' . $entry->getId());
1215
1216 $this->assertSame(302, $client->getResponse()->getStatusCode());
1217 $this->assertSame('/', $client->getResponse()->headers->get('location'));
1218 }
1219
1220 public function testRedirectToCurrentPage()
1221 {
1222 $this->logInAs('empty');
1223 $client = $this->getClient();
1224
1225 // Redirect to current page
1226 $config = $this->getLoggedInUser()->getConfig();
1227 $config->setActionMarkAsRead(Config::REDIRECT_TO_CURRENT_PAGE);
1228 $this->getEntityManager()->persist($config);
1229
1230 $entry = new Entry($this->getLoggedInUser());
1231 $entry->setUrl($this->url);
1232 $this->getEntityManager()->persist($entry);
1233
1234 $this->getEntityManager()->flush();
1235
1236 $client->request('GET', '/view/' . $entry->getId());
1237 $client->request('GET', '/archive/' . $entry->getId());
1238
1239 $this->assertSame(302, $client->getResponse()->getStatusCode());
1240 $this->assertContains('/view/' . $entry->getId(), $client->getResponse()->headers->get('location'));
1241 }
1242
1243 public function testFilterOnHttpStatus()
1244 {
1245 $this->logInAs('admin');
1246 $client = $this->getClient();
1247
1248 $entry = new Entry($this->getLoggedInUser());
1249 $entry->setUrl('https://www.lemonde.fr/incorrect-url/');
1250 $entry->setHttpStatus(404);
1251 $this->getEntityManager()->persist($entry);
1252
1253 $this->getEntityManager()->flush();
1254
1255 $crawler = $client->request('GET', '/all/list');
1256 $form = $crawler->filter('button[id=submit-filter]')->form();
1257
1258 $data = [
1259 'entry_filter[httpStatus]' => 404,
1260 ];
1261
1262 $crawler = $client->submit($form, $data);
1263
1264 $this->assertCount(1, $crawler->filter('li.entry'));
1265
1266 $entry = new Entry($this->getLoggedInUser());
1267 $entry->setUrl($this->url);
1268 $entry->setHttpStatus(200);
1269 $this->getEntityManager()->persist($entry);
1270
1271 $entry = new Entry($this->getLoggedInUser());
1272 $entry->setUrl('http://www.nextinpact.com/news/101235-wallabag-alternative-libre-a-pocket-creuse-petit-a-petit-son-nid.htm');
1273 $entry->setHttpStatus(200);
1274 $this->getEntityManager()->persist($entry);
1275
1276 $this->getEntityManager()->flush();
1277
1278 $crawler = $client->request('GET', '/all/list');
1279 $form = $crawler->filter('button[id=submit-filter]')->form();
1280
1281 $data = [
1282 'entry_filter[httpStatus]' => 200,
1283 ];
1284
1285 $crawler = $client->submit($form, $data);
1286
1287 $this->assertCount(2, $crawler->filter('li.entry'));
1288
1289 $crawler = $client->request('GET', '/all/list');
1290 $form = $crawler->filter('button[id=submit-filter]')->form();
1291
1292 $data = [
1293 'entry_filter[httpStatus]' => 1024,
1294 ];
1295
1296 $crawler = $client->submit($form, $data);
1297
1298 $this->assertCount(8, $crawler->filter('li.entry'));
1299 }
1300
1301 public function testSearch()
1302 {
1303 $this->logInAs('admin');
1304 $client = $this->getClient();
1305
1306 $entry = new Entry($this->getLoggedInUser());
1307 $entry->setUrl($this->url);
1308 $entry->setTitle('test');
1309 $this->getEntityManager()->persist($entry);
1310 $this->getEntityManager()->flush();
1311
1312 // Search on unread list
1313 $crawler = $client->request('GET', '/unread/list');
1314
1315 $form = $crawler->filter('form[name=search]')->form();
1316 $data = [
1317 'search_entry[term]' => 'title',
1318 ];
1319
1320 $crawler = $client->submit($form, $data);
1321
1322 $this->assertCount(4, $crawler->filter('li.entry'));
1323
1324 // Search on starred list
1325 $crawler = $client->request('GET', '/starred/list');
1326
1327 $entry = new Entry($this->getLoggedInUser());
1328 $entry->setUrl('http://localhost/foo/bar');
1329 $entry->setTitle('testeur');
1330 $entry->setStarred(true);
1331 $this->getEntityManager()->persist($entry);
1332 $this->getEntityManager()->flush();
1333
1334 $form = $crawler->filter('form[name=search]')->form();
1335 $data = [
1336 'search_entry[term]' => 'testeur',
1337 ];
1338
1339 $crawler = $client->submit($form, $data);
1340
1341 $this->assertCount(1, $crawler->filter('li.entry'));
1342
1343 $crawler = $client->request('GET', '/archive/list');
1344
1345 // Added new article to test on archive list
1346 $entry = new Entry($this->getLoggedInUser());
1347 $entry->setUrl('http://0.0.0.0/foo/baz/qux');
1348 $entry->setTitle('Le manège');
1349 $entry->updateArchived(true);
1350 $this->getEntityManager()->persist($entry);
1351 $this->getEntityManager()->flush();
1352
1353 $form = $crawler->filter('form[name=search]')->form();
1354 $data = [
1355 'search_entry[term]' => 'manège',
1356 ];
1357
1358 $crawler = $client->submit($form, $data);
1359
1360 $this->assertCount(1, $crawler->filter('li.entry'));
1361 $client->request('GET', '/delete/' . $entry->getId());
1362
1363 // test on list of all articles
1364 $crawler = $client->request('GET', '/all/list');
1365
1366 $form = $crawler->filter('form[name=search]')->form();
1367 $data = [
1368 'search_entry[term]' => 'wxcvbnqsdf', // a string not available in the database
1369 ];
1370
1371 $crawler = $client->submit($form, $data);
1372
1373 $this->assertCount(0, $crawler->filter('li.entry'));
1374
1375 // test url search on list of all articles
1376 $entry = new Entry($this->getLoggedInUser());
1377 $entry->setUrl('http://domain/qux');
1378 $entry->setTitle('Le manège');
1379 $entry->updateArchived(true);
1380 $this->getEntityManager()->persist($entry);
1381 $this->getEntityManager()->flush();
1382
1383 $crawler = $client->request('GET', '/all/list');
1384
1385 $form = $crawler->filter('form[name=search]')->form();
1386 $data = [
1387 'search_entry[term]' => 'domain', // the search will match an entry with 'domain' in its url
1388 ];
1389
1390 $crawler = $client->submit($form, $data);
1391
1392 $this->assertCount(1, $crawler->filter('li.entry'));
1393
1394 // same as previous test but for case-sensitivity
1395 $crawler = $client->request('GET', '/all/list');
1396
1397 $form = $crawler->filter('form[name=search]')->form();
1398 $data = [
1399 'search_entry[term]' => 'doMain', // the search will match an entry with 'domain' in its url
1400 ];
1401
1402 $crawler = $client->submit($form, $data);
1403
1404 $this->assertCount(1, $crawler->filter('li.entry'));
1405 }
1406
1407 public function dataForLanguage()
1408 {
1409 return [
1410 'ru' => [
1411 'https://www.pravda.ru/world/09-06-2017/1337283-qatar-0/',
1412 'ru',
1413 ],
1414 'fr' => [
1415 'https://fr.wikipedia.org/wiki/Wallabag',
1416 'fr',
1417 ],
1418 'de' => [
1419 'https://www.bild.de/politik/ausland/theresa-may/wahlbeben-grossbritannien-analyse-52108924.bild.html',
1420 'de',
1421 ],
1422 'it' => [
1423 'https://www.ansa.it/sito/notizie/mondo/europa/2017/06/08/voto-gb-seggi-aperti-misure-sicurezza-rafforzate_0cb71f7f-e23b-4d5f-95ca-bc12296419f0.html',
1424 'it',
1425 ],
1426 'zh_CN' => [
1427 'http://www.hao123.com/shequ?__noscript__-=1',
1428 'zh_CN',
1429 ],
1430 'pt_BR' => [
1431 'https://politica.estadao.com.br/noticias/eleicoes,campanha-catatonica,70002491983',
1432 'pt_BR',
1433 ],
1434 'fucked_list_of_languages' => [
1435 'http://geocatalog.webservice-energy.org/geonetwork/srv/eng/main.home',
1436 null,
1437 ],
1438 'es-ES' => [
1439 'https://www.20minutos.es/noticia/3360685/0/gobierno-sanchez-primero-historia-mas-mujeres-que-hombres/',
1440 'es_ES',
1441 ],
1442 ];
1443 }
1444
1445 /**
1446 * @dataProvider dataForLanguage
1447 * @group NetworkCalls
1448 */
1449 public function testLanguageValidation($url, $expectedLanguage)
1450 {
1451 $this->logInAs('admin');
1452 $client = $this->getClient();
1453
1454 $crawler = $client->request('GET', '/new');
1455
1456 $this->assertSame(200, $client->getResponse()->getStatusCode());
1457
1458 $form = $crawler->filter('form[name=entry]')->form();
1459
1460 $data = [
1461 'entry[url]' => $url,
1462 ];
1463
1464 $client->submit($form, $data);
1465
1466 $this->assertSame(302, $client->getResponse()->getStatusCode());
1467
1468 $content = $client->getContainer()
1469 ->get('doctrine.orm.entity_manager')
1470 ->getRepository('WallabagCoreBundle:Entry')
1471 ->findByUrlAndUserId($url, $this->getLoggedInUserId());
1472
1473 $this->assertInstanceOf('Wallabag\CoreBundle\Entity\Entry', $content);
1474 $this->assertSame($url, $content->getUrl());
1475 $this->assertSame($expectedLanguage, $content->getLanguage());
1476 }
1477
1478 /**
1479 * @group NetworkCalls
1480 */
1481 public function testRestrictedArticle()
1482 {
1483 $url = 'https://www.monde-diplomatique.fr/2017/05/BONNET/57475';
1484 $this->logInAs('admin');
1485 $client = $this->getClient();
1486 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
1487
1488 // enable restricted access
1489 $client->getContainer()->get('craue_config')->set('restricted_access', 1);
1490
1491 // create a new site_credential
1492 $user = $client->getContainer()->get('security.token_storage')->getToken()->getUser();
1493 $credential = new SiteCredential($user);
1494 $credential->setHost('monde-diplomatique.fr');
1495 $credential->setUsername($client->getContainer()->get('wallabag_core.helper.crypto_proxy')->crypt('foo'));
1496 $credential->setPassword($client->getContainer()->get('wallabag_core.helper.crypto_proxy')->crypt('bar'));
1497
1498 $em->persist($credential);
1499 $em->flush();
1500
1501 $crawler = $client->request('GET', '/new');
1502
1503 $this->assertSame(200, $client->getResponse()->getStatusCode());
1504
1505 $form = $crawler->filter('form[name=entry]')->form();
1506
1507 $data = [
1508 'entry[url]' => $url,
1509 ];
1510
1511 $client->submit($form, $data);
1512
1513 $this->assertSame(302, $client->getResponse()->getStatusCode());
1514
1515 $crawler = $client->followRedirect();
1516
1517 $this->assertSame(200, $client->getResponse()->getStatusCode());
1518 $this->assertContains('flashes.entry.notice.entry_saved', $crawler->filter('body')->extract(['_text'])[0]);
1519
1520 $content = $em
1521 ->getRepository('WallabagCoreBundle:Entry')
1522 ->findByUrlAndUserId($url, $this->getLoggedInUserId());
1523
1524 $this->assertInstanceOf('Wallabag\CoreBundle\Entity\Entry', $content);
1525 $this->assertSame('Crimes et réformes aux Philippines', $content->getTitle());
1526
1527 $client->getContainer()->get('craue_config')->set('restricted_access', 0);
1528 }
1529
1530 public function testPostEntryWhenFetchFails()
1531 {
1532 $url = 'http://example.com/papers/email_tracking.pdf';
1533 $this->logInAs('admin');
1534 $client = $this->getClient();
1535
1536 $container = $client->getContainer();
1537 $contentProxy = $this->getMockBuilder(ContentProxy::class)
1538 ->disableOriginalConstructor()
1539 ->setMethods(['updateEntry'])
1540 ->getMock();
1541 $contentProxy->expects($this->any())
1542 ->method('updateEntry')
1543 ->willThrowException(new \Exception('Test Fetch content fails'));
1544
1545 $crawler = $client->request('GET', '/new');
1546
1547 $this->assertSame(200, $client->getResponse()->getStatusCode());
1548
1549 $form = $crawler->filter('form[name=entry]')->form();
1550
1551 $data = [
1552 'entry[url]' => $url,
1553 ];
1554
1555 /**
1556 * We generate a new client to be able to use Mock ContentProxy
1557 * Also we reinject the cookie from the previous client to keep the
1558 * session.
1559 */
1560 $cookie = $client->getCookieJar()->all();
1561 $client = $this->getNewClient();
1562 $client->getCookieJar()->set($cookie[0]);
1563 $client->getContainer()->set('wallabag_core.content_proxy', $contentProxy);
1564 $client->submit($form, $data);
1565
1566 $this->assertSame(302, $client->getResponse()->getStatusCode());
1567
1568 $content = $client->getContainer()
1569 ->get('doctrine.orm.entity_manager')
1570 ->getRepository('WallabagCoreBundle:Entry')
1571 ->findByUrlAndUserId($url, $this->getLoggedInUserId());
1572
1573 $authors = $content->getPublishedBy();
1574 $this->assertSame('email_tracking.pdf', $content->getTitle());
1575 $this->assertSame('example.com', $content->getDomainName());
1576 }
1577
1578 public function testEntryDeleteTagLink()
1579 {
1580 $this->logInAs('admin');
1581 $client = $this->getClient();
1582
1583 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
1584 $entry = $em->getRepository('WallabagCoreBundle:Entry')->findByUrlAndUserId('http://0.0.0.0/entry1', $this->getLoggedInUserId());
1585 $tag = $entry->getTags()[0];
1586
1587 $crawler = $client->request('GET', '/view/' . $entry->getId());
1588
1589 // As long as the deletion link of a tag is following
1590 // a link to the tag view, we take the second one to retrieve
1591 // the deletion link of the first tag
1592 $link = $crawler->filter('body div#article div.tools ul.tags li.chip a')->extract('href')[1];
1593
1594 $this->assertSame(sprintf('/remove-tag/%s/%s', $entry->getId(), $tag->getId()), $link);
1595 }
1596
1597 public function testRandom()
1598 {
1599 $this->logInAs('admin');
1600 $client = $this->getClient();
1601
1602 $client->request('GET', '/unread/random');
1603 $this->assertSame(302, $client->getResponse()->getStatusCode());
1604 $this->assertContains('/view/', $client->getResponse()->getTargetUrl(), 'Unread random');
1605
1606 $client->request('GET', '/starred/random');
1607 $this->assertSame(302, $client->getResponse()->getStatusCode());
1608 $this->assertContains('/view/', $client->getResponse()->getTargetUrl(), 'Starred random');
1609
1610 $client->request('GET', '/archive/random');
1611 $this->assertSame(302, $client->getResponse()->getStatusCode());
1612 $this->assertContains('/view/', $client->getResponse()->getTargetUrl(), 'Archive random');
1613
1614 $client->request('GET', '/untagged/random');
1615 $this->assertSame(302, $client->getResponse()->getStatusCode());
1616 $this->assertContains('/view/', $client->getResponse()->getTargetUrl(), 'Untagged random');
1617
1618 $client->request('GET', '/all/random');
1619 $this->assertSame(302, $client->getResponse()->getStatusCode());
1620 $this->assertContains('/view/', $client->getResponse()->getTargetUrl(), 'All random');
1621 }
1622
1623 public function testMass()
1624 {
1625 $this->logInAs('admin');
1626 $client = $this->getClient();
1627
1628 $entry1 = new Entry($this->getLoggedInUser());
1629 $entry1->setUrl($this->url);
1630 $this->getEntityManager()->persist($entry1);
1631
1632 $entry2 = new Entry($this->getLoggedInUser());
1633 $entry2->setUrl($this->url);
1634 $this->getEntityManager()->persist($entry2);
1635
1636 $this->getEntityManager()->flush();
1637 $this->getEntityManager()->clear();
1638
1639 $entries = [];
1640 $entries[] = $entry1->getId();
1641 $entries[] = $entry2->getId();
1642
1643 // Mass actions : archive
1644 $client->request('POST', '/mass', [
1645 'toggle-archive' => '',
1646 'entry-checkbox' => $entries,
1647 ]);
1648
1649 $this->assertSame(302, $client->getResponse()->getStatusCode());
1650
1651 $res = $client->getContainer()
1652 ->get('doctrine.orm.entity_manager')
1653 ->getRepository('WallabagCoreBundle:Entry')
1654 ->find($entry1->getId());
1655
1656 $this->assertSame(1, $res->isArchived());
1657
1658 $res = $client->getContainer()
1659 ->get('doctrine.orm.entity_manager')
1660 ->getRepository('WallabagCoreBundle:Entry')
1661 ->find($entry2->getId());
1662
1663 $this->assertSame(1, $res->isArchived());
1664
1665 // Mass actions : star
1666 $client->request('POST', '/mass', [
1667 'toggle-star' => '',
1668 'entry-checkbox' => $entries,
1669 ]);
1670
1671 $this->assertSame(302, $client->getResponse()->getStatusCode());
1672
1673 $res = $client->getContainer()
1674 ->get('doctrine.orm.entity_manager')
1675 ->getRepository('WallabagCoreBundle:Entry')
1676 ->find($entry1->getId());
1677
1678 $this->assertSame(1, $res->isStarred());
1679
1680 $res = $client->getContainer()
1681 ->get('doctrine.orm.entity_manager')
1682 ->getRepository('WallabagCoreBundle:Entry')
1683 ->find($entry2->getId());
1684
1685 $this->assertSame(1, $res->isStarred());
1686
1687 // Mass actions : delete
1688 $client->request('POST', '/mass', [
1689 'delete' => '',
1690 'entry-checkbox' => $entries,
1691 ]);
1692
1693 $client->request('GET', '/delete/' . $entry1->getId());
1694 $this->assertSame(404, $client->getResponse()->getStatusCode());
1695
1696 $client->request('GET', '/delete/' . $entry2->getId());
1697 $this->assertSame(404, $client->getResponse()->getStatusCode());
1698 }
1699 }