Skip to content

Commit

Permalink
MNT Make data providers static
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Jan 15, 2025
1 parent 9b61628 commit c307ecc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 29 deletions.
33 changes: 11 additions & 22 deletions tests/php/Core/XssSanitiserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
use SilverStripe\Core\XssSanitiser;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\View\Parsers\HTMLValue;
use PHPUnit\Framework\Attributes\DataProvider;

class XssSanitiserTest extends SapphireTest
{
protected $usesDatabase = false;

public function provideSanitise(): array
public static function provideSanitise(): array
{
// Most of these scenarios are inspired by Symfony's HtmlSanitizerAllTest scenarios
return [
Expand Down Expand Up @@ -305,18 +306,14 @@ public function provideSanitise(): array
];
}

/**
* @dataProvider provideSanitise
*/
#[DataProvider('provideSanitise')]
public function testSanitiseString(string $input, string $expected): void
{
$sanitiser = new XssSanitiser();
$this->assertSame($expected, $sanitiser->sanitiseString($input));
}

/**
* @dataProvider provideSanitise
*/
#[DataProvider('provideSanitise')]
public function testSanitiseHtmlValue(string $input, string $expected): void
{
$sanitiser = new XssSanitiser();
Expand All @@ -325,9 +322,7 @@ public function testSanitiseHtmlValue(string $input, string $expected): void
$this->assertSame($expected, $htmlValue->getContent());
}

/**
* @dataProvider provideSanitise
*/
#[DataProvider('provideSanitise')]
public function testSanitiseElement(string $input, string $expected): void
{
$sanitiser = new XssSanitiser();
Expand All @@ -341,7 +336,7 @@ public function testSanitiseElement(string $input, string $expected): void
$this->assertSame($expected, $htmlValue->getContent());
}

public function provideSanitiseElementsAllowed(): array
public static function provideSanitiseElementsAllowed(): array
{
return [
'disallow these by default' => [
Expand All @@ -362,9 +357,7 @@ public function provideSanitiseElementsAllowed(): array
];
}

/**
* @dataProvider provideSanitiseElementsAllowed
*/
#[DataProvider('provideSanitiseElementsAllowed')]
public function testSanitiseElementsAllowed(string $input, ?array $removeElements, string $expected): void
{
$sanitiser = new XssSanitiser();
Expand All @@ -374,7 +367,7 @@ public function testSanitiseElementsAllowed(string $input, ?array $removeElement
$this->assertSame($expected, $sanitiser->sanitiseString($input));
}

public function provideSanitiseAttributesAllowed(): array
public static function provideSanitiseAttributesAllowed(): array
{
return [
'disallow these by default' => [
Expand Down Expand Up @@ -413,9 +406,7 @@ public function provideSanitiseAttributesAllowed(): array
];
}

/**
* @dataProvider provideSanitiseAttributesAllowed
*/
#[DataProvider('provideSanitiseAttributesAllowed')]
public function testSanitiseAttributesAllowed(string $input, ?array $removeAttributes, string $expected): void
{
$sanitiser = new XssSanitiser();
Expand All @@ -425,7 +416,7 @@ public function testSanitiseAttributesAllowed(string $input, ?array $removeAttri
$this->assertSame($expected, $sanitiser->sanitiseString($input));
}

public function provideSanitiseNoKeepInnerHtml(): array
public static function provideSanitiseNoKeepInnerHtml(): array
{
return [
'keeps inner html' => [
Expand All @@ -451,9 +442,7 @@ public function provideSanitiseNoKeepInnerHtml(): array
];
}

/**
* @dataProvider provideSanitiseNoKeepInnerHtml
*/
#[DataProvider('provideSanitiseNoKeepInnerHtml')]
public function testSanitiseNoKeepInnerHtml(string $input, bool $keepInnerHtml, string $expected): void
{
$sanitiser = new XssSanitiser();
Expand Down
7 changes: 4 additions & 3 deletions tests/php/Forms/FormMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
use SilverStripe\Core\XssSanitiser;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\Tests\FormMessageTest\TestFormMessage;
use SilverStripe\ORM\ValidationResult;
use SilverStripe\Core\Validation\ValidationResult;
use PHPUnit\Framework\Attributes\DataProvider;

class FormMessageTest extends SapphireTest
{
protected $usesDatabase = false;

public function provideGetMessage(): array
public static function provideGetMessage(): array
{
return [
'empty HTML' => [
Expand Down Expand Up @@ -69,8 +70,8 @@ public function provideGetMessage(): array
/**
* Test that getMessage() generally works and calls the sanitiser as appropriate.
* Note we don't actually test the sanitisation here, as that is handled by the sanitiser's unit tests.
* @dataProvider provideGetMessage
*/
#[DataProvider('provideGetMessage')]
public function testGetMessage(string $message, string $type, string $casting, string $expected): void
{
$mockSanitiserClass = get_class(new class extends XssSanitiser {
Expand Down
7 changes: 3 additions & 4 deletions tests/php/View/Shortcodes/EmbedShortcodeProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use SilverStripe\View\Embed\EmbedContainer;
use stdClass;
use RuntimeException;
use PHPUnit\Framework\Attributes\DataProvider;

class EmbedShortcodeProviderTest extends EmbedUnitTest
{
Expand Down Expand Up @@ -222,7 +223,7 @@ public function testOnlyWhitelistedAttributesAllowed()
);
}

public function provideSandboxHtml(): array
public static function provideSandboxHtml(): array
{
return [
'normal' => [
Expand Down Expand Up @@ -365,9 +366,7 @@ public function provideSandboxHtml(): array
];
}

/**
* @dataProvider provideSandboxHtml
*/
#[DataProvider('provideSandboxHtml')]
public function testSandboxHtml(
string $url,
array $excluded,
Expand Down

0 comments on commit c307ecc

Please sign in to comment.