Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Y
yii2bootstrap
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
夏彬
yii2bootstrap
Commits
234ccf6a
Commit
234ccf6a
authored
Nov 06, 2016
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Covered more cases for #162, #163. Better tests.
parent
81b6738a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
153 additions
and
6 deletions
+153
-6
Nav.php
Nav.php
+4
-3
NavTest.php
tests/NavTest.php
+149
-3
No files found.
Nav.php
View file @
234ccf6a
...
@@ -192,9 +192,7 @@ class Nav extends Widget
...
@@ -192,9 +192,7 @@ class Nav extends Widget
$label
.=
' '
.
$this
->
dropDownCaret
;
$label
.=
' '
.
$this
->
dropDownCaret
;
}
}
if
(
is_array
(
$items
))
{
if
(
is_array
(
$items
))
{
if
(
$this
->
activateItems
)
{
$items
=
$this
->
isChildActive
(
$items
,
$active
);
$items
=
$this
->
isChildActive
(
$items
,
$active
);
}
$items
=
$this
->
renderDropdown
(
$items
,
$item
);
$items
=
$this
->
renderDropdown
(
$items
,
$item
);
}
}
}
}
...
@@ -258,6 +256,9 @@ class Nav extends Widget
...
@@ -258,6 +256,9 @@ class Nav extends Widget
*/
*/
protected
function
isItemActive
(
$item
)
protected
function
isItemActive
(
$item
)
{
{
if
(
!
$this
->
activateItems
)
{
return
false
;
}
if
(
isset
(
$item
[
'url'
])
&&
is_array
(
$item
[
'url'
])
&&
isset
(
$item
[
'url'
][
0
]))
{
if
(
isset
(
$item
[
'url'
])
&&
is_array
(
$item
[
'url'
])
&&
isset
(
$item
[
'url'
][
0
]))
{
$route
=
$item
[
'url'
][
0
];
$route
=
$item
[
'url'
][
0
];
if
(
$route
[
0
]
!==
'/'
&&
Yii
::
$app
->
controller
)
{
if
(
$route
[
0
]
!==
'/'
&&
Yii
::
$app
->
controller
)
{
...
...
tests/NavTest.php
View file @
234ccf6a
<?php
<?php
namespace
yiiunit\extensions\bootstrap
;
namespace
yiiunit\extensions\bootstrap
;
use
yii\base\Action
;
use
yii\base\Module
;
use
yii\bootstrap\Nav
;
use
yii\bootstrap\Nav
;
use
yii\web\Controller
;
/**
/**
* Tests for Nav widget
* Tests for Nav widget
...
@@ -10,6 +13,26 @@ use yii\bootstrap\Nav;
...
@@ -10,6 +13,26 @@ use yii\bootstrap\Nav;
*/
*/
class
NavTest
extends
TestCase
class
NavTest
extends
TestCase
{
{
protected
function
setUp
()
{
$this
->
mockWebApplication
([
'components'
=>
[
'request'
=>
[
'class'
=>
'yii\web\Request'
,
'scriptUrl'
=>
'/base/index.php'
,
'hostInfo'
=>
'http://example.com/'
,
'url'
=>
'/base/index.php&r=site%2Fcurrent&id=42'
],
'urlManager'
=>
[
'class'
=>
'yii\web\UrlManager'
,
'baseUrl'
=>
'/base'
,
'scriptUrl'
=>
'/base/index.php'
,
'hostInfo'
=>
'http://example.com/'
,
]
],
]);
}
public
function
testIds
()
public
function
testIds
()
{
{
Nav
::
$counter
=
0
;
Nav
::
$counter
=
0
;
...
@@ -125,27 +148,150 @@ EXPECTED;
...
@@ -125,27 +148,150 @@ EXPECTED;
*/
*/
public
function
testExplicitActive
()
public
function
testExplicitActive
()
{
{
$this
->
mockAction
(
'site'
,
'index'
);
Nav
::
$counter
=
0
;
Nav
::
$counter
=
0
;
$out
=
Nav
::
widget
([
$out
=
Nav
::
widget
([
'activateItems'
=>
false
,
'activateItems'
=>
false
,
'items'
=>
[
'items'
=>
[
[
[
'label'
=>
'Item1'
,
'label'
=>
'Item1'
,
//'url' => ['some/route1'],
'active'
=>
true
,
'active'
=>
true
,
],
],
[
[
'label'
=>
'Item2'
,
'label'
=>
'Item2'
,
//'url' => ['some/route2'],
'url'
=>
[
'site/index'
],
],
],
]);
$expected
=
<<<EXPECTED
<ul id="w0" class="nav"><li class="active"><a href="#">Item1</a></li>
<li><a href="/base/index.php?r=site%2Findex">Item2</a></li></ul>
EXPECTED;
$this
->
assertEqualsWithoutLE
(
$expected
,
$out
);
$this
->
removeMockedAction
();
}
/**
* @see https://github.com/yiisoft/yii2-bootstrap/issues/162
*/
public
function
testImplicitActive
()
{
$this
->
mockAction
(
'site'
,
'index'
);
Nav
::
$counter
=
0
;
$out
=
Nav
::
widget
([
'items'
=>
[
[
'label'
=>
'Item1'
,
'active'
=>
true
,
],
[
'label'
=>
'Item2'
,
'url'
=>
[
'site/index'
],
],
],
],
],
]);
]);
$expected
=
<<<EXPECTED
$expected
=
<<<EXPECTED
<ul id="w0" class="nav"><li class="active"><a href="#">Item1</a></li>
<ul id="w0" class="nav"><li class="active"><a href="#">Item1</a></li>
<li><a href="#">Item2</a></li></ul>
<li class="active"><a href="/base/index.php?r=site%2Findex">Item2</a></li></ul>
EXPECTED;
$this
->
assertEqualsWithoutLE
(
$expected
,
$out
);
$this
->
removeMockedAction
();
}
/**
* @see https://github.com/yiisoft/yii2-bootstrap/issues/162
*/
public
function
testExplicitActiveSubitems
()
{
$this
->
mockAction
(
'site'
,
'index'
);
Nav
::
$counter
=
0
;
$out
=
Nav
::
widget
([
'activateItems'
=>
false
,
'items'
=>
[
[
'label'
=>
'Item1'
,
],
[
'label'
=>
'Item2'
,
'items'
=>
[
[
'label'
=>
'Page2'
,
'content'
=>
'Page2'
,
'url'
=>
[
'site/index'
]],
[
'label'
=>
'Page3'
,
'content'
=>
'Page3'
,
'active'
=>
true
],
],
],
],
]);
$expected
=
<<<EXPECTED
<ul id="w0" class="nav"><li><a href="#">Item1</a></li>
<li class="dropdown"><a class="dropdown-toggle" href="#" data-toggle="dropdown">Item2 <span class="caret"></span></a><ul id="w1" class="dropdown-menu"><li><a href="/base/index.php?r=site%2Findex" tabindex="-1">Page2</a></li>
<li class="active dropdown-header">Page3</li></ul></li></ul>
EXPECTED;
$this
->
assertEqualsWithoutLE
(
$expected
,
$out
);
$this
->
removeMockedAction
();
}
/**
* @see https://github.com/yiisoft/yii2-bootstrap/issues/162
*/
public
function
testImplicitActiveSubitems
()
{
$this
->
mockAction
(
'site'
,
'index'
);
Nav
::
$counter
=
0
;
$out
=
Nav
::
widget
([
'items'
=>
[
[
'label'
=>
'Item1'
,
],
[
'label'
=>
'Item2'
,
'items'
=>
[
[
'label'
=>
'Page2'
,
'content'
=>
'Page2'
,
'url'
=>
[
'site/index'
]],
[
'label'
=>
'Page3'
,
'content'
=>
'Page3'
,
'active'
=>
true
],
],
],
],
]);
$expected
=
<<<EXPECTED
<ul id="w0" class="nav"><li><a href="#">Item1</a></li>
<li class="dropdown"><a class="dropdown-toggle" href="#" data-toggle="dropdown">Item2 <span class="caret"></span></a><ul id="w1" class="dropdown-menu"><li class="active"><a href="/base/index.php?r=site%2Findex" tabindex="-1">Page2</a></li>
<li class="active dropdown-header">Page3</li></ul></li></ul>
EXPECTED;
EXPECTED;
$this
->
assertEqualsWithoutLE
(
$expected
,
$out
);
$this
->
assertEqualsWithoutLE
(
$expected
,
$out
);
$this
->
removeMockedAction
();
}
}
/**
* Mocks controller action with parameters
*
* @param string $controllerId
* @param string $actionID
* @param string $moduleID
* @param array $params
*/
protected
function
mockAction
(
$controllerId
,
$actionID
,
$moduleID
=
null
,
$params
=
[])
{
\Yii
::
$app
->
controller
=
$controller
=
new
Controller
(
$controllerId
,
\Yii
::
$app
);
$controller
->
actionParams
=
$params
;
$controller
->
action
=
new
Action
(
$actionID
,
$controller
);
if
(
$moduleID
!==
null
)
{
$controller
->
module
=
new
Module
(
$moduleID
);
}
}
protected
function
removeMockedAction
()
{
\Yii
::
$app
->
controller
=
null
;
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment