Skip to content

Commit

Permalink
Fix xkcd commics
Browse files Browse the repository at this point in the history
  • Loading branch information
SMillerDev committed Mar 26, 2019
1 parent 08e44b8 commit b37e237
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 34 deletions.
34 changes: 18 additions & 16 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 15 additions & 14 deletions lib/Fetcher/FeedFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,9 @@ protected function buildItem(ItemInterface $parsedItem, FeedInterface $parsedFee
$pubDT = $lastmodified;
}

$item->setPubDate(
$pubDT->getTimestamp()
);
$item->setPubDate($pubDT->getTimestamp());

$item->setLastModified(
$lastmodified->getTimestamp()
);
$item->setLastModified($lastmodified->getTimestamp());
$item->setRtl($this->determineRtl($parsedFeed));

// unescape content because angularjs helps against XSS
Expand All @@ -208,14 +204,19 @@ protected function buildItem(ItemInterface $parsedItem, FeedInterface $parsedFee
'HTML-ENTITIES',
mb_detect_encoding($body)
);
libxml_use_internal_errors(true);
$data = simplexml_load_string(
"<?xml version=\"1.0\"?><item>$body</item>",
SimpleXMLElement::class,
LIBXML_NOCDATA
);
libxml_clear_errors();
$body = ($data === false) ? $body : (string) $data;
if (strpos($body, 'CDATA') !== false) {
libxml_use_internal_errors(true);
$data = simplexml_load_string(
"<?xml version=\"1.0\"?><item>$body</item>",
SimpleXMLElement::class,
LIBXML_NOCDATA
);
if ($data !== false && libxml_get_last_error() === false) {
$body = (string) $data;
}
libxml_clear_errors();
}

$item->setBody($body);

if ($parsedItem->hasMedia()) {
Expand Down
58 changes: 54 additions & 4 deletions tests/Unit/Fetcher/FeedFetcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,11 @@
use FeedIo\Feed\Item\MediaInterface;
use FeedIo\Feed\ItemInterface;
use FeedIo\FeedInterface;
use Favicon\Favicon;
use OC\L10N\L10N;
use OCA\AdminAudit\Actions\Auth;
use \OCA\News\Db\Feed;
use \OCA\News\Db\Item;
use OCA\News\Fetcher\FeedFetcher;
use OCA\News\Utility\PsrLogger;
use OCA\News\Utility\Time;
use OCP\IL10N;

use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -243,6 +239,60 @@ public function testFetchIfNoModifiedExists()
$this->assertEquals([$feed, [$item]], $result);
}

/**
* Return body options
* @return array
*/
public function feedBodyProvider()
{
return [
[
'<![CDATA[let the bodies hit the floor <a href="test">test</a>]]>',
'let the bodies hit the floor <a href="test">test</a>'
],
[
'let the bodies hit the floor <a href="test">test</a>',
'let the bodies hit the floor <a href="test">test</a>'
],
[
'let the bodies hit the floor "test" test',
'let the bodies hit the floor "test" test'
],
[
'<img src="https://imgs.xkcd.com/google_trends_maps.png" title="It\'s early 2020. The entire country is gripped with Marco Rubio" />',
'<img src="https://imgs.xkcd.com/google_trends_maps.png" title="It\'s early 2020. The entire country is gripped with Marco Rubio" />'
],
];
}

/**
* Test if body is set correctly.
*
* @dataProvider feedBodyProvider
*
* @param string $body The body before parsing.
* @param string $parsed_body The body after parsing.
*/
public function testFetchWithFeedContent($body, $parsed_body)
{
$bodyBackup = $this->body;
$parsedBackup = $this->parsed_body;

$this->body = $body;
$this->parsed_body = $parsed_body;

$this->setUpReader($this->url, null, true);
$item = $this->createItem();
$feed = $this->createFeed();
$this->mockIterator($this->feed_mock, [$this->item_mock]);
$result = $this->fetcher->fetch($this->url, false, '0', null, null);

$this->assertEquals([$feed, [$item]], $result);

$this->body = $bodyBackup;
$this->parsed_body = $parsedBackup;
}

/**
* Test if the fetch function fetches a feed.
*/
Expand Down

0 comments on commit b37e237

Please sign in to comment.