Files
FreshRSS/tests/app/Utils/dotNotationUtilTest.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

119 lines
3.7 KiB
PHP

<?php
declare(strict_types=1);
use PHPUnit\Framework\Attributes\DataProvider;
final class dotNotationUtilTest extends \PHPUnit\Framework\TestCase {
/**
* @return Traversable<array{array<string,mixed>,string,string}>
*/
public static function provideJsonDots(): Traversable {
$json = <<<json
{
"hello": "world",
"deeper": {
"hello": "again"
},
"items": [
{
"meta": {"title": "first"}
},
{
"meta": {"title": "second"}
}
]
}
json;
$array = json_decode($json, true);
yield [$array, 'hello', 'world'];
yield [$array, 'deeper.hello', 'again'];
yield [$array, 'items.0.meta.title', 'first'];
yield [$array, 'items[0].meta.title', 'first'];
yield [$array, 'items.1.meta.title', 'second'];
yield [$array, 'items[1].meta.title', 'second'];
yield [$array, '"Hello " & hello & \'!\'', 'Hello world!'];
yield [$array, '"Hello & goodbye " & hello & \'!\'', 'Hello & goodbye world!'];
yield [$array, '"Hello " & hello & deeper.hello & "!"', 'Hello worldagain!'];
}
/**
* @param array<string,mixed> $array
*/
#[DataProvider('provideJsonDots')]
public static function testJsonDots(array $array, string $key, string $expected): void {
$value = FreshRSS_dotNotation_Util::get($array, $key);
self::assertSame($expected, $value);
}
public static function testConvertJsonToRssEscapesCategories(): void {
FreshRSS_Context::initSystem();
Minz_Translate::init('en');
$json = <<<json
{
"items": [
{"title": "First", "url": "https://example.net/1", "categories": ["News & Politics", "Arts & Culture"]},
{"title": "Second", "url": "https://example.net/2", "categories": "Science & Tech"}
]
}
json;
$jf = json_decode($json, true);
self::assertIsArray($jf);
$rss = FreshRSS_dotNotation_Util::convertJsonToRss($jf, 'https://example.net/', [
'item' => 'items',
'itemTitle' => 'title',
'itemUri' => 'url',
'itemCategories' => 'categories',
], 'Test');
self::assertIsString($rss);
self::assertStringContainsString('<category>News &amp; Politics</category>', $rss);
self::assertStringContainsString('<category>Arts &amp; Culture</category>', $rss);
self::assertStringContainsString('<category>Science &amp; Tech</category>', $rss);
try {
$dom = Dom\XMLDocument::createFromString($rss);
} catch (Throwable $ex) { // @phpstan-ignore catch.neverThrown (Dom\XMLDocument::createFromString() does throw)
self::fail('Output must be valid XML: ' . $ex->getMessage());
}
self::assertInstanceOf(Dom\XMLDocument::class, $dom);
}
public static function testConvertJsonToRssKeepsAttachments(): void {
FreshRSS_Context::initSystem();
Minz_Translate::init('en');
$json = <<<json
{
"items": [
{
"title": "Episode 1",
"url": "https://example.net/1",
"attachments": [
{"url": "https://example.net/1.mp3?a=1&b=2", "mime_type": "audio/mpeg", "size_in_bytes": 1234}
]
}
]
}
json;
$jf = json_decode($json, true);
self::assertIsArray($jf);
$rss = FreshRSS_dotNotation_Util::convertJsonToRss($jf, 'https://example.net/', [
'item' => 'items',
'itemTitle' => 'title',
'itemUri' => 'url',
'itemAttachment' => 'attachments',
'itemAttachmentUrl' => 'url',
'itemAttachmentType' => 'mime_type',
'itemAttachmentLength' => 'size_in_bytes',
], 'Test');
self::assertIsString($rss);
self::assertStringContainsString(
'<media:content url="https://example.net/1.mp3?a=1&amp;b=2" type="audio/mpeg" length="1234">', $rss);
try {
$dom = Dom\XMLDocument::createFromString($rss);
} catch (Throwable $ex) { // @phpstan-ignore catch.neverThrown (the XML parser throws on malformed documents, not disclosed in the PHP stubs)
self::fail('Output must be valid XML: ' . $ex->getMessage());
}
self::assertInstanceOf(Dom\XMLDocument::class, $dom);
}
}