Test_WP_Customize_Nav_Menu_Setting   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 504
Duplicated Lines 6.94 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 35
loc 504
rs 10
c 0
b 0
f 0
wmc 21
lcom 1
cbo 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 10 10 1
A clean_up_global_scope() 0 5 1
A get_nav_menu_items_option() 0 3 1
A test_constants() 0 4 1
B test_construct() 0 27 3
A test_construct_empty_menus() 14 14 2
A test_construct_placeholder() 11 11 1
B test_value() 0 29 2
A test_preview_updated() 0 56 1
B test_preview_inserted() 0 39 1
B test_preview_deleted() 0 28 1
B test_sanitize() 0 25 1
A test_save_updated() 0 54 2
B test_save_inserted() 0 43 1
A test_save_inserted_conflicted_name() 0 19 1
B test_save_deleted() 0 37 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * Tests WP_Customize_Nav_Menu_Setting.
5
 *
6
 * @group customize
7
 */
8
class Test_WP_Customize_Nav_Menu_Setting extends WP_UnitTestCase {
9
10
	/**
11
	 * Instance of WP_Customize_Manager which is reset for each test.
12
	 *
13
	 * @var WP_Customize_Manager
14
	 */
15
	public $wp_customize;
16
17
	/**
18
	 * Set up a test case.
19
	 *
20
	 * @see WP_UnitTestCase::setup()
21
	 */
22 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...
23
		parent::setUp();
24
		require_once( WP_FIELDS_API_DIR . 'implementation/wp-includes/class-wp-customize-manager.php' );
25
		//require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
26
		wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
27
28
		global $wp_customize;
29
		$this->wp_customize = new WP_Customize_Manager();
30
		$wp_customize = $this->wp_customize;
31
	}
32
33
	/**
34
	 * Delete the $wp_customize global when cleaning up scope.
35
	 */
36
	function clean_up_global_scope() {
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...
37
		global $wp_customize;
38
		$wp_customize = null;
39
		parent::clean_up_global_scope();
40
	}
41
42
	/**
43
	 * Helper for getting the nav_menu_options option.
44
	 *
45
	 * @return array
46
	 */
47
	function get_nav_menu_items_option() {
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...
48
		return get_option( 'nav_menu_options', array( 'auto_add' => array() ) );
49
	}
50
51
	/**
52
	 * Test constants and statics.
53
	 */
54
	function test_constants() {
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...
55
		do_action( 'customize_register', $this->wp_customize );
56
		$this->assertTrue( taxonomy_exists( WP_Customize_Nav_Menu_Setting::TAXONOMY ) );
57
	}
58
59
	/**
60
	 * Test constructor.
61
	 *
62
	 * @see WP_Customize_Nav_Menu_Setting::__construct()
63
	 */
64
	function test_construct() {
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...
65
		do_action( 'customize_register', $this->wp_customize );
66
67
		$setting = new WP_Customize_Nav_Menu_Setting( $this->wp_customize, 'nav_menu[123]' );
68
		$this->assertEquals( 'nav_menu', $setting->type );
69
		$this->assertEquals( 'postMessage', $setting->transport );
70
		$this->assertEquals( 123, $setting->term_id );
71
		$this->assertNull( $setting->previous_term_id );
72
		$this->assertNull( $setting->update_status );
73
		$this->assertNull( $setting->update_error );
74
		$this->assertInternalType( 'array', $setting->default );
75
		foreach ( array( 'name', 'description', 'parent' ) as $key ) {
76
			$this->assertArrayHasKey( $key, $setting->default );
77
		}
78
		$this->assertEquals( '', $setting->default['name'] );
79
		$this->assertEquals( '', $setting->default['description'] );
80
		$this->assertEquals( 0, $setting->default['parent'] );
81
82
		$exception = null;
83
		try {
84
			$bad_setting = new WP_Customize_Nav_Menu_Setting( $this->wp_customize, 'foo_bar_baz' );
85
			unset( $bad_setting );
86
		} catch ( Exception $e ) {
87
			$exception = $e;
88
		}
89
		$this->assertInstanceOf( 'Exception', $exception );
90
	}
