Skip to content

Commit

Permalink
Merge pull request #2274 from the-events-calendar/slr/fast-follow-1
Browse files Browse the repository at this point in the history
Slr/fast follow 1
  • Loading branch information
dpanta94 authored Nov 15, 2024
2 parents 353bd56 + da420df commit a5f4210
Show file tree
Hide file tree
Showing 17 changed files with 322 additions and 13 deletions.
4 changes: 4 additions & 0 deletions changelog/feat-implement-stellar-wp-asset
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: feat

Update stellarwp/assets to version 1.4.2. [SL-246]
4 changes: 4 additions & 0 deletions changelog/feat-implement-stellar-wp-asset-2
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: feat

Introduced Asset interface which accounts for symlinks, while still provides a fluent api. [SL-246]
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"stellarwp/models": "dev-main",
"stellarwp/schema": "^1.1.3",
"stellarwp/telemetry": "^2.3.1",
"stellarwp/assets": "^1.2.6",
"stellarwp/assets": "1.4.2",
"stellarwp/uplink": "2.2.1"
},
"require-dev": {
Expand Down
20 changes: 10 additions & 10 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions src/Common/Asset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* TEC Implementation of StellarWP Asset.
*
* @since TBD
*
* @package TEC\Common;
*/

namespace TEC\Common;

use TEC\Common\StellarWP\Assets\Asset as Stellar_Asset;
use TEC\Common\StellarWP\Assets\Assets;

/**
* Extending TEC\Common\StellarWP\Assets\Asset in order to allow following
* possible symlinks in an asset's path.
*
* @since TBD
*/
class Asset extends Stellar_Asset {
/**
* Gets the root path for the resource considering the resource is inside a PLUGIN
* and that it could be a symlink.
*
* @since TBD
*
* @return ?string
*/
public function get_root_path(): ?string {
return str_replace( trailingslashit( dirname( __DIR__, 4 ) ), trailingslashit( WP_PLUGIN_DIR ), parent::get_root_path() );
}

/**
* Registers an asset.
*
* @param string $slug The asset slug.
* @param string $file The asset file path.
* @param string|null $version The asset version.
* @param string|null $root_path The path to the root of the plugin.
*/
public static function add( string $slug, string $file, string $version = null, $root_path = null ) {
return Assets::init()->add( new self( $slug, $file, $version, $root_path ) );
}
}
11 changes: 9 additions & 2 deletions src/Tribe/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,15 @@ public function set_template_folder( $folder = null ) {
$folder = (array) explode( '/', $folder );
}

// Cast as Array and save
$this->folder = (array) $folder;
/**
* Cast as array and filter removing potentially empty values.
*
* Filtering can forgive small issues where a leading or trailing slash has been specified
* while it shouldn't.
*
* @see [ECP-1477] https://github.com/the-events-calendar/events-pro/pull/2609
*/
$this->folder = array_values( array_filter( (array) ( $folder ) ) );

return $this;
}
Expand Down
Empty file.
1 change: 1 addition & 0 deletions tests/_data/stellar-resources/feature-base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Empty file.
1 change: 1 addition & 0 deletions tests/_data/stellar-resources/feature-base.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.
1 change: 1 addition & 0 deletions tests/_data/stellar-resources/feature-editor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Empty file.
1 change: 1 addition & 0 deletions tests/_data/stellar-resources/feature-frontend.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions tests/_support/Traits/With_Uopz.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ trait With_Uopz {
private static array $uopz_set_properties = [];
private static array $uopz_add_class_fns = [];
private static array $uopz_del_functions = [];
/**
* @var array<class-string>
*/
private static array $uopz_class_mocks = [];

/**
* @after
Expand Down Expand Up @@ -82,6 +86,18 @@ public function unset_uopz_functions() {
self::$uopz_del_functions = [];
}

/**
* @after
*/
public function unset_uopz_class_mocks():void {
if(function_exists( 'uopz_unset_mock' ) ) {
foreach ( self::$uopz_class_mocks as $class ) {
uopz_unset_mock( $class );
}
}
self::$uopz_class_mocks = [];
}

/**
* Wrapper for uopz_set_return
*
Expand Down Expand Up @@ -219,4 +235,18 @@ private function add_fn( string $function, Closure $handler ) {
uopz_add_function( $function, $handler );
self::$uopz_del_functions[] = $function;
}

/**
* Replaces the return value of `new` calls for the class to return the mock.
*
* @since TBD
*
* @param string $class The class to replace with the mock. It will only apply to new instances.
* @param string|object $mock Either the name of the class to mock the original with, or the object that
* will be returned in place of any new instances.
*/
protected function set_class_mock( string $class, $mock ): void {
self::$uopz_class_mocks[] = $class;
uopz_set_mock( $class, $mock );
}
}
186 changes: 186 additions & 0 deletions tests/wpunit/Common/Asset_Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
<?php

