Skip to content

Commit bdba35b

Browse files
Tests: Update scripts tests to use semantic HTML comparison.
This aims to make the tests more robust. Follow-up to [50167], [60295], [61391], [61392]. Props jonsurrell. See #64225. git-svn-id: https://develop.svn.wordpress.org/trunk@61394 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 44f1517 commit bdba35b

File tree

2 files changed

+54
-19
lines changed

2 files changed

+54
-19
lines changed

tests/phpunit/tests/dependencies/scripts.php

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,44 @@ public function tear_down() {
6969
parent::tear_down();
7070
}
7171

72+
/**
73+
* Asserts that two HTML SCRIPT tags are semantically equal within a larger HTML text.
74+
*
75+
* The expected string should contain a single SCRIPT tag with an ID attribute. This ID will
76+
* be used to locate the corresponding SCRIPT tag within the provided HTML.
77+
*
78+
* The provided HTML will be traversed to locate the SCRIPT tag with the matcing ID.
79+
*
80+
* These two tags will be compared for semantic equality of their HTML.
81+
*
82+
* @since 7.0.0
83+
*
84+
* @param string $expected The expected SCRIPT tag HTML.
85+
* @param string $html The HTML to search within.
86+
* @param string $message Optional. Message to display upon failure. Default 'The SCRIPT tag did not match.'.
87+
*/
88+
private function assertEqualHTMLScriptTagById( string $expected, string $html, string $message = 'The SCRIPT tag did not match.' ) {
89+
$find_id_tag_processor = new WP_HTML_Tag_Processor( $expected );
90+
$find_id_tag_processor->next_token();
91+
$id = $find_id_tag_processor->get_attribute( 'id' );
92+
assert( is_string( $id ) );
93+
94+
$processor = ( new class('', WP_HTML_Processor::CONSTRUCTOR_UNLOCK_CODE ) extends WP_HTML_Processor {
95+
public function get_script_html() {
96+
assert( 'SCRIPT' === $this->get_tag() );
97+
$this->set_bookmark( 'here' );
98+
$span = $this->bookmarks['_here'];
99+
return substr( $this->html, $span->start, $span->length );
100+
}
101+
} )::create_fragment( $html );
102+
103+
while ( $processor->next_tag( 'SCRIPT' ) && $processor->get_attribute( 'id' ) !== $id ) {
104+
// Loop until we find the right script tag.
105+
}
106+
$this->assertSame( 'SCRIPT', $processor->get_tag(), "Matching tag `script#{$id}` could not be found." );
107+
$this->assertEqualHTML( $expected, $processor->get_script_html(), '<body>', $message );
108+
}
109+
72110
/**
73111
* Test versioning
74112
*
@@ -1558,15 +1596,13 @@ public function test_loading_strategy_with_valid_blocking_registration() {
15581596
wp_enqueue_script( 'main-script-b1', '/main-script-b1.js', array(), null );
15591597
$output = get_echo( 'wp_print_scripts' );
15601598
$expected = "<script type='text/javascript' src='/main-script-b1.js' id='main-script-b1-js'></script>\n";
1561-
$expected = str_replace( "'", '"', $expected );
1562-
$this->assertSame( $expected, $output, 'Scripts registered with a "blocking" strategy, and who have no dependencies, should have no loading strategy attributes printed.' );
1599+
$this->assertEqualHTML( $expected, $output, '<body>', 'Scripts registered with a "blocking" strategy, and who have no dependencies, should have no loading strategy attributes printed.' );
15631600

15641601
// strategy args not set.
15651602
wp_enqueue_script( 'main-script-b2', '/main-script-b2.js', array(), null, array() );
15661603
$output = get_echo( 'wp_print_scripts' );
15671604
$expected = "<script type='text/javascript' src='/main-script-b2.js' id='main-script-b2-js'></script>\n";
1568-
$expected = str_replace( "'", '"', $expected );
1569-
$this->assertSame( $expected, $output, 'Scripts registered with no strategy assigned, and who have no dependencies, should have no loading strategy attributes printed.' );
1605+
$this->assertEqualHTML( $expected, $output, '<body>', 'Scripts registered with no strategy assigned, and who have no dependencies, should have no loading strategy attributes printed.' );
15701606
}
15711607

15721608
/**
@@ -2616,14 +2652,6 @@ public function test_wp_add_inline_script_customize_dependency() {
26162652
$wp_scripts->base_url = '';
26172653
$wp_scripts->do_concat = true;
26182654

2619-
$expected_tail = "<script type='text/javascript' src='/customize-dependency.js' id='customize-dependency-js'></script>\n";
2620-
$expected_tail .= "<script type='text/javascript' id='customize-dependency-js-after'>\n";
2621-
$expected_tail .= "/* <![CDATA[ */\n";
2622-
$expected_tail .= "tryCustomizeDependency()\n";
2623-
$expected_tail .= "//# sourceURL=customize-dependency-js-after\n";
2624-
$expected_tail .= "/* ]]> */\n";
2625-
$expected_tail .= "</script>\n";
2626-
26272655
$handle = 'customize-dependency';
26282656
wp_enqueue_script( $handle, '/customize-dependency.js', array( 'customize-controls' ), null );
26292657
wp_add_inline_script( $handle, 'tryCustomizeDependency()' );
@@ -2635,9 +2663,16 @@ public function test_wp_add_inline_script_customize_dependency() {
26352663
_print_scripts();
26362664
$print_scripts = $this->getActualOutput();
26372665

2638-
$tail = substr( $print_scripts, strrpos( $print_scripts, '<script type="text/javascript" src="/customize-dependency.js" id="customize-dependency-js">' ) );
2666+
$expected = "<script type='text/javascript' src='/customize-dependency.js' id='customize-dependency-js'></script>\n";
2667+
$this->assertEqualHTMLScriptTagById( $expected, $print_scripts );
26392668

2640-
$this->assertEqualHTML( $expected_tail, $tail );
2669+
$expected = "<script type='text/javascript' id='customize-dependency-js-after'>\n";
2670+
$expected .= "/* <![CDATA[ */\n";
2671+
$expected .= "tryCustomizeDependency()\n";
2672+
$expected .= "//# sourceURL=customize-dependency-js-after\n";
2673+
$expected .= "/* ]]> */\n";
2674+
$expected .= "</script>\n";
2675+
$this->assertEqualHTMLScriptTagById( $expected, $print_scripts );
26412676
}
26422677

