. * */ namespace OCA\News\Utility\ArticleEnhancer; use \OCA\News\Db\Item; require_once(__DIR__ . "/../../../classloader.php"); class TestEnhancer extends ArticleEnhancer { public function __construct($purifier, $fileFactory, $regexXPathPair, $timeout){ parent::__construct($purifier, $fileFactory, $regexXPathPair, $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, array( '/explosm.net\/comics/' => '//*[@id=\'maincontent\']/div[2]/div/img', '/explosm.net\/shorts/' => '//*[@id=\'maincontent\']/div/div' ), $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->body = '
nooo
'; $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('')) ->will($this->returnValue('')); $result = $this->testEnhancer->enhance($item); $this->assertEquals('', $result->getBody()); } public function testDoesModifiyAllArticlesThatMatch() { $file = new \stdClass; $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->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->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 = '

'; $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()); } }