Changeset 61394
- Timestamp:
- 12/19/2025 11:16:42 PM (4 months ago)
- Location:
- trunk/tests/phpunit/tests/dependencies
- Files:
-
- 2 edited
-
scripts.php (modified) (5 diffs)
-
wpScriptTag.php (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/dependencies/scripts.php
r61323 r61394 71 71 72 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 110 /** 73 111 * Test versioning 74 112 * … … 1559 1597 $output = get_echo( 'wp_print_scripts' ); 1560 1598 $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.' ); 1563 1600 1564 1601 // strategy args not set. … … 1566 1603 $output = get_echo( 'wp_print_scripts' ); 1567 1604 $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.' ); 1570 1606 } 1571 1607 … … 2617 2653 $wp_scripts->do_concat = true; 2618 2654 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 2627 2655 $handle = 'customize-dependency'; 2628 2656 wp_enqueue_script( $handle, '/customize-dependency.js', array( 'customize-controls' ), null ); … … 2636 2664 $print_scripts = $this->getActualOutput(); 2637 2665 2638 $tail = substr( $print_scripts, strrpos( $print_scripts, '<script type="text/javascript" src="/customize-dependency.js" id="customize-dependency-js">' ) ); 2639 2640 $this->assertEqualHTML( $expected_tail, $tail ); 2666 $expected = "<script type='text/javascript' src='/customize-dependency.js' id='customize-dependency-js'></script>\n"; 2667 $this->assertEqualHTMLScriptTagById( $expected, $print_scripts ); 2668 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 ); 2641 2676 } 2642 2677 -
trunk/tests/phpunit/tests/dependencies/wpScriptTag.php
r52010 r61394 12 12 add_theme_support( 'html5', array( 'script' ) ); 13 13 14 $this->assert Same(14 $this->assertEqualHTML( 15 15 '<script src="https://localhost/PATH/FILE.js" type="application/javascript" nomodule></script>' . "\n", 16 16 wp_get_script_tag( … … 26 26 remove_theme_support( 'html5' ); 27 27 28 $this->assert Same(28 $this->assertEqualHTML( 29 29 '<script src="https://localhost/PATH/FILE.js" type="application/javascript" nomodule></script>' . "\n", 30 30 wp_get_script_tag( … … 45 45 add_theme_support( 'html5', array( 'script' ) ); 46 46 47 $this->assert Same(47 $this->assertEqualHTML( 48 48 '<script src="https://localhost/PATH/FILE.js" nomodule></script>' . "\n", 49 49 wp_get_script_tag( … … 81 81 ); 82 82 83 $this->assert Same(83 $this->assertEqualHTML( 84 84 wp_get_script_tag( $attributes ), 85 85 get_echo( … … 91 91 remove_theme_support( 'html5' ); 92 92 93 $this->assert Same(93 $this->assertEqualHTML( 94 94 wp_get_script_tag( $attributes ), 95 95 get_echo(
Note: See TracChangeset
for help on using the changeset viewer.