Skip to content

Commit

Permalink
External Libraries: Test registered script versions match package.json.
Browse files Browse the repository at this point in the history
Expands tests to ensure the version number of packages updated via NPM matches the version number used for registering the script in the script loader.

This adds tests for (by their registered name in WordPress):

* backbone
* clipboard
* hoverIntent
* hoverintent-js
* imagesloaded
* jquery-color
* jquery-core
* jquery-form
* masonry
* react-jsx-runtime
* underscore
* wp-polyfill-dom-rect
* wp-polyfill-element-closest
* wp-polyfill-fetch
* wp-polyfill-formdata
* wp-polyfill-inert
* wp-polyfill-node-contains
* wp-polyfill-object-fit
* wp-polyfill-url


This expands on the earlier tests introduced for:

* lodash
* moment
* react
* react-dom
* regenerator-runtime

An additional test is added to ensure that the data provider for these tests is maintained as libraries are added via package.json.

`@wordpress/*` scripts are excluded from these tests as wp-scripts generates a version number automatically based on the file's contents.

Additionally, the version of element-closest listed in package.json is updated to use a fixed version rather than a range. This reflects the current practice of WordPress to define the specific version in core. For the avoidance of doubt, this does not affect the version shipped in WordPress.

Follow up to [57185].

Props peterwilsoncc, jorbin.
Fixes #61855.


git-svn-id: https://develop.svn.wordpress.org/trunk@59071 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
peterwilsoncc committed Sep 20, 2024
1 parent 951f887 commit d46067a
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
"backbone": "1.6.0",
"clipboard": "2.0.11",
"core-js-url-browser": "3.6.4",
"element-closest": "^3.0.2",
"element-closest": "3.0.2",
"formdata-polyfill": "4.0.10",
"hoverintent": "2.2.1",
"imagesloaded": "5.0.0",
Expand Down
111 changes: 101 additions & 10 deletions tests/phpunit/tests/dependencies/scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -3386,29 +3386,120 @@ public function data_provider_script_move_to_footer() {
}

/**
* Tests default scripts are registered with the correct versions.
*
* Ensures that vendor scripts registered in wp_default_scripts() and
* wp_default_packages_vendor() are registered with the correct version
* number from package.json.
*
* @ticket 61855
* @ticket 60048
*
* @covers ::wp_default_scripts
* @covers ::wp_default_packages_vendor
*
* @dataProvider data_wp_default_packages_vendor
* @dataProvider data_vendor_script_versions_registered_manually
*
* @param string $script Script name as defined in package.json.
* @param string $handle Optional. Handle to check for. Defaults to the script name.
*/
public function test_wp_default_packages_vendor( $script ) {
public function test_vendor_script_versions_registered_manually( $script, $handle = null ) {
global $wp_scripts;
wp_default_packages_vendor( $wp_scripts );
wp_default_scripts( $wp_scripts );

$package_json = $this->_scripts_from_package_json();
if ( ! $handle ) {
$handle = $script;
}

wp_default_packages_vendor( $wp_scripts );
$script_query = $wp_scripts->query( $handle, 'registered' );

$this->assertSame( $package_json[ $script ], $wp_scripts->query( $script, 'registered' )->ver );
$this->assertNotFalse( $script_query, "The script '{$handle}' should be registered." );
$this->assertArrayHasKey( $script, $package_json, "The dependency '{$script}' should be included in package.json." );
$this->assertSame( $package_json[ $script ], $wp_scripts->query( $handle, 'registered' )->ver, "The script '{$handle}' should be registered with version {$package_json[ $script ]}." );
}

public function data_wp_default_packages_vendor() {
/**
* Data provider for test_vendor_script_versions_registered_manually.
*
* @return array[]
*/
public function data_vendor_script_versions_registered_manually() {
return array(
array( 'script' => 'lodash' ),
array( 'script' => 'moment' ),
array( 'script' => 'react' ),
array( 'script' => 'react-dom' ),
array( 'script' => 'regenerator-runtime' ),
'backbone' => array( 'backbone' ),
'clipboard' => array( 'clipboard' ),
'core-js-url-browser' => array( 'core-js-url-browser', 'wp-polyfill-url' ),
'element-closest' => array( 'element-closest', 'wp-polyfill-element-closest' ),
'formdata-polyfill' => array( 'formdata-polyfill', 'wp-polyfill-formdata' ),
'imagesloaded' => array( 'imagesloaded' ),
'jquery-color' => array( 'jquery-color' ),
'jquery-core' => array( 'jquery', 'jquery-core' ),
'jquery-form' => array( 'jquery-form' ),
'jquery-hoverintent' => array( 'jquery-hoverintent', 'hoverIntent' ),
'lodash' => array( 'lodash' ),
'masonry' => array( 'masonry-layout', 'masonry' ),
'moment' => array( 'moment' ),
'objectFitPolyfill' => array( 'objectFitPolyfill', 'wp-polyfill-object-fit' ),
'polyfill-library (dom rect)' => array( 'polyfill-library', 'wp-polyfill-dom-rect' ),
'polyfill-library (node contains)' => array( 'polyfill-library', 'wp-polyfill-node-contains' ),
'react (jsx-runtime)' => array( 'react', 'react-jsx-runtime' ),
'react (React)' => array( 'react' ),
'react-dom' => array( 'react-dom' ),
'regenerator-runtime' => array( 'regenerator-runtime' ),
'underscore' => array( 'underscore' ),
'vanilla-js-hoverintent' => array( 'hoverintent', 'hoverintent-js' ),
'whatwg-fetch' => array( 'whatwg-fetch', 'wp-polyfill-fetch' ),
'wicg-inert' => array( 'wicg-inert', 'wp-polyfill-inert' ),
);
}

/**
* Ensures that all the scripts in the package.json are included in the data provider.
*
* This is a test the tests to ensure the data provider includes all the scripts in package.json.
*
* @ticket 61855
*/
public function test_vendor_script_data_provider_includes_all_packages() {
$package_json_dependencies = array_keys( $this->_scripts_from_package_json() );
$data_provider_dependencies = $this->data_vendor_script_versions_registered_manually();

/*
* Exclude `@wordpress/*` packages from the packages in package.json.
*
* The version numbers for these packages is generated by the build
* process based on a hash of the file contents.
*/
$package_json_dependencies = array_filter(
$package_json_dependencies,
static function ( $dependency ) {
return 0 !== strpos( $dependency, '@wordpress/' );
}
);

// Get the script names from the data provider.
$data_provider_dependencies = array_map(
static function ( $dependency ) {
return $dependency[0];
},
$data_provider_dependencies
);

// Exclude packages that are not registered in WordPress.
$exclude = array( 'react-is', 'json2php' );
$package_json_dependencies = array_diff( $package_json_dependencies, $exclude );

/*
* Ensure the arrays are unique.
*
* This is for the react package as it is included in the data provider
* as both `react` and `react-jsx-runtime`.
*/
$package_json_dependencies = array_unique( $package_json_dependencies );
$data_provider_dependencies = array_unique( $data_provider_dependencies );

$this->assertSameSets( $package_json_dependencies, $data_provider_dependencies );
}

/**
Expand Down

0 comments on commit d46067a

Please sign in to comment.