Tests_WP_Customize_Manager::test_post_value()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Tests for the WP_Customize_Manager class.
5
 *
6
 * @group customize
7
 */
8
class Tests_WP_Customize_Manager extends WP_UnitTestCase {
9
10 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...
11
		parent::setUp();
12
		require_once( WP_FIELDS_API_DIR . 'implementation/wp-includes/class-wp-customize-manager.php' );
13
		//require_once( ABSPATH . WPINC . '/class-wp-customize-manager.php' );
14
		$GLOBALS['wp_customize'] = new WP_Customize_Manager();
15
		$this->manager = $GLOBALS['wp_customize'];
16
		$this->undefined = new stdClass();
17
	}
18
19
	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...
20
		$this->manager = null;
21
		unset( $GLOBALS['wp_customize'] );
22
		parent::tearDown();
23
	}
24
25
	/**
26
	 * Instantiate class, set global $wp_customize, and return instance.
27
	 *
28
	 * @return WP_Customize_Manager
29
	 */
30
	function instantiate() {
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...
31
		$GLOBALS['wp_customize'] = new WP_Customize_Manager();
32
		return $GLOBALS['wp_customize'];
33
	}
34
35
	/**
36
	 * Test WP_Customize_Manager::doing_ajax().
37
	 *
38
	 * @group ajax
39
	 */
40
	function test_doing_ajax() {
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...
41
		if ( ! defined( 'DOING_AJAX' ) ) {
42
			define( 'DOING_AJAX', true );
43
		}
44
45
		$manager = $this->instantiate();
46
		$this->assertTrue( $manager->doing_ajax() );
47
48
		$_REQUEST['action'] = 'customize_save';
49
		$this->assertTrue( $manager->doing_ajax( 'customize_save' ) );
50
		$this->assertFalse( $manager->doing_ajax( 'update-widget' ) );
51
	}
52
53
	/**
54
	 * Test ! WP_Customize_Manager::doing_ajax().
55
	 */
56
	function test_not_doing_ajax() {
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...
57
		if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
58
			$this->markTestSkipped( 'Cannot test when DOING_AJAX' );
59
		}
60
61
		$manager = $this->instantiate();
62
		$this->assertFalse( $manager->doing_ajax() );
63
	}
64
65
	/**
66
	 * Test WP_Customize_Manager::unsanitized_post_values().
67
	 *
68
	 * @ticket 30988
69
	 */
70
	function test_unsanitized_post_values() {
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...
71
		$manager = $this->instantiate();
72
73
		$customized = array(
74
			'foo' => 'bar',
75
			'baz[quux]' => 123,
76
		);
77
		$_POST['customized'] = wp_slash( wp_json_encode( $customized ) );
78
		$post_values = $manager->unsanitized_post_values();
79
		$this->assertEquals( $customized, $post_values );
80
	}
81
82
	/**
83
	 * Test the WP_Customize_Manager::post_value() method.
84
	 *
85
	 * @ticket 30988
86
	 */
87
	function test_post_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...
88
		$posted_settings = array(
89
			'foo' => 'OOF',
90
		);
91
		$_POST['customized'] = wp_slash( wp_json_encode( $posted_settings ) );
92
93
		$manager = $this->instantiate();
94
95
		$manager->add_setting( 'foo', array( 'default' => 'foo_default' ) );
96
		$foo_setting = $manager->get_setting( 'foo' );
97
		$this->assertEquals( 'foo_default', $manager->get_setting( 'foo' )->value(), 'Expected non-previewed setting to return default when value() method called.' );
98
		$this->assertEquals( $posted_settings['foo'], $manager->post_value( $foo_setting, 'post_value_foo_default' ), 'Expected post_value($foo_setting) to return value supplied in $_POST[customized][foo]' );
99
100
		$manager->add_setting( 'bar', array( 'default' => 'bar_default' ) );
101
		$bar_setting = $manager->get_setting( 'bar' );
102
		$this->assertEquals( 'post_value_bar_default', $manager->post_value( $bar_setting, 'post_value_bar_default' ), 'Expected post_value($bar_setting, $default) to return $default since no value supplied in $_POST[customized][bar]' );
103
	}
104
105
	/**
106
	 * Test the WP_Customize_Manager::add_dynamic_settings() method.
107
	 *
108
	 * @ticket 30936
109
	 */
