diff --git a/library/Zend/Navigation/AbstractContainer.php b/library/Zend/Navigation/AbstractContainer.php index ed3c213df1b..7572ccdb647 100644 --- a/library/Zend/Navigation/AbstractContainer.php +++ b/library/Zend/Navigation/AbstractContainer.php @@ -485,7 +485,7 @@ public function valid() */ public function hasChildren() { - return $this->hasPages(); + return ($this->valid() && $this->current()->hasPages()); } /** diff --git a/tests/ZendTest/Navigation/ContainerTest.php b/tests/ZendTest/Navigation/ContainerTest.php index b933d34ed4f..644b1e2ae3b 100644 --- a/tests/ZendTest/Navigation/ContainerTest.php +++ b/tests/ZendTest/Navigation/ContainerTest.php @@ -225,6 +225,133 @@ public function testRecursiveIteration() $this->assertEquals($expected, $actual); } + /** + * + * @see https://github.com/zendframework/zf2/issues/3211 + */ + public function testHasChildrenCompatibility() + { + $nav = new Navigation\Navigation(array( + array( + 'label' => 'Page 1', + 'uri' => '#', + 'pages' => array( + array( + 'label' => 'Page 1.1', + 'uri' => '#', + 'pages' => array( + array( + 'label' => 'Page 1.1.1', + 'uri' => '#' + ), + array( + 'label' => 'Page 1.1.2', + 'uri' => '#' + ) + ) + ), + array( + 'label' => 'Page 1.2', + 'uri' => '#' + ) + ) + ), + array( + 'label' => 'Page 2', + 'uri' => '#', + 'pages' => array( + array( + 'label' => 'Page 2.1', + 'uri' => '#' + ) + ) + ), + array( + 'label' => 'Page 3', + 'uri' => '#' + ) + )); + + $page1 = $nav->findOneBy('label', 'Page 1'); + $this->assertTrue($page1->hasChildren(), "page1's first child has children 1.1.1 1.1.2"); + + $page2 = $nav->findOneBy('label', 'Page 2'); + $this->assertFalse($page2->hasChildren(), "page2's first child doesn't have children"); + } + + public function testDetailedRecursiveIteration() + { + $nav = new Navigation\Navigation(array( + array( + 'label' => 'Page 1', + 'uri' => '#', + 'pages' => array( + array( + 'label' => 'Page 1.1', + 'uri' => '#', + 'pages' => array( + array( + 'label' => 'Page 1.1.1', + 'uri' => '#' + ), + array( + 'label' => 'Page 1.1.2', + 'uri' => '#' + ) + ) + ), + array( + 'label' => 'Page 1.2', + 'uri' => '#' + ) + ) + ), + array( + 'label' => 'Page 2', + 'uri' => '#', + 'pages' => array( + array( + 'label' => 'Page 2.1', + 'uri' => '#' + ) + ) + ), + array( + 'label' => 'Page 3', + 'uri' => '#' + ) + )); + + $expected = array( + 'beginIteration', + 'Page 1', + 'beginChildren', + 'Page 1.1', + 'beginChildren', + 'Page 1.1.1', + 'Page 1.1.2', + 'endChildren', + 'Page 1.2', + 'endChildren', + 'Page 2', + 'beginChildren', + 'Page 2.1', + 'endChildren', + 'Page 3', + 'endIteration', + ); + + $iterator = new TestAsset\RecursiveIteratorIterator($nav, \RecursiveIteratorIterator::SELF_FIRST); + $iterator->logger = array(); + $iterator->rewind(); + //#4517 logging with walking through RecursiveIterator + while ($iterator->valid()) { + $iterator->current(); + $iterator->next(); + } + $this->assertEquals($expected, $iterator->logger); + } + public function testSettingPageOrderShouldUpdateContainerOrder() { $nav = new Navigation\Navigation(array( diff --git a/tests/ZendTest/Navigation/TestAsset/RecursiveIteratorIterator.php b/tests/ZendTest/Navigation/TestAsset/RecursiveIteratorIterator.php new file mode 100644 index 00000000000..4265f366e24 --- /dev/null +++ b/tests/ZendTest/Navigation/TestAsset/RecursiveIteratorIterator.php @@ -0,0 +1,44 @@ +logger[] = 'beginIteration'; + } + + public function endIteration() + { + $this->logger[] = 'endIteration'; + } + + public function beginChildren() + { + $this->logger[] = 'beginChildren'; + } + + public function endChildren() + { + $this->logger[] = 'endChildren'; + } + + public function current() + { + $this->logger[] = parent::current()->getLabel(); + } +}