diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index a3c8b92695f4f..16cda9fe655cd 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -69,6 +69,42 @@ 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(); + $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 * @@ -1558,15 +1594,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.' ); } /** @@ -2616,14 +2650,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 ); wp_add_inline_script( $handle, 'tryCustomizeDependency()' ); @@ -2635,9 +2661,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 ); } /** 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',