From b41f760025d5b967c1e5ed5d063ff44f6af1a2a5 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 19 Dec 2025 22:19:57 +0100 Subject: [PATCH 1/6] Add utility for semantically comparing a SCRIPT tag within HTML --- tests/phpunit/tests/dependencies/scripts.php | 22 ++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index a3c8b92695f4f..81a8d8a7e84b8 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -69,6 +69,28 @@ public function tear_down() { parent::tear_down(); } + private function assertEqualHTMLScriptTagById( string $expected, string $html, ?string $message ) { + $find_id_tag_processor = new WP_HTML_Tag_Processor( $expected ); + $find_id_tag_processor->next_token(); + $id = $find_id_tag_processor->get_attribute( 'id' ); + assert( is_string( $id ) ); + + $processor = ( new class('', WP_HTML_Processor::CONSTRUCTOR_UNLOCK_CODE ) extends WP_HTML_Processor { + public function get_script_html() { + assert( 'SCRIPT' === $this->get_tag() ); + $this->set_bookmark( 'here' ); + $span = $this->bookmarks['_here']; + return substr( $this->html, $span->start, $span->length ); + } + } )::create_fragment( $html ); + + while ( $processor->next_tag( 'SCRIPT' ) && $processor->get_attribute( 'id' ) !== $id ) { + // Loop until we find the right script tag. + } + $this->assertSame( 'SCRIPT', $processor->get_tag(), "Matching tag `script#{$id}` could not be found." ); + $this->assertEqualHTML( $expected, $processor->get_script_html(), '', $message ); + } + /** * Test versioning * From 3406b9700c91ba16d505921a7226ccb9fa836cac Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 19 Dec 2025 22:29:19 +0100 Subject: [PATCH 2/6] fixup! Use script tag comparison in tests --- tests/phpunit/tests/dependencies/scripts.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 81a8d8a7e84b8..949b93e5269e1 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -2638,13 +2638,6 @@ public function test_wp_add_inline_script_customize_dependency() { $wp_scripts->base_url = ''; $wp_scripts->do_concat = true; - $expected_tail = "\n"; - $expected_tail .= "\n"; $handle = 'customize-dependency'; wp_enqueue_script( $handle, '/customize-dependency.js', array( 'customize-controls' ), null ); @@ -2657,9 +2650,16 @@ public function test_wp_add_inline_script_customize_dependency() { _print_scripts(); $print_scripts = $this->getActualOutput(); - $tail = substr( $print_scripts, strrpos( $print_scripts, '\n"; + $this->assertEqualHTMLScriptTagById( $expected, $print_scripts ); - $this->assertEqualHTML( $expected_tail, $tail ); + $expected = "\n"; + $this->assertEqualHTMLScriptTagById( $expected, $print_scripts ); } /** From 51006c60d7f52c761a5e25b18c7cd561630ba251 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 19 Dec 2025 22:29:31 +0100 Subject: [PATCH 3/6] fixup! Add utility for semantically comparing a SCRIPT tag within HTML --- tests/phpunit/tests/dependencies/scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 949b93e5269e1..fe6bea3a64164 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -69,7 +69,7 @@ public function tear_down() { parent::tear_down(); } - private function assertEqualHTMLScriptTagById( string $expected, string $html, ?string $message ) { + private function assertEqualHTMLScriptTagById( string $expected, string $html, string $message = 'The SCRIPT tag did not match.' ) { $find_id_tag_processor = new WP_HTML_Tag_Processor( $expected ); $find_id_tag_processor->next_token(); $id = $find_id_tag_processor->get_attribute( 'id' ); From 045b1f2c4cc137255731d09398fc7981002c3d5d Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 19 Dec 2025 21:17:56 +0100 Subject: [PATCH 4/6] Switch to assertEqualHTML assertions --- tests/phpunit/tests/dependencies/scripts.php | 6 ++---- tests/phpunit/tests/dependencies/wpScriptTag.php | 10 +++++----- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index fe6bea3a64164..eeb2d30ac46f0 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -1580,15 +1580,13 @@ public function test_loading_strategy_with_valid_blocking_registration() { wp_enqueue_script( 'main-script-b1', '/main-script-b1.js', array(), null ); $output = get_echo( 'wp_print_scripts' ); $expected = "\n"; - $expected = str_replace( "'", '"', $expected ); - $this->assertSame( $expected, $output, 'Scripts registered with a "blocking" strategy, and who have no dependencies, should have no loading strategy attributes printed.' ); + $this->assertEqualHTML( $expected, $output, '', 'Scripts registered with a "blocking" strategy, and who have no dependencies, should have no loading strategy attributes printed.' ); // strategy args not set. wp_enqueue_script( 'main-script-b2', '/main-script-b2.js', array(), null, array() ); $output = get_echo( 'wp_print_scripts' ); $expected = "\n"; - $expected = str_replace( "'", '"', $expected ); - $this->assertSame( $expected, $output, 'Scripts registered with no strategy assigned, and who have no dependencies, should have no loading strategy attributes printed.' ); + $this->assertEqualHTML( $expected, $output, '', 'Scripts registered with no strategy assigned, and who have no dependencies, should have no loading strategy attributes printed.' ); } /** diff --git a/tests/phpunit/tests/dependencies/wpScriptTag.php b/tests/phpunit/tests/dependencies/wpScriptTag.php index ee273f8fb3687..ea963a383b29e 100644 --- a/tests/phpunit/tests/dependencies/wpScriptTag.php +++ b/tests/phpunit/tests/dependencies/wpScriptTag.php @@ -11,7 +11,7 @@ class Tests_Functions_wpScriptTag extends WP_UnitTestCase { public function get_script_tag_type_set() { add_theme_support( 'html5', array( 'script' ) ); - $this->assertSame( + $this->assertEqualHTML( '' . "\n", wp_get_script_tag( array( @@ -25,7 +25,7 @@ public function get_script_tag_type_set() { remove_theme_support( 'html5' ); - $this->assertSame( + $this->assertEqualHTML( '' . "\n", wp_get_script_tag( array( @@ -44,7 +44,7 @@ public function get_script_tag_type_set() { public function test_get_script_tag_type_not_set() { add_theme_support( 'html5', array( 'script' ) ); - $this->assertSame( + $this->assertEqualHTML( '' . "\n", wp_get_script_tag( array( @@ -80,7 +80,7 @@ static function ( $attributes ) { 'nomodule' => true, ); - $this->assertSame( + $this->assertEqualHTML( wp_get_script_tag( $attributes ), get_echo( 'wp_print_script_tag', @@ -90,7 +90,7 @@ static function ( $attributes ) { remove_theme_support( 'html5' ); - $this->assertSame( + $this->assertEqualHTML( wp_get_script_tag( $attributes ), get_echo( 'wp_print_script_tag', From 42bce382abe97409eb7b90297601b0feac01c409 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 19 Dec 2025 22:33:07 +0100 Subject: [PATCH 5/6] lint --- tests/phpunit/tests/dependencies/scripts.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index eeb2d30ac46f0..5b813bcdf69f5 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -2636,7 +2636,6 @@ public function test_wp_add_inline_script_customize_dependency() { $wp_scripts->base_url = ''; $wp_scripts->do_concat = true; - $handle = 'customize-dependency'; wp_enqueue_script( $handle, '/customize-dependency.js', array( 'customize-controls' ), null ); wp_add_inline_script( $handle, 'tryCustomizeDependency()' ); From 4e0655922cc2a037d5c37c0294c2c26ade9f5ec0 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 19 Dec 2025 22:38:58 +0100 Subject: [PATCH 6/6] Document new assertion helper --- tests/phpunit/tests/dependencies/scripts.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 5b813bcdf69f5..16cda9fe655cd 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -69,6 +69,20 @@ public function tear_down() { parent::tear_down(); } + /** + * Asserts that two HTML SCRIPT tags are semantically equal within a larger HTML text. + * + * The expected string should contain a single SCRIPT tag with an ID attribute. This ID will + * be used to locate the corresponding SCRIPT tag within the provided HTML. + * + * The provided HTML will be traversed to locate the SCRIPT tag with the matcing ID. + * + * These two tags will be compared for semantic equality of their HTML. + * + * @param string $expected The expected SCRIPT tag HTML. + * @param string $html The HTML to search within. + * @param string $message Optional. Message to display upon failure. Default 'The SCRIPT tag did not match.'. + */ private function assertEqualHTMLScriptTagById( string $expected, string $html, string $message = 'The SCRIPT tag did not match.' ) { $find_id_tag_processor = new WP_HTML_Tag_Processor( $expected ); $find_id_tag_processor->next_token();