|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * WP Customize Dynamic Control |
| 4 | + * |
| 5 | + * @package WordPress |
| 6 | + * @subpackage Customize |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * Class WP_Customize_Dynamic_Control |
| 11 | + */ |
| 12 | +class Test_WP_Customize_Dynamic_Control extends WP_UnitTestCase { |
| 13 | + |
| 14 | + /** |
| 15 | + * Customize Manager instance. |
| 16 | + * |
| 17 | + * @var WP_Customize_Manager |
| 18 | + */ |
| 19 | + public $wp_customize; |
| 20 | + |
| 21 | + /** |
| 22 | + * Component. |
| 23 | + * |
| 24 | + * @var WP_Customize_Posts |
| 25 | + */ |
| 26 | + public $posts; |
| 27 | + |
| 28 | + /** |
| 29 | + * Setup. |
| 30 | + * |
| 31 | + * @inheritdoc |
| 32 | + */ |
| 33 | + public function setUp() { |
| 34 | + parent::setUp(); |
| 35 | + require_once( ABSPATH . WPINC . '/class-wp-customize-manager.php' ); |
| 36 | + $GLOBALS['wp_customize'] = new WP_Customize_Manager(); |
| 37 | + $this->wp_customize = $GLOBALS['wp_customize']; |
| 38 | + wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) ); |
| 39 | + $this->posts = new WP_Customize_Posts( $this->wp_customize ); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Teardown. |
| 44 | + * |
| 45 | + * @inheritdoc |
| 46 | + */ |
| 47 | + function tearDown() { |
| 48 | + $this->wp_customize = null; |
| 49 | + $this->posts = null; |
| 50 | + unset( $GLOBALS['wp_customize'] ); |
| 51 | + unset( $GLOBALS['wp_scripts'] ); |
| 52 | + unset( $_REQUEST['nonce'] ); |
| 53 | + unset( $_REQUEST['customize_preview_post_nonce'] ); |
| 54 | + unset( $_REQUEST['wp_customize'] ); |
| 55 | + unset( $_GET['previewed_post'] ); |
| 56 | + parent::tearDown(); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Do Customizer boot actions. |
| 61 | + */ |
| 62 | + function do_customize_boot_actions() { |
| 63 | + // Remove actions that call add_theme_support( 'title-tag' ). |
| 64 | + remove_action( 'after_setup_theme', 'twentyfifteen_setup' ); |
| 65 | + remove_action( 'after_setup_theme', 'twentysixteen_setup' ); |
| 66 | + |
| 67 | + $_SERVER['REQUEST_METHOD'] = 'POST'; |
| 68 | + $_POST['customized'] = ''; |
| 69 | + do_action( 'setup_theme' ); |
| 70 | + $_REQUEST['nonce'] = wp_create_nonce( 'preview-customize_' . $this->wp_customize->theme()->get_stylesheet() ); |
| 71 | + $_REQUEST['customize_preview_post_nonce'] = wp_create_nonce( 'customize_preview_post' ); |
| 72 | + do_action( 'after_setup_theme' ); |
| 73 | + do_action( 'customize_register', $this->wp_customize ); |
| 74 | + $this->wp_customize->customize_preview_init(); |
| 75 | + do_action( 'wp', $GLOBALS['wp'] ); |
| 76 | + $_REQUEST['wp_customize'] = 'on'; |
| 77 | + $_GET['previewed_post'] = 123; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Creates the dynamic control. |
| 82 | + * |
| 83 | + * @param int $setting_id The setting ID. |
| 84 | + */ |
| 85 | + public function control( $setting_id ) { |
| 86 | + $args = array( |
| 87 | + 'label' => 'Heading Text', |
| 88 | + 'section' => $setting_id, |
| 89 | + 'settings' => $setting_id, |
| 90 | + 'priority' => 1, |
| 91 | + 'field_type' => 'text', |
| 92 | + 'setting_property' => 'post_title', |
| 93 | + 'input_attrs' => array( 'data-test' => 'value-test' ), |
| 94 | + ); |
| 95 | + return new WP_Customize_Dynamic_Control( $this->wp_customize, 'dynamic_control_id', $args ); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Filter to register a setting, section, & control. |
| 100 | + */ |
| 101 | + public function customize_register() { |
| 102 | + $setting_id = 'post[post][123]'; |
| 103 | + |
| 104 | + $this->wp_customize->add_setting( $setting_id ); |
| 105 | + |
| 106 | + $setting = new WP_Customize_Post_Section( $this->wp_customize, $setting_id, array( |
| 107 | + 'panel' => 'posts[post]', |
| 108 | + 'post_setting' => $this->wp_customize->get_setting( $setting_id ), |
| 109 | + 'priority' => 1, |
| 110 | + ) ); |
| 111 | + $this->wp_customize->add_section( $setting ); |
| 112 | + |
| 113 | + $control = $this->control( $setting_id ); |
| 114 | + $this->wp_customize->add_control( $control ); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Test export data to JS. |
| 119 | + * |
| 120 | + * @see WP_Customize_Dynamic_Control::json() |
| 121 | + */ |
| 122 | + public function test_json() { |
| 123 | + add_action( 'customize_register', array( $this, 'customize_register' ), 15 ); |
| 124 | + $this->do_customize_boot_actions(); |
| 125 | + |
| 126 | + $control = $this->control( 'post[post][123]' ); |
| 127 | + $json = $control->json(); |
| 128 | + |
| 129 | + $this->assertArrayHasKey( 'input_attrs', $json ); |
| 130 | + $this->assertArrayHasKey( 'field_type', $json ); |
| 131 | + $this->assertArrayHasKey( 'setting_property', $json ); |
| 132 | + $this->assertEquals( array( 'data-test' => 'value-test' ), $json['input_attrs'] ); |
| 133 | + $this->assertEquals( 'text', $json['field_type'] ); |
| 134 | + $this->assertEquals( 'post_title', $json['setting_property'] ); |
| 135 | + } |
| 136 | +} |
0 commit comments