WordPress.org

Making WordPress.org


Ignore:
Timestamp:
01/18/2017 10:01:01 PM (3 years ago)
Author:
tellyworth
Message:

Plugin directory tests: update the unit tests to run on both the old and new APIs.

Fix a few stubborn issues and account for differences between the two.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/tests/phpunit/tests/wporg-plugin-api.php

    r4223 r4724  
    88 */
    99class Tests_Plugins_API extends WP_UnitTestCase {
     10
     11    public $api_endpoint_1_0 = 'http://api.wordpress.org/plugins/info/1.0/';
     12    public $api_endpoint_1_1 = 'http://api.wordpress.org/plugins/info/1.1/';
     13    public $api_endpoint_plugin_php = 'http://api.wordpress.org/plugins/info/1.0/jetpack.php';
     14    public $api_endpoint_plugin_xml = 'http://api.wordpress.org/plugins/info/1.0/jetpack.xml';
     15    public $api_endpoint_plugin_json = 'http://api.wordpress.org/plugins/info/1.0/jetpack.json';
     16
     17    public $user_agent = 'WordPress/4.8'; // Tell the API to use the v3 back-end
     18    public $require_tested_value = true;
    1019
    1120    public $fields = array(
     
    3241        'active_installs'   => true,
    3342        'contributors'      => true,
     43        'donate_link'       => true,
    3444    );
    3545
    36     function test_wporg_plugin_api_serialize_php() {
    37         $response = wp_remote_post( 'http://api.wordpress.org/plugins/info/1.0/', array(
     46    function setUp() {
     47        parent::setUp();
     48        add_filter( 'http_headers_useragent', array( $this, 'filter_http_headers_useragent' ) );
     49    }
     50
     51    function tearDown() {
     52        remove_filter( 'http_headers_useragent', array( $this, 'filter_http_headers_useragent' ) );
     53        parent::tearDown();
     54    }
     55
     56    // Override the user-agent header in plugins_api() requests to force the API to use the new WP codebase.
     57    function filter_http_headers_useragent( $user_agent ) {
     58        return $this->user_agent;
     59    }
     60
     61    function api_remote_post( $url, $action, $request ) {
     62        return wp_remote_post( $url, array(
    3863            'timeout' => 15,
    3964            'body'    => array(
    40                 'action'  => 'plugin_information',
    41                 'request' => serialize( (object) array( 'slug' => 'jetpack', 'fields' => $this->fields ) ),
     65                'action'  => $action,
     66                'request' => serialize( (object) $request ),
    4267            ),
     68            'headers' => array( 'Host', 'api.wordpress.org' ),
    4369        ) );
     70    }
     71
     72    function api_remote_get( $url ) {
     73        return wp_remote_get( $url, array(
     74            'timeout' => 15,
     75            'headers' => array( 'Host', 'api.wordpress.org' ),
     76        ) );
     77    }
     78
     79    function test_wporg_plugin_api_serialize_php() {
     80        $response = $this->api_remote_post( $this->api_endpoint_1_0, 'plugin_information', array( 'slug' => 'jetpack', 'fields' => $this->fields ) );
    4481        $plugins = maybe_unserialize( wp_remote_retrieve_body( $response ) );
    4582
     
    4885
    4986    function test_wporg_plugin_api_serialize_php_get() {
    50         $url      = add_query_arg( 'fields', implode( ',' , array_keys( $this->fields ) ), 'http://api.wordpress.org/plugins/info/1.0/jetpack.php' );
    51         $response = wp_remote_get( $url );
     87        $url      = add_query_arg( 'fields', implode( ',' , array_keys( $this->fields ) ), $this->api_endpoint_plugin_php );
     88        $response = $this->api_remote_get( $url );
    5289        $plugins  = maybe_unserialize( wp_remote_retrieve_body( $response ) );
    5390
     
    5693
    5794    function test_wporg_plugin_api_xml() {
    58         $url      = add_query_arg( 'fields', implode( ',' , array_keys( $this->fields ) ), 'http://api.wordpress.org/plugins/info/1.0/jetpack.xml' );
    59         $response = wp_remote_get( $url );
     95        $url      = add_query_arg( 'fields', implode( ',' , array_keys( $this->fields ) ), $this->api_endpoint_plugin_xml );
     96        $response = $this->api_remote_get( $url );
    6097        $plugins  = wp_remote_retrieve_body( $response );
    6198
     
    66103
    67104    function test_wporg_plugin_api_json() {
    68         $url      = add_query_arg( 'fields', implode( ',' , array_keys( $this->fields ) ), 'http://api.wordpress.org/plugins/info/1.0/jetpack.json' );
    69         $response = wp_remote_get( $url );
     105        $url      = add_query_arg( 'fields', implode( ',' , array_keys( $this->fields ) ), $this->api_endpoint_plugin_json );
     106        $response = $this->api_remote_get( $url );
    70107        $plugins  = (object) json_decode( wp_remote_retrieve_body( $response ), true );
    71108
     
    74111
    75112    function test_wporg_plugin_api_1_1_json() {
    76         $response = wp_remote_post( 'http://api.wordpress.org/plugins/info/1.1/', array(
     113        $response = wp_remote_post( $this->api_endpoint_1_1, array(
    77114            'timeout' => 15,
    78115            'body'    => array(
     
    80117                'request' => (object) array( 'slug' => 'jetpack', 'fields' => $this->fields ),
    81118            ),
     119            'headers' => array( 'Host', 'api.wordpress.org' ),
     120
    82121        ) );
     122
    83123        $plugins = (object) json_decode( wp_remote_retrieve_body( $response ), true );
    84 
    85124        $this->_check_response_attributes( $plugins );
    86125    }
     
    143182
    144183    function test_plugins_api_function_action_query_plugins_installed_plugins() {
    145         $plugins = plugins_api( 'query_plugins', array( 'installed_plugins' => array( 'jetpack' ), 'fields' => $this->fields ) );
     184        $plugins = plugins_api( 'query_plugins', array( 'browse' => 'recommended', 'installed_plugins' => array( 'jetpack' ), 'fields' => $this->fields ) );
    146185        $this->_check_response_plugin_query( $plugins, 1 );
    147186    }
     
    199238        $this->assertAttributeInternalType( 'array', 'contributors', $plugin_info, 'Contributors should be an array' );
    200239        $this->assertObjectHasAttribute( 'requires', $plugin_info, 'Requires exists' );
    201         $this->assertObjectHasAttribute( 'tested', $plugin_info, 'Tested exists' );
    202         $this->assertAttributeInternalType( 'string', 'tested', $plugin_info, 'Tested should be a string' );
     240        if ( $this->require_tested_value || isset( $plugin_info->tested ) ) {
     241            $this->assertObjectHasAttribute( 'tested', $plugin_info, 'Tested exists' );
     242            $this->assertAttributeInternalType( 'string', 'tested', $plugin_info, 'Tested should be a string' );
     243        }
    203244        $this->assertObjectHasAttribute( 'compatibility', $plugin_info, 'Compatibility exists' );
    204245        $this->assertAttributeInternalType( 'array', 'compatibility', $plugin_info, 'Compatibility should be an array' );
     
    225266        $this->assertObjectHasAttribute( 'added', $plugin_info, 'Added exists' );
    226267        $this->assertAttributeInternalType( 'string', 'short_description', $plugin_info, 'Added should be a string' );
    227 
    228268        if ( function_exists( 'date_create_from_format' ) ) {
    229269            $last_updated_date = DateTime::createFromFormat( 'Y-m-d g:ia \G\M\T', $plugin_info->last_updated );
     
    270310    }
    271311}
     312
     313/**
     314 *
     315 * @group plugins-api
     316 */
     317
     318class Tests_Plugins_API_Old extends Tests_Plugins_API {
     319    public $user_agent = 'WordPress/4.7'; // Tell the API to use the old back-end
     320    public $require_tested_value = false; // Old API omits 'tested' if its value is empty
     321
     322
     323}
Note: See TracChangeset for help on using the changeset viewer.