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