Tests_WP_Customize_Setting::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Tests for the WP_Customize_Setting class.
5
 *
6
 * @group customize
7
 */
8
class Tests_WP_Customize_Setting extends WP_UnitTestCase {
9
10
	/**
11
	 * @var WP_Customize_Manager
12
	 */
13
	protected $manager;
14
15
	/**
16
	 * @var stdClass an instance which serves as a symbol to do identity checks with
17
	 */
18
	public $undefined;
19
20 View Code Duplication
	function setUp() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
		parent::setUp();
22
		require_once( WP_FIELDS_API_DIR . 'implementation/wp-includes/class-wp-customize-manager.php' );
23
		//require_once( ABSPATH . WPINC . '/class-wp-customize-manager.php' );
24
		$GLOBALS['wp_customize'] = new WP_Customize_Manager();
25
		$this->manager = $GLOBALS['wp_customize'];
26
		$this->undefined = new stdClass();
27
	}
28
29
	function tearDown() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
30
		$this->manager = null;
31
		unset( $GLOBALS['wp_customize'] );
32
		parent::tearDown();
33
	}
34
35
	function test_constructor_without_args() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
36
		$setting = new WP_Customize_Setting( $this->manager, 'foo' );
37
		$this->assertEquals( $this->manager, $setting->manager );
38
		$this->assertEquals( 'foo', $setting->id );
39
		$this->assertEquals( 'theme_mod', $setting->type );
40
		$this->assertEquals( 'edit_theme_options', $setting->capability );
41
		$this->assertEquals( '', $setting->theme_supports );
42
		$this->assertEquals( '', $setting->default );
43
		$this->assertEquals( 'refresh', $setting->transport );
44
		$this->assertEquals( '', $setting->sanitize_callback );
45
		$this->assertEquals( '', $setting->sanitize_js_callback );
46
		$this->assertFalse( has_filter( "customize_sanitize_{$setting->id}" ) );
47
		$this->assertFalse( has_filter( "customize_sanitize_js_{$setting->id}" ) );
48
		$this->assertEquals( false, $setting->dirty );
49
	}
50
51
	function test_constructor_with_args() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
52
		$args = array(
53
			'type' => 'option',
54
			'capability' => 'edit_posts',
55
			'theme_supports' => 'widgets',
56
			'default' => 'barbar',
57
			'transport' => 'postMessage',
58
			'sanitize_callback' => create_function( '$value', 'return $value . ":sanitize_callback";' ),
0 ignored issues
show
Security Best Practice introduced by
The use of create_function is highly discouraged, better use a closure.

create_function can pose a great security vulnerability as it is similar to eval, and could be used for arbitrary code execution. We highly recommend to use a closure instead.

// Instead of
$function = create_function('$a, $b', 'return $a + $b');

// Better use
$function = function($a, $b) { return $a + $b; }
Loading history...
59
			'sanitize_js_callback' => create_function( '$value', 'return $value . ":sanitize_js_callback";' ),
0 ignored issues
show
Security Best Practice introduced by
The use of create_function is highly discouraged, better use a closure.

create_function can pose a great security vulnerability as it is similar to eval, and could be used for arbitrary code execution. We highly recommend to use a closure instead.

// Instead of
$function = create_function('$a, $b', 'return $a + $b');

// Better use
$function = function($a, $b) { return $a + $b; }
Loading history...
60
		);
61
		$setting = new WP_Customize_Setting( $this->manager, 'bar', $args );
62
		$this->assertEquals( 'bar', $setting->id );
63
		foreach ( $args as $key => $value ) {
64
			$this->assertEquals( $value, $setting->$key );
65
		}
66
		$this->assertEquals( 10, has_filter( "customize_sanitize_{$setting->id}", $args['sanitize_callback'] ) );
67
		$this->assertEquals( 10, has_filter( "customize_sanitize_js_{$setting->id}" ), $args['sanitize_js_callback'] );
68
	}
