-
Notifications
You must be signed in to change notification settings - Fork 98
Add Force_Single_Plugin_Preparation class #68
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
Merged
vishalkakadiya
merged 29 commits into
trunk
from
feature/add-force-single-plugin-preparation
Feb 7, 2023
Merged
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
9409735
Working on adding Force_Single_Plugin_Preparation class
3b65eff
Working on adding Force_Single_Plugin_Preparation class
ec73b57
Add Force_Single_Plugin_Preparation class
3930be2
erge branch 'trunk' into feature/add-force-single-plugin-preparation
eb4a03d
Update spacings
5a2648d
Update class description.
b03abc7
Remove unwanted function
fed225e
Update message
7c87571
Remove unwanted doc block
249b181
Fix phplint issue
9682778
Remove unwanted doc block
655ba46
Remove blank line.
392dad7
Provide the more detailed context from the WP_Error
2de9ff9
Remove blank line
2e11efe
Update error message
1242c16
Address feedbacks
44eaa89
Address feedbacks
0b3d186
Address feedbacks
890b2a8
Address feedbacks
9631e13
Merge branch 'trunk' into feature/add-force-single-plugin-preparation
a247497
Address feedbacks
f88bd13
Merge branch 'trunk' into feature/add-force-single-plugin-preparation
198452b
Fix error
1102175
fix active_plugins in prepare test
jjgrainger 52b9da6
Address feedbacks
26e73d8
Address feedback - Update constant
ce1cd3d
Address feedback - Update constant
d44e746
Address feedback - Update constant
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
includes/Checker/Preparations/Force_Single_Plugin_Preparation.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 ) { | ||
|
|
||
| return array( | ||
| $plugin_base_file, | ||
| ); | ||
| } | ||
|
|
||
| return array( | ||
| $this->plugin_basename, | ||
| $plugin_base_file, | ||
| ); | ||
| } | ||
|
|
||
| return $active_plugins; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
tests/Checker/Preparations/Force_Single_Plugin_Preparation_Tests.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.