From 937b0b2eba73007f5b50402a6b2e485e7a34e920 Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Tue, 22 Oct 2024 18:59:14 +0300 Subject: [PATCH] Adding edge test case --- tests/wpunit/AssetsTest.php | 56 ++++++++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 7 deletions(-) diff --git a/tests/wpunit/AssetsTest.php b/tests/wpunit/AssetsTest.php index 0c08af0..44d141e 100644 --- a/tests/wpunit/AssetsTest.php +++ b/tests/wpunit/AssetsTest.php @@ -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 ) { @@ -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 ); } } @@ -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 = [ [ @@ -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 ); @@ -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.