Tests_WP_Customize_Panel::test_active()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Tests for the WP_Customize_Panel class.
5
 *
6
 * @group customize
7
 */
8
class Tests_WP_Customize_Panel extends WP_UnitTestCase {
9
10
	/**
11
	 * @var WP_Customize_Manager
12
	 */
13
	protected $manager;
14
15 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...
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
		$this->undefined = new stdClass();
22
	}
23
24
	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...
25
		$this->manager = null;
26
		unset( $GLOBALS['wp_customize'] );
27
		parent::tearDown();
28
	}
29
30
	/**
31
	 * @see WP_Customize_Panel::__construct()
32
	 */
33 View Code Duplication
	function test_construct_default_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...
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...
34
		$panel = new WP_Customize_Panel( $this->manager, 'foo' );
35
		$this->assertInternalType( 'int', $panel->instance_number );
36
		$this->assertEquals( $this->manager, $panel->manager );
37
		$this->assertEquals( 'foo', $panel->id );
38
		$this->assertEquals( 160, $panel->priority );
39
		$this->assertEquals( 'edit_theme_options', $panel->capability );
40
		$this->assertEquals( '', $panel->theme_supports );
41
		$this->assertEquals( '', $panel->title );
42
		$this->assertEquals( '', $panel->description );
43
		$this->assertEmpty( $panel->sections );
44
		$this->assertEquals( 'default', $panel->type );
45
		$this->assertEquals( array( $panel, 'active_callback' ), $panel->active_callback );
46
	}
47
48
	/**
49
	 * @see WP_Customize_Panel::__construct()
50
	 */
51
	function test_construct_custom_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
			'priority' => 200,
54
			'capability' => 'edit_posts',
55
			'theme_supports' => 'html5',
56
			'title' => 'Hello World',
57
			'description' => 'Lorem Ipsum',
58
			'type' => 'horizontal',
59
			'active_callback' => '__return_true',
60
		);
61
62
		$panel = new WP_Customize_Panel( $this->manager, 'foo', $args );
63
		foreach ( $args as $key => $value ) {
64
			$this->assertEquals( $value, $panel->$key );
65
		}
66
	}
67
68
	/**
69
	 * @see WP_Customize_Panel::__construct()
70
	 */
71
	function test_construct_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...
72
		$panel = new Custom_Panel_Test( $this->manager, 'foo' );
73
		$this->assertEquals( 'titleless', $panel->type );
74
	}
75
76
	/**
77
	 * @see WP_Customize_Panel::active()
78
	 * @see WP_Customize_Panel::active_callback()
79
	 */
80 View Code Duplication
	function test_active() {
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...
81
		$panel = new WP_Customize_Panel( $this->manager, 'foo' );
82
		$this->assertTrue( $panel->active() );
83
84
		$panel = new WP_Customize_Panel( $this->manager, 'foo', array(
85
			'active_callback' => '__return_false',
86
		) );
87
		$this->assertFalse( $panel->active() );
88
		add_filter( 'customize_panel_active', array( $this, 'filter_active_test' ), 10, 2 );
89
		$this->assertTrue( $panel->active() );
90
	}
91
92
	/**
93
	 * @param bool $active
94
	 * @param WP_Customize_Panel $panel
95
	 * @return bool
96
	 */
97
	function filter_active_test( $active, $panel ) {
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...
98
		$this->assertFalse( $active );
99
		$this->assertInstanceOf( 'WP_Customize_Panel', $panel );
100
		$active = true;
101
		return $active;
102
	}
103
104
	/**
105
	 * @see WP_Customize_Panel::json()
106
	 */
107
	function test_json() {
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...
108
		$args = array(
109
			'priority' => 200,
110
			'capability' => 'edit_posts',
111
			'theme_supports' => 'html5',
112
			'title' => 'Hello World',
113
			'description' => 'Lorem Ipsum',
114
			'type' => 'horizontal',
115
			'active_callback' => '__return_true',
116
		);
117
		$panel = new WP_Customize_Panel( $this->manager, 'foo', $args );
118
		$data = $panel->json();
119
		$this->assertEquals( 'foo', $data['id'] );
120 View Code Duplication
		foreach ( array( 'title', 'description', 'priority', 'type' ) as $key ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
121
			$this->assertEquals( $args[ $key ], $data[ $key ] );
122
		}
123
		$this->assertEmpty( $data['content'] );
124
		$this->assertTrue( $data['active'] );
125
		$this->assertInternalType( 'int', $data['instanceNumber'] );
126
	}
127
128
	/**
129
	 * @see WP_Customize_Panel::check_capabilities()
130
	 */
