From e88e9b6c793b5101aa4b48dd2f3fdca86789c88f Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Sat, 16 Mar 2024 11:31:21 +0100 Subject: [PATCH] tweak test environments * update bootsstrap and helper scripts * downgrade PHPUnit polyfills * use minimum WP 5.2 for CI tests --- .github/workflows/tests.yml | 2 +- bin/install-wp-tests.sh | 59 ++++++++++++++++++++++++------------- composer.json | 2 +- tests/bootstrap.php | 14 ++++++--- 4 files changed, 51 insertions(+), 26 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index d2221688..9dae8ac9 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -7,7 +7,7 @@ jobs: matrix: include: - php: '7.2' - wordpress: '5.1' + wordpress: '5.2' - php: '7.4' wordpress: '5.9' - php: '8.0' diff --git a/bin/install-wp-tests.sh b/bin/install-wp-tests.sh index b9447efd..a748e99b 100644 --- a/bin/install-wp-tests.sh +++ b/bin/install-wp-tests.sh @@ -15,7 +15,7 @@ SKIP_DB_CREATE=${6-false} TMPDIR=${TMPDIR-/tmp} TMPDIR=$(echo $TMPDIR | sed -e "s/\/$//") WP_TESTS_DIR=${WP_TESTS_DIR-$TMPDIR/wordpress-tests-lib} -WP_CORE_DIR=${WP_CORE_DIR-$TMPDIR/wordpress/} +WP_CORE_DIR=${WP_CORE_DIR-$TMPDIR/wordpress} download() { if [ `which curl` ]; then @@ -25,7 +25,11 @@ download() { fi } -if [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+$ ]]; then +if [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+\-(beta|RC)[0-9]+$ ]]; then + WP_BRANCH=${WP_VERSION%\-*} + WP_TESTS_TAG="branches/$WP_BRANCH" + +elif [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+$ ]]; then WP_TESTS_TAG="branches/$WP_VERSION" elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0-9]+ ]]; then if [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0] ]]; then @@ -59,10 +63,10 @@ install_wp() { mkdir -p $WP_CORE_DIR if [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then - mkdir -p $TMPDIR/wordpress-nightly - download https://wordpress.org/nightly-builds/wordpress-latest.zip $TMPDIR/wordpress-nightly/wordpress-nightly.zip - unzip -q $TMPDIR/wordpress-nightly/wordpress-nightly.zip -d $TMPDIR/wordpress-nightly/ - mv $TMPDIR/wordpress-nightly/wordpress/* $WP_CORE_DIR + mkdir -p $TMPDIR/wordpress-trunk + rm -rf $TMPDIR/wordpress-trunk/* + svn export --quiet https://core.svn.wordpress.org/trunk $TMPDIR/wordpress-trunk/wordpress + mv $TMPDIR/wordpress-trunk/wordpress/* $WP_CORE_DIR else if [ $WP_VERSION == 'latest' ]; then local ARCHIVE_NAME='latest' @@ -89,13 +93,13 @@ install_wp() { tar --strip-components=1 -zxmf $TMPDIR/wordpress.tar.gz -C $WP_CORE_DIR fi - download https://raw.github.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php + download https://raw.githubusercontent.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php } install_test_suite() { # portable in-place argument for both GNU sed and Mac OSX sed if [[ $(uname -s) == 'Darwin' ]]; then - local ioption='-i .bak' + local ioption='-i.bak' else local ioption='-i' fi @@ -104,8 +108,9 @@ install_test_suite() { if [ ! -d $WP_TESTS_DIR ]; then # set up testing suite mkdir -p $WP_TESTS_DIR - svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes - svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ $WP_TESTS_DIR/data + rm -rf $WP_TESTS_DIR/{includes,data} + svn export --quiet --ignore-externals https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes + svn export --quiet --ignore-externals https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ $WP_TESTS_DIR/data fi if [ ! -f wp-tests-config.php ]; then @@ -113,23 +118,30 @@ install_test_suite() { # remove all forward slashes in the end WP_CORE_DIR=$(echo $WP_CORE_DIR | sed "s:/\+$::") sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php + sed $ioption "s:__DIR__ . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_DIR"/wp-tests-config.php sed $ioption "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR"/wp-tests-config.php sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php sed $ioption "s|localhost|${DB_HOST}|" "$WP_TESTS_DIR"/wp-tests-config.php fi - # Modify the WP_UnitTestCase class to use the polyfilled version for PHPUnit cross-compatibility. - # This is a dirty "backport" of the polyfills used in WP 5.9 and might fail with future updates. - if [ ! -f "$WP_TESTS_DIR"/includes/abstract-testcase.php ]; then - local testcase_file="$WP_TESTS_DIR"/includes/testcase.php - sed $ioption 's/class WP_UnitTestCase extends PHPUnit_Framework_TestCase /class WP_UnitTestCase extends Yoast\\PHPUnitPolyfills\\TestCases\\TestCase /' "$testcase_file" - sed $ioption 's/setUpBeforeClass[(][)]/set_up_before_class()/g' "$testcase_file" - sed $ioption 's/tearDownAfterClass[(][)]/tear_down_after_class()/g' "$testcase_file" - sed $ioption 's/setUp[(][)]/set_up()/g' "$testcase_file" - sed $ioption 's/tearDown[(][)]/tear_down()/g' "$testcase_file" +} + +recreate_db() { + shopt -s nocasematch + if [[ $1 =~ ^(y|yes)$ ]] + then + mysqladmin drop $DB_NAME -f --user="$DB_USER" --password="$DB_PASS"$EXTRA + create_db + echo "Recreated the database ($DB_NAME)." + else + echo "Leaving the existing database ($DB_NAME) in place." fi + shopt -u nocasematch +} +create_db() { + mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA } install_db() { @@ -155,7 +167,14 @@ install_db() { fi # create database - mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA + if [ $(mysql --user="$DB_USER" --password="$DB_PASS"$EXTRA --execute='show databases;' | grep ^$DB_NAME$) ] + then + echo "Reinstalling will delete the existing test database ($DB_NAME)" + read -p 'Are you sure you want to proceed? [y/N]: ' DELETE_EXISTING_DB + recreate_db $DELETE_EXISTING_DB + else + create_db + fi } install_wp diff --git a/composer.json b/composer.json index a5b23d5f..69b2bbd3 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "phpcompatibility/phpcompatibility-wp": "^2.1", "wp-coding-standards/wpcs": "^3.0", "phpunit/phpunit": "^7|^9", - "yoast/phpunit-polyfills": "^2.0" + "yoast/phpunit-polyfills": "^1.1" }, "repositories": [ { diff --git a/tests/bootstrap.php b/tests/bootstrap.php index e9bda043..26074ccc 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -11,13 +11,19 @@ $_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib'; } -if ( ! file_exists( $_tests_dir . '/includes/functions.php' ) ) { - echo "Could not find $_tests_dir/includes/functions.php, have you run bin/install-wp-tests.sh ?"; +// Forward custom PHPUnit Polyfills configuration to PHPUnit bootstrap file. +$_phpunit_polyfills_path = getenv( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH' ); +if ( false !== $_phpunit_polyfills_path ) { + define( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH', $_phpunit_polyfills_path ); +} + +if ( ! file_exists( "{$_tests_dir}/includes/functions.php" ) ) { + echo "Could not find {$_tests_dir}/includes/functions.php, have you run bin/install-wp-tests.sh ?" . PHP_EOL; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped exit( 1 ); } // Give access to tests_add_filter() function. -require_once $_tests_dir . '/includes/functions.php'; +require_once "{$_tests_dir}/includes/functions.php"; /** * Manually load the plugin being tested. @@ -29,7 +35,7 @@ function _manually_load_plugin() { tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' ); // Start up the WP testing environment. -require $_tests_dir . '/includes/bootstrap.php'; +require "{$_tests_dir}/includes/bootstrap.php"; // Statify-specific test support. require_once __DIR__ . '/trait-statify-test-support.php';