From 71cde2c8569dd641a1b6fb268affd1c256306760 Mon Sep 17 00:00:00 2001 From: Jamel Noel Reid Date: Thu, 13 Apr 2023 18:06:53 -0500 Subject: [PATCH] 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 --- plugins/woocommerce/.wp-env.json | 41 ++++---- .../add-e2e-tests-wp-version-support | 4 + plugins/woocommerce/package.json | 7 +- plugins/woocommerce/tests/e2e-pw/README.md | 21 +++- .../tests/e2e-pw/bin/test-env-setup.sh | 34 +++---- .../tests/e2e-pw/bin/update-wp-env.php | 99 +++++++++++++++++++ plugins/woocommerce/tests/wp-cli.yml | 2 + 7 files changed, 168 insertions(+), 40 deletions(-) create mode 100644 plugins/woocommerce/changelog/add-e2e-tests-wp-version-support create mode 100644 plugins/woocommerce/tests/e2e-pw/bin/update-wp-env.php create mode 100644 plugins/woocommerce/tests/wp-cli.yml diff --git a/plugins/woocommerce/.wp-env.json b/plugins/woocommerce/.wp-env.json index be25f189c38..618c4a21fdf 100644 --- a/plugins/woocommerce/.wp-env.json +++ b/plugins/woocommerce/.wp-env.json @@ -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 + } + } + } +} \ No newline at end of file diff --git a/plugins/woocommerce/changelog/add-e2e-tests-wp-version-support b/plugins/woocommerce/changelog/add-e2e-tests-wp-version-support new file mode 100644 index 00000000000..0525f22750f --- /dev/null +++ b/plugins/woocommerce/changelog/add-e2e-tests-wp-version-support @@ -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 diff --git a/plugins/woocommerce/package.json b/plugins/woocommerce/package.json index 827d2042bf8..4c235643577 100644 --- a/plugins/woocommerce/package.json +++ b/plugins/woocommerce/package.json @@ -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", diff --git a/plugins/woocommerce/tests/e2e-pw/README.md b/plugins/woocommerce/tests/e2e-pw/README.md index 4cb9d6fb2a8..5df175f010b 100644 --- a/plugins/woocommerce/tests/e2e-pw/README.md +++ b/plugins/woocommerce/tests/e2e-pw/README.md @@ -57,13 +57,30 @@ To see all options, run `cd plugins/woocommerce && pnpm playwright test --help` The default values are: -- Latest stable WordPress version +- Latest stable WordPress version - PHP 7.4 - MariaDB - 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. diff --git a/plugins/woocommerce/tests/e2e-pw/bin/test-env-setup.sh b/plugins/woocommerce/tests/e2e-pw/bin/test-env-setup.sh index 9f3d7601740..623f4caa128 100755 --- a/plugins/woocommerce/tests/e2e-pw/bin/test-env-setup.sh +++ b/plugins/woocommerce/tests/e2e-pw/bin/test-env-setup.sh @@ -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 diff --git a/plugins/woocommerce/tests/e2e-pw/bin/update-wp-env.php b/plugins/woocommerce/tests/e2e-pw/bin/update-wp-env.php new file mode 100644 index 00000000000..07e5be7c0db --- /dev/null +++ b/plugins/woocommerce/tests/e2e-pw/bin/update-wp-env.php @@ -0,0 +1,99 @@ +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(); +} diff --git a/plugins/woocommerce/tests/wp-cli.yml b/plugins/woocommerce/tests/wp-cli.yml new file mode 100644 index 00000000000..3cf7565b3bc --- /dev/null +++ b/plugins/woocommerce/tests/wp-cli.yml @@ -0,0 +1,2 @@ +apache_modules: + - mod_rewrite