-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2274 from the-events-calendar/slr/fast-follow-1
Slr/fast follow 1
- Loading branch information
Showing
17 changed files
with
322 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
Empty file.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
Empty file.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) | ||
); | ||
} | ||
} | ||
} |
Oops, something went wrong.