Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 47 additions & 14 deletions tests/phpunit/tests/dependencies/scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(), '<body>', $message );
}

/**
* Test versioning
*
Expand Down Expand Up @@ -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 = "<script type='text/javascript' src='/main-script-b1.js' id='main-script-b1-js'></script>\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, '<body>', '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 = "<script type='text/javascript' src='/main-script-b2.js' id='main-script-b2-js'></script>\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, '<body>', 'Scripts registered with no strategy assigned, and who have no dependencies, should have no loading strategy attributes printed.' );
}

/**
Expand Down Expand Up @@ -2616,14 +2650,6 @@ public function test_wp_add_inline_script_customize_dependency() {
$wp_scripts->base_url = '';
$wp_scripts->do_concat = true;

$expected_tail = "<script type='text/javascript' src='/customize-dependency.js' id='customize-dependency-js'></script>\n";
$expected_tail .= "<script type='text/javascript' id='customize-dependency-js-after'>\n";
$expected_tail .= "/* <![CDATA[ */\n";
$expected_tail .= "tryCustomizeDependency()\n";
$expected_tail .= "//# sourceURL=customize-dependency-js-after\n";
$expected_tail .= "/* ]]> */\n";
$expected_tail .= "</script>\n";

$handle = 'customize-dependency';
wp_enqueue_script( $handle, '/customize-dependency.js', array( 'customize-controls' ), null );
wp_add_inline_script( $handle, 'tryCustomizeDependency()' );
Expand All @@ -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, '<script type="text/javascript" src="/customize-dependency.js" id="customize-dependency-js">' ) );
$expected = "<script type='text/javascript' src='/customize-dependency.js' id='customize-dependency-js'></script>\n";
$this->assertEqualHTMLScriptTagById( $expected, $print_scripts );

$this->assertEqualHTML( $expected_tail, $tail );
$expected = "<script type='text/javascript' id='customize-dependency-js-after'>\n";
$expected .= "/* <![CDATA[ */\n";
$expected .= "tryCustomizeDependency()\n";
$expected .= "//# sourceURL=customize-dependency-js-after\n";
$expected .= "/* ]]> */\n";
$expected .= "</script>\n";
$this->assertEqualHTMLScriptTagById( $expected, $print_scripts );
}

/**
Expand Down
10 changes: 5 additions & 5 deletions tests/phpunit/tests/dependencies/wpScriptTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'<script src="https://localhost/PATH/FILE.js" type="application/javascript" nomodule></script>' . "\n",
wp_get_script_tag(
array(
Expand All @@ -25,7 +25,7 @@ public function get_script_tag_type_set() {

remove_theme_support( 'html5' );

$this->assertSame(
$this->assertEqualHTML(
'<script src="https://localhost/PATH/FILE.js" type="application/javascript" nomodule></script>' . "\n",
wp_get_script_tag(
array(
Expand All @@ -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(
'<script src="https://localhost/PATH/FILE.js" nomodule></script>' . "\n",
wp_get_script_tag(
array(
Expand Down Expand Up @@ -80,7 +80,7 @@ static function ( $attributes ) {
'nomodule' => true,
);

$this->assertSame(
$this->assertEqualHTML(
wp_get_script_tag( $attributes ),
get_echo(
'wp_print_script_tag',
Expand All @@ -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',
Expand Down
Loading