91
92
	/**
93
	 * Test empty constructor.
94
	 */
95 View Code Duplication
	function test_construct_empty_menus() {
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...
96
		do_action( 'customize_register', $this->wp_customize );
97
		$_wp_customize = $this->wp_customize;
98
		unset( $_wp_customize->nav_menus );
99
100
		$exception = null;
101
		try {
102
			$bad_setting = new WP_Customize_Nav_Menu_Setting( $_wp_customize, 'nav_menu_item[123]' );
103
			unset( $bad_setting );
104
		} catch ( Exception $e ) {
105
			$exception = $e;
106
		}
107
		$this->assertInstanceOf( 'Exception', $exception );
108
	}
109
110
	/**
111
	 * Test constructor for placeholder (draft) menu.
112
	 *
113
	 * @see WP_Customize_Nav_Menu_Setting::__construct()
114
	 */
115 View Code Duplication
	function test_construct_placeholder() {
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...
116
		do_action( 'customize_register', $this->wp_customize );
117
		$default = array(
118
			'name' => 'Lorem',
119
			'description' => 'ipsum',
120
			'parent' => 123,
121
		);
122
		$setting = new WP_Customize_Nav_Menu_Setting( $this->wp_customize, 'nav_menu[-5]', compact( 'default' ) );
123
		$this->assertEquals( -5, $setting->term_id );
124
		$this->assertEquals( $default, $setting->default );
125
	}
126
127
	/**
128
	 * Test value method.
129
	 *
130
	 * @see WP_Customize_Nav_Menu_Setting::value()
131
	 */
132
	function test_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...
133
		do_action( 'customize_register', $this->wp_customize );
134
135
		$menu_name = 'Test 123';
136
		$parent_menu_id = wp_create_nav_menu( "Parent $menu_name" );
137
		$description = 'Hello my world.';
138
		$menu_id = wp_update_nav_menu_object( 0, array(
139
			'menu-name' => $menu_name,
140
			'parent' => $parent_menu_id,
141
			'description' => $description,
142
		) );
143
144
		$setting_id = "nav_menu[$menu_id]";
145
		$setting = new WP_Customize_Nav_Menu_Setting( $this->wp_customize, $setting_id );
146
147
		$value = $setting->value();
148
		$this->assertInternalType( 'array', $value );
149
		foreach ( array( 'name', 'description', 'parent' ) as $key ) {
150
			$this->assertArrayHasKey( $key, $value );
151
		}
152
		$this->assertEquals( $menu_name, $value['name'] );
153
		$this->assertEquals( $description, $value['description'] );
154
		$this->assertEquals( $parent_menu_id, $value['parent'] );
155
156
		$new_menu_name = 'Foo';
157
		wp_update_nav_menu_object( $menu_id, array( 'menu-name' => $new_menu_name ) );
158
		$updated_value = $setting->value();
159
		$this->assertEquals( $new_menu_name, $updated_value['name'] );
160
	}
161
162
	/**
163
	 * Test preview method for updated menu.
164
	 *
165
	 * @see WP_Customize_Nav_Menu_Setting::preview()
166
	 */
