Skip to content

Commit

Permalink
Dev: Only one WordPress (#41057)
Browse files Browse the repository at this point in the history
Co-authored-by: Brad Jorsch <[email protected]>
  • Loading branch information
kraftbj and anomiex authored Jan 31, 2025
1 parent 1807db5 commit 4597864
Show file tree
Hide file tree
Showing 155 changed files with 1,198 additions and 2,455 deletions.
19 changes: 6 additions & 13 deletions .github/files/setup-wordpress-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ git clone --depth=1 --branch "$WORDPRESS_TAG" git://develop.git.wordpress.org/ "
# We need a built version of WordPress to test against, so download that into the src directory instead of what's in wordpress-develop.
rm -rf "/tmp/wordpress-$WP_BRANCH/src"
git clone --depth=1 --branch "$WORDPRESS_TAG" git://core.git.wordpress.org/ "/tmp/wordpress-$WP_BRANCH/src"

echo "::group::Setting up WordPress uploads directory"
mkdir -p "/tmp/wordpress-$WP_BRANCH/src/wp-content/uploads"
chmod -R 777 "/tmp/wordpress-$WP_BRANCH/src/wp-content/uploads"
echo "::endgroup::"

echo "::endgroup::"

if [[ -n "$GITHUB_ENV" ]]; then
Expand Down Expand Up @@ -105,19 +111,6 @@ for PLUGIN in projects/plugins/*/composer.json; do
fi
cd "$BASE"

# Upgrade/downgrade WorDBless if necessary.
if [[ ( "$WP_BRANCH" == 'trunk' || "$WP_BRANCH" == 'previous' ) && "$TEST_SCRIPT" == "test-php" ]]; then
VER=$(composer --format=json --working-dir="$DIR" show | jq -r '.installed[] | select( .name == "roots/wordpress" ) | .version')
if [[ -n "$VER" ]]; then
INSVER=$WORDPRESS_TAG
[[ "$WORDPRESS_TAG" == 'trunk' ]] && INSVER="dev-main as $VER"
echo "Supposed to run tests against WordPress $WORDPRESS_TAG, so setting roots/wordpress and roots/wordpress-no-content to \"$INSVER\""
# Composer seems to sometimes have issues with deleting the wordpress dir on its own, so do it manually first.
rm -rf "$DIR/wordpress"
composer --working-dir="$DIR" require --dev roots/wordpress="$INSVER" roots/wordpress-no-content="$INSVER"
fi
fi

cp -r "$DIR" "/tmp/wordpress-$WP_BRANCH/src/wp-content/plugins/$NAME"
# Plugin dir for tests in WP >= 5.6-beta1
ln -s "/tmp/wordpress-$WP_BRANCH/src/wp-content/plugins/$NAME" "/tmp/wordpress-$WP_BRANCH/tests/phpunit/data/plugins/$NAME"
Expand Down
58 changes: 37 additions & 21 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,6 @@ jobs:
pnpm install
echo "::endgroup::"
# If we're going to be making WorDBless use WP "nightlies", remove the relevant package from Composer's cache to get the latest version.
if [[ "$WP_BRANCH" == 'trunk' && ( "$TEST_SCRIPT" == "test-php" || "$TEST_SCRIPT" == "test-coverage" ) ]]; then
echo "::group::Clear composer cache for roots/wordpress"
DIR=$(composer config cache-files-dir)
rm -rf "$DIR/roots/wordpress" "$DIR/roots/wordpress-no-content"
echo "::endgroup::"
fi
- name: Detect changed projects
id: changed
run: |
Expand All @@ -110,9 +102,45 @@ jobs:
echo "any-plugins=${ANY_PLUGINS}" >> "$GITHUB_OUTPUT"
- name: Select WordPress version
if: steps.changed.outputs.any-plugins != 'true' && matrix.wp != 'none'
if: matrix.wp != 'none'
run: .github/files/select-wordpress-tag.sh

- name: Composer Install
run: |
# If we're going to be making WorDBless use WP "nightlies", remove the relevant package from Composer's cache to get the latest version.
if [[ "$WP_BRANCH" == 'trunk' && ( "$TEST_SCRIPT" == "test-php" || "$TEST_SCRIPT" == "test-coverage" ) ]]; then
echo "::group::Clear composer cache for roots/wordpress"
DIR=$(composer config cache-files-dir)
rm -rf "$DIR/roots/wordpress" "$DIR/roots/wordpress-no-content"
echo "::endgroup::"
fi
echo "::group::Composer"
composer install --working-dir=tools/php-test-env
# Add WorDBless for various package tests that use uploads since concurrency can nuke uploads that are in use.
# @todo Update WorDBless to be able to use a customized upload dir, do that, and remove this.
if [[ "$TEST_SCRIPT" =~ ^test-(php|coverage)$ ]]; then
VER=$( jq -r '.require["automattic/wordbless"]' tools/php-test-env/composer.json )
for pkg in packages/image-cdn packages/publicize packages/videopress; do
echo "Adding WordBless for $pkg tests"
composer require --working-dir="projects/$pkg" "automattic/wordbless:$VER" --dev --no-update
done
fi
if [[ ( "$TEST_SCRIPT" == "test-php" || "$TEST_SCRIPT" == "test-coverage" ) && ( "$WP_BRANCH" == 'trunk' || "$WP_BRANCH" == 'previous' ) ]]; then
VER=$(composer --format=json --working-dir="tools/php-test-env" show | jq -r '.installed[] | select( .name == "roots/wordpress" ) | .version')
if [[ -n "$VER" ]]; then
INSVER=$WORDPRESS_TAG
[[ "$WORDPRESS_TAG" == 'trunk' ]] && INSVER="dev-main as $VER"
echo "Supposed to run tests against WordPress $WORDPRESS_TAG, so setting roots/wordpress and roots/wordpress-no-content to \"$INSVER\""
# Composer seems to sometimes have issues with deleting the wordpress dir on its own, so do it manually first.
rm -rf "tools/php-test-env/wordpress"
composer --working-dir="tools/php-test-env" require --dev "roots/wordpress:$INSVER" "roots/wordpress-no-content:$INSVER"
fi
fi
echo "::endgroup::"
- name: Setup WordPress environment for plugin tests
env:
API_TOKEN_GITHUB: ${{ secrets.GITHUB_TOKEN }}
Expand Down Expand Up @@ -196,18 +224,6 @@ jobs:
echo 'Platform reqs failed, running `composer update`'
composer --working-dir="$DIR" update
fi
if [[ "$WP_BRANCH" == 'trunk' || "$WP_BRANCH" == 'previous' ]]; then
VER=$(composer --format=json --working-dir="$DIR" show | jq -r '.installed[] | select( .name == "roots/wordpress" ) | .version')
if [[ -n "$VER" ]]; then
INSVER=$WORDPRESS_TAG
[[ "$WORDPRESS_TAG" == 'trunk' ]] && INSVER="dev-main as $VER"
echo "Supposed to run tests against WordPress $WORDPRESS_TAG, so setting roots/wordpress and roots/wordpress-no-content to \"$INSVER\""
# Composer seems to sometimes have issues with deleting the wordpress dir on its own, so do it manually first.
rm -rf "$DIR/wordpress"
composer --working-dir="$DIR" require --dev roots/wordpress="$INSVER" roots/wordpress-no-content="$INSVER"
fi
fi
fi
fi
Expand Down
19 changes: 19 additions & 0 deletions .phan/config.base.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,25 @@ function make_phan_config( $dir, $options = array() ) {
$internal_stubs[ $stub ] = $stub_file_path;
}

// Check if test-environment is a dependency and add WorDBless if it is
$composer_json = $dir . '/composer.json';
if ( file_exists( $composer_json ) ) {
$composer_data = json_decode( file_get_contents( $composer_json ), true );
foreach ( array( 'require', 'require-dev' ) as $require_type ) {
if ( isset( $composer_data[ $require_type ]['automattic/jetpack-test-environment'] ) ) {
// Use absolute path to ensure WorDBless is found
$wordbless_path = dirname( __DIR__ ) . '/tools/php-test-env/vendor/automattic/wordbless';
if ( is_dir( $wordbless_path ) ) {
// Only include the src directory
$options['directory_list'][] = $wordbless_path . '/src';
// Exclude from analysis
$options['exclude_analysis_directory_list'][] = $wordbless_path;
}
break;
}
}
}

$config = array(
// Apparently this is only useful when upgrading from php 5, not for 7-to-8.
'backward_compatibility_checks' => false,
Expand Down
2 changes: 2 additions & 0 deletions .phan/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
// Ignore stuff in various subdirs too.
'.*/node_modules/',
'tools/docker/',
'tools/php-test-env/wordpress/',
'tools/php-test-env/vendor/',
// Don't load the stubs. (if we need to start loading _a_ stub for the "monorepo", do like `(?!filename\.php)` to exclude it from the exclusion.)
'.phan/stubs/',
),
Expand Down
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@
],
"test-js": [
"cd tools/cli && pnpm test"
],
"post-install-cmd": [
"cd tools/php-test-env && composer install"
],
"post-update-cmd": [
"cd tools/php-test-env && composer update"
]
},
"scripts-descriptions": {
Expand Down
14 changes: 7 additions & 7 deletions composer.lock

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

2 changes: 1 addition & 1 deletion docs/automated-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ There are normally two reasons why you would choose integration over unit tests:
Normally, integration tests for packages rely on various mocking solutions available:

- [brain/monkey](https://packagist.org/packages/brain/monkey) - For mocking and stubbing WordPress functions and classes.
- [automattic/wordbless](https://packagist.org/packages/automattic/wordbless) is used to pull in WordPress for testing. As name implies, it's a lightweight version of WordPress, without database and other limitations.
- [automattic/jetpack-test-environment](../projects/packages/test-environment/README.md) is used to pull in WordPress for testing. It calls in a lightweight version of WordPress (via the [WorDBless](https://packagist.org/packages/automattic/wordbless) package) and provides a way to run tests in a WordPress environment. We use the jetpack-test-environment package within the monorepo to only need one install of WordPress for the entire monorepo.

There are a lot of examples on how to use these tools in the `/projects/packages` folder, such as [here](/projects/packages/connection/tests/php/test_Manager_integration.php).

Expand Down
8 changes: 5 additions & 3 deletions docs/monorepo.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,12 @@ We currently make use of the following packages in testing; it's encouraged to u
* Do not use `Yoast\PHPUnitPolyfills\TestCases\TestCase` or `Yoast\PHPUnitPolyfills\TestCases\XTestCase`. Just use the `@before`, `@after`, `@beforeClass`, and `@afterClass` annotations directly.
* PHPUnit's built-in mocking is used for class mocks.
* [brain/monkey](https://packagist.org/packages/brain/monkey) is used for mocking functions, and can also provide some functions for minimal WordPress compatibility.
* [automattic/wordbless](https://packagist.org/packages/automattic/wordbless) is used to pull in WordPress for testing.
* If using both Brain Monkey and WorDBless, note the following requirements:
* You must `require_once __DIR__ . '/../../vendor/antecedent/patchwork/Patchwork.php';` in `bootstrap.php` before WorDBless's setup, so Brain Monkey can mock WordPress functions.
* [automattic/jetpack-test-environment](../projects/packages/test-environment/README.md) is used to pull in WordPress for testing.
* If using both Brain Monkey and the Jetpack Test Environment, note the following requirements:
* You must `require_once __DIR__ . '/../../vendor/antecedent/patchwork/Patchwork.php';` in `bootstrap.php` before the Jetpack Test Environment's setup, so Brain Monkey can mock WordPress functions.
* Follow Brain Monkey's [functions-setup.md](https://github.com/Brain-WP/BrainMonkey/blob/master/docs/functions-testing-tools/functions-setup.md) instead of [wordpress-setup.md](https://github.com/Brain-WP/BrainMonkey/blob/master/docs/wordpress-specific-tools/wordpress-setup.md); don't call `Monkey\setUp()` or try to use its WordPress-specific tools.
* To initiate the Jetpack Test Environment, call `\Automattic\Jetpack\Test_Environment\Bootstrap::init();` in `bootstrap.php`.
* See the [Jetpack Test Environment README](../projects/packages/test-environment/README.md) for more details.
#### PHP tests for plugins
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updating the method of loading WordPress for testing. No functional change and the tests don't currently exist either way :)


9 changes: 1 addition & 8 deletions projects/packages/account-protection/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"require-dev": {
"yoast/phpunit-polyfills": "^1.1.1",
"automattic/jetpack-changelogger": "@dev",
"automattic/wordbless": "dev-master"
"automattic/jetpack-test-environment": "@dev"
},
"autoload": {
"classmap": [
Expand All @@ -22,8 +22,6 @@
"phpunit": [
"./vendor/phpunit/phpunit/phpunit --colors=always"
],
"post-install-cmd": "WorDBless\\Composer\\InstallDropin::copy",
"post-update-cmd": "WorDBless\\Composer\\InstallDropin::copy",
"test-coverage": [
"php -dpcov.directory=. ./vendor/bin/phpunit --coverage-php \"$COVERAGE_DIR/php.cov\""
],
Expand All @@ -42,11 +40,6 @@
],
"minimum-stability": "dev",
"prefer-stable": true,
"config": {
"allow-plugins": {
"roots/wordpress-core-installer": true
}
},
"extra": {
"autotagger": true,
"branch-alias": {
Expand Down
3 changes: 3 additions & 0 deletions projects/packages/account-protection/tests/php/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@
* Include the composer autoloader.
*/
require_once __DIR__ . '/../../vendor/autoload.php';

// Initialize WordPress test environment
\Automattic\Jetpack\Test_Environment::init();
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: added
Comment: Update development platform only


6 changes: 2 additions & 4 deletions projects/packages/admin-ui/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"yoast/phpunit-polyfills": "^1.1.1",
"automattic/jetpack-changelogger": "@dev",
"automattic/jetpack-logo": "@dev",
"automattic/wordbless": "^0.4.2"
"automattic/jetpack-test-environment": "@dev"
},
"suggest": {
"automattic/jetpack-autoloader": "Allow for better interoperability with other plugins that use this package."
Expand All @@ -29,9 +29,7 @@
],
"test-php": [
"@composer phpunit"
],
"post-install-cmd": "WorDBless\\Composer\\InstallDropin::copy",
"post-update-cmd": "WorDBless\\Composer\\InstallDropin::copy"
]
},
"repositories": [
{
Expand Down
3 changes: 2 additions & 1 deletion projects/packages/admin-ui/tests/php/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@

define( 'WP_DEBUG', true );

\WorDBless\Load::load();
// Initialize WordPress test environment
\Automattic\Jetpack\Test_Environment::init();
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: added
Comment: Update development platform only


6 changes: 2 additions & 4 deletions projects/packages/backup-helper-script-manager/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"require-dev": {
"yoast/phpunit-polyfills": "^1.1.1",
"automattic/jetpack-changelogger": "@dev",
"automattic/wordbless": "^0.4.2"
"automattic/jetpack-test-environment": "@dev"
},
"suggest": {
"automattic/jetpack-autoloader": "Allow for better interoperability with other plugins that use this package."
Expand All @@ -28,9 +28,7 @@
],
"test-php": [
"@composer phpunit"
],
"post-install-cmd": "WorDBless\\Composer\\InstallDropin::copy",
"post-update-cmd": "WorDBless\\Composer\\InstallDropin::copy"
]
},
"repositories": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@

define( 'WP_DEBUG', true );

\WorDBless\Load::load();
// Initialize WordPress test environment
\Automattic\Jetpack\Test_Environment::init();
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: added
Comment: Update development platform only


4 changes: 1 addition & 3 deletions projects/packages/backup/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"require-dev": {
"automattic/jetpack-changelogger": "@dev",
"yoast/phpunit-polyfills": "^1.1.1",
"automattic/wordbless": "^0.4.2"
"automattic/jetpack-test-environment": "@dev"
},
"suggest": {
"automattic/jetpack-autoloader": "Allow for better interoperability with other plugins that use this package."
Expand All @@ -43,8 +43,6 @@
"test-php": [
"@composer phpunit"
],
"post-install-cmd": "WorDBless\\Composer\\InstallDropin::copy",
"post-update-cmd": "WorDBless\\Composer\\InstallDropin::copy",
"build-development": [
"pnpm run build"
],
Expand Down
3 changes: 2 additions & 1 deletion projects/packages/backup/tests/php/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@

define( 'WP_DEBUG', true );

\WorDBless\Load::load();
// Initialize WordPress test environment
\Automattic\Jetpack\Test_Environment::init();
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: added
Comment: Update development platform only


6 changes: 2 additions & 4 deletions projects/packages/blaze/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"require-dev": {
"yoast/phpunit-polyfills": "^1.1.1",
"automattic/jetpack-changelogger": "@dev",
"automattic/wordbless": "^0.4.2"
"automattic/jetpack-test-environment": "@dev"
},
"suggest": {
"automattic/jetpack-autoloader": "Allow for better interoperability with other plugins that use this package."
Expand Down Expand Up @@ -45,9 +45,7 @@
"watch": [
"Composer\\Config::disableProcessTimeout",
"pnpm run watch"
],
"post-install-cmd": "WorDBless\\Composer\\InstallDropin::copy",
"post-update-cmd": "WorDBless\\Composer\\InstallDropin::copy"
]
},
"repositories": [
{
Expand Down
Loading

0 comments on commit 4597864

Please sign in to comment.