diff options
Diffstat (limited to 'tests/Wallabag/CoreBundle/Helper')
-rw-r--r-- | tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php | 203 | ||||
-rw-r--r-- | tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php | 24 | ||||
-rw-r--r-- | tests/Wallabag/CoreBundle/Helper/RedirectTest.php | 18 |
3 files changed, 214 insertions, 31 deletions
diff --git a/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php b/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php index 77dfd5bf..a3570125 100644 --- a/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php +++ b/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php | |||
@@ -3,12 +3,14 @@ | |||
3 | namespace Tests\Wallabag\CoreBundle\Helper; | 3 | namespace Tests\Wallabag\CoreBundle\Helper; |
4 | 4 | ||
5 | use Psr\Log\NullLogger; | 5 | use Psr\Log\NullLogger; |
6 | use Monolog\Logger; | ||
7 | use Monolog\Handler\TestHandler; | ||
6 | use Wallabag\CoreBundle\Helper\ContentProxy; | 8 | use Wallabag\CoreBundle\Helper\ContentProxy; |
7 | use Wallabag\CoreBundle\Entity\Entry; | 9 | use Wallabag\CoreBundle\Entity\Entry; |
8 | use Wallabag\CoreBundle\Entity\Tag; | 10 | use Wallabag\CoreBundle\Entity\Tag; |
9 | use Wallabag\UserBundle\Entity\User; | 11 | use Wallabag\UserBundle\Entity\User; |
10 | use Wallabag\CoreBundle\Repository\TagRepository; | ||
11 | use Wallabag\CoreBundle\Helper\RuleBasedTagger; | 12 | use Wallabag\CoreBundle\Helper\RuleBasedTagger; |
13 | use Graby\Graby; | ||
12 | 14 | ||
13 | class ContentProxyTest extends \PHPUnit_Framework_TestCase | 15 | class ContentProxyTest extends \PHPUnit_Framework_TestCase |
14 | { | 16 | { |
@@ -36,7 +38,8 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
36 | ]); | 38 | ]); |
37 | 39 | ||
38 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); | 40 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); |
39 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://user@:80'); | 41 | $entry = new Entry(new User()); |
42 | $proxy->updateEntry($entry, 'http://user@:80'); | ||
40 | 43 | ||
41 | $this->assertEquals('http://user@:80', $entry->getUrl()); | 44 | $this->assertEquals('http://user@:80', $entry->getUrl()); |
42 | $this->assertEmpty($entry->getTitle()); | 45 | $this->assertEmpty($entry->getTitle()); |
@@ -70,7 +73,8 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
70 | ]); | 73 | ]); |
71 | 74 | ||
72 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); | 75 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); |
73 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0'); | 76 | $entry = new Entry(new User()); |
77 | $proxy->updateEntry($entry, 'http://0.0.0.0'); | ||
74 | 78 | ||
75 | $this->assertEquals('http://0.0.0.0', $entry->getUrl()); | 79 | $this->assertEquals('http://0.0.0.0', $entry->getUrl()); |
76 | $this->assertEmpty($entry->getTitle()); | 80 | $this->assertEmpty($entry->getTitle()); |
@@ -109,7 +113,8 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
109 | ]); | 113 | ]); |
110 | 114 | ||
111 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); | 115 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); |
112 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://domain.io'); | 116 | $entry = new Entry(new User()); |
117 | $proxy->updateEntry($entry, 'http://domain.io'); | ||
113 | 118 | ||
114 | $this->assertEquals('http://domain.io', $entry->getUrl()); | 119 | $this->assertEquals('http://domain.io', $entry->getUrl()); |
115 | $this->assertEquals('my title', $entry->getTitle()); | 120 | $this->assertEquals('my title', $entry->getTitle()); |
@@ -150,7 +155,8 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
150 | ]); | 155 | ]); |
151 | 156 | ||
152 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); | 157 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); |
153 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0'); | 158 | $entry = new Entry(new User()); |
159 | $proxy->updateEntry($entry, 'http://0.0.0.0'); | ||
154 | 160 | ||
155 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); | 161 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); |
156 | $this->assertEquals('this is my title', $entry->getTitle()); | 162 | $this->assertEquals('this is my title', $entry->getTitle()); |
@@ -191,7 +197,8 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
191 | ]); | 197 | ]); |
192 | 198 | ||
193 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); | 199 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); |
194 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0'); | 200 | $entry = new Entry(new User()); |
201 | $proxy->updateEntry($entry, 'http://0.0.0.0'); | ||
195 | 202 | ||
196 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); | 203 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); |
197 | $this->assertEquals('this is my title', $entry->getTitle()); | 204 | $this->assertEquals('this is my title', $entry->getTitle()); |
@@ -210,16 +217,62 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
210 | $tagger->expects($this->once()) | 217 | $tagger->expects($this->once()) |
211 | ->method('tag'); | 218 | ->method('tag'); |
212 | 219 | ||
213 | $graby = $this->getMockBuilder('Graby\Graby')->getMock(); | 220 | $proxy = new ContentProxy((new Graby()), $tagger, $this->getLogger(), $this->fetchingErrorMessage); |
221 | $entry = new Entry(new User()); | ||
222 | $proxy->updateEntry( | ||
223 | $entry, | ||
224 | 'http://0.0.0.0', | ||
225 | [ | ||
226 | 'html' => str_repeat('this is my content', 325), | ||
227 | 'title' => 'this is my title', | ||
228 | 'url' => 'http://1.1.1.1', | ||
229 | 'content_type' => 'text/html', | ||
230 | 'language' => 'fr', | ||
231 | 'date' => '1395635872', | ||
232 | 'authors' => ['Jeremy', 'Nico', 'Thomas'], | ||
233 | 'all_headers' => [ | ||
234 | 'Cache-Control' => 'no-cache', | ||
235 | ], | ||
236 | ] | ||
237 | ); | ||
214 | 238 | ||
215 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); | 239 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); |
216 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0', [ | 240 | $this->assertEquals('this is my title', $entry->getTitle()); |
217 | 'html' => str_repeat('this is my content', 325), | 241 | $this->assertContains('this is my content', $entry->getContent()); |
218 | 'title' => 'this is my title', | 242 | $this->assertEquals('text/html', $entry->getMimetype()); |
219 | 'url' => 'http://1.1.1.1', | 243 | $this->assertEquals('fr', $entry->getLanguage()); |
220 | 'content_type' => 'text/html', | 244 | $this->assertEquals(4.0, $entry->getReadingTime()); |
221 | 'language' => 'fr', | 245 | $this->assertEquals('1.1.1.1', $entry->getDomainName()); |
222 | ]); | 246 | $this->assertEquals('24/03/2014', $entry->getPublishedAt()->format('d/m/Y')); |
247 | $this->assertContains('Jeremy', $entry->getPublishedBy()); | ||
248 | $this->assertContains('Nico', $entry->getPublishedBy()); | ||
249 | $this->assertContains('Thomas', $entry->getPublishedBy()); | ||
250 | $this->assertContains('no-cache', $entry->getHeaders()); | ||
251 | } | ||
252 | |||
253 | public function testWithForcedContentAndDatetime() | ||
254 | { | ||
255 | $tagger = $this->getTaggerMock(); | ||
256 | $tagger->expects($this->once()) | ||
257 | ->method('tag'); | ||
258 | |||
259 | $logHandler = new TestHandler(); | ||
260 | $logger = new Logger('test', [$logHandler]); | ||
261 | |||
262 | $proxy = new ContentProxy((new Graby()), $tagger, $logger, $this->fetchingErrorMessage); | ||
263 | $entry = new Entry(new User()); | ||
264 | $proxy->updateEntry( | ||
265 | $entry, | ||
266 | 'http://1.1.1.1', | ||
267 | [ | ||
268 | 'html' => str_repeat('this is my content', 325), | ||
269 | 'title' => 'this is my title', | ||
270 | 'url' => 'http://1.1.1.1', | ||
271 | 'content_type' => 'text/html', | ||
272 | 'language' => 'fr', | ||
273 | 'date' => '2016-09-08T11:55:58+0200', | ||
274 | ] | ||
275 | ); | ||
223 | 276 | ||
224 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); | 277 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); |
225 | $this->assertEquals('this is my title', $entry->getTitle()); | 278 | $this->assertEquals('this is my title', $entry->getTitle()); |
@@ -228,32 +281,126 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
228 | $this->assertEquals('fr', $entry->getLanguage()); | 281 | $this->assertEquals('fr', $entry->getLanguage()); |
229 | $this->assertEquals(4.0, $entry->getReadingTime()); | 282 | $this->assertEquals(4.0, $entry->getReadingTime()); |
230 | $this->assertEquals('1.1.1.1', $entry->getDomainName()); | 283 | $this->assertEquals('1.1.1.1', $entry->getDomainName()); |
284 | $this->assertEquals('08/09/2016', $entry->getPublishedAt()->format('d/m/Y')); | ||
231 | } | 285 | } |
232 | 286 | ||
233 | public function testTaggerThrowException() | 287 | public function testWithForcedContentAndBadDate() |
234 | { | 288 | { |
235 | $graby = $this->getMockBuilder('Graby\Graby') | 289 | $tagger = $this->getTaggerMock(); |
236 | ->disableOriginalConstructor() | 290 | $tagger->expects($this->once()) |
237 | ->getMock(); | 291 | ->method('tag'); |
238 | 292 | ||
293 | $logger = new Logger('foo'); | ||
294 | $handler = new TestHandler(); | ||
295 | $logger->pushHandler($handler); | ||
296 | |||
297 | $proxy = new ContentProxy((new Graby()), $tagger, $logger, $this->fetchingErrorMessage); | ||
298 | $entry = new Entry(new User()); | ||
299 | $proxy->updateEntry( | ||
300 | $entry, | ||
301 | 'http://1.1.1.1', | ||
302 | [ | ||
303 | 'html' => str_repeat('this is my content', 325), | ||
304 | 'title' => 'this is my title', | ||
305 | 'url' => 'http://1.1.1.1', | ||
306 | 'content_type' => 'text/html', | ||
307 | 'language' => 'fr', | ||
308 | 'date' => '01 02 2012', | ||
309 | ] | ||
310 | ); | ||
311 | |||
312 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); | ||
313 | $this->assertEquals('this is my title', $entry->getTitle()); | ||
314 | $this->assertContains('this is my content', $entry->getContent()); | ||
315 | $this->assertEquals('text/html', $entry->getMimetype()); | ||
316 | $this->assertEquals('fr', $entry->getLanguage()); | ||
317 | $this->assertEquals(4.0, $entry->getReadingTime()); | ||
318 | $this->assertEquals('1.1.1.1', $entry->getDomainName()); | ||
319 | $this->assertNull($entry->getPublishedAt()); | ||
320 | |||
321 | $records = $handler->getRecords(); | ||
322 | |||
323 | $this->assertCount(1, $records); | ||
324 | $this->assertContains('Error while defining date', $records[0]['message']); | ||
325 | } | ||
326 | |||
327 | public function testTaggerThrowException() | ||
328 | { | ||
239 | $tagger = $this->getTaggerMock(); | 329 | $tagger = $this->getTaggerMock(); |
240 | $tagger->expects($this->once()) | 330 | $tagger->expects($this->once()) |
241 | ->method('tag') | 331 | ->method('tag') |
242 | ->will($this->throwException(new \Exception())); | 332 | ->will($this->throwException(new \Exception())); |
243 | 333 | ||
244 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); | 334 | $proxy = new ContentProxy((new Graby()), $tagger, $this->getLogger(), $this->fetchingErrorMessage); |
245 | 335 | $entry = new Entry(new User()); | |
246 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0', [ | 336 | $proxy->updateEntry( |
247 | 'html' => str_repeat('this is my content', 325), | 337 | $entry, |
248 | 'title' => 'this is my title', | 338 | 'http://1.1.1.1', |
249 | 'url' => 'http://1.1.1.1', | 339 | [ |
250 | 'content_type' => 'text/html', | 340 | 'html' => str_repeat('this is my content', 325), |
251 | 'language' => 'fr', | 341 | 'title' => 'this is my title', |
252 | ]); | 342 | 'url' => 'http://1.1.1.1', |
343 | 'content_type' => 'text/html', | ||
344 | 'language' => 'fr', | ||
345 | ] | ||
346 | ); | ||
253 | 347 | ||
254 | $this->assertCount(0, $entry->getTags()); | 348 | $this->assertCount(0, $entry->getTags()); |
255 | } | 349 | } |
256 | 350 | ||
351 | public function dataForCrazyHtml() | ||
352 | { | ||
353 | return [ | ||
354 | 'script and comment' => [ | ||
355 | '<strong>Script inside:</strong> <!--[if gte IE 4]><script>alert(\'lol\');</script><![endif]--><br />', | ||
356 | 'lol', | ||
357 | ], | ||
358 | 'script' => [ | ||
359 | '<strong>Script inside:</strong><script>alert(\'lol\');</script>', | ||
360 | 'script', | ||
361 | ], | ||
362 | ]; | ||
363 | } | ||
364 | |||
365 | /** | ||
366 | * @dataProvider dataForCrazyHtml | ||
367 | */ | ||
368 | public function testWithCrazyHtmlContent($html, $escapedString) | ||
369 | { | ||
370 | $tagger = $this->getTaggerMock(); | ||
371 | $tagger->expects($this->once()) | ||
372 | ->method('tag'); | ||
373 | |||
374 | $proxy = new ContentProxy((new Graby()), $tagger, $this->getLogger(), $this->fetchingErrorMessage); | ||
375 | $entry = new Entry(new User()); | ||
376 | $proxy->updateEntry( | ||
377 | $entry, | ||
378 | 'http://1.1.1.1', | ||
379 | [ | ||
380 | 'html' => $html, | ||
381 | 'title' => 'this is my title', | ||
382 | 'url' => 'http://1.1.1.1', | ||
383 | 'content_type' => 'text/html', | ||
384 | 'language' => 'fr', | ||
385 | 'status' => '200', | ||
386 | 'open_graph' => [ | ||
387 | 'og_title' => 'my OG title', | ||
388 | 'og_description' => 'OG desc', | ||
389 | 'og_image' => 'http://3.3.3.3/cover.jpg', | ||
390 | ], | ||
391 | ] | ||
392 | ); | ||
393 | |||
394 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); | ||
395 | $this->assertEquals('this is my title', $entry->getTitle()); | ||
396 | $this->assertNotContains($escapedString, $entry->getContent()); | ||
397 | $this->assertEquals('http://3.3.3.3/cover.jpg', $entry->getPreviewPicture()); | ||
398 | $this->assertEquals('text/html', $entry->getMimetype()); | ||
399 | $this->assertEquals('fr', $entry->getLanguage()); | ||
400 | $this->assertEquals('200', $entry->getHttpStatus()); | ||
401 | $this->assertEquals('1.1.1.1', $entry->getDomainName()); | ||
402 | } | ||
403 | |||
257 | private function getTaggerMock() | 404 | private function getTaggerMock() |
258 | { | 405 | { |
259 | return $this->getMockBuilder(RuleBasedTagger::class) | 406 | return $this->getMockBuilder(RuleBasedTagger::class) |
diff --git a/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php b/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php index 85f12d87..9125f8dc 100644 --- a/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php +++ b/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php | |||
@@ -12,7 +12,24 @@ use GuzzleHttp\Stream\Stream; | |||
12 | 12 | ||
13 | class DownloadImagesTest extends \PHPUnit_Framework_TestCase | 13 | class DownloadImagesTest extends \PHPUnit_Framework_TestCase |
14 | { | 14 | { |
15 | public function testProcessHtml() | 15 | public function dataForSuccessImage() |
16 | { | ||
17 | return [ | ||
18 | 'imgur' => [ | ||
19 | '<div><img src="http://i.imgur.com/T9qgcHc.jpg" /></div>', | ||
20 | 'http://imgur.com/gallery/WxtWY', | ||
21 | ], | ||
22 | 'image with &' => [ | ||
23 | '<div><img src="https://i2.wp.com/www.tvaddons.ag/wp-content/uploads/2017/01/Screen-Shot-2017-01-07-at-10.17.40-PM.jpg?w=640&ssl=1" /></div>', | ||
24 | 'https://www.tvaddons.ag/realdebrid-kodi-jarvis/', | ||
25 | ], | ||
26 | ]; | ||
27 | } | ||
28 | |||
29 | /** | ||
30 | * @dataProvider dataForSuccessImage | ||
31 | */ | ||
32 | public function testProcessHtml($html, $url) | ||
16 | { | 33 | { |
17 | $client = new Client(); | 34 | $client = new Client(); |
18 | 35 | ||
@@ -27,9 +44,10 @@ class DownloadImagesTest extends \PHPUnit_Framework_TestCase | |||
27 | 44 | ||
28 | $download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', 'http://wallabag.io/', $logger); | 45 | $download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', 'http://wallabag.io/', $logger); |
29 | 46 | ||
30 | $res = $download->processHtml(123, '<div><img src="http://i.imgur.com/T9qgcHc.jpg" /></div>', 'http://imgur.com/gallery/WxtWY'); | 47 | $res = $download->processHtml(123, $html, $url); |
31 | 48 | ||
32 | $this->assertContains('http://wallabag.io/assets/images/9/b/9b0ead26/c638b4c2.png', $res); | 49 | // this the base path of all image (since it's calculated using the entry id: 123) |
50 | $this->assertContains('http://wallabag.io/assets/images/9/b/9b0ead26/', $res); | ||
33 | } | 51 | } |
34 | 52 | ||
35 | public function testProcessHtmlWithBadImage() | 53 | public function testProcessHtmlWithBadImage() |
diff --git a/tests/Wallabag/CoreBundle/Helper/RedirectTest.php b/tests/Wallabag/CoreBundle/Helper/RedirectTest.php index 0539f20a..f420d06a 100644 --- a/tests/Wallabag/CoreBundle/Helper/RedirectTest.php +++ b/tests/Wallabag/CoreBundle/Helper/RedirectTest.php | |||
@@ -89,4 +89,22 @@ class RedirectTest extends \PHPUnit_Framework_TestCase | |||
89 | 89 | ||
90 | $this->assertEquals($this->routerMock->generate('homepage'), $redirectUrl); | 90 | $this->assertEquals($this->routerMock->generate('homepage'), $redirectUrl); |
91 | } | 91 | } |
92 | |||
93 | public function testUserForRedirectWithIgnoreActionMarkAsRead() | ||
94 | { | ||
95 | $this->token->getUser()->getConfig()->setActionMarkAsRead(Config::REDIRECT_TO_HOMEPAGE); | ||
96 | |||
97 | $redirectUrl = $this->redirect->to('/unread/list', '', true); | ||
98 | |||
99 | $this->assertEquals('/unread/list', $redirectUrl); | ||
100 | } | ||
101 | |||
102 | public function testUserForRedirectNullWithFallbackWithIgnoreActionMarkAsRead() | ||
103 | { | ||
104 | $this->token->getUser()->getConfig()->setActionMarkAsRead(Config::REDIRECT_TO_HOMEPAGE); | ||
105 | |||
106 | $redirectUrl = $this->redirect->to(null, 'fallback', true); | ||
107 | |||
108 | $this->assertEquals('fallback', $redirectUrl); | ||
109 | } | ||
92 | } | 110 | } |