69
70
	public $post_data_overrides = array(
71
		'unset_option_overridden' => 'unset_option_post_override_value',
72
		'unset_theme_mod_overridden' => 'unset_theme_mod_post_override_value',
73
		'set_option_overridden' => 'set_option_post_override_value',
74
		'set_theme_mod_overridden' => 'set_theme_mod_post_override_value',
75
		'unset_option_multi_overridden[foo]' => 'unset_option_multi_overridden[foo]_post_override_value',
76
		'unset_theme_mod_multi_overridden[foo]' => 'unset_theme_mod_multi_overridden[foo]_post_override_value',
77
		'set_option_multi_overridden[foo]' => 'set_option_multi_overridden[foo]_post_override_value',
78
		'set_theme_mod_multi_overridden[foo]' => 'set_theme_mod_multi_overridden[foo]_post_override_value',
79
	);
80
81
	public $standard_type_configs = array(
82
		'option' => array(
83
			'getter' => 'get_option',
84
			'setter' => 'update_option',
85
		),
86
		'theme_mod' => array(
87
			'getter' => 'get_theme_mod',
88
			'setter' => 'set_theme_mod',
89
		),
90
	);
91
92
	/**
93
	 * Run assertions on non-multidimensional standard settings.
94
	 */
95
	function test_preview_standard_types_non_multidimensional() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
96
		$_POST['customized'] = wp_slash( wp_json_encode( $this->post_data_overrides ) );
97
98
		// Try non-multidimensional settings
99
		foreach ( $this->standard_type_configs as $type => $type_options ) {
100
			// Non-multidimensional: See what effect the preview filter has on a non-existent setting (default value should be seen)
101
			$name = "unset_{$type}_without_post_value";
102
			$default = "default_value_{$name}";
103
			$setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) );
104
			$this->assertEquals( $this->undefined, call_user_func( $type_options['getter'], $name, $this->undefined ) );
105
			$this->assertEquals( $default, $setting->value() );
106
			$setting->preview();
107
			$this->assertEquals( $default, call_user_func( $type_options['getter'], $name, $this->undefined ), sprintf( 'Expected %s(%s) to return setting default: %s.', $type_options['getter'], $name, $default ) );
108
			$this->assertEquals( $default, $setting->value() );
109
110
			// Non-multidimensional: See what effect the preview has on an extant setting (default value should not be seen)
111
			$name = "set_{$type}_without_post_value";
112
			$default = "default_value_{$name}";
113
			$initial_value = "initial_value_{$name}";
114
			call_user_func( $type_options['setter'], $name, $initial_value );
115
			$setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) );
116
			$this->assertEquals( $initial_value, call_user_func( $type_options['getter'], $name ) );
117
			$this->assertEquals( $initial_value, $setting->value() );
118
			$setting->preview();
119
			$this->assertEquals( 0, did_action( "customize_preview_{$setting->id}" ) ); // only applicable for custom types (not options or theme_mods)
120
			$this->assertEquals( 0, did_action( "customize_preview_{$setting->type}" ) ); // only applicable for custom types (not options or theme_mods)
121
			$this->assertEquals( $initial_value, call_user_func( $type_options['getter'], $name ) );
122
			$this->assertEquals( $initial_value, $setting->value() );
123
124
			// @todo What if we call the setter after preview() is called? If no post_value, should the new set value be stored? If that happens, then the following 3 assertions should be inverted
125
			$overridden_value = "overridden_value_$name";
126
			call_user_func( $type_options['setter'], $name, $overridden_value );
127
			$this->assertEquals( $initial_value, call_user_func( $type_options['getter'], $name ) );
128
			$this->assertEquals( $initial_value, $setting->value() );
129
			$this->assertNotEquals( $overridden_value, $setting->value() );
130
131
			// Non-multidimensional: Test unset setting being overridden by a post value
132
			$name = "unset_{$type}_overridden";
133
			$default = "default_value_{$name}";
134
			$setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) );
