Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
e5eae06
Working on adding Force_Single_Plugin_Preparation class
Jan 27, 2023
9409735
Working on adding Force_Single_Plugin_Preparation class
Jan 27, 2023
3b65eff
Working on adding Force_Single_Plugin_Preparation class
Jan 27, 2023
ec73b57
Add Force_Single_Plugin_Preparation class
Jan 31, 2023
3930be2
erge branch 'trunk' into feature/add-force-single-plugin-preparation
Jan 31, 2023
eb4a03d
Update spacings
Feb 1, 2023
5a2648d
Update class description.
Feb 1, 2023
b03abc7
Remove unwanted function
Feb 1, 2023
fed225e
Update message
Feb 1, 2023
7c87571
Remove unwanted doc block
Feb 1, 2023
249b181
Fix phplint issue
Feb 1, 2023
9682778
Remove unwanted doc block
Feb 1, 2023
655ba46
Remove blank line.
Feb 2, 2023
392dad7
Provide the more detailed context from the WP_Error
Feb 2, 2023
2de9ff9
Remove blank line
Feb 2, 2023
2e11efe
Update error message
Feb 2, 2023
1242c16
Address feedbacks
Feb 2, 2023
44eaa89
Address feedbacks
Feb 2, 2023
0b3d186
Address feedbacks
Feb 2, 2023
890b2a8
Address feedbacks
Feb 2, 2023
9631e13
Merge branch 'trunk' into feature/add-force-single-plugin-preparation
Feb 3, 2023
a247497
Address feedbacks
Feb 3, 2023
f88bd13
Merge branch 'trunk' into feature/add-force-single-plugin-preparation
Feb 3, 2023
198452b
Fix error
Feb 3, 2023
1102175
fix active_plugins in prepare test
jjgrainger Feb 5, 2023
52b9da6
Address feedbacks
Feb 6, 2023
26e73d8
Address feedback - Update constant
Feb 7, 2023
ce1cd3d
Address feedback - Update constant
Feb 7, 2023
d44e746
Address feedback - Update constant
Feb 7, 2023
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
103 changes: 103 additions & 0 deletions includes/Checker/Preparations/Force_Single_Plugin_Preparation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
/**
* Class WordPress\Plugin_Check\Checker\Preparations\Force_Single_Plugin_Preparation
*
* @package plugin-check
*/

namespace WordPress\Plugin_Check\Checker\Preparations;

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

/**
* Class for the preparation to force the plugin to be checked as the only active plugin.
*
* This ensures the plugin is checked as much in isolation as possible.
*
* @since n.e.x.t
*/
class Force_Single_Plugin_Preparation implements Preparation {

/**
* Plugin slug.
*
* @since n.e.x.t
* @var string
*/
protected $plugin_basename;

/**
* Sets the plugin slug.
*
* @since n.e.x.t
*
* @param string $plugin_basename Slug of the plugin, E.g. "akismet\akismet.php".
*/
public function __construct( $plugin_basename ) {
$this->plugin_basename = $plugin_basename;
}

/**
* Runs this preparation step for the environment and returns a cleanup function.
*
* @since n.e.x.t
*
* @return callable Cleanup function to revert any changes made here.
*
* @throws Exception Thrown when preparation fails.
*/
public function prepare() {
$valid_plugin = validate_plugin( $this->plugin_basename );

// Check if the plugin exists.
if ( is_wp_error( $valid_plugin ) ) {

throw new Exception(
sprintf(
/* translators: 1: plugin basename, 2: error message */
__( 'Invalid plugin %1$s: %2$s', 'plugin-check' ),
$this->plugin_basename,
$valid_plugin->get_error_message()
)
);
}

add_filter( 'option_active_plugins', array( $this, 'filter_active_plugins' ) );
add_filter( 'default_option_active_plugins', array( $this, 'filter_active_plugins' ) );

// Return the cleanup function.
return function() {
remove_filter( 'option_active_plugins', array( $this, 'filter_active_plugins' ) );
remove_filter( 'default_option_active_plugins', array( $this, 'filter_active_plugins' ) );
};
}

/**
* Filter active plugins.
*
* @param array $active_plugins List of active plugins.
* @return array List of active plugins.
*/
public function filter_active_plugins( $active_plugins ) {
if ( is_array( $active_plugins ) && in_array( $this->plugin_basename, $active_plugins, true ) ) {

$plugin_base_file = plugin_basename( WP_PLUGIN_CHECK_MAIN_FILE );

// If the plugin-check is the only available plugin then return that one only.
if ( $this->plugin_basename === $plugin_base_file ) {

Comment thread
felixarntz marked this conversation as resolved.
return array(
$plugin_base_file,
);
}

return array(
$this->plugin_basename,
$plugin_base_file,
);
}

return $active_plugins;
}
}
5 changes: 3 additions & 2 deletions plugin-check.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

