Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

General: remove backwards compatibility code now that Jetpack requires WP 5.9 #24086

Merged
merged 14 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 1 addition & 34 deletions docs/examples/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
<?php
/**
* PHPUnit bootstrap file to apply WordPress core's monkey-patching to versions
* of PHPUnit that don't natively support PHP 8.0 for PHP 8.0.
*
* WARNING: This is outdated as of September 2021. WordPress is in the midst of
* updating their stuff to stop being hacky, but have left WP 5.7 and 5.8 in a
* strange in-between state. Once we only support 5.9+ we should update this
* for whatever it turns out to be.
* PHPUnit bootstrap file.
*
* @package automattic/jetpack
*/
Expand Down Expand Up @@ -52,33 +46,6 @@
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo "Using test root $_tests_dir\n";

// WordPress requires PHPUnit 7.5 or earlier and hacks around a few things to
// make it work with PHP 8. Unfortunately for MockObjects they do it via
// composer.json rather than bootstrap.php, so we have to manually do it here.
if ( version_compare( PHP_VERSION, '8.0', '>=' ) &&
( ! class_exists( PHPUnit\Runner\Version::class ) || version_compare( PHPUnit\Runner\Version::id(), '9.3', '<' ) )
) {
if ( ! class_exists( PHPUnit\Framework\MockObject\InvocationMocker::class, false ) &&
file_exists( "$_tests_dir/includes/phpunit7/MockObject/InvocationMocker.php" )
) {
// phpcs:disable WordPressVIPMinimum.Files.IncludingFile.NotAbsolutePath
require "$_tests_dir/includes/phpunit7/MockObject/Builder/NamespaceMatch.php";
require "$_tests_dir/includes/phpunit7/MockObject/Builder/ParametersMatch.php";
require "$_tests_dir/includes/phpunit7/MockObject/InvocationMocker.php";
require "$_tests_dir/includes/phpunit7/MockObject/MockMethod.php";
// phpcs:enable
} else {
fprintf(
STDOUT,
"Warning: PHPUnit <9.3 is not compatible with PHP 8.0+, and the hack could not be loaded.\n Class %s exists: %s\n File %s exists: %s\n",
PHPUnit\Framework\MockObject\InvocationMocker::class,
class_exists( PHPUnit\Framework\MockObject\InvocationMocker::class, false ) ? 'yes (bad)' : 'no (good)',
"$_tests_dir/includes/phpunit7/MockObject/InvocationMocker.php",
file_exists( "$_tests_dir/includes/phpunit7/MockObject/InvocationMocker.php" ) ? 'yes (good)' : 'no (bad)'
);
}
}

// Give access to tests_add_filter() function.
jeherve marked this conversation as resolved.
Show resolved Hide resolved
require_once $_tests_dir . '/includes/functions.php';

Expand Down
4 changes: 4 additions & 0 deletions projects/plugins/jetpack/changelog/update-wp-compat-60-rm-old
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: compat

