- Timestamp:
- 03/02/2025 10:05:08 PM (3 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/rest-api/rest-taxonomies-controller.php
r56746 r59899 61 61 } 62 62 63 /** 64 * @ticket 56481 65 */ 66 public function test_get_items_with_head_request_should_not_prepare_taxonomy_data() { 67 $request = new WP_REST_Request( 'HEAD', '/wp/v2/taxonomies' ); 68 $hook_name = 'rest_prepare_taxonomy'; 69 $filter = new MockAction(); 70 $callback = array( $filter, 'filter' ); 71 add_filter( $hook_name, $callback ); 72 $response = rest_get_server()->dispatch( $request ); 73 remove_filter( $hook_name, $callback ); 74 $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' ); 75 $this->assertSame( 0, $filter->get_call_count(), 'The "' . $hook_name . '" filter was called when it should not be for HEAD requests.' ); 76 $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' ); 77 } 78 63 79 public function test_get_items_context_edit() { 64 80 wp_set_current_user( self::$contributor_id ); … … 80 96 } 81 97 82 public function test_get_items_invalid_permission_for_context() { 98 99 /** 100 * @dataProvider data_readable_http_methods 101 * @ticket 56481 102 * 103 * @param string $method HTTP method to use. 104 */ 105 public function test_get_items_invalid_permission_for_context( $method ) { 83 106 wp_set_current_user( 0 ); 84 $request = new WP_REST_Request( 'GET', '/wp/v2/taxonomies' );107 $request = new WP_REST_Request( $method, '/wp/v2/taxonomies' ); 85 108 $request->set_param( 'context', 'edit' ); 86 109 $response = rest_get_server()->dispatch( $request ); 87 110 $this->assertErrorResponse( 'rest_cannot_view', $response, 401 ); 111 } 112 113 /** 114 * Data provider intended to provide HTTP method names for testing GET and HEAD requests. 115 * 116 * @return array 117 */ 118 public static function data_readable_http_methods() { 119 return array( 120 'GET request' => array( 'GET' ), 121 'HEAD request' => array( 'HEAD' ), 122 ); 88 123 } 89 124 … … 95 130 } 96 131 97 public function test_get_taxonomies_for_invalid_type() { 98 $request = new WP_REST_Request( 'GET', '/wp/v2/taxonomies' ); 132 /** 133 * @dataProvider data_readable_http_methods 134 * @ticket 56481 135 * 136 * @param string $method HTTP method to use. 137 */ 138 public function test_get_taxonomies_for_invalid_type( $method ) { 139 $request = new WP_REST_Request( $method, '/wp/v2/taxonomies' ); 99 140 $request->set_param( 'type', 'wingding' ); 100 141 $response = rest_get_server()->dispatch( $request ); 101 142 $this->assertSame( 200, $response->get_status() ); 143 if ( 'HEAD' === $method ) { 144 return null; 145 } 102 146 $data = $response->get_data(); 103 147 $this->assertSame( '{}', json_encode( $data ) ); … … 108 152 $response = rest_get_server()->dispatch( $request ); 109 153 $this->check_taxonomy_object_response( 'view', $response ); 154 } 155 156 /** 157 * @dataProvider data_readable_http_methods 158 * @ticket 56481 159 * 160 * @param string $method The HTTP method to use. 161 */ 162 public function test_get_item_should_allow_adding_headers_via_filter( $method ) { 163 $request = new WP_REST_Request( 'HEAD', '/wp/v2/taxonomies/category' ); 164 $hook_name = 'rest_prepare_taxonomy'; 165 $filter = new MockAction(); 166 $callback = array( $filter, 'filter' ); 167 add_filter( $hook_name, $callback ); 168 $header_filter = new class() { 169 public static function add_custom_header( $response ) { 170 $response->header( 'X-Test-Header', 'Test' ); 171 172 return $response; 173 } 174 }; 175 add_filter( $hook_name, array( $header_filter, 'add_custom_header' ) ); 176 $response = rest_get_server()->dispatch( $request ); 177 remove_filter( $hook_name, $callback ); 178 remove_filter( $hook_name, array( $header_filter, 'add_custom_header' ) ); 179 180 $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' ); 181 $this->assertSame( 1, $filter->get_call_count(), 'The "' . $hook_name . '" filter was called when it should not be for HEAD requests.' ); 182 $headers = $response->get_headers(); 183 $this->assertArrayHasKey( 'X-Test-Header', $headers, 'The "X-Test-Header" header should be present in the response.' ); 184 $this->assertSame( 'Test', $headers['X-Test-Header'], 'The "X-Test-Header" header value should be equal to "Test".' ); 185 if ( 'HEAD' !== $method ) { 186 return null; 187 } 188 $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' ); 110 189 } 111 190 … … 119 198 } 120 199 121 public function test_get_item_invalid_permission_for_context() { 200 /** 201 * @dataProvider data_readable_http_methods 202 * @ticket 56481 203 * 204 * @param string $method HTTP method to use. 205 */ 206 public function test_get_item_invalid_permission_for_context( $method ) { 122 207 wp_set_current_user( 0 ); 123 $request = new WP_REST_Request( 'GET', '/wp/v2/taxonomies/category' );208 $request = new WP_REST_Request( $method, '/wp/v2/taxonomies/category' ); 124 209 $request->set_param( 'context', 'edit' ); 125 210 $response = rest_get_server()->dispatch( $request ); … … 127 212 } 128 213 129 public function test_get_invalid_taxonomy() { 130 $request = new WP_REST_Request( 'GET', '/wp/v2/taxonomies/invalid' ); 214 /** 215 * @dataProvider data_readable_http_methods 216 * @ticket 56481 217 * 218 * @param string $method HTTP method to use. 219 */ 220 public function test_get_invalid_taxonomy( $method ) { 221 $request = new WP_REST_Request( $method, '/wp/v2/taxonomies/invalid' ); 131 222 $response = rest_get_server()->dispatch( $request ); 132 223 $this->assertErrorResponse( 'rest_taxonomy_invalid', $response, 404 ); 133 224 } 134 225 135 public function test_get_non_public_taxonomy_not_authenticated() { 226 /** 227 * @dataProvider data_readable_http_methods 228 * @ticket 56481 229 * 230 * @param string $method HTTP method to use. 231 */ 232 public function test_get_non_public_taxonomy_not_authenticated( $method ) { 136 233 register_taxonomy( 'api-private', 'post', array( 'public' => false ) ); 137 234 138 $request = new WP_REST_Request( 'GET', '/wp/v2/taxonomies/api-private' );235 $request = new WP_REST_Request( $method, '/wp/v2/taxonomies/api-private' ); 139 236 $response = rest_get_server()->dispatch( $request ); 140 237 $this->assertErrorResponse( 'rest_forbidden', $response, 401 ); 141 238 } 142 239 143 public function test_get_non_public_taxonomy_no_permission() { 240 /** 241 * @dataProvider data_readable_http_methods 242 * @ticket 56481 243 * 244 * @param string $method HTTP method to use. 245 */ 246 public function test_get_non_public_taxonomy_no_permission( $method ) { 144 247 wp_set_current_user( self::$contributor_id ); 145 248 register_taxonomy( 'api-private', 'post', array( 'public' => false ) ); 146 249 147 $request = new WP_REST_Request( 'GET', '/wp/v2/taxonomies/api-private' );250 $request = new WP_REST_Request( $method, '/wp/v2/taxonomies/api-private' ); 148 251 $response = rest_get_server()->dispatch( $request ); 149 252 $this->assertErrorResponse( 'rest_forbidden', $response, 403 );
Note: See TracChangeset
for help on using the changeset viewer.