Skip to content

Commit 06b5f6e

Browse files
committed
Add test case for classic scripts
1 parent 49721aa commit 06b5f6e

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

src/wp-includes/class-wp-scripts.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ private function filter_eligible_strategies( $handle, $eligible_strategies = nul
10921092
*
10931093
* @since 6.9.0
10941094
* @see self::filter_eligible_strategies()
1095-
* @see WP_Script_Modules::get_highest_fetchpriority_with_dependents()
1095+
* @see WP_Script_Modules::get_highest_fetchpriority()
10961096
*
10971097
* @param string $handle Script module ID.
10981098
* @param array<string, true> $checked Optional. An array of already checked script handles, used to avoid recursive loops.

tests/phpunit/tests/dependencies/scripts.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,6 +1516,30 @@ public function test_highest_fetchpriority_with_dependents_uses_cached_result()
15161516
);
15171517
}
15181518

1519+
/**
1520+
* Tests expected priority is used when a dependent is registered but not enqueued.
1521+
*
1522+
* @ticket 64429
1523+
*
1524+
* @covers WP_Scripts::print_scripts
1525+
* @covers WP_Scripts::get_highest_fetchpriority_with_dependents
1526+
*/
1527+
public function test_priority_of_dependency_for_non_enqueued_dependent() {
1528+
$wp_scripts = wp_scripts();
1529+
wp_default_scripts( $wp_scripts );
1530+
1531+
$wp_scripts->add( 'not-enqueued', 'https://example.com/not-enqueued.js', array( 'comment-reply' ), null, array( 'priority' => 'high' ) );
1532+
$wp_scripts->enqueue( 'comment-reply' );
1533+
1534+
$actual = $this->normalize_markup_for_snapshot( get_echo( array( $wp_scripts, 'print_scripts' ) ) );
1535+
$this->assertEqualHTML(
1536+
'<script type="text/javascript" src="/wp-includes/js/comment-reply.js" id="comment-reply-js" async="async" data-wp-strategy="async" fetchpriority="low"></script>',
1537+
$actual,
1538+
'<body>',
1539+
"Snapshot:\n$actual"
1540+
);
1541+
}
1542+
15191543
/**
15201544
* Tests that printing a script without enqueueing has the same output as when it is enqueued.
15211545
*
@@ -4256,4 +4280,26 @@ public function data_varying_versions_handle_args() {
42564280
),
42574281
);
42584282
}
4283+
4284+
/**
4285+
* Normalizes markup for snapshot.
4286+
*
4287+
* @param string $markup Markup.
4288+
* @return string Normalized markup.
4289+
*/
4290+
private function normalize_markup_for_snapshot( string $markup ): string {
4291+
$processor = new WP_HTML_Tag_Processor( $markup );
4292+
$clean_url = static function ( string $url ): string {
4293+
$url = preg_replace( '#^https?://[^/]+#', '', $url );
4294+
return remove_query_arg( 'ver', $url );
4295+
};
4296+
while ( $processor->next_tag() ) {
4297+
if ( 'LINK' === $processor->get_tag() && is_string( $processor->get_attribute( 'href' ) ) ) {
4298+
$processor->set_attribute( 'href', $clean_url( $processor->get_attribute( 'href' ) ) );
4299+
} elseif ( 'SCRIPT' === $processor->get_tag() && is_string( $processor->get_attribute( 'src' ) ) ) {
4300+
$processor->set_attribute( 'src', $clean_url( $processor->get_attribute( 'src' ) ) );
4301+
}
4302+
}
4303+
return $processor->get_updated_html();
4304+
}
42594305
}

tests/phpunit/tests/script-modules/wpScriptModules.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1864,12 +1864,12 @@ public function test_priority_of_dependency_for_non_enqueued_dependent() {
18641864
wp_register_script_module( 'not-enqueued', 'https://example.com/not-enqueued.js', array( '@wordpress/a11y' ), null, array( 'priority' => 'high' ) );
18651865
wp_enqueue_script_module( '@wordpress/a11y' );
18661866

1867-
$actual_script_modules = $this->normalize_markup_for_snapshot( get_echo( array( wp_script_modules(), 'print_enqueued_script_modules' ) ) );
1867+
$actual = $this->normalize_markup_for_snapshot( get_echo( array( wp_script_modules(), 'print_enqueued_script_modules' ) ) );
18681868
$this->assertEqualHTML(
18691869
'<script type="module" src="/wp-includes/js/dist/script-modules/a11y/index.min.js" id="@wordpress/a11y-js-module" fetchpriority="low"></script>',
1870-
$actual_script_modules,
1870+
$actual,
18711871
'<body>',
1872-
"Snapshot:\n$actual_script_modules"
1872+
"Snapshot:\n$actual"
18731873
);
18741874
}
18751875

0 commit comments

Comments
 (0)