Changeset 4724 for sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/tests/phpunit/tests/wporg-plugin-api.php
- Timestamp:
- 01/18/2017 10:01:01 PM (8 years ago)
- 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 8 8 */ 9 9 class 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; 10 19 11 20 public $fields = array( … … 32 41 'active_installs' => true, 33 42 'contributors' => true, 43 'donate_link' => true, 34 44 ); 35 45 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( 38 63 'timeout' => 15, 39 64 'body' => array( 40 'action' => 'plugin_information',41 'request' => serialize( (object) array( 'slug' => 'jetpack', 'fields' => $this->fields )),65 'action' => $action, 66 'request' => serialize( (object) $request ), 42 67 ), 68 'headers' => array( 'Host', 'api.wordpress.org' ), 43 69 ) ); 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 ) ); 44 81 $plugins = maybe_unserialize( wp_remote_retrieve_body( $response ) ); 45 82 … … 48 85 49 86 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 ); 52 89 $plugins = maybe_unserialize( wp_remote_retrieve_body( $response ) ); 53 90 … … 56 93 57 94 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 ); 60 97 $plugins = wp_remote_retrieve_body( $response ); 61 98 … … 66 103 67 104 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 ); 70 107 $plugins = (object) json_decode( wp_remote_retrieve_body( $response ), true ); 71 108 … … 74 111 75 112 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( 77 114 'timeout' => 15, 78 115 'body' => array( … … 80 117 'request' => (object) array( 'slug' => 'jetpack', 'fields' => $this->fields ), 81 118 ), 119 'headers' => array( 'Host', 'api.wordpress.org' ), 120 82 121 ) ); 122 83 123 $plugins = (object) json_decode( wp_remote_retrieve_body( $response ), true ); 84 85 124 $this->_check_response_attributes( $plugins ); 86 125 } … … 143 182 144 183 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 ) ); 146 185 $this->_check_response_plugin_query( $plugins, 1 ); 147 186 } … … 199 238 $this->assertAttributeInternalType( 'array', 'contributors', $plugin_info, 'Contributors should be an array' ); 200 239 $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 } 203 244 $this->assertObjectHasAttribute( 'compatibility', $plugin_info, 'Compatibility exists' ); 204 245 $this->assertAttributeInternalType( 'array', 'compatibility', $plugin_info, 'Compatibility should be an array' ); … … 225 266 $this->assertObjectHasAttribute( 'added', $plugin_info, 'Added exists' ); 226 267 $this->assertAttributeInternalType( 'string', 'short_description', $plugin_info, 'Added should be a string' ); 227 228 268 if ( function_exists( 'date_create_from_format' ) ) { 229 269 $last_updated_date = DateTime::createFromFormat( 'Y-m-d g:ia \G\M\T', $plugin_info->last_updated ); … … 270 310 } 271 311 } 312 313 /** 314 * 315 * @group plugins-api 316 */ 317 318 class 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.