110
	function test_add_dynamic_settings() {
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...
111
		$manager = $this->instantiate();
112
		$setting_ids = array( 'foo', 'bar' );
113
		$manager->add_setting( 'foo', array( 'default' => 'foo_default' ) );
114
		$this->assertEmpty( $manager->get_setting( 'bar' ), 'Expected there to not be a bar setting up front.' );
115
		$manager->add_dynamic_settings( $setting_ids );
116
		$this->assertEmpty( $manager->get_setting( 'bar' ), 'Expected the bar setting to remain absent since filters not added.' );
117
118
		$this->action_customize_register_for_dynamic_settings();
119
		$manager->add_dynamic_settings( $setting_ids );
120
		$this->assertNotEmpty( $manager->get_setting( 'bar' ), 'Expected bar setting to be created since filters were added.' );
121
		$this->assertEquals( 'foo_default', $manager->get_setting( 'foo' )->default, 'Expected static foo setting to not get overridden by dynamic setting.' );
122
		$this->assertEquals( 'dynamic_bar_default', $manager->get_setting( 'bar' )->default, 'Expected dynamic setting bar to have default providd by filter.' );
123
	}
124
125
	/**
126
	 * Test the WP_Customize_Manager::register_dynamic_settings() method.
127
	 *
128
	 * This is similar to test_add_dynamic_settings, except the settings are passed via $_POST['customized'].
129
	 *
130
	 * @ticket 30936
131
	 */
132
	function test_register_dynamic_settings() {
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...
133
		$posted_settings = array(
134
			'foo' => 'OOF',
135
			'bar' => 'RAB',
136
		);
137
		$_POST['customized'] = wp_slash( wp_json_encode( $posted_settings ) );
138
139
		add_action( 'customize_register', array( $this, 'action_customize_register_for_dynamic_settings' ) );
140
141
		$manager = $this->instantiate();
142
		$manager->add_setting( 'foo', array( 'default' => 'foo_default' ) );
143
144
		$this->assertEmpty( $manager->get_setting( 'bar' ), 'Expected dynamic setting "bar" to not be registered.' );
145
		do_action( 'customize_register', $manager );
146
		$this->assertNotEmpty( $manager->get_setting( 'bar' ), 'Expected dynamic setting "bar" to be automatically registered after customize_register action.' );
147
		$this->assertEmpty( $manager->get_setting( 'baz' ), 'Expected unrecognized dynamic setting "baz" to remain unregistered.' );
148
	}
149
150
	/**
151
	 * In lieu of closures, callback for customize_register action added in test_register_dynamic_settings().
152
	 */
153
	function action_customize_register_for_dynamic_settings() {
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...
154
		add_filter( 'customize_dynamic_setting_args', array( $this, 'filter_customize_dynamic_setting_args_for_test_dynamic_settings' ), 10, 2 );
155
		add_filter( 'customize_dynamic_setting_class', array( $this, 'filter_customize_dynamic_setting_class_for_test_dynamic_settings' ), 10, 3 );
156
	}
157
158
	/**
159
	 * In lieu of closures, callback for customize_dynamic_setting_args filter added for test_register_dynamic_settings().
160
	 */
161
	function filter_customize_dynamic_setting_args_for_test_dynamic_settings( $setting_args, $setting_id ) {
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...
162
		$this->assertEquals( false, $setting_args, 'Expected $setting_args to be false by default.' );
163
		$this->assertInternalType( 'string', $setting_id );
164
		if ( in_array( $setting_id, array( 'foo', 'bar' ) ) ) {
165
			$setting_args = array( 'default' => "dynamic_{$setting_id}_default" );
166
		}
167
		return $setting_args;
168
	}
169
170
	/**
171
	 * In lieu of closures, callback for customize_dynamic_setting_class filter added for test_register_dynamic_settings().
172
	 */
173
	function filter_customize_dynamic_setting_class_for_test_dynamic_settings( $setting_class, $setting_id, $setting_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...
174
		$this->assertEquals( 'WP_Customize_Setting', $setting_class );
175
		$this->assertInternalType( 'string', $setting_id );
176
		$this->assertInternalType( 'array', $setting_args );
177
		return $setting_class;
178
	}
179
}
180