aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php')
-rw-r--r--tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php154
1 files changed, 145 insertions, 9 deletions
diff --git a/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php b/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php
index 44fca073..0c715d90 100644
--- a/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php
+++ b/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php
@@ -8,6 +8,9 @@ use Wallabag\CoreBundle\Entity\Entry;
8use Wallabag\CoreBundle\Entity\Tag; 8use Wallabag\CoreBundle\Entity\Tag;
9use Wallabag\UserBundle\Entity\User; 9use Wallabag\UserBundle\Entity\User;
10use Wallabag\CoreBundle\Helper\RuleBasedTagger; 10use Wallabag\CoreBundle\Helper\RuleBasedTagger;
11use Graby\Graby;
12use Monolog\Handler\TestHandler;
13use Monolog\Logger;
11 14
12class ContentProxyTest extends \PHPUnit_Framework_TestCase 15class ContentProxyTest extends \PHPUnit_Framework_TestCase
13{ 16{
@@ -209,16 +212,91 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase
209 $tagger->expects($this->once()) 212 $tagger->expects($this->once())
210 ->method('tag'); 213 ->method('tag');
211 214
212 $graby = $this->getMockBuilder('Graby\Graby')->getMock(); 215 $proxy = new ContentProxy((new Graby()), $tagger, $this->getLogger(), $this->fetchingErrorMessage);
216 $entry = $proxy->updateEntry(
217 new Entry(new User()),
218 'http://0.0.0.0',
219 [
220 'html' => str_repeat('this is my content', 325),
221 'title' => 'this is my title',
222 'url' => 'http://1.1.1.1',
223 'content_type' => 'text/html',
224 'language' => 'fr',
225 'date' => '1395635872',
226 'authors' => ['Jeremy', 'Nico', 'Thomas'],
227 'all_headers' => [
228 'Cache-Control' => 'no-cache',
229 ],
230 ]
231 );
213 232
214 $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); 233 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
215 $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0', [ 234 $this->assertEquals('this is my title', $entry->getTitle());
216 'html' => str_repeat('this is my content', 325), 235 $this->assertContains('this is my content', $entry->getContent());
217 'title' => 'this is my title', 236 $this->assertEquals('text/html', $entry->getMimetype());
218 'url' => 'http://1.1.1.1', 237 $this->assertEquals('fr', $entry->getLanguage());
219 'content_type' => 'text/html', 238 $this->assertEquals(4.0, $entry->getReadingTime());
220 'language' => 'fr', 239 $this->assertEquals('1.1.1.1', $entry->getDomainName());
221 ]); 240 $this->assertEquals('24/03/2014', $entry->getPublishedAt()->format('d/m/Y'));
241 $this->assertContains('Jeremy', $entry->getPublishedBy());
242 $this->assertContains('Nico', $entry->getPublishedBy());
243 $this->assertContains('Thomas', $entry->getPublishedBy());
244 $this->assertContains('no-cache', $entry->getHeaders());
245 }
246
247 public function testWithForcedContentAndDatetime()
248 {
249 $tagger = $this->getTaggerMock();
250 $tagger->expects($this->once())
251 ->method('tag');
252
253 $proxy = new ContentProxy((new Graby()), $tagger, $this->getLogger(), $this->fetchingErrorMessage);
254 $entry = $proxy->updateEntry(
255 new Entry(new User()),
256 'http://0.0.0.0',
257 [
258 'html' => str_repeat('this is my content', 325),
259 'title' => 'this is my title',
260 'url' => 'http://1.1.1.1',
261 'content_type' => 'text/html',
262 'language' => 'fr',
263 'date' => '2016-09-08T11:55:58+0200',
264 ]
265 );
266
267 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
268 $this->assertEquals('this is my title', $entry->getTitle());
269 $this->assertContains('this is my content', $entry->getContent());
270 $this->assertEquals('text/html', $entry->getMimetype());
271 $this->assertEquals('fr', $entry->getLanguage());
272 $this->assertEquals(4.0, $entry->getReadingTime());
273 $this->assertEquals('1.1.1.1', $entry->getDomainName());
274 $this->assertEquals('08/09/2016', $entry->getPublishedAt()->format('d/m/Y'));
275 }
276
277 public function testWithForcedContentAndBadDate()
278 {
279 $tagger = $this->getTaggerMock();
280 $tagger->expects($this->once())
281 ->method('tag');
282
283 $logger = new Logger('foo');
284 $handler = new TestHandler();
285 $logger->pushHandler($handler);
286
287 $proxy = new ContentProxy((new Graby()), $tagger, $logger, $this->fetchingErrorMessage);
288 $entry = $proxy->updateEntry(
289 new Entry(new User()),
290 'http://0.0.0.0',
291 [
292 'html' => str_repeat('this is my content', 325),
293 'title' => 'this is my title',
294 'url' => 'http://1.1.1.1',
295 'content_type' => 'text/html',
296 'language' => 'fr',
297 'date' => '01 02 2012',
298 ]
299 );
222 300
223 $this->assertEquals('http://1.1.1.1', $entry->getUrl()); 301 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
224 $this->assertEquals('this is my title', $entry->getTitle()); 302 $this->assertEquals('this is my title', $entry->getTitle());
@@ -227,6 +305,12 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase
227 $this->assertEquals('fr', $entry->getLanguage()); 305 $this->assertEquals('fr', $entry->getLanguage());
228 $this->assertEquals(4.0, $entry->getReadingTime()); 306 $this->assertEquals(4.0, $entry->getReadingTime());
229 $this->assertEquals('1.1.1.1', $entry->getDomainName()); 307 $this->assertEquals('1.1.1.1', $entry->getDomainName());
308 $this->assertNull($entry->getPublishedAt());
309
310 $records = $handler->getRecords();
311
312 $this->assertCount(1, $records);
313 $this->assertContains('Error while defining date', $records[0]['message']);
230 } 314 }
231 315
232 public function testTaggerThrowException() 316 public function testTaggerThrowException()
@@ -253,6 +337,58 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase
253 $this->assertCount(0, $entry->getTags()); 337 $this->assertCount(0, $entry->getTags());
254 } 338 }
255 339
340 public function dataForCrazyHtml()
341 {
342 return [
343 'script and comment' => [
344 '<strong>Script inside:</strong> <!--[if gte IE 4]><script>alert(\'lol\');</script><![endif]--><br />',
345 'lol',
346 ],
347 'script' => [
348 '<strong>Script inside:</strong><script>alert(\'lol\');</script>',
349 'script',
350 ],
351 ];
352 }
353
354 /**
355 * @dataProvider dataForCrazyHtml
356 */
357 public function testWithCrazyHtmlContent($html, $escapedString)
358 {
359 $tagger = $this->getTaggerMock();
360 $tagger->expects($this->once())
361 ->method('tag');
362
363 $proxy = new ContentProxy((new Graby()), $tagger, $this->getLogger(), $this->fetchingErrorMessage);
364 $entry = $proxy->updateEntry(
365 new Entry(new User()),
366 'http://1.1.1.1',
367 [
368 'html' => $html,
369 'title' => 'this is my title',
370 'url' => 'http://1.1.1.1',
371 'content_type' => 'text/html',
372 'language' => 'fr',
373 'status' => '200',
374 'open_graph' => [
375 'og_title' => 'my OG title',
376 'og_description' => 'OG desc',
377 'og_image' => 'http://3.3.3.3/cover.jpg',
378 ],
379 ]
380 );
381
382 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
383 $this->assertEquals('this is my title', $entry->getTitle());
384 $this->assertNotContains($escapedString, $entry->getContent());
385 $this->assertEquals('http://3.3.3.3/cover.jpg', $entry->getPreviewPicture());
386 $this->assertEquals('text/html', $entry->getMimetype());
387 $this->assertEquals('fr', $entry->getLanguage());
388 $this->assertEquals('200', $entry->getHttpStatus());
389 $this->assertEquals('1.1.1.1', $entry->getDomainName());
390 }
391
256 private function getTaggerMock() 392 private function getTaggerMock()
257 { 393 {
258 return $this->getMockBuilder(RuleBasedTagger::class) 394 return $this->getMockBuilder(RuleBasedTagger::class)