167
	function test_preview_updated() {
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...
168
		do_action( 'customize_register', $this->wp_customize );
169
170
		$menu_id = wp_update_nav_menu_object( 0, array(
171
			'menu-name' => 'Name 1',
172
			'description' => 'Description 1',
173
			'parent' => 0,
174
		) );
175
		$setting_id = "nav_menu[$menu_id]";
176
		$setting = new WP_Customize_Nav_Menu_Setting( $this->wp_customize, $setting_id );
177
178
		$nav_menu_options = $this->get_nav_menu_items_option();
179
		$this->assertNotContains( $menu_id, $nav_menu_options['auto_add'] );
180
181
		$post_value = array(
182
			'name' => 'Name 2',
183
			'description' => 'Description 2',
184
			'parent' => 1,
185
			'auto_add' => true,
186
		);
187
		$this->wp_customize->set_post_value( $setting_id, $post_value );
188
189
		$value = $setting->value();
190
		$this->assertEquals( 'Name 1', $value['name'] );
191
		$this->assertEquals( 'Description 1', $value['description'] );
192
		$this->assertEquals( 0, $value['parent'] );
193
194
		$term = (array) wp_get_nav_menu_object( $menu_id );
195
196
		$this->assertEqualSets(
197
			wp_array_slice_assoc( $value, array( 'name', 'description', 'parent' ) ),
198
			wp_array_slice_assoc( $term, array( 'name', 'description', 'parent' ) )
199
		);
200
201
		$setting->preview();
202
		$value = $setting->value();
203
		$this->assertEquals( 'Name 2', $value['name'] );
204
		$this->assertEquals( 'Description 2', $value['description'] );
205
		$this->assertEquals( 1, $value['parent'] );
206
		$term = (array) wp_get_nav_menu_object( $menu_id );
207
		$this->assertEqualSets( $value, wp_array_slice_assoc( $term, array_keys( $value ) ) );
208
209
		$menu_object = wp_get_nav_menu_object( $menu_id );
210
		$this->assertEquals( (object) $term, $menu_object );
211
		$this->assertEquals( $post_value['name'], $menu_object->name );
212
213
		$nav_menu_options = get_option( 'nav_menu_options', array( 'auto_add' => array() ) );
214
		$this->assertContains( $menu_id, $nav_menu_options['auto_add'] );
215
216
		$menus = wp_get_nav_menus();
217
		$menus_ids = wp_list_pluck( $menus, 'term_id' );
218
		$i = array_search( $menu_id, $menus_ids );
219
		$this->assertInternalType( 'int', $i, 'Update-previewed menu does not appear in wp_get_nav_menus()' );
220
		$filtered_menu = $menus[ $i ];
221
		$this->assertEquals( 'Name 2', $filtered_menu->name );
222
	}
223
224
	/**
225
	 * Test preview method for inserted menu.
226
	 *
227
	 * @see WP_Customize_Nav_Menu_Setting::preview()
228
	 */
229
	function test_preview_inserted() {
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...
230
		do_action( 'customize_register', $this->wp_customize );
231
232
		$menu_id = -123;
233
		$post_value = array(
234
			'name' => 'New Menu Name 1',
235
			'description' => 'New Menu Description 1',
236
			'parent' => 0,
237
			'auto_add' => false,
238
		);
239
		$setting_id = "nav_menu[$menu_id]";
240
		$setting = new WP_Customize_Nav_Menu_Setting( $this->wp_customize, $setting_id );
241
242
		$this->wp_customize->set_post_value( $setting->id, $post_value );
243
		$setting->preview();
244
		$value = $setting->value();
245
		$this->assertEquals( $post_value, $value );
246
247
		$term = (array) wp_get_nav_menu_object( $menu_id );
248
		$this->assertNotEmpty( $term );
249
		$this->assertNotInstanceOf( 'WP_Error', $term );
250
		$this->assertEqualSets( $post_value, wp_array_slice_assoc( $term, array_keys( $value ) ) );
251
		$this->assertEquals( $menu_id, $term['term_id'] );
252
		$this->assertEquals( $menu_id, $term['term_taxonomy_id'] );
253
254
		$menu_object = wp_get_nav_menu_object( $menu_id );
255
		$this->assertEquals( (object) $term, $menu_object );
256
		$this->assertEquals( $post_value['name'], $menu_object->name );
257
258
		$nav_menu_options = $this->get_nav_menu_items_option();
259
		$this->assertNotContains( $menu_id, $nav_menu_options['auto_add'] );
260
261
		$menus = wp_get_nav_menus();
262
		$menus_ids = wp_list_pluck( $menus, 'term_id' );
263
		$i = array_search( $menu_id, $menus_ids );
264
		$this->assertInternalType( 'int', $i, 'Insert-previewed menu was not injected into wp_get_nav_menus()' );
265
		$filtered_menu = $menus[ $i ];
266
		$this->assertEquals( 'New Menu Name 1', $filtered_menu->name );
267
	}
