mockWebApplication([
'components'=>[
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
],
],
]);
}
public function testEncodeLabel(): void
{
$output = Menu::widget([
'route' => 'test/test',
'params' => [],
'encodeLabels' => true,
'items' => [
[
'encode' => false,
'label' => ' Users',
'url' => '#',
],
[
'encode' => true,
'label' => 'Authors & Publications',
'url' => '#',
],
],
]);
$expected = <<<'HTML'
HTML;
$this->assertEqualsWithoutLE($expected, $output);
$output = Menu::widget([
'route' => 'test/test',
'params' => [],
'encodeLabels' => false,
'items' => [
[
'encode' => false,
'label' => ' Users',
'url' => '#',
],
[
'encode' => true,
'label' => 'Authors & Publications',
'url' => '#',
],
],
]);
$expected = <<<'HTML'
HTML;
$this->assertEqualsWithoutLE($expected, $output);
}
/**
* @see https://github.com/yiisoft/yii2/issues/8064
*/
public function testTagOption(): void
{
$output = Menu::widget([
'route' => 'test/test',
'params' => [],
'encodeLabels' => true,
'options' => [
'tag' => false,
],
'items' => [
[
'label' => 'item1',
'url' => '#',
'options' => ['tag' => 'div'],
],
[
'label' => 'item2',
'url' => '#',
'options' => ['tag' => false],
],
],
]);
$expected = <<<'HTML'
item2
HTML;
$this->assertEqualsWithoutLE($expected, $output);
$output = Menu::widget([
'route' => 'test/test',
'params' => [],
'encodeLabels' => true,
'options' => [
'tag' => false,
],
'items' => [
[
'label' => 'item1',
'url' => '#',
],
[
'label' => 'item2',
'url' => '#',
],
],
'itemOptions' => ['tag' => false],
]);
$expected = <<<'HTML'
item1
item2
HTML;
$this->assertEqualsWithoutLE($expected, $output);
}
public function testItemTemplate(): void
{
$output = Menu::widget([
'route' => 'test/test',
'params' => [],
'linkTemplate' => '',
'labelTemplate' => '',
'items' => [
[
'label' => 'item1',
'url' => '#',
'template' => 'label: {label}; url: {url}',
],
[
'label' => 'item2',
'template' => 'label: {label}',
],
[
'label' => 'item3 (no template)',
],
],
]);
$expected = <<<'HTML'
- label: item1; url: #
- label: item2
HTML;
$this->assertEqualsWithoutLE($expected, $output);
}
public function testActiveItemClosure(): void
{
$output = Menu::widget([
'route' => 'test/test',
'params' => [],
'linkTemplate' => '',
'labelTemplate' => '',
'items' => [
[
'label' => 'item1',
'url' => '#',
'template' => 'label: {label}; url: {url}',
'active' => fn($item, $hasActiveChild, $isItemActive, $widget) => isset($item, $hasActiveChild, $isItemActive, $widget),
],
[
'label' => 'item2',
'template' => 'label: {label}',
'active' => false,
],
[
'label' => 'item3 (no template)',
'active' => 'somestring',
],
],
]);
$expected = <<<'HTML'
- label: item1; url: #
- label: item2
HTML;
$this->assertEqualsWithoutLE($expected, $output);
}
public function testActiveItemClosureWithLogic(): void
{
$output = Menu::widget([
'route' => 'test/logic',
'params' => [],
'linkTemplate' => '',
'labelTemplate' => '',
'items' => [
[
'label' => 'logic item',
'url' => 'test/logic',
'template' => 'label: {label}; url: {url}',
'active' => fn($item, $hasActiveChild, $isItemActive, $widget) => $widget->route === 'test/logic',
],
[
'label' => 'another item',
'url' => 'test/another',
'template' => 'label: {label}; url: {url}',
]
],
]);
$expected = <<<'HTML'
- label: logic item; url: test/logic
- label: another item; url: test/another
HTML;
$this->assertEqualsWithoutLE($expected, $output);
}
public function testActiveItemClosureWithLogicParent(): void
{
$output = Menu::widget([
'route' => 'test/logic',
'params' => [],
'linkTemplate' => '',
'labelTemplate' => '',
'activateParents' => true,
'items' => [
[
'label' => 'Home',
'url' => 'test/home',
'template' => 'label: {label}; url: {url}',
],
[
'label' => 'About',
'url' => 'test/about',
'template' => 'label: {label}; url: {url}',
],
[
'label' => 'Parent',
'items' => [
[
'label' => 'logic item',
'url' => 'test/logic',
'template' => 'label: {label}; url: {url}',
'active' => fn($item, $hasActiveChild, $isItemActive, $widget) => $widget->route === 'test/logic',
],
[
'label' => 'another item',
'url' => 'test/another',
'template' => 'label: {label}; url: {url}',
]
],
],
],
]);
$expected = <<<'HTML'
- label: Home; url: test/home
- label: About; url: test/about
-
- label: logic item; url: test/logic
- label: another item; url: test/another
HTML;
$this->assertEqualsWithoutLE($expected, $output);
}
public function testActiveItemClosureParentAnotherItem(): void
{
/** @see https://github.com/yiisoft/yii2/issues/19060 */
$output = Menu::widget([
'route' => 'test/another',
'params' => [],
'linkTemplate' => '',
'labelTemplate' => '',
'activateParents' => true,
'items' => [
[
'label' => 'Home',
'url' => 'test/home',
'template' => 'label: {label}; url: {url}',
],
[
'label' => 'About',
'url' => 'test/about',
'template' => 'label: {label}; url: {url}',
],
[
'label' => 'Parent',
'items' => [
[
'label' => 'another item',
// use non relative route to avoid error in BaseUrl::normalizeRoute (missing controller)
'url' => ['/test/another'],
'template' => 'label: {label}; url: {url}',
],
[
'label' => 'logic item',
'url' => 'test/logic',
'template' => 'label: {label}; url: {url}',
'active' => fn($item, $hasActiveChild, $isItemActive, $widget) => $widget->route === 'test/logic',
],
],
],
],
]);
$expected = <<<'HTML'
- label: Home; url: test/home
- label: About; url: test/about
-
- label: another item; url: /test/another
- label: logic item; url: test/logic
HTML;
$this->assertEqualsWithoutLE($expected, $output);
}
public function testItemClassAsArray(): void
{
$output = Menu::widget([
'route' => 'test/test',
'params' => [],
'encodeLabels' => true,
'activeCssClass' => 'item-active',
'items' => [
[
'label' => 'item1',
'url' => '#',
'active' => true,
'options' => [
'class' => [
'someclass',
],
],
],
[
'label' => 'item2',
'url' => '#',
'options' => [
'class' => [
'another-class',
'other--class',
'two classes',
],
],
],
[
'label' => 'item3',
'url' => '#',
],
[
'label' => 'item4',
'url' => '#',
'options' => [
'class' => [
'some-other-class',
'foo_bar_baz_class',
],
],
],
],
]);
$expected = <<<'HTML'
HTML;
$this->assertEqualsWithoutLE($expected, $output);
}
public function testItemClassAsString(): void
{
$output = Menu::widget([
'route' => 'test/test',
'params' => [],
'encodeLabels' => true,
'activeCssClass' => 'item-active',
'items' => [
[
'label' => 'item1',
'url' => '#',
'options' => [
'class' => 'someclass',
],
],
[
'label' => 'item2',
'url' => '#',
],
[
'label' => 'item3',
'url' => '#',
'options' => [
'class' => 'some classes',
],
],
[
'label' => 'item4',
'url' => '#',
'active' => true,
'options' => [
'class' => 'another-class other--class two classes',
],
],
],
]);
$expected = <<<'HTML'
HTML;
$this->assertEqualsWithoutLE($expected, $output);
}
public function testIsItemActive(): void
{
$output = Menu::widget([
'route' => 'test/item2',
'params' => [
'page'=>'5',
],
'items' => [
[
'label' => 'item1',
'url' => ['/test/item1']
],
[
'label' => 'item2',
// use non relative route to avoid error in BaseUrl::normalizeRoute (missing controller)
'url' => ['/test/item2','page'=>'5']
],
],
]);
$expected = <<<'HTML'
HTML;
$this->assertEqualsWithoutLE($expected, $output);
}
}