Changeset 42343 for trunk/tests/phpunit/tests/ajax/CustomizeManager.php
- Timestamp:
- 11/30/2017 11:09:33 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/ajax/CustomizeManager.php
r41839 r42343 45 45 public static function wpSetUpBeforeClass( $factory ) { 46 46 self::$subscriber_user_id = $factory->user->create( array( 'role' => 'subscriber' ) ); 47 self::$admin_user_id = $factory->user->create( array( 'role' => 'administrator' ) );47 self::$admin_user_id = $factory->user->create( array( 'role' => 'administrator' ) ); 48 48 } 49 49 … … 71 71 protected function make_ajax_call( $action ) { 72 72 $this->_last_response_parsed = null; 73 $this->_last_response = '';73 $this->_last_response = ''; 74 74 try { 75 75 $this->_handleAjax( $action ); … … 120 120 // Unauthorized. 121 121 wp_set_current_user( self::$subscriber_user_id ); 122 $nonce = wp_create_nonce( 'save-customize_' . $wp_customize->get_stylesheet() );122 $nonce = wp_create_nonce( 'save-customize_' . $wp_customize->get_stylesheet() ); 123 123 $_POST['nonce'] = $_GET['nonce'] = $_REQUEST['nonce'] = $nonce; 124 $exception = null;124 $exception = null; 125 125 try { 126 126 ob_start(); … … 134 134 // Not called setup_theme. 135 135 wp_set_current_user( self::$admin_user_id ); 136 $nonce = wp_create_nonce( 'save-customize_' . $wp_customize->get_stylesheet() );136 $nonce = wp_create_nonce( 'save-customize_' . $wp_customize->get_stylesheet() ); 137 137 $_POST['nonce'] = $_GET['nonce'] = $_REQUEST['nonce'] = $nonce; 138 138 $this->make_ajax_call( 'customize_save' ); … … 148 148 149 149 // User cannot create. 150 $nonce = wp_create_nonce( 'save-customize_' . $wp_customize->get_stylesheet() );151 $_POST['nonce'] = $_GET['nonce'] = $_REQUEST['nonce'] = $nonce;152 $post_type_obj = get_post_type_object( 'customize_changeset' );150 $nonce = wp_create_nonce( 'save-customize_' . $wp_customize->get_stylesheet() ); 151 $_POST['nonce'] = $_GET['nonce'] = $_REQUEST['nonce'] = $nonce; 152 $post_type_obj = get_post_type_object( 'customize_changeset' ); 153 153 $post_type_obj->cap->create_posts = 'create_customize_changesets'; 154 154 $this->make_ajax_call( 'customize_save' ); … … 166 166 $this->assertFalse( $this->_last_response_parsed['success'] ); 167 167 $this->assertEquals( 'changeset_already_published', $this->_last_response_parsed['data']['code'] ); 168 wp_update_post( array( 169 'ID' => $wp_customize->changeset_post_id(), 170 'post_status' => 'auto-draft', 171 ) ); 168 wp_update_post( 169 array( 170 'ID' => $wp_customize->changeset_post_id(), 171 'post_status' => 'auto-draft', 172 ) 173 ); 172 174 173 175 // User cannot edit. 174 $post_type_obj = get_post_type_object( 'customize_changeset' );176 $post_type_obj = get_post_type_object( 'customize_changeset' ); 175 177 $post_type_obj->cap->edit_post = 'edit_customize_changesets'; 176 178 $this->make_ajax_call( 'customize_save' ); … … 189 191 190 192 // Bad customize_changeset_status. 191 $_POST['customize_changeset_data'] = '{}';193 $_POST['customize_changeset_data'] = '{}'; 192 194 $_POST['customize_changeset_status'] = 'unrecognized'; 193 195 $this->make_ajax_call( 'customize_save' ); … … 196 198 197 199 // Disallowed publish posts if not allowed. 198 $post_type_obj = get_post_type_object( 'customize_changeset' );199 $post_type_obj->cap->publish_posts = 'publish_customize_changesets';200 $post_type_obj = get_post_type_object( 'customize_changeset' ); 201 $post_type_obj->cap->publish_posts = 'publish_customize_changesets'; 200 202 $_POST['customize_changeset_status'] = 'publish'; 201 203 $this->make_ajax_call( 'customize_save' ); … … 210 212 // Validate date. 211 213 $_POST['customize_changeset_status'] = 'draft'; 212 $_POST['customize_changeset_date'] = 'BAD DATE';214 $_POST['customize_changeset_date'] = 'BAD DATE'; 213 215 $this->make_ajax_call( 'customize_save' ); 214 216 $this->assertFalse( $this->_last_response_parsed['success'] ); … … 222 224 $this->assertTrue( $this->_last_response_parsed['success'] ); 223 225 $_POST['customize_changeset_status'] = 'future'; 224 $_POST['customize_changeset_date'] = '+10 minutes';226 $_POST['customize_changeset_date'] = '+10 minutes'; 225 227 $this->make_ajax_call( 'customize_save' ); 226 228 $this->assertTrue( $this->_last_response_parsed['success'] ); 227 229 $this->assertEquals( 'future', get_post_status( $wp_customize->changeset_post_id() ) ); 228 wp_update_post( array( 229 'ID' => $wp_customize->changeset_post_id(), 230 'post_status' => 'auto-draft', 231 ) ); 230 wp_update_post( 231 array( 232 'ID' => $wp_customize->changeset_post_id(), 233 'post_status' => 'auto-draft', 234 ) 235 ); 232 236 } 233 237 … … 241 245 global $wp_customize; 242 246 wp_set_current_user( self::$admin_user_id ); 243 $wp_customize = new WP_Customize_Manager( array( 244 'changeset_uuid' => $uuid, 245 ) ); 247 $wp_customize = new WP_Customize_Manager( 248 array( 249 'changeset_uuid' => $uuid, 250 ) 251 ); 246 252 $wp_customize->register_controls(); 247 $nonce = wp_create_nonce( 'save-customize_' . $wp_customize->get_stylesheet() );253 $nonce = wp_create_nonce( 'save-customize_' . $wp_customize->get_stylesheet() ); 248 254 $_POST['nonce'] = $_GET['nonce'] = $_REQUEST['nonce'] = $nonce; 249 255 $wp_customize->setup_theme(); … … 261 267 262 268 $_POST['customize_changeset_status'] = 'publish'; 263 $_POST['customize_changeset_title'] = 'Success Changeset'; 264 $_POST['customize_changeset_data'] = wp_json_encode( array( 265 'blogname' => array( 266 'value' => 'Successful Site Title', 267 ), 268 ) ); 269 $_POST['customize_changeset_title'] = 'Success Changeset'; 270 $_POST['customize_changeset_data'] = wp_json_encode( 271 array( 272 'blogname' => array( 273 'value' => 'Successful Site Title', 274 ), 275 ) 276 ); 269 277 $this->make_ajax_call( 'customize_save' ); 270 278 $this->assertTrue( $this->_last_response_parsed['success'] ); … … 287 295 $uuid = wp_generate_uuid4(); 288 296 289 $post_id = $this->factory()->post->create( array( 290 'post_name' => $uuid, 291 'post_title' => 'Original', 292 'post_type' => 'customize_changeset', 293 'post_status' => 'auto-draft', 294 'post_content' => wp_json_encode( array( 295 'blogname' => array( 296 'value' => 'New Site Title', 297 $post_id = $this->factory()->post->create( 298 array( 299 'post_name' => $uuid, 300 'post_title' => 'Original', 301 'post_type' => 'customize_changeset', 302 'post_status' => 'auto-draft', 303 'post_content' => wp_json_encode( 304 array( 305 'blogname' => array( 306 'value' => 'New Site Title', 307 ), 308 ) 297 309 ), 298 ) ),299 ) );310 ) 311 ); 300 312 $wp_customize = $this->set_up_valid_state( $uuid ); 301 313 302 314 $_POST['customize_changeset_status'] = 'publish'; 303 $_POST['customize_changeset_title'] = 'Published';315 $_POST['customize_changeset_title'] = 'Published'; 304 316 $this->make_ajax_call( 'customize_save' ); 305 317 $this->assertTrue( $this->_last_response_parsed['success'] ); … … 320 332 */ 321 333 function test_success_save_post_date() { 322 $uuid = wp_generate_uuid4(); 323 $post_id = $this->factory()->post->create( array( 324 'post_name' => $uuid, 325 'post_title' => 'Original', 326 'post_type' => 'customize_changeset', 327 'post_status' => 'auto-draft', 328 'post_content' => wp_json_encode( array( 329 'blogname' => array( 330 'value' => 'New Site Title', 334 $uuid = wp_generate_uuid4(); 335 $post_id = $this->factory()->post->create( 336 array( 337 'post_name' => $uuid, 338 'post_title' => 'Original', 339 'post_type' => 'customize_changeset', 340 'post_status' => 'auto-draft', 341 'post_content' => wp_json_encode( 342 array( 343 'blogname' => array( 344 'value' => 'New Site Title', 345 ), 346 ) 331 347 ), 332 ) ),333 ) );348 ) 349 ); 334 350 $wp_customize = $this->set_up_valid_state( $uuid ); 335 351 336 352 // Success future schedule date. 337 $future_date = ( gmdate( 'Y' ) + 1 ) . '-01-01 00:00:00';353 $future_date = ( gmdate( 'Y' ) + 1 ) . '-01-01 00:00:00'; 338 354 $_POST['customize_changeset_status'] = 'future'; 339 $_POST['customize_changeset_title'] = 'Future date';340 $_POST['customize_changeset_date'] = $future_date;355 $_POST['customize_changeset_title'] = 'Future date'; 356 $_POST['customize_changeset_date'] = $future_date; 341 357 $this->make_ajax_call( 'customize_save' ); 342 358 $this->assertTrue( $this->_last_response_parsed['success'] ); … … 363 379 // Success if draft with past date. 364 380 $now = current_time( 'mysql' ); 365 wp_update_post( array( 366 'ID' => $post_id, 367 'post_status' => 'draft', 368 'post_date' => $now, 369 'post_date_gmt' => get_gmt_from_date( $now ), 370 ) ); 381 wp_update_post( 382 array( 383 'ID' => $post_id, 384 'post_status' => 'draft', 385 'post_date' => $now, 386 'post_date_gmt' => get_gmt_from_date( $now ), 387 ) 388 ); 371 389 372 390 // Fail if future request and existing date is past. … … 378 396 379 397 // Success publish changeset reset date to current. 380 wp_update_post( array( 381 'ID' => $post_id, 382 'post_status' => 'future', 383 'post_date' => $future_date, 384 'post_date_gmt' => get_gmt_from_date( $future_date ), 385 ) ); 398 wp_update_post( 399 array( 400 'ID' => $post_id, 401 'post_status' => 'future', 402 'post_date' => $future_date, 403 'post_date_gmt' => get_gmt_from_date( $future_date ), 404 ) 405 ); 386 406 unset( $_POST['customize_changeset_date'] ); 387 407 $_POST['customize_changeset_status'] = 'publish'; … … 412 432 $uuid = wp_generate_uuid4(); 413 433 414 $post_id = $this->factory()->post->create( array( 415 'post_name' => $uuid, 416 'post_type' => 'customize_changeset', 417 'post_status' => 'draft', 418 'post_content' => wp_json_encode( array( 434 $post_id = $this->factory()->post->create( 435 array( 436 'post_name' => $uuid, 437 'post_type' => 'customize_changeset', 438 'post_status' => 'draft', 439 'post_content' => wp_json_encode( 440 array( 441 'blogname' => array( 442 'value' => 'New Site Title', 443 ), 444 ) 445 ), 446 ) 447 ); 448 $this->set_up_valid_state( $uuid ); 449 450 $this->assertFalse( wp_get_post_autosave( $post_id ) ); 451 452 $_POST['customize_changeset_data'] = wp_json_encode( 453 array( 419 454 'blogname' => array( 420 'value' => ' NewSite Title',455 'value' => 'Autosaved Site Title', 421 456 ), 422 ) ), 423 ) ); 424 $this->set_up_valid_state( $uuid ); 425 426 $this->assertFalse( wp_get_post_autosave( $post_id ) ); 427 428 $_POST['customize_changeset_data'] = wp_json_encode( array( 429 'blogname' => array( 430 'value' => 'Autosaved Site Title', 431 ), 432 ) ); 457 ) 458 ); 433 459 434 460 $_POST['customize_changeset_autosave'] = 'on'; … … 450 476 */ 451 477 public function test_handle_changeset_trash_request() { 452 $uuid = wp_generate_uuid4();478 $uuid = wp_generate_uuid4(); 453 479 $wp_customize = $this->set_up_valid_state( $uuid ); 454 480 … … 457 483 $this->assertEquals( 'invalid_nonce', $this->_last_response_parsed['data']['code'] ); 458 484 459 $nonce = wp_create_nonce( 'trash_customize_changeset' );485 $nonce = wp_create_nonce( 'trash_customize_changeset' ); 460 486 $_POST['nonce'] = $_GET['nonce'] = $_REQUEST['nonce'] = $nonce; 461 487 $this->make_ajax_call( 'customize_trash' ); … … 465 491 $wp_customize->register_controls(); // And settings too. 466 492 $wp_customize->set_post_value( 'blogname', 'HELLO' ); 467 $wp_customize->save_changeset_post( array( 468 'status' => 'save', 469 ) ); 493 $wp_customize->save_changeset_post( 494 array( 495 'status' => 'save', 496 ) 497 ); 470 498 471 499 add_filter( 'map_meta_cap', array( $this, 'return_do_not_allow' ) ); … … 475 503 remove_filter( 'map_meta_cap', array( $this, 'return_do_not_allow' ) ); 476 504 477 wp_update_post( array( 478 'ID' => $wp_customize->changeset_post_id(), 479 'post_status' => 'trash', 480 ) ); 505 wp_update_post( 506 array( 507 'ID' => $wp_customize->changeset_post_id(), 508 'post_status' => 'trash', 509 ) 510 ); 481 511 $this->make_ajax_call( 'customize_trash' ); 482 512 $this->assertFalse( $this->_last_response_parsed['success'] ); 483 513 $this->assertEquals( 'changeset_already_trashed', $this->_last_response_parsed['data']['code'] ); 484 514 485 wp_update_post( array( 486 'ID' => $wp_customize->changeset_post_id(), 487 'post_status' => 'draft', 488 ) ); 515 wp_update_post( 516 array( 517 'ID' => $wp_customize->changeset_post_id(), 518 'post_status' => 'draft', 519 ) 520 ); 489 521 490 522 $wp_trash_post_count = did_action( 'wp_trash_post' ); … … 521 553 */ 522 554 public function test_handle_dismiss_autosave_or_lock_request() { 523 $uuid = wp_generate_uuid4();555 $uuid = wp_generate_uuid4(); 524 556 $wp_customize = $this->set_up_valid_state( $uuid ); 525 557 … … 528 560 $this->assertEquals( 'invalid_nonce', $this->_last_response_parsed['data'] ); 529 561 530 $nonce = wp_create_nonce( 'customize_dismiss_autosave_or_lock' );562 $nonce = wp_create_nonce( 'customize_dismiss_autosave_or_lock' ); 531 563 $_POST['nonce'] = $_GET['nonce'] = $_REQUEST['nonce'] = $nonce; 532 564 … … 546 578 $user_auto_draft_ids = array(); 547 579 for ( $i = 0; $i < 3; $i++ ) { 548 $user_auto_draft_ids[] = $this->factory()->post->create( array( 549 'post_name' => wp_generate_uuid4(), 550 'post_type' => 'customize_changeset', 551 'post_status' => 'auto-draft', 552 'post_author' => self::$admin_user_id, 553 'post_content' => wp_json_encode( array() ), 554 ) ); 580 $user_auto_draft_ids[] = $this->factory()->post->create( 581 array( 582 'post_name' => wp_generate_uuid4(), 583 'post_type' => 'customize_changeset', 584 'post_status' => 'auto-draft', 585 'post_author' => self::$admin_user_id, 586 'post_content' => wp_json_encode( array() ), 587 ) 588 ); 555 589 } 556 590 $other_user_auto_draft_ids = array(); 557 591 for ( $i = 0; $i < 3; $i++ ) { 558 $other_user_auto_draft_ids[] = $this->factory()->post->create( array( 559 'post_name' => wp_generate_uuid4(), 560 'post_type' => 'customize_changeset', 561 'post_status' => 'auto-draft', 562 'post_author' => $other_user_id, 563 'post_content' => wp_json_encode( array() ), 564 ) ); 592 $other_user_auto_draft_ids[] = $this->factory()->post->create( 593 array( 594 'post_name' => wp_generate_uuid4(), 595 'post_type' => 'customize_changeset', 596 'post_status' => 'auto-draft', 597 'post_author' => $other_user_id, 598 'post_content' => wp_json_encode( array() ), 599 ) 600 ); 565 601 } 566 602 foreach ( array_merge( $user_auto_draft_ids, $other_user_auto_draft_ids ) as $post_id ) { … … 585 621 586 622 // Save a changeset as a draft. 587 $r = $wp_customize->save_changeset_post( array( 588 'data' => array( 589 'blogname' => array( 590 'value' => 'Foo', 623 $r = $wp_customize->save_changeset_post( 624 array( 625 'data' => array( 626 'blogname' => array( 627 'value' => 'Foo', 628 ), 591 629 ), 592 ),593 'status' => 'draft',594 ) );630 'status' => 'draft', 631 ) 632 ); 595 633 596 634 $_POST['dismiss_autosave'] = $_GET['dismiss_autosave'] = $_REQUEST['dismiss_autosave'] = false; … … 610 648 611 649 // Add the autosave revision. 612 $r = $wp_customize->save_changeset_post( array( 613 'data' => array( 614 'blogname' => array( 615 'value' => 'Bar', 650 $r = $wp_customize->save_changeset_post( 651 array( 652 'data' => array( 653 'blogname' => array( 654 'value' => 'Bar', 655 ), 616 656 ), 617 ),618 'autosave' => true,619 ) );657 'autosave' => true, 658 ) 659 ); 620 660 $this->assertNotInstanceOf( 'WP_Error', $r ); 621 661 $autosave_revision = wp_get_post_autosave( $wp_customize->changeset_post_id() );
Note: See TracChangeset
for help on using the changeset viewer.