1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Tests WP_Customize_Nav_Menus. |
5
|
|
|
* |
6
|
|
|
* @group customize |
7
|
|
|
*/ |
8
|
|
|
class Test_WP_Customize_Nav_Menus 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
|
|
|
function setUp() { |
|
|
|
|
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
|
|
|
global $wp_customize; |
28
|
|
|
$this->wp_customize = new WP_Customize_Manager(); |
29
|
|
|
$wp_customize = $this->wp_customize; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Delete the $wp_customize global when cleaning up scope. |
34
|
|
|
*/ |
35
|
|
|
function clean_up_global_scope() { |
|
|
|
|
36
|
|
|
global $wp_customize; |
37
|
|
|
$wp_customize = null; |
38
|
|
|
parent::clean_up_global_scope(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Filter to add custom menu item types. |
43
|
|
|
* |
44
|
|
|
* @param array $items Menu item types. |
45
|
|
|
* @return array Menu item types. |
46
|
|
|
*/ |
47
|
|
|
function filter_item_types( $items ) { |
|
|
|
|
48
|
|
|
$items[] = array( |
49
|
|
|
'title' => 'Custom', |
50
|
|
|
'type' => 'custom_type', |
51
|
|
|
'object' => 'custom_object', |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
return $items; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Filter to add custom menu items. |
59
|
|
|
* |
60
|
|
|
* @param array $items The menu items. |
61
|
|
|
* @param string $type The object type (e.g. taxonomy). |
62
|
|
|
* @param string $object The object name (e.g. category). |
63
|
|
|
* @return array Menu items. |
64
|
|
|
*/ |
65
|
|
|
function filter_items( $items, $type, $object ) { |
|
|
|
|
66
|
|
|
$items[] = array( |
67
|
|
|
'id' => 'custom-1', |
68
|
|
|
'title' => 'Cool beans', |
69
|
|
|
'type' => $type, |
70
|
|
|
'type_label' => 'Custom Label', |
71
|
|
|
'object' => $object, |
72
|
|
|
'url' => home_url( '/cool-beans/' ), |
73
|
|
|
'classes' => 'custom-menu-item cool-beans', |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
return $items; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Test constructor. |
81
|
|
|
* |
82
|
|
|
* @see WP_Customize_Nav_Menus::__construct() |
83
|
|
|
*/ |
84
|
|
|
function test_construct() { |
|
|
|
|
85
|
|
|
do_action( 'customize_register', $this->wp_customize ); |
86
|
|
|
$menus = new WP_Customize_Nav_Menus( $this->wp_customize ); |
87
|
|
|
$this->assertInstanceOf( 'WP_Customize_Manager', $menus->manager ); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Test that the load_available_items_query method returns a WP_Error object. |
92
|
|
|
* |
93
|
|
|
* @see WP_Customize_Nav_Menus::load_available_items_query() |
94
|
|
|
*/ |
95
|
|
|
function test_load_available_items_query_returns_wp_error() { |
|
|
|
|
96
|
|
|
$menus = new WP_Customize_Nav_Menus( $this->wp_customize ); |
97
|
|
|
|
98
|
|
|
// Invalid post type $obj_name. |
99
|
|
|
$items = $menus->load_available_items_query( 'post_type', 'invalid' ); |
100
|
|
|
$this->assertInstanceOf( 'WP_Error', $items ); |
101
|
|
|
$this->assertEquals( 'nav_menus_invalid_post_type', $items->get_error_code() ); |
102
|
|
|
|
103
|
|
|
// Invalid taxonomy $obj_name. |
104
|
|
|
$items = $menus->load_available_items_query( 'taxonomy', 'invalid' ); |
105
|
|
|
$this->assertInstanceOf( 'WP_Error', $items ); |
106
|
|
|
$this->assertEquals( 'invalid_taxonomy', $items->get_error_code() ); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Test the load_available_items_query method maybe returns the home page item. |
111
|
|
|
* |
112
|
|
|
* @see WP_Customize_Nav_Menus::load_available_items_query() |
113
|
|
|
*/ |
114
|
|
|
function test_load_available_items_query_maybe_returns_home() { |
|
|
|
|
115
|
|
|
$menus = new WP_Customize_Nav_Menus( $this->wp_customize ); |
116
|
|
|
|
117
|
|
|
// Expected menu item array. |
118
|
|
|
$expected = array( |
119
|
|
|
'id' => 'home', |
120
|
|
|
'title' => _x( 'Home', 'nav menu home label' ), |
121
|
|
|
'type' => 'custom', |
122
|
|
|
'type_label' => __( 'Custom Link' ), |
123
|
|
|
'object' => '', |
124
|
|
|
'url' => home_url(), |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
// Create pages. |
128
|
|
|
$this->factory->post->create_many( 15, array( 'post_type' => 'page' ) ); |
129
|
|
|
|
130
|
|
|
// Home is included in menu items when page is zero. |
131
|
|
|
$items = $menus->load_available_items_query( 'post_type', 'page', 0 ); |
132
|
|
|
$this->assertContains( $expected, $items ); |
133
|
|
|
|
134
|
|
|
// Home is not included in menu items when page is larger than zero. |
135
|
|
|
$items = $menus->load_available_items_query( 'post_type', 'page', 1 ); |
136
|
|
|
$this->assertNotEmpty( $items ); |
137
|
|
|
$this->assertNotContains( $expected, $items ); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Test the load_available_items_query method returns post item. |
142
|
|
|
* |
143
|
|
|
* @see WP_Customize_Nav_Menus::load_available_items_query() |
144
|
|
|
*/ |
145
|
|
View Code Duplication |
function test_load_available_items_query_returns_post_item_with_page_number() { |
|
|
|
|
146
|
|
|
$menus = new WP_Customize_Nav_Menus( $this->wp_customize ); |
147
|
|
|
|
148
|
|
|
// Create page. |
149
|
|
|
$post_id = $this->factory->post->create( array( 'post_title' => 'Post Title' ) ); |
150
|
|
|
|
151
|
|
|
// Create pages. |
152
|
|
|
$this->factory->post->create_many( 10 ); |
153
|
|
|
|
154
|
|
|
// Expected menu item array. |
155
|
|
|
$expected = array( |
156
|
|
|
'id' => "post-{$post_id}", |
157
|
|
|
'title' => 'Post Title', |
158
|
|
|
'type' => 'post_type', |
159
|
|
|
'type_label' => 'Post', |
160
|
|
|
'object' => 'post', |
161
|
|
|
'object_id' => intval( $post_id ), |
162
|
|
|
'url' => get_permalink( intval( $post_id ) ), |
163
|
|
|
); |
164
|
|
|
|
165
|
|
|
// Offset the query and get the second page of menu items. |
166
|
|
|
$items = $menus->load_available_items_query( 'post_type', 'post', 1 ); |
167
|
|
|
$this->assertContains( $expected, $items ); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Test the load_available_items_query method returns page item. |
172
|
|
|
* |
173
|
|
|
* @see WP_Customize_Nav_Menus::load_available_items_query() |
174
|
|
|
*/ |
175
|
|
View Code Duplication |
function test_load_available_items_query_returns_page_item() { |
|
|
|
|
176
|
|
|
$menus = new WP_Customize_Nav_Menus( $this->wp_customize ); |
177
|
|
|
|
178
|
|
|
// Create page. |
179
|
|
|
$page_id = $this->factory->post->create( array( 'post_title' => 'Page Title', 'post_type' => 'page' ) ); |
180
|
|
|
|
181
|
|
|
// Expected menu item array. |
182
|
|
|
$expected = array( |
183
|
|
|
'id' => "post-{$page_id}", |
184
|
|
|
'title' => 'Page Title', |
185
|
|
|
'type' => 'post_type', |
186
|
|
|
'type_label' => 'Page', |
187
|
|
|
'object' => 'page', |
188
|
|
|
'object_id' => intval( $page_id ), |
189
|
|
|
'url' => get_permalink( intval( $page_id ) ), |
190
|
|
|
); |
191
|
|
|
|
192
|
|
|
$items = $menus->load_available_items_query( 'post_type', 'page', 0 ); |
193
|
|
|
$this->assertContains( $expected, $items ); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Test the load_available_items_query method returns post item. |
198
|
|
|
* |
199
|
|
|
* @see WP_Customize_Nav_Menus::load_available_items_query() |
200
|
|
|
*/ |
201
|
|
View Code Duplication |
function test_load_available_items_query_returns_post_item() { |
|
|
|
|
202
|
|
|
$menus = new WP_Customize_Nav_Menus( $this->wp_customize ); |
203
|
|
|
|
204
|
|
|
// Create post. |
205
|
|
|
$post_id = $this->factory->post->create( array( 'post_title' => 'Post Title' ) ); |
206
|
|
|
|
207
|
|
|
// Expected menu item array. |
208
|
|
|
$expected = array( |
209
|
|
|
'id' => "post-{$post_id}", |
210
|
|
|
'title' => 'Post Title', |
211
|
|
|
'type' => 'post_type', |
212
|
|
|
'type_label' => 'Post', |
213
|
|
|
'object' => 'post', |
214
|
|
|
'object_id' => intval( $post_id ), |
215
|
|
|
'url' => get_permalink( intval( $post_id ) ), |
216
|
|
|
); |
217
|
|
|
|
218
|
|
|
$items = $menus->load_available_items_query( 'post_type', 'post', 0 ); |
219
|
|
|
$this->assertContains( $expected, $items ); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Test the load_available_items_query method returns term item. |
224
|
|
|
* |
225
|
|
|
* @see WP_Customize_Nav_Menus::load_available_items_query() |
226
|
|
|
*/ |
227
|
|
View Code Duplication |
function test_load_available_items_query_returns_term_item() { |
|
|
|
|
228
|
|
|
$menus = new WP_Customize_Nav_Menus( $this->wp_customize ); |
229
|
|
|
|
230
|
|
|
// Create term. |
231
|
|
|
$term_id = $this->factory->category->create( array( 'name' => 'Term Title' ) ); |
232
|
|
|
|
233
|
|
|
// Expected menu item array. |
234
|
|
|
$expected = array( |
235
|
|
|
'id' => "term-{$term_id}", |
236
|
|
|
'title' => 'Term Title', |
237
|
|
|
'type' => 'taxonomy', |
238
|
|
|
'type_label' => 'Category', |
239
|
|
|
'object' => 'category', |
240
|
|
|
'object_id' => intval( $term_id ), |
241
|
|
|
'url' => get_term_link( intval( $term_id ), 'category' ), |
242
|
|
|
); |
243
|
|
|
|
244
|
|
|
$items = $menus->load_available_items_query( 'taxonomy', 'category', 0 ); |
245
|
|
|
$this->assertContains( $expected, $items ); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Test the load_available_items_query method returns custom item. |
250
|
|
|
* |
251
|
|
|
* @see WP_Customize_Nav_Menus::load_available_items_query() |
252
|
|
|
*/ |
253
|
|
|
function test_load_available_items_query_returns_custom_item() { |
|
|
|
|
254
|
|
|
add_filter( 'customize_nav_menu_available_item_types', array( $this, 'filter_item_types' ) ); |
255
|
|
|
add_filter( 'customize_nav_menu_available_items', array( $this, 'filter_items' ), 10, 4 ); |
256
|
|
|
$menus = new WP_Customize_Nav_Menus( $this->wp_customize ); |
257
|
|
|
|
258
|
|
|
// Expected menu item array. |
259
|
|
|
$expected = array( |
260
|
|
|
'id' => 'custom-1', |
261
|
|
|
'title' => 'Cool beans', |
262
|
|
|
'type' => 'custom_type', |
263
|
|
|
'type_label' => 'Custom Label', |
264
|
|
|
'object' => 'custom_object', |
265
|
|
|
'url' => home_url( '/cool-beans/' ), |
266
|
|
|
'classes' => 'custom-menu-item cool-beans', |
267
|
|
|
); |
268
|
|
|
|
269
|
|
|
$items = $menus->load_available_items_query( 'custom_type', 'custom_object', 0 ); |
270
|
|
|
$this->assertContains( $expected, $items ); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Test the search_available_items_query method. |
275
|
|
|
* |
276
|
|
|
* @see WP_Customize_Nav_Menus::search_available_items_query() |
277
|
|
|
*/ |
278
|
|
|
function test_search_available_items_query() { |
|
|
|
|
279
|
|
|
$menus = new WP_Customize_Nav_Menus( $this->wp_customize ); |
280
|
|
|
|
281
|
|
|
// Create posts |
282
|
|
|
$post_ids = array(); |
283
|
|
|
$post_ids[] = $this->factory->post->create( array( 'post_title' => 'Search & Test' ) ); |
284
|
|
|
$post_ids[] = $this->factory->post->create( array( 'post_title' => 'Some Other Title' ) ); |
285
|
|
|
|
286
|
|
|
// Create terms |
287
|
|
|
$term_ids = array(); |
288
|
|
|
$term_ids[] = $this->factory->category->create( array( 'name' => 'Dogs Are Cool' ) ); |
289
|
|
|
$term_ids[] = $this->factory->category->create( array( 'name' => 'Cats Drool' ) ); |
290
|
|
|
|
291
|
|
|
// Test empty results |
292
|
|
|
$expected = array(); |
293
|
|
|
$results = $menus->search_available_items_query( array( 'pagenum' => 1, 's' => 'This Does NOT Exist' ) ); |
294
|
|
|
$this->assertEquals( $expected, $results ); |
295
|
|
|
|
296
|
|
|
// Test posts |
297
|
|
|
foreach ( $post_ids as $post_id ) { |
298
|
|
|
$expected = array( |
299
|
|
|
'id' => 'post-' . $post_id, |
300
|
|
|
'title' => html_entity_decode( get_the_title( $post_id ) ), |
301
|
|
|
'type' => 'post_type', |
302
|
|
|
'type_label' => get_post_type_object( 'post' )->labels->singular_name, |
303
|
|
|
'object' => 'post', |
304
|
|
|
'object_id' => intval( $post_id ), |
305
|
|
|
'url' => get_permalink( intval( $post_id ) ), |
306
|
|
|
); |
307
|
|
|
wp_set_object_terms( $post_id, $term_ids, 'category' ); |
308
|
|
|
$search = $post_id === $post_ids[0] ? 'test & search' : 'other title'; |
309
|
|
|
$s = sanitize_text_field( wp_unslash( $search ) ); |
310
|
|
|
$results = $menus->search_available_items_query( array( 'pagenum' => 1, 's' => $s ) ); |
311
|
|
|
$this->assertEquals( $expected, $results[0] ); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
// Test terms |
315
|
|
|
foreach ( $term_ids as $term_id ) { |
316
|
|
|
$term = get_term_by( 'id', $term_id, 'category' ); |
317
|
|
|
$expected = array( |
318
|
|
|
'id' => 'term-' . $term_id, |
319
|
|
|
'title' => $term->name, |
320
|
|
|
'type' => 'taxonomy', |
321
|
|
|
'type_label' => get_taxonomy( 'category' )->labels->singular_name, |
322
|
|
|
'object' => 'category', |
323
|
|
|
'object_id' => intval( $term_id ), |
324
|
|
|
'url' => get_term_link( intval( $term_id ), 'category' ), |
325
|
|
|
); |
326
|
|
|
$s = sanitize_text_field( wp_unslash( $term->name ) ); |
327
|
|
|
$results = $menus->search_available_items_query( array( 'pagenum' => 1, 's' => $s ) ); |
328
|
|
|
$this->assertEquals( $expected, $results[0] ); |
329
|
|
|
} |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Test the enqueue method. |
334
|
|
|
* |
335
|
|
|
* @see WP_Customize_Nav_Menus::enqueue_scripts() |
336
|
|
|
*/ |
337
|
|
|
function test_enqueue_scripts() { |
|
|
|
|
338
|
|
|
do_action( 'customize_register', $this->wp_customize ); |
339
|
|
|
$menus = new WP_Customize_Nav_Menus( $this->wp_customize ); |
340
|
|
|
$menus->enqueue_scripts(); |
341
|
|
|
$this->assertTrue( wp_script_is( 'customize-nav-menus' ) ); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Test the filter_dynamic_setting_args method. |
346
|
|
|
* |
347
|
|
|
* @see WP_Customize_Nav_Menus::filter_dynamic_setting_args() |
348
|
|
|
*/ |
349
|
|
|
function test_filter_dynamic_setting_args() { |
|
|
|
|
350
|
|
|
$menus = new WP_Customize_Nav_Menus( $this->wp_customize ); |
351
|
|
|
|
352
|
|
|
$expected = array( 'type' => 'nav_menu_item' ); |
353
|
|
|
$results = $menus->filter_dynamic_setting_args( $this->wp_customize, 'nav_menu_item[123]' ); |
354
|
|
|
$this->assertEquals( $expected, $results ); |
355
|
|
|
|
356
|
|
|
$expected = array( 'type' => 'nav_menu' ); |
357
|
|
|
$results = $menus->filter_dynamic_setting_args( $this->wp_customize, 'nav_menu[123]' ); |
358
|
|
|
$this->assertEquals( $expected, $results ); |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* Test the filter_dynamic_setting_class method. |
363
|
|
|
* |
364
|
|
|
* @see WP_Customize_Nav_Menus::filter_dynamic_setting_class() |
365
|
|
|
*/ |
366
|
|
|
function test_filter_dynamic_setting_class() { |
|
|
|
|
367
|
|
|
do_action( 'customize_register', $this->wp_customize ); |
368
|
|
|
$menus = new WP_Customize_Nav_Menus( $this->wp_customize ); |
369
|
|
|
|
370
|
|
|
$expected = 'WP_Customize_Nav_Menu_Item_Setting'; |
371
|
|
|
$results = $menus->filter_dynamic_setting_class( 'WP_Customize_Setting', 'nav_menu_item[123]', array( 'type' => 'nav_menu_item' ) ); |
372
|
|
|
$this->assertEquals( $expected, $results ); |
373
|
|
|
|
374
|
|
|
$expected = 'WP_Customize_Nav_Menu_Setting'; |
375
|
|
|
$results = $menus->filter_dynamic_setting_class( 'WP_Customize_Setting', 'nav_menu[123]', array( 'type' => 'nav_menu' ) ); |
376
|
|
|
$this->assertEquals( $expected, $results ); |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* Test the customize_register method. |
381
|
|
|
* |
382
|
|
|
* @see WP_Customize_Nav_Menus::customize_register() |
383
|
|
|
*/ |
384
|
|
|
function test_customize_register() { |
|
|
|
|
385
|
|
|
do_action( 'customize_register', $this->wp_customize ); |
386
|
|
|
$menu_id = wp_create_nav_menu( 'Primary' ); |
387
|
|
|
$post_id = $this->factory->post->create( array( 'post_title' => 'Hello World' ) ); |
388
|
|
|
$item_id = wp_update_nav_menu_item( $menu_id, 0, array( |
389
|
|
|
'menu-item-type' => 'post_type', |
390
|
|
|
'menu-item-object' => 'post', |
391
|
|
|
'menu-item-object-id' => $post_id, |
392
|
|
|
'menu-item-title' => 'Hello World', |
393
|
|
|
'menu-item-status' => 'publish', |
394
|
|
|
) ); |
395
|
|
|
$setting = new WP_Customize_Nav_Menu_Item_Setting( $this->wp_customize, "nav_menu_item[$item_id]" ); |
|
|
|
|
396
|
|
|
do_action( 'customize_register', $this->wp_customize ); |
397
|
|
|
$this->assertEquals( 'Primary', $this->wp_customize->get_section( "nav_menu[$menu_id]" )->title ); |
398
|
|
|
$this->assertEquals( 'Hello World', $this->wp_customize->get_control( "nav_menu_item[$item_id]" )->label ); |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* Test the intval_base10 method. |
403
|
|
|
* |
404
|
|
|
* @see WP_Customize_Nav_Menus::intval_base10() |
405
|
|
|
*/ |
406
|
|
|
function test_intval_base10() { |
|
|
|
|
407
|
|
|
|
408
|
|
|
$menus = new WP_Customize_Nav_Menus( $this->wp_customize ); |
409
|
|
|
|
410
|
|
|
$this->assertEquals( 2, $menus->intval_base10( 2 ) ); |
411
|
|
|
$this->assertEquals( 4, $menus->intval_base10( 4.1 ) ); |
412
|
|
|
$this->assertEquals( 4, $menus->intval_base10( '4' ) ); |
413
|
|
|
$this->assertEquals( 4, $menus->intval_base10( '04' ) ); |
414
|
|
|
$this->assertEquals( 42, $menus->intval_base10( +42 ) ); |
415
|
|
|
$this->assertEquals( -42, $menus->intval_base10( -42 ) ); |
416
|
|
|
$this->assertEquals( 26, $menus->intval_base10( 0x1A ) ); |
417
|
|
|
$this->assertEquals( 0, $menus->intval_base10( array() ) ); |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* Test the available_item_types method. |
422
|
|
|
* |
423
|
|
|
* @see WP_Customize_Nav_Menus::available_item_types() |
424
|
|
|
*/ |
425
|
|
|
function test_available_item_types() { |
|
|
|
|
426
|
|
|
|
427
|
|
|
$menus = new WP_Customize_Nav_Menus( $this->wp_customize ); |
428
|
|
|
|
429
|
|
|
$expected = array( |
430
|
|
|
array( 'title' => 'Posts', 'type' => 'post_type', 'object' => 'post' ), |
431
|
|
|
array( 'title' => 'Pages', 'type' => 'post_type', 'object' => 'page' ), |
432
|
|
|
array( 'title' => 'Categories', 'type' => 'taxonomy', 'object' => 'category' ), |
433
|
|
|
array( 'title' => 'Tags', 'type' => 'taxonomy', 'object' => 'post_tag' ), |
434
|
|
|
); |
435
|
|
|
|
436
|
|
|
if ( current_theme_supports( 'post-formats' ) ) { |
437
|
|
|
$expected[] = array( 'title' => 'Format', 'type' => 'taxonomy', 'object' => 'post_format' ); |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
$this->assertEquals( $expected, $menus->available_item_types() ); |
441
|
|
|
|
442
|
|
|
register_taxonomy( 'wptests_tax', array( 'post' ), array( 'labels' => array( 'name' => 'Foo' ) ) ); |
443
|
|
|
$expected[] = array( 'title' => 'Foo', 'type' => 'taxonomy', 'object' => 'wptests_tax' ); |
444
|
|
|
|
445
|
|
|
$this->assertEquals( $expected, $menus->available_item_types() ); |
446
|
|
|
|
447
|
|
|
$expected[] = array( 'title' => 'Custom', 'type' => 'custom_type', 'object' => 'custom_object' ); |
448
|
|
|
|
449
|
|
|
add_filter( 'customize_nav_menu_available_item_types', array( $this, 'filter_item_types' ) ); |
450
|
|
|
$this->assertEquals( $expected, $menus->available_item_types() ); |
451
|
|
|
remove_filter( 'customize_nav_menu_available_item_types', array( $this, 'filter_item_types' ) ); |
452
|
|
|
|
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
/** |
456
|
|
|
* Test the print_templates method. |
457
|
|
|
* |
458
|
|
|
* @see WP_Customize_Nav_Menus::print_templates() |
459
|
|
|
*/ |
460
|
|
|
function test_print_templates() { |
|
|
|
|
461
|
|
|
do_action( 'customize_register', $this->wp_customize ); |
462
|
|
|
$menus = new WP_Customize_Nav_Menus( $this->wp_customize ); |
463
|
|
|
|
464
|
|
|
ob_start(); |
465
|
|
|
$menus->print_templates(); |
466
|
|
|
$template = ob_get_clean(); |
467
|
|
|
|
468
|
|
|
$expected = sprintf( |
469
|
|
|
'<button type="button" class="menus-move-up">%1$s</button><button type="button" class="menus-move-down">%2$s</button><button type="button" class="menus-move-left">%3$s</button><button type="button" class="menus-move-right">%4$s</button>', |
470
|
|
|
esc_html( 'Move up' ), |
471
|
|
|
esc_html( 'Move down' ), |
472
|
|
|
esc_html( 'Move one level up' ), |
473
|
|
|
esc_html( 'Move one level down' ) |
474
|
|
|
); |
475
|
|
|
|
476
|
|
|
$this->assertContains( $expected, $template ); |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
/** |
480
|
|
|
* Test the available_items_template method. |
481
|
|
|
* |
482
|
|
|
* @see WP_Customize_Nav_Menus::available_items_template() |
483
|
|
|
*/ |
484
|
|
|
function test_available_items_template() { |
|
|
|
|
485
|
|
|
add_filter( 'customize_nav_menu_available_item_types', array( $this, 'filter_item_types' ) ); |
486
|
|
|
do_action( 'customize_register', $this->wp_customize ); |
487
|
|
|
$menus = new WP_Customize_Nav_Menus( $this->wp_customize ); |
488
|
|
|
|
489
|
|
|
ob_start(); |
490
|
|
|
$menus->available_items_template(); |
491
|
|
|
$template = ob_get_clean(); |
492
|
|
|
|
493
|
|
|
$expected = sprintf( 'Customizing ▸ %s', esc_html( $this->wp_customize->get_panel( 'nav_menus' )->title ) ); |
494
|
|
|
|
495
|
|
|
$this->assertContains( $expected, $template ); |
496
|
|
|
|
497
|
|
|
$post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'object' ); |
498
|
|
|
if ( $post_types ) { |
499
|
|
|
foreach ( $post_types as $type ) { |
500
|
|
|
$this->assertContains( 'available-menu-items-post_type-' . esc_attr( $type->name ), $template ); |
501
|
|
|
$this->assertRegExp( '#<h4 class="accordion-section-title".*>\s*' . esc_html( $type->labels->singular_name ) . '#', $template ); |
502
|
|
|
$this->assertContains( 'data-type="post_type"', $template ); |
503
|
|
|
$this->assertContains( 'data-object="' . esc_attr( $type->name ) . '"', $template ); |
504
|
|
|
} |
505
|
|
|
} |
506
|
|
|
|
507
|
|
|
$taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'object' ); |
508
|
|
|
if ( $taxonomies ) { |
509
|
|
|
foreach ( $taxonomies as $tax ) { |
510
|
|
|
$this->assertContains( 'available-menu-items-taxonomy-' . esc_attr( $tax->name ), $template ); |
511
|
|
|
$this->assertRegExp( '#<h4 class="accordion-section-title".*>\s*' . esc_html( $tax->labels->name ) . '#', $template ); |
512
|
|
|
$this->assertContains( 'data-type="taxonomy"', $template ); |
513
|
|
|
$this->assertContains( 'data-object="' . esc_attr( $tax->name ) . '"', $template ); |
514
|
|
|
} |
515
|
|
|
} |
516
|
|
|
|
517
|
|
|
$this->assertContains( 'available-menu-items-custom_type', $template ); |
518
|
|
|
$this->assertRegExp( '#<h4 class="accordion-section-title".*>\s*Custom#', $template ); |
519
|
|
|
$this->assertContains( 'data-type="custom_type"', $template ); |
520
|
|
|
$this->assertContains( 'data-object="custom_object"', $template ); |
521
|
|
|
} |
522
|
|
|
|
523
|
|
|
/** |
524
|
|
|
* Test the customize_preview_init method. |
525
|
|
|
* |
526
|
|
|
* @see WP_Customize_Nav_Menus::customize_preview_init() |
527
|
|
|
*/ |
528
|
|
|
function test_customize_preview_init() { |
|
|
|
|
529
|
|
|
do_action( 'customize_register', $this->wp_customize ); |
530
|
|
|
$menus = new WP_Customize_Nav_Menus( $this->wp_customize ); |
531
|
|
|
|
532
|
|
|
$menus->customize_preview_init(); |
533
|
|
|
$this->assertEquals( 10, has_action( 'template_redirect', array( $menus, 'render_menu' ) ) ); |
534
|
|
|
$this->assertEquals( 10, has_action( 'wp_enqueue_scripts', array( $menus, 'customize_preview_enqueue_deps' ) ) ); |
535
|
|
|
|
536
|
|
|
if ( ! isset( $_REQUEST[ WP_Customize_Nav_Menus::RENDER_QUERY_VAR ] ) ) { |
537
|
|
|
$this->assertEquals( 1000, has_filter( 'wp_nav_menu_args', array( $menus, 'filter_wp_nav_menu_args' ) ) ); |
538
|
|
|
$this->assertEquals( 10, has_filter( 'wp_nav_menu', array( $menus, 'filter_wp_nav_menu' ) ) ); |
539
|
|
|
} |
540
|
|
|
} |
541
|
|
|
|
542
|
|
|
/** |
543
|
|
|
* Test the filter_wp_nav_menu_args method. |
544
|
|
|
* |
545
|
|
|
* @see WP_Customize_Nav_Menus::filter_wp_nav_menu_args() |
546
|
|
|
*/ |
547
|
|
|
function test_filter_wp_nav_menu_args() { |
|
|
|
|
548
|
|
|
do_action( 'customize_register', $this->wp_customize ); |
549
|
|
|
$menus = new WP_Customize_Nav_Menus( $this->wp_customize ); |
550
|
|
|
|
551
|
|
|
$results = $menus->filter_wp_nav_menu_args( array( |
552
|
|
|
'echo' => true, |
553
|
|
|
'fallback_cb' => 'wp_page_menu', |
554
|
|
|
'walker' => '', |
555
|
|
|
'menu' => wp_create_nav_menu( 'Foo' ), |
556
|
|
|
) ); |
557
|
|
|
$this->assertEquals( 1, $results['can_partial_refresh'] ); |
558
|
|
|
|
559
|
|
|
$expected = array( |
560
|
|
|
'echo', |
561
|
|
|
'can_partial_refresh', |
562
|
|
|
'fallback_cb', |
563
|
|
|
'instance_number', |
564
|
|
|
'walker', |
565
|
|
|
); |
566
|
|
|
$results = $menus->filter_wp_nav_menu_args( array( |
567
|
|
|
'echo' => false, |
568
|
|
|
'fallback_cb' => 'wp_page_menu', |
569
|
|
|
'walker' => new Walker_Nav_Menu(), |
570
|
|
|
) ); |
571
|
|
|
$this->assertEqualSets( $expected, array_keys( $results ) ); |
572
|
|
|
$this->assertEquals( 'wp_page_menu', $results['fallback_cb'] ); |
573
|
|
|
$this->assertEquals( 0, $results['can_partial_refresh'] ); |
574
|
|
|
|
575
|
|
|
$this->assertNotEmpty( $menus->preview_nav_menu_instance_args[ $results['instance_number'] ] ); |
576
|
|
|
$preview_nav_menu_instance_args = $menus->preview_nav_menu_instance_args[ $results['instance_number'] ]; |
577
|
|
|
$this->assertEquals( '', $preview_nav_menu_instance_args['fallback_cb'] ); |
578
|
|
|
$this->assertEquals( '', $preview_nav_menu_instance_args['walker'] ); |
579
|
|
|
$this->assertNotEmpty( $preview_nav_menu_instance_args['args_hash'] ); |
580
|
|
|
} |
581
|
|
|
|
582
|
|
|
/** |
583
|
|
|
* Test the filter_wp_nav_menu method. |
584
|
|
|
* |
585
|
|
|
* @see WP_Customize_Nav_Menus::filter_wp_nav_menu() |
586
|
|
|
*/ |
587
|
|
|
function test_filter_wp_nav_menu() { |
|
|
|
|
588
|
|
|
do_action( 'customize_register', $this->wp_customize ); |
589
|
|
|
$menus = new WP_Customize_Nav_Menus( $this->wp_customize ); |
590
|
|
|
|
591
|
|
|
$args = $menus->filter_wp_nav_menu_args( array( |
592
|
|
|
'echo' => true, |
593
|
|
|
'menu' => wp_create_nav_menu( 'Foo' ), |
594
|
|
|
'fallback_cb' => 'wp_page_menu', |
595
|
|
|
'walker' => '', |
596
|
|
|
) ); |
597
|
|
|
|
598
|
|
|
ob_start(); |
599
|
|
|
wp_nav_menu( $args ); |
600
|
|
|
$nav_menu_content = ob_get_clean(); |
601
|
|
|
|
602
|
|
|
$object_args = json_decode( json_encode( $args ), false ); |
603
|
|
|
$result = $menus->filter_wp_nav_menu( $nav_menu_content, $object_args ); |
604
|
|
|
$expected = sprintf( |
605
|
|
|
'<div class="partial-refreshable-nav-menu partial-refreshable-nav-menu-%1$d menu">', |
606
|
|
|
$args['instance_number'] |
607
|
|
|
); |
608
|
|
|
$this->assertStringStartsWith( $expected, $result ); |
609
|
|
|
} |
610
|
|
|
|
611
|
|
|
/** |
612
|
|
|
* Test the customize_preview_enqueue_deps method. |
613
|
|
|
* |
614
|
|
|
* @see WP_Customize_Nav_Menus::customize_preview_enqueue_deps() |
615
|
|
|
*/ |
616
|
|
|
function test_customize_preview_enqueue_deps() { |
|
|
|
|
617
|
|
|
do_action( 'customize_register', $this->wp_customize ); |
618
|
|
|
$menus = new WP_Customize_Nav_Menus( $this->wp_customize ); |
619
|
|
|
|
620
|
|
|
$menus->customize_preview_enqueue_deps(); |
621
|
|
|
|
622
|
|
|
$this->assertTrue( wp_script_is( 'customize-preview-nav-menus' ) ); |
623
|
|
|
$this->assertEquals( 10, has_action( 'wp_print_footer_scripts', array( $menus, 'export_preview_data' ) ) ); |
624
|
|
|
} |
625
|
|
|
|
626
|
|
|
/** |
627
|
|
|
* Test the export_preview_data method. |
628
|
|
|
* |
629
|
|
|
* @see WP_Customize_Nav_Menus::export_preview_data() |
630
|
|
|
*/ |
631
|
|
|
function test_export_preview_data() { |
|
|
|
|
632
|
|
|
do_action( 'customize_register', $this->wp_customize ); |
633
|
|
|
$menus = new WP_Customize_Nav_Menus( $this->wp_customize ); |
634
|
|
|
|
635
|
|
|
$request_uri = $_SERVER['REQUEST_URI']; |
636
|
|
|
|
637
|
|
|
ob_start(); |
638
|
|
|
$_SERVER['REQUEST_URI'] = '/wp-admin'; |
639
|
|
|
$menus->export_preview_data(); |
640
|
|
|
$data = ob_get_clean(); |
641
|
|
|
|
642
|
|
|
$_SERVER['REQUEST_URI'] = $request_uri; |
643
|
|
|
|
644
|
|
|
$this->assertContains( '_wpCustomizePreviewNavMenusExports', $data ); |
645
|
|
|
$this->assertContains( 'renderQueryVar', $data ); |
646
|
|
|
$this->assertContains( 'renderNonceValue', $data ); |
647
|
|
|
$this->assertContains( 'renderNoncePostKey', $data ); |
648
|
|
|
$this->assertContains( 'requestUri', $data ); |
649
|
|
|
$this->assertContains( 'theme', $data ); |
650
|
|
|
$this->assertContains( 'previewCustomizeNonce', $data ); |
651
|
|
|
$this->assertContains( 'navMenuInstanceArgs', $data ); |
652
|
|
|
$this->assertContains( 'requestUri', $data ); |
653
|
|
|
|
654
|
|
|
} |
655
|
|
|
|
656
|
|
|
} |
657
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.