* @author Bernhard Posselt * @copyright Alessandro Cosentino 2012 * @copyright Bernhard Posselt 2012, 2014 */ namespace OCA\News\ArticleEnhancer; use \OCA\News\Db\Item; class XPathArticleEnhancerTest extends \PHPUnit_Framework_TestCase { private $testEnhancer; private $fileFactory; private $timeout; private $redirects; private $headers; private $userAgent; private $proxyHost; private $proxyPort; private $proxyAuth; protected function setUp() { $this->timeout = 30; $this->fileFactory = $this->getMockBuilder('\OCA\News\Utility\SimplePieAPIFactory') ->disableOriginalConstructor() ->getMock(); $this->proxyHost = 'test'; $this->proxyPort = 3; $this->proxyAuth = 'hi'; $this->config = $this->getMockBuilder( '\OCA\News\Config\Config') ->disableOriginalConstructor() ->getMock(); $this->config->expects($this->any()) ->method('getProxyHost') ->will($this->returnValue('')); $this->config->expects($this->any()) ->method('getProxyAuth') ->will($this->returnValue($this->proxyAuth)); $this->config->expects($this->any()) ->method('getProxyPort') ->will($this->returnValue($this->proxyPort)); $this->config->expects($this->any()) ->method('getFeedFetcherTimeout') ->will($this->returnValue($this->timeout)); $this->testEnhancer = new XPathArticleEnhancer( $this->fileFactory, [ '/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->config ); $this->redirects = 5; $this->headers = null; $this->userAgent = 'Mozilla/5.0 AppleWebKit'; } public function testXPathUsesNoProxy() { $file = new \stdClass; $file->headers = ["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), $this->equalTo($this->redirects), $this->equalTo($this->headers), $this->equalTo($this->userAgent), $this->equalTo(false)) ->will($this->returnValue($file)); $result = $this->testEnhancer->enhance($item); $this->assertEquals('Hello thar', $result->getBody()); } 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 = ["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), $this->equalTo($this->redirects), $this->equalTo($this->headers), $this->equalTo($this->userAgent)) ->will($this->returnValue($file)); $result = $this->testEnhancer->enhance($item); $this->assertEquals('
hiho
', $result->getBody()); } public function testDoesModifiyAllArticlesThatMatch() { $file = new \stdClass; $file->headers = ["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), $this->equalTo($this->redirects), $this->equalTo($this->headers), $this->equalTo($this->userAgent)) ->will($this->returnValue($file)); $result = $this->testEnhancer->enhance($item); $this->assertEquals('
hiho
rawr
', $result->getBody()); } public function testModificationHandlesEmptyResults() { $file = new \stdClass; $file->headers = ["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), $this->equalTo($this->redirects), $this->equalTo($this->headers), $this->equalTo($this->userAgent)) ->will($this->returnValue($file)); $result = $this->testEnhancer->enhance($item); $this->assertEquals('Hello thar', $result->getBody()); } public function testModificationDoesNotBreakOnEmptyDom() { $file = new \stdClass; $file->headers = ["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), $this->equalTo($this->redirects), $this->equalTo($this->headers), $this->equalTo($this->userAgent)) ->will($this->returnValue($file)); $result = $this->testEnhancer->enhance($item); $this->assertEquals('Hello thar', $result->getBody()); } public function testModificationDoesNotBreakOnBrokenDom() { $file = new \stdClass; $file->headers = ["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), $this->equalTo($this->redirects), $this->equalTo($this->headers), $this->equalTo($this->userAgent)) ->will($this->returnValue($file)); $result = $this->testEnhancer->enhance($item); $this->assertEquals('Hello thar', $result->getBody()); } public function testTransformRelativeUrls() { $file = new \stdClass; $file->headers = ["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), $this->equalTo($this->redirects), $this->equalTo($this->headers), $this->equalTo($this->userAgent)) ->will($this->returnValue($file)); $result = $this->testEnhancer->enhance($item); $this->assertEquals('
' . 'link' . 'link2' . '' . '
', $result->getBody()); } public function testTransformRelativeUrlSpecials() { $file = new \stdClass; $file->headers = ["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), $this->equalTo($this->redirects), $this->equalTo($this->headers), $this->equalTo($this->userAgent)) ->will($this->returnValue($file)); $result = $this->testEnhancer->enhance($item); $this->assertEquals( '
', $result->getBody()); } public function testDontTransformAbsoluteUrlsAndMails() { $file = new \stdClass; $file->headers = ["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), $this->equalTo($this->redirects), $this->equalTo($this->headers), $this->equalTo($this->userAgent)) ->will($this->returnValue($file)); $result = $this->testEnhancer->enhance($item); $this->assertEquals( '
' . '' . 'mail' . '
', $result->getBody() ); } }