* @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 $client;
private $clientFactory;
protected function setUp() {
$this->timeout = 30;
$this->clientFactory = $this
->getMockBuilder('\OCA\News\Utility\PicoFeedClientFactory')
->disableOriginalConstructor()
->getMock();
$this->client = $this
->getMockBuilder('\PicoFeed\Client\Client')
->disableOriginalConstructor()
->getMock();
$this->testEnhancer = new XPathArticleEnhancer(
$this->clientFactory,
[
'/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->userAgent = 'Mozilla/5.0 AppleWebKit';
}
private function setUpFile($body, $encoding, $url) {
$this->clientFactory->expects($this->once())
->method('build')
->will($this->returnValue($this->client));
$this->client->expects($this->once())
->method('execute')
->with($this->equalTo($url));
$this->client->expects($this->once())
->method('setUserAgent')
->with($this->equalTo($this->userAgent));
$this->client->expects($this->once())
->method('getContent')
->will($this->returnValue($body));
$this->client->expects($this->once())
->method('getEncoding')
->will($this->returnValue($encoding));
}
public function testDoesNotModifiyNotMatchingResults() {
$item = new Item();
$item->setUrl('http://explosm.net');
$this->assertEquals($item, $this->testEnhancer->enhance($item));
}
public function testDoesModifiyArticlesThatMatch() {
$encoding = 'utf-8';
$body = '
';
$item = new Item();
$item->setUrl('https://www.explosm.net/comics/312');
$item->setBody('Hello thar');
$this->setUpFile($body, $encoding, $item->getUrl());
$result = $this->testEnhancer->enhance($item);
$this->assertEquals('hiho
', $result->getBody());
}
public function testDoesModifiyAllArticlesThatMatch() {
$encoding = 'utf-8';
$body = '
';
$item = new Item();
$item->setUrl('https://www.explosm.net/shorts/312');
$item->setBody('Hello thar');
$this->setUpFile($body, $encoding, $item->getUrl());
$result = $this->testEnhancer->enhance($item);
$this->assertEquals('',
$result->getBody());
}
public function testModificationHandlesEmptyResults() {
$encoding = 'utf-8';
$body = '
';
$item = new Item();
$item->setUrl('https://www.explosm.net/comics/312');
$item->setBody('Hello thar');
$this->setUpFile($body, $encoding, $item->getUrl());
$result = $this->testEnhancer->enhance($item);
$this->assertEquals('Hello thar', $result->getBody());
}
public function testModificationDoesNotBreakOnEmptyDom() {
$encoding = 'utf-8';
$body = '';
$item = new Item();
$item->setUrl('https://www.explosm.net/comics/312');
$item->setBody('Hello thar');
$this->setUpFile($body, $encoding, $item->getUrl());
$result = $this->testEnhancer->enhance($item);
$this->assertEquals('Hello thar', $result->getBody());
}
public function testModificationDoesNotBreakOnBrokenDom() {
$encoding = 'utf-8';
$body = '
';
$item = new Item();
$item->setUrl('https://www.explosm.net/comics/312');
$item->setBody('Hello thar');
$this->setUpFile($body, $encoding, $item->getUrl());
$result = $this->testEnhancer->enhance($item);
$this->assertEquals('Hello thar', $result->getBody());
}
public function testTransformRelativeUrls() {
$encoding = 'utf-8';
$body = '
link
link2
';
$item = new Item();
$item->setUrl('https://www.explosm.net/all/312');
$item->setBody('Hello thar');
$this->setUpFile($body, $encoding, $item->getUrl());
$result = $this->testEnhancer->enhance($item);
$this->assertEquals('', $result->getBody());
}
public function testTransformRelativeUrlSpecials() {
$encoding = 'utf-8';
$body = '
';
$item = new Item();
$item->setUrl('https://username:secret@www.explosm.net/all/312');
$item->setBody('Hello thar');
$this->setUpFile($body, $encoding, $item->getUrl());
$result = $this->testEnhancer->enhance($item);
$this->assertEquals(
'',
$result->getBody());
}
public function testDontTransformAbsoluteUrlsAndMails() {
$encoding = 'utf-8';
$body = '
mail
';
$item = new Item();
$item->setUrl('https://www.explosm.net/all/312');
$item->setBody('Hello thar');
$this->setUpFile($body, $encoding, $item->getUrl());
$result = $this->testEnhancer->enhance($item);
$this->assertEquals(
'',
$result->getBody()
);
}
}