forked from timber/timber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-menu-item-factory.php
186 lines (164 loc) · 5.68 KB
/
test-menu-item-factory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
use Timber\Factory\MenuItemFactory;
use Timber\Menu;
use Timber\MenuItem;
use Timber\Timber;
class MyMenuItem extends MenuItem
{
}
/**
* @group factory
* @group menus-api
*/
class TestMenuItemFactory extends Timber_UnitTestCase
{
public function testMenuFromId()
{
// Destructure the result into the menu WP_Term instance
// and the *first* item_id
[
'term' => $menu_term,
'item_ids' => [$item_id],
] = $this->create_menu_from_posts([
[
'post_title' => 'Home',
'post_status' => 'publish',
'post_name' => 'home',
'post_type' => 'page',
'menu_order' => 1,
],
]);
$menu = Timber::get_menu($menu_term['term_id']);
$factory = new MenuItemFactory();
$this->assertInstanceOf(MenuItem::class, $factory->from($item_id, $menu));
}
public function testMenuGarbageInGarbageOut()
{
[
'term' => $menu_term,
'item_ids' => [$item_id],
] = $this->create_menu_from_posts([
[
'post_title' => 'Home',
'post_status' => 'publish',
'post_name' => 'home',
'post_type' => 'page',
'menu_order' => 1,
],
]);
$menu = Timber::get_menu($menu_term['term_id']);
$factory = new MenuItemFactory();
$this->assertNull($factory->from(null, $menu));
$this->assertNull($factory->from(23442, $menu));
}
public function testMenuFromPost()
{
// Destructure the result into the menu WP_Term instance
// and the *first* item_id
[
'term' => $menu_term,
'item_ids' => [$item_id],
] = $this->create_menu_from_posts([
[
'post_title' => 'Home',
'post_status' => 'publish',
'post_name' => 'home',
'post_type' => 'page',
'menu_order' => 1,
],
]);
$menu = Timber::get_menu($menu_term['term_id']);
$factory = new MenuItemFactory();
$post = get_post($item_id);
$this->assertInstanceOf(MenuItem::class, $factory->from($post, $menu));
}
public function testMenuItemClass()
{
// Destructure the result into the menu WP_Term instance
// and the item_ids
[
'term' => $menu_term,
'item_ids' => [$one, $two, $three],
] = $this->create_menu_from_posts([
[
'post_title' => 'Page One',
'post_status' => 'publish',
'post_name' => 'page-one',
'post_type' => 'page',
'menu_order' => 1,
],
[
'post_title' => 'Page Two',
'post_status' => 'publish',
'post_name' => 'page-two',
'post_type' => 'page',
'menu_order' => 2,
],
[
'post_title' => 'Page Three',
'post_status' => 'publish',
'post_name' => 'page-three',
'post_type' => 'page',
'menu_order' => 3,
],
]);
$menu = Timber::get_menu($menu_term['term_id']);
$factory = new MenuItemFactory();
$this->add_filter_temporarily('timber/menuitem/class', function ($class, WP_Post $item, Menu $menu) use ($two) {
if ($item->ID === $two) {
return MyMenuItem::class;
}
return $class;
}, 10, 3);
$this->assertTrue(MenuItem::class === get_class($factory->from($one, $menu)));
$this->assertTrue(MyMenuItem::class === get_class($factory->from($two, $menu)));
$this->assertTrue(MenuItem::class === get_class($factory->from($three, $menu)));
}
public function testMenuItemClassmap()
{
// Destructure the result into the menu WP_Term instance
// and the item_ids
[
'term' => $menu_term,
'item_ids' => [$one, $two, $three],
] = $this->create_menu_from_posts([
[
'post_title' => 'Page One',
'post_status' => 'publish',
'post_name' => 'page-one',
'post_type' => 'page',
'menu_order' => 1,
],
[
'post_title' => 'Page Two',
'post_status' => 'publish',
'post_name' => 'page-two',
'post_type' => 'page',
'menu_order' => 2,
],
[
'post_title' => 'Page Three',
'post_status' => 'publish',
'post_name' => 'page-three',
'post_type' => 'page',
'menu_order' => 3,
],
]);
register_nav_menu('custom', 'Custom nav location');
set_theme_mod('nav_menu_locations', [
'custom' => $menu_term['term_id'],
]);
$menu = Timber::get_menu($menu_term['term_id']);
$factory = new MenuItemFactory();
$this->add_filter_temporarily('timber/menuitem/classmap', function () {
return [
'custom' => MyMenuItem::class,
];
});
// Don't use instanceOf, it will return true whether it's MyMenuItem or MenuItem class
// and thus does not properly checks the classmap
$this->assertTrue(MyMenuItem::class === get_class($factory->from($one, $menu)));
$this->assertTrue(MyMenuItem::class === get_class($factory->from($two, $menu)));
$this->assertTrue(MyMenuItem::class === get_class($factory->from($three, $menu)));
}
}