1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Tests for the WP_Customize_Widgets class. |
5
|
|
|
* |
6
|
|
|
* @group customize |
7
|
|
|
*/ |
8
|
|
|
class Tests_WP_Customize_Widgets extends WP_UnitTestCase { |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @var WP_Customize_Manager |
12
|
|
|
*/ |
13
|
|
|
protected $manager; |
14
|
|
|
|
15
|
|
|
function setUp() { |
|
|
|
|
16
|
|
|
parent::setUp(); |
17
|
|
|
require_once( WP_FIELDS_API_DIR . 'implementation/wp-includes/class-wp-customize-manager.php' ); |
18
|
|
|
//require_once( ABSPATH . WPINC . '/class-wp-customize-manager.php' ); |
19
|
|
|
$GLOBALS['wp_customize'] = new WP_Customize_Manager(); |
20
|
|
|
$this->manager = $GLOBALS['wp_customize']; |
21
|
|
|
|
22
|
|
|
unset( $GLOBALS['_wp_sidebars_widgets'] ); // clear out cache set by wp_get_sidebars_widgets() |
23
|
|
|
$sidebars_widgets = wp_get_sidebars_widgets(); |
24
|
|
|
$this->assertEqualSets( array( 'wp_inactive_widgets', 'sidebar-1' ), array_keys( wp_get_sidebars_widgets() ) ); |
25
|
|
|
$this->assertContains( 'search-2', $sidebars_widgets['sidebar-1'] ); |
26
|
|
|
$this->assertContains( 'categories-2', $sidebars_widgets['sidebar-1'] ); |
27
|
|
|
$this->assertArrayHasKey( 2, get_option( 'widget_search' ) ); |
28
|
|
|
$widget_categories = get_option( 'widget_categories' ); |
29
|
|
|
$this->assertArrayHasKey( 2, $widget_categories ); |
30
|
|
|
$this->assertEquals( '', $widget_categories[2]['title'] ); |
31
|
|
|
|
32
|
|
|
remove_action( 'after_setup_theme', 'twentyfifteen_setup' ); // @todo We should not be including a theme anyway |
33
|
|
|
|
34
|
|
|
$user_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); |
35
|
|
|
wp_set_current_user( $user_id ); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
function tearDown() { |
|
|
|
|
39
|
|
|
$this->manager = null; |
40
|
|
|
unset( $GLOBALS['wp_customize'] ); |
41
|
|
|
unset( $GLOBALS['wp_scripts'] ); |
42
|
|
|
parent::tearDown(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
function set_customized_post_data( $customized ) { |
|
|
|
|
46
|
|
|
$_POST['customized'] = wp_slash( wp_json_encode( $customized ) ); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
function do_customize_boot_actions() { |
|
|
|
|
50
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST'; |
51
|
|
|
do_action( 'setup_theme' ); |
52
|
|
|
$_REQUEST['nonce'] = wp_create_nonce( 'preview-customize_' . $this->manager->theme()->get_stylesheet() ); |
53
|
|
|
do_action( 'after_setup_theme' ); |
54
|
|
|
do_action( 'init' ); |
55
|
|
|
do_action( 'wp_loaded' ); |
56
|
|
|
do_action( 'wp', $GLOBALS['wp'] ); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Test WP_Customize_Widgets::__construct() |
61
|
|
|
*/ |
62
|
|
|
function test_construct() { |
|
|
|
|
63
|
|
|
$this->assertInstanceOf( 'WP_Customize_Widgets', $this->manager->widgets ); |
64
|
|
|
$this->assertEquals( $this->manager, $this->manager->widgets->manager ); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Test WP_Customize_Widgets::register_settings() |
69
|
|
|
* |
70
|
|
|
* @ticket 30988 |
71
|
|
|
*/ |
72
|
|
|
function test_register_settings() { |
|
|
|
|
73
|
|
|
|
74
|
|
|
$raw_widget_customized = array( |
75
|
|
|
'widget_categories[2]' => array( |
76
|
|
|
'title' => 'Taxonomies Brand New Value', |
77
|
|
|
'count' => 0, |
78
|
|
|
'hierarchical' => 0, |
79
|
|
|
'dropdown' => 0, |
80
|
|
|
), |
81
|
|
|
'widget_search[3]' => array( |
82
|
|
|
'title' => 'Not as good as Google!', |
83
|
|
|
), |
84
|
|
|
); |
85
|
|
|
$customized = array(); |
86
|
|
|
foreach ( $raw_widget_customized as $setting_id => $instance ) { |
87
|
|
|
$customized[ $setting_id ] = $this->manager->widgets->sanitize_widget_js_instance( $instance ); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this->set_customized_post_data( $customized ); |
91
|
|
|
$this->do_customize_boot_actions(); |
92
|
|
|
$this->assertTrue( is_customize_preview() ); |
93
|
|
|
|
94
|
|
|
$this->assertNotEmpty( $this->manager->get_setting( 'widget_categories[2]' ), 'Expected setting for pre-existing widget category-2, being customized.' ); |
95
|
|
|
$this->assertNotEmpty( $this->manager->get_setting( 'widget_search[2]' ), 'Expected setting for pre-existing widget search-2, not being customized.' ); |
96
|
|
|
$this->assertNotEmpty( $this->manager->get_setting( 'widget_search[3]' ), 'Expected dynamic setting for non-existing widget search-3, being customized.' ); |
97
|
|
|
|
98
|
|
|
$widget_categories = get_option( 'widget_categories' ); |
99
|
|
|
$this->assertEquals( $raw_widget_customized['widget_categories[2]'], $widget_categories[2], 'Expected $wp_customize->get_setting(widget_categories[2])->preview() to have been called.' ); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Test WP_Customize_Widgets::get_setting_args() |
104
|
|
|
*/ |
105
|
|
|
function test_get_setting_args() { |
|
|
|
|
106
|
|
|
|
107
|
|
|
add_filter( 'widget_customizer_setting_args', array( $this, 'filter_widget_customizer_setting_args' ), 10, 2 ); |
108
|
|
|
|
109
|
|
|
$default_args = array( |
110
|
|
|
'type' => 'option', |
111
|
|
|
'capability' => 'edit_theme_options', |
112
|
|
|
'transport' => 'refresh', |
113
|
|
|
'default' => array(), |
114
|
|
|
'sanitize_callback' => array( $this->manager->widgets, 'sanitize_widget_instance' ), |
115
|
|
|
'sanitize_js_callback' => array( $this->manager->widgets, 'sanitize_widget_js_instance' ), |
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
$args = $this->manager->widgets->get_setting_args( 'widget_foo[2]' ); |
119
|
|
|
foreach ( $default_args as $key => $default_value ) { |
120
|
|
|
$this->assertEquals( $default_value, $args[ $key ] ); |
121
|
|
|
} |
122
|
|
|
$this->assertEquals( 'WIDGET_FOO[2]', $args['uppercase_id_set_by_filter'] ); |
123
|
|
|
|
124
|
|
|
$override_args = array( |
125
|
|
|
'type' => 'theme_mod', |
126
|
|
|
'capability' => 'edit_posts', |
127
|
|
|
'transport' => 'postMessage', |
128
|
|
|
'default' => array( 'title' => 'asd' ), |
129
|
|
|
'sanitize_callback' => '__return_empty_array', |
130
|
|
|
'sanitize_js_callback' => '__return_empty_array', |
131
|
|
|
); |
132
|
|
|
$args = $this->manager->widgets->get_setting_args( 'widget_bar[3]', $override_args ); |
133
|
|
|
foreach ( $override_args as $key => $override_value ) { |
134
|
|
|
$this->assertEquals( $override_value, $args[ $key ] ); |
135
|
|
|
} |
136
|
|
|
$this->assertEquals( 'WIDGET_BAR[3]', $args['uppercase_id_set_by_filter'] ); |
137
|
|
|
|
138
|
|
|
$default_args = array( |
139
|
|
|
'type' => 'option', |
140
|
|
|
'capability' => 'edit_theme_options', |
141
|
|
|
'transport' => 'refresh', |
142
|
|
|
'default' => array(), |
143
|
|
|
'sanitize_callback' => array( $this->manager->widgets, 'sanitize_sidebar_widgets' ), |
144
|
|
|
'sanitize_js_callback' => array( $this->manager->widgets, 'sanitize_sidebar_widgets_js_instance' ), |
145
|
|
|
); |
146
|
|
|
$args = $this->manager->widgets->get_setting_args( 'sidebars_widgets[sidebar-1]' ); |
147
|
|
|
foreach ( $default_args as $key => $default_value ) { |
148
|
|
|
$this->assertEquals( $default_value, $args[ $key ] ); |
149
|
|
|
} |
150
|
|
|
$this->assertEquals( 'SIDEBARS_WIDGETS[SIDEBAR-1]', $args['uppercase_id_set_by_filter'] ); |
151
|
|
|
|
152
|
|
|
$override_args = array( |
153
|
|
|
'type' => 'theme_mod', |
154
|
|
|
'capability' => 'edit_posts', |
155
|
|
|
'transport' => 'postMessage', |
156
|
|
|
'default' => array( 'title' => 'asd' ), |
157
|
|
|
'sanitize_callback' => '__return_empty_array', |
158
|
|
|
'sanitize_js_callback' => '__return_empty_array', |
159
|
|
|
); |
160
|
|
|
$args = $this->manager->widgets->get_setting_args( 'sidebars_widgets[sidebar-2]', $override_args ); |
161
|
|
|
foreach ( $override_args as $key => $override_value ) { |
162
|
|
|
$this->assertEquals( $override_value, $args[ $key ] ); |
163
|
|
|
} |
164
|
|
|
$this->assertEquals( 'SIDEBARS_WIDGETS[SIDEBAR-2]', $args['uppercase_id_set_by_filter'] ); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
function filter_widget_customizer_setting_args( $args, $id ) { |
|
|
|
|
168
|
|
|
$args['uppercase_id_set_by_filter'] = strtoupper( $id ); |
169
|
|
|
return $args; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Test WP_Customize_Widgets::sanitize_widget_js_instance() and WP_Customize_Widgets::sanitize_widget_instance() |
174
|
|
|
*/ |
175
|
|
|
function test_sanitize_widget_js_instance() { |
|
|
|
|
176
|
|
|
$this->do_customize_boot_actions(); |
177
|
|
|
|
178
|
|
|
$new_categories_instance = array( |
179
|
|
|
'title' => 'Taxonomies Brand New Value', |
180
|
|
|
'count' => '1', |
181
|
|
|
'hierarchical' => '1', |
182
|
|
|
'dropdown' => '1', |
183
|
|
|
); |
184
|
|
|
|
185
|
|
|
$sanitized_for_js = $this->manager->widgets->sanitize_widget_js_instance( $new_categories_instance ); |
186
|
|
|
$this->assertArrayHasKey( 'encoded_serialized_instance', $sanitized_for_js ); |
187
|
|
|
$this->assertTrue( is_serialized( base64_decode( $sanitized_for_js['encoded_serialized_instance'] ), true ) ); |
188
|
|
|
$this->assertEquals( $new_categories_instance['title'], $sanitized_for_js['title'] ); |
189
|
|
|
$this->assertTrue( $sanitized_for_js['is_widget_customizer_js_value'] ); |
190
|
|
|
$this->assertArrayHasKey( 'instance_hash_key', $sanitized_for_js ); |
191
|
|
|
|
192
|
|
|
$corrupted_sanitized_for_js = $sanitized_for_js; |
193
|
|
|
$corrupted_sanitized_for_js['encoded_serialized_instance'] = base64_encode( serialize( array( 'title' => 'EVIL' ) ) ); |
194
|
|
|
$this->assertNull( $this->manager->widgets->sanitize_widget_instance( $corrupted_sanitized_for_js ), 'Expected sanitize_widget_instance to reject corrupted data.' ); |
195
|
|
|
|
196
|
|
|
$unsanitized_from_js = $this->manager->widgets->sanitize_widget_instance( $sanitized_for_js ); |
197
|
|
|
$this->assertEquals( $unsanitized_from_js, $new_categories_instance ); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.