Skip to content

Commit

Permalink
Merge pull request #785 from WordPress/fix/test-errors-missing-db-tables
Browse files Browse the repository at this point in the history
Ensure runtime environment is set up before testing `Universal_Runtime_Preparation`
  • Loading branch information
ernilambar authored Nov 13, 2024
2 parents fe8ea10 + e548c22 commit d6551d0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,43 @@

use WordPress\Plugin_Check\Checker\Check_Context;
use WordPress\Plugin_Check\Checker\Preparations\Universal_Runtime_Preparation;
use WordPress\Plugin_Check\Checker\Runtime_Environment_Setup;
use WP_UnitTestCase;

class Universal_Runtime_Preparation_Tests extends WP_UnitTestCase {

/**
* Storage for preparation cleanups that need to be run after a test.
*
* @var array
*/
private $cleanups = array();

public function tear_down() {
if ( count( $this->cleanups ) > 0 ) {
$this->cleanups = array_reverse( $this->cleanups );
foreach ( $this->cleanups as $cleanup ) {
$cleanup();
}
$this->cleanups = array();
}
parent::tear_down();
}

public function test_prepare() {
$check_context = new Check_Context( plugin_basename( WP_PLUGIN_CHECK_MAIN_FILE ) );

// The runtime environment must be prepared manually before regular runtime preparations.
$runtime = new Runtime_Environment_Setup();
$runtime->set_up();
$this->cleanups[] = function () use ( $runtime ) {
$runtime->clean_up();
};

$universal_runtime_preparation = new Universal_Runtime_Preparation( $check_context );

$cleanup = $universal_runtime_preparation->prepare();
$cleanup = $universal_runtime_preparation->prepare();
$this->cleanups[] = $cleanup;

$this->assertTrue( has_filter( 'option_active_plugins' ) );
$this->assertTrue( has_filter( 'default_option_active_plugins' ) );
Expand All @@ -32,6 +59,9 @@ public function test_prepare() {

$cleanup();

// If this is reached, the universal runtime preparation cleanup was already done, so we can remove it again.
array_pop( $this->cleanups );

$this->assertFalse( has_filter( 'option_active_plugins' ) );
$this->assertFalse( has_filter( 'default_option_active_plugins' ) );
$this->assertFalse( has_filter( 'stylesheet' ) );
Expand Down
27 changes: 23 additions & 4 deletions tests/phpunit/tests/Utilities/Plugin_Request_Utility_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use WordPress\Plugin_Check\Checker\Checks;
use WordPress\Plugin_Check\Checker\Checks\General\I18n_Usage_Check;
use WordPress\Plugin_Check\Checker\CLI_Runner;
use WordPress\Plugin_Check\Checker\Runtime_Environment_Setup;
use WordPress\Plugin_Check\Test_Data\Runtime_Check;
use WordPress\Plugin_Check\Test_Utils\Traits\With_Mock_Filesystem;
use WordPress\Plugin_Check\Utilities\Plugin_Request_Utility;
Expand Down Expand Up @@ -90,6 +91,16 @@ public function test_initialize_runner_with_ajax() {
$_REQUEST['action'] = 'plugin_check_run_checks';
$_REQUEST['plugin'] = 'plugin-check';

/*
* The runtime environment must be prepared manually before regular runtime preparations.
* This is necessary because in reality it happens in a separate AJAX request before.
*/
$runtime = new Runtime_Environment_Setup();
$runtime->set_up();
$this->cleanups[] = function () use ( $runtime ) {
$runtime->clean_up();
};

Plugin_Request_Utility::initialize_runner();
$this->cleanups[] = function () {
Plugin_Request_Utility::destroy_runner();
Expand All @@ -109,7 +120,7 @@ public function test_initialize_runner_with_ajax() {
public function test_destroy_runner_with_cli() {
define( 'WP_CLI', true );

global $wpdb, $table_prefix, $wp_actions;
global $wp_actions;

$this->set_up_mock_filesystem();

Expand Down Expand Up @@ -151,15 +162,14 @@ function ( $checks ) {

unset( $_SERVER['argv'] );
$wp_actions['muplugins_loaded'] = $muplugins_loaded;
$wpdb->set_prefix( $table_prefix );

$this->assertTrue( $prepared );
$this->assertTrue( $cleanup );
$this->assertNull( $runner );
}

public function test_destroy_runner_with_ajax() {
global $wpdb, $table_prefix, $wp_actions;
global $wp_actions;

$this->set_up_mock_filesystem();

Expand All @@ -168,6 +178,16 @@ public function test_destroy_runner_with_ajax() {
$_REQUEST['plugin'] = 'plugin-check';
$_REQUEST['checks'] = array( 'runtime_check' );

/*
* The runtime environment must be prepared manually before regular runtime preparations.
* This is necessary because in reality it happens in a separate AJAX request before.
*/
$runtime = new Runtime_Environment_Setup();
$runtime->set_up();
$this->cleanups[] = function () use ( $runtime ) {
$runtime->clean_up();
};

add_filter(
'wp_plugin_check_checks',
function ( $checks ) {
Expand Down Expand Up @@ -196,7 +216,6 @@ function ( $checks ) {
$cleanup = ! has_filter( 'option_active_plugins' );
$runner = Plugin_Request_Utility::get_runner();

$wpdb->set_prefix( $table_prefix );
$wp_actions['muplugins_loaded'] = $muplugins_loaded;

$this->assertTrue( $prepared );
Expand Down

0 comments on commit d6551d0

Please sign in to comment.