Skip to content

Commit 69882e6

Browse files
committed
Tests: Cover navigation menu conditionals.
Add coverage for `is_nav_menu()`, `has_nav_menu()`, and `is_nav_menu_item()`, including menu identification, location assignments, filters, invalid inputs, and trashed menu items. Preserve menu registrations and theme support across each test. Developed in: #14354 Props ravipatel, mukesh27. See #65819. git-svn-id: https://develop.svn.wordpress.org/trunk@64614 602fd350-edb4-49c9-b593-d223f7449a82
1 parent ff0b79c commit 69882e6

3 files changed

Lines changed: 423 additions & 0 deletions

File tree

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
3+
/**
4+
* Tests for the has_nav_menu() function.
5+
*
6+
* @group menu
7+
*
8+
* @covers ::has_nav_menu
9+
*/
10+
class Tests_Menu_HasNavMenu extends WP_UnitTestCase {
11+
12+
/**
13+
* Menu ID used by the tests.
14+
*
15+
* @var int
16+
*/
17+
private static $menu_id;
18+
19+
/**
20+
* Menu locations registered before the test.
21+
*
22+
* @var array
23+
*/
24+
private $original_nav_menus;
25+
26+
/**
27+
* Theme features before the test: register_nav_menu() adds menu support.
28+
*
29+
* @var array
30+
*/
31+
private $original_theme_features;
32+
33+
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
34+
self::$menu_id = wp_create_nav_menu( 'Primary Menu' );
35+
}
36+
37+
public function set_up() {
38+
global $_wp_registered_nav_menus, $_wp_theme_features;
39+
40+
parent::set_up();
41+
42+
$this->original_nav_menus = $_wp_registered_nav_menus;
43+
$this->original_theme_features = $_wp_theme_features;
44+
45+
register_nav_menu( 'primary', 'Primary' );
46+
}
47+
48+
public function tear_down() {
49+
global $_wp_registered_nav_menus, $_wp_theme_features;
50+
51+
try {
52+
parent::tear_down();
53+
} finally {
54+
// Restore the registrations and theme support exactly as they were,
55+
// including any this test overwrote or added.
56+
$_wp_registered_nav_menus = $this->original_nav_menus;
57+
$_wp_theme_features = $this->original_theme_features;
58+
}
59+
}
60+
61+
/**
62+
* Tests that a registered location with a menu assigned has a menu.
63+
*
64+
* @ticket 65819
65+
*/
66+
public function test_should_return_true_for_a_registered_location_with_a_menu() {
67+
set_theme_mod( 'nav_menu_locations', array( 'primary' => self::$menu_id ) );
68+
69+
$this->assertTrue( has_nav_menu( 'primary' ) );
70+
}
71+
72+
/**
73+
* Tests that a registered location without a menu assigned has no menu.
74+
*
75+
* @ticket 65819
76+
*/
77+
public function test_should_return_false_for_a_registered_location_without_a_menu() {
78+
$this->assertFalse( has_nav_menu( 'primary' ) );
79+
}
80+
81+
/**
82+
* Tests that a location set to 0, as when its menu is unassigned, has no menu.
83+
*
84+
* @ticket 65819
85+
*/
86+
public function test_should_return_false_for_a_location_whose_menu_was_unassigned() {
87+
set_theme_mod( 'nav_menu_locations', array( 'primary' => 0 ) );
88+
89+
$this->assertFalse( has_nav_menu( 'primary' ) );
90+
}
91+
92+
/**
93+
* Tests that an assignment is ignored when its location is not registered,
94+
* such as one left behind by a previously active theme.
95+
*
96+
* @ticket 65819
97+
*/
98+
public function test_should_return_false_for_an_assigned_location_that_is_not_registered() {
99+
set_theme_mod( 'nav_menu_locations', array( 'old-theme-location' => self::$menu_id ) );
100+
101+
$this->assertFalse( has_nav_menu( 'old-theme-location' ) );
102+
}
103+
104+
/**
105+
* Tests that an unknown location has no menu.
106+
*
107+
* @ticket 65819
108+
*/
109+
public function test_should_return_false_for_an_unknown_location() {
110+
$this->assertFalse( has_nav_menu( 'no-such-location' ) );
111+
}
112+
113+
/**
114+
* Tests that each location is checked on its own.
115+
*
116+
* @ticket 65819
117+
*/
118+
public function test_should_check_each_location_separately() {
119+
register_nav_menu( 'footer', 'Footer' );
120+
set_theme_mod( 'nav_menu_locations', array( 'footer' => self::$menu_id ) );
121+
122+
$this->assertTrue( has_nav_menu( 'footer' ), 'The footer location should have a menu.' );
123+
$this->assertFalse( has_nav_menu( 'primary' ), 'The primary location should not have a menu.' );
124+
}
125+
126+
/**
127+
* Tests that the `has_nav_menu` filter receives the result and the location,
128+
* and that its return value is used.
129+
*
130+
* @ticket 65819
131+
*/
132+
public function test_should_apply_the_has_nav_menu_filter() {
133+
$filter = new MockAction();
134+
add_filter( 'has_nav_menu', array( $filter, 'filter' ), 10, 2 );
135+
add_filter( 'has_nav_menu', '__return_true', 11 );
136+
137+
$this->assertTrue( has_nav_menu( 'primary' ), 'The filtered value should be returned.' );
138+
$this->assertSame( 1, $filter->get_call_count(), 'The filter should run once.' );
139+
$this->assertSame( array( false, 'primary' ), $filter->get_args()[0], 'The filter should receive the unfiltered result and the location.' );
140+
}
141+
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
3+
/**
4+
* Tests for the is_nav_menu() function.
5+
*
6+
* @group menu
7+
*
8+
* @covers ::is_nav_menu
9+
*/
10+
class Tests_Menu_IsNavMenu extends WP_UnitTestCase {
11+
12+
/**
13+
* Menu term used by the tests.
14+
*
15+
* @var WP_Term
16+
*/
17+
private static $menu;
18+
19+
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
20+
$menu_id = wp_create_nav_menu( 'Primary Menu' );
21+
self::$menu = get_term( $menu_id, 'nav_menu' );
22+
}
23+
24+
/**
25+
* Tests that a menu is found by its ID, slug, name, or term object.
26+
*
27+
* @ticket 65819
28+
*
29+
* @dataProvider data_ways_to_identify_a_menu
30+
*
31+
* @param string $field The menu field to identify the menu by.
32+
*/
33+
public function test_should_return_true_for_an_existing_menu( $field ) {
34+
$menu = 'object' === $field ? self::$menu : self::$menu->$field;
35+
36+
$this->assertTrue( is_nav_menu( $menu ) );
37+
}
38+
39+
/**
40+
* Data provider.
41+
*
42+
* @return array[]
43+
*/
44+
public static function data_ways_to_identify_a_menu() {
45+
return array(
46+
'ID' => array( 'term_id' ),
47+
'slug' => array( 'slug' ),
48+
'name' => array( 'name' ),
49+
'term object' => array( 'object' ),
50+
);
51+
}
52+
53+
/**
54+
* Tests that a menu ID passed as a numeric string is found.
55+
*
56+
* @ticket 65819
57+
*/
58+
public function test_should_return_true_for_a_numeric_string_id() {
59+
$this->assertTrue( is_nav_menu( (string) self::$menu->term_id ) );
60+
}
61+
62+
/**
63+
* Tests that empty values return false.
64+
*
65+
* @ticket 65819
66+
*
67+
* @dataProvider data_empty_values
68+
*
69+
* @param mixed $menu An empty value.
70+
*/
71+
public function test_should_return_false_for_an_empty_value( $menu ) {
72+
$this->assertFalse( is_nav_menu( $menu ) );
73+
}
74+
75+
/**
76+
* Data provider.
77+
*
78+
* @return array[]
79+
*/
80+
public static function data_empty_values() {
81+
return array(
82+
'zero' => array( 0 ),
83+
'string zero' => array( '0' ),
84+
'empty string' => array( '' ),
85+
'null' => array( null ),
86+
'false' => array( false ),
87+
);
88+
}
89+
90+
/**
91+
* Tests that values that do not match a menu return false.
92+
*
93+
* @ticket 65819
94+
*
95+
* @dataProvider data_unknown_menus
96+
*
97+
* @param int|string $menu A value that does not identify a menu.
98+
*/
99+
public function test_should_return_false_for_an_unknown_menu( $menu ) {
100+
$this->assertFalse( is_nav_menu( $menu ) );
101+
}
102+
103+
/**
104+
* Data provider.
105+
*
106+
* @return array[]
107+
*/
108+
public static function data_unknown_menus() {
109+
return array(
110+
'ID' => array( PHP_INT_MAX ),
111+
'slug' => array( 'no-such-menu' ),
112+
'name' => array( 'No Such Menu' ),
113+
);
114+
}
115+
116+
/**
117+
* Tests that a term from another taxonomy is not a menu, whether it is
118+
* passed by ID, slug, or as a term object.
119+
*
120+
* @ticket 65819
121+
*/
122+
public function test_should_return_false_for_a_term_from_another_taxonomy() {
123+
$category = self::factory()->category->create_and_get( array( 'slug' => 'not-a-menu' ) );
124+
125+
$this->assertFalse( is_nav_menu( $category->term_id ), 'A category ID should not be a menu.' );
126+
$this->assertFalse( is_nav_menu( $category->slug ), 'A category slug should not be a menu.' );
127+
$this->assertFalse( is_nav_menu( $category ), 'A category term object should not be a menu.' );
128+
}
129+
130+
/**
131+
* Tests that a menu is no longer found once it is deleted.
132+
*
133+
* @ticket 65819
134+
*/
135+
public function test_should_return_false_for_a_deleted_menu() {
136+
$menu_id = wp_create_nav_menu( 'Temporary Menu' );
137+
wp_delete_nav_menu( $menu_id );
138+
139+
$this->assertFalse( is_nav_menu( $menu_id ) );
140+
}
141+
142+
/**
143+
* Tests that the result follows the `wp_get_nav_menu_object` filter.
144+
*
145+
* @ticket 65819
146+
*/
147+
public function test_should_return_false_when_the_menu_object_is_filtered_out() {
148+
add_filter( 'wp_get_nav_menu_object', '__return_false' );
149+
150+
$this->assertFalse( is_nav_menu( self::$menu->term_id ) );
151+
}
152+
}

0 commit comments

Comments
 (0)