From d169fbfe52396d4df1778a8612b765c4ef5dce25 Mon Sep 17 00:00:00 2001 From: Nicolas Lemoine Date: Thu, 4 Jan 2024 11:54:49 +0100 Subject: [PATCH 1/8] Fix `mb_convert_encoding` PHP 8.2 deprecation --- src/OutputFilter/AttributesOutputFilter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OutputFilter/AttributesOutputFilter.php b/src/OutputFilter/AttributesOutputFilter.php index 8aec887..a6a57bc 100644 --- a/src/OutputFilter/AttributesOutputFilter.php +++ b/src/OutputFilter/AttributesOutputFilter.php @@ -45,7 +45,7 @@ public function __invoke(string $html, Asset $asset): string $doc = new \DOMDocument(); libxml_use_internal_errors(true); @$doc->loadHTML( - mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8"), + mb_encode_numericentity($html, [0x80, 0xFFFF, 0, 0xFFFF], "UTF-8"), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD ); libxml_clear_errors(); From f842c307ffc7b8a42ef3db1949b681d12f7cb09a Mon Sep 17 00:00:00 2001 From: shvlv Date: Tue, 27 Feb 2024 16:44:21 +0200 Subject: [PATCH 2/8] fix: PHP 8.2 deprecation warning for mb_convert_encoding --- src/OutputFilter/AttributesOutputFilter.php | 4 ++-- .../phpunit/Unit/OutputFilter/AttributesOutputFilterTest.php | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/OutputFilter/AttributesOutputFilter.php b/src/OutputFilter/AttributesOutputFilter.php index a6a57bc..248cfe5 100644 --- a/src/OutputFilter/AttributesOutputFilter.php +++ b/src/OutputFilter/AttributesOutputFilter.php @@ -45,7 +45,7 @@ public function __invoke(string $html, Asset $asset): string $doc = new \DOMDocument(); libxml_use_internal_errors(true); @$doc->loadHTML( - mb_encode_numericentity($html, [0x80, 0xFFFF, 0, 0xFFFF], "UTF-8"), + mb_encode_numericentity($html, [0x80, 0x10FFFF, 0, ~0], "UTF-8"), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD ); libxml_clear_errors(); @@ -60,7 +60,7 @@ public function __invoke(string $html, Asset $asset): string $this->applyAttributes($script, $attributes); } - return $this->removeRootElement($doc->saveHTML()); + return $this->removeRootElement(html_entity_decode($doc->saveHTML())); } /** diff --git a/tests/phpunit/Unit/OutputFilter/AttributesOutputFilterTest.php b/tests/phpunit/Unit/OutputFilter/AttributesOutputFilterTest.php index c89ff98..1ba1ad0 100644 --- a/tests/phpunit/Unit/OutputFilter/AttributesOutputFilterTest.php +++ b/tests/phpunit/Unit/OutputFilter/AttributesOutputFilterTest.php @@ -218,5 +218,10 @@ public function provideRenderWithInlineScripts(): \Generator $multiLineJs, $multiLineJs, ]; + + yield 'before and after multibyte line' => [ + '', + '', + ]; } } From 332b725c7f832f50204b71628200d6f490419f0a Mon Sep 17 00:00:00 2001 From: Volodymyr Shelmuk Date: Fri, 15 Mar 2024 23:03:53 +0200 Subject: [PATCH 3/8] tests: add non-standard URL case --- .../Unit/OutputFilter/AttributesOutputFilterTest.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/Unit/OutputFilter/AttributesOutputFilterTest.php b/tests/phpunit/Unit/OutputFilter/AttributesOutputFilterTest.php index 1ba1ad0..6525ad2 100644 --- a/tests/phpunit/Unit/OutputFilter/AttributesOutputFilterTest.php +++ b/tests/phpunit/Unit/OutputFilter/AttributesOutputFilterTest.php @@ -193,6 +193,8 @@ public function provideRenderWithInlineScripts(): \Generator console.log("script with multiple lines") })(); JS; + $multiByteLine = ''; + $nonStandardUrl = ''; yield 'before single line' => [ $singleLineJs, @@ -220,8 +222,13 @@ public function provideRenderWithInlineScripts(): \Generator ]; yield 'before and after multibyte line' => [ - '', - '', + $multiByteLine, + $multiByteLine, + ]; + + yield 'before and after URL with non-alphanumeric characters' => [ + $nonStandardUrl, + $nonStandardUrl, ]; } } From 623c5e46ff7b2c1c67ad54c61267e196736cd994 Mon Sep 17 00:00:00 2001 From: Volodymyr Shelmuk Date: Sat, 16 Mar 2024 00:49:32 +0200 Subject: [PATCH 4/8] feat: use WP_HTML_Tag_Processor for AttributesOutputFilter --- composer.json | 4 +- phpunit.xml.dist | 7 +- src/OutputFilter/AttributesOutputFilter.php | 96 ++------ .../AttributesOutputFilterTest.php | 227 ++++++++++++++++++ .../AttributesOutputFilterTest.php | 206 +--------------- 5 files changed, 262 insertions(+), 278 deletions(-) create mode 100644 tests/phpunit/Integration/OutputFilter/AttributesOutputFilterTest.php diff --git a/composer.json b/composer.json index 38a2049..64543ea 100644 --- a/composer.json +++ b/composer.json @@ -31,8 +31,8 @@ "mikey179/vfsstream": "^1.6.8", "inpsyde/php-coding-standards": "^1", "vimeo/psalm": "@stable", - "php-stubs/wordpress-stubs": ">=6.0@stable", - "johnpbloch/wordpress-core": ">=6.0" + "php-stubs/wordpress-stubs": ">=6.2@stable", + "johnpbloch/wordpress-core": ">=6.2" }, "autoload": { "psr-4": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 0ade292..ac9394f 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,5 +1,5 @@ - + src @@ -14,7 +14,10 @@ - tests/phpunit/Unit + tests/phpunit/Unit + + + tests/phpunit/Integration diff --git a/src/OutputFilter/AttributesOutputFilter.php b/src/OutputFilter/AttributesOutputFilter.php index 248cfe5..dc4e87b 100644 --- a/src/OutputFilter/AttributesOutputFilter.php +++ b/src/OutputFilter/AttributesOutputFilter.php @@ -15,24 +15,8 @@ use Inpsyde\Assets\Asset; -/** - * @psalm-suppress UndefinedMethod - */ class AttributesOutputFilter implements AssetOutputFilter { - private const ROOT_ELEMENT_START = ''; - private const ROOT_ELEMENT_END = ''; - - /** - * @param string $html - * @param Asset $asset - * - * @return string - * - * phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged - * @psalm-suppress PossiblyFalseArgument - * @psalm-suppress ArgumentTypeCoercion - */ public function __invoke(string $html, Asset $asset): string { $attributes = $asset->attributes(); @@ -40,70 +24,36 @@ public function __invoke(string $html, Asset $asset): string return $html; } - $html = $this->wrapHtmlIntoRoot($html); - - $doc = new \DOMDocument(); - libxml_use_internal_errors(true); - @$doc->loadHTML( - mb_encode_numericentity($html, [0x80, 0x10FFFF, 0, ~0], "UTF-8"), - LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD - ); - libxml_clear_errors(); + if (!class_exists(\WP_HTML_Tag_Processor::class)) { + // phpcs:disable WordPress.PHP.DevelopmentFunctions.error_log_trigger_error + trigger_error( + 'Adding attributes is not supported for WordPress < 6.2', + \E_USER_DEPRECATED + ); + // phpcs:enable WordPress.PHP.DevelopmentFunctions.error_log_trigger_error - $scripts = $doc->getElementsByTagName('script'); - foreach ($scripts as $script) { - // Only extend before and after. - if (!$script->hasAttribute('src')) { - continue; - } - $this->applyAttributes($script, $attributes); + return $html; } - return $this->removeRootElement(html_entity_decode($doc->saveHTML())); - } - - /** - * Wrapping multiple scripts into a root-element - * to be able to load it via DOMDocument. - * - * @param string $html - * - * @return string - */ - protected function wrapHtmlIntoRoot(string $html): string - { - return self::ROOT_ELEMENT_START . $html . self::ROOT_ELEMENT_END; - } + $tags = new \WP_HTML_Tag_Processor($html); - /** - * Remove root element and return original HTML. - * - * @param string $html - * - * @return string - * @see AttributesOutputFilter::wrapHtmlIntoRoot() - * - */ - protected function removeRootElement(string $html): string - { - $regex = '~' . self::ROOT_ELEMENT_START . '(.+?)' . self::ROOT_ELEMENT_END . '~s'; - preg_match($regex, $html, $matches); + // Only extend before and after. + if ( + $tags->next_tag(['tag_name' => 'script']) + && (string) $tags->get_attribute('src') + ) { + $this->applyAttributes($tags, $attributes); + } - return $matches[1]; + return $tags->get_updated_html(); } - /** - * @param \DOMElement $script - * @param array $attributes - * - * @return void - */ - protected function applyAttributes(\DOMElement $script, array $attributes) + protected function applyAttributes(\WP_HTML_Tag_Processor $script, array $attributes): void { foreach ($attributes as $key => $value) { - $key = esc_attr((string) $key); - if ($script->hasAttribute($key)) { + $key = esc_attr((string)$key); + if ((string) $script->get_attribute($key)) { continue; } if (is_bool($value) && !$value) { @@ -111,9 +61,9 @@ protected function applyAttributes(\DOMElement $script, array $attributes) } $value = is_bool($value) ? esc_attr($key) - : esc_attr((string) $value); + : esc_attr((string)$value); - $script->setAttribute($key, $value); + $script->set_attribute($key, $value); } } } diff --git a/tests/phpunit/Integration/OutputFilter/AttributesOutputFilterTest.php b/tests/phpunit/Integration/OutputFilter/AttributesOutputFilterTest.php new file mode 100644 index 0000000..81089c6 --- /dev/null +++ b/tests/phpunit/Integration/OutputFilter/AttributesOutputFilterTest.php @@ -0,0 +1,227 @@ +'; + + static::assertInstanceOf(AssetOutputFilter::class, $testee); + static::assertSame($input, $testee($input, $stub)); + } + + /** + * @dataProvider provideAttributes + */ + public function testRenderWithAttributes(array $attributes, array $expected, array $notExpected): void + { + $stub = new Script('stub-script', 'https://syde.com/foo.js'); + $stub->withAttributes($attributes); + + $input = ''; + + $testee = new AttributesOutputFilter(); + $output = $testee($input, $stub); + + foreach ($expected as $test) { + static::assertStringContainsString($test, $output); + } + foreach ($notExpected as $test) { + static::assertStringNotContainsString($test, $output); + } + } + + /** + * @return \Generator + */ + public function provideAttributes(): \Generator + { + yield 'string value' => [ + [ + 'key' => 'value', + ], + ['key="value"'], + [], + ]; + + yield 'integer value' => [ + [ + 'key' => 1, + ], + ['key="1"'], + [], + ]; + + yield 'bool true value' => [ + [ + 'key' => true, + ], + ['key="key"'], + [], + ]; + + yield 'bool false value' => [ + [ + 'key' => false, + ], + [], + ['key="key"'], + ]; + + yield 'overwriting src-attribute' => [ + [ + 'key' => 'value', + 'src' => 'not-allowed.js', + ], + ['key="value"'], + ['src="not-allowed.js"'], + ]; + } + + public function testRenderNotOverwriteExistingAttributes(): void + { + $expectedKey = 'src'; + $expectedValue = 'foo.js'; + $expectedAttribute = sprintf('%s="%s"', $expectedKey, $expectedValue); + + $stub = new Script('stub-script', 'https://syde.com/foo.js'); + // We're trying to overwrite the "src" with "bar.js". + $stub->withAttributes([$expectedKey => 'bar.js']); + + $input = sprintf('', $expectedAttribute); + + $testee = new AttributesOutputFilter(); + static::assertStringContainsString($expectedAttribute, $testee($input, $stub)); + } + + public function testRenderInlineScriptsNotChanged() + { + $expectedKey = 'key'; + $expectedValue = 'value'; + $expectedAttributes = [$expectedKey => $expectedValue]; + + $expectedBefore = ""; + $expectedAfter = ""; + + $stub = new Script('stub-script', 'https://syde.com/foo.js'); + $stub->withAttributes($expectedAttributes); + + $input = $expectedBefore . '' . $expectedAfter; + + $testee = new AttributesOutputFilter(); + $output = $testee($input, $stub); + static::assertStringContainsString($expectedBefore, $output); + static::assertStringContainsString($expectedAfter, $output); + } + + /** + * @dataProvider provideRenderWithInlineScripts + */ + public function testRenderWithInlineScripts(string $expectedBefore, string $expectedAfter): void + { + $stub = new Script('stub-script', 'https://syde.com/foo.js'); + $stub->withAttributes(['foo' => 'bar']); + + $input = $expectedBefore . '' . $expectedAfter; + + $testee = new AttributesOutputFilter(); + $output = $testee($input, $stub); + static::assertStringContainsString($expectedBefore, $output); + static::assertStringContainsString($expectedAfter, $output); + } + + public function provideRenderWithInlineScripts(): \Generator + { + $singleLineJs = '(function(){ console.log("script with single line"); })();'; + $multiLineJs = << [ + $singleLineJs, + '', + ]; + + yield 'after single line' => [ + '', + $singleLineJs, + ]; + + yield 'before and after single line' => [ + $singleLineJs, + $singleLineJs, + ]; + + yield 'before multi, after single line' => [ + $multiLineJs, + $singleLineJs, + ]; + + yield 'before and after multi line' => [ + $multiLineJs, + $multiLineJs, + ]; + + yield 'before and after multibyte line' => [ + $multiByteLine, + $multiByteLine, + ]; + + yield 'before and after URL with non-alphanumeric characters' => [ + $nonStandardUrl, + $nonStandardUrl, + ]; + } +} diff --git a/tests/phpunit/Unit/OutputFilter/AttributesOutputFilterTest.php b/tests/phpunit/Unit/OutputFilter/AttributesOutputFilterTest.php index 6525ad2..896e8f7 100644 --- a/tests/phpunit/Unit/OutputFilter/AttributesOutputFilterTest.php +++ b/tests/phpunit/Unit/OutputFilter/AttributesOutputFilterTest.php @@ -18,217 +18,21 @@ use Inpsyde\Assets\OutputFilter\AttributesOutputFilter; use Inpsyde\Assets\Tests\Unit\AbstractTestCase; -use function Brain\Monkey\Functions\expect; - class AttributesOutputFilterTest extends AbstractTestCase { - /** - * @test - */ - public function testBasic() + public function testIfTagProcessorIsUnavailable(): void { + $this->expectDeprecation(); $testee = new AttributesOutputFilter(); $stub = \Mockery::mock(Asset::class); - $stub->expects('attributes')->andReturn([]); + $stub->expects('attributes')->andReturn([ + 'key' => 'value', + ]); $input = ''; static::assertInstanceOf(AssetOutputFilter::class, $testee); static::assertSame($input, $testee($input, $stub)); } - - /** - * @param array $attributes - * @param array $expected - * @param array $notExpected - * - * @test - * @dataProvider provideAttributes - */ - public function testRenderWithAttributes(array $attributes, array $expected, array $notExpected) - { - // esc_attr always returns a string, even if we input an integer. - expect('esc_attr')->andReturnUsing( - static function ($input): string { - return (string) $input; - } - ); - - $stub = \Mockery::mock(Asset::class); - $stub->expects('attributes')->andReturn($attributes); - - $input = ''; - - $testee = new AttributesOutputFilter(); - $output = $testee($input, $stub); - - foreach ($expected as $test) { - static::assertStringContainsString($test, $output); - } - foreach ($notExpected as $test) { - static::assertStringNotContainsString($test, $output); - } - } - - /** - * @return \Generator - */ - public function provideAttributes(): \Generator - { - yield 'string value' => [ - [ - 'key' => 'value', - ], - ['key="value"'], - [], - ]; - - yield 'integer value' => [ - [ - 'key' => 1, - ], - ['key="1"'], - [], - ]; - - yield 'bool true value' => [ - [ - 'key' => true, - ], - ['key="key"'], - [], - ]; - - yield 'bool false value' => [ - [ - 'key' => false, - ], - [], - ['key="key"'], - ]; - - yield 'overwriting src-attribute' => [ - [ - 'key' => 'value', - 'src' => 'not-allowed.js', - ], - ['key="value"'], - ['src="not-allowed.js"'], - ]; - } - - /** - * @test - */ - public function testRenderNotOverwriteExistingAttributes() - { - $expectedKey = 'src'; - $expectedValue = 'foo.js'; - $expectedAttribute = sprintf('%s="%s"', $expectedKey, $expectedValue); - - expect('esc_attr')->andReturnFirstArg(); - - $stub = \Mockery::mock(Asset::class); - // We're trying to overwrite the "src" with "bar.js". - $stub->expects('attributes')->andReturn([$expectedKey => 'bar.js']); - - $input = sprintf('', $expectedAttribute); - - $testee = new AttributesOutputFilter(); - static::assertStringContainsString($expectedAttribute, $testee($input, $stub)); - } - - /** - * @test - */ - public function testRenderInlineScriptsNotChanged() - { - $expectedKey = 'key'; - $expectedValue = 'value'; - $expectedAttributes = [$expectedKey => $expectedValue]; - - $expectedBefore = ""; - $expectedAfter = ""; - - expect('esc_attr')->andReturnFirstArg(); - - $stub = \Mockery::mock(Asset::class); - $stub->expects('attributes')->andReturn($expectedAttributes); - - $input = $expectedBefore . '' . $expectedAfter; - - $testee = new AttributesOutputFilter(); - $output = $testee($input, $stub); - static::assertStringContainsString($expectedBefore, $output); - static::assertStringContainsString($expectedAfter, $output); - } - - /** - * @param string $expectedBefore - * @param string $expectedAfter - * - * @test - * @dataProvider provideRenderWithInlineScripts - */ - public function testRenderWithInlineScripts(string $expectedBefore, string $expectedAfter) - { - expect('esc_attr')->andReturnFirstArg(); - $stub = \Mockery::mock(Asset::class); - $stub->expects('attributes')->andReturn(['foo' => 'bar']); - - $input = $expectedBefore . '' . $expectedAfter; - - $testee = new AttributesOutputFilter(); - $output = $testee($input, $stub); - static::assertStringContainsString($expectedBefore, $output); - static::assertStringContainsString($expectedAfter, $output); - } - - public function provideRenderWithInlineScripts(): \Generator - { - $singleLineJs = '(function(){ console.log("script with single line"); })();'; - $multiLineJs = << [ - $singleLineJs, - '', - ]; - - yield 'after single line' => [ - '', - $singleLineJs, - ]; - - yield 'before and after single line' => [ - $singleLineJs, - $singleLineJs, - ]; - - yield 'before multi, after single line' => [ - $multiLineJs, - $singleLineJs, - ]; - - yield 'before and after multi line' => [ - $multiLineJs, - $multiLineJs, - ]; - - yield 'before and after multibyte line' => [ - $multiByteLine, - $multiByteLine, - ]; - - yield 'before and after URL with non-alphanumeric characters' => [ - $nonStandardUrl, - $nonStandardUrl, - ]; - } } From 268f66f4171f98157f9999b2dd2cc545a30341fd Mon Sep 17 00:00:00 2001 From: Volodymyr Shelmuk Date: Mon, 18 Mar 2024 09:13:35 +0200 Subject: [PATCH 5/8] tests: improve AttributesOutputFilter unit test --- .../AttributesOutputFilterTest.php | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/Unit/OutputFilter/AttributesOutputFilterTest.php b/tests/phpunit/Unit/OutputFilter/AttributesOutputFilterTest.php index 896e8f7..f8957c0 100644 --- a/tests/phpunit/Unit/OutputFilter/AttributesOutputFilterTest.php +++ b/tests/phpunit/Unit/OutputFilter/AttributesOutputFilterTest.php @@ -20,9 +20,25 @@ class AttributesOutputFilterTest extends AbstractTestCase { + /** + * phpcs:disable WordPress.PHP.DevelopmentFunctions.prevent_path_disclosure_error_reporting + * phpcs:disable WordPress.PHP.DiscouragedPHPFunctions.runtime_configuration_error_reporting + * phpcs:disable WordPress.PHP.DevelopmentFunctions.error_log_set_error_handler + */ public function testIfTagProcessorIsUnavailable(): void { - $this->expectDeprecation(); + $currentErrorReporting = error_reporting(); + error_reporting($currentErrorReporting | \E_USER_DEPRECATED); + $errorMessages = []; + + set_error_handler( + static function (int $code, string $msg) use (&$errorMessages): bool { + $errorMessages[] = $msg; + return true; + }, + \E_USER_DEPRECATED + ); + $testee = new AttributesOutputFilter(); $stub = \Mockery::mock(Asset::class); @@ -34,5 +50,12 @@ public function testIfTagProcessorIsUnavailable(): void static::assertInstanceOf(AssetOutputFilter::class, $testee); static::assertSame($input, $testee($input, $stub)); + static::assertSame( + 'Adding attributes is not supported for WordPress < 6.2', + $errorMessages[0] + ); + + error_reporting($currentErrorReporting); + // phpcs:enable } } From e04a108c06b46d3231d6473d818b5b8b51965adc Mon Sep 17 00:00:00 2001 From: Philipp Bammes <8144115+tyrann0us@users.noreply.github.com> Date: Thu, 28 Nov 2024 09:38:40 +0100 Subject: [PATCH 6/8] fix: reintroduce whitespace after type casting Signed-off-by: Philipp Bammes <8144115+tyrann0us@users.noreply.github.com> --- src/OutputFilter/AttributesOutputFilter.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/OutputFilter/AttributesOutputFilter.php b/src/OutputFilter/AttributesOutputFilter.php index dc4e87b..20d9eed 100644 --- a/src/OutputFilter/AttributesOutputFilter.php +++ b/src/OutputFilter/AttributesOutputFilter.php @@ -52,7 +52,7 @@ public function __invoke(string $html, Asset $asset): string protected function applyAttributes(\WP_HTML_Tag_Processor $script, array $attributes): void { foreach ($attributes as $key => $value) { - $key = esc_attr((string)$key); + $key = esc_attr((string) $key); if ((string) $script->get_attribute($key)) { continue; } @@ -61,7 +61,7 @@ protected function applyAttributes(\WP_HTML_Tag_Processor $script, array $attrib } $value = is_bool($value) ? esc_attr($key) - : esc_attr((string)$value); + : esc_attr((string) $value); $script->set_attribute($key, $value); } From dd79db6b4910dd3b53af2cd6c23b9f4ffc5a617e Mon Sep 17 00:00:00 2001 From: Volodymyr Shelmuk Date: Thu, 28 Nov 2024 12:08:03 +0200 Subject: [PATCH 7/8] tests: remove integration tests --- composer.json | 3 +- phpunit.xml.dist | 3 - .../AttributesOutputFilterTest.php | 227 ------------------ 3 files changed, 1 insertion(+), 232 deletions(-) delete mode 100644 tests/phpunit/Integration/OutputFilter/AttributesOutputFilterTest.php diff --git a/composer.json b/composer.json index 64543ea..ef68ec2 100644 --- a/composer.json +++ b/composer.json @@ -45,8 +45,7 @@ }, "autoload-dev": { "psr-4": { - "Inpsyde\\Assets\\Tests\\Unit\\": "tests/phpunit/Unit/", - "Inpsyde\\Assets\\Tests\\Integration\\": "tests/phpunit/Integration/" + "Inpsyde\\Assets\\Tests\\Unit\\": "tests/phpunit/Unit/" } }, "scripts": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index ac9394f..5635080 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -16,9 +16,6 @@ tests/phpunit/Unit - - tests/phpunit/Integration - diff --git a/tests/phpunit/Integration/OutputFilter/AttributesOutputFilterTest.php b/tests/phpunit/Integration/OutputFilter/AttributesOutputFilterTest.php deleted file mode 100644 index 81089c6..0000000 --- a/tests/phpunit/Integration/OutputFilter/AttributesOutputFilterTest.php +++ /dev/null @@ -1,227 +0,0 @@ -'; - - static::assertInstanceOf(AssetOutputFilter::class, $testee); - static::assertSame($input, $testee($input, $stub)); - } - - /** - * @dataProvider provideAttributes - */ - public function testRenderWithAttributes(array $attributes, array $expected, array $notExpected): void - { - $stub = new Script('stub-script', 'https://syde.com/foo.js'); - $stub->withAttributes($attributes); - - $input = ''; - - $testee = new AttributesOutputFilter(); - $output = $testee($input, $stub); - - foreach ($expected as $test) { - static::assertStringContainsString($test, $output); - } - foreach ($notExpected as $test) { - static::assertStringNotContainsString($test, $output); - } - } - - /** - * @return \Generator - */ - public function provideAttributes(): \Generator - { - yield 'string value' => [ - [ - 'key' => 'value', - ], - ['key="value"'], - [], - ]; - - yield 'integer value' => [ - [ - 'key' => 1, - ], - ['key="1"'], - [], - ]; - - yield 'bool true value' => [ - [ - 'key' => true, - ], - ['key="key"'], - [], - ]; - - yield 'bool false value' => [ - [ - 'key' => false, - ], - [], - ['key="key"'], - ]; - - yield 'overwriting src-attribute' => [ - [ - 'key' => 'value', - 'src' => 'not-allowed.js', - ], - ['key="value"'], - ['src="not-allowed.js"'], - ]; - } - - public function testRenderNotOverwriteExistingAttributes(): void - { - $expectedKey = 'src'; - $expectedValue = 'foo.js'; - $expectedAttribute = sprintf('%s="%s"', $expectedKey, $expectedValue); - - $stub = new Script('stub-script', 'https://syde.com/foo.js'); - // We're trying to overwrite the "src" with "bar.js". - $stub->withAttributes([$expectedKey => 'bar.js']); - - $input = sprintf('', $expectedAttribute); - - $testee = new AttributesOutputFilter(); - static::assertStringContainsString($expectedAttribute, $testee($input, $stub)); - } - - public function testRenderInlineScriptsNotChanged() - { - $expectedKey = 'key'; - $expectedValue = 'value'; - $expectedAttributes = [$expectedKey => $expectedValue]; - - $expectedBefore = ""; - $expectedAfter = ""; - - $stub = new Script('stub-script', 'https://syde.com/foo.js'); - $stub->withAttributes($expectedAttributes); - - $input = $expectedBefore . '' . $expectedAfter; - - $testee = new AttributesOutputFilter(); - $output = $testee($input, $stub); - static::assertStringContainsString($expectedBefore, $output); - static::assertStringContainsString($expectedAfter, $output); - } - - /** - * @dataProvider provideRenderWithInlineScripts - */ - public function testRenderWithInlineScripts(string $expectedBefore, string $expectedAfter): void - { - $stub = new Script('stub-script', 'https://syde.com/foo.js'); - $stub->withAttributes(['foo' => 'bar']); - - $input = $expectedBefore . '' . $expectedAfter; - - $testee = new AttributesOutputFilter(); - $output = $testee($input, $stub); - static::assertStringContainsString($expectedBefore, $output); - static::assertStringContainsString($expectedAfter, $output); - } - - public function provideRenderWithInlineScripts(): \Generator - { - $singleLineJs = '(function(){ console.log("script with single line"); })();'; - $multiLineJs = << [ - $singleLineJs, - '', - ]; - - yield 'after single line' => [ - '', - $singleLineJs, - ]; - - yield 'before and after single line' => [ - $singleLineJs, - $singleLineJs, - ]; - - yield 'before multi, after single line' => [ - $multiLineJs, - $singleLineJs, - ]; - - yield 'before and after multi line' => [ - $multiLineJs, - $multiLineJs, - ]; - - yield 'before and after multibyte line' => [ - $multiByteLine, - $multiByteLine, - ]; - - yield 'before and after URL with non-alphanumeric characters' => [ - $nonStandardUrl, - $nonStandardUrl, - ]; - } -} From d9276a5781bd05f1013c5ff1f783e7d25a343420 Mon Sep 17 00:00:00 2001 From: Volodymyr Shelmuk Date: Thu, 28 Nov 2024 12:11:17 +0200 Subject: [PATCH 8/8] fix: remove deprecation message about unsupported WordPress version --- src/OutputFilter/AttributesOutputFilter.php | 13 +--- .../AttributesOutputFilterTest.php | 61 ------------------- 2 files changed, 1 insertion(+), 73 deletions(-) delete mode 100644 tests/phpunit/Unit/OutputFilter/AttributesOutputFilterTest.php diff --git a/src/OutputFilter/AttributesOutputFilter.php b/src/OutputFilter/AttributesOutputFilter.php index dc4e87b..f22cece 100644 --- a/src/OutputFilter/AttributesOutputFilter.php +++ b/src/OutputFilter/AttributesOutputFilter.php @@ -20,18 +20,7 @@ class AttributesOutputFilter implements AssetOutputFilter public function __invoke(string $html, Asset $asset): string { $attributes = $asset->attributes(); - if (count($attributes) === 0) { - return $html; - } - - if (!class_exists(\WP_HTML_Tag_Processor::class)) { - // phpcs:disable WordPress.PHP.DevelopmentFunctions.error_log_trigger_error - trigger_error( - 'Adding attributes is not supported for WordPress < 6.2', - \E_USER_DEPRECATED - ); - // phpcs:enable WordPress.PHP.DevelopmentFunctions.error_log_trigger_error - + if (!class_exists(\WP_HTML_Tag_Processor::class) || count($attributes) === 0) { return $html; } diff --git a/tests/phpunit/Unit/OutputFilter/AttributesOutputFilterTest.php b/tests/phpunit/Unit/OutputFilter/AttributesOutputFilterTest.php deleted file mode 100644 index f8957c0..0000000 --- a/tests/phpunit/Unit/OutputFilter/AttributesOutputFilterTest.php +++ /dev/null @@ -1,61 +0,0 @@ -expects('attributes')->andReturn([ - 'key' => 'value', - ]); - - $input = ''; - - static::assertInstanceOf(AssetOutputFilter::class, $testee); - static::assertSame($input, $testee($input, $stub)); - static::assertSame( - 'Adding attributes is not supported for WordPress < 6.2', - $errorMessages[0] - ); - - error_reporting($currentErrorReporting); - // phpcs:enable - } -}