268
269
	/**
270
	 * Test preview method for deleted menu.
271
	 *
272
	 * @see WP_Customize_Nav_Menu_Setting::preview()
273
	 */
274
	function test_preview_deleted() {
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...
275
		do_action( 'customize_register', $this->wp_customize );
276
277
		$menu_id = wp_update_nav_menu_object( 0, array(
278
			'menu-name' => 'Name 1',
279
			'description' => 'Description 1',
280
			'parent' => 0,
281
		) );
282
		$setting_id = "nav_menu[$menu_id]";
283
		$setting = new WP_Customize_Nav_Menu_Setting( $this->wp_customize, $setting_id );
284
		$nav_menu_options = $this->get_nav_menu_items_option();
285
		$nav_menu_options['auto_add'][] = $menu_id;
286
		update_option( 'nav_menu_options', $nav_menu_options );
287
288
		$nav_menu_options = $this->get_nav_menu_items_option();
289
		$this->assertContains( $menu_id, $nav_menu_options['auto_add'] );
290
291
		$this->wp_customize->set_post_value( $setting_id, false );
292
293
		$this->assertInternalType( 'array', $setting->value() );
294
		$this->assertInternalType( 'object', wp_get_nav_menu_object( $menu_id ) );
295
		$setting->preview();
296
		$this->assertFalse( $setting->value() );
297
		$this->assertFalse( wp_get_nav_menu_object( $menu_id ) );
298
299
		$nav_menu_options = $this->get_nav_menu_items_option();
300
		$this->assertNotContains( $menu_id, $nav_menu_options['auto_add'] );
301
	}
302
303
	/**
304
	 * Test sanitize method.
305
	 *
306
	 * @see WP_Customize_Nav_Menu_Setting::sanitize()
307
	 */
308
	function test_sanitize() {
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...
309
		do_action( 'customize_register', $this->wp_customize );
310
		$setting = new WP_Customize_Nav_Menu_Setting( $this->wp_customize, 'nav_menu[123]' );
311
312
		$this->assertNull( $setting->sanitize( 'not an array' ) );
313
		$this->assertNull( $setting->sanitize( 123 ) );
314
315
		$value = array(
316
			'name' => ' Hello <b>world</b> ',
317
			'description' => "New\nline",
318
			'parent' => -12,
319
			'auto_add' => true,
320
			'extra' => 'ignored',
321
		);
322
		$sanitized = $setting->sanitize( $value );
323
		$this->assertEquals( 'Hello &lt;b&gt;world&lt;/b&gt;', $sanitized['name'] );
324
		$this->assertEquals( 'New line', $sanitized['description'] );
325
		$this->assertEquals( 0, $sanitized['parent'] );
326
		$this->assertEquals( true, $sanitized['auto_add'] );
327
		$this->assertEqualSets( array( 'name', 'description', 'parent', 'auto_add' ), array_keys( $sanitized ) );
328
329
		$value['name'] = '    '; // Blank spaces.
330
		$sanitized = $setting->sanitize( $value );
331
		$this->assertEquals( '(unnamed)', $sanitized['name'] );
332
	}
333
334
	/**
335
	 * Test protected update() method via the save() method, for updated menu.
336
	 *
337
	 * @see WP_Customize_Nav_Menu_Setting::update()
338
	 */