General: remove backwards compatibility code now that Jetpack requires WordPress 5.9.
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,6 @@ public function callback( $path = '', $blog_id = 0 ) {

if ( $authors_only ) {
$query['capability'] = array( 'edit_posts' );
// To-do: remove this once Jetpack requires WordPress 5.9.
global $wp_version;
if ( version_compare( $wp_version, '5.9-alpha', '<' ) ) {
$query['who'] = 'authors';
unset( $query['capability'] );
}
}

if ( ! empty( $args['search'] ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,6 @@ public static function widget_admin_setup() {
'fields' => array( 'ID', 'display_name' ),
);

// To-do: remove this once Jetpack requires WordPress 5.9.
global $wp_version;
if ( version_compare( $wp_version, '5.9-alpha', '<' ) ) {
$authors_args['who'] = 'authors';
unset( $authors_args['capability'] );
}

$authors = get_users( $authors_args );

foreach ( $authors as $author ) {
Expand Down
7 changes: 0 additions & 7 deletions projects/plugins/jetpack/modules/widgets/authors.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,6 @@ public function widget( $args, $instance ) {
)
);

// To-do: remove this once Jetpack requires WordPress 5.9.
global $wp_version;
if ( version_compare( $wp_version, '5.9-alpha', '<' ) ) {
$get_author_params['who'] = 'authors';
unset( $get_author_params['capability'] );
}

$authors = get_users( $get_author_params );

echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
Expand Down
1 change: 0 additions & 1 deletion projects/plugins/jetpack/tests/action-phpunit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@

set -eo pipefail

. tests/maybe-downgrade-phpunit.sh
exec phpunit "$@"
2 changes: 0 additions & 2 deletions projects/plugins/jetpack/tests/action-test-coverage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

set -eo pipefail

. tests/maybe-downgrade-phpunit.sh

PLUGINDIR="$PWD"

cd "$MONOREPO_BASE/projects/plugins/jetpack"
Expand Down
2 changes: 0 additions & 2 deletions projects/plugins/jetpack/tests/action-test-php.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

set -eo pipefail

. tests/maybe-downgrade-phpunit.sh

echo "::group::Jetpack tests"
phpunit
echo "::endgroup::"
Expand Down
24 changes: 0 additions & 24 deletions projects/plugins/jetpack/tests/maybe-downgrade-phpunit.sh

This file was deleted.

69 changes: 40 additions & 29 deletions projects/plugins/jetpack/tests/php/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,46 @@
*/
define( 'TESTING_IN_JETPACK', true );

$test_root = require __DIR__ . '/find-test-root.php';
// Support for:
// 1. `WP_DEVELOP_DIR` environment variable.
// 2. Plugin installed inside of WordPress.org developer checkout.
// 3. Tests checked out to /tmp.
if ( false !== getenv( 'WP_DEVELOP_DIR' ) ) {
// Defined on command line.
$test_root = getenv( 'WP_DEVELOP_DIR' );
if ( file_exists( "$test_root/tests/phpunit/" ) ) {
$test_root .= '/tests/phpunit/';
}
} elseif ( file_exists( '../../../../tests/phpunit/includes/bootstrap.php' ) ) {
// Installed inside wordpress-develop.
$test_root = '../../../../tests/phpunit';
} elseif ( file_exists( '/vagrant/www/wordpress-develop/public_html/tests/phpunit/includes/bootstrap.php' ) ) {
// VVV.
$test_root = '/vagrant/www/wordpress-develop/public_html/tests/phpunit';
} elseif ( file_exists( '/srv/www/wordpress-trunk/public_html/tests/phpunit/includes/bootstrap.php' ) ) {
// VVV 3.0.
$test_root = '/srv/www/wordpress-trunk/public_html/tests/phpunit';
} elseif ( file_exists( '/tmp/wordpress-develop/tests/phpunit/includes/bootstrap.php' ) ) {
// Manual checkout & Jetpack's docker environment.
$test_root = '/tmp/wordpress-develop/tests/phpunit';
} elseif ( file_exists( '/tmp/wordpress-tests-lib/includes/bootstrap.php' ) ) {
// Legacy tests.
$test_root = '/tmp/wordpress-tests-lib';
}

if ( ! isset( $test_root ) || ! file_exists( $test_root . '/includes/bootstrap.php' ) ) {
fprintf(
STDERR,
<<<'EOF'
Failed to automatically locate WordPress or wordpress-develop to run tests.

Set the WP_DEVELOP_DIR environment variable to point to a copy of WordPress
or wordpress-develop.
EOF
);
exit( 1 );
}

echo "Using test root $test_root\n";

$jp_autoloader = __DIR__ . '/../../vendor/autoload.php';
Expand All @@ -32,34 +71,6 @@

require $jp_autoloader;

// WordPress until recently required PHPUnit 7.5 or earlier and hacks around a few things to
// make it work with PHP 8. Unfortunately for MockObjects they do it via
// composer.json rather than bootstrap.php, so we have to manually do it here.
// @todo: Remove this once either WP backports their bootstrap changes to 5.8.1 or they release 5.8.2.
if ( version_compare( PHP_VERSION, '8.0', '>=' ) &&
( ! class_exists( PHPUnit\Runner\Version::class ) || version_compare( PHPUnit\Runner\Version::id(), '9.3', '<' ) )
) {
if ( ! class_exists( PHPUnit\Framework\MockObject\InvocationMocker::class, false ) &&
file_exists( "$test_root/includes/phpunit7/MockObject/InvocationMocker.php" )
) {
// phpcs:disable WordPressVIPMinimum.Files.IncludingFile.NotAbsolutePath
require "$test_root/includes/phpunit7/MockObject/Builder/NamespaceMatch.php";
require "$test_root/includes/phpunit7/MockObject/Builder/ParametersMatch.php";
require "$test_root/includes/phpunit7/MockObject/InvocationMocker.php";
require "$test_root/includes/phpunit7/MockObject/MockMethod.php";
// phpcs:enable
} else {
fprintf(
STDOUT,
"Warning: PHPUnit <9.3 is not compatible with PHP 8.0+, and the hack could not be loaded.\n Class %s exists: %s\n File %s exists: %s\n",
PHPUnit\Framework\MockObject\InvocationMocker::class,
class_exists( PHPUnit\Framework\MockObject\InvocationMocker::class, false ) ? 'yes (bad)' : 'no (good)',
"$test_root/includes/phpunit7/MockObject/InvocationMocker.php",
file_exists( "$test_root/includes/phpunit7/MockObject/InvocationMocker.php" ) ? 'yes (good)' : 'no (bad)'
);
}
}

if ( '1' !== getenv( 'WP_MULTISITE' ) && ( ! defined( 'WP_TESTS_MULTISITE' ) || ! WP_TESTS_MULTISITE ) ) {
echo 'To run Jetpack multisite, use -c tests/php.multisite.xml' . PHP_EOL;
echo "Disregard Core's -c tests/phpunit/multisite.xml notice below." . PHP_EOL;
Expand Down
48 changes: 0 additions & 48 deletions projects/plugins/jetpack/tests/php/find-test-root.php

This file was deleted.

4 changes: 0 additions & 4 deletions projects/plugins/jetpack/tests/php/test_deprecation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ class WP_Test_Jetpack_Deprecation extends WP_UnitTestCase {
* @dataProvider provider_deprecated_file_paths
*/
public function test_deprecated_file_paths( $file_path, $replacement_path ) {
// WordPress 5.9 starts adding this hook. Simplest way to make the tests work
// is to add it for earlier versions too.
// @todo Remove the add_action once we drop support for WordPress < 5.9.
add_action( 'deprecated_file_included', array( $this, 'deprecated_function_run' ) );
$this->setExpectedDeprecated( $file_path );

$mock = $this->getMockBuilder( stdClass::class )
Expand Down