Tests_WP_Customize_Section::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_Section class.
5
 *
6
 * @group customize
7
 */
8
class Tests_WP_Customize_Section 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_Section::__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
		$section = new WP_Customize_Section( $this->manager, 'foo' );
35
		$this->assertInternalType( 'int', $section->instance_number );
36
		$this->assertEquals( $this->manager, $section->manager );
37
		$this->assertEquals( 'foo', $section->id );
38
		$this->assertEquals( 160, $section->priority );
39
		$this->assertEquals( 'edit_theme_options', $section->capability );
40
		$this->assertEquals( '', $section->theme_supports );
41
		$this->assertEquals( '', $section->title );
42
		$this->assertEquals( '', $section->description );
43
		$this->assertEmpty( $section->panel );
44
		$this->assertEquals( 'default', $section->type );
45
		$this->assertEquals( array( $section, 'active_callback' ), $section->active_callback );
46
	}
47
48
	/**
49
	 * @see WP_Customize_Section::__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
			'panel' => 'bar',
61
		);
62
63
		$this->manager->add_panel( 'bar' );
64
65
		$section = new WP_Customize_Section( $this->manager, 'foo', $args );
66
		foreach ( $args as $key => $value ) {
67
			$this->assertEquals( $value, $section->$key );
68
		}
69
	}
70
71
	/**
72
	 * @see WP_Customize_Section::__construct()
73
	 */
74
	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...
75
		$section = new Custom_Section_Test( $this->manager, 'foo' );
76
		$this->assertEquals( 'titleless', $section->type );
77
	}
78
79
	/**
80
	 * @see WP_Customize_Section::active()
81
	 * @see WP_Customize_Section::active_callback()
82
	 */
83 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...
84
		$section = new WP_Customize_Section( $this->manager, 'foo' );
85
		$this->assertTrue( $section->active() );
86
87
		$section = new WP_Customize_Section( $this->manager, 'foo', array(
88
			'active_callback' => '__return_false',
89
		) );
90
		$this->assertFalse( $section->active() );
91
		add_filter( 'customize_section_active', array( $this, 'filter_active_test' ), 10, 2 );
92
		$this->assertTrue( $section->active() );
93
	}
94
95
	/**
96
	 * @param bool $active
97
	 * @param WP_Customize_Section $section
98
	 * @return bool
99
	 */
100
	function filter_active_test( $active, $section ) {
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...
101
		$this->assertFalse( $active );
102
		$this->assertInstanceOf( 'WP_Customize_Section', $section );
103
		$active = true;
104
		return $active;
105
	}
106
107
	/**
108
	 * @see WP_Customize_Section::json()
109
	 */
110
	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...
111
		$args = array(
112
			'priority' => 200,
113
			'capability' => 'edit_posts',
114
			'theme_supports' => 'html5',
115
			'title' => 'Hello World',
116
			'description' => 'Lorem Ipsum',
117
			'type' => 'horizontal',
118
			'panel' => 'bar',
119
			'active_callback' => '__return_true',
120
		);
121
122
		$this->manager->add_panel( 'bar' );
123
124
		$section = new WP_Customize_Section( $this->manager, 'foo', $args );
125
		$data = $section->json();
126
		$this->assertEquals( 'foo', $data['id'] );
127 View Code Duplication
		foreach ( array( 'title', 'description', 'priority', 'panel', '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...
128
			$this->assertEquals( $args[ $key ], $data[ $key ] );
129
		}
130
		$this->assertEmpty( $data['content'] );
131
		$this->assertTrue( $data['active'] );
132
		$this->assertInternalType( 'int', $data['instanceNumber'] );
133
	}
134
135
	/**
136
	 * @see WP_Customize_Section::check_capabilities()
137
	 */
138 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...
139
		$user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
140
		wp_set_current_user( $user_id );
141
142
		$section = new WP_Customize_Section( $this->manager, 'foo' );
143
		$this->assertTrue( $section->check_capabilities() );
144
		$old_cap = $section->capability;
145
		$section->capability = 'do_not_allow';
146
		$this->assertFalse( $section->check_capabilities() );
147
		$section->capability = $old_cap;
148
		$this->assertTrue( $section->check_capabilities() );
149
		$section->theme_supports = 'impossible_feature';
150
		$this->assertFalse( $section->check_capabilities() );
151
	}
152
153
	/**
154
	 * @see WP_Customize_Section::get_content()
155
	 */
156
	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...
157
		$section = new WP_Customize_Section( $this->manager, 'foo' );
158
		$this->assertEmpty( $section->get_content() );
159
	}
160
161
	/**
162
	 * @see WP_Customize_Section::maybe_render()
163
	 */
164 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...
165
		wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
166
		$section = new WP_Customize_Section( $this->manager, 'bar' );
167
		$customize_render_section_count = did_action( 'customize_render_section' );
168
		add_action( 'customize_render_section', array( $this, 'action_customize_render_section_test' ) );
169
		ob_start();
170
		$section->maybe_render();
171
		$content = ob_get_clean();
172
		$this->assertTrue( $section->check_capabilities() );
173
		$this->assertEmpty( $content );
174
		$this->assertEquals( $customize_render_section_count + 1, did_action( 'customize_render_section' ), 'Unexpected did_action count for customize_render_section' );
175
		$this->assertEquals( 1, did_action( "customize_render_section_{$section->id}" ), "Unexpected did_action count for customize_render_section_{$section->id}" );
176
	}
177
178
	/**
179
	 * @see WP_Customize_Section::maybe_render()
180
	 * @param WP_Customize_Section $section
181
	 */
182
	function action_customize_render_section_test( $section ) {
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
		$this->assertInstanceOf( 'WP_Customize_Section', $section );
184
	}
185
186
	/**
187
	 * @see WP_Customize_Section::print_template()
188
	 */
189 View Code Duplication
	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...
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...
190
		wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
191
192
		$section = new WP_Customize_Section( $this->manager, 'baz' );
193
		ob_start();
194
		$section->print_template();
195
		$content = ob_get_clean();
196
		$this->assertContains( '<script type="text/html" id="tmpl-customize-section-default">', $content );
197
		$this->assertContains( 'accordion-section-title', $content );
198
		$this->assertContains( 'accordion-section-content', $content );
199
	}
200
201
	/**
202
	 * @see WP_Customize_Section::print_template()
203
	 */
204 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...
205
		wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
206
207
		$section = new Custom_Section_Test( $this->manager, 'baz' );
208
		ob_start();
209
		$section->print_template();
210
		$content = ob_get_clean();
211
		$this->assertContains( '<script type="text/html" id="tmpl-customize-section-titleless">', $content );
212
		$this->assertNotContains( 'accordion-section-title', $content );
213
		$this->assertContains( 'accordion-section-content', $content );
214
	}
215
}
216
217
require_once( WP_FIELDS_API_DIR . 'implementation/wp-includes/class-wp-customize-section.php' );
218
//require_once ABSPATH . WPINC . '/class-wp-customize-section.php';
219
class Custom_Section_Test extends WP_Customize_Section {
220
	public $type = 'titleless';
221
222
	protected function render_template() {
223
		?>
224
		<li id="accordion-section-{{ data.id }}" class="accordion-section control-section control-section-{{ data.type }}">
225
			<ul class="accordion-section-content">
226
				<# if ( data.description ) { #>
227
					<li class="customize-section-description-container">
228
						<p class="description customize-section-description">{{{ data.description }}}</p>
229
					</li>
230
				<# } #>
231
			</ul>
232
		</li>
233
		<?php
234
	}
235
236
}
237