mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2025-10-16 12:05:38 +00:00
Tests: Mock API response in Plugin Dependencies tests.
Previously, the tests for `WP_Plugin_Dependencies::get_dependency_names()` performed an API request to WordPress.org. If an HTTP failure occurred when connecting to WordPress.org, this could trigger test failures. This mocks the API response to prevent HTTP failures from triggering test failures. Follow-up to [57545]. Props swissspidy, peterwilsoncc, costdev. See #59647. git-svn-id: https://develop.svn.wordpress.org/trunk@57674 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
bd78247617
commit
75eb1a86e9
@ -18,6 +18,33 @@ require_once __DIR__ . '/base.php';
|
||||
*/
|
||||
class Tests_Admin_WPPluginDependencies_GetDependencyNames extends WP_PluginDependencies_UnitTestCase {
|
||||
|
||||
/**
|
||||
* Mocks an API response.
|
||||
*
|
||||
* @param string $type The type of response. Accepts 'success' or 'failure'.
|
||||
*/
|
||||
private function mock_api_response( $type ) {
|
||||
add_filter(
|
||||
'plugins_api',
|
||||
function ( $bypass, $action, $args ) use ( $type ) {
|
||||
if ( 'plugin_information' === $action && isset( $args->slug ) && str_starts_with( $args->slug, 'dependency' ) ) {
|
||||
if ( 'success' === $type ) {
|
||||
return (object) array(
|
||||
'slug' => $args->slug,
|
||||
'name' => 'Dependency ' . str_replace( 'dependency', '', $args->slug ),
|
||||
);
|
||||
} elseif ( 'failure' === $type ) {
|
||||
return new WP_Error( 'plugin_not_found', 'Plugin not found.' );
|
||||
}
|
||||
}
|
||||
|
||||
return $bypass;
|
||||
},
|
||||
10,
|
||||
3
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that dependency names are retrieved.
|
||||
*
|
||||
@ -40,6 +67,7 @@ class Tests_Admin_WPPluginDependencies_GetDependencyNames extends WP_PluginDepen
|
||||
array( 'dependent/dependent.php' => array( 'RequiresPlugins' => 'dependency, dependency2' ) )
|
||||
);
|
||||
|
||||
$this->mock_api_response( 'success' );
|
||||
self::$instance::initialize();
|
||||
|
||||
$this->set_property_value(
|
||||
@ -108,6 +136,7 @@ class Tests_Admin_WPPluginDependencies_GetDependencyNames extends WP_PluginDepen
|
||||
)
|
||||
);
|
||||
|
||||
$this->mock_api_response( 'failure' );
|
||||
self::$instance::initialize();
|
||||
|
||||
$this->set_property_value(
|
||||
@ -157,6 +186,7 @@ class Tests_Admin_WPPluginDependencies_GetDependencyNames extends WP_PluginDepen
|
||||
array( 'dependent/dependent.php' => array( 'RequiresPlugins' => 'dependency, dependency2' ) )
|
||||
);
|
||||
|
||||
$this->mock_api_response( 'failure' );
|
||||
self::$instance::initialize();
|
||||
|
||||
// The plugins are not in the Plugins repository.
|
||||
@ -211,6 +241,8 @@ class Tests_Admin_WPPluginDependencies_GetDependencyNames extends WP_PluginDepen
|
||||
|
||||
set_site_transient( 'wp_plugin_dependencies_plugin_data', array( 'dependency' => false ) );
|
||||
set_site_transient( 'wp_plugin_dependencies_plugin_timeout_dependency', true, 12 * HOUR_IN_SECONDS );
|
||||
|
||||
$this->mock_api_response( 'success' );
|
||||
self::$instance::get_dependency_names( 'dependent' );
|
||||
|
||||
// Restore $pagenow.
|
||||
|
||||
Loading…
Reference in New Issue
Block a user