Make WordPress Core

Changeset 57674


Ignore:
Timestamp:
02/21/2024 01:05:53 AM (3 months ago)
Author:
costdev
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/admin/plugin-dependencies/getDependencyNames.php

    r57545 r57674  
    2020
    2121    /**
     22     * Mocks an API response.
     23     *
     24     * @param string $type The type of response. Accepts 'success' or 'failure'.
     25     */
     26    private function mock_api_response( $type ) {
     27        add_filter(
     28            'plugins_api',
     29            function ( $bypass, $action, $args ) use ( $type ) {
     30                if ( 'plugin_information' === $action && isset( $args->slug ) && str_starts_with( $args->slug, 'dependency' ) ) {
     31                    if ( 'success' === $type ) {
     32                        return (object) array(
     33                            'slug' => $args->slug,
     34                            'name' => 'Dependency ' . str_replace( 'dependency', '', $args->slug ),
     35                        );
     36                    } elseif ( 'failure' === $type ) {
     37                        return new WP_Error( 'plugin_not_found', 'Plugin not found.' );
     38                    }
     39                }
     40
     41                return $bypass;
     42            },
     43            10,
     44            3
     45        );
     46    }
     47
     48    /**
    2249     * Tests that dependency names are retrieved.
    2350     *
     
    4168        );
    4269
     70        $this->mock_api_response( 'success' );
    4371        self::$instance::initialize();
    4472
     
    109137        );
    110138
     139        $this->mock_api_response( 'failure' );
    111140        self::$instance::initialize();
    112141
     
    158187        );
    159188
     189        $this->mock_api_response( 'failure' );
    160190        self::$instance::initialize();
    161191
     
    212242        set_site_transient( 'wp_plugin_dependencies_plugin_data', array( 'dependency' => false ) );
    213243        set_site_transient( 'wp_plugin_dependencies_plugin_timeout_dependency', true, 12 * HOUR_IN_SECONDS );
     244
     245        $this->mock_api_response( 'success' );
    214246        self::$instance::get_dependency_names( 'dependent' );
    215247
Note: See TracChangeset for help on using the changeset viewer.