forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidgets.php
More file actions
296 lines (240 loc) · 8.27 KB
/
widgets.php
File metadata and controls
296 lines (240 loc) · 8.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
/**
* Test functions and classes for widgets and sidebars.
*
* @group widgets
*/
class Tests_Widgets extends WP_UnitTestCase {
function clean_up_global_scope() {
global $wp_widget_factory, $wp_registered_sidebars, $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_widget_updates;
$wp_registered_sidebars = array();
$wp_registered_widgets = array();
$wp_registered_widget_controls = array();
$wp_registered_widget_updates = array();
$wp_widget_factory->widgets = array();
parent::clean_up_global_scope();
}
function tearDown() {
global $wp_customize;
$wp_customize = null;
parent::tearDown();
}
/**
* @see register_widget()
* @see unregister_widget()
*/
function test_register_and_unregister_widget_core_widget() {
global $wp_widget_factory;
$widget_class = 'WP_Widget_Search';
register_widget( $widget_class );
$this->assertArrayHasKey( $widget_class, $wp_widget_factory->widgets );
unregister_widget( $widget_class );
$this->assertArrayNotHasKey( $widget_class, $wp_widget_factory->widgets );
}
/**
* @see register_sidebars()
*/
function test_register_sidebars_single() {
global $wp_registered_sidebars;
register_sidebars( 1, array( 'id' => 'wp-unit-test' ) );
$this->assertTrue( isset( $wp_registered_sidebars['wp-unit-test'] ) );
}
/**
* @see register_sidebars()
*/
function test_register_sidebars_multiple() {
global $wp_registered_sidebars;
$result = array();
$num = 3;
$id_base = 'WP Unit Test';
register_sidebars( $num, array( 'name' => $id_base . ' %d' ) );
$names = wp_list_pluck( $wp_registered_sidebars, 'name' );
for ( $i = 1; $i <= $num; $i++ ) {
if ( in_array( "$id_base $i", $names ) ) {
$result[] = true;
}
}
$this->assertEquals( $num, count( $result ) );
}
/**
* @see register_sidebar
* @see unregister_sidebar
*/
function test_register_and_unregister_sidebar() {
global $wp_registered_sidebars;
$sidebar_id = 'wp-unit-test';
register_sidebar( array( 'id' => $sidebar_id ) );
$this->assertArrayHasKey( $sidebar_id, $wp_registered_sidebars );
unregister_sidebar( $sidebar_id );
$this->assertArrayNotHasKey( 'wp-unit-test', $wp_registered_sidebars );
}
/**
* @see WP_Widget_Search::form()
*/
function test_wp_widget_search_form() {
$widget = new WP_Widget_Search( 'foo', 'Foo' );
ob_start();
$args = array(
'before_widget' => '<section>',
'after_widget' => "</section>\n",
'before_title' => '<h2>',
'after_title' => "</h2>\n",
);
$instance = array( 'title' => 'Buscar' );
$widget->_set( 2 );
$widget->widget( $args, $instance );
$output = ob_get_clean();
$this->assertNotContains( 'no-options-widget', $output );
$this->assertContains( '<h2>Buscar</h2>', $output );
$this->assertContains( '<section>', $output );
$this->assertContains( '</section>', $output );
}
/**
* @see WP_Widget::form()
*/
function test_wp_widget_form() {
$widget = new WP_Widget( 'foo', 'Foo' );
ob_start();
$retval = $widget->form( array() );
$output = ob_get_clean();
$this->assertEquals( 'noform', $retval );
$this->assertContains( 'no-options-widget', $output );
}
/**
* @see WP_Widget::__construct()
*/
function test_wp_widget_constructor() {
$id_base = 'foo';
$name = 'Foo';
$foo_widget = new WP_Widget( $id_base, $name );
$this->assertEquals( $id_base, $foo_widget->id_base );
$this->assertEquals( $name, $foo_widget->name );
$this->assertEquals( "widget_{$id_base}", $foo_widget->option_name );
$this->assertArrayHasKey( 'classname', $foo_widget->widget_options );
$this->assertEquals( "widget_{$id_base}", $foo_widget->widget_options['classname'] );
$this->assertArrayHasKey( 'id_base', $foo_widget->control_options );
$this->assertEquals( $id_base, $foo_widget->control_options['id_base'] );
$id_base = 'bar';
$name = 'Bar';
$widget_options = array(
'classname' => 'bar_classname',
);
$control_options = array(
'id_base' => 'bar_id_base',
);
$bar_widget = new WP_Widget( $id_base, $name, $widget_options, $control_options );
$this->assertEquals( $widget_options['classname'], $bar_widget->widget_options['classname'] );
$this->assertEquals( $control_options['id_base'], $bar_widget->control_options['id_base'] );
}
/**
* @see WP_Widget::get_field_name()
*/
function test_wp_widget_get_field_name() {
$widget = new WP_Widget( 'foo', 'Foo' );
$widget->_set( 2 );
$this->assertEquals( 'widget-foo[2][title]', $widget->get_field_name( 'title' ) );
}
/**
* @see WP_Widget::get_field_id()
*/
function test_wp_widget_get_field_id() {
$widget = new WP_Widget( 'foo', 'Foo' );
$widget->_set( 2 );
$this->assertEquals( 'widget-foo-2-title', $widget->get_field_id( 'title' ) );
}
/**
* @see WP_Widget::_register()
*/
function test_wp_widget__register() {
global $wp_registered_widgets;
$settings = get_option( 'widget_search' );
unset( $settings['_multiwidget'] );
$this->assertArrayHasKey( 2, $settings );
$this->assertEmpty( $wp_registered_widgets );
wp_widgets_init();
// Note: We cannot use array_keys() here because $settings could be an ArrayIterator
foreach ( $settings as $widget_number => $instance ) {
$widget_id = "search-$widget_number";
$this->assertArrayHasKey( $widget_id, $wp_registered_widgets );
}
}
// @todo test WP_Widget::display_callback()
/**
* @see WP_Widget::is_preview()
*/
function test_wp_widget_is_preview() {
global $wp_customize;
$widget = new WP_Widget( 'foo', 'Foo' );
$this->assertEmpty( $wp_customize );
$this->assertFalse( $widget->is_preview() );
wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
$wp_customize = new WP_Customize_Manager();
$wp_customize->start_previewing_theme();
$this->assertTrue( $widget->is_preview() );
}
// @todo test WP_Widget::update_callback()
// @todo test WP_Widget::form_callback()
// @todo test WP_Widget::_register_one()
/**
* @see WP_Widget::get_settings()
*/
function test_wp_widget_get_settings() {
global $wp_registered_widgets;
$option_value = get_option( 'widget_search' );
$this->assertArrayHasKey( '_multiwidget', $option_value );
$this->assertEquals( 1, $option_value['_multiwidget'] );
$this->assertArrayHasKey( 2, $option_value );
$instance = $option_value[2];
$this->assertInternalType( 'array', $instance );
$this->assertArrayHasKey( 'title', $instance );
unset( $option_value['_multiwidget'] );
wp_widgets_init();
$wp_widget_search = $wp_registered_widgets['search-2']['callback'][0];
$settings = $wp_widget_search->get_settings();
// @todo $this->assertArrayNotHasKey( '_multiwidget', $settings ); ?
$this->assertArrayHasKey( 2, $settings );
foreach ( $option_value as $widget_number => $instance ) {
$this->assertEquals( $settings[ $widget_number ], $option_value[ $widget_number ] );
}
}
/**
* @see WP_Widget::save_settings()
*/
function test_wp_widget_save_settings() {
global $wp_registered_widgets;
wp_widgets_init();
$wp_widget_search = $wp_registered_widgets['search-2']['callback'][0];
$settings = $wp_widget_search->get_settings();
$overridden_title = 'Unit Tested';
/*
* Note that if a plugin is filtering $settings to be an ArrayIterator,
* then doing this:
* $settings[2]['title'] = $overridden_title;
* Will fail with this:
* > Indirect modification of overloaded element of X has no effect.
* So this is why the value must be obtained.
*/
$instance = $settings[2];
$instance['title'] = $overridden_title;
$settings[2] = $instance;
$wp_widget_search->save_settings( $settings );
$option_value = get_option( $wp_widget_search->option_name );
$this->assertArrayHasKey( '_multiwidget', $option_value );
$this->assertEquals( $overridden_title, $option_value[2]['title'] );
}
/**
* @see WP_Widget::save_settings()
*/
function test_wp_widget_save_settings_delete() {
global $wp_registered_widgets;
wp_widgets_init();
$wp_widget_search = $wp_registered_widgets['search-2']['callback'][0];
$settings = $wp_widget_search->get_settings();
$this->assertArrayHasKey( 2, $settings );
unset( $settings[2] );
$wp_widget_search->save_settings( $settings );
$option_value = get_option( $wp_widget_search->option_name );
$this->assertArrayNotHasKey( 2, $option_value );
}
}