From 967abef19eb6d3f6c39e5c7d068b017ff597391d Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Mon, 17 Jul 2023 14:04:00 +0000 Subject: [PATCH] Script Loader: Improve test coverage for `wp_print_scripts()`. This is a follow-up to [56092], which further improves PHPUnit test coverage and inline docs for ensuring `async` and `defer` attributes are being properly handled for scripts that are printed without being enqueued. Props peterwilsoncc, azaozz, westonruter, joemcgill. See #58648. git-svn-id: https://develop.svn.wordpress.org/trunk@56246 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-scripts.php | 5 ++++- tests/phpunit/tests/dependencies/scripts.php | 19 ++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 0c85201bad..08aba0aa5d 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -914,7 +914,10 @@ JS; return ''; } - // If the intended strategy is 'defer', limit the initial list of eligibles. + /* + * If the intended strategy is 'defer', limit the initial list of eligible + * strategies, since 'async' can fallback to 'defer', but not vice-versa. + */ $initial = ( 'defer' === $intended ) ? array( 'defer' ) : null; $eligible = $this->filter_eligible_strategies( $handle, $initial ); diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index b3ff0ad123..ca88433713 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -2943,7 +2943,24 @@ HTML $actual = get_echo( 'wp_print_scripts', array( array( 'wp-tinymce' ) ) ); - $this->assertStringNotContainsString( 'async', $actual ); + $this->assertStringNotContainsString( 'async', $actual, 'TinyMCE should not have an async attribute.' ); + $this->assertStringNotContainsString( 'defer', $actual, 'TinyMCE should not have a defer attribute.' ); + } + + /** + * Make sure scripts with a loading strategy that are printed + * without being enqueued are handled properly. + * + * @ticket 58648 + * + * @dataProvider data_provider_delayed_strategies + */ + public function test_printing_non_enqueued_scripts( $strategy ) { + wp_register_script( 'test-script', 'test-script.js', array(), false, array( 'strategy' => $strategy ) ); + + $actual = get_echo( 'wp_print_scripts', array( array( 'test-script' ) ) ); + + $this->assertStringContainsString( $strategy, $actual ); } /**