339
	function test_save_updated() {
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...
340
		do_action( 'customize_register', $this->wp_customize );
341
342
		$menu_id = wp_update_nav_menu_object( 0, array(
343
			'menu-name' => 'Name 1',
344
			'description' => 'Description 1',
345
			'parent' => 0,
346
		) );
347
		$nav_menu_options = $this->get_nav_menu_items_option();
348
		$nav_menu_options['auto_add'][] = $menu_id;
349
		update_option( 'nav_menu_options', $nav_menu_options );
350
351
		$setting_id = "nav_menu[$menu_id]";
352
		$setting = new WP_Customize_Nav_Menu_Setting( $this->wp_customize, $setting_id );
353
354
		$auto_add = false;
355
		$new_value = array(
356
			'name' => 'Name 2',
357
			'description' => 'Description 2',
358
			'parent' => 1,
359
			'auto_add' => $auto_add,
360
		);
361
362
		$this->wp_customize->set_post_value( $setting_id, $new_value );
363
		$setting->save();
364
365
		$menu_object = wp_get_nav_menu_object( $menu_id );
366
		foreach ( array( 'name', 'description', 'parent' ) as $key ) {
367
			$this->assertEquals( $new_value[ $key ], $menu_object->$key );
368
		}
369
		$this->assertEqualSets(
370
			wp_array_slice_assoc( $new_value, array( 'name', 'description', 'parent' ) ),
371
			wp_array_slice_assoc( (array) $menu_object, array( 'name', 'description', 'parent' ) )
372
		);
373
		$this->assertEquals( $new_value, $setting->value() );
374
375
		$save_response = apply_filters( 'customize_save_response', array() );
376
		$this->assertArrayHasKey( 'nav_menu_updates', $save_response );
377
		$update_result = array_shift( $save_response['nav_menu_updates'] );
378
		$this->assertArrayHasKey( 'term_id', $update_result );
379
		$this->assertArrayHasKey( 'previous_term_id', $update_result );
380
		$this->assertArrayHasKey( 'error', $update_result );
381
		$this->assertArrayHasKey( 'status', $update_result );
382
		$this->assertArrayHasKey( 'saved_value', $update_result );
383
		$this->assertEquals( $new_value, $update_result['saved_value'] );
384
385
		$this->assertEquals( $menu_id, $update_result['term_id'] );
386
		$this->assertNull( $update_result['previous_term_id'] );
387
		$this->assertNull( $update_result['error'] );
388
		$this->assertEquals( 'updated', $update_result['status'] );
389
390
		$nav_menu_options = $this->get_nav_menu_items_option();
391
		$this->assertNotContains( $menu_id, $nav_menu_options['auto_add'] );
392
	}
393
394
	/**
395
	 * Test protected update() method via the save() method, for inserted menu.
396
	 *
397
	 * @see WP_Customize_Nav_Menu_Setting::update()
398
	 */
399
	function test_save_inserted() {
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...
400
		do_action( 'customize_register', $this->wp_customize );
401
402
		$menu_id = -123;
403
		$post_value = array(
404
			'name' => 'New Menu Name 1',
405
			'description' => 'New Menu Description 1',
406
			'parent' => 0,
407
			'auto_add' => true,
408
		);
409
		$setting_id = "nav_menu[$menu_id]";
410
		$setting = new WP_Customize_Nav_Menu_Setting( $this->wp_customize, $setting_id );
411
412
		$this->wp_customize->set_post_value( $setting->id, $post_value );
413
414
		$this->assertNull( $setting->previous_term_id );
415
		$this->assertLessThan( 0, $setting->term_id );
416
		$setting->save();
417
		$this->assertEquals( $menu_id, $setting->previous_term_id );
418
		$this->assertGreaterThan( 0, $setting->term_id );
419
420
		$nav_menu_options = $this->get_nav_menu_items_option();
421
		$this->assertContains( $setting->term_id, $nav_menu_options['auto_add'] );
422
423
		$menu = wp_get_nav_menu_object( $setting->term_id );
424
		unset( $post_value['auto_add'] );
425
		$this->assertEqualSets( $post_value, wp_array_slice_assoc( (array) $menu, array_keys( $post_value ) ) );
426
427
		$save_response = apply_filters( 'customize_save_response', array() );
428
		$this->assertArrayHasKey( 'nav_menu_updates', $save_response );
429
		$update_result = array_shift( $save_response['nav_menu_updates'] );
430
		$this->assertArrayHasKey( 'term_id', $update_result );
431
		$this->assertArrayHasKey( 'previous_term_id', $update_result );
432
		$this->assertArrayHasKey( 'error', $update_result );
433
		$this->assertArrayHasKey( 'status', $update_result );
434
		$this->assertArrayHasKey( 'saved_value', $update_result );
435
		$this->assertEquals( $setting->value(), $update_result['saved_value'] );
436
437
		$this->assertEquals( $menu->term_id, $update_result['term_id'] );
438
		$this->assertEquals( $menu_id, $update_result['previous_term_id'] );
439
		$this->assertNull( $update_result['error'] );
440
		$this->assertEquals( 'inserted', $update_result['status'] );
441
	}
