Add PHP unit tests and page tests.

Run "composer install", then "composer test".
This commit is contained in:
tim
2026-09-26 03:48:09 +02:00
parent 848c447c5b
commit ed959888b4
42 changed files with 5522 additions and 0 deletions
+54
View File
@@ -0,0 +1,54 @@
<?php
namespace Xxx\Tests\Unit;
use PHPUnit\Framework\Attributes\DataProvider;
use Xxx\Tests\Support\TestCase;
// h() and form_value() in html.lib.php
class HtmlTest extends TestCase {
public static function escapes(): array {
return array(
'tags' => array('<script>alert(1)</script>', '&lt;script&gt;alert(1)&lt;/script&gt;'),
'double quotes' => array('say "hi"', 'say &quot;hi&quot;'),
'single quotes' => array("it's", 'it&#039;s'),
'ampersand' => array('fish & chips', 'fish &amp; chips'),
'plain text' => array('Plain text', 'Plain text'),
'utf-8' => array('café – “quoted”', 'café – “quoted”'),
);
}
#[DataProvider('escapes')]
public function testHEscapesText(string $input, string $expected): void {
$this->assertSame($expected, h($input));
}
#[DataProvider('escapes')]
public function testFormValueEscapesText(string $input, string $expected): void {
$this->assertSame($expected, form_value($input));
}
public function testHKeepsExistingEntities(): void {
$this->assertSame('Wait&hellip; &amp; see', h('Wait&hellip; & see'));
}
public function testFormValueEscapesExistingEntitiesSoThatTheyComeBackUnchanged(): void {
$this->assertSame('Wait&amp;hellip;', form_value('Wait&hellip;'));
$this->assertSame('Wait&hellip;', html_entity_decode(form_value('Wait&hellip;'), ENT_QUOTES, 'UTF-8'));
}
public function testFormValueStopsATextareaFromEnding(): void {
$this->assertSame('&lt;/textarea&gt;&lt;script&gt;', form_value('</textarea><script>'));
}
public function testInvalidUtf8IsReplacedNotDropped(): void {
$this->assertSame("a\u{FFFD}b", h("a\xC3b"));
$this->assertSame("a\u{FFFD}b", form_value("a\xC3b"));
}
public function testNonStringsBecomeStrings(): void {
$this->assertSame('', h(NULL));
$this->assertSame('42', h(42));
$this->assertSame('1', form_value(TRUE));
$this->assertSame('', form_value(NULL));
}
}