. * */ namespace OCA\News\ArticleEnhancer; use \OCA\News\Db\Item; require_once(__DIR__ . "/../../classloader.php"); class XPathArticleEnhancerTest 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 XPathArticleEnhancer( $this->purifier, $this->fileFactory, array( '/explosm.net\/comics/' => '//*[@id=\'maincontent\']/div[2]/div/span', '/explosm.net\/shorts/' => '//*[@id=\'maincontent\']/div/div', '/explosm.net\/all/' => '//body/*', '/themerepublic.net/' => '//*[@class=\'post hentry\']' ), $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->headers = array("content-type"=>"text/html; charset=utf-8"); $file->body = '
nooo
hiho
'; $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('hiho')) ->will($this->returnValue('hiho')); $result = $this->testEnhancer->enhance($item); $this->assertEquals('hiho', $result->getBody()); } public function testDoesModifiyAllArticlesThatMatch() { $file = new \stdClass; $file->headers = array("content-type"=>"text/html; charset=utf-8"); $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->headers = array("content-type"=>"text/html; charset=utf-8"); $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->headers = array("content-type"=>"text/html; charset=utf-8"); $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->headers = array("content-type"=>"text/html; charset=utf-8"); $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 testTransformRelativeUrls() { $file = new \stdClass; $file->headers = array("content-type"=>"text/html; charset=utf-8"); $file->body = ' link link2 '; $item = new Item(); $item->setUrl('https://www.explosm.net/all/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('linklink2')) ->will($this->returnValue('linklink2')); $result = $this->testEnhancer->enhance($item); $this->assertEquals('linklink2', $result->getBody()); } public function testTransformRelativeUrlSpecials() { $file = new \stdClass; $file->headers = array("content-type"=>"text/html; charset=utf-8"); $file->body = ' '; $item = new Item(); $item->setUrl('https://username:secret@www.explosm.net/all/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 testDontTransformAbsoluteUrlsAndMails() { $file = new \stdClass; $file->headers = array("content-type"=>"text/html; charset=utf-8"); $file->body = ' mail '; $item = new Item(); $item->setUrl('https://www.explosm.net/all/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('mail')) ->will($this->returnValue('mail')); $result = $this->testEnhancer->enhance($item); $this->assertEquals('mail', $result->getBody()); } }