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