135
			$this->assertEquals( $this->undefined, call_user_func( $type_options['getter'], $name, $this->undefined ) );
136
			$this->assertEquals( $default, $setting->value() );
137
			$setting->preview(); // activate post_data
138
			$this->assertEquals( $this->post_data_overrides[ $name ], call_user_func( $type_options['getter'], $name, $this->undefined ) );
139
			$this->assertEquals( $this->post_data_overrides[ $name ], $setting->value() );
140
141
			// Non-multidimensional: Test set setting being overridden by a post value
142
			$name = "set_{$type}_overridden";
143
			$default = "default_value_{$name}";
144
			$initial_value = "initial_value_{$name}";
145
			call_user_func( $type_options['setter'], $name, $initial_value );
146
			$setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) );
147
			$this->assertEquals( $initial_value, call_user_func( $type_options['getter'], $name, $this->undefined ) );
148
			$this->assertEquals( $initial_value, $setting->value() );
149
			$setting->preview(); // activate post_data
150
			$this->assertEquals( 0, did_action( "customize_preview_{$setting->id}" ) ); // only applicable for custom types (not options or theme_mods)
151
			$this->assertEquals( 0, did_action( "customize_preview_{$setting->type}" ) ); // only applicable for custom types (not options or theme_mods)
152
			$this->assertEquals( $this->post_data_overrides[ $name ], call_user_func( $type_options['getter'], $name, $this->undefined ) );
153
			$this->assertEquals( $this->post_data_overrides[ $name ], $setting->value() );
154
		}
155
	}
156
157
	/**
158
	 * Run assertions on multidimensional standard settings.
159
	 */
160
	function test_preview_standard_types_multidimensional() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
161
		$_POST['customized'] = wp_slash( wp_json_encode( $this->post_data_overrides ) );
162
163
		foreach ( $this->standard_type_configs as $type => $type_options ) {
164
			// Multidimensional: See what effect the preview filter has on a non-existent setting (default value should be seen)
165
			$base_name = "unset_{$type}_multi";
166
			$name = $base_name . '[foo]';
167
			$default = "default_value_{$name}";
168
			$setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) );
169
			$this->assertEquals( $this->undefined, call_user_func( $type_options['getter'], $base_name, $this->undefined ) );
170
			$this->assertEquals( $default, $setting->value() );
171
			$setting->preview();
172
			$base_value = call_user_func( $type_options['getter'], $base_name, $this->undefined );
173
			$this->assertArrayHasKey( 'foo', $base_value );
174
			$this->assertEquals( $default, $base_value['foo'] );
175
176
			// Multidimensional: See what effect the preview has on an extant setting (default value should not be seen)
177
			$base_name = "set_{$type}_multi";
178
			$name = $base_name . '[foo]';
179
			$default = "default_value_{$name}";
180
			$initial_value = "initial_value_{$name}";
181
			$base_initial_value = array( 'foo' => $initial_value, 'bar' => 'persisted' );
182
			call_user_func( $type_options['setter'], $base_name, $base_initial_value );
183
			$setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) );
184
			$base_value = call_user_func( $type_options['getter'], $base_name, array() );
185
			$this->assertEquals( $initial_value, $base_value['foo'] );
186
			$this->assertEquals( $initial_value, $setting->value() );
187
			$setting->preview();
188
			$this->assertEquals( 0, did_action( "customize_preview_{$setting->id}" ) ); // only applicable for custom types (not options or theme_mods)
189
			$this->assertEquals( 0, did_action( "customize_preview_{$setting->type}" ) ); // only applicable for custom types (not options or theme_mods)
190
			$base_value = call_user_func( $type_options['getter'], $base_name, array() );
191
			$this->assertEquals( $initial_value, $base_value['foo'] );
192
			$this->assertEquals( $initial_value, $setting->value() );
193
194
			// Multidimensional: Test unset setting being overridden by a post value
195
			$base_name = "unset_{$type}_multi_overridden";
196
			$name = $base_name . '[foo]';
