]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php
Merge pull request #4438 from wallabag/dependabot/composer/scheb/two-factor-bundle...
[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 testShareEntryPublicly()
1019 {
1020 $this->logInAs('admin');
1021 $client = $this->getClient();
1022
1023 // sharing is enabled
1024 $client->getContainer()->get('craue_config')->set('share_public', 1);
1025
1026 $content = new Entry($this->getLoggedInUser());
1027 $content->setUrl($this->url);
1028 $this->getEntityManager()->persist($content);
1029 $this->getEntityManager()->flush();
1030 $this->getEntityManager()->clear();
1031
1032 // no uid
1033 $client->request('GET', '/share/' . $content->getUid());
1034 $this->assertSame(404, $client->getResponse()->getStatusCode());
1035
1036 // generating the uid
1037 $client->request('GET', '/share/' . $content->getId());
1038 $this->assertSame(302, $client->getResponse()->getStatusCode());
1039
1040 $shareUrl = $client->getResponse()->getTargetUrl();
1041
1042 // use a new client to have a fresh empty session (instead of a logged one from the previous client)
1043 $client->restart();
1044
1045 $client->request('GET', $shareUrl);
1046
1047 $this->assertSame(200, $client->getResponse()->getStatusCode());
1048 $this->assertContains('max-age=25200', $client->getResponse()->headers->get('cache-control'));
1049 $this->assertContains('public', $client->getResponse()->headers->get('cache-control'));
1050 $this->assertContains('s-maxage=25200', $client->getResponse()->headers->get('cache-control'));
1051 $this->assertNotContains('no-cache', $client->getResponse()->headers->get('cache-control'));
1052 $this->assertContains('og:title', $client->getResponse()->getContent());
1053 $this->assertContains('og:type', $client->getResponse()->getContent());
1054 $this->assertContains('og:url', $client->getResponse()->getContent());
1055 $this->assertContains('og:image', $client->getResponse()->getContent());
1056
1057 // sharing is now disabled
1058 $client->getContainer()->get('craue_config')->set('share_public', 0);
1059 $client->request('GET', '/share/' . $content->getUid());
1060 $this->assertSame(404, $client->getResponse()->getStatusCode());
1061
1062 // removing the share
1063 $client->request('GET', '/share/delete/' . $content->getId());
1064 $this->assertSame(302, $client->getResponse()->getStatusCode());
1065
1066 // share is now disable
1067 $client->request('GET', '/share/' . $content->getUid());
1068 $this->assertSame(404, $client->getResponse()->getStatusCode());
1069 }
1070
1071 /**
1072 * @group NetworkCalls
1073 */
1074 public function testNewEntryWithDownloadImagesEnabled()
1075 {
1076 $this->downloadImagesEnabled = true;
1077 $this->logInAs('admin');
1078 $client = $this->getClient();
1079
1080 $url = self::AN_URL_CONTAINING_AN_ARTICLE_WITH_IMAGE;
1081 $client->getContainer()->get('craue_config')->set('download_images_enabled', 1);
1082
1083 $crawler = $client->request('GET', '/new');
1084
1085 $this->assertSame(200, $client->getResponse()->getStatusCode());
1086
1087 $form = $crawler->filter('form[name=entry]')->form();
1088
1089 $data = [
1090 'entry[url]' => $url,
1091 ];
1092
1093 $client->submit($form, $data);
1094
1095 $this->assertSame(302, $client->getResponse()->getStatusCode());
1096
1097 $em = $client->getContainer()
1098 ->get('doctrine.orm.entity_manager');
1099
1100 $entry = $em
1101 ->getRepository('WallabagCoreBundle:Entry')
1102 ->findByUrlAndUserId($url, $this->getLoggedInUserId());
1103
1104 $this->assertInstanceOf('Wallabag\CoreBundle\Entity\Entry', $entry);
1105 $this->assertSame($url, $entry->getUrl());
1106 $this->assertContains('Judo', $entry->getTitle());
1107 // instead of checking for the filename (which might change) check that the image is now local
1108 $this->assertContains(rtrim($client->getContainer()->getParameter('domain_name'), '/') . '/assets/images/', $entry->getContent());
1109
1110 $client->getContainer()->get('craue_config')->set('download_images_enabled', 0);
1111 }
1112
1113 /**
1114 * @depends testNewEntryWithDownloadImagesEnabled
1115 */
1116 public function testRemoveEntryWithDownloadImagesEnabled()
1117 {
1118 $this->downloadImagesEnabled = true;
1119 $this->logInAs('admin');
1120 $client = $this->getClient();
1121
1122 $url = self::AN_URL_CONTAINING_AN_ARTICLE_WITH_IMAGE;
1123 $client->getContainer()->get('craue_config')->set('download_images_enabled', 1);
1124
1125 $crawler = $client->request('GET', '/new');
1126
1127 $this->assertSame(200, $client->getResponse()->getStatusCode());
1128
1129 $form = $crawler->filter('form[name=entry]')->form();
1130
1131 $data = [
1132 'entry[url]' => $url,
1133 ];
1134
1135 $client->submit($form, $data);
1136
1137 $this->assertSame(302, $client->getResponse()->getStatusCode());
1138
1139 $content = $client->getContainer()
1140 ->get('doctrine.orm.entity_manager')
1141 ->getRepository('WallabagCoreBundle:Entry')
1142 ->findByUrlAndUserId($url, $this->getLoggedInUserId());
1143
1144 $client->request('GET', '/delete/' . $content->getId());
1145
1146 $this->assertSame(302, $client->getResponse()->getStatusCode());
1147
1148 $client->getContainer()->get('craue_config')->set('download_images_enabled', 0);
1149 }
1150
1151 public function testRedirectToHomepage()
1152 {
1153 $this->logInAs('empty');
1154 $client = $this->getClient();
1155
1156 // Redirect to homepage
1157 $config = $this->getLoggedInUser()->getConfig();
1158 $config->setActionMarkAsRead(Config::REDIRECT_TO_HOMEPAGE);
1159 $this->getEntityManager()->persist($config);
1160
1161 $entry = new Entry($this->getLoggedInUser());
1162 $entry->setUrl($this->url);
1163 $this->getEntityManager()->persist($entry);
1164
1165 $this->getEntityManager()->flush();
1166
1167 $client->request('GET', '/view/' . $entry->getId());
1168 $client->request('GET', '/archive/' . $entry->getId());
1169
1170 $this->assertSame(302, $client->getResponse()->getStatusCode());
1171 $this->assertSame('/', $client->getResponse()->headers->get('location'));
1172 }
1173
1174 public function testRedirectToCurrentPage()
1175 {
1176 $this->logInAs('empty');
1177 $client = $this->getClient();
1178
1179 // Redirect to current page
1180 $config = $this->getLoggedInUser()->getConfig();
1181 $config->setActionMarkAsRead(Config::REDIRECT_TO_CURRENT_PAGE);
1182 $this->getEntityManager()->persist($config);
1183
1184 $entry = new Entry($this->getLoggedInUser());
1185 $entry->setUrl($this->url);
1186 $this->getEntityManager()->persist($entry);
1187
1188 $this->getEntityManager()->flush();
1189
1190 $client->request('GET', '/view/' . $entry->getId());
1191 $client->request('GET', '/archive/' . $entry->getId());
1192
1193 $this->assertSame(302, $client->getResponse()->getStatusCode());
1194 $this->assertContains('/view/' . $entry->getId(), $client->getResponse()->headers->get('location'));
1195 }
1196
1197 public function testFilterOnHttpStatus()
1198 {
1199 $this->logInAs('admin');
1200 $client = $this->getClient();
1201
1202 $entry = new Entry($this->getLoggedInUser());
1203 $entry->setUrl('https://www.lemonde.fr/incorrect-url/');
1204 $entry->setHttpStatus(404);
1205 $this->getEntityManager()->persist($entry);
1206
1207 $this->getEntityManager()->flush();
1208
1209 $crawler = $client->request('GET', '/all/list');
1210 $form = $crawler->filter('button[id=submit-filter]')->form();
1211
1212 $data = [
1213 'entry_filter[httpStatus]' => 404,
1214 ];
1215
1216 $crawler = $client->submit($form, $data);
1217
1218 $this->assertCount(1, $crawler->filter('li.entry'));
1219
1220 $entry = new Entry($this->getLoggedInUser());
1221 $entry->setUrl($this->url);
1222 $entry->setHttpStatus(200);
1223 $this->getEntityManager()->persist($entry);
1224
1225 $entry = new Entry($this->getLoggedInUser());
1226 $entry->setUrl('http://www.nextinpact.com/news/101235-wallabag-alternative-libre-a-pocket-creuse-petit-a-petit-son-nid.htm');
1227 $entry->setHttpStatus(200);
1228 $this->getEntityManager()->persist($entry);
1229
1230 $this->getEntityManager()->flush();
1231
1232 $crawler = $client->request('GET', '/all/list');
1233 $form = $crawler->filter('button[id=submit-filter]')->form();
1234
1235 $data = [
1236 'entry_filter[httpStatus]' => 200,
1237 ];
1238
1239 $crawler = $client->submit($form, $data);
1240
1241 $this->assertCount(2, $crawler->filter('li.entry'));
1242
1243 $crawler = $client->request('GET', '/all/list');
1244 $form = $crawler->filter('button[id=submit-filter]')->form();
1245
1246 $data = [
1247 'entry_filter[httpStatus]' => 1024,
1248 ];
1249
1250 $crawler = $client->submit($form, $data);
1251
1252 $this->assertCount(8, $crawler->filter('li.entry'));
1253 }
1254
1255 public function testSearch()
1256 {
1257 $this->logInAs('admin');
1258 $client = $this->getClient();
1259
1260 $entry = new Entry($this->getLoggedInUser());
1261 $entry->setUrl($this->url);
1262 $entry->setTitle('test');
1263 $this->getEntityManager()->persist($entry);
1264 $this->getEntityManager()->flush();
1265
1266 // Search on unread list
1267 $crawler = $client->request('GET', '/unread/list');
1268
1269 $form = $crawler->filter('form[name=search]')->form();
1270 $data = [
1271 'search_entry[term]' => 'title',
1272 ];
1273
1274 $crawler = $client->submit($form, $data);
1275
1276 $this->assertCount(4, $crawler->filter('li.entry'));
1277
1278 // Search on starred list
1279 $crawler = $client->request('GET', '/starred/list');
1280
1281 $entry = new Entry($this->getLoggedInUser());
1282 $entry->setUrl('http://localhost/foo/bar');
1283 $entry->setTitle('testeur');
1284 $entry->setStarred(true);
1285 $this->getEntityManager()->persist($entry);
1286 $this->getEntityManager()->flush();
1287
1288 $form = $crawler->filter('form[name=search]')->form();
1289 $data = [
1290 'search_entry[term]' => 'testeur',
1291 ];
1292
1293 $crawler = $client->submit($form, $data);
1294
1295 $this->assertCount(1, $crawler->filter('li.entry'));
1296
1297 $crawler = $client->request('GET', '/archive/list');
1298
1299 // Added new article to test on archive list
1300 $entry = new Entry($this->getLoggedInUser());
1301 $entry->setUrl('http://0.0.0.0/foo/baz/qux');
1302 $entry->setTitle('Le manège');
1303 $entry->updateArchived(true);
1304 $this->getEntityManager()->persist($entry);
1305 $this->getEntityManager()->flush();
1306
1307 $form = $crawler->filter('form[name=search]')->form();
1308 $data = [
1309 'search_entry[term]' => 'manège',
1310 ];
1311
1312 $crawler = $client->submit($form, $data);
1313
1314 $this->assertCount(1, $crawler->filter('li.entry'));
1315 $client->request('GET', '/delete/' . $entry->getId());
1316
1317 // test on list of all articles
1318 $crawler = $client->request('GET', '/all/list');
1319
1320 $form = $crawler->filter('form[name=search]')->form();
1321 $data = [
1322 'search_entry[term]' => 'wxcvbnqsdf', // a string not available in the database
1323 ];
1324
1325 $crawler = $client->submit($form, $data);
1326
1327 $this->assertCount(0, $crawler->filter('li.entry'));
1328
1329 // test url search on list of all articles
1330 $entry = new Entry($this->getLoggedInUser());
1331 $entry->setUrl('http://domain/qux');
1332 $entry->setTitle('Le manège');
1333 $entry->updateArchived(true);
1334 $this->getEntityManager()->persist($entry);
1335 $this->getEntityManager()->flush();
1336
1337 $crawler = $client->request('GET', '/all/list');
1338
1339 $form = $crawler->filter('form[name=search]')->form();
1340 $data = [
1341 'search_entry[term]' => 'domain', // the search will match an entry with 'domain' in its url
1342 ];
1343
1344 $crawler = $client->submit($form, $data);
1345
1346 $this->assertCount(1, $crawler->filter('li.entry'));
1347
1348 // same as previous test but for case-sensitivity
1349 $crawler = $client->request('GET', '/all/list');
1350
1351 $form = $crawler->filter('form[name=search]')->form();
1352 $data = [
1353 'search_entry[term]' => 'doMain', // the search will match an entry with 'domain' in its url
1354 ];
1355
1356 $crawler = $client->submit($form, $data);
1357
1358 $this->assertCount(1, $crawler->filter('li.entry'));
1359 }
1360
1361 public function dataForLanguage()
1362 {
1363 return [
1364 'ru' => [
1365 'https://www.pravda.ru/world/09-06-2017/1337283-qatar-0/',
1366 'ru',
1367 ],
1368 'fr' => [
1369 'https://fr.wikipedia.org/wiki/Wallabag',
1370 'fr',
1371 ],
1372 'de' => [
1373 'https://www.bild.de/politik/ausland/theresa-may/wahlbeben-grossbritannien-analyse-52108924.bild.html',
1374 'de',
1375 ],
1376 'it' => [
1377 'https://www.ansa.it/sito/notizie/mondo/europa/2017/06/08/voto-gb-seggi-aperti-misure-sicurezza-rafforzate_0cb71f7f-e23b-4d5f-95ca-bc12296419f0.html',
1378 'it',
1379 ],
1380 'zh_CN' => [
1381 'http://www.hao123.com/shequ?__noscript__-=1',
1382 'zh_CN',
1383 ],
1384 'pt_BR' => [
1385 'https://politica.estadao.com.br/noticias/eleicoes,campanha-catatonica,70002491983',
1386 'pt_BR',
1387 ],
1388 'fucked_list_of_languages' => [
1389 'http://geocatalog.webservice-energy.org/geonetwork/srv/eng/main.home',
1390 null,
1391 ],
1392 'es-ES' => [
1393 'https://www.20minutos.es/noticia/3360685/0/gobierno-sanchez-primero-historia-mas-mujeres-que-hombres/',
1394 'es_ES',
1395 ],
1396 ];
1397 }
1398
1399 /**
1400 * @dataProvider dataForLanguage
1401 * @group NetworkCalls
1402 */
1403 public function testLanguageValidation($url, $expectedLanguage)
1404 {
1405 $this->logInAs('admin');
1406 $client = $this->getClient();
1407
1408 $crawler = $client->request('GET', '/new');
1409
1410 $this->assertSame(200, $client->getResponse()->getStatusCode());
1411
1412 $form = $crawler->filter('form[name=entry]')->form();
1413
1414 $data = [
1415 'entry[url]' => $url,
1416 ];
1417
1418 $client->submit($form, $data);
1419
1420 $this->assertSame(302, $client->getResponse()->getStatusCode());
1421
1422 $content = $client->getContainer()
1423 ->get('doctrine.orm.entity_manager')
1424 ->getRepository('WallabagCoreBundle:Entry')
1425 ->findByUrlAndUserId($url, $this->getLoggedInUserId());
1426
1427 $this->assertInstanceOf('Wallabag\CoreBundle\Entity\Entry', $content);
1428 $this->assertSame($url, $content->getUrl());
1429 $this->assertSame($expectedLanguage, $content->getLanguage());
1430 }
1431
1432 /**
1433 * @group NetworkCalls
1434 */
1435 public function testRestrictedArticle()
1436 {
1437 $url = 'https://www.monde-diplomatique.fr/2017/05/BONNET/57475';
1438 $this->logInAs('admin');
1439 $client = $this->getClient();
1440 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
1441
1442 // enable restricted access
1443 $client->getContainer()->get('craue_config')->set('restricted_access', 1);
1444
1445 // create a new site_credential
1446 $user = $client->getContainer()->get('security.token_storage')->getToken()->getUser();
1447 $credential = new SiteCredential($user);
1448 $credential->setHost('monde-diplomatique.fr');
1449 $credential->setUsername($client->getContainer()->get('wallabag_core.helper.crypto_proxy')->crypt('foo'));
1450 $credential->setPassword($client->getContainer()->get('wallabag_core.helper.crypto_proxy')->crypt('bar'));
1451
1452 $em->persist($credential);
1453 $em->flush();
1454
1455 $crawler = $client->request('GET', '/new');
1456
1457 $this->assertSame(200, $client->getResponse()->getStatusCode());
1458
1459 $form = $crawler->filter('form[name=entry]')->form();
1460
1461 $data = [
1462 'entry[url]' => $url,
1463 ];
1464
1465 $client->submit($form, $data);
1466
1467 $this->assertSame(302, $client->getResponse()->getStatusCode());
1468
1469 $crawler = $client->followRedirect();
1470
1471 $this->assertSame(200, $client->getResponse()->getStatusCode());
1472 $this->assertContains('flashes.entry.notice.entry_saved', $crawler->filter('body')->extract(['_text'])[0]);
1473
1474 $content = $em
1475 ->getRepository('WallabagCoreBundle:Entry')
1476 ->findByUrlAndUserId($url, $this->getLoggedInUserId());
1477
1478 $this->assertInstanceOf('Wallabag\CoreBundle\Entity\Entry', $content);
1479 $this->assertSame('Crimes et réformes aux Philippines', $content->getTitle());
1480
1481 $client->getContainer()->get('craue_config')->set('restricted_access', 0);
1482 }
1483
1484 public function testPostEntryWhenFetchFails()
1485 {
1486 $url = 'http://example.com/papers/email_tracking.pdf';
1487 $this->logInAs('admin');
1488 $client = $this->getClient();
1489
1490 $container = $client->getContainer();
1491 $contentProxy = $this->getMockBuilder(ContentProxy::class)
1492 ->disableOriginalConstructor()
1493 ->setMethods(['updateEntry'])
1494 ->getMock();
1495 $contentProxy->expects($this->any())
1496 ->method('updateEntry')
1497 ->willThrowException(new \Exception('Test Fetch content fails'));
1498
1499 $crawler = $client->request('GET', '/new');
1500
1501 $this->assertSame(200, $client->getResponse()->getStatusCode());
1502
1503 $form = $crawler->filter('form[name=entry]')->form();
1504
1505 $data = [
1506 'entry[url]' => $url,
1507 ];
1508
1509 /**
1510 * We generate a new client to be able to use Mock ContentProxy
1511 * Also we reinject the cookie from the previous client to keep the
1512 * session.
1513 */
1514 $cookie = $client->getCookieJar()->all();
1515 $client = $this->getNewClient();
1516 $client->getCookieJar()->set($cookie[0]);
1517 $client->getContainer()->set('wallabag_core.content_proxy', $contentProxy);
1518 $client->submit($form, $data);
1519
1520 $this->assertSame(302, $client->getResponse()->getStatusCode());
1521
1522 $content = $client->getContainer()
1523 ->get('doctrine.orm.entity_manager')
1524 ->getRepository('WallabagCoreBundle:Entry')
1525 ->findByUrlAndUserId($url, $this->getLoggedInUserId());
1526
1527 $authors = $content->getPublishedBy();
1528 $this->assertSame('email_tracking.pdf', $content->getTitle());
1529 $this->assertSame('example.com', $content->getDomainName());
1530 }
1531
1532 public function testEntryDeleteTagLink()
1533 {
1534 $this->logInAs('admin');
1535 $client = $this->getClient();
1536
1537 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
1538 $entry = $em->getRepository('WallabagCoreBundle:Entry')->findByUrlAndUserId('http://0.0.0.0/entry1', $this->getLoggedInUserId());
1539 $tag = $entry->getTags()[0];
1540
1541 $crawler = $client->request('GET', '/view/' . $entry->getId());
1542
1543 // As long as the deletion link of a tag is following
1544 // a link to the tag view, we take the second one to retrieve
1545 // the deletion link of the first tag
1546 $link = $crawler->filter('body div#article div.tools ul.tags li.chip a')->extract('href')[1];
1547
1548 $this->assertSame(sprintf('/remove-tag/%s/%s', $entry->getId(), $tag->getId()), $link);
1549 }
1550
1551 public function testRandom()
1552 {
1553 $this->logInAs('admin');
1554 $client = $this->getClient();
1555
1556 $client->request('GET', '/unread/random');
1557 $this->assertSame(302, $client->getResponse()->getStatusCode());
1558 $this->assertContains('/view/', $client->getResponse()->getTargetUrl(), 'Unread random');
1559
1560 $client->request('GET', '/starred/random');
1561 $this->assertSame(302, $client->getResponse()->getStatusCode());
1562 $this->assertContains('/view/', $client->getResponse()->getTargetUrl(), 'Starred random');
1563
1564 $client->request('GET', '/archive/random');
1565 $this->assertSame(302, $client->getResponse()->getStatusCode());
1566 $this->assertContains('/view/', $client->getResponse()->getTargetUrl(), 'Archive random');
1567
1568 $client->request('GET', '/untagged/random');
1569 $this->assertSame(302, $client->getResponse()->getStatusCode());
1570 $this->assertContains('/view/', $client->getResponse()->getTargetUrl(), 'Untagged random');
1571
1572 $client->request('GET', '/all/random');
1573 $this->assertSame(302, $client->getResponse()->getStatusCode());
1574 $this->assertContains('/view/', $client->getResponse()->getTargetUrl(), 'All random');
1575 }
1576
1577 public function testMass()
1578 {
1579 $this->logInAs('admin');
1580 $client = $this->getClient();
1581
1582 $entry1 = new Entry($this->getLoggedInUser());
1583 $entry1->setUrl($this->url);
1584 $this->getEntityManager()->persist($entry1);
1585
1586 $entry2 = new Entry($this->getLoggedInUser());
1587 $entry2->setUrl($this->url);
1588 $this->getEntityManager()->persist($entry2);
1589
1590 $this->getEntityManager()->flush();
1591 $this->getEntityManager()->clear();
1592
1593 $entries = [];
1594 $entries[] = $entry1->getId();
1595 $entries[] = $entry2->getId();
1596
1597 // Mass actions : archive
1598 $client->request('POST', '/mass', [
1599 'toggle-archive' => '',
1600 'entry-checkbox' => $entries,
1601 ]);
1602
1603 $this->assertSame(302, $client->getResponse()->getStatusCode());
1604
1605 $res = $client->getContainer()
1606 ->get('doctrine.orm.entity_manager')
1607 ->getRepository('WallabagCoreBundle:Entry')
1608 ->find($entry1->getId());
1609
1610 $this->assertSame(1, $res->isArchived());
1611
1612 $res = $client->getContainer()
1613 ->get('doctrine.orm.entity_manager')
1614 ->getRepository('WallabagCoreBundle:Entry')
1615 ->find($entry2->getId());
1616
1617 $this->assertSame(1, $res->isArchived());
1618
1619 // Mass actions : star
1620 $client->request('POST', '/mass', [
1621 'toggle-star' => '',
1622 'entry-checkbox' => $entries,
1623 ]);
1624
1625 $this->assertSame(302, $client->getResponse()->getStatusCode());
1626
1627 $res = $client->getContainer()
1628 ->get('doctrine.orm.entity_manager')
1629 ->getRepository('WallabagCoreBundle:Entry')
1630 ->find($entry1->getId());
1631
1632 $this->assertSame(1, $res->isStarred());
1633
1634 $res = $client->getContainer()
1635 ->get('doctrine.orm.entity_manager')
1636 ->getRepository('WallabagCoreBundle:Entry')
1637 ->find($entry2->getId());
1638
1639 $this->assertSame(1, $res->isStarred());
1640
1641 // Mass actions : delete
1642 $client->request('POST', '/mass', [
1643 'delete' => '',
1644 'entry-checkbox' => $entries,
1645 ]);
1646
1647 $client->request('GET', '/delete/' . $entry1->getId());
1648 $this->assertSame(404, $client->getResponse()->getStatusCode());
1649
1650 $client->request('GET', '/delete/' . $entry2->getId());
1651 $this->assertSame(404, $client->getResponse()->getStatusCode());
1652 }
1653 }