diff options
author | Bernhard Posselt <dev@bernhard-posselt.com> | 2014-10-22 10:49:34 +0200 |
---|---|---|
committer | Bernhard Posselt <dev@bernhard-posselt.com> | 2014-10-22 10:49:45 +0200 |
commit | cc3fa38cee2e24dacb940ec5d7ca41e593aa824a (patch) | |
tree | 376eeb6910fc24d14f19992ab4a3d64aa1189448 | |
parent | 720db05f884817ef3683acb697ab89b76a8e3f91 (diff) |
fix tests
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | appinfo/application.php | 34 | ||||
-rw-r--r-- | fetcher/feedfetcher.php | 11 | ||||
-rw-r--r-- | tests/unit/fetcher/FeedFetcherTest.php | 260 | ||||
-rw-r--r-- | tests/unit/utility/ProxyConfigParserTest.php | 93 | ||||
-rw-r--r-- | utility/proxyconfigparser.php | 65 |
6 files changed, 282 insertions, 185 deletions
@@ -79,4 +79,6 @@ appstore: clean --exclude=$(project_dir)/3rdparty/ezyang/htmlpurifier/maintenance \ --exclude=$(project_dir)/3rdparty/ezyang/htmlpurifier/plugins \ --exclude=$(project_dir)/3rdparty/ezyang/htmlpurifier/smoketests \ - --exclude=$(project_dir)/3rdparty/ezyang/htmlpurifier/tests + --exclude=$(project_dir)/3rdparty/ezyang/htmlpurifier/tests \ + --exclude=$(project_dir)/3rdparty/fguillot/picofeed/docs \ + --exclude=$(project_dir)/3rdparty/fguillot/picofeed/tests diff --git a/appinfo/application.php b/appinfo/application.php index 0faceb5be..be903b120 100644 --- a/appinfo/application.php +++ b/appinfo/application.php @@ -51,7 +51,7 @@ use \OCA\News\Db\MapperFactory; use \OCA\News\Utility\OPMLExporter; use \OCA\News\Utility\Updater; use \OCA\News\Utility\PicoFeedClientFactory; -use \OCA\News\Utility\FaviconFetcher; +use \OCA\News\Utility\ProxyConfigParser; use \OCA\News\Fetcher\Fetcher; use \OCA\News\Fetcher\FeedFetcher; @@ -413,6 +413,7 @@ class Application extends App { // FIXME: move this into a separate class for testing? $config = $c->query('Config'); $appConfig = $c->query('AppConfig'); + $proxy = $c->query('ProxyConfigParser'); $pico = new PicoFeedConfig(); $pico->setClientUserAgent( @@ -424,26 +425,23 @@ class Application extends App { ->setContentFiltering(false); // proxy settings - $proxy = \OCP\Config::getSystemValue('proxy'); - if ($proxy) { - // we need to filter out the port -.- - $url = new \Net_URL2($proxy); - $port = $url->getPort(); - $url->setPort(false); - $host = $url->getUrl(); + $proxySettings = $proxy->parse(); + $host = $proxySettings['host']; + $port = $proxySettings['port']; + $user = $proxySettings['user']; + $password = $proxySettings['password']; + + if ($host) { + $pico->setProxyHostname($host); if ($port) { $pico->setProxyPort($port); } - - $pico->setProxyHostname($host); } - $proxyAuth = \OCP\Config::getSystemValue('proxyuserpwd'); - if ($proxyAuth) { - $auth = explode(':', $proxyAuth, 2); - $pico->setProxyUsername($auth[0]) - ->setProxyPassword($auth[1]); + if ($user) { + $pico->setProxyUsername($user) + ->setProxyPassword($password); } return $pico; @@ -484,6 +482,12 @@ class Application extends App { return new OPMLExporter(); }); + $container->registerService('ProxyConfigParser', function($c) { + return new ProxyConfigParser( + $c->query('CoreConfig') + ); + }); + $container->registerService('Updater', function($c) { return new Updater( $c->query('FolderService'), diff --git a/fetcher/feedfetcher.php b/fetcher/feedfetcher.php index f13b6b653..a2023dd0e 100644 --- a/fetcher/feedfetcher.php +++ b/fetcher/feedfetcher.php @@ -51,6 +51,7 @@ class FeedFetcher implements IFeedFetcher { */ public function fetch($url, $getFavicon=true) { $resource = $this->reader->download($url); + $modified = $resource->getLastModified(); try { @@ -74,7 +75,8 @@ class FeedFetcher implements IFeedFetcher { $link = $parsedFeed->getUrl(); foreach($parsedFeed->getItems() as $item) { - $items[] = $this->buildItem($item, $link); + //throw new \Exception($resource->getEncoding() . '' . $item->getContent()); + $items[] = $this->buildItem($item); } $feed = $this->buildFeed($parsedFeed, $url, $getFavicon, $modified); @@ -102,14 +104,11 @@ class FeedFetcher implements IFeedFetcher { } - protected function buildItem($parsedItem, $feedLink) { + protected function buildItem($parsedItem) { $item = new Item(); $item->setStatus(0); $item->setUnread(); $url = $this->decodeTwice($parsedItem->getUrl()); - if (!$url) { - $url = $feedLink; - } $item->setUrl($url); // unescape content because angularjs helps against XSS @@ -118,7 +117,7 @@ class FeedFetcher implements IFeedFetcher { $item->setGuid($guid); // purification is done in the service layer - $item->setBody($parsedItem->getContent()); + $item->setBody(utf8_decode($parsedItem->getContent())); // pubdate is not required. if not given use the current date $date = $parsedItem->getDate(); diff --git a/tests/unit/fetcher/FeedFetcherTest.php b/tests/unit/fetcher/FeedFetcherTest.php index d87d88952..1f3b3d108 100644 --- a/tests/unit/fetcher/FeedFetcherTest.php +++ b/tests/unit/fetcher/FeedFetcherTest.php @@ -36,7 +36,6 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase { private $pub; private $body; private $author; - private $authorMail; private $enclosureLink; // feed @@ -44,6 +43,7 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase { private $feedLink; private $feedImage; private $webFavicon; + private $modified; protected function setUp(){ $this->reader = $this->getMockBuilder( @@ -62,11 +62,15 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase { '\PicoFeed\Feed') ->disableOriginalConstructor() ->getMock(); - + $this->item = $this->getMockBuilder( + '\PicoFeed\Item') + ->disableOriginalConstructor() + ->getMock(); $this->faviconFetcher = $this->getMockBuilder( '\PicoFeed\Favicon') ->disableOriginalConstructor() ->getMock(); + $this->time = 2323; $timeFactory = $this->getMock('TimeFactory', ['getTime']); $timeFactory->expects($this->any()) @@ -92,6 +96,7 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase { $this->feedImage = '/an/image'; $this->webFavicon = 'http://anon.google.com'; $this->authorMail = 'doe@joes.com'; + $this->modified = 3; } @@ -101,42 +106,45 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase { $this->assertTrue($this->fetcher->canHandle($url)); } - private function setUpReader($url='', $modified=true) { + private function setUpReader($url='', $modified=true, $noParser=false, + $noFeed=false) { $this->reader->expects($this->once()) ->method('download') ->with($this->equalTo($url)) ->will($this->returnValue($this->client)); $this->client->expects($this->once()) ->method('getLastModified') - ->with() - ->will($this->returnValue($modified)); - } - - - public function testFetchThrowsExceptionWhenFetchingFailed() { - $this->setUpReader($this->url); - $this->reader->expects($this->once()) - ->method('getParser') - ->will($this->returnValue(false)); + ->will($this->returnValue($this->modified)); - $this->setExpectedException('\OCA\News\Fetcher\FetcherException'); - $this->fetcher->fetch($this->url); - } - - - public function testFetchThrowsExceptionWhenParsingFailed() { - $this->setUpReader($this->url); - $this->reader->expects($this->once()) - ->method('getParser') - ->will($this->returnValue(false)); + if ($noParser) { + $this->reader->expects($this->once()) + ->method('getParser') + ->will($this->returnValue(false)); + } else { + $this->reader->expects($this->once()) + ->method('getParser') + ->will($this->returnValue($this->parser)); + + if ($noFeed) { + $this->parser->expects($this->once()) + ->method('execute') + ->will($this->returnValue(false)); + } else { + $this->parser->expects($this->once()) + ->method('execute') + ->will($this->returnValue($this->parsedFeed)); + } + } - $this->setExpectedException('\OCA\News\Fetcher\FetcherException'); - $this->fetcher->fetch($this->url); + // uncomment if testing caching + /*$this->client->expects($this->once()) + ->method('isModified') + ->will($this->returnValue($modified));*/ } - private function expectCore($method, $return, $count = 1) { - $this->core->expects($this->exactly($count)) + private function expectFeed($method, $return, $count = 1) { + $this->parsedFeed->expects($this->exactly($count)) ->method($method) ->will($this->returnValue($return)); } @@ -148,22 +156,16 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase { } - private function createItem($author=false, $enclosureType=null, - $noPubDate=false) { - $this->expectItem('get_permalink', $this->permalink); - $this->expectItem('get_title', $this->title); - $this->expectItem('get_id', $this->guid); - $this->expectItem('get_content', $this->body); + 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(); - if($noPubDate) { - $this->expectItem('get_date', 0); - $item->setPubDate($this->time); - } else { - $this->expectItem('get_date', $this->pub); - $item->setPubDate($this->pub); - } + $this->expectItem('getDate', $this->pub); + $item->setPubDate($this->pub); $item->setStatus(0); $item->setUnread(); @@ -173,46 +175,14 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase { $item->setGuidHash(md5($this->guid)); $item->setBody($this->body); $item->setLastModified($this->time); - if($author) { - $mock = $this->getMock('author', ['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', ['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', ['get_type', 'get_link']); - $mock->expects($this->any()) - ->method('get_type') - ->will($this->returnValue($enclosureType)); - $mock->expects($this->any()) - ->method('get_link') - ->will($this->returnValue($this->enclosureLink)); - $this->expectItem('get_enclosure', $mock); - $item->setEnclosureMime($enclosureType); - $item->setEnclosureLink($this->enclosureLink); - } elseif ($enclosureType === 'video/ogg') { - $mock = $this->getMock('enclosure', ['get_type', 'get_link']); - $mock->expects($this->any()) - ->method('get_type') - ->will($this->returnValue($enclosureType)); - $mock->expects($this->any()) - ->method('get_link') - ->will($this->returnValue($this->enclosureLink)); - $this->expectItem('get_enclosure', $mock); + $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); } @@ -220,9 +190,9 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase { } - private function createFeed($hasFeedFavicon=false, $hasWebFavicon=false) { - $this->expectCore('get_title', $this->feedTitle); - $this->expectCore('get_permalink', $this->feedLink, 2); + private function createFeed($hasFavicon=false) { + $this->expectFeed('getTitle', $this->feedTitle); + $this->expectFeed('getUrl', $this->feedLink, 2); $feed = new Feed(); $feed->setTitle('&its a title'); @@ -230,7 +200,7 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase { $feed->setLink($this->feedLink); $feed->setAdded($this->time); - if($hasWebFavicon) { + if($hasFavicon) { $this->faviconFetcher->expects($this->once()) ->method('find') ->with($this->equalTo($this->feedLink)) @@ -238,35 +208,40 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase { $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; + } - return $feed; + public function testFetchThrowsExceptionWhenFetchingFailed() { + $this->setUpReader($this->url, true, false); + + $this->setExpectedException('\OCA\News\Fetcher\FetcherException'); + $this->fetcher->fetch($this->url); } - public function testFetchMapItems(){ - $this->core->expects($this->once()) - ->method('init') - ->will($this->returnValue(true)); - $item = $this->createItem(false, 'audio/ogg'); + public function testFetchThrowsExceptionWhenParsingFailed() { + $this->setUpReader($this->url, true, true, false); + + $this->setExpectedException('\OCA\News\Fetcher\FetcherException'); + $this->fetcher->fetch($this->url); + } + + public function testFetch(){ + $this->setUpReader($this->url); + $item = $this->createItem(); $feed = $this->createFeed(); - $this->expectCore('get_items', [$this->item]); + $this->expectFeed('getItems', [$this->item]); $result = $this->fetcher->fetch($this->url); $this->assertEquals([$feed, [$item]], $result); } - public function testFetchMapItemsNoFeedTitleUsesUrl(){ - $this->expectCore('get_title', ''); - $this->expectCore('get_permalink', $this->feedLink, 2); + public function testNoTitleUsesUrl(){ + $this->setUpReader($this->url); + $this->expectFeed('getTitle', ''); + $this->expectFeed('getUrl', $this->feedLink, 2); $feed = new Feed(); $feed->setTitle($this->url); @@ -275,100 +250,59 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase { $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', [$this->item]); - $result = $this->fetcher->fetch($this->url); - - $this->assertEquals([$feed, [$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', [$this->item]); + $this->expectFeed('getItems', [$this->item]); $result = $this->fetcher->fetch($this->url); $this->assertEquals([$feed, [$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', [$this->item]); + 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); $this->assertEquals([$feed, [$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', [$this->item]); + 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); $this->assertEquals([$feed, [$item]], $result); } - public function testFetchMapItemsGetFavicon() { - $this->expectCore('get_title', $this->feedTitle); - $this->expectCore('get_permalink', $this->feedLink, 2); - $feed = new Feed(); - $feed->setTitle('&its a title'); - $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('find') - ->will($this->returnValue($this->webFavicon)); + public function testFavicon() { + $this->setUpReader($this->url); - $item = $this->createItem(false, 'video/ogg'); - $this->expectCore('get_items', [$this->item]); + $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 testFetchMapItemsNoGetFavicon() { - $this->expectCore('get_title', $this->feedTitle); - $this->expectCore('get_permalink', $this->feedLink, 2); - $feed = new Feed(); - $feed->setTitle('&its a title'); - $feed->setUrl($this->url); - $feed->setLink($this->feedLink); - $feed->setAdded($this->time); + public function testNoFavicon() { + $this->setUpReader($this->url); - $this->core->expects($this->once()) - ->method('init') - ->will($this->returnValue(true)); + $feed = $this->createFeed(false); $this->faviconFetcher->expects($this->never()) - ->method('fetch'); + ->method('find'); - $item = $this->createItem(false, true); - $this->expectCore('get_items', [$this->item]); + $item = $this->createItem(); + $this->expectFeed('getItems', [$this->item]); $result = $this->fetcher->fetch($this->url, false); $this->assertEquals([$feed, [$item]], $result); diff --git a/tests/unit/utility/ProxyConfigParserTest.php b/tests/unit/utility/ProxyConfigParserTest.php new file mode 100644 index 000000000..39f6e61a3 --- /dev/null +++ b/tests/unit/utility/ProxyConfigParserTest.php @@ -0,0 +1,93 @@ +<?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\Utility; + + +class ProxyConfigParserTest extends \PHPUnit_Framework_TestCase { + + private $config; + private $feedService; + private $itemService; + private $parser; + + protected function setUp() { + $this->config = $this->getMockBuilder( + '\OCP\IConfig') + ->disableOriginalConstructor() + ->getMock(); + $this->parser = new ProxyConfigParser($this->config); + } + + private function setExpectedProxy($proxy=null, $userpasswd=null) { + $this->config->expects($this->at(0)) + ->method('getSystemValue') + ->with($this->equalTo('proxy')) + ->will($this->returnValue($proxy)); + $this->config->expects($this->at(1)) + ->method('getSystemValue') + ->with($this->equalTo('proxyuserpwd')) + ->will($this->returnValue($userpasswd)); + } + + public function testParsesNoProxy() { + $expected = [ + 'host' => null, + 'port' => null, + 'user' => null, + 'password' => null + ]; + $this->setExpectedProxy(); + $result = $this->parser->parse(); + $this->assertEquals($expected, $result); + } + + + public function testParsesHost() { + $expected = [ + 'host' => 'http://google.com/mytest', + 'port' => null, + 'user' => null, + 'password' => null + ]; + $this->setExpectedProxy('http://google.com/mytest'); + $result = $this->parser->parse(); + $this->assertEquals($expected, $result); + } + + + public function testParsesHostAndPort() { + $expected = [ + 'host' => 'http://google.com/mytest', + 'port' => 89, + 'user' => null, + 'password' => null + ]; + $this->setExpectedProxy('http://google.com:89/mytest'); + $result = $this->parser->parse(); + $this->assertEquals($expected, $result); + } + + + public function testParsesUser() { + $expected = [ + 'host' => null, + 'port' => null, + 'user' => 'john', + 'password' => 'doe:hey' + ]; + $this->setExpectedProxy(null, 'john:doe:hey'); + $result = $this->parser->parse(); + $this->assertEquals($expected, $result); + } +}
\ No newline at end of file diff --git a/utility/proxyconfigparser.php b/utility/proxyconfigparser.php new file mode 100644 index 000000000..4710477e8 --- /dev/null +++ b/utility/proxyconfigparser.php @@ -0,0 +1,65 @@ +<?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\Utility; + +use \OCP\IConfig; + + +class ProxyConfigParser { + + private $config; + + public function __construct(IConfig $config) { + $this->config = $config; + } + + + /** + * Parses the config and splits up the port + url + * @return array + */ + public function parse() { + $proxy = $this->config->getSystemValue('proxy'); + $userpasswd = $this->config->getSystemValue('proxyuserpwd'); + + $result = [ + 'host' => null, + 'port' => null, + 'user' => null, + 'password' => null + ]; + + // we need to filter out the port -.- + $url = new \Net_URL2($proxy); + $port = $url->getPort(); + + $url->setPort(false); + $host = $url->getUrl(); + + + $result['host'] = $host; + $result['port'] = $port; + + if ($userpasswd) { + $auth = explode(':', $userpasswd, 2); + $result['user'] = $auth[0]; + $result['password'] = $auth[1]; + } + + return $result; + } + + +}
\ No newline at end of file |