197
			$default = "default_value_{$name}";
198
			$setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) );
199
			$this->assertEquals( $this->undefined, call_user_func( $type_options['getter'], $base_name, $this->undefined ) );
200
			$this->assertEquals( $default, $setting->value() );
201
			$setting->preview();
202
			$this->assertEquals( 0, did_action( "customize_preview_{$setting->id}" ) ); // only applicable for custom types (not options or theme_mods)
203
			$this->assertEquals( 0, did_action( "customize_preview_{$setting->type}" ) ); // only applicable for custom types (not options or theme_mods)
204
			$base_value = call_user_func( $type_options['getter'], $base_name, $this->undefined );
205
			$this->assertArrayHasKey( 'foo', $base_value );
206
			$this->assertEquals( $this->post_data_overrides[ $name ], $base_value['foo'] );
207
208
			// Multidimemsional: Test set setting being overridden by a post value
209
			$base_name = "set_{$type}_multi_overridden";
210
			$name = $base_name . '[foo]';
211
			$default = "default_value_{$name}";
212
			$initial_value = "initial_value_{$name}";
213
			$base_initial_value = array( 'foo' => $initial_value, 'bar' => 'persisted' );
214
			call_user_func( $type_options['setter'], $base_name, $base_initial_value );
215
			$setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) );
216
			$base_value = call_user_func( $type_options['getter'], $base_name, $this->undefined );
217
			$this->arrayHasKey( 'foo', $base_value );
218
			$this->arrayHasKey( 'bar', $base_value );
219
			$this->assertEquals( $base_initial_value['foo'], $base_value['foo'] );
220
221
			$getter = call_user_func( $type_options['getter'], $base_name, $this->undefined );
222
			$this->assertEquals( $base_initial_value['bar'], $getter['bar'] );
223
			$this->assertEquals( $initial_value, $setting->value() );
224
			$setting->preview();
225
			$this->assertEquals( 0, did_action( "customize_preview_{$setting->id}" ) ); // only applicable for custom types (not options or theme_mods)
226
			$this->assertEquals( 0, did_action( "customize_preview_{$setting->type}" ) ); // only applicable for custom types (not options or theme_mods)
227
			$base_value = call_user_func( $type_options['getter'], $base_name, $this->undefined );
228
			$this->assertArrayHasKey( 'foo', $base_value );
229
			$this->assertEquals( $this->post_data_overrides[ $name ], $base_value['foo'] );
230
			$this->arrayHasKey( 'bar', call_user_func( $type_options['getter'], $base_name, $this->undefined ) );
231
232
			$getter = call_user_func( $type_options['getter'], $base_name, $this->undefined );
233
			$this->assertEquals( $base_initial_value['bar'], $getter['bar'] );
234
		}
235
	}
236
237
	/**
238
	 * @var array storage for saved custom type data that are tested in self::test_preview_custom_type()
239
	 */
240
	protected $custom_type_data_saved;
241
242
	/**
243
	 * @var array storage for previewed custom type data that are tested in self::test_preview_custom_type()
244
	 */
245
	protected $custom_type_data_previewed;
246
247
	function custom_type_getter( $name, $default = null ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
248
		if ( did_action( "customize_preview_{$name}" ) && array_key_exists( $name, $this->custom_type_data_previewed ) ) {
249
			$value = $this->custom_type_data_previewed[ $name ];
250
		} else if ( array_key_exists( $name, $this->custom_type_data_saved ) ) {
251
			$value = $this->custom_type_data_saved[ $name ];
252
		} else {
253
			$value = $default;
254
		}
255
		return $value;
256
	}
257
258
	function custom_type_setter( $name, $value ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
259
		$this->custom_type_data_saved[ $name ] = $value;
260
	}
261
262
	function custom_type_value_filter( $default ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
263
		$name = preg_replace( '/^customize_value_/', '', current_filter() );
264
		return $this->custom_type_getter( $name, $default );
265
	}
