1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Yiisoft\Widget\Tests; |
5
|
|
|
|
6
|
|
|
use Yiisoft\Tests\TestCase; |
7
|
|
|
use Yiisoft\Widget\Menu; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* MenuTest. |
11
|
|
|
*/ |
12
|
|
|
class MenuTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
public function testEncodeLabelTrue(): void |
15
|
|
|
{ |
16
|
|
|
ob_start(); |
17
|
|
|
ob_implicit_flush(0); |
18
|
|
|
|
19
|
|
|
echo Menu::widget() |
20
|
|
|
->encodeLabels(true) |
21
|
|
|
->items([ |
22
|
|
|
[ |
23
|
|
|
'encode' => false, |
24
|
|
|
'label' => '<span class="glyphicon glyphicon-user"></span> Users', |
25
|
|
|
'url' => '#', |
26
|
|
|
], |
27
|
|
|
[ |
28
|
|
|
'encode' => true, |
29
|
|
|
'label' => 'Authors & Publications', |
30
|
|
|
'url' => '#', |
31
|
|
|
], |
32
|
|
|
]); |
33
|
|
|
|
34
|
|
|
$expected = <<<'HTML' |
35
|
|
|
<ul><li><a href="#"><span class="glyphicon glyphicon-user"></span> Users</a></li> |
36
|
|
|
<li><a href="#">Authors & Publications</a></li></ul> |
37
|
|
|
HTML; |
38
|
|
|
$this->assertEqualsWithoutLE($expected, ob_get_clean()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testEncodeLabelFalse(): void |
42
|
|
|
{ |
43
|
|
|
ob_start(); |
44
|
|
|
ob_implicit_flush(0); |
45
|
|
|
|
46
|
|
|
echo Menu::widget() |
47
|
|
|
->encodeLabels(false) |
48
|
|
|
->items([ |
49
|
|
|
[ |
50
|
|
|
'encode' => false, |
51
|
|
|
'label' => '<span class="glyphicon glyphicon-user"></span> Users', |
52
|
|
|
'url' => '#', |
53
|
|
|
], |
54
|
|
|
[ |
55
|
|
|
'encode' => true, |
56
|
|
|
'label' => 'Authors & Publications', |
57
|
|
|
'url' => '#', |
58
|
|
|
], |
59
|
|
|
]); |
60
|
|
|
|
61
|
|
|
$expected = <<<'HTML' |
62
|
|
|
<ul><li><a href="#"><span class="glyphicon glyphicon-user"></span> Users</a></li> |
63
|
|
|
<li><a href="#">Authors & Publications</a></li></ul> |
64
|
|
|
HTML; |
65
|
|
|
$this->assertEqualsWithoutLE($expected, ob_get_clean()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @see https://github.com/yiisoft/yii2/issues/8064 |
70
|
|
|
*/ |
71
|
|
|
public function testTagOption(): void |
72
|
|
|
{ |
73
|
|
|
ob_start(); |
74
|
|
|
ob_implicit_flush(0); |
75
|
|
|
|
76
|
|
|
echo Menu::widget() |
77
|
|
|
->encodeLabels(true) |
78
|
|
|
->options([ |
79
|
|
|
'tag' => false, |
80
|
|
|
]) |
81
|
|
|
->items([ |
82
|
|
|
[ |
83
|
|
|
'label' => 'item1', |
84
|
|
|
'url' => '#', |
85
|
|
|
'options' => ['tag' => 'div'], |
86
|
|
|
], |
87
|
|
|
[ |
88
|
|
|
'label' => 'item2', |
89
|
|
|
'url' => '#', |
90
|
|
|
'options' => ['tag' => false], |
91
|
|
|
], |
92
|
|
|
]); |
93
|
|
|
|
94
|
|
|
$expected = <<<'HTML' |
95
|
|
|
<div><a href="#">item1</a></div> |
96
|
|
|
<a href="#">item2</a> |
97
|
|
|
HTML; |
98
|
|
|
|
99
|
|
|
$this->assertEqualsWithoutLE($expected, ob_get_clean()); |
100
|
|
|
|
101
|
|
|
ob_start(); |
102
|
|
|
ob_implicit_flush(0); |
103
|
|
|
|
104
|
|
|
echo Menu::widget() |
105
|
|
|
->encodeLabels(true) |
106
|
|
|
->options([ |
107
|
|
|
'tag' => false, |
108
|
|
|
]) |
109
|
|
|
->items([ |
110
|
|
|
[ |
111
|
|
|
'label' => 'item1', |
112
|
|
|
'url' => '#', |
113
|
|
|
], |
114
|
|
|
[ |
115
|
|
|
'label' => 'item2', |
116
|
|
|
'url' => '#', |
117
|
|
|
], |
118
|
|
|
]) |
119
|
|
|
->itemOptions(['tag' => false]); |
120
|
|
|
|
121
|
|
|
$expected = <<<'HTML' |
122
|
|
|
<a href="#">item1</a> |
123
|
|
|
<a href="#">item2</a> |
124
|
|
|
HTML; |
125
|
|
|
$this->assertEqualsWithoutLE($expected, ob_get_clean()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function testItemTemplate(): void |
129
|
|
|
{ |
130
|
|
|
ob_start(); |
131
|
|
|
ob_implicit_flush(0); |
132
|
|
|
|
133
|
|
|
echo Menu::widget() |
134
|
|
|
->labelTemplate('') |
135
|
|
|
->linkTemplate('') |
136
|
|
|
->items([ |
137
|
|
|
[ |
138
|
|
|
'label' => 'item1', |
139
|
|
|
'url' => '#', |
140
|
|
|
'template' => 'label: {label}; url: {url}', |
141
|
|
|
], |
142
|
|
|
[ |
143
|
|
|
'label' => 'item2', |
144
|
|
|
'template' => 'label: {label}', |
145
|
|
|
], |
146
|
|
|
[ |
147
|
|
|
'label' => 'item3 (no template)', |
148
|
|
|
], |
149
|
|
|
]); |
150
|
|
|
|
151
|
|
|
$expected = <<<'HTML' |
152
|
|
|
<ul><li>label: item1; url: #</li> |
153
|
|
|
<li>label: item2</li> |
154
|
|
|
<li></li></ul> |
155
|
|
|
HTML; |
156
|
|
|
$this->assertEqualsWithoutLE($expected, ob_get_clean()); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function testActiveItemClosure(): void |
160
|
|
|
{ |
161
|
|
|
ob_start(); |
162
|
|
|
ob_implicit_flush(0); |
163
|
|
|
|
164
|
|
|
echo Menu::widget() |
165
|
|
|
->linkTemplate('') |
166
|
|
|
->labelTemplate('') |
167
|
|
|
->items([ |
168
|
|
|
[ |
169
|
|
|
'label' => 'item1', |
170
|
|
|
'url' => '#', |
171
|
|
|
'template' => 'label: {label}; url: {url}', |
172
|
|
|
'active' => function ($item, $hasActiveChild, $isItemActive, $widget) { |
173
|
|
|
return isset($item, $hasActiveChild, $isItemActive, $widget); |
174
|
|
|
}, |
175
|
|
|
], |
176
|
|
|
[ |
177
|
|
|
'label' => 'item2', |
178
|
|
|
'template' => 'label: {label}', |
179
|
|
|
'active' => false, |
180
|
|
|
], |
181
|
|
|
[ |
182
|
|
|
'label' => 'item3 (no template)', |
183
|
|
|
'active' => 'somestring', |
184
|
|
|
], |
185
|
|
|
]); |
186
|
|
|
|
187
|
|
|
$expected = <<<'HTML' |
188
|
|
|
<ul><li class="active">label: item1; url: #</li> |
189
|
|
|
<li>label: item2</li> |
190
|
|
|
<li class="active"></li></ul> |
191
|
|
|
HTML; |
192
|
|
|
$this->assertEqualsWithoutLE($expected, ob_get_clean()); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function testItemClassAsArray(): void |
196
|
|
|
{ |
197
|
|
|
ob_start(); |
198
|
|
|
ob_implicit_flush(0); |
199
|
|
|
|
200
|
|
|
echo Menu::widget() |
201
|
|
|
->encodeLabels(true) |
202
|
|
|
->activeCssClass('item-active') |
203
|
|
|
->items([ |
204
|
|
|
[ |
205
|
|
|
'label' => 'item1', |
206
|
|
|
'url' => '#', |
207
|
|
|
'active' => true, |
208
|
|
|
'options' => [ |
209
|
|
|
'class' => [ |
210
|
|
|
'someclass', |
211
|
|
|
], |
212
|
|
|
], |
213
|
|
|
], |
214
|
|
|
[ |
215
|
|
|
'label' => 'item2', |
216
|
|
|
'url' => '#', |
217
|
|
|
'options' => [ |
218
|
|
|
'class' => [ |
219
|
|
|
'another-class', |
220
|
|
|
'other--class', |
221
|
|
|
'two classes', |
222
|
|
|
], |
223
|
|
|
], |
224
|
|
|
], |
225
|
|
|
[ |
226
|
|
|
'label' => 'item3', |
227
|
|
|
'url' => '#', |
228
|
|
|
], |
229
|
|
|
[ |
230
|
|
|
'label' => 'item4', |
231
|
|
|
'url' => '#', |
232
|
|
|
'options' => [ |
233
|
|
|
'class' => [ |
234
|
|
|
'some-other-class', |
235
|
|
|
'foo_bar_baz_class', |
236
|
|
|
], |
237
|
|
|
], |
238
|
|
|
], |
239
|
|
|
]); |
240
|
|
|
|
241
|
|
|
$expected = <<<'HTML' |
242
|
|
|
<ul><li class="someclass item-active"><a href="#">item1</a></li> |
243
|
|
|
<li class="another-class other--class two classes"><a href="#">item2</a></li> |
244
|
|
|
<li><a href="#">item3</a></li> |
245
|
|
|
<li class="some-other-class foo_bar_baz_class"><a href="#">item4</a></li></ul> |
246
|
|
|
HTML; |
247
|
|
|
$this->assertEqualsWithoutLE($expected, ob_get_clean()); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
public function testItemClassAsString(): void |
251
|
|
|
{ |
252
|
|
|
ob_start(); |
253
|
|
|
ob_implicit_flush(0); |
254
|
|
|
|
255
|
|
|
echo Menu::widget() |
256
|
|
|
->encodeLabels(true) |
257
|
|
|
->activeCssClass('item-active') |
258
|
|
|
->items([ |
259
|
|
|
[ |
260
|
|
|
'label' => 'item1', |
261
|
|
|
'url' => '#', |
262
|
|
|
'options' => [ |
263
|
|
|
'class' => 'someclass', |
264
|
|
|
], |
265
|
|
|
], |
266
|
|
|
[ |
267
|
|
|
'label' => 'item2', |
268
|
|
|
'url' => '#', |
269
|
|
|
], |
270
|
|
|
[ |
271
|
|
|
'label' => 'item3', |
272
|
|
|
'url' => '#', |
273
|
|
|
'options' => [ |
274
|
|
|
'class' => 'some classes', |
275
|
|
|
], |
276
|
|
|
], |
277
|
|
|
[ |
278
|
|
|
'label' => 'item4', |
279
|
|
|
'url' => '#', |
280
|
|
|
'active' => true, |
281
|
|
|
'options' => [ |
282
|
|
|
'class' => 'another-class other--class two classes', |
283
|
|
|
], |
284
|
|
|
], |
285
|
|
|
]); |
286
|
|
|
|
287
|
|
|
$expected = <<<'HTML' |
288
|
|
|
<ul><li class="someclass"><a href="#">item1</a></li> |
289
|
|
|
<li><a href="#">item2</a></li> |
290
|
|
|
<li class="some classes"><a href="#">item3</a></li> |
291
|
|
|
<li class="another-class other--class two classes item-active"><a href="#">item4</a></li></ul> |
292
|
|
|
HTML; |
293
|
|
|
$this->assertEqualsWithoutLE($expected, ob_get_clean()); |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|