Add support for defining WP, WC & PHP version for e2e & api tests (#37587)

* Added wp-cli.yml to test environment

* Run docker compose directly and fix permission

Running docker-compose directly circumvents the permission issues that arise occur when we try to specify a WP version for wp-env.

This commit also normalizes the permission for wp-config.php file which allows wp-cli to properly update the url structure of the site

* Added changelog

* Normalized wp-env wp-content permissions

* Added script to support setting WP Version

* Added support for setting WC & PHP versions

* Fixed phpversion update

* Fixed WooCommerce update

* Used user

* Updated changelog

* Told PHPCS to ignore file

* Added support for WP trunk

* Added support for nightly versions fo WP

* Fixed bug

* Updated docs
This commit is contained in:
Jamel Noel Reid 2023-04-13 18:06:53 -05:00 committed by GitHub
parent ce3d3e3cc9
commit 71cde2c856
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 168 additions and 40 deletions

View File

@ -1,19 +1,24 @@
{
"phpVersion": "7.4",
"plugins": [ "." ],
"config": {
"JETPACK_AUTOLOAD_DEV": true,
"WP_DEBUG_LOG": true,
"WP_DEBUG_DISPLAY": true,
"ALTERNATE_WP_CRON": true
},
"env": {
"development": {},
"tests": {
"port": 8086,
"config": {
"ALTERNATE_WP_CRON": false
}
}
}
"phpVersion": "7.4",
"plugins": [
"."
],
"config": {
"JETPACK_AUTOLOAD_DEV": true,
"WP_DEBUG_LOG": true,
"WP_DEBUG_DISPLAY": true,
"ALTERNATE_WP_CRON": true
},
"mappings": {
"wp-cli.yml": ".\/tests\/wp-cli.yml"
},
"env": {
"development": [],
"tests": {
"port": 8086,
"config": {
"ALTERNATE_WP_CRON": false
}
}
}
}

View File

@ -0,0 +1,4 @@
Significance: patch
Type: add
Allows the WP, WC & PHP version to be specified in .wp-env.json for e2e and api tests

View File

@ -30,16 +30,17 @@
"docker:ssh": "pnpm exec wc-e2e docker:ssh",
"docker:up": "pnpm exec wc-e2e docker:up",
"env:dev": "pnpm wp-env start",
"env:test": "pnpm run env:dev && ./tests/e2e-pw/bin/test-env-setup.sh",
"update-wp-env": "php ./tests/e2e-pw/bin/update-wp-env.php",
"env:test": "pnpm run update-wp-env && pnpm run env:dev && ./tests/e2e-pw/bin/test-env-setup.sh",
"test:e2e-pw": "USE_WP_ENV=1 pnpm playwright test --config=tests/e2e-pw/playwright.config.js",
"test:api-pw": "USE_WP_ENV=1 pnpm playwright test --config=tests/api-core-tests/playwright.config.js",
"env:start": "pnpm wp-env start && ./tests/e2e-pw/bin/test-env-setup.sh",
"env:restart": "pnpm wp-env destroy && pnpm wp-env start && ./tests/e2e-pw/bin/test-env-setup.sh",
"env:stop": "pnpm wp-env stop",
"env:test:cot": "pnpm run env:dev && ENABLE_HPOS=1 ./tests/e2e-pw/bin/test-env-setup.sh",
"env:test:cot": "pnpm run update-wp-env && pnpm run env:dev && ENABLE_HPOS=1 ./tests/e2e-pw/bin/test-env-setup.sh",
"env:performance-init": "./tests/performance/bin/init-sample-products.sh",
"env:down": "pnpm wp-env stop",
"env:destroy": "pnpm wp-env destroy",
"env:destroy": "pnpm run update-wp-env && pnpm wp-env destroy",
"test:api": "API_TEST_REPORT_DIR=\"$PWD/tests/api\" pnpm exec wc-api-tests test api",
"make:collection": "pnpm exec wc-api-tests make:collection",
"e2e:debug": "pnpm exec wc-e2e test:e2e-debug",

View File

@ -63,7 +63,24 @@ The default values are:
- URL: `http://localhost:8086/`
- Admin credentials: `admin/password`
If you want to customize these, check the [Test Variables](#test-variables) section.
If you want to customize the port or admin credentials, check the [Test Variables](#test-variables) section.
If you would like to customize the `PHP`, `WordPress` or `WooCommerce` versions installed in the environment, you can define `UPDATE_WP_JSON_FILE=1` along with any or all of the following env vars when building the environment.
- `WP_VERSION`
- Acceptable versions are `nightly`, `trunk`, and any version listed on [WordPress Releases] page.
- `WC_VERSION`
- Acceptable versions can be found on the [WooCommerce Releases](https://github.com/woocommerce/woocommerce/releases) page
- `PHP`
- Any PHP version you see it. Please note that WooCommerce requries a minimum of PHP 7.2.
**Example**
The command below will create and environment with WordPress version 6.2, WooCommerce version 7.5.1 and PHP version 8.2 installed.
`UPDATE_WP_JSON_FILE=1 WP_VERSION=6.2 WC_TEST_VERSION=7.5.1 PHP_VERSION=8.2 pnpm run env:test`
If you'd like to run with the default configuation, simply remove the `UPDATE_WP_JSON_FILE`.
For more information how to configure the test environment for `wp-env`, please checkout the [documentation](https://github.com/WordPress/gutenberg/tree/trunk/packages/env) documentation.

View File

@ -4,39 +4,39 @@ ENABLE_HPOS="${ENABLE_HPOS:-0}"
ENABLE_NEW_PRODUCT_EDITOR="${ENABLE_NEW_PRODUCT_EDITOR:-0}"
ENABLE_TRACKING="${ENABLE_TRACKING:-0}"
wp-env run tests-cli "wp theme install twentynineteen --activate"
echo -e 'Normalize permissions for wp-content directory \n'
docker-compose -f $(wp-env install-path)/docker-compose.yml run --rm -u www-data -e HOME=/tmp tests-wordpress sh -c "chmod -c ugo+w /var/www/html/wp-config.php \
&& chmod -c ugo+w /var/www/html/wp-content \
&& chmod -c ugo+w /var/www/html/wp-content/themes \
&& chmod -c ugo+w /var/www/html/wp-content/plugins"
wp-env run tests-cli "wp plugin install https://github.com/WP-API/Basic-Auth/archive/master.zip --activate"
wp-env run tests-cli "wp plugin install wp-mail-logging --activate"
wp-env run tests-cli "wp plugin install https://github.com/woocommerce/woocommerce-reset/archive/refs/heads/trunk.zip --activate"
wp-env run tests-cli "wp rewrite structure /%postname%/"
wp-env run tests-cli "wp user create customer customer@woocommercecoree2etestsuite.com \
docker-compose -f $(wp-env install-path)/docker-compose.yml run --rm -u www-data -e HOME=/tmp tests-cli sh -c "ls \
&& wp theme install twentynineteen --activate \
&& wp plugin install https://github.com/WP-API/Basic-Auth/archive/master.zip --activate \
&& wp plugin install wp-mail-logging --activate \
&& wp plugin install https://github.com/woocommerce/woocommerce-reset/archive/refs/heads/trunk.zip --activate \
&& wp rewrite structure '/%postname%/' --hard \
&& wp user create customer customer@woocommercecoree2etestsuite.com \
--user_pass=password \
--role=subscriber \
--first_name='Jane' \
--last_name='Smith' \
--path=/var/www/html \
--user_registered='2022-01-01 12:23:45'
"
--user_registered='2022-01-01 12:23:45'"
echo -e 'Update Blog Name \n'
wp-env run tests-cli 'wp option update blogname "WooCommerce Core E2E Test Suite"'
docker-compose -f $(wp-env install-path)/docker-compose.yml run --rm -u $(id -u) -e HOME=/tmp tests-cli sh -c 'wp option update blogname "WooCommerce Core E2E Test Suite"'
if [ $ENABLE_HPOS == 1 ]; then
echo 'Enable the COT feature'
wp-env run tests-cli "wp plugin install https://gist.github.com/vedanshujain/564afec8f5e9235a1257994ed39b1449/archive/b031465052fc3e04b17624acbeeb2569ef4d5301.zip --activate"
docker-compose -f $(wp-env install-path)/docker-compose.yml run --rm -u www-data -e HOME=/tmp tests-cli sh -c "wp plugin install https://gist.github.com/vedanshujain/564afec8f5e9235a1257994ed39b1449/archive/b031465052fc3e04b17624acbeeb2569ef4d5301.zip --activate"
fi
if [ $ENABLE_NEW_PRODUCT_EDITOR == 1 ]; then
echo 'Enable the new product editor feature'
wp-env run tests-cli "wp plugin install https://github.com/woocommerce/woocommerce-experimental-enable-new-product-editor/releases/download/0.1.0/woocommerce-experimental-enable-new-product-editor.zip --activate"
docker-compose -f $(wp-env install-path)/docker-compose.yml run --rm -u www-data -e HOME=/tmp tests-cli sh -c "wp plugin install https://github.com/woocommerce/woocommerce-experimental-enable-new-product-editor/releases/download/0.1.0/woocommerce-experimental-enable-new-product-editor.zip --activate"
fi
if [ $ENABLE_TRACKING == 1 ]; then
echo 'Enable tracking'
wp-env run tests-cli "wp option update woocommerce_allow_tracking 'yes'"
docker-compose -f $(wp-env install-path)/docker-compose.yml run --rm -u $(id -u) -e HOME=/tmp tests-cli sh -c "wp option update woocommerce_allow_tracking 'yes'"
fi

View File

@ -0,0 +1,99 @@
<?php
// phpcs:ignoreFile
if ( ! class_exists( UPDATE_WP_JSON::class ) ) {
class UPDATE_WP_JSON {
private $wp_env_path = __DIR__ . '/../../../.wp-env.json';
private $wp_json = [];
private $wc_version = null;
private $wp_version = null;
private $php_version = null;
public function __construct() {
if ( file_exists( $this->wp_env_path ) ) {
$this->wp_json = json_decode( file_get_contents( $this->wp_env_path ), true );
} else {
throw new Exception( ".wp_env.json doesn't exist!" );
}
$env = getenv();
$this->wp_version = isset( $env['WP_VERSION'] ) ? $env['WP_VERSION'] : null;
$this->wc_version = isset( $env['WC_TEST_VERSION'] ) ? $env['WC_TEST_VERSION'] : null;
$this->php_version = isset( $env['PHP_VERSION'] ) ? $env['PHP_VERSION'] : null;
}
public function set_wp_version(){
if ( $this->wp_version ) {
$version = "WordPress/WordPress#tags/$this->wp_version";
if ( 'trunk' === $this->wp_version ) {
$version = "WordPress/WordPress";
}
if ( 'nightly' === $this->wp_version ) {
$version = "https://wordpress.org/nightly-builds/wordpress-latest.zip";
}
echo "Set WP Version to $version \n";
$this->wp_json["core"] = $version;
}
}
public function revert_wp_version(){
unset( $this->wp_json["core"] );
}
public function set_wc_version(){
if ( $this->wc_version ) {
echo "Set WC Version to $this->wc_version \n";
$this->wp_json["plugins"] = [ "https://github.com/woocommerce/woocommerce/releases/download/$this->wc_version/woocommerce.zip" ];
}
}
public function revert_wc_version(){
$this->wp_json["plugins"] = [ "." ];
}
public function set_php_version(){
if ( $this->php_version ) {
echo "Set PHP Version to $this->php_version \n";
$this->wp_json["phpVersion"] = $this->php_version;
}
}
public function revert_php_version(){
$this->wp_json["phpVersion"] = "7.4";
}
public function update(){
$this->set_wp_version();
$this->set_wc_version();
$this->set_php_version();
file_put_contents( $this->wp_env_path, json_encode( $this->wp_json, JSON_PRETTY_PRINT ) );
}
public function revert(){
$this->revert_wp_version();
$this->revert_wc_version();
$this->revert_php_version();
file_put_contents( $this->wp_env_path, json_encode( $this->wp_json, JSON_PRETTY_PRINT ) );
echo "Reverted .wp-env.json \n";
}
}
}
$env = getenv();
$update_wp_json = isset( $env['UPDATE_WP_JSON_FILE'] ) ? $env['UPDATE_WP_JSON_FILE'] : null;
if ( is_null( $update_wp_json ) || $update_wp_json == false ) {
$wp_json = new UPDATE_WP_JSON();
$wp_json->revert();
} else {
$wp_json = new UPDATE_WP_JSON();
$wp_json->update();
}

View File

@ -0,0 +1,2 @@
apache_modules:
- mod_rewrite