266
267
	/**
268
	 * @param WP_Customize_Setting $setting
269
	 */
270
	function custom_type_preview( $setting ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
271
		$previewed_value = $setting->post_value( $this->undefined );
272
		if ( $this->undefined !== $previewed_value ) {
273
			$this->custom_type_data_previewed[ $setting->id ] = $previewed_value;
274
		}
275
	}
276
277
	function test_preview_custom_type() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
278
		$type = 'custom_type';
279
		$post_data_overrides = array(
280
			"unset_{$type}_with_post_value" => "unset_{$type}_without_post_value",
281
			"set_{$type}_with_post_value" => "set_{$type}_without_post_value",
282
		);
283
		$_POST['customized'] = wp_slash( wp_json_encode( $post_data_overrides ) );
284
285
		$this->custom_type_data_saved = array();
286
		$this->custom_type_data_previewed = array();
287
288
		add_action( "customize_preview_{$type}", array( $this, 'custom_type_preview' ) );
289
290
		// Custom type not existing and no post value override
291
		$name = "unset_{$type}_without_post_value";
292
		$default = "default_value_{$name}";
293
		$setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) );
294
		// Note: #29316 will allow us to have one filter for all settings of a given type, which is what we need
295
		add_filter( "customize_value_{$name}", array( $this, 'custom_type_value_filter' ) );
296
		$this->assertEquals( $this->undefined, $this->custom_type_getter( $name, $this->undefined ) );
297
		$this->assertEquals( $default, $setting->value() );
298
		$setting->preview();
299
		$this->assertEquals( 1, did_action( "customize_preview_{$setting->id}" ) );
300
		$this->assertEquals( 1, did_action( "customize_preview_{$setting->type}" ) );
301
		$this->assertEquals( $this->undefined, $this->custom_type_getter( $name, $this->undefined ) ); // Note: for a non-custom type this is $default
302
		$this->assertEquals( $default, $setting->value() ); // should be same as above
303
304
		// Custom type existing and no post value override
305
		$name = "set_{$type}_without_post_value";
306
		$default = "default_value_{$name}";
307
		$initial_value = "initial_value_{$name}";
308
		$this->custom_type_setter( $name, $initial_value );
309
		$setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) );
310
		// Note: #29316 will allow us to have one filter for all settings of a given type, which is what we need
311
		add_filter( "customize_value_{$name}", array( $this, 'custom_type_value_filter' ) );
312
		$this->assertEquals( $initial_value, $this->custom_type_getter( $name, $this->undefined ) );
313
		$this->assertEquals( $initial_value, $setting->value() );
314
		$setting->preview();
315
		$this->assertEquals( 1, did_action( "customize_preview_{$setting->id}" ) );
316
		$this->assertEquals( 2, did_action( "customize_preview_{$setting->type}" ) );
317
		$this->assertEquals( $initial_value, $this->custom_type_getter( $name, $this->undefined ) ); // should be same as above
318
		$this->assertEquals( $initial_value, $setting->value() ); // should be same as above
319
320
		// Custom type not existing and with a post value override
321
		$name = "unset_{$type}_with_post_value";
322
		$default = "default_value_{$name}";
323
		$setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) );
324
		// Note: #29316 will allow us to have one filter for all settings of a given type, which is what we need
325
		add_filter( "customize_value_{$name}", array( $this, 'custom_type_value_filter' ) );
326
		$this->assertEquals( $this->undefined, $this->custom_type_getter( $name, $this->undefined ) );
327
		$this->assertEquals( $default, $setting->value() );
328
		$setting->preview();
329
		$this->assertEquals( 1, did_action( "customize_preview_{$setting->id}" ) );
330
		$this->assertEquals( 3, did_action( "customize_preview_{$setting->type}" ) );
331
		$this->assertEquals( $post_data_overrides[ $name ], $this->custom_type_getter( $name, $this->undefined ) );
332
		$this->assertEquals( $post_data_overrides[ $name ], $setting->value() );
