From e1007de2f551e3643844b16e7dfc0a60800adf71 Mon Sep 17 00:00:00 2001 From: Renato Alves <19148962+renatonascalves@users.noreply.github.com> Date: Mon, 26 Aug 2024 09:10:06 -0300 Subject: [PATCH 1/2] Fix: `Asset_Manager_Preload::set_asset_types()` should return an empty array if no valid arguments are passed. --- CHANGELOG.md | 5 +++++ php/class-asset-manager-preload.php | 10 +++++++--- phpunit.xml | 2 +- tests/test-preload.php | 8 ++++++++ 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5fb437d..a231571 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ # Change Log This project adheres to [Semantic Versioning](http://semver.org/). +## 1.3.8 (unreleased) + +* Fix: `Asset_Manager_Preload::set_asset_types()` should return an empty array if no valid arguments are passed. +* Add: Add `convertDeprecationsToExceptions` to `phpunit.xml`. + ## 1.3.7 * Adds support for async and defer using the 'strategy' argument added for wp_enqueue_script in WordPress 6.3. diff --git a/php/class-asset-manager-preload.php b/php/class-asset-manager-preload.php index c8f43e6..79cfd58 100644 --- a/php/class-asset-manager-preload.php +++ b/php/class-asset-manager-preload.php @@ -189,10 +189,14 @@ public function post_validate_asset( $asset ) { * A MIME type isn't required, but will prevent the browser downloading an * asset it doesn't support. * - * @param array $asset The asset for which the types are needed. - * @return array The $asset. + * @param array $asset The asset for which the types are needed. + * @return array */ public function set_asset_types( $asset ) { + if ( empty( $asset ) || ! isset( $asset['src'] ) ) { + return $asset; + } + $path_parts = pathinfo( $asset['src'] ); if ( empty( $path_parts['extension'] ) ) { @@ -201,8 +205,8 @@ public function set_asset_types( $asset ) { $asset_types = $this->asset_types[ $path_parts['extension'] ] ?? []; + // Force these values through. if ( ! empty( $asset_types ) ) { - // Force these values through. return array_replace( $asset, $asset_types ); } diff --git a/phpunit.xml b/phpunit.xml index ec73ce0..73240d4 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,5 +1,5 @@ <?xml version="1.0"?> -<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="tests/bootstrap.php" backupGlobals="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"> +<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="tests/bootstrap.php" backupGlobals="false" colors="true" convertErrorsToExceptions="true" convertDeprecationsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"> <testsuites> <testsuite name="wp-asset-manager"> <directory prefix="test-" suffix=".php">./tests/</directory> diff --git a/tests/test-preload.php b/tests/test-preload.php index 3c90d65..096627d 100644 --- a/tests/test-preload.php +++ b/tests/test-preload.php @@ -87,6 +87,14 @@ function test_print_asset() { * @group preload */ function test_set_asset_types() { + $actual_output = \Asset_Manager_Preload::instance()->set_asset_types( [] ); + + $this->assertEquals( + $actual_output, + [], + "Should return an empty array if no arguments are passed" + ); + // Adds the expected attributes for preloading a CSS file. $expected_style = array_merge( $this->test_style, From 8f8049aee1e03693cb22af8d0f9d978957f4f5c4 Mon Sep 17 00:00:00 2001 From: Renato Alves <19148962+renatonascalves@users.noreply.github.com> Date: Mon, 26 Aug 2024 09:12:10 -0300 Subject: [PATCH 2/2] Minor tweak --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a231571..ad0fd13 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## 1.3.8 (unreleased) * Fix: `Asset_Manager_Preload::set_asset_types()` should return an empty array if no valid arguments are passed. -* Add: Add `convertDeprecationsToExceptions` to `phpunit.xml`. +* Add: `convertDeprecationsToExceptions` to `phpunit.xml`. ## 1.3.7