From 94edf361896d4e6e224d5215ebb77d89843a60d6 Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Mon, 20 Mar 2023 16:16:22 +0000 Subject: [PATCH] Tests: Add test class for `wp_script_is()`. Changes: * Adds a test class for `wp_script_is()`. * Moves `WP_Dependencies_jQuery::test_wp_script_is_dep_enqueued()` into this test class and splits it into 2 separate tests, happy and unhappy paths. * Adds `WP_Scripts::query` `@covers`, as `wp_script_is()` is helper function for it. * Relocates the global resetting from the test method to the `clean_up_global_scope()` method. Follow-up to [52010], [29252]. Props hellofromTonya, antonvlasenko. See #57841, #57958. git-svn-id: https://develop.svn.wordpress.org/trunk@55565 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/dependencies/jquery.php | 14 -- .../phpunit/tests/dependencies/wpScriptIs.php | 232 ++++++++++++++++++ 2 files changed, 232 insertions(+), 14 deletions(-) create mode 100644 tests/phpunit/tests/dependencies/wpScriptIs.php diff --git a/tests/phpunit/tests/dependencies/jquery.php b/tests/phpunit/tests/dependencies/jquery.php index aa44b0047a..8e332484e0 100644 --- a/tests/phpunit/tests/dependencies/jquery.php +++ b/tests/phpunit/tests/dependencies/jquery.php @@ -84,20 +84,6 @@ class Tests_Dependencies_jQuery extends WP_UnitTestCase { } } - /** - * @ticket 28404 - * - * @covers ::wp_script_is - */ - public function test_wp_script_is_dep_enqueued() { - wp_enqueue_script( 'jquery-ui-accordion' ); - - $this->assertTrue( wp_script_is( 'jquery', 'enqueued' ) ); - $this->assertFalse( wp_script_is( 'underscore', 'enqueued' ) ); - - unset( $GLOBALS['wp_scripts'] ); - } - /** * Test placing of jQuery in footer. * diff --git a/tests/phpunit/tests/dependencies/wpScriptIs.php b/tests/phpunit/tests/dependencies/wpScriptIs.php new file mode 100644 index 0000000000..f2783e4396 --- /dev/null +++ b/tests/phpunit/tests/dependencies/wpScriptIs.php @@ -0,0 +1,232 @@ +assertTrue( wp_script_is( $handle, 'registered' ) ); + } + + /** + * @dataProvider data_script_handles + * + * @param string $handle Script handle to test. + */ + public function test_script_is_enqueued( $handle ) { + // Test set up. + wp_enqueue_script( $handle ); + + $this->assertTrue( wp_script_is( $handle ), "Script `{$handle}` should be enqueued after invoking wp_enqueue_script()" ); + } + + /** + * @dataProvider data_script_handles + * + * @param string $handle Script handle to test. + */ + public function test_script_is_not_enqueued( $handle ) { + $this->assertFalse( wp_script_is( $handle ), "Script `{$handle}` should not be enqueued when test starts" ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_script_handles() { + return array( + array( 'heartbeat' ), + array( 'jquery' ), + array( 'wp-lists' ), + array( 'wp-pointer' ), + array( 'thickbox' ), + ); + } + + /** + * @ticket 28404 + * + * @dataProvider data_deps_are_enqueued + * + * @param string $handle Script handle. + * @param string[] $deps The deps to test for the given script handle. + */ + public function test_deps_are_enqueued( $handle, $deps ) { + // Check the deps are not enqueued before enqueuing. + $this->assertFalse( wp_script_is( $handle ), 'Script `jquery-ui-accordion` should not be enqueued when test starts' ); + foreach ( $deps as $dep_handle ) { + $this->assertFalse( wp_script_is( $dep_handle ), "Dependency `{$dep_handle}` should not be enqueued when test starts" ); + } + + // Test set up. + wp_enqueue_script( $handle ); + + foreach ( $deps as $dep_handle ) { + $this->assertTrue( wp_script_is( $dep_handle ), "Dependency `{$dep_handle}` should be enqueued" ); + } + + $this->assertFalse( wp_script_is( 'underscore' ), 'Script "underscore" is not a dependency of "jquery-ui-accordion" and should not be enqueued' ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_deps_are_enqueued() { + return array( + 'jquery: 1 level of deps' => array( + 'handle' => 'jquery', + 'deps' => array( + 'jquery-core', + 'jquery-migrate', + ), + ), + 'mediaelement: 1 level of deps' => array( + 'handle' => 'mediaelement', + 'deps' => array( + 'mediaelement-core', + 'mediaelement-migrate', + ), + ), + 'jquery-effects-core: 2 levels of deps' => array( + 'handle' => 'jquery-effects-core', + 'deps' => array( + // Dep to 'jquery-effects-core'. + 'jquery', + // Deps to 'jquery'. + 'jquery-core', + 'jquery-migrate', + ), + ), + 'jquery-ui-accordion: 3 levels of deps' => array( + 'handle' => 'jquery-ui-accordion', + 'deps' => array( + // Dep to 'jquery-ui-accordion'. + 'jquery-ui-core', + // Dep to 'jquery-ui-core'. + 'jquery', + // Deps to 'jquery'. + 'jquery-core', + 'jquery-migrate', + ), + ), + 'wp-mediaelement: 2 and 3 levels of deps' => array( + 'handle' => 'wp-mediaelement', + 'deps' => array( + // Dep to 'wp-mediaelement'. + 'mediaelement', + // Deps to 'mediaelement'. + 'jquery', + 'mediaelement-core', + 'mediaelement-migrate', + // Deps to 'jquery'. + 'jquery-core', + 'jquery-migrate', + ), + ), + ); + } + + /** + * @ticket 28404 + * + * @dataProvider data_non_deps_should_not_enqueue + * + * @param string $handle Script handle. + * @param string[] $not_deps The handles that are not deps of the given script handle. + */ + public function test_non_deps_are_not_enqueued( $handle, $not_deps ) { + // Check the deps are not enqueued before enqueuing. + $this->assertFalse( wp_script_is( $handle ), "Script `{$handle}` should not be enqueued when test starts" ); + foreach ( $not_deps as $not_dep_handle ) { + $this->assertFalse( wp_script_is( $not_dep_handle ), "Dependency `{$not_dep_handle}` should not be enqueued when test starts" ); + } + + // Test set up. + wp_enqueue_script( $handle ); + + foreach ( $not_deps as $not_dep_handle ) { + $this->assertFalse( wp_script_is( $not_dep_handle ), "Script `{$not_dep_handle}` should not be enqueued as it is not a dependency of `{$handle}`" ); + } + } + + /** + * Data provider. + * + * @return array + */ + public function data_non_deps_should_not_enqueue() { + return array( + 'imagesloaded: no dependencies' => array( + 'handle' => 'imagesloaded', + 'not_deps' => array( + 'jquery', + 'masonry', + ), + ), + 'wp-sanitize: no dependencies' => array( + 'handle' => 'wp-sanitize', + 'not_deps' => array( + 'jquery', + 'jquery-core', + 'jquery-migrate', + ), + ), + 'jquery-ui-accordion' => array( + 'handle' => 'jquery-ui-accordion', + 'not_deps' => array( + 'underscore', + 'thickbox', + 'jquery-effects-core', + ), + ), + 'jquery-ui-datepicker' => array( + 'handle' => 'jquery-ui-datepicker', + 'not_deps' => array( + 'backbone', + 'jquery-effects-core', + 'jquery-effects-highlight', + ), + ), + ); + } +}