Files
FreshRSS/tests/app/Views/headerTest.php
T
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

52 lines
2.2 KiB
PHP

<?php
declare(strict_types=1);
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\PreserveGlobalState;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
#[RunTestsInSeparateProcesses]
#[PreserveGlobalState(false)]
final class headerTest extends \PHPUnit\Framework\TestCase {
/** @return Traversable<string,array{?string,?string}> */
public static function provideSearchSorts(): Traversable {
foreach (['id', 'c.name', 'date', 'f.name', 'lastUserModified', 'length', 'link', 'title', 'rand'] as $sort) {
yield $sort => [$sort, $sort];
}
yield 'no override' => [null, null];
yield 'empty override' => ['', null];
yield 'invalid override' => ['" onfocus="alert(1)', null];
}
#[DataProvider('provideSearchSorts')]
public function testSearchPreservesSort(?string $requestedSort, ?string $expectedSort): void {
FreshRSS_Context::initSystem();
FreshRSS_Context::systemConf()->allow_anonymous = true;
Minz_User::change(FreshRSS_Context::systemConf()->default_user);
FreshRSS_Context::setUserConf(clone FreshRSS_UserConfiguration::default());
Minz_Translate::init('en');
Minz_Request::_params($requestedSort === null ? [] : ['sort' => $requestedSort]);
// The context has already validated the requested sort before rendering the header.
FreshRSS_Context::$sort = match ($expectedSort) {
'c.name', 'date', 'f.name', 'lastUserModified', 'length', 'link', 'title', 'rand' => $expectedSort,
default => 'id',
};
FreshRSS_Context::$search = new FreshRSS_BooleanSearch('needle');
$view = new FreshRSS_View();
$view->_path('../layout/header.phtml');
$view->html_url = 'https://example.net/';
// Fragment of a page, so prefix with `<!DOCTYPE html>`
$doc = Dom\HTMLDocument::createFromString('<!DOCTYPE html>' . $view->renderToString(), Dom\HTML_NO_DEFAULT_NS);
$inputs = (new Dom\XPath($doc))->query('//form[.//input[@name="search"]]/input[@name="sort"]');
self::assertSame($expectedSort === null ? 0 : 1, $inputs->length);
if ($expectedSort !== null) {
$input = $inputs->item(0);
self::assertInstanceOf(Dom\Element::class, $input);
self::assertSame('hidden', $input->getAttribute('type'));
self::assertSame($expectedSort, $input->getAttribute('value'));
}
}
}