diff --git a/.github/workflows/php-test-plugins.yml b/.github/workflows/php-test-plugins.yml index 5ebd8738b0..ec1aebda1b 100644 --- a/.github/workflows/php-test-plugins.yml +++ b/.github/workflows/php-test-plugins.yml @@ -13,7 +13,6 @@ on: - '**/package.json' - 'package-lock.json' - 'phpunit.xml.dist' - - 'tests/multisite.xml' - 'composer.json' - 'composer.lock' pull_request: @@ -25,7 +24,6 @@ on: - '**/package.json' - 'package-lock.json' - 'phpunit.xml.dist' - - 'tests/multisite.xml' - 'composer.json' - 'composer.lock' types: diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 9182f7e46a..7d675ac7da 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -5,7 +5,6 @@ parameters: treatPhpDocTypesAsCertain: false paths: - plugins - - tests - performance.php - plugins/performance-lab/load.php bootstrapFiles: diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 4b09476b23..dabb920a24 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,7 +1,7 @@ array( - 'image_path' => __DIR__ . '/images/animated.gif', + 'image_path' => TESTS_PLUGIN_DIR . '/tests/data/images/animated.gif', 'expected_color' => array( '874e4e', '864e4e', 'df7f7f' ), 'expected_transparency' => true, ), 'red_jpg' => array( - 'image_path' => __DIR__ . '/images/red.jpg', + 'image_path' => TESTS_PLUGIN_DIR . '/tests/data/images/red.jpg', 'expected_color' => array( 'ff0000', 'fe0000' ), 'expected_transparency' => false, ), 'green_jpg' => array( - 'image_path' => __DIR__ . '/images/green.jpg', + 'image_path' => TESTS_PLUGIN_DIR . '/tests/data/images/green.jpg', 'expected_color' => array( '00ff00', '00ff01', '02ff01' ), 'expected_transparency' => false, ), 'white_jpg' => array( - 'image_path' => __DIR__ . '/images/white.jpg', + 'image_path' => TESTS_PLUGIN_DIR . '/tests/data/images/white.jpg', 'expected_color' => array( 'ffffff' ), 'expected_transparency' => false, ), 'red_gif' => array( - 'image_path' => __DIR__ . '/images/red.gif', + 'image_path' => TESTS_PLUGIN_DIR . '/tests/data/images/red.gif', 'expected_color' => array( 'ff0000' ), 'expected_transparency' => false, ), 'green_gif' => array( - 'image_path' => __DIR__ . '/images/green.gif', + 'image_path' => TESTS_PLUGIN_DIR . '/tests/data/images/green.gif', 'expected_color' => array( '00ff00' ), 'expected_transparency' => false, ), 'white_gif' => array( - 'image_path' => __DIR__ . '/images/white.gif', + 'image_path' => TESTS_PLUGIN_DIR . '/tests/data/images/white.gif', 'expected_color' => array( 'ffffff' ), 'expected_transparency' => false, ), 'trans_gif' => array( - 'image_path' => __DIR__ . '/images/trans.gif', + 'image_path' => TESTS_PLUGIN_DIR . '/tests/data/images/trans.gif', 'expected_color' => array( '5a5a5a', '020202' ), 'expected_transparency' => true, ), 'red_png' => array( - 'image_path' => __DIR__ . '/images/red.png', + 'image_path' => TESTS_PLUGIN_DIR . '/tests/data/images/red.png', 'expected_color' => array( 'ff0000' ), 'expected_transparency' => false, ), 'green_png' => array( - 'image_path' => __DIR__ . '/images/green.png', + 'image_path' => TESTS_PLUGIN_DIR . '/tests/data/images/green.png', 'expected_color' => array( '00ff00' ), 'expected_transparency' => false, ), 'white_png' => array( - 'image_path' => __DIR__ . '/images/white.png', + 'image_path' => TESTS_PLUGIN_DIR . '/tests/data/images/white.png', 'expected_color' => array( 'ffffff' ), 'expected_transparency' => false, ), 'trans_png' => array( - 'image_path' => __DIR__ . '/images/trans.png', + 'image_path' => TESTS_PLUGIN_DIR . '/tests/data/images/trans.png', 'expected_color' => array( '000000' ), 'expected_transparency' => true, ), 'red_webp' => array( - 'image_path' => __DIR__ . '/images/red.webp', + 'image_path' => TESTS_PLUGIN_DIR . '/tests/data/images/red.webp', 'expected_color' => array( 'ff0000' ), 'expected_transparency' => false, ), 'green_webp' => array( - 'image_path' => __DIR__ . '/images/green.webp', + 'image_path' => TESTS_PLUGIN_DIR . '/tests/data/images/green.webp', 'expected_color' => array( '00ff00' ), 'expected_transparency' => false, ), 'white_webp' => array( - 'image_path' => __DIR__ . '/images/white.webp', + 'image_path' => TESTS_PLUGIN_DIR . '/tests/data/images/white.webp', 'expected_color' => array( 'ffffff' ), 'expected_transparency' => false, ), 'trans_webp' => array( - 'image_path' => __DIR__ . '/images/trans.webp', + 'image_path' => TESTS_PLUGIN_DIR . '/tests/data/images/trans.webp', 'expected_color' => array( '000000' ), 'expected_transparency' => true, ), @@ -101,7 +101,7 @@ public function provider_get_dominant_color(): array { 'expected_transparency' => false, ), 'half_opaque' => array( - 'image_path' => __DIR__ . '/images/half-opaque.png', + 'image_path' => TESTS_PLUGIN_DIR . '/tests/data/images/half-opaque.png', 'expected_color' => array( '7e7e7e' ), 'expected_transparency' => true, ), @@ -116,10 +116,10 @@ public function provider_get_dominant_color(): array { public function provider_get_dominant_color_invalid_images(): array { return array( 'tiff' => array( - 'image_path' => __DIR__ . '/images/test-image.tiff', + 'image_path' => TESTS_PLUGIN_DIR . '/tests/data/images/test-image.tiff', ), 'bmp' => array( - 'image_path' => __DIR__ . '/images/test-image.bmp', + 'image_path' => TESTS_PLUGIN_DIR . '/tests/data/images/test-image.bmp', ), ); } @@ -132,10 +132,10 @@ public function provider_get_dominant_color_invalid_images(): array { public function provider_get_dominant_color_none_images(): array { return array( 'pdf' => array( - 'files_path' => __DIR__ . '/images/wordpress-gsoc-flyer.pdf', + 'files_path' => TESTS_PLUGIN_DIR . '/tests/data/images/wordpress-gsoc-flyer.pdf', ), 'mp4' => array( - 'files_path' => __DIR__ . '/images/small-video.mp4', + 'files_path' => TESTS_PLUGIN_DIR . '/tests/data/images/small-video.mp4', ), ); } diff --git a/tests/data/images/balloons.webp b/plugins/dominant-color-images/tests/data/images/balloons.webp similarity index 100% rename from tests/data/images/balloons.webp rename to plugins/dominant-color-images/tests/data/images/balloons.webp diff --git a/plugins/dominant-color-images/tests/test-dominant-color.php b/plugins/dominant-color-images/tests/test-dominant-color.php index 7214f765cc..9c321c9c8e 100644 --- a/plugins/dominant-color-images/tests/test-dominant-color.php +++ b/plugins/dominant-color-images/tests/test-dominant-color.php @@ -162,7 +162,7 @@ public function test_tag_add_adjust_to_image_attributes( string $image_path, arr * @param bool $expected Whether the dominant color should be added. */ public function test_dominant_color_img_tag_add_dominant_color_requires_proper_quotes( string $image, bool $expected ): void { - $attachment_id = self::factory()->attachment->create_upload_object( __DIR__ . '/data/images/red.jpg' ); + $attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/red.jpg' ); wp_maybe_generate_attachment_metadata( get_post( $attachment_id ) ); $image_url = wp_get_attachment_image_url( $attachment_id ); @@ -206,7 +206,7 @@ public function data_dominant_color_img_tag_add_dominant_color_requires_proper_q * @param string $expected The expected style attribute and value. */ public function test_dominant_color_img_tag_add_dominant_color_should_add_dominant_color_inline_style( string $filtered_image, string $expected ): void { - $attachment_id = self::factory()->attachment->create_upload_object( __DIR__ . '/data/images/red.jpg' ); + $attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/red.jpg' ); wp_maybe_generate_attachment_metadata( get_post( $attachment_id ) ); list( $src, $width, $height ) = wp_get_attachment_image_src( $attachment_id ); @@ -246,7 +246,7 @@ public function data_provider_dominant_color_check_inline_style(): array { * @param string $expected The expected style attribute and value. */ public function test_dominant_color_update_attachment_image_attributes( string $style_attr, string $expected ): void { - $attachment_id = self::factory()->attachment->create_upload_object( __DIR__ . '/data/images/red.jpg' ); + $attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/red.jpg' ); $attachment_image = wp_get_attachment_image( $attachment_id, 'full', false, array( 'style' => $style_attr ) ); $this->assertStringContainsString( $expected, $attachment_image ); diff --git a/plugins/embed-optimizer/tests/bootstrap.php b/plugins/embed-optimizer/tests/bootstrap.php index 260eb784d5..43203c20b0 100644 --- a/plugins/embed-optimizer/tests/bootstrap.php +++ b/plugins/embed-optimizer/tests/bootstrap.php @@ -6,4 +6,7 @@ */ // Require the suggested plugin. -require_once __DIR__ . '/../../optimization-detective/load.php'; +require_once TESTS_REPO_ROOT_DIR . '/plugins/optimization-detective/load.php'; + +// Load the test helpers. +require_once TESTS_REPO_ROOT_DIR . '/plugins/optimization-detective/tests/class-optimization-detective-test-helpers.php'; diff --git a/plugins/image-prioritizer/tests/bootstrap.php b/plugins/image-prioritizer/tests/bootstrap.php new file mode 100644 index 0000000000..c151e78081 --- /dev/null +++ b/plugins/image-prioritizer/tests/bootstrap.php @@ -0,0 +1,9 @@ + tests/* + tools/phpunit/* tests/* + tools/phpunit/* tests/* + tools/phpunit/* tests/* diff --git a/tools/phpstan/constants.php b/tools/phpstan/constants.php index ae469bf4dc..bae8b53db4 100644 --- a/tools/phpstan/constants.php +++ b/tools/phpstan/constants.php @@ -6,6 +6,7 @@ */ define( 'TESTS_PLUGIN_DIR', './' ); +define( 'TESTS_REPO_ROOT_DIR', './' ); define( 'WPINC', 'wp-includes' ); define( 'SPECULATION_RULES_VERSION', '0.0.0' ); diff --git a/tests/bootstrap.php b/tools/phpunit/bootstrap.php similarity index 79% rename from tests/bootstrap.php rename to tools/phpunit/bootstrap.php index b2a0fc08a3..b845c92fde 100644 --- a/tests/bootstrap.php +++ b/tools/phpunit/bootstrap.php @@ -5,18 +5,18 @@ * @package performance-lab */ -define( 'TESTS_PLUGIN_DIR', dirname( __DIR__ ) ); +define( 'TESTS_REPO_ROOT_DIR', dirname( __DIR__, 2 ) ); // Determine correct location for plugins directory to use. if ( false !== getenv( 'WP_PLUGIN_DIR' ) ) { define( 'WP_PLUGIN_DIR', getenv( 'WP_PLUGIN_DIR' ) ); } else { - define( 'WP_PLUGIN_DIR', dirname( TESTS_PLUGIN_DIR ) ); + define( 'WP_PLUGIN_DIR', dirname( TESTS_REPO_ROOT_DIR ) ); } // Load Composer dependencies if applicable. -if ( file_exists( TESTS_PLUGIN_DIR . '/vendor/autoload.php' ) ) { - require_once TESTS_PLUGIN_DIR . '/vendor/autoload.php'; +if ( file_exists( TESTS_REPO_ROOT_DIR . '/vendor/autoload.php' ) ) { + require_once TESTS_REPO_ROOT_DIR . '/vendor/autoload.php'; } // Detect where to load the WordPress tests environment from. @@ -26,8 +26,8 @@ $_test_root = getenv( 'WP_DEVELOP_DIR' ) . '/tests/phpunit'; } elseif ( false !== getenv( 'WP_PHPUNIT__DIR' ) ) { $_test_root = getenv( 'WP_PHPUNIT__DIR' ); -} elseif ( file_exists( TESTS_PLUGIN_DIR . '/../../../../tests/phpunit/includes/functions.php' ) ) { - $_test_root = TESTS_PLUGIN_DIR . '/../../../../tests/phpunit'; +} elseif ( file_exists( TESTS_REPO_ROOT_DIR . '/../../../../tests/phpunit/includes/functions.php' ) ) { + $_test_root = TESTS_REPO_ROOT_DIR . '/../../../../tests/phpunit'; } else { // Fallback. $_test_root = '/tmp/wordpress-tests-lib'; } @@ -47,7 +47,7 @@ if ( '--testsuite' === $arg && isset( $_SERVER['argv'][ $index + 1 ] ) && - file_exists( TESTS_PLUGIN_DIR . '/plugins/' . $_SERVER['argv'][ $index + 1 ] ) + file_exists( TESTS_REPO_ROOT_DIR . '/plugins/' . $_SERVER['argv'][ $index + 1 ] ) ) { $plugin_name = $_SERVER['argv'][ $index + 1 ]; } @@ -58,13 +58,15 @@ $plugin_name = 'performance-lab'; } +define( 'TESTS_PLUGIN_DIR', TESTS_REPO_ROOT_DIR . "/plugins/$plugin_name" ); + /** * Load plugin bootstrap and any dependencies. * * @param string $plugin_name Plugin slug to load. */ $load_plugin = static function ( string $plugin_name ) use ( &$load_plugin ): void { - $plugin_test_path = TESTS_PLUGIN_DIR . '/plugins/' . $plugin_name; + $plugin_test_path = TESTS_REPO_ROOT_DIR . '/plugins/' . $plugin_name; if ( file_exists( $plugin_test_path . '/' . $plugin_name . '.php' ) ) { $plugin_file = $plugin_test_path . '/' . $plugin_name . '.php'; } elseif ( file_exists( $plugin_test_path . '/load.php' ) ) { @@ -102,9 +104,9 @@ static function () use ( $load_plugin, $plugin_name ): void { tests_add_filter( 'plugins_loaded', static function () use ( $plugin_name ): void { - require_once TESTS_PLUGIN_DIR . '/plugins/' . $plugin_name . '/includes/admin/load.php'; - require_once TESTS_PLUGIN_DIR . '/plugins/' . $plugin_name . '/includes/admin/server-timing.php'; - require_once TESTS_PLUGIN_DIR . '/plugins/' . $plugin_name . '/includes/admin/plugins.php'; + require_once TESTS_REPO_ROOT_DIR . '/plugins/' . $plugin_name . '/includes/admin/load.php'; + require_once TESTS_REPO_ROOT_DIR . '/plugins/' . $plugin_name . '/includes/admin/server-timing.php'; + require_once TESTS_REPO_ROOT_DIR . '/plugins/' . $plugin_name . '/includes/admin/plugins.php'; }, 1 ); @@ -127,8 +129,5 @@ static function ( bool $passthrough ): bool { } ); -// Require helper classes. -require_once __DIR__ . '/class-optimization-detective-test-helpers.php'; - // Start up the WP testing environment. require $_test_root . '/includes/bootstrap.php';