mirror of
https://github.com/FreshRSS/FreshRSS.git
synced 2026-12-02 13:32:43 -05:00
* feat: add the <enclosure/> field to enhance compatibility with BitTorrent downloaders Closes #6227 Changes proposed in this pull request: - Emit an additional RSS 2.0 `<enclosure>` element for BitTorrent attachments in the generated RSS output (`p/api/query.php?...&f=rss` and the per-feed RSS view). The existing `<media:content>` output is unchanged. - Emit it only for `application/x-bittorrent`, `magnet:` and `.torrent` URLs, and skip images / audio / video. Since 1.24 thumbnails are prepended to the enclosure list, a naive implementation would duplicate cover images (and podcast media) as attachments. - Always set `type="application/x-bittorrent"`, and copy `length` when the feed provides it. - Add unit tests in `tests/app/Views/rssViewTest.php`: torrent, magnet without type, image attachment skipped. * fix(rss): emit <enclosure> only for BitTorrent attachments The previous patch emitted <enclosure> for every enclosure. Since FreshRSS 1.24, thumbnails are prepended to the enclosures list, so cover images and podcast audio were also duplicated as RSS attachments. Emit the tag only for application/x-bittorrent, magnet: or .torrent URLs, and always set type="application/x-bittorrent" so clients such as qBittorrent can use it. Add three rssViewTest cases: torrent, magnet without type, image skipped. * Improve regex * Minor whitespace --------- Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>
80 lines
3.0 KiB
PHP
80 lines
3.0 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
final class rssViewTest extends \PHPUnit\Framework\TestCase {
|
|
|
|
#[\Override]
|
|
public static function setUpBeforeClass(): void {
|
|
// `FreshRSS_View` needs a system configuration; the shipped defaults are enough to render a feed.
|
|
Minz_Configuration::register('system', FRESHRSS_PATH . '/config.default.php', FRESHRSS_PATH . '/config.default.php');
|
|
}
|
|
|
|
/** @param array<string,mixed> $enclosure */
|
|
private static function renderEntryWithEnclosure(array $enclosure): string {
|
|
$entry = new FreshRSS_Entry(1, 'guid', 'Title', '', 'Content', 'https://example.net/article', 1700000000);
|
|
$entry->_attribute('enclosures', [$enclosure]);
|
|
|
|
$view = new FreshRSS_View();
|
|
$view->_path('index/rss.phtml');
|
|
$view->internal_rendering = true;
|
|
$view->rss_title = 'Test';
|
|
$view->rss_url = 'https://example.net/rss';
|
|
$view->html_url = 'https://example.net/';
|
|
$view->description = 'Test';
|
|
$view->entries = [$entry];
|
|
|
|
return $view->renderToString();
|
|
}
|
|
|
|
/** An enclosure with several `<media:credit>` must keep them all, see https://github.com/FreshRSS/FreshRSS/issues/5066 */
|
|
public function test_rss_multipleEnclosureCredits(): void {
|
|
$rss = self::renderEntryWithEnclosure([
|
|
'url' => 'https://example.net/audio.mp3',
|
|
'type' => 'audio/mpeg',
|
|
'credit' => ['Alice', 'Bob', 'Carol'],
|
|
]);
|
|
self::assertStringContainsString(
|
|
'<media:credit>Alice</media:credit><media:credit>Bob</media:credit><media:credit>Carol</media:credit>', $rss);
|
|
}
|
|
|
|
/** Entries older than FreshRSS 1.24 store a single credit as a string instead of an array */
|
|
public function test_rss_legacySingleEnclosureCredit(): void {
|
|
$rss = self::renderEntryWithEnclosure([
|
|
'url' => 'https://example.net/audio.mp3',
|
|
'type' => 'audio/mpeg',
|
|
'credit' => 'Alice',
|
|
]);
|
|
self::assertStringContainsString('<media:credit>Alice</media:credit>', $rss);
|
|
}
|
|
|
|
/** BitTorrent enclosures must also be exposed as RSS 2.0 `<enclosure>` for clients that ignore Media RSS */
|
|
public function test_rss_enclosureForBitTorrent(): void {
|
|
$rss = self::renderEntryWithEnclosure([
|
|
'url' => 'https://example.net/file.torrent',
|
|
'type' => 'application/x-bittorrent',
|
|
'length' => 12216,
|
|
]);
|
|
self::assertStringContainsString(
|
|
'<enclosure url="https://example.net/file.torrent" type="application/x-bittorrent" length="12216"/>', $rss);
|
|
}
|
|
|
|
/** A magnet link is a torrent even without a MIME type */
|
|
public function test_rss_enclosureForMagnetWithoutType(): void {
|
|
$rss = self::renderEntryWithEnclosure([
|
|
'url' => 'magnet:?xt=urn:btih:0123456789abcdef',
|
|
]);
|
|
self::assertStringContainsString(
|
|
'<enclosure url="magnet:?xt=urn:btih:0123456789abcdef" type="application/x-bittorrent"/>', $rss);
|
|
}
|
|
|
|
/** Images / audio / video must not be duplicated as RSS 2.0 `<enclosure>` */
|
|
public function test_rss_noEnclosureForImageAttachment(): void {
|
|
$rss = self::renderEntryWithEnclosure([
|
|
'url' => 'https://example.net/cover.jpg',
|
|
'type' => 'image/jpeg',
|
|
'medium' => 'image',
|
|
]);
|
|
self::assertStringNotContainsString('<enclosure', $rss);
|
|
}
|
|
}
|