wordpress-develop/tests/phpunit/tests/admin/plugin-dependencies/base.php
Colin Stewart 9173f8baa9 Plugin Dependencies: Remove auto-deactivation and bootstrapping logic.
Automatic deactivation of dependents with unmet dependencies requires a write operation to the database. This was performed during Core's bootstrap, which risked the database and cache becoming out-of-sync on sites with heavy traffic.

No longer loading plugins that have unmet requirements has not had a final approach decided core-wide, and is still in discussion in #60491 to be handled in a future release.

The `plugin_data` option, used to persistently store plugin data for detecting unmet dependencies during Core's bootstrap, is no longer needed.

Follow-up to [57545], [57592], [57606], [57617].

Props dd32, azaozz, swissspidy, desrosj, afragen, pbiron, zunaid321, costdev.
Fixes #60457. See #60491, #60510, #60518.

git-svn-id: https://develop.svn.wordpress.org/trunk@57658 602fd350-edb4-49c9-b593-d223f7449a82
2024-02-20 07:25:38 +00:00

128 lines
3.7 KiB
PHP

<?php
/**
* Test case for the Plugin Dependencies tests.
*
* @package WP_Plugin_Dependencies
*
* Abstracts the common properties and tasks for the Plugin Dependencies tests.
*/
abstract class WP_PluginDependencies_UnitTestCase extends WP_UnitTestCase {
/**
* Stores an instance of WP_Plugin_Dependencies
* for managing property visibility during tests.
*
* @var WP_Plugin_Dependencies
*/
protected static $instance;
/**
* Stores a list of static properties and their default values.
* for resetting after each test runs.
*
* @var array
*/
protected static $static_properties = array(
'plugins' => null,
'plugin_dirnames' => null,
'dependencies' => null,
'dependency_slugs' => null,
'dependent_slugs' => null,
'dependency_api_data' => null,
'dependency_filepaths' => null,
'circular_dependencies_pairs' => null,
'circular_dependencies_slugs' => null,
'initialized' => false,
);
/**
* An array of reflected class members.
*
* @var ReflectionMethod[]|ReflectionProperty[]
*/
protected static $reflected_members = array();
/**
* Sets up the WP_Plugin_Dependencies instance before any tests run.
*/
public static function set_up_before_class() {
parent::set_up_before_class();
self::$instance = new WP_Plugin_Dependencies();
}
/**
* Empties the '$reflected_members' property after all tests run.
*/
public static function tear_down_after_class() {
self::$reflected_members = array();
parent::tear_down_after_class();
}
/**
* Resets all static properties to a default value after each test.
*/
public function tear_down() {
foreach ( self::$static_properties as $name => $default_value ) {
$this->set_property_value( $name, $default_value );
}
parent::tear_down();
}
/**
* Temporarily modifies the accessibility of a property to change its value.
*
* @param string $property The property's name.
* @param mixed $value The new value.
*/
public function set_property_value( $property, $value ) {
if ( ! isset( self::$reflected_members[ $property ] ) ) {
self::$reflected_members[ $property ] = new ReflectionProperty( self::$instance, $property );
}
self::$reflected_members[ $property ]->setAccessible( true );
self::$reflected_members[ $property ]->setValue( self::$instance, $value );
self::$reflected_members[ $property ]->setAccessible( false );
}
/**
* Temporarily modifies the accessibility of a property to get its value.
*
* @param string $property The property's name.
* @return mixed The value of the property.
*/
public function get_property_value( $property ) {
if ( ! isset( self::$reflected_members[ $property ] ) ) {
self::$reflected_members[ $property ] = new ReflectionProperty( self::$instance, $property );
}
self::$reflected_members[ $property ]->setAccessible( true );
$value = self::$reflected_members[ $property ]->getValue( self::$instance );
self::$reflected_members[ $property ]->setAccessible( false );
return $value;
}
/**
* Temporarily modifies the accessibility of a method to invoke it
* and return its result.
*
* @param string $method The method's name.
* @param mixed ...$args Arguments for the method.
* @return mixed The result of the method call.
*/
protected function call_method( $method, ...$args ) {
if ( ! isset( self::$reflected_members[ $method ] ) ) {
self::$reflected_members[ $method ] = new ReflectionMethod( self::$instance, $method );
}
self::$reflected_members[ $method ]->setAccessible( true );
$value = self::$reflected_members[ $method ]->invokeArgs( self::$instance, $args );
self::$reflected_members[ $method ]->setAccessible( false );
return $value;
}
}