- Timestamp:
- 01/03/2021 09:45:42 PM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/rest-api/rest-themes-controller.php
r49603 r49925 31 31 32 32 /** 33 * Admin user ID. 34 * 35 * @since 5.7.0 36 * 37 * @var int $admin_id 38 */ 39 protected static $admin_id; 40 41 /** 33 42 * The current theme object. 34 43 * … … 92 101 */ 93 102 public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 103 self::$admin_id = $factory->user->create( 104 array( 105 'role' => 'administrator', 106 ) 107 ); 94 108 self::$subscriber_id = $factory->user->create( 95 109 array( … … 115 129 self::delete_user( self::$subscriber_id ); 116 130 self::delete_user( self::$contributor_id ); 131 self::delete_user( self::$admin_id ); 117 132 } 118 133 … … 137 152 $routes = rest_get_server()->get_routes(); 138 153 $this->assertArrayHasKey( self::$themes_route, $routes ); 154 $this->assertArrayHasKey( self::$themes_route . '/(?P<stylesheet>[\\w-]+)', $routes ); 139 155 } 140 156 … … 152 168 $this->check_get_theme_response( $response ); 153 169 $fields = array( 170 '_links', 154 171 'author', 155 172 'author_uri', … … 159 176 'requires_wp', 160 177 'screenshot', 178 'status', 161 179 'stylesheet', 162 180 'tags', … … 171 189 172 190 /** 191 * Test retrieving a collection of inactive themes. 192 * 193 * @ticket 50152 194 */ 195 public function test_get_items_inactive() { 196 wp_set_current_user( self::$admin_id ); 197 $request = new WP_REST_Request( 'GET', self::$themes_route ); 198 $request->set_param( 'status', 'inactive' ); 199 200 $response = rest_get_server()->dispatch( $request ); 201 202 $this->assertEquals( 200, $response->get_status() ); 203 $data = $response->get_data(); 204 205 $fields = array( 206 '_links', 207 'author', 208 'author_uri', 209 'description', 210 'name', 211 'requires_php', 212 'requires_wp', 213 'screenshot', 214 'status', 215 'stylesheet', 216 'tags', 217 'template', 218 'textdomain', 219 'theme_uri', 220 'version', 221 ); 222 $this->assertEqualSets( $fields, array_keys( $data[0] ) ); 223 224 $this->assertContains( 'twentytwenty', wp_list_pluck( $data, 'stylesheet' ) ); 225 $this->assertNotContains( get_stylesheet(), wp_list_pluck( $data, 'stylesheet' ) ); 226 } 227 228 /** 229 * Test retrieving a collection of inactive themes. 230 * 231 * @ticket 50152 232 */ 233 public function test_get_items_active_and_inactive() { 234 wp_set_current_user( self::$admin_id ); 235 $request = new WP_REST_Request( 'GET', self::$themes_route ); 236 $request->set_param( 'status', array( 'active', 'inactive' ) ); 237 238 $response = rest_get_server()->dispatch( $request ); 239 240 $this->assertEquals( 200, $response->get_status() ); 241 $data = $response->get_data(); 242 243 $this->assertContains( 'twentytwenty', wp_list_pluck( $data, 'stylesheet' ) ); 244 $this->assertContains( get_stylesheet(), wp_list_pluck( $data, 'stylesheet' ) ); 245 } 246 247 /** 173 248 * @ticket 46723 174 */ 175 public function test_get_items_logged_out() { 249 * @ticket 50152 250 * @dataProvider data_get_items_by_status 251 */ 252 public function test_get_items_logged_out( $status, $error_code ) { 176 253 wp_set_current_user( 0 ); 177 $response = self::perform_active_theme_request(); 178 $this->assertErrorResponse( 'rest_user_cannot_view', $response, 401 ); 254 $request = new WP_REST_Request( 'GET', self::$themes_route ); 255 $request->set_param( 'status', $status ); 256 257 $response = rest_get_server()->dispatch( $request ); 258 $this->assertErrorResponse( $error_code, $response, 401 ); 179 259 } 180 260 … … 183 263 * 184 264 * @ticket 45016 185 */ 186 public function test_get_items_no_permission() { 265 * @ticket 50152 266 * @dataProvider data_get_items_by_status 267 */ 268 public function test_get_items_no_permission( $status, $error_code ) { 187 269 wp_set_current_user( self::$subscriber_id ); 188 $response = self::perform_active_theme_request(); 189 $this->assertErrorResponse( 'rest_user_cannot_view', $response, 403 ); 270 $request = new WP_REST_Request( 'GET', self::$themes_route ); 271 $request->set_param( 'status', $status ); 272 273 $response = rest_get_server()->dispatch( $request ); 274 $this->assertErrorResponse( $error_code, $response, 403 ); 275 } 276 277 public function data_get_items_by_status() { 278 return array( 279 array( 'active', 'rest_cannot_view_active_theme' ), 280 array( 'active, inactive', 'rest_cannot_view_themes' ), 281 array( 'inactive', 'rest_cannot_view_themes' ), 282 array( '', 'rest_cannot_view_themes' ), 283 ); 284 } 285 286 /** 287 * @ticket 50152 288 * @dataProvider data_get_items_by_status_for_contributor 289 */ 290 public function test_get_items_contributor( $status, $error_code ) { 291 wp_set_current_user( self::$contributor_id ); 292 $request = new WP_REST_Request( 'GET', self::$themes_route ); 293 $request->set_param( 'status', $status ); 294 295 $response = rest_get_server()->dispatch( $request ); 296 297 if ( $error_code ) { 298 $this->assertErrorResponse( $error_code, $response, 403 ); 299 } else { 300 $this->assertEquals( 200, $response->get_status() ); 301 } 302 } 303 304 public function data_get_items_by_status_for_contributor() { 305 return array( 306 array( 'active', '' ), 307 array( 'active, inactive', 'rest_cannot_view_themes' ), 308 array( 'inactive', 'rest_cannot_view_themes' ), 309 array( '', 'rest_cannot_view_themes' ), 310 ); 190 311 } 191 312 … … 222 343 $data = $response->get_data(); 223 344 $properties = $data['schema']['properties']; 224 $this->assertSame( 1 4, count( $properties ) );345 $this->assertSame( 15, count( $properties ) ); 225 346 226 347 $this->assertArrayHasKey( 'author', $properties ); … … 243 364 $this->assertArrayHasKey( 'requires_wp', $properties ); 244 365 $this->assertArrayHasKey( 'screenshot', $properties ); 366 $this->assertArrayHasKey( 'status', $properties ); 245 367 $this->assertArrayHasKey( 'stylesheet', $properties ); 246 368 … … 1084 1206 1085 1207 /** 1086 * The get_item() method does not exist for themes. 1087 */ 1088 public function test_get_item() {} 1208 * Test single theme 1209 * 1210 * @ticket 50152 1211 */ 1212 public function test_get_item() { 1213 wp_set_current_user( self::$admin_id ); 1214 $route = sprintf( '%s/%s', self::$themes_route, WP_DEFAULT_THEME ); 1215 $request = new WP_REST_Request( 'GET', $route ); 1216 $response = rest_get_server()->dispatch( $request ); 1217 1218 $this->assertEquals( 200, $response->get_status() ); 1219 $data = $response->get_data(); 1220 $links = $response->get_links(); 1221 $fields = array( 1222 'author', 1223 'author_uri', 1224 'description', 1225 'name', 1226 'requires_php', 1227 'requires_wp', 1228 'screenshot', 1229 'status', 1230 'stylesheet', 1231 'tags', 1232 'template', 1233 'textdomain', 1234 'theme_uri', 1235 'version', 1236 ); 1237 $fields_links = array( 'collection', 'self' ); 1238 1239 $this->assertEqualSets( $fields, array_keys( $data ) ); 1240 $this->assertEqualSets( $fields_links, array_keys( $links ) ); 1241 } 1242 1243 /** 1244 * @ticket 50152 1245 */ 1246 public function test_get_item_no_permission() { 1247 wp_set_current_user( self::$subscriber_id ); 1248 $request = new WP_REST_Request( 'GET', self::$themes_route . '/' . WP_DEFAULT_THEME ); 1249 $response = rest_get_server()->dispatch( $request ); 1250 $this->assertErrorResponse( 'rest_cannot_view_themes', $response, 403 ); 1251 } 1252 1253 /** 1254 * @ticket 50152 1255 */ 1256 public function test_get_active_item_no_permission() { 1257 wp_set_current_user( self::$subscriber_id ); 1258 $request = new WP_REST_Request( 'GET', self::$themes_route . '/' . get_stylesheet() ); 1259 $response = rest_get_server()->dispatch( $request ); 1260 $this->assertErrorResponse( 'rest_cannot_view_active_theme', $response, 403 ); 1261 } 1262 1263 /** 1264 * @ticket 50152 1265 */ 1266 public function test_get_item_invalid() { 1267 wp_set_current_user( self::$admin_id ); 1268 $request = new WP_REST_Request( 'GET', self::$themes_route . '/invalid' ); 1269 $response = rest_get_server()->dispatch( $request ); 1270 $this->assertErrorResponse( 'rest_theme_not_found', $response, 404 ); 1271 } 1272 1273 /** 1274 * @ticket 50152 1275 */ 1276 public function test_get_active_item_as_contributor() { 1277 $route = sprintf( '%s/%s', self::$themes_route, get_stylesheet() ); 1278 $request = new WP_REST_Request( 'GET', $route ); 1279 $response = rest_get_server()->dispatch( $request ); 1280 1281 $this->assertEquals( 200, $response->get_status() ); 1282 } 1089 1283 1090 1284 /**
Note: See TracChangeset
for help on using the changeset viewer.