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