diff options
author | Bernhard Posselt <nukeawhale@gmail.com> | 2013-08-28 17:26:38 +0200 |
---|---|---|
committer | Bernhard Posselt <nukeawhale@gmail.com> | 2013-08-28 17:26:38 +0200 |
commit | a9eb72911b6f022da645dc08cf8c0f4b1702d1e1 (patch) | |
tree | 4e16f84cd4a8d0214fc5a445f4817bc4a3a93bf0 /tests/unit | |
parent | 998b3c29438b09e50d2c56ed982b48df8038a910 (diff) |
add enhancers for articles, fix #14
Diffstat (limited to 'tests/unit')
4 files changed, 364 insertions, 140 deletions
diff --git a/tests/unit/businesslayer/FeedBusinessLayerTest.php b/tests/unit/businesslayer/FeedBusinessLayerTest.php index 0a923cace..7a4cf24e6 100644 --- a/tests/unit/businesslayer/FeedBusinessLayerTest.php +++ b/tests/unit/businesslayer/FeedBusinessLayerTest.php @@ -48,6 +48,7 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { private $time; private $importParser; private $autoPurgeMinimumInterval; + private $enhancer; protected function setUp(){ $this->api = $this->getAPIMock(); @@ -72,9 +73,13 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { $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->importParser, $this->autoPurgeMinimumInterval, + $this->enhancer); $this->user = 'jack'; $response = 'hi'; } @@ -146,6 +151,10 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { $this->equalTo($item2->getFeedId()), $this->equalTo($this->user)) ->will($this->throwException($ex)); + $this->enhancer->expects($this->at(0)) + ->method('enhance') + ->with($this->equalTo($return[1][1])) + ->will($this->returnValue($return[1][1])); $this->itemMapper->expects($this->at(1)) ->method('insert') ->with($this->equalTo($return[1][1])); @@ -156,6 +165,10 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { $this->equalTo($item1->getFeedId()), $this->equalTo($this->user)) ->will($this->throwException($ex)); + $this->enhancer->expects($this->at(1)) + ->method('enhance') + ->with($this->equalTo($return[1][0])) + ->will($this->returnValue($return[1][0])); $this->itemMapper->expects($this->at(3)) ->method('insert') ->with($this->equalTo($return[1][0])); @@ -201,6 +214,10 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { $this->equalTo($item2->getFeedId()), $this->equalTo($this->user)) ->will($this->throwException($ex)); + $this->enhancer->expects($this->at(0)) + ->method('enhance') + ->with($this->equalTo($return[1][1])) + ->will($this->returnValue($return[1][1])); $this->itemMapper->expects($this->at(1)) ->method('insert') ->with($this->equalTo($return[1][1])); @@ -249,6 +266,10 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { $this->equalTo($items[0]->getFeedId()), $this->equalTo($this->user)) ->will($this->throwException($ex)); + $this->enhancer->expects($this->at(0)) + ->method('enhance') + ->with($this->equalTo($items[0])) + ->will($this->returnValue($items[0])); $this->itemMapper->expects($this->once()) ->method('insert') ->with($this->equalTo($items[0])); @@ -263,145 +284,7 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { $this->assertEquals($return, $feed); } - - public function testUpdateUpdatesEntryNotWhenPubDateSame(){ - $feed = new Feed(); - $feed->setId(3); - $feed->getUrl('test'); - - $item = new Item(); - $item->setGuidHash(md5('hi')); - $item->setPubDate(3333); - $items = array( - $item - ); - - $fetchReturn = array($feed, $items); - - $this->feedMapper->expects($this->at(0)) - ->method('find') - ->with($this->equalTo($feed->getId()), - $this->equalTo($this->user)) - ->will($this->returnValue($feed)); - $this->fetcher->expects($this->once()) - ->method('fetch') - ->will($this->returnValue($fetchReturn)); - $this->itemMapper->expects($this->once()) - ->method('findByGuidHash') - ->with($this->equalTo($item->getGuidHash()), - $this->equalTo($feed->getId()), - $this->equalTo($this->user)) - ->will($this->returnValue($item)); - $this->itemMapper->expects($this->never()) - ->method('insert'); - $this->itemMapper->expects($this->never()) - ->method('delete'); - - $this->feedMapper->expects($this->at(1)) - ->method('find') - ->with($feed->getId(), $this->user) - ->will($this->returnValue($feed)); - - $return = $this->feedBusinessLayer->update($feed->getId(), $this->user); - - $this->assertEquals($return, $feed); - } - - - public function testUpdateUpdatesEntryNotWhenPubDateUnkown(){ - $feed = new Feed(); - $feed->setId(3); - $feed->getUrl('test'); - - $item = new Item(); - $item->setGuidHash(md5('hi')); - $item->setPubDate(false); - $items = array( - $item - ); - - $item2 = new Item(); - $item2->setPubDate(0); - - $fetchReturn = array($feed, $items); - - $this->feedMapper->expects($this->at(0)) - ->method('find') - ->with($this->equalTo($feed->getId()), - $this->equalTo($this->user)) - ->will($this->returnValue($feed)); - $this->fetcher->expects($this->once()) - ->method('fetch') - ->will($this->returnValue($fetchReturn)); - $this->itemMapper->expects($this->once()) - ->method('findByGuidHash') - ->with($this->equalTo($item->getGuidHash()), - $this->equalTo($feed->getId()), - $this->equalTo($this->user)) - ->will($this->returnValue($item2)); - $this->itemMapper->expects($this->never()) - ->method('insert'); - $this->itemMapper->expects($this->never()) - ->method('delete'); - - $this->feedMapper->expects($this->at(1)) - ->method('find') - ->with($feed->getId(), $this->user) - ->will($this->returnValue($feed)); - - $return = $this->feedBusinessLayer->update($feed->getId(), $this->user); - - $this->assertEquals($return, $feed); - } - - public function testUpdateUpdatesEntryNotWhenNoPubDate(){ - $feed = new Feed(); - $feed->setId(3); - $feed->getUrl('test'); - - $item = new Item(); - $item->setGuidHash(md5('hi')); - $item->setPubDate(null); - $items = array( - $item - ); - - $item2 = new Item(); - $item2->setPubDate(null); - - $fetchReturn = array($feed, $items); - - $this->feedMapper->expects($this->at(0)) - ->method('find') - ->with($this->equalTo($feed->getId()), - $this->equalTo($this->user)) - ->will($this->returnValue($feed)); - $this->fetcher->expects($this->once()) - ->method('fetch') - ->will($this->returnValue($fetchReturn)); - $this->itemMapper->expects($this->once()) - ->method('findByGuidHash') - ->with($this->equalTo($item->getGuidHash()), - $this->equalTo($feed->getId()), - $this->equalTo($this->user)) - ->will($this->returnValue($item2)); - $this->itemMapper->expects($this->never()) - ->method('insert'); - $this->itemMapper->expects($this->never()) - ->method('delete'); - - $this->feedMapper->expects($this->at(1)) - ->method('find') - ->with($feed->getId(), $this->user) - ->will($this->returnValue($feed)); - - $return = $this->feedBusinessLayer->update($feed->getId(), $this->user); - - $this->assertEquals($return, $feed); - } - - - public function testCreateUpdateFails(){ + public function testUpdateFails(){ $feed = new Feed(); $feed->setId(3); $feed->getUrl('test'); diff --git a/tests/unit/utility/articleenhancer/ArticleEnhancerTest.php b/tests/unit/utility/articleenhancer/ArticleEnhancerTest.php new file mode 100644 index 000000000..8d507c0f8 --- /dev/null +++ b/tests/unit/utility/articleenhancer/ArticleEnhancerTest.php @@ -0,0 +1,178 @@ +<?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\Utility\ArticleEnhancer; + +use \OCA\News\Db\Item; + +require_once(__DIR__ . "/../../../classloader.php"); + + +class TestEnhancer extends ArticleEnhancer { + public function __construct($purifier, $fileFactory, $articleRegex, + $articleXPATH, $timeout){ + parent::__construct($purifier, $fileFactory, $articleRegex, + $articleXPATH, $timeout); + } +} + + +class ArticleEnhancerTest 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 TestEnhancer( + $this->purifier, + $this->fileFactory, + '/explosm.net\/comics/', + '//*[@id=\'maincontent\']/div[2]/img', + $this->timeout + ); + } + + + public function testCanHandle() { + $item = new Item(); + $item->setUrl('http://explosm.net/comics'); + $this->assertTrue($this->testEnhancer->canHandle($item)); + } + + + public function testDoesModifiyArticlesThatMatch() { + $file = new \stdClass; + $file->body = '<html> + <body> + <div id="maincontent"> + <div>nooo</div> + <div><img src="hiho"></div> + </div> + </body> + </html>'; + $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('<img src="hiho">')) + ->will($this->returnValue('<img src="hiho">')); + + $result = $this->testEnhancer->enhance($item); + $this->assertEquals('<img src="hiho">', $result->getBody()); + } + + + public function testModificationHandlesEmptyResults() { + $file = new \stdClass; + $file->body = '<html> + <body> + <div id="maincontent"> + </div> + </body> + </html>'; + $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->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->body = '<html/><p> + <body> + <div id="maincontent"> + </div> + </body> + </html>'; + $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()); + } + + +}
\ No newline at end of file diff --git a/tests/unit/utility/articleenhancer/DefaultEnhancerTest.php b/tests/unit/utility/articleenhancer/DefaultEnhancerTest.php new file mode 100644 index 000000000..901428616 --- /dev/null +++ b/tests/unit/utility/articleenhancer/DefaultEnhancerTest.php @@ -0,0 +1,54 @@ +<?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\Utility\ArticleEnhancer; + +use \OCA\News\Db\Item; + +require_once(__DIR__ . "/../../../classloader.php"); + + +class DefaultEnhancerTest extends \OCA\AppFramework\Utility\TestUtility { + + private $testEnhancer; + + protected function setUp() { + $this->testEnhancer = new DefaultEnhancer(); + } + + + public function testCanHandle() { + $item = new Item(); + $this->assertTrue($this->testEnhancer->canHandle($item)); + } + + + public function testEnhance() { + $item = new Item(); + $this->assertEquals($item, $this->testEnhancer->enhance($item)); + } + + +}
\ No newline at end of file diff --git a/tests/unit/utility/articleenhancer/EnhancerTest.php b/tests/unit/utility/articleenhancer/EnhancerTest.php new file mode 100644 index 000000000..559722e60 --- /dev/null +++ b/tests/unit/utility/articleenhancer/EnhancerTest.php @@ -0,0 +1,109 @@ +<?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\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->articleEnhancer2 = $this->getMockBuilder( + '\OCA\News\Utility\ArticleEnhancer\ArticleEnhancer') + ->disableOriginalConstructor() + ->getMock(); + } + + + public function testFetch(){ + $item = new Item(); + $item->setUrl('hi'); + + $this->articleEnhancer->expects($this->once()) + ->method('canHandle') + ->with($this->equalTo($item)) + ->will($this->returnValue(true)); + $this->enhancer->registerEnhancer($this->articleEnhancer); + + $this->enhancer->enhance($item); + } + + + public function testMultipleFetchers(){ + $item = new Item(); + $item->setUrl('hi'); + $this->articleEnhancer->expects($this->once()) + ->method('canHandle') + ->with($this->equalTo($item)) + ->will($this->returnValue(false)); + $this->articleEnhancer2->expects($this->once()) + ->method('canHandle') + ->with($this->equalTo($item)) + ->will($this->returnValue(true)); + + $this->enhancer->registerEnhancer($this->articleEnhancer); + $this->enhancer->registerEnhancer($this->articleEnhancer2); + + $this->enhancer->enhance($item); + } + + + public function testMultipleFetchersOnlyOneShouldHandle(){ + $item = new Item(); + $item->setUrl('hi'); + $return = 'zeas'; + $this->articleEnhancer->expects($this->once()) + ->method('canHandle') + ->with($this->equalTo($item)) + ->will($this->returnValue(true)); + $this->articleEnhancer->expects($this->once()) + ->method('enhance') + ->with($this->equalTo($item)) + ->will($this->returnValue($return)); + $this->articleEnhancer2->expects($this->never()) + ->method('canHandle'); + + $this->enhancer->registerEnhancer($this->articleEnhancer); + $this->enhancer->registerEnhancer($this->articleEnhancer2); + + $result = $this->enhancer->enhance($item); + + $this->assertEquals($return, $result); + } + + +}
\ No newline at end of file |