Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Asset_Manager_Preload::set_asset_types() should return an empty array if no valid arguments are passed #65

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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: `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.
Expand Down
10 changes: 7 additions & 3 deletions php/class-asset-manager-preload.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ) ) {
Expand All @@ -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 );
}

Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -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>
Expand Down
8 changes: 8 additions & 0 deletions tests/test-preload.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down