From c307ecc623ce3e8920a0652f2b880ffe6c1aa3e3 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Wed, 15 Jan 2025 13:30:26 +1300 Subject: [PATCH] MNT Make data providers static --- tests/php/Core/XssSanitiserTest.php | 33 +++++++------------ tests/php/Forms/FormMessageTest.php | 7 ++-- .../Shortcodes/EmbedShortcodeProviderTest.php | 7 ++-- 3 files changed, 18 insertions(+), 29 deletions(-) diff --git a/tests/php/Core/XssSanitiserTest.php b/tests/php/Core/XssSanitiserTest.php index 0d1ef959447..4c15da87a49 100644 --- a/tests/php/Core/XssSanitiserTest.php +++ b/tests/php/Core/XssSanitiserTest.php @@ -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 [ @@ -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(); @@ -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(); @@ -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' => [ @@ -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(); @@ -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' => [ @@ -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(); @@ -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' => [ @@ -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(); diff --git a/tests/php/Forms/FormMessageTest.php b/tests/php/Forms/FormMessageTest.php index 02f689a7378..9d7c1d427f3 100644 --- a/tests/php/Forms/FormMessageTest.php +++ b/tests/php/Forms/FormMessageTest.php @@ -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' => [ @@ -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 { diff --git a/tests/php/View/Shortcodes/EmbedShortcodeProviderTest.php b/tests/php/View/Shortcodes/EmbedShortcodeProviderTest.php index 2cac3fe3712..6328a4aed54 100644 --- a/tests/php/View/Shortcodes/EmbedShortcodeProviderTest.php +++ b/tests/php/View/Shortcodes/EmbedShortcodeProviderTest.php @@ -12,6 +12,7 @@ use SilverStripe\View\Embed\EmbedContainer; use stdClass; use RuntimeException; +use PHPUnit\Framework\Attributes\DataProvider; class EmbedShortcodeProviderTest extends EmbedUnitTest { @@ -222,7 +223,7 @@ public function testOnlyWhitelistedAttributesAllowed() ); } - public function provideSandboxHtml(): array + public static function provideSandboxHtml(): array { return [ 'normal' => [ @@ -365,9 +366,7 @@ public function provideSandboxHtml(): array ]; } - /** - * @dataProvider provideSandboxHtml - */ + #[DataProvider('provideSandboxHtml')] public function testSandboxHtml( string $url, array $excluded,