Make WordPress Core

Changeset 61394


Ignore:
Timestamp:
12/19/2025 11:16:42 PM (4 months ago)
Author:
SergeyBiryukov
Message:

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.

Location:
trunk/tests/phpunit/tests/dependencies
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/dependencies/scripts.php

    r61323 r61394  
    7171
    7272    /**
     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    /**
    73111     * Test versioning
    74112     *
     
    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.
     
    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
     
    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 );
     
    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">' ) );
    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 );
    26412676    }
    26422677
  • trunk/tests/phpunit/tests/dependencies/wpScriptTag.php

    r52010 r61394  
    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(
     
    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(
     
    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(
     
    8181        );
    8282
    83         $this->assertSame(
     83        $this->assertEqualHTML(
    8484            wp_get_script_tag( $attributes ),
    8585            get_echo(
     
    9191        remove_theme_support( 'html5' );
    9292
    93         $this->assertSame(
     93        $this->assertEqualHTML(
    9494            wp_get_script_tag( $attributes ),
    9595            get_echo(
Note: See TracChangeset for help on using the changeset viewer.