From fdf037e7287616b5211f8f8f7c8ce85b260d781e Mon Sep 17 00:00:00 2001 From: Accalia Elementia Date: Tue, 24 May 2022 11:07:50 -0400 Subject: Use Feed Link as GUID when Feed omits Guid. (#1785) * Use Feed Link as GUID when Feed omits Guid. As noted in nextcloud/news#1702 some feeds omit the GUID and are therefore not a valid RSS feed. nextcloud/news#1738 resolved the issue to allow valid feeds to update correctly when an invalid feed is present. This commit allows parsing of the invalid feed as well by assuming that the item link of the feed is unique to the feed and using it in place of the GUID when the feed omits the GUID. This will allow NextCloud News to accept and behave like many other popular feed aggregators when presented with such an invalid feed. Signed-off-by: Accalia * Add basic Logging when using fallback guid Signed-off-by: Accalia * Add basic Logging when using fallback guid - Fix Fatfinger Typo Signed-off-by: Accalia * Add basic Logging when using fallback guid - Update tests to account for additional logging Signed-off-by: Accalia --- tests/Unit/Fetcher/FeedFetcherTest.php | 44 +++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/Unit/Fetcher/FeedFetcherTest.php b/tests/Unit/Fetcher/FeedFetcherTest.php index 42f518453..af8066171 100644 --- a/tests/Unit/Fetcher/FeedFetcherTest.php +++ b/tests/Unit/Fetcher/FeedFetcherTest.php @@ -458,6 +458,40 @@ class FeedFetcherTest extends TestCase $this->assertSame($items[0]->getPubDate(), 1519761029); } + /** + * Test if the fetch function fetches a feed that specifies a guid. + */ + public function testFetchWithGuid() + { + $this->setUpReader($this->url); + $this->createItem(); + $feed = $this->createFeed(); + $this->mockIterator($this->feed_mock, [$this->item_mock]); + $result = $this->fetcher->fetch($this->url, false, null, null); + //Explicitly assert GUID value + $this->assertEquals(2, count($result)); + $this->assertEquals(1, count($result[1])); + $resultItem = $result[1][0]; + $this->assertEquals($this->guid, $resultItem->getGuid()); + } + + /** + * Test if the fetch function fetches a feed that does not specify a guid. + */ + public function testFetchWithoutGuid() + { + $this->setUpReader($this->url); + $this->guid = null; + $this->createItem(); + $feed = $this->createFeed(); + $this->mockIterator($this->feed_mock, [$this->item_mock]); + $result = $this->fetcher->fetch($this->url, false, null, null); + //Explicitly assert GUID value + $this->assertEquals(2, count($result)); + $this->assertEquals(1, count($result[1])); + $resultItem = $result[1][0]; + $this->assertEquals($this->permalink, $resultItem->getGuid()); + } /** * Mock an iteration option on an existing mock * @@ -576,10 +610,10 @@ class FeedFetcherTest extends TestCase $this->item_mock->expects($this->exactly(1)) ->method('getLink') ->will($this->returnValue($this->permalink)); - $this->item_mock->expects($this->exactly(2)) + $this->item_mock->expects($this->exactly(1)) ->method('getTitle') ->will($this->returnValue($this->title)); - $this->item_mock->expects($this->exactly(2)) + $this->item_mock->expects($this->exactly(1)) ->method('getPublicId') ->will($this->returnValue($this->guid)); $this->item_mock->expects($this->exactly(1)) @@ -600,7 +634,6 @@ class FeedFetcherTest extends TestCase $item->setUnread(true) ->setUrl($this->permalink) ->setTitle('my<\' title') - ->setGuid($this->guid) ->setGuidHash($this->guid_hash) ->setBody($this->parsed_body) ->setRtl(false) @@ -609,6 +642,11 @@ class FeedFetcherTest extends TestCase ->setAuthor(html_entity_decode($this->author->getName())) ->setCategoriesJson($this->categoriesJson); + // some tests deliberately omit this, so leave default value if the guid is to be ignored + if ($this->guid !== null) { + $item->setGuid($this->guid); + } + if ($enclosureType === 'audio/ogg' || $enclosureType === 'video/ogg') { $media = $this->getMockbuilder(MediaInterface::class)->getMock(); $media->expects($this->once()) -- cgit v1.2.3