Skip to content

Commit

Permalink
Adding edge test case
Browse files Browse the repository at this point in the history
  • Loading branch information
dpanta94 committed Oct 22, 2024
1 parent b4fc7a1 commit 937b0b2
Showing 1 changed file with 49 additions and 7 deletions.
56 changes: 49 additions & 7 deletions tests/wpunit/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ public function it_should_locate_minified_versions_of_external_assets() {
*/
public function it_should_get_the_correct_url_when_wp_content_dir_and_wp_content_url_are_diff( $id, $constants ) {
$slugs = [
'fake1' => [ true, false ],
'fake2' => [ false, false ],
'fake3' => [ true, true ]
'fake1' => [ 'has_min' => true, 'has_only_min' => false ],
'fake2' => [ 'has_min' => false, 'has_only_min' => false ],
'fake3' => [ 'has_min' => true, 'has_only_min' => true ]
];

foreach ( array_keys( $slugs ) as $slug ) {
Expand All @@ -115,8 +115,8 @@ public function it_should_get_the_correct_url_when_wp_content_dir_and_wp_content
}

foreach ( $slugs as $slug => $data ) {
$this->assert_minified_found( $slug, true, $data['0'], $data['1'], $id );
$this->assert_minified_found( $slug, false, $data['0'], $data['1'], $id );
$this->assert_minified_found( $slug, true, $data['has_min'], $data['has_only_min'], $id );
$this->assert_minified_found( $slug, false, $data['has_min'], $data['has_only_min'], $id );
}
}

Expand Down Expand Up @@ -161,6 +161,48 @@ public function it_should_get_the_correct_url_when_wp_content_dir_and_wp_content
}
}

/**
* @test
*
* @dataProvider constantProvider
*/
public function it_should_get_the_correct_url_when_wp_content_dir_and_wp_content_url_are_diff_and_assets_are_in_asset_group_while_outside_of_root_path( $id, $constants ) {
$slugs = [
'fake1' => [ 'has_min' => true, 'has_only_min' => false ],
'fake2' => [ 'has_min' => false, 'has_only_min' => false ],
'fake3' => [ 'has_min' => true, 'has_only_min' => true ]
];

foreach ( array_keys( $slugs ) as $slug ) {
Assets::init()->remove( $slug . '-script' );
Assets::init()->remove( $slug . '-style' );
}

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

Config::reset();

Config::set_hook_prefix( 'bork' );
Config::set_version( '1.1.0' );
Config::set_path( constant( 'WP_PLUGIN_DIR' ) . '/assets' );
Config::set_relative_asset_path( 'tests/_data/' );
// Now are scripts are using a path that does not actually exist in the filesystem. So we can't expect it to figure out minified vs un-minified. So we are adding a new param.
Config::add_group_path( 'fake-group-path', constant( 'WP_PLUGIN_DIR' ) . '/another-plugin/ecp', 'random/feature' );

foreach ( array_keys( $slugs ) as $slug ) {
Asset::add( $slug . '-script', $slug . '.js' )->add_to_group_path( 'fake-group-path' );
Asset::add( $slug . '-style', $slug . '.css' )->add_to_group_path( 'fake-group-path' );
}

foreach ( $slugs as $slug => $data ) {
$this->assert_minified_found( $slug, true, $data['has_min'], $data['has_only_min'], $id, 'fake-group-path', '/another-plugin/ecp/random/feature/', true );
$this->assert_minified_found( $slug, false, $data['has_min'], $data['has_only_min'], $id, 'fake-group-path', '/another-plugin/ecp/random/feature/', true );
}
}

public function constantProvider() {
$data = [
[
Expand Down Expand Up @@ -861,7 +903,7 @@ protected function existence_assertions( $test_slug_prefix ) {
* @param string $add_to_path_group
* @param string $group_path_path
*/
protected function assert_minified_found( $slug_prefix, $is_js = true, $has_min = true, $has_only_min = false, $id = '', $add_to_path_group = '', $group_path_path = '' ) {
protected function assert_minified_found( $slug_prefix, $is_js = true, $has_min = true, $has_only_min = false, $id = '', $add_to_path_group = '', $group_path_path = '', $wont_figure_out_min_vs_unmin = false ) {
$asset = Assets::init()->get( $slug_prefix . '-' . ( $is_js ? 'script' : 'style' ) );

$url = plugins_url( ( $group_path_path ? $group_path_path : '/assets/tests/_data/' ) . ( $is_js ? 'js' : 'css' ) . '/' . $slug_prefix );
Expand All @@ -885,7 +927,7 @@ protected function assert_minified_found( $slug_prefix, $is_js = true, $has_min

$plugins_path = str_replace( constant( 'WP_CONTENT_DIR' ), '', constant( 'WP_PLUGIN_DIR' ) );

if ( constant( 'WP_PLUGIN_DIR' ) !== constant( 'WP_CONTENT_DIR' ) . $plugins_path || strpos( constant( 'ABSPATH' ), 'C:') === 0 ) {
if ( constant( 'WP_PLUGIN_DIR' ) !== constant( 'WP_CONTENT_DIR' ) . $plugins_path || strpos( constant( 'ABSPATH' ), 'C:') === 0 || $wont_figure_out_min_vs_unmin ) {
// If we are testing outside of the actual plugin directory, the file_exists will always fail.
// In installations where this set up is the actual, the file should exist.
// In this case it will always fail to locate mins.
Expand Down

0 comments on commit 937b0b2

Please sign in to comment.