Changeset 42343 for trunk/tests/phpunit/tests/customize/setting.php
- Timestamp:
- 11/30/2017 11:09:33 PM (8 years ago)
- File:
-
- 1 edited
-
trunk/tests/phpunit/tests/customize/setting.php (modified) (29 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/customize/setting.php
r40564 r42343 22 22 require_once( ABSPATH . WPINC . '/class-wp-customize-manager.php' ); 23 23 $GLOBALS['wp_customize'] = new WP_Customize_Manager(); 24 $this->manager = $GLOBALS['wp_customize'];25 $this->undefined = new stdClass();24 $this->manager = $GLOBALS['wp_customize']; 25 $this->undefined = new stdClass(); 26 26 } 27 27 … … 90 90 91 91 function test_constructor_with_args() { 92 $args = array(93 'type' => 'option',94 'capability' => 'edit_posts',95 'theme_supports' => 'widgets',96 'default' => 'barbar',97 'transport' => 'postMessage',98 'validate_callback' => array( $this, 'validate_callback_for_tests' ),99 'sanitize_callback' => array( $this, 'sanitize_callback_for_tests' ),92 $args = array( 93 'type' => 'option', 94 'capability' => 'edit_posts', 95 'theme_supports' => 'widgets', 96 'default' => 'barbar', 97 'transport' => 'postMessage', 98 'validate_callback' => array( $this, 'validate_callback_for_tests' ), 99 'sanitize_callback' => array( $this, 'sanitize_callback_for_tests' ), 100 100 'sanitize_js_callback' => array( $this, 'sanitize_js_callback_for_tests' ), 101 101 ); … … 111 111 112 112 public $post_data_overrides = array( 113 'unset_option_overridden' => 'unset_option_post_override_value\\o/',114 'unset_theme_mod_overridden' => 'unset_theme_mod_post_override_value\\o/',115 'set_option_overridden' => 'set_option_post_override_value\\o/',116 'set_theme_mod_overridden' => 'set_theme_mod_post_override_value\\o/',117 'unset_option_multi_overridden[foo]' => 'unset_option_multi_overridden[foo]_post_override_value\\o/',113 'unset_option_overridden' => 'unset_option_post_override_value\\o/', 114 'unset_theme_mod_overridden' => 'unset_theme_mod_post_override_value\\o/', 115 'set_option_overridden' => 'set_option_post_override_value\\o/', 116 'set_theme_mod_overridden' => 'set_theme_mod_post_override_value\\o/', 117 'unset_option_multi_overridden[foo]' => 'unset_option_multi_overridden[foo]_post_override_value\\o/', 118 118 'unset_theme_mod_multi_overridden[foo]' => 'unset_theme_mod_multi_overridden[foo]_post_override_value\\o/', 119 'set_option_multi_overridden[foo]' => 'set_option_multi_overridden[foo]_post_override_value\\o/',120 'set_theme_mod_multi_overridden[foo]' => 'set_theme_mod_multi_overridden[foo]_post_override_value\\o/',119 'set_option_multi_overridden[foo]' => 'set_option_multi_overridden[foo]_post_override_value\\o/', 120 'set_theme_mod_multi_overridden[foo]' => 'set_theme_mod_multi_overridden[foo]_post_override_value\\o/', 121 121 ); 122 122 123 123 public $standard_type_configs = array( 124 'option' => array(124 'option' => array( 125 125 'getter' => 'get_option', 126 126 'setter' => 'update_option', … … 144 144 foreach ( $this->standard_type_configs as $type => $type_options ) { 145 145 // Non-multidimensional: See what effect the preview filter has on a non-existent setting (default value should be seen). 146 $name = "unset_{$type}_without_post_value";146 $name = "unset_{$type}_without_post_value"; 147 147 $default = "default_value_{$name}"; 148 148 $setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) ); … … 154 154 155 155 // Non-multidimensional: See what effect the preview has on an extant setting (default value should not be seen). 156 $name = "set_{$type}_without_post_value";157 $default = "default_value_{$name}";156 $name = "set_{$type}_without_post_value"; 157 $default = "default_value_{$name}"; 158 158 $initial_value = "initial_value_{$name}"; 159 159 call_user_func( $type_options['setter'], $name, $initial_value ); … … 185 185 186 186 // Non-multidimensional: Test unset setting being overridden by a post value. 187 $name = "unset_{$type}_overridden";187 $name = "unset_{$type}_overridden"; 188 188 $default = "default_value_{$name}"; 189 189 $setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) ); … … 195 195 196 196 // Non-multidimensional: Test set setting being overridden by a post value. 197 $name = "set_{$type}_overridden";198 $default = "default_value_{$name}";197 $name = "set_{$type}_overridden"; 198 $default = "default_value_{$name}"; 199 199 $initial_value = "initial_value_{$name}"; 200 200 call_user_func( $type_options['setter'], $name, $initial_value ); … … 223 223 // Multidimensional: See what effect the preview filter has on a non-existent setting (default value should be seen). 224 224 $base_name = "unset_{$type}_multi"; 225 $name = $base_name . '[foo]';226 $default = "default_value_{$name}";227 $setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) );225 $name = $base_name . '[foo]'; 226 $default = "default_value_{$name}"; 227 $setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) ); 228 228 $this->assertEquals( $this->undefined, call_user_func( $type_options['getter'], $base_name, $this->undefined ) ); 229 229 $this->assertEquals( $default, $setting->value() ); … … 234 234 235 235 // Multidimensional: See what effect the preview has on an extant setting (default value should not be seen) without post value. 236 $base_name = "set_{$type}_multi"; 237 $name = $base_name . '[foo]'; 238 $default = "default_value_{$name}"; 239 $initial_value = "initial_value_{$name}"; 240 $base_initial_value = array( 'foo' => $initial_value, 'bar' => 'persisted' ); 236 $base_name = "set_{$type}_multi"; 237 $name = $base_name . '[foo]'; 238 $default = "default_value_{$name}"; 239 $initial_value = "initial_value_{$name}"; 240 $base_initial_value = array( 241 'foo' => $initial_value, 242 'bar' => 'persisted', 243 ); 241 244 call_user_func( $type_options['setter'], $base_name, $base_initial_value ); 242 $setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) );245 $setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) ); 243 246 $base_value = call_user_func( $type_options['getter'], $base_name, array() ); 244 247 $this->assertEquals( $initial_value, $base_value['foo'] ); … … 260 263 // Multidimensional: Test unset setting being overridden by a post value. 261 264 $base_name = "unset_{$type}_multi_overridden"; 262 $name = $base_name . '[foo]';263 $default = "default_value_{$name}";264 $setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) );265 $name = $base_name . '[foo]'; 266 $default = "default_value_{$name}"; 267 $setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) ); 265 268 $this->assertEquals( $this->undefined, call_user_func( $type_options['getter'], $base_name, $this->undefined ) ); 266 269 $this->assertEquals( $default, $setting->value() ); … … 273 276 274 277 // Multidimensional: Test set setting being overridden by a post value. 275 $base_name = "set_{$type}_multi_overridden"; 276 $name = $base_name . '[foo]'; 277 $default = "default_value_{$name}"; 278 $initial_value = "initial_value_{$name}"; 279 $base_initial_value = array( 'foo' => $initial_value, 'bar' => 'persisted' ); 278 $base_name = "set_{$type}_multi_overridden"; 279 $name = $base_name . '[foo]'; 280 $default = "default_value_{$name}"; 281 $initial_value = "initial_value_{$name}"; 282 $base_initial_value = array( 283 'foo' => $initial_value, 284 'bar' => 'persisted', 285 ); 280 286 call_user_func( $type_options['setter'], $base_name, $base_initial_value ); 281 $setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) );287 $setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) ); 282 288 $base_value = call_user_func( $type_options['getter'], $base_name, $this->undefined ); 283 289 $this->assertArrayHasKey( 'foo', $base_value ); … … 314 320 if ( did_action( "customize_preview_{$name}" ) && array_key_exists( $name, $this->custom_type_data_previewed ) ) { 315 321 $value = $this->custom_type_data_previewed[ $name ]; 316 } else if ( array_key_exists( $name, $this->custom_type_data_saved ) ) {322 } elseif ( array_key_exists( $name, $this->custom_type_data_saved ) ) { 317 323 $value = $this->custom_type_data_saved[ $name ]; 318 324 } else { … … 358 364 function test_preview_custom_type() { 359 365 wp_set_current_user( $this->factory()->user->create( array( 'role' => 'administrator' ) ) ); 360 $type = 'custom_type';366 $type = 'custom_type'; 361 367 $post_data_overrides = array( 362 368 "unset_{$type}_with_post_value" => "unset_{$type}_without_post_value\\o/", 363 "set_{$type}_with_post_value" => "set_{$type}_without_post_value\\o/",369 "set_{$type}_with_post_value" => "set_{$type}_without_post_value\\o/", 364 370 ); 365 371 $_POST['customized'] = wp_slash( wp_json_encode( $post_data_overrides ) ); 366 372 367 $this->custom_type_data_saved = array();373 $this->custom_type_data_saved = array(); 368 374 $this->custom_type_data_previewed = array(); 369 375 … … 371 377 372 378 // Custom type not existing and no post value override. 373 $name = "unset_{$type}_without_post_value";379 $name = "unset_{$type}_without_post_value"; 374 380 $default = "default_value_{$name}"; 375 381 $setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) ); … … 385 391 386 392 // Custom type existing and no post value override. 387 $name = "set_{$type}_without_post_value";388 $default = "default_value_{$name}";393 $name = "set_{$type}_without_post_value"; 394 $default = "default_value_{$name}"; 389 395 $initial_value = "initial_value_{$name}"; 390 396 $this->custom_type_setter( $name, $initial_value ); … … 407 413 408 414 // Custom type not existing and with a post value override. 409 $name = "unset_{$type}_with_post_value";415 $name = "unset_{$type}_with_post_value"; 410 416 $default = "default_value_{$name}"; 411 417 $setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) ); … … 421 427 422 428 // Custom type not existing and with a post value override. 423 $name = "set_{$type}_with_post_value";424 $default = "default_value_{$name}";429 $name = "set_{$type}_with_post_value"; 430 $default = "default_value_{$name}"; 425 431 $initial_value = "initial_value_{$name}"; 426 432 $this->custom_type_setter( $name, $initial_value ); … … 438 444 // Custom type that does not handle supplying the post value from the customize_value_{$id_base} filter. 439 445 $setting_id = 'custom_without_previewing_value_filter'; 440 $setting = $this->manager->add_setting( $setting_id, array( 441 'type' => 'custom_preview_test', 442 'default' => 123, 443 'sanitize_callback' => array( $this->manager->nav_menus, 'intval_base10' ), 444 ) ); 446 $setting = $this->manager->add_setting( 447 $setting_id, array( 448 'type' => 'custom_preview_test', 449 'default' => 123, 450 'sanitize_callback' => array( $this->manager->nav_menus, 'intval_base10' ), 451 ) 452 ); 445 453 446 454 /* … … 466 474 */ 467 475 function test_non_posted_setting_applying_default_value_in_preview() { 468 $type = 'option';469 $name = 'unset_option_without_post_value';476 $type = 'option'; 477 $name = 'unset_option_without_post_value'; 470 478 $default = "default_value_{$name}"; 471 479 $setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) ); … … 484 492 */ 485 493 function test_update_custom_type() { 486 $type = 'custom';487 $name = 'foo';494 $type = 'custom'; 495 $name = 'foo'; 488 496 $setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type' ) ); 489 497 $this->manager->add_setting( $setting ); … … 544 552 function test_is_current_blog_previewed() { 545 553 wp_set_current_user( $this->factory()->user->create( array( 'role' => 'administrator' ) ) ); 546 $type = 'option';547 $name = 'blogname';554 $type = 'option'; 555 $name = 'blogname'; 548 556 $post_value = __FUNCTION__; 549 557 $this->manager->set_post_value( $name, $post_value ); … … 566 574 function test_previewing_with_switch_to_blog() { 567 575 wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) ); 568 $type = 'option';569 $name = 'blogdescription';576 $type = 'option'; 577 $name = 'blogdescription'; 570 578 $post_value = __FUNCTION__; 571 579 $this->manager->set_post_value( $name, $post_value ); … … 590 598 wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) ); 591 599 592 $name = 'autoloaded1'; 593 $setting = new WP_Customize_Setting( $this->manager, $name, array( 594 'type' => 'option', 595 ) ); 596 $value = 'value1'; 600 $name = 'autoloaded1'; 601 $setting = new WP_Customize_Setting( 602 $this->manager, $name, array( 603 'type' => 'option', 604 ) 605 ); 606 $value = 'value1'; 597 607 $this->manager->set_post_value( $setting->id, $value ); 598 608 $setting->save(); … … 601 611 $this->assertEquals( $value, get_option( $name ) ); 602 612 603 $name = 'autoloaded2'; 604 $setting = new WP_Customize_Setting( $this->manager, $name, array( 605 'type' => 'option', 606 'autoload' => true, 607 ) ); 608 $value = 'value2'; 613 $name = 'autoloaded2'; 614 $setting = new WP_Customize_Setting( 615 $this->manager, $name, array( 616 'type' => 'option', 617 'autoload' => true, 618 ) 619 ); 620 $value = 'value2'; 609 621 $this->manager->set_post_value( $setting->id, $value ); 610 622 $setting->save(); … … 613 625 $this->assertEquals( $value, get_option( $name ) ); 614 626 615 $name = 'not-autoloaded1'; 616 $setting = new WP_Customize_Setting( $this->manager, $name, array( 617 'type' => 'option', 618 'autoload' => false, 619 ) ); 620 $value = 'value3'; 627 $name = 'not-autoloaded1'; 628 $setting = new WP_Customize_Setting( 629 $this->manager, $name, array( 630 'type' => 'option', 631 'autoload' => false, 632 ) 633 ); 634 $value = 'value3'; 621 635 $this->manager->set_post_value( $setting->id, $value ); 622 636 $setting->save(); … … 625 639 $this->assertEquals( $value, get_option( $name ) ); 626 640 627 $id_base = 'multi-not-autoloaded'; 628 $setting1 = new WP_Customize_Setting( $this->manager, $id_base . '[foo]', array( 629 'type' => 'option', 630 ) ); 631 $setting2 = new WP_Customize_Setting( $this->manager, $id_base . '[bar]', array( 632 'type' => 'option', 633 'autoload' => false, 634 ) ); 641 $id_base = 'multi-not-autoloaded'; 642 $setting1 = new WP_Customize_Setting( 643 $this->manager, $id_base . '[foo]', array( 644 'type' => 'option', 645 ) 646 ); 647 $setting2 = new WP_Customize_Setting( 648 $this->manager, $id_base . '[bar]', array( 649 'type' => 'option', 650 'autoload' => false, 651 ) 652 ); 635 653 $this->manager->set_post_value( $setting1->id, 'value1' ); 636 654 $this->manager->set_post_value( $setting2->id, 'value2' ); … … 648 666 public function test_js_value() { 649 667 $default = "\x00"; 650 $args = array(651 'type' => 'binary',652 'default' => $default,653 'transport' => 'postMessage',654 'dirty' => true,668 $args = array( 669 'type' => 'binary', 670 'default' => $default, 671 'transport' => 'postMessage', 672 'dirty' => true, 655 673 'sanitize_js_callback' => array( $this, 'sanitize_js_callback_base64_for_testing' ), 656 674 ); … … 677 695 */ 678 696 public function test_validate() { 679 $setting = new WP_Customize_Setting( $this->manager, 'name', array( 680 'type' => 'key', 681 'validate_callback' => array( $this, 'filter_validate_for_test_validate' ), 682 ) ); 697 $setting = new WP_Customize_Setting( 698 $this->manager, 'name', array( 699 'type' => 'key', 700 'validate_callback' => array( $this, 'filter_validate_for_test_validate' ), 701 ) 702 ); 683 703 $validity = $setting->validate( 'BAD!' ); 684 704 $this->assertInstanceOf( 'WP_Error', $validity ); … … 715 735 716 736 $initial_value = 456; 717 set_theme_mod( 'nav_menu_locations', array( 718 'primary' => $initial_value, 719 ) ); 737 set_theme_mod( 738 'nav_menu_locations', array( 739 'primary' => $initial_value, 740 ) 741 ); 720 742 $setting_id = 'nav_menu_locations[primary]'; 721 743
Note: See TracChangeset
for help on using the changeset viewer.