26432678
/**

tests/phpunit/tests/dependencies/wpScriptTag.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Tests_Functions_wpScriptTag extends WP_UnitTestCase {
1111
public function get_script_tag_type_set() {
1212
add_theme_support( 'html5', array( 'script' ) );
1313

14-
$this->assertSame(
14+
$this->assertEqualHTML(
1515
'<script src="https://localhost/PATH/FILE.js" type="application/javascript" nomodule></script>' . "\n",
1616
wp_get_script_tag(
1717
array(
@@ -25,7 +25,7 @@ public function get_script_tag_type_set() {
2525

2626
remove_theme_support( 'html5' );
2727

28-
$this->assertSame(
28+
$this->assertEqualHTML(
2929
'<script src="https://localhost/PATH/FILE.js" type="application/javascript" nomodule></script>' . "\n",
3030
wp_get_script_tag(
3131
array(
@@ -44,7 +44,7 @@ public function get_script_tag_type_set() {
4444
public function test_get_script_tag_type_not_set() {
4545
add_theme_support( 'html5', array( 'script' ) );
4646

47-
$this->assertSame(
47+
$this->assertEqualHTML(
4848
'<script src="https://localhost/PATH/FILE.js" nomodule></script>' . "\n",
4949
wp_get_script_tag(
5050
array(
@@ -80,7 +80,7 @@ static function ( $attributes ) {
8080
'nomodule' => true,
8181
);
8282

83-
$this->assertSame(
83+
$this->assertEqualHTML(
8484
wp_get_script_tag( $attributes ),
8585
get_echo(
8686
'wp_print_script_tag',
@@ -90,7 +90,7 @@ static function ( $attributes ) {
9090

9191
remove_theme_support( 'html5' );
9292

93-
$this->assertSame(
93+
$this->assertEqualHTML(
9494
wp_get_script_tag( $attributes ),
9595
get_echo(
9696
'wp_print_script_tag',

0 commit comments

Comments
 (0)