From 8d831fb387ac75b7ac93bb65dc82972f249293d5 Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Wed, 5 Jun 2024 15:15:07 +0300 Subject: [PATCH 1/5] Identify min vers of assets that reside outside of the $relative_asset_path --- src/Assets/Asset.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index 3b06837..89346d4 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -927,7 +927,11 @@ public function maybe_get_min_file( $url ) { substr( $relative_location, -3, 3 ) === '.js' || substr( $relative_location, -4, 4 ) === '.css' ) { - $urls[] = preg_replace( '#(.*)(' . preg_quote( $relative_asset_path, '#' ) . ')(.*[a-zA-Z0-0\-\_\.]+).(js|css)#', '$1' . $min_asset_path . '$3.min.$4', $relative_location ); + if ( $min_asset_path !== $relative_asset_path ) { + $urls[] = preg_replace( '#(.*)(' . preg_quote( $relative_asset_path, '#' ) . ')(.*[a-zA-Z0-0\-\_\.]+).(js|css)#', '$1' . $min_asset_path . '$3.min.$4', $relative_location ); + } else { + $urls[] = preg_replace( '#(.*).(js|css)#', '$1.min.$2', $relative_location ); + } } if ( ! $script_debug ) { From 9dd6f86eb48f7c9adf54fd2727d0194bdfe25698 Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Wed, 5 Jun 2024 17:24:40 +0300 Subject: [PATCH 2/5] tests updated --- tests/_data/css/fake.min.css | 0 tests/_data/css/fake2.css | 0 tests/_data/css/fake3.min.css | 0 tests/_data/js/fake.min.js | 0 tests/_data/js/fake2.js | 0 tests/_data/js/fake3.min.js | 0 tests/wpunit/AssetsTest.php | 112 +++++++++++++++++++++++++++++++--- 7 files changed, 104 insertions(+), 8 deletions(-) create mode 100644 tests/_data/css/fake.min.css create mode 100644 tests/_data/css/fake2.css create mode 100644 tests/_data/css/fake3.min.css create mode 100644 tests/_data/js/fake.min.js create mode 100644 tests/_data/js/fake2.js create mode 100644 tests/_data/js/fake3.min.js diff --git a/tests/_data/css/fake.min.css b/tests/_data/css/fake.min.css new file mode 100644 index 0000000..e69de29 diff --git a/tests/_data/css/fake2.css b/tests/_data/css/fake2.css new file mode 100644 index 0000000..e69de29 diff --git a/tests/_data/css/fake3.min.css b/tests/_data/css/fake3.min.css new file mode 100644 index 0000000..e69de29 diff --git a/tests/_data/js/fake.min.js b/tests/_data/js/fake.min.js new file mode 100644 index 0000000..e69de29 diff --git a/tests/_data/js/fake2.js b/tests/_data/js/fake2.js new file mode 100644 index 0000000..e69de29 diff --git a/tests/_data/js/fake3.min.js b/tests/_data/js/fake3.min.js new file mode 100644 index 0000000..e69de29 diff --git a/tests/wpunit/AssetsTest.php b/tests/wpunit/AssetsTest.php index 3f92974..4c9bfa2 100644 --- a/tests/wpunit/AssetsTest.php +++ b/tests/wpunit/AssetsTest.php @@ -22,22 +22,53 @@ public function tearDown() { /** * @test */ - public function it_should_should_register_multiple_assets() { + public function it_should_register_multiple_assets() { Asset::add( 'my-script', 'fake.js' )->register(); Asset::add( 'my-style', 'fake.css' )->register(); - $this->assertTrue( Assets::init()->exists( 'my-script' ) ); - $this->assertTrue( Assets::init()->exists( 'my-style' ) ); - $this->assertTrue( wp_script_is( 'my-script', 'registered' ) ); - $this->assertTrue( wp_style_is( 'my-style', 'registered' ) ); - $this->assertEquals( 'my-script', Assets::init()->get( 'my-script' )->get_slug() ); - $this->assertEquals( 'my-style', Assets::init()->get( 'my-style' )->get_slug() ); + $this->existence_assertions( 'my' ); + } + + /** + * @test + */ + public function it_should_locate_minified_versions_of_external_assets() { + if ( defined( SCRIPT_DEBUG ) ) { + $old_script_debug = SCRIPT_DEBUG; + uopz_undefine( 'SCRIPT_DEBUG' ); + } + + Asset::add( 'fake-script', 'fake.js' )->register(); + Asset::add( 'fake-style', 'fake.css' )->register(); + Asset::add( 'fake2-script', 'fake2.js' )->register(); + Asset::add( 'fake2-style', 'fake2.css' )->register(); + Asset::add( 'fake3-script', 'fake3.js' )->register(); + Asset::add( 'fake3-style', 'fake3.css' )->register(); + + $slugs = [ + 'fake' => [ true, false ], + 'fake2' => [ false, false ], + 'fake3' => [ true, true ] + ]; + + foreach ( array_keys( $slugs ) as $slug ) { + $this->existence_assertions( $slug ); + } + + foreach ( $slugs as $slug => $data ) { + $this->assert_minified_found( $slug, true, ...$data ); + $this->assert_minified_found( $slug, false, ...$data ); + } + + if ( isset( $old_script_debug ) ) { + uopz_redefine( 'SCRIPT_DEBUG', $old_script_debug ); + } } /** * @test */ - public function it_should_should_remove_assets() { + public function it_should_remove_assets() { Asset::add( 'my-script', 'fake.js' )->register(); Asset::add( 'my-style', 'fake.css' )->register(); @@ -332,4 +363,69 @@ public function should_allow_setting_dependencies_with_a_callable(): void { ob_get_clean() ); } + + /** + * Evaluates if a script and style have been registered. + */ + protected function existence_assertions( $test_slug_prefix ) { + $this->assertTrue( Assets::init()->exists( $test_slug_prefix . '-script' ) ); + $this->assertTrue( Assets::init()->exists( $test_slug_prefix . '-style' ) ); + $this->assertTrue( wp_script_is( $test_slug_prefix . '-script', 'registered' ) ); + $this->assertTrue( wp_style_is( $test_slug_prefix . '-style', 'registered' ) ); + $this->assertEquals( $test_slug_prefix . '-script', Assets::init()->get( $test_slug_prefix . '-script' )->get_slug() ); + $this->assertEquals( $test_slug_prefix . '-style', Assets::init()->get( $test_slug_prefix. '-style' )->get_slug() ); + } + + /** + * Asserts that the minified version of a script or style is found. + * + * @param string $slug_prefix + * @param bool $is_js + * @param bool $has_min + * @param bool $has_only_min + */ + protected function assert_minified_found( $slug_prefix, $is_js = true, $has_min = true, $has_only_min = false ) { + $asset = Assets::init()->get( $slug_prefix . '-' . ( $is_js ? 'script' : 'style' ) ); + + $url = get_site_url() . '/wp-content/plugins/assets/tests/_data/' . ( $is_js ? 'js' : 'css' ) . '/' . $slug_prefix; + + $urls = []; + + uopz_redefine( 'SCRIPT_DEBUG', false ); + + $this->assertFalse( SCRIPT_DEBUG ); + + if ( $has_only_min ) { + $urls[] = $url . '.min' . ( $is_js ? '.js' : '.css' ); + $urls[] = $url . '.min' . ( $is_js ? '.js' : '.css' ); + } elseif ( $has_min ) { + $urls[] = $url . ( $is_js ? '.min.js' : '.min.css' ); + $urls[] = $url . ( $is_js ? '.js' : '.css' ); + } else { + $urls[] = $url . ( $is_js ? '.js' : '.css' ); + $urls[] = $url . ( $is_js ? '.js' : '.css' ); + } + + $this->assertEquals( + $urls['0'], + $asset->get_url() + ); + + uopz_redefine( 'SCRIPT_DEBUG', true ); + + $this->assertTrue( SCRIPT_DEBUG ); + + // Remove and re add to clear cache. + Assets::init()->remove( $slug_prefix . '-' . ( $is_js ? 'script' : 'style' ) ); + Asset::add( $slug_prefix . '-' . ( $is_js ? 'script' : 'style' ), $slug_prefix . '.' . ( $is_js ? 'js' : 'css' ) )->register(); + + $asset = Assets::init()->get( $slug_prefix . '-' . ( $is_js ? 'script' : 'style' ) ); + + $this->assertEquals( + $urls['1'], + $asset->get_url() + ); + + uopz_undefine( 'SCRIPT_DEBUG' ); + } } From 04dc41ad488a44c3c341d19f9155085390fe2bf2 Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Wed, 5 Jun 2024 17:55:00 +0300 Subject: [PATCH 3/5] Making sure every const set is cleared after tests --- tests/wpunit/AssetsTest.php | 62 ++++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 12 deletions(-) diff --git a/tests/wpunit/AssetsTest.php b/tests/wpunit/AssetsTest.php index 4c9bfa2..83f436e 100644 --- a/tests/wpunit/AssetsTest.php +++ b/tests/wpunit/AssetsTest.php @@ -3,8 +3,16 @@ namespace StellarWP\Assets; use StellarWP\Assets\Tests\AssetTestCase; +use PHPUnit\Framework\Assert; class AssetsTest extends AssetTestCase { + /** + * Store the value of SCRIPT_DEBUG before modifying it. + * + * @var mixed + */ + protected static $uopz_redefines = []; + public function setUp() { // before parent::setUp(); @@ -19,6 +27,19 @@ public function tearDown() { Config::reset(); } + /** + * @after + */ + public function unset_uopz_redefines() { + if ( function_exists( 'uopz_redefine' ) ) { + foreach ( self::$uopz_redefines as $restore_callback ) { + $restore_callback(); + } + } + + self::$uopz_redefines = []; + } + /** * @test */ @@ -33,11 +54,6 @@ public function it_should_register_multiple_assets() { * @test */ public function it_should_locate_minified_versions_of_external_assets() { - if ( defined( SCRIPT_DEBUG ) ) { - $old_script_debug = SCRIPT_DEBUG; - uopz_undefine( 'SCRIPT_DEBUG' ); - } - Asset::add( 'fake-script', 'fake.js' )->register(); Asset::add( 'fake-style', 'fake.css' )->register(); Asset::add( 'fake2-script', 'fake2.js' )->register(); @@ -59,10 +75,6 @@ public function it_should_locate_minified_versions_of_external_assets() { $this->assert_minified_found( $slug, true, ...$data ); $this->assert_minified_found( $slug, false, ...$data ); } - - if ( isset( $old_script_debug ) ) { - uopz_redefine( 'SCRIPT_DEBUG', $old_script_debug ); - } } /** @@ -391,7 +403,7 @@ protected function assert_minified_found( $slug_prefix, $is_js = true, $has_min $urls = []; - uopz_redefine( 'SCRIPT_DEBUG', false ); + $this->set_const_value( 'SCRIPT_DEBUG', false ); $this->assertFalse( SCRIPT_DEBUG ); @@ -411,7 +423,7 @@ protected function assert_minified_found( $slug_prefix, $is_js = true, $has_min $asset->get_url() ); - uopz_redefine( 'SCRIPT_DEBUG', true ); + $this->set_const_value( 'SCRIPT_DEBUG', true ); $this->assertTrue( SCRIPT_DEBUG ); @@ -425,7 +437,33 @@ protected function assert_minified_found( $slug_prefix, $is_js = true, $has_min $urls['1'], $asset->get_url() ); + } - uopz_undefine( 'SCRIPT_DEBUG' ); + /** + * Set a constant value using uopz. + * + * @param string $const + * @param mixed $value + */ + private function set_const_value( $const, $value ) { + if ( ! function_exists( 'uopz_redefine' ) ) { + $this->markTestSkipped( 'uopz extension is not installed' ); + } + + // Normal const redefinition. + $previous_value = defined( $const ) ? constant( $const ) : null; + if ( null === $previous_value ) { + $restore_callback = static function () use ( $const ) { + uopz_undefine( $const ); + Assert::assertFalse( defined( $const ) ); + }; + } else { + $restore_callback = static function () use ( $previous_value, $const ) { + uopz_redefine( $const, $previous_value ); + Assert::assertEquals( $previous_value, constant( $const ) ); + }; + } + uopz_redefine( $const, $value ); + self::$uopz_redefines[] = $restore_callback; } } From 758f298bf88ee6aea6648c1399029108d161fd9e Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Wed, 5 Jun 2024 17:56:52 +0300 Subject: [PATCH 4/5] alter properties comment --- tests/wpunit/AssetsTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/wpunit/AssetsTest.php b/tests/wpunit/AssetsTest.php index 83f436e..281fe3a 100644 --- a/tests/wpunit/AssetsTest.php +++ b/tests/wpunit/AssetsTest.php @@ -7,7 +7,7 @@ class AssetsTest extends AssetTestCase { /** - * Store the value of SCRIPT_DEBUG before modifying it. + * Store const modifications. * * @var mixed */ From 4ecb5b5f880e4e0e55e6cd347fc928599ffcd4da Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Wed, 5 Jun 2024 18:14:12 +0300 Subject: [PATCH 5/5] Renamed files to not conflict with previous tests --- tests/_data/css/{fake.min.css => fake1.css} | 0 tests/_data/{js/fake.min.js => css/fake1.min.css} | 0 tests/_data/js/fake1.js | 0 tests/_data/js/fake1.min.js | 0 tests/wpunit/AssetsTest.php | 6 +++--- 5 files changed, 3 insertions(+), 3 deletions(-) rename tests/_data/css/{fake.min.css => fake1.css} (100%) rename tests/_data/{js/fake.min.js => css/fake1.min.css} (100%) create mode 100644 tests/_data/js/fake1.js create mode 100644 tests/_data/js/fake1.min.js diff --git a/tests/_data/css/fake.min.css b/tests/_data/css/fake1.css similarity index 100% rename from tests/_data/css/fake.min.css rename to tests/_data/css/fake1.css diff --git a/tests/_data/js/fake.min.js b/tests/_data/css/fake1.min.css similarity index 100% rename from tests/_data/js/fake.min.js rename to tests/_data/css/fake1.min.css diff --git a/tests/_data/js/fake1.js b/tests/_data/js/fake1.js new file mode 100644 index 0000000..e69de29 diff --git a/tests/_data/js/fake1.min.js b/tests/_data/js/fake1.min.js new file mode 100644 index 0000000..e69de29 diff --git a/tests/wpunit/AssetsTest.php b/tests/wpunit/AssetsTest.php index 281fe3a..9c2d638 100644 --- a/tests/wpunit/AssetsTest.php +++ b/tests/wpunit/AssetsTest.php @@ -54,15 +54,15 @@ public function it_should_register_multiple_assets() { * @test */ public function it_should_locate_minified_versions_of_external_assets() { - Asset::add( 'fake-script', 'fake.js' )->register(); - Asset::add( 'fake-style', 'fake.css' )->register(); + Asset::add( 'fake1-script', 'fake1.js' )->register(); + Asset::add( 'fake1-style', 'fake1.css' )->register(); Asset::add( 'fake2-script', 'fake2.js' )->register(); Asset::add( 'fake2-style', 'fake2.css' )->register(); Asset::add( 'fake3-script', 'fake3.js' )->register(); Asset::add( 'fake3-style', 'fake3.css' )->register(); $slugs = [ - 'fake' => [ true, false ], + 'fake1' => [ true, false ], 'fake2' => [ false, false ], 'fake3' => [ true, true ] ];