Skip to content
71 changes: 71 additions & 0 deletions includes/Checker/Preparations/Universal_Runtime_Preparation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Class WordPress\Plugin_Check\Checker\Preparations\Universal_Runtime_Preparation
*
* @package plugin-check
*/

namespace WordPress\Plugin_Check\Checker\Preparations;

use WordPress\Plugin_Check\Checker\Check_Context;
use WordPress\Plugin_Check\Checker\Preparation;
use Exception;

/**
* Class handle all preparations required for when at least one `Runtime_Check` is being run.
*
* @since n.e.x.t
*/
class Universal_Runtime_Preparation implements Preparation {

/**
* Context for the plugin to check.
*
* @since n.e.x.t
* @var Check_Context
*/
protected $check_context;

/**
* Sets the context for the plugin to check.
*
* @since n.e.x.t
*
* @param Check_Context $check_context Check context instance for the plugin.
*/
public function __construct( Check_Context $check_context ) {
$this->check_context = $check_context;
}

/**
* Runs preparation step for the environment by modifying the plugins and theme to use,
* and returns a closure as a cleanup function.
*
* This preparation needs to be called very early in the WordPress lifecycle, before
* plugins are loaded, e.g. from a drop-in like `object-cache.php`.
*
* @since n.e.x.t
*
* @return callable Cleanup function to revert changes made by theme and plugin preparation classes.
*
* @throws Exception Thrown when preparation fails.
*/
public function prepare() {

$cleanup_functions = array();

$use_minimal_theme_preparation = new Use_Minimal_Theme_Preparation( 'wp-empty-theme', WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . '/test-content/themes' );
$cleanup_functions[] = $use_minimal_theme_preparation->prepare();

$force_single_plugin_preparation = new Force_Single_Plugin_Preparation( $this->check_context->basename() );
$cleanup_functions[] = $force_single_plugin_preparation->prepare();

// Return the cleanup function.
return function () use ( $cleanup_functions ) {

foreach ( $cleanup_functions as $cleanup_function ) {
$cleanup_function();
}
};
}
}
45 changes: 45 additions & 0 deletions tests/Checker/Preparations/Universal_Runtime_Preparation_Tests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* Tests for the Universal_Runtime_Preparation class.
*
* @package plugin-check
*/

namespace Checker\Preparations;

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

class Universal_Runtime_Preparation_Tests extends WP_UnitTestCase {

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

$universal_runtime_preparation = new Universal_Runtime_Preparation( $check_context );

$cleanup = $universal_runtime_preparation->prepare();

$this->assertTrue( has_filter( 'option_active_plugins' ) );
$this->assertTrue( has_filter( 'default_option_active_plugins' ) );
$this->assertTrue( has_filter( 'stylesheet' ) );
$this->assertTrue( has_filter( 'template' ) );
$this->assertTrue( has_filter( 'pre_option_template' ) );
$this->assertTrue( has_filter( 'pre_option_stylesheet' ) );
$this->assertTrue( has_filter( 'pre_option_current_theme' ) );
$this->assertTrue( has_filter( 'pre_option_template_root' ) );
$this->assertTrue( has_filter( 'pre_option_stylesheet_root' ) );

$cleanup();

$this->assertFalse( has_filter( 'option_active_plugins' ) );
$this->assertFalse( has_filter( 'default_option_active_plugins' ) );
$this->assertFalse( has_filter( 'stylesheet' ) );
$this->assertFalse( has_filter( 'template' ) );
$this->assertFalse( has_filter( 'pre_option_template' ) );
$this->assertFalse( has_filter( 'pre_option_stylesheet' ) );
$this->assertFalse( has_filter( 'pre_option_current_theme' ) );
$this->assertFalse( has_filter( 'pre_option_template_root' ) );
$this->assertFalse( has_filter( 'pre_option_stylesheet_root' ) );
}
}