From 00c6e040deec9c3998ab679dcb84fc46ae72d2ac Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Wed, 11 Sep 2013 20:31:58 +0200 Subject: removed google importer on the serverside, add importer for articles --- tests/unit/businesslayer/FeedBusinessLayerTest.php | 69 ++++---- tests/unit/controller/FeedControllerTest.php | 35 +++- tests/unit/db/FeedTest.php | 7 + tests/unit/db/ItemTest.php | 38 +++- tests/unit/utility/ImportParserTest.php | 191 --------------------- 5 files changed, 109 insertions(+), 231 deletions(-) delete mode 100644 tests/unit/utility/ImportParserTest.php (limited to 'tests') diff --git a/tests/unit/businesslayer/FeedBusinessLayerTest.php b/tests/unit/businesslayer/FeedBusinessLayerTest.php index 220b1c980..23bb98c55 100644 --- a/tests/unit/businesslayer/FeedBusinessLayerTest.php +++ b/tests/unit/businesslayer/FeedBusinessLayerTest.php @@ -34,7 +34,6 @@ use \OCA\News\Db\Feed; use \OCA\News\Db\Item; use \OCA\News\Utility\Fetcher; use \OCA\News\Utility\FetcherException; -use \OCA\News\Utility\ImportParser; class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { @@ -70,15 +69,12 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { $this->itemMapper = $this->getMockBuilder('\OCA\News\Db\ItemMapper') ->disableOriginalConstructor() ->getMock(); - $this->importParser = $this->getMockBuilder('\OCA\News\Utility\ImportParser') - ->disableOriginalConstructor() - ->getMock(); $this->enhancer = $this->getMockBuilder('\OCA\News\Utility\ArticleEnhancer\Enhancer') ->disableOriginalConstructor() ->getMock(); $this->feedBusinessLayer = new FeedBusinessLayer($this->feedMapper, $this->fetcher, $this->itemMapper, $this->api, - $timeFactory, $this->importParser, $this->autoPurgeMinimumInterval, + $timeFactory, $this->autoPurgeMinimumInterval, $this->enhancer); $this->user = 'jack'; $response = 'hi'; @@ -423,51 +419,58 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { } - public function testImportGoogleReaderJSON(){ - $url = 'http://owncloud/googlereader'; - $urlHash = md5($url); + public function testImportArticles(){ + $url = 'http://owncloud/nofeed'; $feed = new Feed(); $feed->setId(3); $feed->setUserId($this->user); - $feed->setUrlHash($urlHash); $feed->setUrl($url); - $feed->setTitle('Google Reader'); + $feed->setLink($url); + $feed->setTitle('Articles without feed'); $feed->setAdded($this->time); $feed->setFolderId(0); $feed->setPreventUpdate(true); - $items = array(new Item()); + $feeds = array($feed); + + $item = new Item(); + $item->setFeedId(3); + $item->setAuthor('john'); + $item->setGuid('s'); + $item->setTitle('hey'); + $item->setPubDate(333); + $item->setBody('come over'); + $item->setEnclosureMime('mime'); + $item->setEnclosureLink('lin'); + $item->setUnread(); + $item->setUnstarred(); + $item->setLastModified($this->time); + + $json = $item->toExport(array('feed3' => $feed)); + + $items = array($json); + + $this->feedMapper->expects($this->once()) + ->method('findAllFromUser') + ->with($this->equalTo($this->user)) + ->will($this->returnValue($feeds)); - $this->feedMapper->expects($this->at(0)) - ->method('findByUrlHash') - ->with($this->equalTo($urlHash), - $this->equalTo($this->user)) - ->will($this->throwException(new DoesNotExistException('hi'))); - $this->feedMapper->expects($this->at(1)) - ->method('insert') - ->will($this->returnValue($feed)); - $this->feedMapper->expects($this->at(2)) - ->method('findByUrlHash') - ->with($this->equalTo($urlHash), - $this->equalTo($this->user)) - ->will($this->returnValue($feed)); - $this->importParser->expects($this->once()) - ->method('parse') - ->will($this->returnValue($items)); $this->itemMapper->expects($this->once()) - ->method('findByGuidHash'); - $this->itemMapper->expects($this->never()) - ->method('insert'); + ->method('findByGuidHash') + ->will($this->throwException(new DoesNotExistException('yo'))); + $this->itemMapper->expects($this->once()) + ->method('insert') + ->with($this->equalTo($item)); - $result = $this->feedBusinessLayer->importGoogleReaderJSON(array(), $this->user); + $result = $this->feedBusinessLayer->importArticles($items, $this->user); - $this->assertEquals($feed, $result); + $this->assertEquals(null, $result); } - public function testImportGoogleReaderJSONFeedExists(){ + public function atestImportGoogleReaderJSONFeedExists(){ $url = 'http://owncloud/googlereader'; $urlHash = md5($url); diff --git a/tests/unit/controller/FeedControllerTest.php b/tests/unit/controller/FeedControllerTest.php index efbf90ea3..e3204517b 100644 --- a/tests/unit/controller/FeedControllerTest.php +++ b/tests/unit/controller/FeedControllerTest.php @@ -126,8 +126,8 @@ class FeedControllerTest extends ControllerTestUtility { } - public function testImportGoogleReaderAnnotations(){ - $this->assertFeedControllerAnnotations('importGoogleReader'); + public function testImportArticlesAnnotations(){ + $this->assertFeedControllerAnnotations('importArticles'); } public function testReadAnnotations(){ @@ -543,7 +543,7 @@ class FeedControllerTest extends ControllerTestUtility { } - public function testImportGoogleReader() { + public function testImportArticles() { $feed = new Feed(); $post = array( @@ -558,12 +558,37 @@ class FeedControllerTest extends ControllerTestUtility { ->method('getUserId') ->will($this->returnValue($this->user)); $this->feedBusinessLayer->expects($this->once()) - ->method('importGoogleReaderJSON') + ->method('importArticles') ->with($this->equalTo($post['json']), $this->equalTo($this->user)) ->will($this->returnValue($feed)); - $response = $this->controller->importGoogleReader(); + $response = $this->controller->importArticles(); + + $this->assertEquals($expected, $response->getParams()); + $this->assertTrue($response instanceof JSONResponse); + } + + + public function testImportArticlesCreatesNoAdditionalFeed() { + $feed = new Feed(); + + $post = array( + 'json' => 'the json' + ); + $expected = array(); + $this->controller = $this->getPostController($post); + + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->feedBusinessLayer->expects($this->once()) + ->method('importArticles') + ->with($this->equalTo($post['json']), + $this->equalTo($this->user)) + ->will($this->returnValue(null)); + + $response = $this->controller->importArticles(); $this->assertEquals($expected, $response->getParams()); $this->assertTrue($response instanceof JSONResponse); diff --git a/tests/unit/db/FeedTest.php b/tests/unit/db/FeedTest.php index b4192db1a..da9326dd4 100644 --- a/tests/unit/db/FeedTest.php +++ b/tests/unit/db/FeedTest.php @@ -62,6 +62,13 @@ class FeedTest extends \PHPUnit_Framework_TestCase { } + public function testSetLinkUpdatesHash() { + $feed = new Feed(); + $feed->setLink('http://test'); + $this->assertEquals(md5('http://test'), $feed->getUrlHash()); + } + + public function testSetXSSLink() { $feed = new Feed(); $feed->setLink('javascript:alert()'); diff --git a/tests/unit/db/ItemTest.php b/tests/unit/db/ItemTest.php index 511badeeb..b4360f273 100644 --- a/tests/unit/db/ItemTest.php +++ b/tests/unit/db/ItemTest.php @@ -117,7 +117,7 @@ class ItemTest extends \PHPUnit_Framework_TestCase { $item->setEnclosureLink('enclink'); $item->setFeedId(1); $item->setStatus(0); - $item->setUnread(); + $item->setRead(); $item->setStarred(); $item->setLastModified(321); @@ -136,13 +136,39 @@ class ItemTest extends \PHPUnit_Framework_TestCase { 'body' => 'body', 'enclosureMime' => 'audio/ogg', 'enclosureLink' => 'enclink', - 'unread' => true, + 'unread' => false, 'starred' => true, 'feedLink' => 'http://test' ), $item->toExport($feeds)); } + public function testFromImport() { + $item = new Item(); + $item->setGuid('guid'); + $item->setUrl('https://google'); + $item->setTitle('title'); + $item->setAuthor('author'); + $item->setPubDate(123); + $item->setBody('body'); + $item->setEnclosureMime('audio/ogg'); + $item->setEnclosureLink('enclink'); + $item->setFeedId(1); + $item->setUnread(); + $item->setStarred(); + + $feed = new Feed(); + $feed->setLink('http://test'); + $feeds = array( + "feed1" => $feed + ); + + $compareWith = Item::fromImport($item->toExport($feeds)); + $item->setFeedId(null); + $this->assertEquals($item, $compareWith); + } + + public function testSetAuthor(){ $item = new Item(); $item->setAuthor('my link'); @@ -172,4 +198,12 @@ class ItemTest extends \PHPUnit_Framework_TestCase { $this->assertEquals('magnet://link.com', $item->getUrl()); } + + public function testSetGuidUpdatesHash() { + $feed = new Item(); + $feed->setGuid('http://test'); + $this->assertEquals(md5('http://test'), $feed->getGuidHash()); + } + + } \ No newline at end of file diff --git a/tests/unit/utility/ImportParserTest.php b/tests/unit/utility/ImportParserTest.php deleted file mode 100644 index 82328139f..000000000 --- a/tests/unit/utility/ImportParserTest.php +++ /dev/null @@ -1,191 +0,0 @@ -. -* -*/ - -namespace OCA\News\Utility; - -use \OCA\News\Db\Item; - -require_once(__DIR__ . "/../../classloader.php"); - - -class ImportParserTest extends \OCA\AppFramework\Utility\TestUtility { - - private $parser; - private $time; - private $in; - private $purifier; - - protected function setUp(){ - $this->time = 222; - $this->purifier = $this->getMock('purifier', array('purify')); - $timeFactory = $this->getMockBuilder( - '\OCA\AppFramework\Utility\TimeFactory') - ->disableOriginalConstructor() - ->getMock(); - $timeFactory->expects($this->any()) - ->method('getTime') - ->will($this->returnValue($this->time)); - - $this->parser = new ImportParser($timeFactory, $this->purifier); - $this->in = array( - 'items' => array( - array( - 'id' => "tag:google.com,2005:reader/item/f9fd1dd3c19262e1", - 'title' => "[HorribleSubs] Mushibugyo - 01 [720p].mkv", - "published" => 1365415485, - "alternate" => array( array( - "href" => "http://www.nyaa.eu/?page=view&tid=421561", - "type" => "text/html" - )), - "summary" => array( - "content" => "1596 seeder(s), 775 leecher(s), 8005 download(s) - 316.7 MiB - Trusted" - ) - ) - ) - ); - } - - - public function testImportParserReturnsEmptyArrayIfNoInput(){ - $result = $this->parser->parse(array()); - - $this->assertEquals($result, array()); - } - - - public function testParsesItems() { - $body = $this->in['items'][0]['summary']['content']; - $this->purifier->expects($this->once()) - ->method('purify') - ->with($this->equalTo($body)) - ->will($this->returnValue($body)); - - $result = $this->parser->parse($this->in); - - $out = new Item(); - $out->setTitle($this->in['items'][0]['title']); - $out->setPubDate($this->in['items'][0]['published']); - $out->setBody($body); - $out->setUrl($this->in['items'][0]['alternate'][0]['href']); - $out->setGuid($this->in['items'][0]['id']); - $out->setGuidHash(md5($this->in['items'][0]['id'])); - $out->setStatus(0); - $out->setUnread(); - $out->setStarred(); - - $this->assertEquals(array($out), $result); - } - - - public function testParsesItemsNoSummary() { - $this->in['items'][0]['content']['content'] = 'hi'; - $body = $this->in['items'][0]['content']['content']; - - $this->purifier->expects($this->once()) - ->method('purify') - ->with($this->equalTo($body)) - ->will($this->returnValue($body)); - - unset($this->in['items'][0]['summary']); - $result = $this->parser->parse($this->in); - - $out = new Item(); - $out->setTitle($this->in['items'][0]['title']); - $out->setPubDate($this->in['items'][0]['published']); - $out->setBody($body); - $out->setUrl($this->in['items'][0]['alternate'][0]['href']); - $out->setGuid($this->in['items'][0]['id']); - $out->setGuidHash(md5($this->in['items'][0]['id'])); - $out->setStatus(0); - $out->setUnread(); - $out->setStarred(); - - $this->assertEquals(array($out), $result); - } - - - public function testDontAddIfNoTitleAndUrlAndGuid() { - $in = array( - 'items' => array( - array( - "published" => 1365415485, - - "summary" => array( - "content" => "1596 seeder(s), 775 leecher(s), 8005 download(s) - 316.7 MiB - Trusted" - ) - ) - ) - ); - - $result = $this->parser->parse($in); - $this->assertEquals(array(), $result); - } - - public function testParsesItemsNoPubDate() { - $body = $this->in['items'][0]['summary']['content']; - $this->purifier->expects($this->once()) - ->method('purify') - ->with($this->equalTo($body)) - ->will($this->returnValue($body)); - - unset($this->in['items'][0]['published']); - $result = $this->parser->parse($this->in); - - $out = new Item(); - $out->setTitle($this->in['items'][0]['title']); - $out->setPubDate($this->time); - $out->setBody($body); - $out->setUrl($this->in['items'][0]['alternate'][0]['href']); - $out->setGuid($this->in['items'][0]['id']); - $out->setGuidHash(md5($this->in['items'][0]['id'])); - $out->setStatus(0); - $out->setUnread(); - $out->setStarred(); - - $this->assertEquals(array($out), $result); - } - - - public function testParsesReverse() { - $this->in['items'][1]= array( - 'id' => "tag", - 'title' => "[HorribleSubs] Mushibugyo - 01 [720p].mkv", - "published" => 1365415485, - "alternate" => array( array( - "href" => "http://www.nyaa.eu/?page=view&tid=421561", - "type" => "text/html" - )), - "summary" => array( - "content" => "1596 seeder(s), 775 leecher(s), 8005 download(s) - 316.7 MiB - Trusted" - ) - ); - - $result = $this->parser->parse($this->in); - - $this->assertEquals('tag', $result[0]->getGuid()); - $this->assertEquals('tag:google.com,2005:reader/item/f9fd1dd3c19262e1', - $result[1]->getGuid()); - } -} -- cgit v1.2.3