Make WordPress Core


Ignore:
Timestamp:
02/04/2024 10:02:02 PM (14 months ago)
Author:
peterwilsoncc
Message:

Build/Test Tools: Mock plugin API response in WP_REST_Plugins_Controller_Test.

Avoid false test failures due to network conditions in the WP_REST_Plugins_Controller_Test class. This mocks HTTP responses from the plugin information endpoint for the link-manager plugin.

Props: peterwilsoncc, costdev.
See #59647.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-plugins-controller.php

    r56559 r57531  
    4242
    4343    /**
     44     * JSON decoded response from the WordPress.org plugin API.
     45     *
     46     * @var stdClass
     47     */
     48    private static $plugin_api_decoded_response;
     49
     50    /**
    4451     * Set up class test fixtures.
    4552     *
     
    6875            grant_super_admin( self::$super_admin );
    6976        }
     77
     78        self::$plugin_api_decoded_response = json_decode( file_get_contents( DIR_TESTDATA . '/plugins/link-manager.json' ) );
    7079    }
    7180
     
    10181027
    10191028    /**
    1020      * Sets up the plugin download to come locally instead of downloading it from .org
     1029     * Sets up the plugin repository requests to use local data.
     1030     *
     1031     * Requests to the plugin repository are mocked to avoid external HTTP requests so
     1032     * the test suite does not produce false negatives due to network failures.
     1033     *
     1034     * Both the plugin ZIP file and the plugin API response are mocked.
    10211035     *
    10221036     * @since 5.5.0
     
    10371051        );
    10381052
    1039         // Remove upgrade hooks which are not required for plugin installation tests
    1040         // and may interfere with the results due to a timeout in external HTTP requests.
     1053        add_filter(
     1054            'plugins_api',
     1055            function ( $bypass, $action, $args ) {
     1056                // Only mock the plugin_information (link-manager) request.
     1057                if ( 'plugin_information' !== $action || 'link-manager' !== $args->slug ) {
     1058                    return $bypass;
     1059                }
     1060                return self::$plugin_api_decoded_response;
     1061            },
     1062            10,
     1063            3
     1064        );
     1065
     1066        /*
     1067         * Remove upgrade hooks which are not required for plugin installation tests
     1068         * and may interfere with the results due to a timeout in external HTTP requests.
     1069         */
    10411070        remove_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 );
    10421071        remove_action( 'upgrader_process_complete', 'wp_version_check' );
Note: See TracChangeset for help on using the changeset viewer.