.
*
*/
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 = '
';
$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 = '
';
$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('Hello thar', $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('Hello thar', $result->getBody());
}
public function testModificationDoesNotBreakOnBrokenDom() {
$file = new \stdClass;
$file->headers = array("content-type"=>"text/html; charset=utf-8");
$file->body = '