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