X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=tests%2FWallabag%2FCoreBundle%2FHelper%2FContentProxyTest.php;h=11f1d410fb8a39a207697cb06488e74965e12c18;hb=0d6cfb884c8ef75e4dc5fd667fb9d29702523a2a;hp=5956b502fb68cf4fa6372e3382cb7808cd2f86ef;hpb=f17b89fadce0a8c2280fbe1518d66f20dddce59d;p=github%2Fwallabag%2Fwallabag.git diff --git a/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php b/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php index 5956b502..11f1d410 100644 --- a/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php +++ b/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php @@ -7,10 +7,12 @@ 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.'; + private $fetchingErrorMessage = 'wallabag can\'t retrieve contents for this article. Please troubleshoot this issue.'; public function testWithBadUrl() { @@ -33,7 +35,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase 'language' => '', ]); - $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage); + $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()); @@ -67,7 +69,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase 'language' => '', ]); - $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage); + $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()); @@ -106,12 +108,12 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase ], ]); - $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage); + $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($this->fetchingErrorMessage . '

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()); @@ -147,7 +149,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase ], ]); - $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage); + $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()); @@ -188,7 +190,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase ], ]); - $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage); + $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()); @@ -208,16 +210,18 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase $tagger->expects($this->once()) ->method('tag'); - $graby = $this->getMockBuilder('Graby\Graby')->getMock(); - - $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $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', - ]); + $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()); @@ -239,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(), $this->fetchingErrorMessage); + $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), @@ -253,134 +256,66 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase $this->assertCount(0, $entry->getTags()); } - public function testAssignTagsWithArrayAndExtraSpaces() - { - $graby = $this->getMockBuilder('Graby\Graby') - ->disableOriginalConstructor() - ->getMock(); - - $tagRepo = $this->getTagRepositoryMock(); - $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage); - - $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 testAssignTagsWithString() - { - $graby = $this->getMockBuilder('Graby\Graby') - ->disableOriginalConstructor() - ->getMock(); - - $tagRepo = $this->getTagRepositoryMock(); - $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage); - - $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() + public function dataForCrazyHtml() { - $graby = $this->getMockBuilder('Graby\Graby') - ->disableOriginalConstructor() - ->getMock(); - - $tagRepo = $this->getTagRepositoryMock(); - $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage); - - $entry = new Entry(new User()); - - $proxy->assignTagsToEntry($entry, []); - - $this->assertCount(0, $entry->getTags()); + return [ + 'script and comment' => [ + 'Script inside:
', + 'lol' + ], + 'script' => [ + 'Script inside:', + 'script' + ], + ]; } - public function testAssignTagsWithEmptyString() + /** + * @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(), $this->fetchingErrorMessage); - - $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(), $this->fetchingErrorMessage); - - $tagEntity = new Tag(); - $tagEntity->setLabel('tag1'); - - $entry = new Entry(new User()); - $entry->addTag($tagEntity); - - $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 testAssignTagsNotFlushed() - { - $graby = $this->getMockBuilder('Graby\Graby') - ->disableOriginalConstructor() - ->getMock(); - - $tagRepo = $this->getTagRepositoryMock(); - $tagRepo->expects($this->never()) - ->method('__call'); - - $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage); - - $tagEntity = new Tag(); - $tagEntity->setLabel('tag1'); - - $entry = new Entry(new User()); + $tagger = $this->getTaggerMock(); + $tagger->expects($this->once()) + ->method('tag'); - $proxy->assignTagsToEntry($entry, 'tag1', [$tagEntity]); + $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(1, $entry->getTags()); - $this->assertEquals('tag1', $entry->getTags()[0]->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();