From d6066c382083dedf426abbfd5f2f1df725c68aaf Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Fri, 27 Sep 2013 20:03:00 +0200 Subject: use seperate direcotires for article enhancers and fetchers --- tests/unit/articleenhancer/EnhancerTest.php | 91 +++++ .../articleenhancer/RegexArticleEnhancerTest.php | 49 +++ .../articleenhancer/XPathArticleEnhancerTest.php | 288 +++++++++++++++ tests/unit/businesslayer/FeedBusinessLayerTest.php | 8 +- tests/unit/controller/TwitterFetcherTest.php | 77 ---- tests/unit/fetcher/FeedFetcherTest.php | 392 +++++++++++++++++++++ tests/unit/fetcher/FetcherTest.php | 108 ++++++ tests/unit/utility/FeedFetcherTest.php | 392 --------------------- tests/unit/utility/FetcherTest.php | 108 ------ .../unit/utility/articleenhancer/EnhancerTest.php | 91 ----- .../articleenhancer/RegexArticleEnhancerTest.php | 49 --- .../articleenhancer/XPathArticleEnhancerTest.php | 288 --------------- 12 files changed, 932 insertions(+), 1009 deletions(-) create mode 100644 tests/unit/articleenhancer/EnhancerTest.php create mode 100644 tests/unit/articleenhancer/RegexArticleEnhancerTest.php create mode 100644 tests/unit/articleenhancer/XPathArticleEnhancerTest.php delete mode 100644 tests/unit/controller/TwitterFetcherTest.php create mode 100644 tests/unit/fetcher/FeedFetcherTest.php create mode 100644 tests/unit/fetcher/FetcherTest.php delete mode 100644 tests/unit/utility/FeedFetcherTest.php delete mode 100644 tests/unit/utility/FetcherTest.php delete mode 100644 tests/unit/utility/articleenhancer/EnhancerTest.php delete mode 100644 tests/unit/utility/articleenhancer/RegexArticleEnhancerTest.php delete mode 100644 tests/unit/utility/articleenhancer/XPathArticleEnhancerTest.php (limited to 'tests') diff --git a/tests/unit/articleenhancer/EnhancerTest.php b/tests/unit/articleenhancer/EnhancerTest.php new file mode 100644 index 000000000..84cbe2a7c --- /dev/null +++ b/tests/unit/articleenhancer/EnhancerTest.php @@ -0,0 +1,91 @@ +. +* +*/ + +namespace OCA\News\ArticleEnhancer; + +use \OCA\News\Db\Item; + +require_once(__DIR__ . "/../../classloader.php"); + + +class EnhancerTest extends \OCA\AppFramework\Utility\TestUtility { + + private $enhancer; + private $articleEnhancer; + private $articleEnhancer2; + + protected function setUp(){ + $this->enhancer = new Enhancer(); + $this->articleEnhancer = $this->getMockBuilder( + '\OCA\News\ArticleEnhancer\ArticleEnhancer') + ->disableOriginalConstructor() + ->getMock(); + $this->enhancer->registerEnhancer('test.com', $this->articleEnhancer); + } + + + public function testEnhanceSetsCorrectHash(){ + $item = new Item(); + $item->setUrl('hi'); + $urls = array( + 'https://test.com', + 'https://www.test.com', + 'https://test.com/', + 'http://test.com', + 'http://test.com/', + 'http://www.test.com' + ); + for ($i=0; $i < count($urls); $i++) { + $url = $urls[$i]; + $this->articleEnhancer->expects($this->at($i)) + ->method('enhance') + ->with($this->equalTo($item)) + ->will($this->returnValue($item)); + } + + for ($i=0; $i < count($urls); $i++) { + $url = $urls[$i]; + $result = $this->enhancer->enhance($item, $url); + $this->assertEquals($item, $result); + } + + } + + + public function testNotMatchShouldJustReturnItem() { + $item = new Item(); + $item->setUrl('hi'); + + $url = 'https://tests.com'; + $this->articleEnhancer->expects($this->never()) + ->method('enhance'); + + $result = $this->enhancer->enhance($item, $url); + $this->assertEquals($item, $result); + + } + + +} \ No newline at end of file diff --git a/tests/unit/articleenhancer/RegexArticleEnhancerTest.php b/tests/unit/articleenhancer/RegexArticleEnhancerTest.php new file mode 100644 index 000000000..2d985edf1 --- /dev/null +++ b/tests/unit/articleenhancer/RegexArticleEnhancerTest.php @@ -0,0 +1,49 @@ +. +* +*/ + +namespace OCA\News\ArticleEnhancer; + +use \OCA\News\Db\Item; + +require_once(__DIR__ . "/../../classloader.php"); + + +class RegexArticleEnhancerTest extends \OCA\AppFramework\Utility\TestUtility { + + + public function testRegexEnhancer() { + $item = new Item(); + $item->setBody('atests is a nice thing'); + $item->setUrl('http://john.com'); + $regex = array("%tes(ts)%" => "heho$1tests"); + + $regexEnhancer = new RegexArticleEnhancer('%john.com%', $regex); + $item = $regexEnhancer->enhance($item); + + $this->assertEquals('ahehotstests is a nice thing', $item->getBody()); + } + + +} \ No newline at end of file diff --git a/tests/unit/articleenhancer/XPathArticleEnhancerTest.php b/tests/unit/articleenhancer/XPathArticleEnhancerTest.php new file mode 100644 index 000000000..a0f8db388 --- /dev/null +++ b/tests/unit/articleenhancer/XPathArticleEnhancerTest.php @@ -0,0 +1,288 @@ +. +* +*/ + +namespace OCA\News\ArticleEnhancer; + +use \OCA\News\Db\Item; + +require_once(__DIR__ . "/../../classloader.php"); + + +class XPathArticleEnhancerTest extends \OCA\AppFramework\Utility\TestUtility { + + private $purifier; + private $testEnhancer; + private $fileFactory; + private $timeout; + + protected function setUp() { + $timeout = 30; + $this->fileFactory = $this->getMockBuilder('\OCA\News\Utility\SimplePieFileFactory') + ->disableOriginalConstructor() + ->getMock(); + $this->purifier = $this->getMock('purifier', array('purify')); + + $this->testEnhancer = new XPathArticleEnhancer( + $this->purifier, + $this->fileFactory, + array( + '/explosm.net\/comics/' => '//*[@id=\'maincontent\']/div[2]/div/span', + '/explosm.net\/shorts/' => '//*[@id=\'maincontent\']/div/div', + '/explosm.net\/all/' => '//body/*', + '/themerepublic.net/' => '//*[@class=\'post hentry\']' + ), + $this->timeout + ); + } + + + public function testDoesNotModifiyNotMatchingResults() { + $item = new Item(); + $item->setUrl('http://explosm.net'); + $this->assertEquals($item, $this->testEnhancer->enhance($item)); + } + + + public function testDoesModifiyArticlesThatMatch() { + $file = new \stdClass; + $file->headers = array("content-type"=>"text/html; charset=utf-8"); + $file->body = ' + +
+
nooo
+
hiho
+
+ + '; + $item = new Item(); + $item->setUrl('https://www.explosm.net/comics/312'); + $item->setBody('Hello thar'); + + $this->fileFactory->expects($this->once()) + ->method('getFile') + ->with($this->equalTo($item->getUrl()), + $this->equalTo($this->timeout)) + ->will($this->returnValue($file)); + $this->purifier->expects($this->once()) + ->method('purify') + ->with($this->equalTo('hiho')) + ->will($this->returnValue('hiho')); + + $result = $this->testEnhancer->enhance($item); + $this->assertEquals('hiho', $result->getBody()); + } + + + public function testDoesModifiyAllArticlesThatMatch() { + $file = new \stdClass; + $file->headers = array("content-type"=>"text/html; charset=utf-8"); + $file->body = ' + +
+
nooo
hiho
+
rawr
+
+ + '; + $item = new Item(); + $item->setUrl('https://www.explosm.net/shorts/312'); + $item->setBody('Hello thar'); + + $this->fileFactory->expects($this->once()) + ->method('getFile') + ->with($this->equalTo($item->getUrl()), + $this->equalTo($this->timeout)) + ->will($this->returnValue($file)); + $this->purifier->expects($this->once()) + ->method('purify') + ->with($this->equalTo('
hiho
rawr
')) + ->will($this->returnValue('
hiho
rawr
')); + + $result = $this->testEnhancer->enhance($item); + $this->assertEquals('
hiho
rawr
', $result->getBody()); + } + + + public function testModificationHandlesEmptyResults() { + $file = new \stdClass; + $file->headers = array("content-type"=>"text/html; charset=utf-8"); + $file->body = ' + +
+
+ + '; + $item = new Item(); + $item->setUrl('https://www.explosm.net/comics/312'); + $item->setBody('Hello thar'); + + $this->fileFactory->expects($this->once()) + ->method('getFile') + ->with($this->equalTo($item->getUrl()), + $this->equalTo($this->timeout)) + ->will($this->returnValue($file)); + $this->purifier->expects($this->once()) + ->method('purify') + ->with($this->equalTo(null)) + ->will($this->returnValue(null)); + + $result = $this->testEnhancer->enhance($item); + $this->assertEquals(null, $result->getBody()); + } + + + public function testModificationDoesNotBreakOnEmptyDom() { + $file = new \stdClass; + $file->headers = array("content-type"=>"text/html; charset=utf-8"); + $file->body = ''; + $item = new Item(); + $item->setUrl('https://www.explosm.net/comics/312'); + $item->setBody('Hello thar'); + + $this->fileFactory->expects($this->once()) + ->method('getFile') + ->with($this->equalTo($item->getUrl()), + $this->equalTo($this->timeout)) + ->will($this->returnValue($file)); + $this->purifier->expects($this->once()) + ->method('purify') + ->with($this->equalTo(null)) + ->will($this->returnValue(null)); + + $result = $this->testEnhancer->enhance($item); + $this->assertEquals(null, $result->getBody()); + } + + + public function testModificationDoesNotBreakOnBrokenDom() { + $file = new \stdClass; + $file->headers = array("content-type"=>"text/html; charset=utf-8"); + $file->body = '

+ +

+
+ + '; + $item = new Item(); + $item->setUrl('https://www.explosm.net/comics/312'); + $item->setBody('Hello thar'); + + $this->fileFactory->expects($this->once()) + ->method('getFile') + ->with($this->equalTo($item->getUrl()), + $this->equalTo($this->timeout)) + ->will($this->returnValue($file)); + $this->purifier->expects($this->once()) + ->method('purify') + ->with($this->equalTo(null)) + ->will($this->returnValue(null)); + + $result = $this->testEnhancer->enhance($item); + $this->assertEquals(null, $result->getBody()); + } + + + public function testTransformRelativeUrls() { + $file = new \stdClass; + $file->headers = array("content-type"=>"text/html; charset=utf-8"); + $file->body = ' + + link + link2 + + + '; + $item = new Item(); + $item->setUrl('https://www.explosm.net/all/312'); + $item->setBody('Hello thar'); + + $this->fileFactory->expects($this->once()) + ->method('getFile') + ->with($this->equalTo($item->getUrl()), + $this->equalTo($this->timeout)) + ->will($this->returnValue($file)); + $this->purifier->expects($this->once()) + ->method('purify') + ->with($this->equalTo('linklink2')) + ->will($this->returnValue('linklink2')); + + $result = $this->testEnhancer->enhance($item); + $this->assertEquals('linklink2', $result->getBody()); + } + + public function testTransformRelativeUrlSpecials() { + $file = new \stdClass; + $file->headers = array("content-type"=>"text/html; charset=utf-8"); + $file->body = ' + + + + '; + $item = new Item(); + $item->setUrl('https://username:secret@www.explosm.net/all/312'); + $item->setBody('Hello thar'); + + $this->fileFactory->expects($this->once()) + ->method('getFile') + ->with($this->equalTo($item->getUrl()), + $this->equalTo($this->timeout)) + ->will($this->returnValue($file)); + $this->purifier->expects($this->once()) + ->method('purify') + ->with($this->equalTo('')) + ->will($this->returnValue('')); + + $result = $this->testEnhancer->enhance($item); + $this->assertEquals('', $result->getBody()); + } + + public function testDontTransformAbsoluteUrlsAndMails() { + $file = new \stdClass; + $file->headers = array("content-type"=>"text/html; charset=utf-8"); + $file->body = ' + + + mail + + '; + $item = new Item(); + $item->setUrl('https://www.explosm.net/all/312'); + $item->setBody('Hello thar'); + + $this->fileFactory->expects($this->once()) + ->method('getFile') + ->with($this->equalTo($item->getUrl()), + $this->equalTo($this->timeout)) + ->will($this->returnValue($file)); + $this->purifier->expects($this->once()) + ->method('purify') + ->with($this->equalTo('mail')) + ->will($this->returnValue('mail')); + + $result = $this->testEnhancer->enhance($item); + $this->assertEquals('mail', $result->getBody()); + } + +} \ No newline at end of file diff --git a/tests/unit/businesslayer/FeedBusinessLayerTest.php b/tests/unit/businesslayer/FeedBusinessLayerTest.php index 550f37dcb..5e379525d 100644 --- a/tests/unit/businesslayer/FeedBusinessLayerTest.php +++ b/tests/unit/businesslayer/FeedBusinessLayerTest.php @@ -32,8 +32,8 @@ use \OCA\AppFramework\Db\DoesNotExistException; use \OCA\News\Db\Feed; use \OCA\News\Db\Item; -use \OCA\News\Utility\Fetcher; -use \OCA\News\Utility\FetcherException; +use \OCA\News\Fetcher\Fetcher; +use \OCA\News\Fetcher\FetcherException; class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { @@ -63,13 +63,13 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { $this->feedMapper = $this->getMockBuilder('\OCA\News\Db\FeedMapper') ->disableOriginalConstructor() ->getMock(); - $this->fetcher = $this->getMockBuilder('\OCA\News\Utility\Fetcher') + $this->fetcher = $this->getMockBuilder('\OCA\News\Fetcher\Fetcher') ->disableOriginalConstructor() ->getMock(); $this->itemMapper = $this->getMockBuilder('\OCA\News\Db\ItemMapper') ->disableOriginalConstructor() ->getMock(); - $this->enhancer = $this->getMockBuilder('\OCA\News\Utility\ArticleEnhancer\Enhancer') + $this->enhancer = $this->getMockBuilder('\OCA\News\ArticleEnhancer\Enhancer') ->disableOriginalConstructor() ->getMock(); $this->feedBusinessLayer = new FeedBusinessLayer($this->feedMapper, diff --git a/tests/unit/controller/TwitterFetcherTest.php b/tests/unit/controller/TwitterFetcherTest.php deleted file mode 100644 index 9bda485bd..000000000 --- a/tests/unit/controller/TwitterFetcherTest.php +++ /dev/null @@ -1,77 +0,0 @@ -. -* -*/ - -namespace OCA\News\Utility; - -require_once(__DIR__ . "/../../classloader.php"); - - -class TwitterFetcherTest extends \OCA\AppFramework\Utility\TestUtility { - - private $fetcher; - private $twitter; - - protected function setUp(){ - $this->fetcher = $this->getMockBuilder('\OCA\News\Utility\FeedFetcher') - ->disableOriginalConstructor() - ->getMock(); - $this->twitter = new TwitterFetcher($this->fetcher); - } - - - public function testCanHandle(){ - $urls = array( - 'https://twitter.com/GeorgeTakei', - 'https://www.twitter.com/GeorgeTakei', - 'http://twitter.com/GeorgeTakei', - 'http://www.twitter.com/GeorgeTakei', - 'www.twitter.com/GeorgeTakei', - 'twitter.com/GeorgeTakei' - ); - foreach($urls as $url){ - $this->assertTrue($this->twitter->canHandle($url), $url); - } - } - - - public function testCanHandleDoesNotUseApiUrls(){ - $url = 'https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=GeorgeTakei'; - $this->assertFalse($this->twitter->canHandle($url)); - } - - - public function testFetch(){ - $inUrl = 'https://www.twitter.com/GeorgeTakei'; - $outUrl = 'https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=GeorgeTakei'; - $out = 'hi'; - $this->fetcher->expects($this->once()) - ->method('fetch') - ->with($this->equalTo($outUrl)) - ->will($this->returnValue($out)); - - $return = $this->twitter->fetch($inUrl); - $this->assertEquals($out, $return); - } -} \ No newline at end of file diff --git a/tests/unit/fetcher/FeedFetcherTest.php b/tests/unit/fetcher/FeedFetcherTest.php new file mode 100644 index 000000000..466bcc446 --- /dev/null +++ b/tests/unit/fetcher/FeedFetcherTest.php @@ -0,0 +1,392 @@ +. +* +*/ + +namespace OCA\News\Fetcher; + +use \OCA\News\Db\Item; +use \OCA\News\Db\Feed; + +require_once(__DIR__ . "/../../classloader.php"); + + +class FeedFetcherTest extends \OCA\AppFramework\Utility\TestUtility { + + private $fetcher; + private $core; + private $coreFactory; + private $faviconFetcher; + private $url; + private $cacheDirectory; + private $cacheDuration; + private $time; + private $item; + private $purifier; + private $fetchTimeout; + + // items + private $permalink; + private $title; + private $guid; + private $pub; + private $body; + private $author; + private $authorMail; + private $enclosureLink; + + // feed + private $feedTitle; + private $feedLink; + private $feedImage; + private $webFavicon; + + protected function setUp(){ + $this->core = $this->getMockBuilder( + '\SimplePie_Core') + ->disableOriginalConstructor() + ->getMock(); + $this->coreFactory = $this->getMockBuilder( + '\OCA\AppFramework\Utility\SimplePieAPIFactory') + ->disableOriginalConstructor() + ->getMock(); + $this->coreFactory->expects($this->any()) + ->method('getCore') + ->will($this->returnValue($this->core)); + $this->item = $this->getMockBuilder( + '\SimplePie_Item') + ->disableOriginalConstructor() + ->getMock(); + $this->faviconFetcher = $this->getMockBuilder( + '\OCA\AppFramework\Utility\FaviconFetcher') + ->disableOriginalConstructor() + ->getMock(); + $this->purifier = $this->getMock('purifier', array('purify')); + $this->time = 2323; + $timeFactory = $this->getMockBuilder( + '\OCA\AppFramework\Utility\TimeFactory') + ->disableOriginalConstructor() + ->getMock(); + $timeFactory->expects($this->any()) + ->method('getTime') + ->will($this->returnValue($this->time)); + $this->cacheDuration = 100; + $this->cacheDirectory = 'dir/'; + $this->fetchTimeout = 40; + $this->fetcher = new FeedFetcher($this->getAPIMock(), + $this->coreFactory, + $this->faviconFetcher, + $timeFactory, + $this->cacheDirectory, + $this->cacheDuration, + $this->fetchTimeout, + $this->purifier); + $this->url = 'http://tests'; + + $this->permalink = 'http://permalink'; + $this->title = 'my&lt;' title'; + $this->guid = 'hey guid here'; + $this->body = 'let the bodies hit the floor test'; + $this->body2 = 'let the bodies hit the floor test'; + $this->pub = 23111; + $this->author = '<boogieman'; + $this->enclosureLink = 'http://enclosure.you'; + + $this->feedTitle = '<e;its a title'; + $this->feedLink = 'http://goatse'; + $this->feedImage = '/an/image'; + $this->webFavicon = 'http://anon.google.com'; + $this->authorMail = 'doe@joes.com'; + } + + + public function testCanHandle(){ + $url = 'google.de'; + + $this->assertTrue($this->fetcher->canHandle($url)); + } + + + public function testFetchThrowsExceptionWhenInitFailed() { + $this->core->expects($this->once()) + ->method('set_feed_url') + ->with($this->equalTo($this->url)); + $this->core->expects($this->once()) + ->method('enable_cache') + ->with($this->equalTo(true)); + $this->core->expects($this->once()) + ->method('set_timeout') + ->with($this->equalTo($this->fetchTimeout)); + $this->core->expects($this->once()) + ->method('set_cache_location') + ->with($this->equalTo($this->cacheDirectory)); + $this->core->expects($this->once()) + ->method('set_cache_duration') + ->with($this->equalTo($this->cacheDuration)); + $this->setExpectedException('\OCA\News\Fetcher\FetcherException'); + $this->fetcher->fetch($this->url); + } + + + public function testShouldCatchExceptionsAndThrowOwnException() { + $this->core->expects($this->once()) + ->method('init') + ->will($this->returnValue(true)); + $this->core->expects($this->once()) + ->method('get_items') + ->will($this->throwException(new \Exception('oh noes!'))); + $this->setExpectedException('\OCA\News\Fetcher\FetcherException'); + $this->fetcher->fetch($this->url); + } + + + private function expectCore($method, $return) { + $this->core->expects($this->once()) + ->method($method) + ->will($this->returnValue($return)); + } + + private function expectItem($method, $return) { + $this->item->expects($this->once()) + ->method($method) + ->will($this->returnValue($return)); + } + + + private function createItem($author=false, $enclosureType=null, $noPubDate=false) { + $this->purifier->expects($this->once()) + ->method('purify') + ->with($this->equalTo($this->body)) + ->will($this->returnValue($this->body)); + $this->expectItem('get_permalink', $this->permalink); + $this->expectItem('get_title', $this->title); + $this->expectItem('get_id', $this->guid); + $this->expectItem('get_content', $this->body); + + $item = new Item(); + + if($noPubDate) { + $this->expectItem('get_date', 0); + $item->setPubDate($this->time); + } else { + $this->expectItem('get_date', $this->pub); + $item->setPubDate($this->pub); + } + + $item->setStatus(0); + $item->setUnread(); + $item->setUrl($this->permalink); + $item->setTitle('my<\' title'); + $item->setGuid($this->guid); + $item->setGuidHash(md5($this->guid)); + $item->setBody($this->body2); + $item->setLastModified($this->time); + if($author) { + $mock = $this->getMock('author', array('get_name')); + $mock->expects($this->once()) + ->method('get_name') + ->will($this->returnValue($this->author)); + $this->expectItem('get_author', $mock); + $item->setAuthor(html_entity_decode($this->author)); + } else { + $mock = $this->getMock('author', array('get_name', 'get_email')); + $mock->expects($this->any()) + ->method('get_name') + ->will($this->returnValue('')); + $mock->expects($this->any()) + ->method('get_email') + ->will($this->returnValue($this->authorMail)); + + $this->expectItem('get_author', $mock); + $item->setAuthor(html_entity_decode($this->authorMail)); + } + + if($enclosureType === 'audio/ogg') { + $mock = $this->getMock('enclosure', array('get_type', 'get_link')); + $mock->expects($this->any()) + ->method('get_type') + ->will($this->returnValue($enclosureType)); + $this->expectItem('get_enclosure', $this->mock); + $item->setEnclosureMime($enclosureType); + $item->setEnclosureLink($this->enclosureLink); + } + return $item; + } + + + private function createFeed($hasFeedFavicon=false, $hasWebFavicon=false) { + $this->expectCore('get_title', $this->feedTitle); + $this->expectCore('get_permalink', $this->feedLink); + + $feed = new Feed(); + $feed->setTitle(html_entity_decode($this->feedTitle)); + $feed->setUrl($this->url); + $feed->setLink($this->feedLink); + $feed->setAdded($this->time); + + if($hasWebFavicon) { + $this->faviconFetcher->expects($this->once()) + ->method('fetch') + ->with($this->equalTo($this->feedLink)) + ->will($this->returnValue($this->webFavicon)); + $feed->setFaviconLink($this->webFavicon); + } + + if($hasFeedFavicon) { + $this->expectCore('get_image_url', $this->feedImage); + $feed->setFaviconLink($this->feedImage); + } elseif(!$hasWebFavicon) { + $feed->setFaviconLink(null); + $this->expectCore('get_image_url', null); + } + + + return $feed; + } + + + public function testFetchMapItems(){ + $this->core->expects($this->once()) + ->method('init') + ->will($this->returnValue(true)); + $item = $this->createItem(); + $feed = $this->createFeed(); + $this->expectCore('get_items', array($this->item)); + $result = $this->fetcher->fetch($this->url); + + $this->assertEquals(array($feed, array($item)), $result); + } + + + public function testFetchMapItemsNoFeedTitleUsesUrl(){ + $this->expectCore('get_title', ''); + $this->expectCore('get_permalink', $this->feedLink); + + $feed = new Feed(); + $feed->setTitle($this->url); + $feed->setUrl($this->url); + $feed->setLink($this->feedLink); + $feed->setAdded($this->time); + $feed->setFaviconLink(null); + + $this->core->expects($this->once()) + ->method('init') + ->will($this->returnValue(true)); + $item = $this->createItem(); + $this->expectCore('get_items', array($this->item)); + $result = $this->fetcher->fetch($this->url); + + $this->assertEquals(array($feed, array($item)), $result); + } + + public function testFetchMapItemsAuthorExists(){ + $this->core->expects($this->once()) + ->method('init') + ->will($this->returnValue(true)); + $item = $this->createItem(true); + $feed = $this->createFeed(true); + $this->expectCore('get_items', array($this->item)); + $result = $this->fetcher->fetch($this->url); + + $this->assertEquals(array($feed, array($item)), $result); + } + + + public function testFetchMapItemsEnclosureExists(){ + $this->core->expects($this->once()) + ->method('init') + ->will($this->returnValue(true)); + $item = $this->createItem(false, true); + $feed = $this->createFeed(false, true); + $this->expectCore('get_items', array($this->item)); + $result = $this->fetcher->fetch($this->url); + + $this->assertEquals(array($feed, array($item)), $result); + } + + + public function testFetchMapItemsNoPubdate(){ + $this->core->expects($this->once()) + ->method('init') + ->will($this->returnValue(true)); + $item = $this->createItem(false, true, true); + $feed = $this->createFeed(false, true); + $this->expectCore('get_items', array($this->item)); + $result = $this->fetcher->fetch($this->url); + + $this->assertEquals(array($feed, array($item)), $result); + } + + + public function testFetchMapItemsGetFavicon() { + $this->expectCore('get_title', $this->feedTitle); + $this->expectCore('get_permalink', $this->feedLink); + + $feed = new Feed(); + $feed->setTitle(html_entity_decode($this->feedTitle)); + $feed->setUrl($this->url); + $feed->setLink($this->feedLink); + $feed->setAdded($this->time); + $feed->setFaviconLink($this->webFavicon); + + $this->core->expects($this->once()) + ->method('init') + ->will($this->returnValue(true)); + + $this->faviconFetcher->expects($this->once()) + ->method('fetch') + ->will($this->returnValue($this->webFavicon)); + + $item = $this->createItem(false, true); + $this->expectCore('get_items', array($this->item)); + $result = $this->fetcher->fetch($this->url /*, true*/); + + $this->assertEquals(array($feed, array($item)), $result); + } + + public function testFetchMapItemsNoGetFavicon() { + $this->expectCore('get_title', $this->feedTitle); + $this->expectCore('get_permalink', $this->feedLink); + + $feed = new Feed(); + $feed->setTitle(html_entity_decode($this->feedTitle)); + $feed->setUrl($this->url); + $feed->setLink($this->feedLink); + $feed->setAdded($this->time); + + $this->core->expects($this->once()) + ->method('init') + ->will($this->returnValue(true)); + + $this->faviconFetcher->expects($this->never()) + ->method('fetch'); + + $item = $this->createItem(false, true); + $this->expectCore('get_items', array($this->item)); + $result = $this->fetcher->fetch($this->url, false); + + $this->assertEquals(array($feed, array($item)), $result); + } + + +} diff --git a/tests/unit/fetcher/FetcherTest.php b/tests/unit/fetcher/FetcherTest.php new file mode 100644 index 000000000..41f33129c --- /dev/null +++ b/tests/unit/fetcher/FetcherTest.php @@ -0,0 +1,108 @@ +. +* +*/ + +namespace OCA\News\Fetcher; + +require_once(__DIR__ . "/../../classloader.php"); + + +class FetcherTest extends \OCA\AppFramework\Utility\TestUtility { + + private $fetcher; + + protected function setUp(){ + $this->fetcher = new Fetcher(); + } + + + public function testFetch(){ + $url = 'hi'; + $mockFetcher = $this->getMockBuilder('\OCA\News\Fetcher\IFeedFetcher') + ->disableOriginalConstructor() + ->getMock(); + $mockFetcher->expects($this->once()) + ->method('canHandle') + ->with($this->equalTo($url)) + ->will($this->returnValue(true)); + $this->fetcher->registerFetcher($mockFetcher); + + $this->fetcher->fetch($url); + } + + + public function testMultipleFetchers(){ + $url = 'hi'; + $mockFetcher = $this->getMockBuilder('\OCA\News\Fetcher\IFeedFetcher') + ->disableOriginalConstructor() + ->getMock(); + $mockFetcher->expects($this->once()) + ->method('canHandle') + ->with($this->equalTo($url)) + ->will($this->returnValue(false)); + $mockFetcher2 = $this->getMockBuilder('\OCA\News\Fetcher\IFeedFetcher') + ->disableOriginalConstructor() + ->getMock(); + $mockFetcher2->expects($this->once()) + ->method('canHandle') + ->with($this->equalTo($url)) + ->will($this->returnValue(true)); + + $this->fetcher->registerFetcher($mockFetcher); + $this->fetcher->registerFetcher($mockFetcher2); + + $this->fetcher->fetch($url); + } + + + public function testMultipleFetchersOnlyOneShouldHandle(){ + $url = 'hi'; + $return = 'zeas'; + $mockFetcher = $this->getMockBuilder('\OCA\News\Fetcher\IFeedFetcher') + ->disableOriginalConstructor() + ->getMock(); + $mockFetcher->expects($this->once()) + ->method('canHandle') + ->with($this->equalTo($url)) + ->will($this->returnValue(true)); + $mockFetcher->expects($this->once()) + ->method('fetch') + ->with($this->equalTo($url)) + ->will($this->returnValue($return)); + $mockFetcher2 = $this->getMockBuilder('\OCA\News\Fetcher\IFeedFetcher') + ->disableOriginalConstructor() + ->getMock(); + $mockFetcher2->expects($this->never()) + ->method('canHandle'); + + $this->fetcher->registerFetcher($mockFetcher); + $this->fetcher->registerFetcher($mockFetcher2); + + $result = $this->fetcher->fetch($url); + + $this->assertEquals($return, $result); + } + + +} \ No newline at end of file diff --git a/tests/unit/utility/FeedFetcherTest.php b/tests/unit/utility/FeedFetcherTest.php deleted file mode 100644 index 3865bda9f..000000000 --- a/tests/unit/utility/FeedFetcherTest.php +++ /dev/null @@ -1,392 +0,0 @@ -. -* -*/ - -namespace OCA\News\Utility; - -use \OCA\News\Db\Item; -use \OCA\News\Db\Feed; - -require_once(__DIR__ . "/../../classloader.php"); - - -class FeedFetcherTest extends \OCA\AppFramework\Utility\TestUtility { - - private $fetcher; - private $core; - private $coreFactory; - private $faviconFetcher; - private $url; - private $cacheDirectory; - private $cacheDuration; - private $time; - private $item; - private $purifier; - private $fetchTimeout; - - // items - private $permalink; - private $title; - private $guid; - private $pub; - private $body; - private $author; - private $authorMail; - private $enclosureLink; - - // feed - private $feedTitle; - private $feedLink; - private $feedImage; - private $webFavicon; - - protected function setUp(){ - $this->core = $this->getMockBuilder( - '\SimplePie_Core') - ->disableOriginalConstructor() - ->getMock(); - $this->coreFactory = $this->getMockBuilder( - '\OCA\AppFramework\Utility\SimplePieAPIFactory') - ->disableOriginalConstructor() - ->getMock(); - $this->coreFactory->expects($this->any()) - ->method('getCore') - ->will($this->returnValue($this->core)); - $this->item = $this->getMockBuilder( - '\SimplePie_Item') - ->disableOriginalConstructor() - ->getMock(); - $this->faviconFetcher = $this->getMockBuilder( - '\OCA\AppFramework\Utility\FaviconFetcher') - ->disableOriginalConstructor() - ->getMock(); - $this->purifier = $this->getMock('purifier', array('purify')); - $this->time = 2323; - $timeFactory = $this->getMockBuilder( - '\OCA\AppFramework\Utility\TimeFactory') - ->disableOriginalConstructor() - ->getMock(); - $timeFactory->expects($this->any()) - ->method('getTime') - ->will($this->returnValue($this->time)); - $this->cacheDuration = 100; - $this->cacheDirectory = 'dir/'; - $this->fetchTimeout = 40; - $this->fetcher = new FeedFetcher($this->getAPIMock(), - $this->coreFactory, - $this->faviconFetcher, - $timeFactory, - $this->cacheDirectory, - $this->cacheDuration, - $this->fetchTimeout, - $this->purifier); - $this->url = 'http://tests'; - - $this->permalink = 'http://permalink'; - $this->title = 'my&lt;' title'; - $this->guid = 'hey guid here'; - $this->body = 'let the bodies hit the floor test'; - $this->body2 = 'let the bodies hit the floor test'; - $this->pub = 23111; - $this->author = '<boogieman'; - $this->enclosureLink = 'http://enclosure.you'; - - $this->feedTitle = '<e;its a title'; - $this->feedLink = 'http://goatse'; - $this->feedImage = '/an/image'; - $this->webFavicon = 'http://anon.google.com'; - $this->authorMail = 'doe@joes.com'; - } - - - public function testCanHandle(){ - $url = 'google.de'; - - $this->assertTrue($this->fetcher->canHandle($url)); - } - - - public function testFetchThrowsExceptionWhenInitFailed() { - $this->core->expects($this->once()) - ->method('set_feed_url') - ->with($this->equalTo($this->url)); - $this->core->expects($this->once()) - ->method('enable_cache') - ->with($this->equalTo(true)); - $this->core->expects($this->once()) - ->method('set_timeout') - ->with($this->equalTo($this->fetchTimeout)); - $this->core->expects($this->once()) - ->method('set_cache_location') - ->with($this->equalTo($this->cacheDirectory)); - $this->core->expects($this->once()) - ->method('set_cache_duration') - ->with($this->equalTo($this->cacheDuration)); - $this->setExpectedException('\OCA\News\Utility\FetcherException'); - $this->fetcher->fetch($this->url); - } - - - public function testShouldCatchExceptionsAndThrowOwnException() { - $this->core->expects($this->once()) - ->method('init') - ->will($this->returnValue(true)); - $this->core->expects($this->once()) - ->method('get_items') - ->will($this->throwException(new \Exception('oh noes!'))); - $this->setExpectedException('\OCA\News\Utility\FetcherException'); - $this->fetcher->fetch($this->url); - } - - - private function expectCore($method, $return) { - $this->core->expects($this->once()) - ->method($method) - ->will($this->returnValue($return)); - } - - private function expectItem($method, $return) { - $this->item->expects($this->once()) - ->method($method) - ->will($this->returnValue($return)); - } - - - private function createItem($author=false, $enclosureType=null, $noPubDate=false) { - $this->purifier->expects($this->once()) - ->method('purify') - ->with($this->equalTo($this->body)) - ->will($this->returnValue($this->body)); - $this->expectItem('get_permalink', $this->permalink); - $this->expectItem('get_title', $this->title); - $this->expectItem('get_id', $this->guid); - $this->expectItem('get_content', $this->body); - - $item = new Item(); - - if($noPubDate) { - $this->expectItem('get_date', 0); - $item->setPubDate($this->time); - } else { - $this->expectItem('get_date', $this->pub); - $item->setPubDate($this->pub); - } - - $item->setStatus(0); - $item->setUnread(); - $item->setUrl($this->permalink); - $item->setTitle('my<\' title'); - $item->setGuid($this->guid); - $item->setGuidHash(md5($this->guid)); - $item->setBody($this->body2); - $item->setLastModified($this->time); - if($author) { - $mock = $this->getMock('author', array('get_name')); - $mock->expects($this->once()) - ->method('get_name') - ->will($this->returnValue($this->author)); - $this->expectItem('get_author', $mock); - $item->setAuthor(html_entity_decode($this->author)); - } else { - $mock = $this->getMock('author', array('get_name', 'get_email')); - $mock->expects($this->any()) - ->method('get_name') - ->will($this->returnValue('')); - $mock->expects($this->any()) - ->method('get_email') - ->will($this->returnValue($this->authorMail)); - - $this->expectItem('get_author', $mock); - $item->setAuthor(html_entity_decode($this->authorMail)); - } - - if($enclosureType === 'audio/ogg') { - $mock = $this->getMock('enclosure', array('get_type', 'get_link')); - $mock->expects($this->any()) - ->method('get_type') - ->will($this->returnValue($enclosureType)); - $this->expectItem('get_enclosure', $this->mock); - $item->setEnclosureMime($enclosureType); - $item->setEnclosureLink($this->enclosureLink); - } - return $item; - } - - - private function createFeed($hasFeedFavicon=false, $hasWebFavicon=false) { - $this->expectCore('get_title', $this->feedTitle); - $this->expectCore('get_permalink', $this->feedLink); - - $feed = new Feed(); - $feed->setTitle(html_entity_decode($this->feedTitle)); - $feed->setUrl($this->url); - $feed->setLink($this->feedLink); - $feed->setAdded($this->time); - - if($hasWebFavicon) { - $this->faviconFetcher->expects($this->once()) - ->method('fetch') - ->with($this->equalTo($this->feedLink)) - ->will($this->returnValue($this->webFavicon)); - $feed->setFaviconLink($this->webFavicon); - } - - if($hasFeedFavicon) { - $this->expectCore('get_image_url', $this->feedImage); - $feed->setFaviconLink($this->feedImage); - } elseif(!$hasWebFavicon) { - $feed->setFaviconLink(null); - $this->expectCore('get_image_url', null); - } - - - return $feed; - } - - - public function testFetchMapItems(){ - $this->core->expects($this->once()) - ->method('init') - ->will($this->returnValue(true)); - $item = $this->createItem(); - $feed = $this->createFeed(); - $this->expectCore('get_items', array($this->item)); - $result = $this->fetcher->fetch($this->url); - - $this->assertEquals(array($feed, array($item)), $result); - } - - - public function testFetchMapItemsNoFeedTitleUsesUrl(){ - $this->expectCore('get_title', ''); - $this->expectCore('get_permalink', $this->feedLink); - - $feed = new Feed(); - $feed->setTitle($this->url); - $feed->setUrl($this->url); - $feed->setLink($this->feedLink); - $feed->setAdded($this->time); - $feed->setFaviconLink(null); - - $this->core->expects($this->once()) - ->method('init') - ->will($this->returnValue(true)); - $item = $this->createItem(); - $this->expectCore('get_items', array($this->item)); - $result = $this->fetcher->fetch($this->url); - - $this->assertEquals(array($feed, array($item)), $result); - } - - public function testFetchMapItemsAuthorExists(){ - $this->core->expects($this->once()) - ->method('init') - ->will($this->returnValue(true)); - $item = $this->createItem(true); - $feed = $this->createFeed(true); - $this->expectCore('get_items', array($this->item)); - $result = $this->fetcher->fetch($this->url); - - $this->assertEquals(array($feed, array($item)), $result); - } - - - public function testFetchMapItemsEnclosureExists(){ - $this->core->expects($this->once()) - ->method('init') - ->will($this->returnValue(true)); - $item = $this->createItem(false, true); - $feed = $this->createFeed(false, true); - $this->expectCore('get_items', array($this->item)); - $result = $this->fetcher->fetch($this->url); - - $this->assertEquals(array($feed, array($item)), $result); - } - - - public function testFetchMapItemsNoPubdate(){ - $this->core->expects($this->once()) - ->method('init') - ->will($this->returnValue(true)); - $item = $this->createItem(false, true, true); - $feed = $this->createFeed(false, true); - $this->expectCore('get_items', array($this->item)); - $result = $this->fetcher->fetch($this->url); - - $this->assertEquals(array($feed, array($item)), $result); - } - - - public function testFetchMapItemsGetFavicon() { - $this->expectCore('get_title', $this->feedTitle); - $this->expectCore('get_permalink', $this->feedLink); - - $feed = new Feed(); - $feed->setTitle(html_entity_decode($this->feedTitle)); - $feed->setUrl($this->url); - $feed->setLink($this->feedLink); - $feed->setAdded($this->time); - $feed->setFaviconLink($this->webFavicon); - - $this->core->expects($this->once()) - ->method('init') - ->will($this->returnValue(true)); - - $this->faviconFetcher->expects($this->once()) - ->method('fetch') - ->will($this->returnValue($this->webFavicon)); - - $item = $this->createItem(false, true); - $this->expectCore('get_items', array($this->item)); - $result = $this->fetcher->fetch($this->url /*, true*/); - - $this->assertEquals(array($feed, array($item)), $result); - } - - public function testFetchMapItemsNoGetFavicon() { - $this->expectCore('get_title', $this->feedTitle); - $this->expectCore('get_permalink', $this->feedLink); - - $feed = new Feed(); - $feed->setTitle(html_entity_decode($this->feedTitle)); - $feed->setUrl($this->url); - $feed->setLink($this->feedLink); - $feed->setAdded($this->time); - - $this->core->expects($this->once()) - ->method('init') - ->will($this->returnValue(true)); - - $this->faviconFetcher->expects($this->never()) - ->method('fetch'); - - $item = $this->createItem(false, true); - $this->expectCore('get_items', array($this->item)); - $result = $this->fetcher->fetch($this->url, false); - - $this->assertEquals(array($feed, array($item)), $result); - } - - -} diff --git a/tests/unit/utility/FetcherTest.php b/tests/unit/utility/FetcherTest.php deleted file mode 100644 index 8a57c9193..000000000 --- a/tests/unit/utility/FetcherTest.php +++ /dev/null @@ -1,108 +0,0 @@ -. -* -*/ - -namespace OCA\News\Utility; - -require_once(__DIR__ . "/../../classloader.php"); - - -class FetcherTest extends \OCA\AppFramework\Utility\TestUtility { - - private $fetcher; - - protected function setUp(){ - $this->fetcher = new Fetcher(); - } - - - public function testFetch(){ - $url = 'hi'; - $mockFetcher = $this->getMockBuilder('\OCA\News\Utility\IFeedFetcher') - ->disableOriginalConstructor() - ->getMock(); - $mockFetcher->expects($this->once()) - ->method('canHandle') - ->with($this->equalTo($url)) - ->will($this->returnValue(true)); - $this->fetcher->registerFetcher($mockFetcher); - - $this->fetcher->fetch($url); - } - - - public function testMultipleFetchers(){ - $url = 'hi'; - $mockFetcher = $this->getMockBuilder('\OCA\News\Utility\IFeedFetcher') - ->disableOriginalConstructor() - ->getMock(); - $mockFetcher->expects($this->once()) - ->method('canHandle') - ->with($this->equalTo($url)) - ->will($this->returnValue(false)); - $mockFetcher2 = $this->getMockBuilder('\OCA\News\Utility\IFeedFetcher') - ->disableOriginalConstructor() - ->getMock(); - $mockFetcher2->expects($this->once()) - ->method('canHandle') - ->with($this->equalTo($url)) - ->will($this->returnValue(true)); - - $this->fetcher->registerFetcher($mockFetcher); - $this->fetcher->registerFetcher($mockFetcher2); - - $this->fetcher->fetch($url); - } - - - public function testMultipleFetchersOnlyOneShouldHandle(){ - $url = 'hi'; - $return = 'zeas'; - $mockFetcher = $this->getMockBuilder('\OCA\News\Utility\IFeedFetcher') - ->disableOriginalConstructor() - ->getMock(); - $mockFetcher->expects($this->once()) - ->method('canHandle') - ->with($this->equalTo($url)) - ->will($this->returnValue(true)); - $mockFetcher->expects($this->once()) - ->method('fetch') - ->with($this->equalTo($url)) - ->will($this->returnValue($return)); - $mockFetcher2 = $this->getMockBuilder('\OCA\News\Utility\IFeedFetcher') - ->disableOriginalConstructor() - ->getMock(); - $mockFetcher2->expects($this->never()) - ->method('canHandle'); - - $this->fetcher->registerFetcher($mockFetcher); - $this->fetcher->registerFetcher($mockFetcher2); - - $result = $this->fetcher->fetch($url); - - $this->assertEquals($return, $result); - } - - -} \ No newline at end of file diff --git a/tests/unit/utility/articleenhancer/EnhancerTest.php b/tests/unit/utility/articleenhancer/EnhancerTest.php deleted file mode 100644 index 769538740..000000000 --- a/tests/unit/utility/articleenhancer/EnhancerTest.php +++ /dev/null @@ -1,91 +0,0 @@ -. -* -*/ - -namespace OCA\News\Utility\ArticleEnhancer; - -use \OCA\News\Db\Item; - -require_once(__DIR__ . "/../../../classloader.php"); - - -class EnhancerTest extends \OCA\AppFramework\Utility\TestUtility { - - private $enhancer; - private $articleEnhancer; - private $articleEnhancer2; - - protected function setUp(){ - $this->enhancer = new Enhancer(); - $this->articleEnhancer = $this->getMockBuilder( - '\OCA\News\Utility\ArticleEnhancer\ArticleEnhancer') - ->disableOriginalConstructor() - ->getMock(); - $this->enhancer->registerEnhancer('test.com', $this->articleEnhancer); - } - - - public function testEnhanceSetsCorrectHash(){ - $item = new Item(); - $item->setUrl('hi'); - $urls = array( - 'https://test.com', - 'https://www.test.com', - 'https://test.com/', - 'http://test.com', - 'http://test.com/', - 'http://www.test.com' - ); - for ($i=0; $i < count($urls); $i++) { - $url = $urls[$i]; - $this->articleEnhancer->expects($this->at($i)) - ->method('enhance') - ->with($this->equalTo($item)) - ->will($this->returnValue($item)); - } - - for ($i=0; $i < count($urls); $i++) { - $url = $urls[$i]; - $result = $this->enhancer->enhance($item, $url); - $this->assertEquals($item, $result); - } - - } - - - public function testNotMatchShouldJustReturnItem() { - $item = new Item(); - $item->setUrl('hi'); - - $url = 'https://tests.com'; - $this->articleEnhancer->expects($this->never()) - ->method('enhance'); - - $result = $this->enhancer->enhance($item, $url); - $this->assertEquals($item, $result); - - } - - -} \ No newline at end of file diff --git a/tests/unit/utility/articleenhancer/RegexArticleEnhancerTest.php b/tests/unit/utility/articleenhancer/RegexArticleEnhancerTest.php deleted file mode 100644 index f775eac91..000000000 --- a/tests/unit/utility/articleenhancer/RegexArticleEnhancerTest.php +++ /dev/null @@ -1,49 +0,0 @@ -. -* -*/ - -namespace OCA\News\Utility\ArticleEnhancer; - -use \OCA\News\Db\Item; - -require_once(__DIR__ . "/../../../classloader.php"); - - -class RegexArticleEnhancerTest extends \OCA\AppFramework\Utility\TestUtility { - - - public function testRegexEnhancer() { - $item = new Item(); - $item->setBody('atests is a nice thing'); - $item->setUrl('http://john.com'); - $regex = array("%tes(ts)%" => "heho$1tests"); - - $regexEnhancer = new RegexArticleEnhancer('%john.com%', $regex); - $item = $regexEnhancer->enhance($item); - - $this->assertEquals('ahehotstests is a nice thing', $item->getBody()); - } - - -} \ No newline at end of file diff --git a/tests/unit/utility/articleenhancer/XPathArticleEnhancerTest.php b/tests/unit/utility/articleenhancer/XPathArticleEnhancerTest.php deleted file mode 100644 index b9c902147..000000000 --- a/tests/unit/utility/articleenhancer/XPathArticleEnhancerTest.php +++ /dev/null @@ -1,288 +0,0 @@ -. -* -*/ - -namespace OCA\News\Utility\ArticleEnhancer; - -use \OCA\News\Db\Item; - -require_once(__DIR__ . "/../../../classloader.php"); - - -class XPathArticleEnhancerTest extends \OCA\AppFramework\Utility\TestUtility { - - private $purifier; - private $testEnhancer; - private $fileFactory; - private $timeout; - - protected function setUp() { - $timeout = 30; - $this->fileFactory = $this->getMockBuilder('\OCA\News\Utility\SimplePieFileFactory') - ->disableOriginalConstructor() - ->getMock(); - $this->purifier = $this->getMock('purifier', array('purify')); - - $this->testEnhancer = new XPathArticleEnhancer( - $this->purifier, - $this->fileFactory, - array( - '/explosm.net\/comics/' => '//*[@id=\'maincontent\']/div[2]/div/span', - '/explosm.net\/shorts/' => '//*[@id=\'maincontent\']/div/div', - '/explosm.net\/all/' => '//body/*', - '/themerepublic.net/' => '//*[@class=\'post hentry\']' - ), - $this->timeout - ); - } - - - public function testDoesNotModifiyNotMatchingResults() { - $item = new Item(); - $item->setUrl('http://explosm.net'); - $this->assertEquals($item, $this->testEnhancer->enhance($item)); - } - - - public function testDoesModifiyArticlesThatMatch() { - $file = new \stdClass; - $file->headers = array("content-type"=>"text/html; charset=utf-8"); - $file->body = ' - -
-
nooo
-
hiho
-
- - '; - $item = new Item(); - $item->setUrl('https://www.explosm.net/comics/312'); - $item->setBody('Hello thar'); - - $this->fileFactory->expects($this->once()) - ->method('getFile') - ->with($this->equalTo($item->getUrl()), - $this->equalTo($this->timeout)) - ->will($this->returnValue($file)); - $this->purifier->expects($this->once()) - ->method('purify') - ->with($this->equalTo('hiho')) - ->will($this->returnValue('hiho')); - - $result = $this->testEnhancer->enhance($item); - $this->assertEquals('hiho', $result->getBody()); - } - - - public function testDoesModifiyAllArticlesThatMatch() { - $file = new \stdClass; - $file->headers = array("content-type"=>"text/html; charset=utf-8"); - $file->body = ' - -
-
nooo
hiho
-
rawr
-
- - '; - $item = new Item(); - $item->setUrl('https://www.explosm.net/shorts/312'); - $item->setBody('Hello thar'); - - $this->fileFactory->expects($this->once()) - ->method('getFile') - ->with($this->equalTo($item->getUrl()), - $this->equalTo($this->timeout)) - ->will($this->returnValue($file)); - $this->purifier->expects($this->once()) - ->method('purify') - ->with($this->equalTo('
hiho
rawr
')) - ->will($this->returnValue('
hiho
rawr
')); - - $result = $this->testEnhancer->enhance($item); - $this->assertEquals('
hiho
rawr
', $result->getBody()); - } - - - public function testModificationHandlesEmptyResults() { - $file = new \stdClass; - $file->headers = array("content-type"=>"text/html; charset=utf-8"); - $file->body = ' - -
-
- - '; - $item = new Item(); - $item->setUrl('https://www.explosm.net/comics/312'); - $item->setBody('Hello thar'); - - $this->fileFactory->expects($this->once()) - ->method('getFile') - ->with($this->equalTo($item->getUrl()), - $this->equalTo($this->timeout)) - ->will($this->returnValue($file)); - $this->purifier->expects($this->once()) - ->method('purify') - ->with($this->equalTo(null)) - ->will($this->returnValue(null)); - - $result = $this->testEnhancer->enhance($item); - $this->assertEquals(null, $result->getBody()); - } - - - public function testModificationDoesNotBreakOnEmptyDom() { - $file = new \stdClass; - $file->headers = array("content-type"=>"text/html; charset=utf-8"); - $file->body = ''; - $item = new Item(); - $item->setUrl('https://www.explosm.net/comics/312'); - $item->setBody('Hello thar'); - - $this->fileFactory->expects($this->once()) - ->method('getFile') - ->with($this->equalTo($item->getUrl()), - $this->equalTo($this->timeout)) - ->will($this->returnValue($file)); - $this->purifier->expects($this->once()) - ->method('purify') - ->with($this->equalTo(null)) - ->will($this->returnValue(null)); - - $result = $this->testEnhancer->enhance($item); - $this->assertEquals(null, $result->getBody()); - } - - - public function testModificationDoesNotBreakOnBrokenDom() { - $file = new \stdClass; - $file->headers = array("content-type"=>"text/html; charset=utf-8"); - $file->body = '

- -

-
- - '; - $item = new Item(); - $item->setUrl('https://www.explosm.net/comics/312'); - $item->setBody('Hello thar'); - - $this->fileFactory->expects($this->once()) - ->method('getFile') - ->with($this->equalTo($item->getUrl()), - $this->equalTo($this->timeout)) - ->will($this->returnValue($file)); - $this->purifier->expects($this->once()) - ->method('purify') - ->with($this->equalTo(null)) - ->will($this->returnValue(null)); - - $result = $this->testEnhancer->enhance($item); - $this->assertEquals(null, $result->getBody()); - } - - - public function testTransformRelativeUrls() { - $file = new \stdClass; - $file->headers = array("content-type"=>"text/html; charset=utf-8"); - $file->body = ' - - link - link2 - - - '; - $item = new Item(); - $item->setUrl('https://www.explosm.net/all/312'); - $item->setBody('Hello thar'); - - $this->fileFactory->expects($this->once()) - ->method('getFile') - ->with($this->equalTo($item->getUrl()), - $this->equalTo($this->timeout)) - ->will($this->returnValue($file)); - $this->purifier->expects($this->once()) - ->method('purify') - ->with($this->equalTo('linklink2')) - ->will($this->returnValue('linklink2')); - - $result = $this->testEnhancer->enhance($item); - $this->assertEquals('linklink2', $result->getBody()); - } - - public function testTransformRelativeUrlSpecials() { - $file = new \stdClass; - $file->headers = array("content-type"=>"text/html; charset=utf-8"); - $file->body = ' - - - - '; - $item = new Item(); - $item->setUrl('https://username:secret@www.explosm.net/all/312'); - $item->setBody('Hello thar'); - - $this->fileFactory->expects($this->once()) - ->method('getFile') - ->with($this->equalTo($item->getUrl()), - $this->equalTo($this->timeout)) - ->will($this->returnValue($file)); - $this->purifier->expects($this->once()) - ->method('purify') - ->with($this->equalTo('')) - ->will($this->returnValue('