Files
tim ed959888b4 Add PHP unit tests and page tests.
Run "composer install", then "composer test".
2026-09-26 03:48:09 +02:00

55 lines
2.0 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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));
}
}