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