- Timestamp:
- 11/30/2017 11:09:33 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/rest-api/rest-settings-controller.php
r42000 r42343 14 14 15 15 public static function wpSetUpBeforeClass( $factory ) { 16 self::$administrator = $factory->user->create( array( 17 'role' => 'administrator', 18 ) ); 16 self::$administrator = $factory->user->create( 17 array( 18 'role' => 'administrator', 19 ) 20 ); 19 21 } 20 22 … … 34 36 35 37 public function test_get_item() { 36 /** Individual settings can't be gotten * */37 wp_set_current_user( self::$administrator ); 38 $request = new WP_REST_Request( 'GET', '/wp/v2/settings/title' );38 /** Individual settings can't be gotten */ 39 wp_set_current_user( self::$administrator ); 40 $request = new WP_REST_Request( 'GET', '/wp/v2/settings/title' ); 39 41 $response = $this->server->dispatch( $request ); 40 42 $this->assertEquals( 404, $response->get_status() ); … … 45 47 46 48 public function test_get_item_is_not_public() { 47 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' );49 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' ); 48 50 $response = $this->server->dispatch( $request ); 49 51 $this->assertEquals( 403, $response->get_status() ); … … 52 54 public function test_get_items() { 53 55 wp_set_current_user( self::$administrator ); 54 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' );55 $response = $this->server->dispatch( $request ); 56 $data = $response->get_data();57 $actual = array_keys( $data );56 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' ); 57 $response = $this->server->dispatch( $request ); 58 $data = $response->get_data(); 59 $actual = array_keys( $data ); 58 60 59 61 $expected = array( … … 88 90 wp_set_current_user( self::$administrator ); 89 91 update_option( 'posts_per_page', 'invalid_number' ); // this is cast to (int) 1 90 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' );91 $response = $this->server->dispatch( $request ); 92 $data = $response->get_data();92 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' ); 93 $response = $this->server->dispatch( $request ); 94 $data = $response->get_data(); 93 95 94 96 $this->assertEquals( 200, $response->get_status() ); … … 99 101 wp_set_current_user( self::$administrator ); 100 102 101 register_setting( 'somegroup', 'mycustomsetting', array( 102 'show_in_rest' => array( 103 'name' => 'mycustomsettinginrest', 104 'schema' => array( 105 'enum' => array( 'validvalue1', 'validvalue2' ), 106 'default' => 'validvalue1', 107 ), 108 ), 109 'type' => 'string', 110 ) ); 111 112 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' ); 113 $response = $this->server->dispatch( $request ); 114 $data = $response->get_data(); 103 register_setting( 104 'somegroup', 'mycustomsetting', array( 105 'show_in_rest' => array( 106 'name' => 'mycustomsettinginrest', 107 'schema' => array( 108 'enum' => array( 'validvalue1', 'validvalue2' ), 109 'default' => 'validvalue1', 110 ), 111 ), 112 'type' => 'string', 113 ) 114 ); 115 116 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' ); 117 $response = $this->server->dispatch( $request ); 118 $data = $response->get_data(); 115 119 116 120 $this->assertEquals( 200, $response->get_status() ); … … 120 124 update_option( 'mycustomsetting', 'validvalue2' ); 121 125 122 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' );123 $response = $this->server->dispatch( $request ); 124 $data = $response->get_data();126 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' ); 127 $response = $this->server->dispatch( $request ); 128 $data = $response->get_data(); 125 129 $this->assertEquals( 'validvalue2', $data['mycustomsettinginrest'] ); 126 130 … … 131 135 wp_set_current_user( self::$administrator ); 132 136 133 register_setting( 'somegroup', 'mycustomsetting', array( 134 'show_in_rest' => array( 135 'schema' => array( 136 'type' => 'array', 137 'items' => array( 138 'type' => 'integer', 139 ), 140 ), 141 ), 142 'type' => 'array', 143 ) ); 137 register_setting( 138 'somegroup', 'mycustomsetting', array( 139 'show_in_rest' => array( 140 'schema' => array( 141 'type' => 'array', 142 'items' => array( 143 'type' => 'integer', 144 ), 145 ), 146 ), 147 'type' => 'array', 148 ) 149 ); 144 150 145 151 // Array is cast to correct types. 146 152 update_option( 'mycustomsetting', array( '1', '2' ) ); 147 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' );148 $response = $this->server->dispatch( $request ); 149 $data = $response->get_data();153 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' ); 154 $response = $this->server->dispatch( $request ); 155 $data = $response->get_data(); 150 156 $this->assertEquals( array( 1, 2 ), $data['mycustomsetting'] ); 151 157 152 158 // Empty array works as expected. 153 159 update_option( 'mycustomsetting', array() ); 154 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' );155 $response = $this->server->dispatch( $request ); 156 $data = $response->get_data();160 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' ); 161 $response = $this->server->dispatch( $request ); 162 $data = $response->get_data(); 157 163 $this->assertEquals( array(), $data['mycustomsetting'] ); 158 164 159 165 // Invalid value 160 166 update_option( 'mycustomsetting', array( array( 1 ) ) ); 161 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' );162 $response = $this->server->dispatch( $request ); 163 $data = $response->get_data();167 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' ); 168 $response = $this->server->dispatch( $request ); 169 $data = $response->get_data(); 164 170 $this->assertEquals( null, $data['mycustomsetting'] ); 165 171 166 172 // No option value 167 173 delete_option( 'mycustomsetting' ); 168 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' );169 $response = $this->server->dispatch( $request ); 170 $data = $response->get_data();174 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' ); 175 $response = $this->server->dispatch( $request ); 176 $data = $response->get_data(); 171 177 $this->assertEquals( null, $data['mycustomsetting'] ); 172 178 … … 177 183 wp_set_current_user( self::$administrator ); 178 184 179 register_setting( 'somegroup', 'mycustomsetting', array( 180 'show_in_rest' => array( 181 'schema' => array( 182 'type' => 'object', 183 'properties' => array( 184 'a' => array( 185 'type' => 'integer', 185 register_setting( 186 'somegroup', 'mycustomsetting', array( 187 'show_in_rest' => array( 188 'schema' => array( 189 'type' => 'object', 190 'properties' => array( 191 'a' => array( 192 'type' => 'integer', 193 ), 186 194 ), 187 195 ), 188 196 ), 189 ),190 'type' => 'object',191 ) );197 'type' => 'object', 198 ) 199 ); 192 200 193 201 // We have to re-register the route, as the args changes based off registered settings. … … 197 205 // Object is cast to correct types. 198 206 update_option( 'mycustomsetting', array( 'a' => '1' ) ); 199 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' );200 $response = $this->server->dispatch( $request ); 201 $data = $response->get_data();207 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' ); 208 $response = $this->server->dispatch( $request ); 209 $data = $response->get_data(); 202 210 $this->assertEquals( array( 'a' => 1 ), $data['mycustomsetting'] ); 203 211 204 212 // Empty array works as expected. 205 213 update_option( 'mycustomsetting', array() ); 206 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' );207 $response = $this->server->dispatch( $request ); 208 $data = $response->get_data();214 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' ); 215 $response = $this->server->dispatch( $request ); 216 $data = $response->get_data(); 209 217 $this->assertEquals( array(), $data['mycustomsetting'] ); 210 218 211 219 // Invalid value 212 update_option( 'mycustomsetting', array( 'a' => 1, 'b' => 2 ) ); 213 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' ); 214 $response = $this->server->dispatch( $request ); 215 $data = $response->get_data(); 220 update_option( 221 'mycustomsetting', array( 222 'a' => 1, 223 'b' => 2, 224 ) 225 ); 226 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' ); 227 $response = $this->server->dispatch( $request ); 228 $data = $response->get_data(); 216 229 $this->assertEquals( null, $data['mycustomsetting'] ); 217 230 … … 232 245 add_filter( 'rest_pre_get_setting', array( $this, 'get_setting_custom_callback' ), 10, 3 ); 233 246 234 register_setting( 'somegroup', 'mycustomsetting1', array( 235 'show_in_rest' => array( 236 'name' => 'mycustomsettinginrest1', 237 ), 238 'type' => 'string', 239 ) ); 240 241 register_setting( 'somegroup', 'mycustomsetting2', array( 242 'show_in_rest' => array( 243 'name' => 'mycustomsettinginrest2', 244 ), 245 'type' => 'string', 246 ) ); 247 register_setting( 248 'somegroup', 'mycustomsetting1', array( 249 'show_in_rest' => array( 250 'name' => 'mycustomsettinginrest1', 251 ), 252 'type' => 'string', 253 ) 254 ); 255 256 register_setting( 257 'somegroup', 'mycustomsetting2', array( 258 'show_in_rest' => array( 259 'name' => 'mycustomsettinginrest2', 260 ), 261 'type' => 'string', 262 ) 263 ); 247 264 248 265 update_option( 'mycustomsetting1', 'unfiltered1' ); 249 266 update_option( 'mycustomsetting2', 'unfiltered2' ); 250 267 251 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' );252 $response = $this->server->dispatch( $request ); 253 $data = $response->get_data();268 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' ); 269 $response = $this->server->dispatch( $request ); 270 $data = $response->get_data(); 254 271 255 272 $this->assertEquals( 200, $response->get_status() ); … … 268 285 wp_set_current_user( self::$administrator ); 269 286 270 register_setting( 'somegroup', 'mycustomsetting', array( 271 'show_in_rest' => array( 272 'name' => 'mycustomsettinginrest', 273 'schema' => array( 274 'enum' => array( 'validvalue1', 'validvalue2' ), 275 'default' => 'validvalue1', 276 ), 277 ), 278 'type' => 'string', 279 ) ); 287 register_setting( 288 'somegroup', 'mycustomsetting', array( 289 'show_in_rest' => array( 290 'name' => 'mycustomsettinginrest', 291 'schema' => array( 292 'enum' => array( 'validvalue1', 'validvalue2' ), 293 'default' => 'validvalue1', 294 ), 295 ), 296 'type' => 'string', 297 ) 298 ); 280 299 281 300 update_option( 'mycustomsetting', array( 'A sneaky array!' ) ); 282 301 283 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' );284 $response = $this->server->dispatch( $request ); 285 $data = $response->get_data();302 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' ); 303 $response = $this->server->dispatch( $request ); 304 $data = $response->get_data(); 286 305 $this->assertEquals( null, $data['mycustomsettinginrest'] ); 287 306 } … … 290 309 wp_set_current_user( self::$administrator ); 291 310 292 register_setting( 'somegroup', 'mycustomsetting', array( 293 'show_in_rest' => array( 294 'name' => 'mycustomsettinginrest', 295 'schema' => array( 296 'enum' => array( 'validvalue1', 'validvalue2' ), 297 'default' => 'validvalue1', 298 ), 299 ), 300 'type' => 'string', 301 ) ); 311 register_setting( 312 'somegroup', 'mycustomsetting', array( 313 'show_in_rest' => array( 314 'name' => 'mycustomsettinginrest', 315 'schema' => array( 316 'enum' => array( 'validvalue1', 'validvalue2' ), 317 'default' => 'validvalue1', 318 ), 319 ), 320 'type' => 'string', 321 ) 322 ); 302 323 303 324 update_option( 'mycustomsetting', (object) array( 'A sneaky array!' ) ); 304 325 305 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' );306 $response = $this->server->dispatch( $request ); 307 $data = $response->get_data();326 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' ); 327 $response = $this->server->dispatch( $request ); 328 $data = $response->get_data(); 308 329 $this->assertEquals( null, $data['mycustomsettinginrest'] ); 309 330 unregister_setting( 'somegroup', 'mycustomsetting' ); … … 319 340 $request->set_param( 'title', 'The new title!' ); 320 341 $response = $this->server->dispatch( $request ); 321 $data = $response->get_data();342 $data = $response->get_data(); 322 343 323 344 $this->assertEquals( 200, $response->get_status() ); … … 336 357 337 358 public function test_update_item_with_array() { 338 register_setting( 'somegroup', 'mycustomsetting', array( 339 'show_in_rest' => array( 340 'schema' => array( 341 'type' => 'array', 342 'items' => array( 343 'type' => 'integer', 344 ), 345 ), 346 ), 347 'type' => 'array', 348 ) ); 359 register_setting( 360 'somegroup', 'mycustomsetting', array( 361 'show_in_rest' => array( 362 'schema' => array( 363 'type' => 'array', 364 'items' => array( 365 'type' => 'integer', 366 ), 367 ), 368 ), 369 'type' => 'array', 370 ) 371 ); 349 372 350 373 // We have to re-register the route, as the args changes based off registered settings. … … 356 379 $request->set_param( 'mycustomsetting', array( '1', '2' ) ); 357 380 $response = $this->server->dispatch( $request ); 358 $data = $response->get_data();381 $data = $response->get_data(); 359 382 $this->assertEquals( array( 1, 2 ), $data['mycustomsetting'] ); 360 383 $this->assertEquals( array( 1, 2 ), get_option( 'mycustomsetting' ) ); … … 364 387 $request->set_param( 'mycustomsetting', array() ); 365 388 $response = $this->server->dispatch( $request ); 366 $data = $response->get_data();389 $data = $response->get_data(); 367 390 $this->assertEquals( array(), $data['mycustomsetting'] ); 368 391 $this->assertEquals( array(), get_option( 'mycustomsetting' ) ); … … 378 401 379 402 public function test_update_item_with_nested_object() { 380 register_setting( 'somegroup', 'mycustomsetting', array( 381 'show_in_rest' => array( 382 'schema' => array( 383 'type' => 'object', 384 'properties' => array( 385 'a' => array( 386 'type' => 'object', 387 'properties' => array( 388 'b' => array( 389 'type' => 'number', 403 register_setting( 404 'somegroup', 'mycustomsetting', array( 405 'show_in_rest' => array( 406 'schema' => array( 407 'type' => 'object', 408 'properties' => array( 409 'a' => array( 410 'type' => 'object', 411 'properties' => array( 412 'b' => array( 413 'type' => 'number', 414 ), 390 415 ), 391 416 ), … … 393 418 ), 394 419 ), 395 ),396 'type' => 'object',397 ) );420 'type' => 'object', 421 ) 422 ); 398 423 399 424 // We have to re-register the route, as the args changes based off registered settings. … … 403 428 404 429 $request = new WP_REST_Request( 'PUT', '/wp/v2/settings' ); 405 $request->set_param( 'mycustomsetting', array( 'a' => array( 'b' => 1, 'c' => 1 ) ) ); 430 $request->set_param( 431 'mycustomsetting', array( 432 'a' => array( 433 'b' => 1, 434 'c' => 1, 435 ), 436 ) 437 ); 406 438 $response = $this->server->dispatch( $request ); 407 439 $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); … … 409 441 410 442 public function test_update_item_with_object() { 411 register_setting( 'somegroup', 'mycustomsetting', array( 412 'show_in_rest' => array( 413 'schema' => array( 414 'type' => 'object', 415 'properties' => array( 416 'a' => array( 417 'type' => 'integer', 443 register_setting( 444 'somegroup', 'mycustomsetting', array( 445 'show_in_rest' => array( 446 'schema' => array( 447 'type' => 'object', 448 'properties' => array( 449 'a' => array( 450 'type' => 'integer', 451 ), 418 452 ), 419 453 ), 420 454 ), 421 ),422 'type' => 'object',423 ) );455 'type' => 'object', 456 ) 457 ); 424 458 425 459 // We have to re-register the route, as the args changes based off registered settings. … … 431 465 $request->set_param( 'mycustomsetting', array( 'a' => 1 ) ); 432 466 $response = $this->server->dispatch( $request ); 433 $data = $response->get_data();467 $data = $response->get_data(); 434 468 $this->assertEquals( array( 'a' => 1 ), $data['mycustomsetting'] ); 435 469 $this->assertEquals( array( 'a' => 1 ), get_option( 'mycustomsetting' ) ); … … 439 473 $request->set_param( 'mycustomsetting', array() ); 440 474 $response = $this->server->dispatch( $request ); 441 $data = $response->get_data();475 $data = $response->get_data(); 442 476 $this->assertEquals( array(), $data['mycustomsetting'] ); 443 477 $this->assertEquals( array(), get_option( 'mycustomsetting' ) ); … … 445 479 // Provide more keys. 446 480 $request = new WP_REST_Request( 'PUT', '/wp/v2/settings' ); 447 $request->set_param( 'mycustomsetting', array( 'a' => 1, 'b' => 2 ) ); 481 $request->set_param( 482 'mycustomsetting', array( 483 'a' => 1, 484 'b' => 2, 485 ) 486 ); 448 487 $response = $this->server->dispatch( $request ); 449 488 … … 465 504 $request->set_param( 'description', 'The old description!' ); 466 505 $response = $this->server->dispatch( $request ); 467 $data = $response->get_data();506 $data = $response->get_data(); 468 507 $this->assertEquals( 200, $response->get_status() ); 469 508 $this->assertEquals( 'The old title!', $data['title'] ); … … 478 517 $request->set_param( 'description', 'The new description!' ); 479 518 $response = $this->server->dispatch( $request ); 480 $data = $response->get_data();519 $data = $response->get_data(); 481 520 482 521 $this->assertEquals( 200, $response->get_status() ); … … 523 562 $request->set_param( 'posts_per_page', null ); 524 563 $response = $this->server->dispatch( $request ); 525 $data = $response->get_data();564 $data = $response->get_data(); 526 565 527 566 $this->assertEquals( 200, $response->get_status() ); … … 542 581 wp_set_current_user( self::$administrator ); 543 582 544 register_setting( 'somegroup', 'mycustomsetting', array( 545 'show_in_rest' => true, 546 'type' => 'string', 547 ) ); 583 register_setting( 584 'somegroup', 'mycustomsetting', array( 585 'show_in_rest' => true, 586 'type' => 'string', 587 ) 588 ); 548 589 update_option( 'mycustomsetting', array( 'A sneaky array!' ) ); 549 590 … … 557 598 558 599 public function test_delete_item() { 559 /** Settings can't be deleted * */560 $request = new WP_REST_Request( 'DELETE', '/wp/v2/settings/title' );600 /** Settings can't be deleted */ 601 $request = new WP_REST_Request( 'DELETE', '/wp/v2/settings/title' ); 561 602 $response = $this->server->dispatch( $request ); 562 603 $this->assertEquals( 404, $response->get_status() );
Note: See TracChangeset
for help on using the changeset viewer.