diff --git a/.github/workflows/end-to-end-tests.yml b/.github/workflows/end-to-end-tests.yml index ee64fd8490b31..f4769fc22534d 100644 --- a/.github/workflows/end-to-end-tests.yml +++ b/.github/workflows/end-to-end-tests.yml @@ -32,6 +32,7 @@ on: - '!phpcs.xml.dist' - '!phpstan.neon.dist' - '!phpunit.xml.dist' + - '!phpunit10.xml.dist' - '!tsconfig.json' # Other test suites, tools, and workflows do not affect these tests. - '!tests/**' diff --git a/.github/workflows/performance.yml b/.github/workflows/performance.yml index f8c5f54aeda6e..90ab911b7c8c2 100644 --- a/.github/workflows/performance.yml +++ b/.github/workflows/performance.yml @@ -32,6 +32,7 @@ on: - '!phpcs.xml.dist' - '!phpstan.neon.dist' - '!phpunit.xml.dist' + - '!phpunit10.xml.dist' - '!tsconfig.json' # Other test suites, tools, and workflows do not affect these tests. - '!tests/**' diff --git a/.github/workflows/phpunit-tests-full-matrix.yml b/.github/workflows/phpunit-tests-full-matrix.yml index eedbbdb087e4e..dcc0d82da71ab 100644 --- a/.github/workflows/phpunit-tests-full-matrix.yml +++ b/.github/workflows/phpunit-tests-full-matrix.yml @@ -42,6 +42,7 @@ on: - 'tools/vendors/**' - 'tools/webpack/**' - 'phpunit.xml.dist' + - 'phpunit10.xml.dist' - '.github/actions/setup-npm/**' - '.github/workflows/phpunit-tests.yml' - '.github/workflows/phpunit-tests-full-matrix.yml' diff --git a/.github/workflows/phpunit-tests.yml b/.github/workflows/phpunit-tests.yml index 2147698eb1e2b..6b251ae8c2b54 100644 --- a/.github/workflows/phpunit-tests.yml +++ b/.github/workflows/phpunit-tests.yml @@ -46,6 +46,7 @@ on: - 'tools/vendors/**' - 'tools/webpack/**' - 'phpunit.xml.dist' + - 'phpunit10.xml.dist' - '.github/actions/setup-npm/**' - '.github/workflows/phpunit-tests.yml' - '.github/workflows/phpunit-tests-full-matrix.yml' diff --git a/.github/workflows/reusable-phpunit-tests-v1.yml b/.github/workflows/reusable-phpunit-tests-v1.yml index a631b243cd3a9..8ae1f6fe3f2f7 100644 --- a/.github/workflows/reusable-phpunit-tests-v1.yml +++ b/.github/workflows/reusable-phpunit-tests-v1.yml @@ -247,4 +247,4 @@ jobs: - name: Run external HTTP tests if: ${{ ! inputs.multisite && ! inputs.split_slow && ! inputs.phpunit-test-groups }} - run: npm run "test:${PHPUNIT_SCRIPT}" -- --verbose -c phpunit.xml.dist --group external-http + run: npm run "test:${PHPUNIT_SCRIPT}" -- --verbose -c "${PHPUNIT_CONFIG}" --group external-http diff --git a/.github/workflows/reusable-phpunit-tests-v2.yml b/.github/workflows/reusable-phpunit-tests-v2.yml index 139c41efccf8c..687aaf81e5902 100644 --- a/.github/workflows/reusable-phpunit-tests-v2.yml +++ b/.github/workflows/reusable-phpunit-tests-v2.yml @@ -254,7 +254,7 @@ jobs: - name: Run external HTTP tests if: ${{ ! inputs.multisite && ! inputs.split_slow && ! inputs.phpunit-test-groups }} - run: npm run "test:${PHPUNIT_SCRIPT}" -- --verbose -c phpunit.xml.dist --group external-http + run: npm run "test:${PHPUNIT_SCRIPT}" -- --verbose -c "${PHPUNIT_CONFIG}" --group external-http # __fakegroup__ is excluded to force PHPUnit to ignore the settings in phpunit.xml.dist. - name: Run (xDebug) tests diff --git a/.github/workflows/reusable-phpunit-tests-v3.yml b/.github/workflows/reusable-phpunit-tests-v3.yml index c025ea8d1f576..f352254d8b92c 100644 --- a/.github/workflows/reusable-phpunit-tests-v3.yml +++ b/.github/workflows/reusable-phpunit-tests-v3.yml @@ -247,6 +247,11 @@ jobs: with: custom-cache-suffix: $(/bin/date -u --date='last Mon' "+%F") + # Release branches run this workflow from trunk, but older branches lack this file. + - name: Select PHPUnit configuration + if: ${{ hashFiles('tests/phpunit/phpunit-config.php') != '' }} + run: echo "PHPUNIT_CONFIG=$(php ./tests/phpunit/phpunit-config.php "${PHPUNIT_CONFIG}")" >> "$GITHUB_ENV" + - name: Install npm dependencies run: npm ci @@ -296,7 +301,7 @@ jobs: - name: Run external HTTP tests if: ${{ ! inputs.multisite && ! inputs.phpunit-test-groups && ! inputs.coverage-report }} continue-on-error: ${{ inputs.allow-errors }} - run: node ./tools/local-env/scripts/docker.js run php ./vendor/bin/phpunit --verbose -c "${PHPUNIT_CONFIG}" --group external-http + run: node ./tools/local-env/scripts/docker.js run php ./vendor/bin/phpunit -c "${PHPUNIT_CONFIG}" --group external-http # PHPUnit versions used below PHP 7.3 do not support --fail-on-empty-test-suite. - name: Run PHPUnit tests${{ inputs.phpunit-test-groups && format( ' ({0} groups)', inputs.phpunit-test-groups ) || '' }}${{ inputs.coverage-report && ' with coverage report' || '' }} @@ -304,7 +309,6 @@ jobs: run: | node ./tools/local-env/scripts/docker.js ${{ inputs.coverage-report && 'exec' || 'run' }} \ php ./vendor/bin/phpunit \ - --verbose \ -c "${PHPUNIT_CONFIG}" \ ${{ inputs.phpunit-test-groups && '--group "${TEST_GROUPS}"' || '' }} \ ${{ inputs.phpunit-test-groups && inputs.php >= '7.3' && '--fail-on-empty-test-suite' || '' }} \ @@ -373,18 +377,18 @@ jobs: - name: Run AJAX tests if: ${{ ! inputs.phpunit-test-groups && ! inputs.coverage-report }} continue-on-error: ${{ inputs.allow-errors }} - run: node ./tools/local-env/scripts/docker.js run php ./vendor/bin/phpunit --verbose -c "${PHPUNIT_CONFIG}" --group ajax + run: node ./tools/local-env/scripts/docker.js run php ./vendor/bin/phpunit -c "${PHPUNIT_CONFIG}" --group ajax - name: Run ms-files tests as a multisite install if: ${{ inputs.multisite && ! inputs.phpunit-test-groups && ! inputs.coverage-report }} continue-on-error: ${{ inputs.allow-errors }} - run: node ./tools/local-env/scripts/docker.js run php ./vendor/bin/phpunit --verbose -c "${PHPUNIT_CONFIG}" --group ms-files + run: node ./tools/local-env/scripts/docker.js run php ./vendor/bin/phpunit -c "${PHPUNIT_CONFIG}" --group ms-files # __fakegroup__ is excluded to force PHPUnit to ignore the settings in phpunit.xml.dist. - name: Run (Xdebug) tests if: ${{ ! inputs.phpunit-test-groups && ! inputs.coverage-report }} continue-on-error: ${{ inputs.allow-errors }} - run: LOCAL_PHP_XDEBUG=true node ./tools/local-env/scripts/docker.js run php ./vendor/bin/phpunit -v --group xdebug --exclude-group __fakegroup__ + run: LOCAL_PHP_XDEBUG=true node ./tools/local-env/scripts/docker.js run php ./vendor/bin/phpunit -c "${PHPUNIT_CONFIG}" --group xdebug --exclude-group __fakegroup__ - name: Upload test coverage report to Codecov if: ${{ inputs.coverage-report }} diff --git a/.github/workflows/test-build-processes.yml b/.github/workflows/test-build-processes.yml index c64bc260c7439..c8f2fd660fb80 100644 --- a/.github/workflows/test-build-processes.yml +++ b/.github/workflows/test-build-processes.yml @@ -34,6 +34,7 @@ on: - '!phpcs.xml.dist' - '!phpstan.neon.dist' - '!phpunit.xml.dist' + - '!phpunit10.xml.dist' - '!tsconfig.json' # Tests, unrelated tools, and other workflows do not affect the build. - '!tests/**' diff --git a/.github/workflows/test-coverage.yml b/.github/workflows/test-coverage.yml index 583a6045213dc..c0220b379cda0 100644 --- a/.github/workflows/test-coverage.yml +++ b/.github/workflows/test-coverage.yml @@ -15,7 +15,9 @@ on: - '.nvmrc' - 'docker-compose.yml' - 'phpunit.xml.dist' + - 'phpunit10.xml.dist' - 'tests/phpunit/multisite.xml' + - 'tests/phpunit/multisite10.xml' pull_request: branches: - trunk @@ -29,7 +31,9 @@ on: - '.nvmrc' - 'docker-compose.yml' - 'phpunit.xml.dist' + - 'phpunit10.xml.dist' - 'tests/phpunit/multisite.xml' + - 'tests/phpunit/multisite10.xml' # Once daily at 00:00 UTC. schedule: - cron: '0 0 * * *' diff --git a/.gitignore b/.gitignore index 24b4282326304..3352f433f977f 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ wp-tests-config.php # Files and folders related to build/test tools .phpunit.result.cache /phpunit.xml +/phpunit10.xml /.phpcs.xml /phpcs.xml .cache/* diff --git a/composer.json b/composer.json index c897fba0fa720..4e924d247fe14 100644 --- a/composer.json +++ b/composer.json @@ -88,6 +88,6 @@ "format": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcbf --report=summary,source", "lint": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcs --report=summary,source", "lint:errors": "@lint -n", - "test": [ "Composer\\Config::disableProcessTimeout", "@php ./vendor/phpunit/phpunit/phpunit" ] + "test": [ "Composer\\Config::disableProcessTimeout", "@php ./tests/phpunit/run.php" ] } } diff --git a/package.json b/package.json index caf5d007d1d9d..3baad46197457 100644 --- a/package.json +++ b/package.json @@ -148,7 +148,7 @@ "env:logs": "node ./tools/local-env/scripts/docker.js logs", "env:pull": "node ./tools/local-env/scripts/docker.js pull", "test:performance": "wp-scripts test-playwright --config tests/performance/playwright.config.js", - "test:php": "node ./tools/local-env/scripts/docker.js run --rm php ./vendor/bin/phpunit", + "test:php": "node ./tools/local-env/scripts/docker.js run --rm php composer test --", "test:coverage": "npm run test:php -- --coverage-html ./coverage/html/ --coverage-php ./coverage/php/report.php --coverage-text=./coverage/text/report.txt", "test:e2e": "wp-scripts test-playwright --config tests/e2e/playwright.config.js", "test:visual": "wp-scripts test-playwright --config tests/visual-regression/playwright.config.js", diff --git a/phpunit10.xml.dist b/phpunit10.xml.dist new file mode 100644 index 0000000000000..937dc46e567e7 --- /dev/null +++ b/phpunit10.xml.dist @@ -0,0 +1,85 @@ + + + + + + tests/phpunit/tests + tests/phpunit/tests/rest-api/rest-autosaves-controller.php + + + + tests/phpunit/tests/rest-api/rest-autosaves-controller.php + + + + + ajax + ms-files + ms-required + external-http + html-api-web-platform-tests + + + + + src + + + + src/wp-includes/ID3 + src/wp-includes/IXR + src/wp-includes/PHPMailer + src/wp-includes/Requests + src/wp-includes/php-ai-client + src/wp-includes/SimplePie + src/wp-includes/sodium_compat + src/wp-includes/Text + + + src/wp-content + + src/wp-admin/includes/class-ftp.php + src/wp-admin/includes/class-ftp-pure.php + src/wp-admin/includes/class-ftp-sockets.php + src/wp-admin/includes/class-pclzip.php + src/wp-admin/includes/deprecated.php + src/wp-admin/includes/ms-deprecated.php + + src/wp-includes/atomlib.php + src/wp-includes/class-IXR.php + src/wp-includes/class-json.php + src/wp-includes/class-phpass.php + src/wp-includes/class-pop3.php + src/wp-includes/class-requests.php + src/wp-includes/class-simplepie.php + src/wp-includes/class-snoopy.php + src/wp-includes/deprecated.php + src/wp-includes/ms-deprecated.php + src/wp-includes/pluggable-deprecated.php + src/wp-includes/rss.php + + + + + + + + + diff --git a/tests/phpunit/README.md b/tests/phpunit/README.md index 6308e75c3d3da..3be119809ff99 100644 --- a/tests/phpunit/README.md +++ b/tests/phpunit/README.md @@ -6,6 +6,23 @@ For more information, please review the relevant Core Handbook pages: - [PHP: PHPUnit](https://make.wordpress.org/core/handbook/testing/automated-testing/phpunit/) - [Writing PHP Tests](https://make.wordpress.org/core/handbook/testing/automated-testing/writing-phpunit-tests/) +## Configuration Files + +WordPress core has separate configuration files for PHPUnit 9 and PHPUnit 10: + +- `phpunit.xml.dist` and `tests/phpunit/multisite.xml`: Used for PHPUnit 9 and earlier. +- `phpunit10.xml.dist` and `tests/phpunit/multisite10.xml`: Used for PHPUnit 10 and later. These use the `` element, so PHPUnit 10.0 is not supported. + +### Running Tests Locally + +`composer test` and `npm run test:php` use the single site configuration file that matches the installed version of PHPUnit. A local `phpunit.xml` file is used instead if one exists, so base it on the file that matches your version of PHPUnit. Passing an explicit `-c`, `--configuration`, or `--no-configuration` option overrides the automatic selection. + +`tests/phpunit/phpunit-config.php` prints the matching configuration file. Pass it either configuration file name to get the one for the installed version, for example to run the multisite tests: + +``` +./vendor/bin/phpunit -c "$(php tests/phpunit/phpunit-config.php tests/phpunit/multisite.xml)" +``` + ## Run on a host that prohibits loopback listening Set `WP_TESTS_SKIP_HTTP_LOOPBACK=1` in the PHPUnit process environment if the host prohibits listening on a local TCP port. When PHP cannot reserve a port, PHPUnit skips only the HTTP transport tests that need the local fixture server. If PHP reserves a port, the tests run normally and server startup errors still fail. diff --git a/tests/phpunit/multisite10.xml b/tests/phpunit/multisite10.xml new file mode 100644 index 0000000000000..eed9c0540bcf9 --- /dev/null +++ b/tests/phpunit/multisite10.xml @@ -0,0 +1,87 @@ + + + + + + + + + + tests + tests/rest-api/rest-autosaves-controller.php + + + + tests/rest-api/rest-autosaves-controller.php + + + + + ajax + ms-files + ms-excluded + external-http + oembed-headers + html-api-web-platform-tests + + + + + ../../src + + + + ../../src/wp-includes/ID3 + ../../src/wp-includes/IXR + ../../src/wp-includes/PHPMailer + ../../src/wp-includes/Requests + ../../src/wp-includes/php-ai-client + ../../src/wp-includes/SimplePie + ../../src/wp-includes/sodium_compat + ../../src/wp-includes/Text + + + ../../src/wp-content + + ../../src/wp-admin/includes/class-ftp.php + ../../src/wp-admin/includes/class-ftp-pure.php + ../../src/wp-admin/includes/class-ftp-sockets.php + ../../src/wp-admin/includes/class-pclzip.php + ../../src/wp-admin/includes/deprecated.php + ../../src/wp-admin/includes/ms-deprecated.php + + ../../src/wp-includes/atomlib.php + ../../src/wp-includes/class-IXR.php + ../../src/wp-includes/class-json.php + ../../src/wp-includes/class-phpass.php + ../../src/wp-includes/class-pop3.php + ../../src/wp-includes/class-requests.php + ../../src/wp-includes/class-simplepie.php + ../../src/wp-includes/class-snoopy.php + ../../src/wp-includes/deprecated.php + ../../src/wp-includes/ms-deprecated.php + ../../src/wp-includes/pluggable-deprecated.php + ../../src/wp-includes/rss.php + + + + + + diff --git a/tests/phpunit/phpunit-config.php b/tests/phpunit/phpunit-config.php new file mode 100644 index 0000000000000..3b2a16ee970a8 --- /dev/null +++ b/tests/phpunit/phpunit-config.php @@ -0,0 +1,51 @@ +=' ); + +$swaps = array( + 'phpunit.xml.dist' => 'phpunit10.xml.dist', + 'tests/phpunit/multisite.xml' => 'tests/phpunit/multisite10.xml', +); + +if ( ! $is_phpunit_10 ) { + $swaps = array_flip( $swaps ); +} + +if ( ! isset( $argv[1] ) ) { + if ( file_exists( $root . '/phpunit.xml' ) ) { + echo 'phpunit.xml'; + } else { + echo $is_phpunit_10 ? 'phpunit10.xml.dist' : 'phpunit.xml.dist'; + } + exit; +} + +$config = $argv[1]; +$real = realpath( $config ); + +foreach ( $swaps as $from => $to ) { + if ( false !== $real && realpath( $root . '/' . $from ) === $real ) { + $config = $to; + break; + } +} + +echo $config; diff --git a/tests/phpunit/run.php b/tests/phpunit/run.php new file mode 100644 index 0000000000000..f2daf48506df1 --- /dev/null +++ b/tests/phpunit/run.php @@ -0,0 +1,101 @@ + array( 'pipe', 'w' ), + 2 => STDERR, + ), + $config_pipes + ); + + if ( ! is_resource( $config_process ) ) { + fwrite( STDERR, "Failed to determine the PHPUnit configuration file.\n" ); + exit( 1 ); + } + + $config = trim( (string) stream_get_contents( $config_pipes[1] ) ); + fclose( $config_pipes[1] ); + $config_exit = proc_close( $config_process ); + + if ( 0 !== $config_exit || '' === $config ) { + exit( 0 !== $config_exit ? $config_exit : 1 ); + } + + array_unshift( $phpunit_args, '-c', $config ); +} + +$process = proc_open( + array( PHP_BINARY, $phpunit, ...$phpunit_args ), + array( + 0 => STDIN, + 1 => STDOUT, + 2 => STDERR, + ), + $pipes +); + +if ( ! is_resource( $process ) ) { + fwrite( STDERR, "Failed to start PHPUnit.\n" ); + exit( 1 ); +} + +exit( proc_close( $process ) );