Skip to content

Commit

Permalink
Merge pull request #10 from stellarwp/fix/undentified-min-non-min-ver…
Browse files Browse the repository at this point in the history
…sions

Identify min vers of assets that reside outside of the $relative_asse_path
  • Loading branch information
lucatume authored Jun 5, 2024
2 parents c5cbed2 + 4ecb5b5 commit 3e097e1
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 9 deletions.
6 changes: 5 additions & 1 deletion src/Assets/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
Empty file added tests/_data/css/fake1.css
Empty file.
Empty file added tests/_data/css/fake1.min.css
Empty file.
Empty file added tests/_data/css/fake2.css
Empty file.
Empty file added tests/_data/css/fake3.min.css
Empty file.
Empty file added tests/_data/js/fake1.js
Empty file.
Empty file added tests/_data/js/fake1.min.js
Empty file.
Empty file added tests/_data/js/fake2.js
Empty file.
Empty file added tests/_data/js/fake3.min.js
Empty file.
150 changes: 142 additions & 8 deletions tests/wpunit/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@
namespace StellarWP\Assets;

use StellarWP\Assets\Tests\AssetTestCase;
use PHPUnit\Framework\Assert;

class AssetsTest extends AssetTestCase {
/**
* Store const modifications.
*
* @var mixed
*/
protected static $uopz_redefines = [];

public function setUp() {
// before
parent::setUp();
Expand All @@ -19,25 +27,60 @@ 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
*/
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_should_remove_assets() {
public function it_should_locate_minified_versions_of_external_assets() {
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 = [
'fake1' => [ 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 );
}
}

/**
* @test
*/
public function it_should_remove_assets() {
Asset::add( 'my-script', 'fake.js' )->register();
Asset::add( 'my-style', 'fake.css' )->register();

Expand Down Expand Up @@ -332,4 +375,95 @@ 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 = [];

$this->set_const_value( '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()
);

$this->set_const_value( '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()
);
}

/**
* 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;
}
}

0 comments on commit 3e097e1

Please sign in to comment.