define( 'WP_PLUGIN_CHECK_VERSION', 'n.e.x.t' );
define( 'WP_PLUGIN_CHECK_MINIMUM_PHP', '5.6' );
define( 'WP_PLUGIN_CHECK_PLUGIN_DIR_PATH', plugin_dir_path( __FILE__ ) );
define( 'WP_PLUGIN_CHECK_MAIN_FILE', __FILE__ );
Comment thread
felixarntz marked this conversation as resolved.
define( 'WP_PLUGIN_CHECK_PLUGIN_DIR_PATH', plugin_dir_path( WP_PLUGIN_CHECK_MAIN_FILE ) );

/**
* Checks basic requirements and loads the plugin.
Expand All @@ -42,7 +43,7 @@ function wp_plugin_check_load() {

// Setup the plugin.
$class_name = 'WordPress\\Plugin_Check\\Plugin_Main';
$instance = new $class_name( __FILE__ );
$instance = new $class_name( WP_PLUGIN_CHECK_MAIN_FILE );
$instance->add_hooks();
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Checker/Check_Context_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public function set_up() {
parent::set_up();

$this->plugin_name = basename( TESTS_PLUGIN_DIR );
$this->check_context = new Check_Context( WP_PLUGIN_DIR . '/' . $this->plugin_name . '/plugin-check.php' );
$this->check_context = new Check_Context( WP_PLUGIN_CHECK_MAIN_FILE );
}

public function test_basename() {
$this->assertSame( $this->plugin_name . '/plugin-check.php', $this->check_context->basename() );
$this->assertSame( plugin_basename( WP_PLUGIN_CHECK_MAIN_FILE ), $this->check_context->basename() );
}

public function test_path() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
/**
* Tests for the Force_Single_Plugin_Preparation class.
*
* @package plugin-check
*/

namespace Checker\Preparations;

use WordPress\Plugin_Check\Checker\Preparations\Force_Single_Plugin_Preparation;
use WP_UnitTestCase;
use Exception;

class Force_Single_Plugin_Preparation_Tests extends WP_UnitTestCase {

protected $plugin_basename_file;

public function set_up() {
parent::set_up();

$this->plugin_basename_file = plugin_basename( WP_PLUGIN_CHECK_MAIN_FILE );
}

public function test_prepare_plugin_exists() {

$preparation = new Force_Single_Plugin_Preparation( 'akismet/akismet.php' );

$this->expectException( 'Exception' );
$this->expectExceptionMessage( 'Invalid plugin akismet/akismet.php: Plugin file does not exist.' );
$preparation->prepare();
}

/**
* @throws Exception Throw exception.
*/
public function test_prepare() {
// Remove the WP tests active plugins filter which interfers with this test.
remove_filter( 'pre_option_active_plugins', 'wp_tests_options' );

$preparation = new Force_Single_Plugin_Preparation( $this->plugin_basename_file );
$active_plugins = array(
'akismet/akismet.php',
$this->plugin_basename_file,
'wp-reset/wp-reset.php',
);

update_option( 'active_plugins', $active_plugins );

$cleanup = $preparation->prepare();
$before = get_option( 'active_plugins' );
$cleanup();
$after = get_option( 'active_plugins' );

$this->assertSame( array( $this->plugin_basename_file ), $before );
$this->assertSame( $active_plugins, $after );
}

public function test_filter_active_plugins() {

$preparation = new Force_Single_Plugin_Preparation( 'wp-reset/wp-reset.php' );

$plugins = array(
'akismet/akismet.php',
$this->plugin_basename_file,
'wp-reset/wp-reset.php',
);

$active_plugins = $preparation->filter_active_plugins( $plugins );

$this->assertSame(
array(
'wp-reset/wp-reset.php',
$this->plugin_basename_file,
),
$active_plugins
);

$plugins = array(
'akismet/akismet.php',
$this->plugin_basename_file,
'test-plugin/test-plugin.php',
);

$active_plugins = $preparation->filter_active_plugins( $plugins );

$this->assertSame( $plugins, $active_plugins );
}
}
4 changes: 2 additions & 2 deletions tests/Plugin_Context_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public function set_up() {
parent::set_up();

$this->plugin_name = basename( TESTS_PLUGIN_DIR );
$this->plugin_context = new Plugin_Context( WP_PLUGIN_DIR . '/' . $this->plugin_name . '/plugin-check.php' );
$this->plugin_context = new Plugin_Context( WP_PLUGIN_CHECK_MAIN_FILE );
}

public function test_basename() {
$this->assertSame( $this->plugin_name . '/plugin-check.php', $this->plugin_context->basename() );
$this->assertSame( plugin_basename( WP_PLUGIN_CHECK_MAIN_FILE ), $this->plugin_context->basename() );
}

public function test_path() {
Expand Down
2 changes: 1 addition & 1 deletion tests/Plugin_Main_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Plugin_Main_Tests extends WP_UnitTestCase {
public function set_up() {
parent::set_up();

$this->plugin_main = new Plugin_Main( basename( dirname( __DIR__ ) ) . '/plugin-check.php' );
$this->plugin_main = new Plugin_Main( WP_PLUGIN_CHECK_MAIN_FILE );
}

public function test_context() {
Expand Down