Skip to content

test: Add void return type to Kirby\Sane #7205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: v5/develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions tests/Sane/DomHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
namespace Kirby\Sane;

use Kirby\Exception\InvalidArgumentException;
use PHPUnit\Framework\Attributes\CoversClass;

/**
* @covers \Kirby\Sane\DomHandler
*/
#[CoversClass(DomHandler::class)]
class DomHandlerTest extends TestCase
{
public const TMP = KIRBY_TMP_DIR . '/Sane.DomHandler';

protected static string $type = 'sane';

public function testSanitize()
public function testSanitize(): void
{
$fixture = '<xml><test attr="value">Hello world</test></xml>';
$this->assertSame($fixture, DomHandler::sanitize($fixture));
Expand All @@ -31,29 +30,29 @@ public function testSanitize()
$this->assertSame('<xml><a>Very malicious</a></xml>', DomHandler::sanitize($string, isExternal: true));
}

public function testValidate()
public function testValidate(): void
{
$this->assertNull(DomHandler::validate('<!DOCTYPE xml><xml><test attr="value">Hello world</test></xml>'));
$this->assertNull(DomHandler::validate('<xml><a xlink:href="/another-folder">Very malicious</a></xml>'));
}

public function testValidateException1()
public function testValidateException1(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The URL is not allowed in attribute "href" (line 2): Unknown URL type');

DomHandler::validate("<xml>\n<a href='javascript:alert(1)'></a>\n</xml>");
}

public function testValidateException2()
public function testValidateException2(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The doctype must not reference external files');

DomHandler::validate("<!DOCTYPE xml SYSTEM \"https://malicious.com/something.dtd\">\n<xml>\n<a href='javascript:alert(1)'></a>\n</xml>");
}

public function testValidateException3()
public function testValidateException3(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The URL points outside of the site index URL');
Expand Down
41 changes: 8 additions & 33 deletions tests/Sane/HandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,16 @@

use Exception;
use Kirby\Exception\InvalidArgumentException;
use PHPUnit\Framework\Attributes\CoversClass;

/**
* @coversDefaultClass \Kirby\Sane\Handler
*/
#[CoversClass(Handler::class)]
class HandlerTest extends TestCase
{
public const TMP = KIRBY_TMP_DIR . '/Sane.Handler';

protected static string $type = 'sane';

/**
* @covers ::sanitizeFile
* @covers ::readFile
*/
public function testSanitizeFile()
public function testSanitizeFile(): void
{
$expected = $this->fixture('doctype-valid.svg');
$tmp = $this->fixture('doctype-valid.svg', true);
Expand All @@ -42,11 +37,7 @@ public function testSanitizeFile()
$this->assertFileEquals($expected, $tmp);
}

/**
* @covers ::sanitizeFile
* @covers ::readFile
*/
public function testSanitizeFileMissing()
public function testSanitizeFileMissing(): void
{
$file = $this->fixture('does-not-exist.svg');

Expand All @@ -56,46 +47,30 @@ public function testSanitizeFileMissing()
CustomHandler::sanitizeFile($file);
}

/**
* @covers ::validateFile
* @covers ::readFile
*/
public function testValidateFile()
public function testValidateFile(): void
{
$this->assertNull(
CustomHandler::validateFile($this->fixture('doctype-valid.svg'))
);
}

/**
* @covers ::validateFile
* @covers ::readFile
*/
public function testValidateFileError()
public function testValidateFileError(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The URL is not allowed in attribute "style"');

CustomHandler::validateFile($this->fixture('external-source-1.svg'));
}

/**
* @covers ::validateFile
* @covers ::readFile
*/
public function testValidateFileErrorExternalFile()
public function testValidateFileErrorExternalFile(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The URL points outside of the site index URL');

CustomHandler::validateFile($this->fixture('xlink-subfolder.svg'));
}

/**
* @covers ::validateFile
* @covers ::readFile
*/
public function testValidateFileMissing()
public function testValidateFileMissing(): void
{
$file = $this->fixture('does-not-exist.svg');

Expand Down
21 changes: 12 additions & 9 deletions tests/Sane/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
namespace Kirby\Sane;

use Kirby\Exception\InvalidArgumentException;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;

/**
* @covers \Kirby\Sane\Html
* @todo Add more tests from DOMPurify and the other test classes
*/
#[CoversClass(Html::class)]
class HtmlTest extends TestCase
{
public const TMP = KIRBY_TMP_DIR . '/Sane.Html';

protected static string $type = 'html';

/**
* @dataProvider allowedProvider
*/
public function testAllowed(string $file)
#[DataProvider('allowedProvider')]
public function testAllowed(string $file): void
{
$fixture = $this->fixture($file);

Expand All @@ -27,18 +27,21 @@ public function testAllowed(string $file)
$this->assertStringEqualsFile($fixture, $sanitized);
}

public static function allowedProvider()
public static function allowedProvider(): array
{
return static::fixtureList('allowed', 'html');
}

public function testDisallowedExternalFile()
public function testDisallowedExternalFile(): void
{
$fixture = $this->fixture('disallowed/link-subfolder.html');
$sanitized = $this->fixture('sanitized/link-subfolder.html');

$this->assertStringEqualsFile($fixture, Html::sanitize(file_get_contents($fixture)));
$this->assertStringEqualsFile($sanitized, Html::sanitize(file_get_contents($fixture), isExternal: true));
$html = Html::sanitize(file_get_contents($fixture));
$this->assertStringEqualsFile($fixture, $html);

$html = Html::sanitize(file_get_contents($fixture), isExternal: true);
$this->assertStringEqualsFile($sanitized, $html);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The URL points outside of the site index URL');
Expand Down
Loading
Loading