Files
FreshRSS/tests/app/Views/entryHeaderTest.php
Alexandre Alapetite ea646d2bde Use PHP 8.4+ \Dom classes and functions (#9567)
Currently, SimplePie uses legacy DOMDocument parsing, which outputs potentially problematic HTML, not correctly sanitised.
This breaks the assumption that one can use something like `<div><!-- some HTML sanitized from SimplePie --></div>`.

```
--- Expected
+++ Actual
@@ @@
 '<ul>
-    <li><div></div></li>
+    <li><div>
     <li>Item</li>
-</ul>'
+</div></li></ul>'
```

When shown in a compliant Web browser, several tags will be wrongly closed, breaking the layout of the client.

This PR patches SimplePie to use the modern parser and code syntax, while maintaining a best-effort polyfill for legacy PHP versions < 8.4.

Depends on:

SimplePie: Upgrade to PHPStan 2
* https://github.com/FreshRSS/simplepie/pull/97
* https://github.com/simplepie/simplepie/pull/939
* https://github.com/simplepie/simplepie/pull/1019

SimplePie: Use PHP 8.4+ \Dom classes and functions
* https://github.com/FreshRSS/simplepie/pull/98
* https://github.com/simplepie/simplepie/issues/1021

SimplePie: Add unit test for HTML5 sanitizing compliance
* https://github.com/simplepie/simplepie/pull/1022

SimplePie: Remove obsolete xml_is_sane and XMLReader fallback
* https://github.com/simplepie/simplepie/pull/1023

CssXPath: Add support for case insensitivity
* https://github.com/phpgt/CssXPath/pull/256

fix https://github.com/FreshRSS/FreshRSS/issues/5201
fix https://github.com/FreshRSS/FreshRSS/issues/6830
fix https://github.com/FreshRSS/FreshRSS/issues/6831

~5% faster for HTML sanitising.
Remove SimpleXML. Remove XMLReader.
2026-11-19 23:17:04 +01:00

51 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
use PHPUnit\Framework\Attributes\PreserveGlobalState;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
require_once LIB_PATH . '/Minz/Url.php'; // For _url()
#[RunTestsInSeparateProcesses]
#[PreserveGlobalState(false)]
final class entryHeaderTest extends \PHPUnit\Framework\TestCase {
private const ARTICLE_URL = 'https://example.net/article';
private const LINK_ITEM = '//li[contains(concat(" ", normalize-space(@class), " "), " link ")]';
private static function renderEntryHeader(bool $titleOpensWebsite): Dom\XPath {
FreshRSS_Context::initSystem();
Minz_Session::init('FreshRSS', volatile: true);
FreshRSS_Context::setUserConf(clone FreshRSS_UserConfiguration::default());
FreshRSS_Context::userConf()->title_opens_website = $titleOpensWebsite;
Minz_Translate::init('en');
$view = new FreshRSS_View();
$view->_path('helpers/index/normal/entry_header.phtml');
$view->feed = new FreshRSS_Feed('https://example.net/feed.xml', false);
$view->entry = new FreshRSS_Entry(1, 'guid', 'Title', '', 'Content', self::ARTICLE_URL, 1700000000);
// Fragment of a page, so prefix with `<!DOCTYPE html>`
$doc = Dom\HTMLDocument::createFromString('<!DOCTYPE html>' . $view->renderToString(), Dom\HTML_NO_DEFAULT_NS);
return new Dom\XPath($doc);
}
private static function countNodes(Dom\XPath $xpath, string $query): int {
return $xpath->query($query)->length;
}
public function testLinkIconOpensOriginalWebsiteByDefault(): void {
$xpath = self::renderEntryHeader(titleOpensWebsite: false);
self::assertSame(1, self::countNodes($xpath, self::LINK_ITEM . '/a[@href="' . self::ARTICLE_URL . '"]'));
self::assertSame(0, self::countNodes($xpath, self::LINK_ITEM . '/button'));
}
public function testLinkIconOpensArticleInFreshRSSWhenTitleOpensWebsite(): void {
$xpath = self::renderEntryHeader(titleOpensWebsite: true);
self::assertSame(1, self::countNodes($xpath, self::LINK_ITEM . '/button[@type="button"]'));
self::assertSame(0, self::countNodes($xpath, self::LINK_ITEM . '/a'));
// The title is now the way to open the original website
self::assertSame(1, self::countNodes($xpath,
'//a[contains(concat(" ", normalize-space(@class), " "), " title ")][@href="' . self::ARTICLE_URL . '"][@target="_blank"]'));
}
}