X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=tests%2FWallabag%2FCoreBundle%2FHelper%2FContentProxyTest.php;h=11f1d410fb8a39a207697cb06488e74965e12c18;hb=9e349f08a651c43c6d5dd890303ed529c38c4fde;hp=7abb0737894bf6f4116dcaed468b2cc526302c44;hpb=23634d5d842dabcf5d7475e2becb7e127824239e;p=github%2Fwallabag%2Fwallabag.git diff --git a/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php b/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php index 7abb0737..11f1d410 100644 --- a/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php +++ b/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php @@ -7,9 +7,13 @@ use Wallabag\CoreBundle\Helper\ContentProxy; use Wallabag\CoreBundle\Entity\Entry; use Wallabag\CoreBundle\Entity\Tag; use Wallabag\UserBundle\Entity\User; +use Wallabag\CoreBundle\Helper\RuleBasedTagger; +use Graby\Graby; class ContentProxyTest extends \PHPUnit_Framework_TestCase { + private $fetchingErrorMessage = 'wallabag can\'t retrieve contents for this article. Please troubleshoot this issue.'; + public function testWithBadUrl() { $tagger = $this->getTaggerMock(); @@ -31,12 +35,12 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase 'language' => '', ]); - $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger()); + $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); $entry = $proxy->updateEntry(new Entry(new User()), 'http://user@:80'); $this->assertEquals('http://user@:80', $entry->getUrl()); $this->assertEmpty($entry->getTitle()); - $this->assertEquals('

Unable to retrieve readable content.

', $entry->getContent()); + $this->assertEquals($this->fetchingErrorMessage, $entry->getContent()); $this->assertEmpty($entry->getPreviewPicture()); $this->assertEmpty($entry->getMimetype()); $this->assertEmpty($entry->getLanguage()); @@ -65,12 +69,12 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase 'language' => '', ]); - $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger()); + $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0'); $this->assertEquals('http://0.0.0.0', $entry->getUrl()); $this->assertEmpty($entry->getTitle()); - $this->assertEquals('

Unable to retrieve readable content.

', $entry->getContent()); + $this->assertEquals($this->fetchingErrorMessage, $entry->getContent()); $this->assertEmpty($entry->getPreviewPicture()); $this->assertEmpty($entry->getMimetype()); $this->assertEmpty($entry->getLanguage()); @@ -97,20 +101,22 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase 'url' => '', 'content_type' => '', 'language' => '', + 'status' => '', 'open_graph' => [ 'og_title' => 'my title', 'og_description' => 'desc', ], ]); - $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger()); + $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); $entry = $proxy->updateEntry(new Entry(new User()), 'http://domain.io'); $this->assertEquals('http://domain.io', $entry->getUrl()); $this->assertEquals('my title', $entry->getTitle()); - $this->assertEquals('

Unable to retrieve readable content.

But we found a short description:

desc', $entry->getContent()); + $this->assertEquals($this->fetchingErrorMessage.'

But we found a short description:

desc', $entry->getContent()); $this->assertEmpty($entry->getPreviewPicture()); $this->assertEmpty($entry->getLanguage()); + $this->assertEmpty($entry->getHttpStatus()); $this->assertEmpty($entry->getMimetype()); $this->assertEquals(0.0, $entry->getReadingTime()); $this->assertEquals('domain.io', $entry->getDomainName()); @@ -135,6 +141,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase 'url' => 'http://1.1.1.1', 'content_type' => 'text/html', 'language' => 'fr', + 'status' => '200', 'open_graph' => [ 'og_title' => 'my OG title', 'og_description' => 'OG desc', @@ -142,7 +149,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase ], ]); - $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger()); + $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0'); $this->assertEquals('http://1.1.1.1', $entry->getUrl()); @@ -151,26 +158,70 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase $this->assertEquals('http://3.3.3.3/cover.jpg', $entry->getPreviewPicture()); $this->assertEquals('text/html', $entry->getMimetype()); $this->assertEquals('fr', $entry->getLanguage()); + $this->assertEquals('200', $entry->getHttpStatus()); $this->assertEquals(4.0, $entry->getReadingTime()); $this->assertEquals('1.1.1.1', $entry->getDomainName()); } - public function testWithForcedContent() + public function testWithContentAndNoOgImage() { $tagger = $this->getTaggerMock(); $tagger->expects($this->once()) ->method('tag'); - $graby = $this->getMockBuilder('Graby\Graby')->getMock(); + $graby = $this->getMockBuilder('Graby\Graby') + ->setMethods(['fetchContent']) + ->disableOriginalConstructor() + ->getMock(); - $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger()); - $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0', [ - 'html' => str_repeat('this is my content', 325), - 'title' => 'this is my title', - 'url' => 'http://1.1.1.1', - 'content_type' => 'text/html', - 'language' => 'fr', - ]); + $graby->expects($this->any()) + ->method('fetchContent') + ->willReturn([ + 'html' => str_repeat('this is my content', 325), + 'title' => 'this is my title', + 'url' => 'http://1.1.1.1', + 'content_type' => 'text/html', + 'language' => 'fr', + 'status' => '200', + 'open_graph' => [ + 'og_title' => 'my OG title', + 'og_description' => 'OG desc', + 'og_image' => false, + ], + ]); + + $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); + $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0'); + + $this->assertEquals('http://1.1.1.1', $entry->getUrl()); + $this->assertEquals('this is my title', $entry->getTitle()); + $this->assertContains('this is my content', $entry->getContent()); + $this->assertNull($entry->getPreviewPicture()); + $this->assertEquals('text/html', $entry->getMimetype()); + $this->assertEquals('fr', $entry->getLanguage()); + $this->assertEquals('200', $entry->getHttpStatus()); + $this->assertEquals(4.0, $entry->getReadingTime()); + $this->assertEquals('1.1.1.1', $entry->getDomainName()); + } + + public function testWithForcedContent() + { + $tagger = $this->getTaggerMock(); + $tagger->expects($this->once()) + ->method('tag'); + + $proxy = new ContentProxy((new Graby()), $tagger, $this->getLogger(), $this->fetchingErrorMessage); + $entry = $proxy->updateEntry( + new Entry(new User()), + 'http://0.0.0.0', + [ + 'html' => str_repeat('this is my content', 325), + 'title' => 'this is my title', + 'url' => 'http://1.1.1.1', + 'content_type' => 'text/html', + 'language' => 'fr', + ] + ); $this->assertEquals('http://1.1.1.1', $entry->getUrl()); $this->assertEquals('this is my title', $entry->getTitle()); @@ -192,8 +243,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase ->method('tag') ->will($this->throwException(new \Exception())); - $tagRepo = $this->getTagRepositoryMock(); - $proxy = new ContentProxy($graby, $tagger, $tagRepo, $this->getLogger()); + $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0', [ 'html' => str_repeat('this is my content', 325), @@ -206,111 +256,66 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase $this->assertCount(0, $entry->getTags()); } - public function testAssignTagsWithArrayAndExtraSpaces() + public function dataForCrazyHtml() { - $graby = $this->getMockBuilder('Graby\Graby') - ->disableOriginalConstructor() - ->getMock(); - - $tagRepo = $this->getTagRepositoryMock(); - $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger()); - - $entry = new Entry(new User()); - - $proxy->assignTagsToEntry($entry, [' tag1', 'tag2 ']); - - $this->assertCount(2, $entry->getTags()); - $this->assertEquals('tag1', $entry->getTags()[0]->getLabel()); - $this->assertEquals('tag2', $entry->getTags()[1]->getLabel()); + return [ + 'script and comment' => [ + 'Script inside:
', + 'lol' + ], + 'script' => [ + 'Script inside:', + 'script' + ], + ]; } - public function testAssignTagsWithString() + /** + * @dataProvider dataForCrazyHtml + */ + public function testWithCrazyHtmlContent($html, $escapedString) { - $graby = $this->getMockBuilder('Graby\Graby') - ->disableOriginalConstructor() - ->getMock(); - - $tagRepo = $this->getTagRepositoryMock(); - $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger()); - - $entry = new Entry(new User()); - - $proxy->assignTagsToEntry($entry, 'tag1, tag2'); - - $this->assertCount(2, $entry->getTags()); - $this->assertEquals('tag1', $entry->getTags()[0]->getLabel()); - $this->assertEquals('tag2', $entry->getTags()[1]->getLabel()); - } - - public function testAssignTagsWithEmptyArray() - { - $graby = $this->getMockBuilder('Graby\Graby') - ->disableOriginalConstructor() - ->getMock(); - - $tagRepo = $this->getTagRepositoryMock(); - $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger()); - - $entry = new Entry(new User()); - - $proxy->assignTagsToEntry($entry, []); - - $this->assertCount(0, $entry->getTags()); - } - - public function testAssignTagsWithEmptyString() - { - $graby = $this->getMockBuilder('Graby\Graby') - ->disableOriginalConstructor() - ->getMock(); - - $tagRepo = $this->getTagRepositoryMock(); - $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger()); - - $entry = new Entry(new User()); - - $proxy->assignTagsToEntry($entry, ''); - - $this->assertCount(0, $entry->getTags()); - } - - public function testAssignTagsAlreadyAssigned() - { - $graby = $this->getMockBuilder('Graby\Graby') - ->disableOriginalConstructor() - ->getMock(); - - $tagRepo = $this->getTagRepositoryMock(); - $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger()); - - $tagEntity = new Tag(); - $tagEntity->setLabel('tag1'); - - $entry = new Entry(new User()); - $entry->addTag($tagEntity); + $tagger = $this->getTaggerMock(); + $tagger->expects($this->once()) + ->method('tag'); - $proxy->assignTagsToEntry($entry, 'tag1, tag2'); + $proxy = new ContentProxy((new Graby()), $tagger, $this->getLogger(), $this->fetchingErrorMessage); + $entry = $proxy->updateEntry( + new Entry(new User()), + 'http://1.1.1.1', + [ + 'html' => $html, + 'title' => 'this is my title', + 'url' => 'http://1.1.1.1', + 'content_type' => 'text/html', + 'language' => 'fr', + 'status' => '200', + 'open_graph' => [ + 'og_title' => 'my OG title', + 'og_description' => 'OG desc', + 'og_image' => 'http://3.3.3.3/cover.jpg', + ], + ] + ); - $this->assertCount(2, $entry->getTags()); - $this->assertEquals('tag1', $entry->getTags()[0]->getLabel()); - $this->assertEquals('tag2', $entry->getTags()[1]->getLabel()); + $this->assertEquals('http://1.1.1.1', $entry->getUrl()); + $this->assertEquals('this is my title', $entry->getTitle()); + $this->assertNotContains($escapedString, $entry->getContent()); + $this->assertEquals('http://3.3.3.3/cover.jpg', $entry->getPreviewPicture()); + $this->assertEquals('text/html', $entry->getMimetype()); + $this->assertEquals('fr', $entry->getLanguage()); + $this->assertEquals('200', $entry->getHttpStatus()); + $this->assertEquals('1.1.1.1', $entry->getDomainName()); } private function getTaggerMock() { - return $this->getMockBuilder('Wallabag\CoreBundle\Helper\RuleBasedTagger') + return $this->getMockBuilder(RuleBasedTagger::class) ->setMethods(['tag']) ->disableOriginalConstructor() ->getMock(); } - private function getTagRepositoryMock() - { - return $this->getMockBuilder('Wallabag\CoreBundle\Repository\TagRepository') - ->disableOriginalConstructor() - ->getMock(); - } - private function getLogger() { return new NullLogger();