Skip to content

Commit

Permalink
tec: Use feed entry id as link if the link is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
marienfressinaud committed Oct 17, 2024
1 parent 4258fe2 commit edfcddd
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/services/FeedFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ public function fetch(models\Collection $collection): void
$links_to_collections_to_create = [];

foreach ($feed->entries as $entry) {
if (!$entry->link && \SpiderBits\Url::isValid($entry->id)) {
$entry->link = $entry->id;
}

if (!$entry->link) {
continue;
}
Expand Down
59 changes: 59 additions & 0 deletions tests/jobs/scheduled/FeedsSyncTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,65 @@ public function testPerformSkipsFetchIfReachedRateLimit(): void
$this->assertNull($collection->feed_fetched_at);
}

public function testPerformUsesIdAsLinkIfEntryHasNoLink(): void
{
$feed_url = 'https://flus.fr/carnet/feeds/all.atom.xml';
$link_url = 'https://flus.fr/carnet/nouveautes-mars-2021.html';
/** @var string */
$old_name = $this->fakeUnique('sentence');
/** @var string */
$expected_name = $this->fakeUnique('sentence');
/** @var string */
$expected_title = $this->fakeUnique('sentence');
$collection = CollectionFactory::create([
'type' => 'feed',
'name' => $old_name,
'feed_url' => $feed_url,
'feed_fetched_at' => \Minz\Time::ago(2, 'hours'),
]);
$user = UserFactory::create([
'validated_at' => \Minz\Time::now(),
]);
FollowedCollectionFactory::create([
'collection_id' => $collection->id,
'user_id' => $user->id,
]);
$hash = \SpiderBits\Cache::hash($feed_url);
$raw_response = <<<XML
HTTP/2 200 OK
Content-Type: application/xml
<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>carnet de flus</title>
<link href="https://flus.fr/carnet/feeds/all.atom.xml" rel="self" type="application/atom+xml" />
<link href="https://flus.fr/carnet/" rel="alternate" type="text/html" />
<id>urn:uuid:4c04fe8e-c966-5b7e-af89-74d092a6ccb0</id>
<updated>2021-03-30T11:26:00+02:00</updated>
<entry>
<title>Les nouveautés de mars 2021</title>
<id>{$link_url}</id>
<author><name>Marien</name></author>
<published>2021-03-30T11:26:00+02:00</published>
<updated>2021-03-30T11:26:00+02:00</updated>
<content type="html"></content>
</entry>
</feed>
XML;
/** @var string */
$cache_path = \Minz\Configuration::$application['cache_path'];
$cache = new \SpiderBits\Cache($cache_path);
$cache->save($hash, $raw_response);
$feeds_sync_job = new FeedsSync();

$feeds_sync_job->perform();

$this->assertSame(1, models\Link::count());
$link = models\Link::take();
$this->assertNotNull($link);
$this->assertSame($link_url, $link->url);
}

public function testPerformIgnoresEntriesWithNoLink(): void
{
$feed_url = 'https://flus.fr/carnet/feeds/all.atom.xml';
Expand Down

0 comments on commit edfcddd

Please sign in to comment.