131 View Code Duplication
	function test_check_capabilities() {
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...
132
		$user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
133
		wp_set_current_user( $user_id );
134
135
		$panel = new WP_Customize_Panel( $this->manager, 'foo' );
136
		$this->assertTrue( $panel->check_capabilities() );
137
		$old_cap = $panel->capability;
138
		$panel->capability = 'do_not_allow';
139
		$this->assertFalse( $panel->check_capabilities() );
140
		$panel->capability = $old_cap;
141
		$this->assertTrue( $panel->check_capabilities() );
142
		$panel->theme_supports = 'impossible_feature';
143
		$this->assertFalse( $panel->check_capabilities() );
144
	}
145
146
	/**
147
	 * @see WP_Customize_Panel::get_content()
148
	 */
149
	function test_get_content() {
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...
150
		$panel = new WP_Customize_Panel( $this->manager, 'foo' );
151
		$this->assertEmpty( $panel->get_content() );
152
	}
153
154
	/**
155
	 * @see WP_Customize_Panel::maybe_render()
156
	 */
157 View Code Duplication
	function test_maybe_render() {
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...
158
		wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
159
		$panel = new WP_Customize_Panel( $this->manager, 'bar' );
160
		$customize_render_panel_count = did_action( 'customize_render_panel' );
161
		add_action( 'customize_render_panel', array( $this, 'action_customize_render_panel_test' ) );
162
		ob_start();
163
		$panel->maybe_render();
164
		$content = ob_get_clean();
165
		$this->assertTrue( $panel->check_capabilities() );
166
		$this->assertEmpty( $content );
167
		$this->assertEquals( $customize_render_panel_count + 1, did_action( 'customize_render_panel' ), 'Unexpected did_action count for customize_render_panel' );
168
		$this->assertEquals( 1, did_action( "customize_render_panel_{$panel->id}" ), "Unexpected did_action count for customize_render_panel_{$panel->id}" );
169
	}
170
171
	/**
172
	 * @see WP_Customize_Panel::maybe_render()
173
	 * @param WP_Customize_Panel $panel
174
	 */
175
	function action_customize_render_panel_test( $panel ) {
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...
176
		$this->assertInstanceOf( 'WP_Customize_Panel', $panel );
177
	}
178
179
	/**
180
	 * @see WP_Customize_Panel::print_template()
181
	 */
182
	function test_print_templates_standard() {
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...
183
		wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
184
185
		$panel = new WP_Customize_Panel( $this->manager, 'baz' );
186
		ob_start();
187
		$panel->print_template();
188
		$content = ob_get_clean();
189
		$this->assertContains( '<script type="text/html" id="tmpl-customize-panel-default-content">', $content );
190
		$this->assertContains( 'accordion-section-title', $content );
191
		$this->assertContains( 'control-panel-content', $content );
192
		$this->assertContains( '<script type="text/html" id="tmpl-customize-panel-default">', $content );
193
		$this->assertContains( 'customize-panel-description', $content );
194
		$this->assertContains( 'preview-notice', $content );
195
	}
196
197
	/**
198
	 * @see WP_Customize_Panel::print_template()
199
	 */
200 View Code Duplication
	function test_print_templates_custom() {
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...
201
		wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
202
203
		$panel = new Custom_Panel_Test( $this->manager, 'baz' );
204
		ob_start();
205
		$panel->print_template();
206
		$content = ob_get_clean();
207
		$this->assertContains( '<script type="text/html" id="tmpl-customize-panel-titleless-content">', $content );
208
		$this->assertNotContains( 'accordion-section-title', $content );
209
210
		$this->assertContains( '<script type="text/html" id="tmpl-customize-panel-titleless">', $content );
211
		$this->assertNotContains( 'preview-notice', $content );
212
	}
213
}
214
215
require_once( WP_FIELDS_API_DIR . 'implementation/wp-includes/class-wp-customize-panel.php' );
216
//require_once ABSPATH . WPINC . '/class-wp-customize-panel.php';
217
class Custom_Panel_Test extends WP_Customize_Panel {
218
	public $type = 'titleless';
219
220
	protected function render_template() {
221
		?>
222
		<li id="accordion-panel-{{ data.id }}" class="accordion-section control-section control-panel control-panel-{{ data.type }}">
223
			<ul class="accordion-sub-container control-panel-content"></ul>
224
		</li>
225
		<?php
226
	}
227
228
	protected function content_template() {
229
		?>
230
		<li class="panel-meta accordion-section control-section<# if ( ! data.description ) { #> cannot-expand<# } #>">
231
			<# if ( data.description ) { #>
232
				<div class="accordion-section-content description">
233
					{{{ data.description }}}
234
				</div>
235
			<# } #>
236
		</li>
237
		<?php
238
	}
239
240
}
241