442
443
	/**
444
	 * Test saving a new name that conflicts with an existing nav menu's name.
445
	 *
446
	 * @see WP_Customize_Nav_Menu_Setting::update()
447
	 */
448
	function test_save_inserted_conflicted_name() {
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...
449
		do_action( 'customize_register', $this->wp_customize );
450
451
		$menu_name = 'Foo';
452
		wp_update_nav_menu_object( 0, array( 'menu-name' => $menu_name ) );
453
454
		$menu_id = -123;
455
		$setting_id = "nav_menu[$menu_id]";
456
		$setting = new WP_Customize_Nav_Menu_Setting( $this->wp_customize, $setting_id );
457
		$this->wp_customize->set_post_value( $setting->id, array( 'name' => $menu_name ) );
458
		$setting->save();
459
460
		$expected_resolved_menu_name = "$menu_name (2)";
461
		$new_menu = wp_get_nav_menu_object( $setting->term_id );
462
		$this->assertEquals( $expected_resolved_menu_name, $new_menu->name );
463
464
		$save_response = apply_filters( 'customize_save_response', array() );
465
		$this->assertEquals( $expected_resolved_menu_name, $save_response['nav_menu_updates'][0]['saved_value']['name'] );
466
	}
467
468
	/**
469
	 * Test protected update() method via the save() method, for deleted menu.
470
	 *
471
	 * @see WP_Customize_Nav_Menu_Setting::update()
472
	 */
473
	function test_save_deleted() {
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...
474
		do_action( 'customize_register', $this->wp_customize );
475
476
		$menu_name = 'Lorem Ipsum';
477
		$menu_id = wp_create_nav_menu( $menu_name );
478
		$setting_id = "nav_menu[$menu_id]";
479
		$setting = new WP_Customize_Nav_Menu_Setting( $this->wp_customize, $setting_id );
480
		$nav_menu_options = $this->get_nav_menu_items_option();
481
		$nav_menu_options['auto_add'][] = $menu_id;
482
		update_option( 'nav_menu_options', $nav_menu_options );
483
484
		$menu = wp_get_nav_menu_object( $menu_id );
485
		$this->assertEquals( $menu_name, $menu->name );
486
487
		$this->wp_customize->set_post_value( $setting_id, false );
488
		$setting->save();
489
490
		$this->assertFalse( wp_get_nav_menu_object( $menu_id ) );
491
492
		$save_response = apply_filters( 'customize_save_response', array() );
493
		$this->assertArrayHasKey( 'nav_menu_updates', $save_response );
494
		$update_result = array_shift( $save_response['nav_menu_updates'] );
495
		$this->assertArrayHasKey( 'term_id', $update_result );
496
		$this->assertArrayHasKey( 'previous_term_id', $update_result );
497
		$this->assertArrayHasKey( 'error', $update_result );
498
		$this->assertArrayHasKey( 'status', $update_result );
499
		$this->assertArrayHasKey( 'saved_value', $update_result );
500
		$this->assertNull( $update_result['saved_value'] );
501
502
		$this->assertEquals( $menu_id, $update_result['term_id'] );
503
		$this->assertNull( $update_result['previous_term_id'] );
504
		$this->assertNull( $update_result['error'] );
505
		$this->assertEquals( 'deleted', $update_result['status'] );
506
507
		$nav_menu_options = $this->get_nav_menu_items_option();
508
		$this->assertNotContains( $menu_id, $nav_menu_options['auto_add'] );
509
	}
510
511
}
512