From fdcb23b6be9eb0ddd1217285bcbae314c29171a3 Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Tue, 17 Dec 2024 20:16:46 +0200 Subject: [PATCH 1/3] False by default for asset file for styles --- src/Assets/Asset.php | 4 ++++ tests/wpunit/AssetsTest.php | 33 +++++++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index db5bdc8..175e0bf 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -269,6 +269,8 @@ class Asset { /** * Whether or not to attempt to load an .asset.php file. * + * By default is true for scripts and false for styles. + * * @since 1.3.1 * * @var bool @@ -1173,12 +1175,14 @@ public function in_header() { * Set the asset type. * * @since 1.0.0 + * @since TBD - For css files, we dont want to use asset file for dependencies by default. */ protected function infer_type() { if ( substr( $this->file, -3, 3 ) === '.js' ) { $this->type = 'js'; } elseif ( substr( $this->file, -4, 4 ) === '.css' ) { $this->type = 'css'; + $this->use_asset_file( false ); } } diff --git a/tests/wpunit/AssetsTest.php b/tests/wpunit/AssetsTest.php index d9ce479..86af111 100644 --- a/tests/wpunit/AssetsTest.php +++ b/tests/wpunit/AssetsTest.php @@ -637,28 +637,36 @@ public function should_allow_setting_dependencies_with_a_callable(): void { /** * @test */ - public function it_should_use_include_css_asset_file_dependencies_when_no_dependencies_are_set(): void { + public function it_should_not_use_include_css_asset_file_dependencies_when_no_dependencies_are_set(): void { $asset = Asset::add( 'my-style' . uniqid(), 'fake4.css' ); - $this->assertContains( 'some-dependency', $asset->get_dependencies() ); + $this->assertEmpty( $asset->get_dependencies() ); + } + + /** + * @test + */ + public function it_should_use_include_css_asset_file_dependencies_when_no_dependencies_are_set(): void { + $asset = Asset::add( 'my-style' . uniqid(), 'fake4.css' )->use_asset_file( true ); + + $this->assertEquals( ['some-dependency'], $asset->get_dependencies() ); } /** * @test */ public function it_should_use_include_css_asset_file_dependencies_when_dependencies_are_set(): void { - $asset = Asset::add( 'my-style' . uniqid(), 'fake4.css' ); + $asset = Asset::add( 'my-style' . uniqid(), 'fake4.css' )->use_asset_file( true ); $asset->set_dependencies( 'fake1' ); - $this->assertContains( 'fake1', $asset->get_dependencies() ); - $this->assertContains( 'some-dependency', $asset->get_dependencies() ); + $this->assertEquals( [ 'some-dependency', 'fake1' ], $asset->get_dependencies() ); } /** * @test */ public function it_should_use_include_css_asset_file_dependencies_when_dependencies_are_set_as_callable(): void { - $asset = Asset::add( 'my-style' . uniqid(), 'fake4.css' ); + $asset = Asset::add( 'my-style' . uniqid(), 'fake4.css' )->use_asset_file( true ); $asset->set_dependencies( static function() { return [ 'fake1' ]; } ); @@ -670,9 +678,18 @@ public function it_should_use_include_css_asset_file_dependencies_when_dependenc /** * @test */ - public function it_should_use_css_asset_file_version_when_no_version_is_set(): void { + public function it_should_not_use_css_asset_file_version_when_no_version_is_set(): void { $asset = Asset::add( 'my-style' . uniqid(), 'fake4.css' ); + $this->assertEquals( '1.0.0', $asset->get_version() ); + } + + /** + * @test + */ + public function it_should_use_css_asset_file_version_when_no_version_is_set(): void { + $asset = Asset::add( 'my-style' . uniqid(), 'fake4.css' )->use_asset_file( true ); + $this->assertEquals( '12345', $asset->get_version() ); } @@ -680,7 +697,7 @@ public function it_should_use_css_asset_file_version_when_no_version_is_set(): v * @test */ public function it_should_use_css_asset_file_version_when_version_is_set(): void { - $asset = Asset::add( 'my-style' . uniqid(), 'fake4.css', '1.0' ); + $asset = Asset::add( 'my-style' . uniqid(), 'fake4.css', '1.0' )->use_asset_file( true ); $this->assertEquals( '12345', $asset->get_version() ); } From 303583b530b4cd840c67b6671dad7918f9fb18c8 Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Tue, 17 Dec 2024 20:22:47 +0200 Subject: [PATCH 2/3] Fixing and adding acceptance test --- tests/acceptance/EnqueueCSSCest.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/acceptance/EnqueueCSSCest.php b/tests/acceptance/EnqueueCSSCest.php index a941f95..fc85bba 100644 --- a/tests/acceptance/EnqueueCSSCest.php +++ b/tests/acceptance/EnqueueCSSCest.php @@ -257,11 +257,31 @@ public function it_should_set_media( AcceptanceTester $I ) { $I->dontSeeElement( 'link', [ 'href' => 'http://wordpress.test/wp-content/plugins/assets/tests/_data/css/fake.css?ver=1.0.0', 'media' => 'screen' ] ); } + public function it_should_enqueue_css_on_default_version_when_using_register_with_js( AcceptanceTester $I ) { + $code = file_get_contents( codecept_data_dir( 'enqueue-template.php' ) ); + $code .= <<set_path( 'tests/_data/build' ) + ->enqueue_on( 'wp_enqueue_scripts' ) + ->register_with_js(); + }, 100 ); + PHP; + + $I->haveMuPlugin( 'enqueue.php', $code ); + + + $I->amOnPage( '/' ); + $I->seeElement( 'link', [ 'href' => 'http://wordpress.test/wp-content/plugins/assets/tests/_data/build/something.css?ver=1.0.0' ] ); + $I->seeElement( 'script', [ 'src' => 'http://wordpress.test/wp-content/plugins/assets/tests/_data/build/something.js?ver=1.0.0' ] ); + } + public function it_should_enqueue_js_when_using_register_with_js( AcceptanceTester $I ) { $code = file_get_contents( codecept_data_dir( 'enqueue-template.php' ) ); $code .= <<use_asset_file( true ) ->set_path( 'tests/_data/build' ) ->enqueue_on( 'wp_enqueue_scripts' ) ->register_with_js(); From 986c5e29bbf9ca0f80dcdcb56f08b2171b695a13 Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Fri, 10 Jan 2025 18:24:46 +0200 Subject: [PATCH 3/3] version bump --- assets.php | 2 +- src/Assets/Asset.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/assets.php b/assets.php index 3f1553c..0e34bbd 100644 --- a/assets.php +++ b/assets.php @@ -2,7 +2,7 @@ /** * Plugin Name: Assets * Description: Asset library with a plugin bootstrap file for automated testing. - * Version: 1.4.2 + * Version: 1.4.4 * Author: StellarWP * Author URI: https://stellarwp.com */ diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index 175e0bf..874256c 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -289,7 +289,7 @@ class Asset { * This flag will be raised when the asset is added to a group path * and lowered when it's removed from it. * - * @since TBD + * @since 1.4.3 * * @var bool */ @@ -1175,7 +1175,7 @@ public function in_header() { * Set the asset type. * * @since 1.0.0 - * @since TBD - For css files, we dont want to use asset file for dependencies by default. + * @since 1.4.4 - For css files, we dont want to use asset file for dependencies by default. */ protected function infer_type() { if ( substr( $this->file, -3, 3 ) === '.js' ) { @@ -1534,7 +1534,7 @@ public function set_action( string $action ) { /** * Set the asset file path for the asset. * - * @since TBD + * @since 1.3.0 * * @param string $path The partial path to the asset. *