namespace TEC\Common;

use Codeception\TestCase\WPTestCase;
use TEC\Common\StellarWP\Assets\Asset as Stellar_Asset;
use TEC\Common\StellarWP\Assets\Assets;
use TEC\Common\StellarWP\Assets\Config;
use Tribe\Tests\Traits\With_Uopz;

/**
* Class Opt_InTest
*
* @since 5.1.13
*
* @package TEC\Common\Telemetry
*/
class Asset_Test extends WPTestCase {
use With_Uopz;

protected static array $back_up = [];

/**
* @before
*/
public function backup_and_set_up() {
self::$back_up = [
'hook_prefix' => Config::get_hook_prefix(),
'version' => Config::get_version(),
'path' => Config::get_path(),
'relative' => Config::get_relative_asset_path(),
];

Config::set_hook_prefix( 'bork' );
Config::set_version( '1.0.0' );
Config::set_path( codecept_root_dir( '/' ) );
Config::set_relative_asset_path( 'tests/_data/stellar-resources' );
}

/**
* @after
*/
public function restore_backup() {
$this->remove_assets( 'feature-base' );
$this->remove_assets( 'feature-editor' );
$this->remove_assets( 'feature-frontend' );
Config::reset();

Config::set_hook_prefix( self::$back_up['hook_prefix'] );
Config::set_version( self::$back_up['version'] );
Config::set_path( self::$back_up['path'] );
Config::set_relative_asset_path( self::$back_up['relative'] );
}

protected function add_assets( $slug ) {
Asset::add( $slug, $slug . '.js' )
->prefix_asset_directory( false );
Asset::add( $slug . '-style', $slug . '.css' )
->prefix_asset_directory( false );
Stellar_Asset::add( 'stellar-' . $slug, $slug . '.js' )
->prefix_asset_directory( false );
Stellar_Asset::add( 'stellar-' . $slug . '-style', $slug . '.css' )
->prefix_asset_directory( false );
}

protected function remove_assets( $slug ) {
Assets::init()->remove( $slug );
Assets::init()->remove( $slug . '-style' );
Assets::init()->remove( 'stellar-' . $slug );
Assets::init()->remove( 'stellar-' . $slug . '-style' );
}

/**
* @test
*/
public function it_should_return_the_same_thing() {
Assets::init();

// Add assets.
$this->add_assets( 'feature-base' );
$this->add_assets( 'feature-editor' );
$this->add_assets( 'feature-frontend' );

foreach ( [ 'feature-base', 'feature-editor', 'feature-frontend' ] as $slug ) {
$this->assertEquals(
Assets::init()->get( $slug )->get_url(),
Assets::init()->get( 'stellar-' . $slug )->get_url()
);
$this->assertEquals(
Assets::init()->get( $slug . '-style' )->get_url(),
Assets::init()->get( 'stellar-' . $slug . '-style' )->get_url()
);
}
}

/**
* The test is that the plugins directory is actually in the path ABSPATH . 'foo/'.
* But in the location ABSPATH . 'wp-content/' user has created a symlink named plugins which points to ABSPATH . 'foo/'.
* Our implementation should return a URL inside the wp-content directory while the asset one should return the true URL outside the plugins directory.
*
* @test
*/
public function it_should_not_return_the_same_thing_when_symlinks() {
Config::set_path( ABSPATH . 'foo/the-events-calendar/common/' );
$this->set_fn_return( 'dirname', static fn( $dir, $level = 1 ) => $level !== 4 ? $dir : ABSPATH . 'foo/', true );

Assets::init();

// Add assets.
$this->add_assets( 'feature-base' );
$this->add_assets( 'feature-editor' );
$this->add_assets( 'feature-frontend' );

foreach ( [ 'feature-base', 'feature-editor', 'feature-frontend' ] as $slug ) {
$this->assertEquals(
home_url( '/wp-content/plugins/the-events-calendar/common/tests/_data/stellar-resources/' . $slug . '.js' ),
Assets::init()->get( $slug )->get_url( false )
);

$this->assertEquals(
'/var/www/html/foo/the-events-calendar/common/tests/_data/stellar-resources/' . $slug . '.js',
Assets::init()->get( 'stellar-' . $slug )->get_url( false )
);

$this->assertEquals(
home_url( '/wp-content/plugins/the-events-calendar/common/tests/_data/stellar-resources/' . $slug . '.css' ),
Assets::init()->get( $slug. '-style' )->get_url( false )
);

$this->assertEquals(
'/var/www/html/foo/the-events-calendar/common/tests/_data/stellar-resources/' . $slug . '.css',
Assets::init()->get( 'stellar-' . $slug . '-style' )->get_url( false )
);
}
}

/**
* @test
*/
public function it_should_work_on_windows_servers_and_should_return_the_same() {
$constants = [
'ABSPATH' => 'C:\\xampp\\htdocs\\wordpress',
'WP_CONTENT_DIR' => 'C:\\xampp\\htdocs\\wordpress\\wp-content',
'WP_CONTENT_URL' => 'http://wordpress.test/wp-content',
'WP_PLUGIN_DIR' => 'C:\\xampp\\htdocs\\wordpress\\wp-content\\plugins',
'WP_PLUGIN_URL' => 'http://wordpress.test/wp-content/plugins',
];

foreach ( $constants as $constant => $value ) {
$this->set_const_value( $constant, $value );
$this->assertEquals( $value, constant( $constant ) );
}

Config::set_path( constant( 'WP_PLUGIN_DIR' ) . '/the-events-calendar/common' );
$this->set_fn_return( 'dirname', static fn( $dir, $level = 1 ) => $level !== 4 ? $dir : ABSPATH . 'foo/', true );

Assets::init();

// Add assets.
$this->add_assets( 'feature-base' );
$this->add_assets( 'feature-editor' );
$this->add_assets( 'feature-frontend' );

foreach ( [ 'feature-base', 'feature-editor', 'feature-frontend' ] as $slug ) {
$this->assertEquals(
home_url( '/wp-content/plugins/the-events-calendar/common/tests/_data/stellar-resources/' . $slug . '.js' ),
Assets::init()->get( $slug )->get_url( false )
);

$this->assertEquals(
Assets::init()->get( $slug )->get_url( false ),
Assets::init()->get( 'stellar-' . $slug )->get_url( false )
);

$this->assertEquals(
home_url( '/wp-content/plugins/the-events-calendar/common/tests/_data/stellar-resources/' . $slug . '.css' ),
Assets::init()->get( $slug. '-style' )->get_url( false )
);

$this->assertEquals(
Assets::init()->get( $slug. '-style' )->get_url( false ),
Assets::init()->get( 'stellar-' . $slug . '-style' )->get_url( false )
);
}
}
}
Loading

0 comments on commit a5f4210

Please sign in to comment.