diff options
author | Bernhard Posselt <dev@bernhard-posselt.com> | 2016-07-23 21:24:54 +0200 |
---|---|---|
committer | Bernhard Posselt <dev@bernhard-posselt.com> | 2016-07-23 21:24:54 +0200 |
commit | 004fcbbcc7609ca83807f2e38967ef54f469bf72 (patch) | |
tree | 49eb99b4ea92b2045793fc567f719b31ec7f9042 /tests/Unit/Fetcher | |
parent | 60abc0ed4438c9b6fda245b0dc33cb483bc2aeaf (diff) |
Move to new directory structure
Diffstat (limited to 'tests/Unit/Fetcher')
-rw-r--r-- | tests/Unit/Fetcher/FeedFetcherTest.php | 386 | ||||
-rw-r--r-- | tests/Unit/Fetcher/FetcherTest.php | 137 | ||||
-rw-r--r-- | tests/Unit/Fetcher/YoutubeFetcherTest.php | 68 |
3 files changed, 591 insertions, 0 deletions
diff --git a/tests/Unit/Fetcher/FeedFetcherTest.php b/tests/Unit/Fetcher/FeedFetcherTest.php new file mode 100644 index 000000000..4b91e15c3 --- /dev/null +++ b/tests/Unit/Fetcher/FeedFetcherTest.php @@ -0,0 +1,386 @@ +<?php +/** + * ownCloud - News + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Alessandro Cosentino <cosenal@gmail.com> + * @author Bernhard Posselt <dev@bernhard-posselt.com> + * @copyright Alessandro Cosentino 2012 + * @copyright Bernhard Posselt 2012, 2014 + */ + +namespace OCA\News\Fetcher; + +use \OCA\News\Db\Item; +use \OCA\News\Db\Feed; + + +class FeedFetcherTest extends \PHPUnit_Framework_TestCase { + + private $fetcher; + private $parser; + private $reader; + private $client; + private $faviconFetcher; + private $parsedFeed; + private $faviconFactory; + private $l10n; + private $url; + private $time; + private $item; + private $content; + private $encoding; + + // items + private $permalink; + private $title; + private $guid; + private $pub; + private $body; + private $author; + private $enclosureLink; + private $rtl; + private $language; + + // feed + private $feedTitle; + private $feedLink; + private $feedImage; + private $webFavicon; + private $modified; + private $etag; + private $location; + private $feedLanguage; + + protected function setUp(){ + $this->l10n = $this->getMockBuilder( + '\OCP\IL10N') + ->disableOriginalConstructor() + ->getMock(); + $this->reader = $this->getMockBuilder( + '\PicoFeed\Reader\Reader') + ->disableOriginalConstructor() + ->getMock(); + $this->parser = $this->getMockBuilder( + '\PicoFeed\Parser\Parser') + ->disableOriginalConstructor() + ->getMock(); + $this->client = $this->getMockBuilder( + '\PicoFeed\Client\Client') + ->disableOriginalConstructor() + ->getMock(); + $this->parsedFeed = $this->getMockBuilder( + '\PicoFeed\Parser\Feed') + ->disableOriginalConstructor() + ->getMock(); + $this->item = $this->getMockBuilder( + '\PicoFeed\Parser\Item') + ->disableOriginalConstructor() + ->getMock(); + $this->faviconFetcher = $this->getMockBuilder( + '\PicoFeed\Reader\Favicon') + ->disableOriginalConstructor() + ->getMock(); + $this->faviconFactory = $this->getMockBuilder( + '\OCA\News\Utility\PicoFeedFaviconFactory') + ->disableOriginalConstructor() + ->getMock(); + + $this->time = 2323; + $timeFactory = $this->getMockBuilder( + '\OCA\News\Utility\Time') + ->disableOriginalConstructor() + ->getMock(); + $timeFactory->expects($this->any()) + ->method('getTime') + ->will($this->returnValue($this->time)); + $this->fetcher = new FeedFetcher( + $this->reader, + $this->faviconFactory, + $this->l10n, + $timeFactory); + $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 <a href="test">test</a>'; + $this->body2 = 'let the bodies hit the floor ' . + '<a target="_blank" href="test">test</a>'; + $this->pub = 23111; + $this->author = '<boogieman'; + $this->enclosureLink = 'http://enclosure.you'; + + $this->feedTitle = '<a>&its a</a> title'; + $this->feedLink = 'http://goatse'; + $this->feedImage = '/an/image'; + $this->webFavicon = 'http://anon.google.com'; + $this->authorMail = 'doe@joes.com'; + $this->modified = 3; + $this->etag = 'yo'; + $this->content = 'some content'; + $this->encoding = 'UTF-8'; + $this->language = 'de-DE'; + $this->feedLanguage = 'de-DE'; + } + + + public function testCanHandle(){ + $url = 'google.de'; + + $this->assertTrue($this->fetcher->canHandle($url)); + } + + private function setUpReader($url='', $modified=true, $noParser=false) { + $this->reader->expects($this->once()) + ->method('discover') + ->with($this->equalTo($url)) + ->will($this->returnValue($this->client)); + $this->client->expects($this->once()) + ->method('isModified') + ->will($this->returnValue($modified)); + + if (!$modified) { + $this->reader->expects($this->never()) + ->method('getParser'); + } else { + $this->client->expects($this->once()) + ->method('getLastModified') + ->will($this->returnValue($this->modified)); + $this->client->expects($this->once()) + ->method('getEtag') + ->will($this->returnValue($this->etag)); + $this->client->expects($this->once()) + ->method('getUrl') + ->will($this->returnValue($this->location)); + $this->client->expects($this->once()) + ->method('getContent') + ->will($this->returnValue($this->content)); + $this->client->expects($this->once()) + ->method('getEncoding') + ->will($this->returnValue($this->encoding)); + + if ($noParser) { + $this->reader->expects($this->once()) + ->method('getParser') + ->will($this->throwException( + new \PicoFeed\Reader\SubscriptionNotFoundException() + )); + } else { + $this->reader->expects($this->once()) + ->method('getParser') + ->with( + $this->equalTo($this->location), + $this->equalTo($this->content), + $this->equalTo($this->encoding) + ) + ->will($this->returnValue($this->parser)); + } + + $this->parser->expects($this->once()) + ->method('execute') + ->will($this->returnValue($this->parsedFeed)); + } + + } + + + private function expectFeed($method, $return, $count = 1) { + $this->parsedFeed->expects($this->exactly($count)) + ->method($method) + ->will($this->returnValue($return)); + } + + private function expectItem($method, $return, $count = 1) { + $this->item->expects($this->exactly($count)) + ->method($method) + ->will($this->returnValue($return)); + } + + + private function createItem($enclosureType=null) { + $this->expectItem('getUrl', $this->permalink); + $this->expectItem('getTitle', $this->title); + $this->expectItem('getId', $this->guid); + $this->expectItem('getContent', $this->body); + + $item = new Item(); + + date_default_timezone_set('America/Los_Angeles'); + $date = new \DateTime(); + $date->setTimestamp($this->pub); + $this->expectItem('getDate', $date); + $item->setPubDate($this->pub); + + $item->setStatus(0); + $item->setUnread(); + $item->setUrl($this->permalink); + $item->setTitle('my<\' title'); + $item->setGuid($this->guid); + $item->setGuidHash($this->guid); + $item->setBody($this->body); + $item->setRtl(false); + + $this->expectItem('getAuthor', $this->author); + $item->setAuthor(html_entity_decode($this->author)); + + if($enclosureType === 'audio/ogg' || $enclosureType === 'video/ogg') { + $this->expectItem('getEnclosureUrl', $this->enclosureLink); + $this->expectItem('getEnclosureType', $enclosureType); + + $item->setEnclosureMime($enclosureType); + $item->setEnclosureLink($this->enclosureLink); + } + $item->generateSearchIndex(); + + return $item; + } + + + private function createFeed($hasFavicon=false) { + $this->expectFeed('getTitle', $this->feedTitle); + $this->expectFeed('getSiteUrl', $this->feedLink); + + $feed = new Feed(); + $feed->setTitle('&its a title'); + $feed->setUrl($this->url); + $feed->setLink($this->feedLink); + $feed->setAdded($this->time); + $feed->setHttpLastModified($this->modified); + $feed->setHttpEtag($this->etag); + $feed->setLocation($this->location); + + if($hasFavicon) { + $this->faviconFactory->expects($this->once()) + ->method('build') + ->will($this->returnValue($this->faviconFetcher)); + $this->faviconFetcher->expects($this->once()) + ->method('find') + ->with($this->equalTo($this->feedLink)) + ->will($this->returnValue($this->webFavicon)); + $feed->setFaviconLink($this->webFavicon); + } + + return $feed; + } + + public function testNoFetchIfNotModified(){ + $this->setUpReader($this->url, false);; + $result = $this->fetcher->fetch($this->url, false); + } + + public function testFetch(){ + $this->setUpReader($this->url); + $item = $this->createItem(); + $feed = $this->createFeed(); + $this->expectFeed('getItems', [$this->item]); + $result = $this->fetcher->fetch($this->url, false); + + $this->assertEquals([$feed, [$item]], $result); + } + + + public function testAudioEnclosure(){ + $this->setUpReader($this->url); + $item = $this->createItem('audio/ogg'); + $feed = $this->createFeed(); + $this->expectFeed('getItems', [$this->item]); + $result = $this->fetcher->fetch($this->url, false); + + $this->assertEquals([$feed, [$item]], $result); + } + + + public function testVideoEnclosure(){ + $this->setUpReader($this->url); + $item = $this->createItem('video/ogg'); + $feed = $this->createFeed(); + $this->expectFeed('getItems', [$this->item]); + $result = $this->fetcher->fetch($this->url, false); + + $this->assertEquals([$feed, [$item]], $result); + } + + + + public function testFavicon() { + $this->setUpReader($this->url); + + $feed = $this->createFeed(true); + $item = $this->createItem(); + $this->expectFeed('getItems', [$this->item]); + $result = $this->fetcher->fetch($this->url); + + $this->assertEquals([$feed, [$item]], $result); + } + + public function testFullText() { + $this->setUpReader($this->url); + + $feed = $this->createFeed(); + $item = $this->createItem(); + $this->parser->expects($this->once()) + ->method('enableContentGrabber'); + $this->expectFeed('getItems', [$this->item]); + $this->fetcher->fetch($this->url, false, null, null, true); + } + + + public function testNoFavicon() { + $this->setUpReader($this->url); + + $feed = $this->createFeed(false); + + $this->faviconFetcher->expects($this->never()) + ->method('find'); + + $item = $this->createItem(); + $this->expectFeed('getItems', [$this->item]); + $result = $this->fetcher->fetch($this->url, false); + + $this->assertEquals([$feed, [$item]], $result); + } + + + public function testRtl() { + $this->setUpReader($this->url); + $this->expectFeed('getLanguage', 'he-IL'); + $this->expectItem('getLanguage', ''); + $feed = $this->createFeed(); + $item = $this->createItem(null); + $this->expectFeed('getItems', [$this->item]); + list($feed, $items) = $this->fetcher->fetch($this->url, false, null, + null, true); + $this->assertTrue($items[0]->getRtl()); + } + + + public function testRtlItemPrecedence() { + $this->setUpReader($this->url); + $this->expectFeed('getLanguage', 'de-DE'); + $this->expectItem('getLanguage', 'he-IL'); + + $feed = $this->createFeed(); + $item = $this->createItem(null); + $this->expectFeed('getItems', [$this->item]); + list($feed, $items) = $this->fetcher->fetch($this->url, false, null, + null, true); + $this->assertTrue($items[0]->getRtl()); + } + + public function testNegativeRtlItemPrecedence() { + $this->setUpReader($this->url); + $this->expectFeed('getLanguage', 'he-IL'); + $this->expectItem('getLanguage', 'de-DE'); + + $feed = $this->createFeed(); + $item = $this->createItem(null); + $this->expectFeed('getItems', [$this->item]); + list($feed, $items) = $this->fetcher->fetch($this->url, false, null, + null, true); + $this->assertFalse($items[0]->getRtl()); + } + +} diff --git a/tests/Unit/Fetcher/FetcherTest.php b/tests/Unit/Fetcher/FetcherTest.php new file mode 100644 index 000000000..5b9efc920 --- /dev/null +++ b/tests/Unit/Fetcher/FetcherTest.php @@ -0,0 +1,137 @@ +<?php + +/** +* ownCloud - News +* +* @author Alessandro Cosentino +* @author Bernhard Posselt +* @copyright 2012 Alessandro Cosentino cosenal@gmail.com +* @copyright 2012 Bernhard Posselt dev@bernhard-posselt.com +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE +* License as published by the Free Software Foundation; either +* version 3 of the License, or any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU AFFERO GENERAL PUBLIC LICENSE for more details. +* +* You should have received a copy of the GNU Affero General Public +* License along with this library. If not, see <http://www.gnu.org/licenses/>. +* +*/ + +namespace OCA\News\Fetcher; + + +class FetcherTest extends \PHPUnit_Framework_TestCase { + + 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)); + $mockFetcher->expects($this->once()) + ->method('fetch') + ->with($this->equalTo($url), + $this->equalTo(true), + $this->equalTo(1), + $this->equalTo(2), + $this->equalTo(3)); + $this->fetcher->registerFetcher($mockFetcher); + + $this->fetcher->fetch($url, true, 1, 2, 3); + } + + + public function testNoFetchers(){ + $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(false)); + + $this->fetcher->registerFetcher($mockFetcher); + $this->fetcher->registerFetcher($mockFetcher2); + + $result = $this->fetcher->fetch($url); + $this->assertEquals([null, []], $result); + } + + 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); + } + + +} diff --git a/tests/Unit/Fetcher/YoutubeFetcherTest.php b/tests/Unit/Fetcher/YoutubeFetcherTest.php new file mode 100644 index 000000000..4adf734ba --- /dev/null +++ b/tests/Unit/Fetcher/YoutubeFetcherTest.php @@ -0,0 +1,68 @@ +<?php +/** + * ownCloud - News + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Bernhard Posselt <dev@bernhard-posselt.com> + * @copyright Bernhard Posselt 2012, 2014 + */ + +namespace OCA\News\Fetcher; + +use \OCA\News\Db\Feed; + + +class YoutubeFetcherTest extends \PHPUnit_Framework_TestCase { + + private $fetcher; + private $feedFetcher; + + public function setUp() { + $this->feedFetcher = $this->getMockBuilder( + '\OCA\News\Fetcher\FeedFetcher') + ->disableOriginalConstructor() + ->getMock(); + $this->fetcher = new YoutubeFetcher($this->feedFetcher); + } + + + public function testCanHandleFails() { + $url = 'http://youtube.com'; + $this->assertFalse($this->fetcher->canHandle($url)); + } + + + public function testCanHandle() { + $url = 'http://youtube.com/test/?test=a&list=b&b=c'; + $this->assertTrue($this->fetcher->canHandle($url)); + } + + + public function testPlaylistUrl() { + $url = 'http://youtube.com/something/weird?a=b&list=sobo3&c=1'; + $transformedUrl = 'http://gdata.youtube.com/feeds/api/playlists/sobo3'; + $favicon = true; + $modified = 3; + $etag = 5; + $feed = new Feed(); + $feed->setUrl('http://google.de'); + $result = [$feed, []]; + + $this->feedFetcher->expects($this->once()) + ->method('fetch') + ->with( + $this->equalTo($transformedUrl), + $this->equalTo($favicon), + $this->equalTo($modified), + $this->equalTo($etag) + ) + ->will($this->returnValue($result)); + $feed = $this->fetcher->fetch($url, $favicon, $modified, $etag); + + $this->assertEquals($url, $result[0]->getUrl()); + } + + +}
\ No newline at end of file |