From b37e237c7a8618a043bbf56b048f5604a0f854e8 Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Mon, 25 Mar 2019 22:53:38 +0100 Subject: Fix xkcd commics --- composer.lock | 34 ++++++++++---------- lib/Fetcher/FeedFetcher.php | 29 +++++++++-------- tests/Unit/Fetcher/FeedFetcherTest.php | 58 +++++++++++++++++++++++++++++++--- 3 files changed, 87 insertions(+), 34 deletions(-) diff --git a/composer.lock b/composer.lock index 9d0c34a1c..578be3693 100644 --- a/composer.lock +++ b/composer.lock @@ -328,27 +328,29 @@ "packages-dev": [ { "name": "doctrine/instantiator", - "version": "1.1.0", + "version": "1.2.0", "source": { "type": "git", "url": "https://github.com/doctrine/instantiator.git", - "reference": "185b8868aa9bf7159f5f953ed5afb2d7fcdc3bda" + "reference": "a2c590166b2133a4633738648b6b064edae0814a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/doctrine/instantiator/zipball/185b8868aa9bf7159f5f953ed5afb2d7fcdc3bda", - "reference": "185b8868aa9bf7159f5f953ed5afb2d7fcdc3bda", + "url": "https://api.github.com/repos/doctrine/instantiator/zipball/a2c590166b2133a4633738648b6b064edae0814a", + "reference": "a2c590166b2133a4633738648b6b064edae0814a", "shasum": "" }, "require": { "php": "^7.1" }, "require-dev": { - "athletic/athletic": "~0.1.8", + "doctrine/coding-standard": "^6.0", "ext-pdo": "*", "ext-phar": "*", - "phpunit/phpunit": "^6.2.3", - "squizlabs/php_codesniffer": "^3.0.2" + "phpbench/phpbench": "^0.13", + "phpstan/phpstan-phpunit": "^0.11", + "phpstan/phpstan-shim": "^0.11", + "phpunit/phpunit": "^7.0" }, "type": "library", "extra": { @@ -373,12 +375,12 @@ } ], "description": "A small, lightweight utility to instantiate objects in PHP without invoking their constructors", - "homepage": "https://github.com/doctrine/instantiator", + "homepage": "https://www.doctrine-project.org/projects/instantiator.html", "keywords": [ "constructor", "instantiate" ], - "time": "2017-07-22T11:58:36+00:00" + "time": "2019-03-17T17:37:11+00:00" }, { "name": "myclabs/deep-copy", @@ -1750,16 +1752,16 @@ }, { "name": "symfony/polyfill-ctype", - "version": "v1.10.0", + "version": "v1.11.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-ctype.git", - "reference": "e3d826245268269cd66f8326bd8bc066687b4a19" + "reference": "82ebae02209c21113908c229e9883c419720738a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/e3d826245268269cd66f8326bd8bc066687b4a19", - "reference": "e3d826245268269cd66f8326bd8bc066687b4a19", + "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/82ebae02209c21113908c229e9883c419720738a", + "reference": "82ebae02209c21113908c229e9883c419720738a", "shasum": "" }, "require": { @@ -1771,7 +1773,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.9-dev" + "dev-master": "1.11-dev" } }, "autoload": { @@ -1793,7 +1795,7 @@ }, { "name": "Gert de Pagter", - "email": "BackEndTea@gmail.com" + "email": "backendtea@gmail.com" } ], "description": "Symfony polyfill for ctype functions", @@ -1804,7 +1806,7 @@ "polyfill", "portable" ], - "time": "2018-08-06T14:22:27+00:00" + "time": "2019-02-06T07:57:58+00:00" }, { "name": "theseer/tokenizer", diff --git a/lib/Fetcher/FeedFetcher.php b/lib/Fetcher/FeedFetcher.php index 7b78ae67d..f433ac224 100755 --- a/lib/Fetcher/FeedFetcher.php +++ b/lib/Fetcher/FeedFetcher.php @@ -185,13 +185,9 @@ class FeedFetcher implements IFeedFetcher $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 @@ -208,14 +204,19 @@ class FeedFetcher implements IFeedFetcher 'HTML-ENTITIES', mb_detect_encoding($body) ); - libxml_use_internal_errors(true); - $data = simplexml_load_string( - "$body", - 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( + "$body", + SimpleXMLElement::class, + LIBXML_NOCDATA + ); + if ($data !== false && libxml_get_last_error() === false) { + $body = (string) $data; + } + libxml_clear_errors(); + } + $item->setBody($body); if ($parsedItem->hasMedia()) { diff --git a/tests/Unit/Fetcher/FeedFetcherTest.php b/tests/Unit/Fetcher/FeedFetcherTest.php index 5f3420810..72badf093 100644 --- a/tests/Unit/Fetcher/FeedFetcherTest.php +++ b/tests/Unit/Fetcher/FeedFetcherTest.php @@ -18,15 +18,11 @@ use FeedIo\Feed\Item\Author; 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; @@ -243,6 +239,60 @@ class FeedFetcherTest extends TestCase $this->assertEquals([$feed, [$item]], $result); } + /** + * Return body options + * @return array + */ + public function feedBodyProvider() + { + return [ + [ + 'test]]>', + 'let the bodies hit the floor test' + ], + [ + 'let the bodies hit the floor test', + 'let the bodies hit the floor test' + ], + [ + 'let the bodies hit the floor "test" test', + 'let the bodies hit the floor "test" test' + ], + [ + '', + '' + ], + ]; + } + + /** + * 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. */ -- cgit v1.2.3