333
334
		// Custom type not existing and with a post value override
335
		$name = "set_{$type}_with_post_value";
336
		$default = "default_value_{$name}";
337
		$initial_value = "initial_value_{$name}";
338
		$this->custom_type_setter( $name, $initial_value );
339
		$setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) );
340
		// Note: #29316 will allow us to have one filter for all settings of a given type, which is what we need
341
		add_filter( "customize_value_{$name}", array( $this, 'custom_type_value_filter' ) );
342
		$this->assertEquals( $initial_value, $this->custom_type_getter( $name, $this->undefined ) );
343
		$this->assertEquals( $initial_value, $setting->value() );
344
		$setting->preview();
345
		$this->assertEquals( 1, did_action( "customize_preview_{$setting->id}" ) );
346
		$this->assertEquals( 4, did_action( "customize_preview_{$setting->type}" ) );
347
		$this->assertEquals( $post_data_overrides[ $name ], $this->custom_type_getter( $name, $this->undefined ) );
348
		$this->assertEquals( $post_data_overrides[ $name ], $setting->value() );
349
350
		unset( $this->custom_type_data_previewed, $this->custom_type_data_saved );
351
	}
352
353
	/**
354
	 * Test specific fix for setting's default value not applying on preview window
355
	 *
356
	 * @ticket 30988
357
	 */
358
	function test_non_posted_setting_applying_default_value_in_preview() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
359
		$type = 'option';
360
		$name = 'unset_option_without_post_value';
361
		$default = "default_value_{$name}";
362
		$setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) );
363
		$this->assertEquals( $this->undefined, get_option( $name, $this->undefined ) );
364
		$this->assertEquals( $default, $setting->value() );
365
		$setting->preview();
366
		$this->assertEquals( $default, get_option( $name, $this->undefined ), sprintf( 'Expected get_option(%s) to return setting default: %s.', $name, $default ) );
367
		$this->assertEquals( $default, $setting->value() );
368
	}
369
370
	/**
371
	 * Ensure that is_current_blog_previewed returns the expected values.
372
	 *
373
	 * This is applicable to both single and multisite. This doesn't do switch_to_blog()
374
	 *
375
	 * @ticket 31428
376
	 */
377
	function test_is_current_blog_previewed() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
378
		$type = 'option';
379
		$name = 'blogname';
380
		$post_value = rand_str();
381
		$this->manager->set_post_value( $name, $post_value );
382
		$setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type' ) );
383
		$this->assertFalse( $setting->is_current_blog_previewed() );
384
		$setting->preview();
385
		$this->assertTrue( $setting->is_current_blog_previewed() );
386
387
		$this->assertEquals( $post_value, $setting->value() );
388
		$this->assertEquals( $post_value, get_option( $name ) );
389
	}
390
391
	/**
392
	 * Ensure that previewing a setting is disabled when the current blog is switched.
393
	 *
394
	 * @ticket 31428
395
	 * @group multisite
396
	 */
397
	function test_previewing_with_switch_to_blog() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
398
		if ( ! is_multisite() ) {
399
			$this->markTestSkipped( 'Cannot test WP_Customize_Setting::is_current_blog_previewed() with switch_to_blog() if not on multisite.' );
400
		}
401
402
		$type = 'option';
403
		$name = 'blogdescription';
404
		$post_value = rand_str();
405
		$this->manager->set_post_value( $name, $post_value );
406
		$setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type' ) );
407
		$this->assertFalse( $setting->is_current_blog_previewed() );
408
		$setting->preview();
409
		$this->assertTrue( $setting->is_current_blog_previewed() );
410
411
		$blog_id = $this->factory->blog->create();
412
		switch_to_blog( $blog_id );
413
		$this->assertFalse( $setting->is_current_blog_previewed() );
414
		$this->assertNotEquals( $post_value, $setting->value() );
415
		$this->assertNotEquals( $post_value, get_option( $name ) );
416
		restore_current_blog();
417
	}
418
}
419
420