From 284dc9d02b36cf8891f84855e634a9e7f9c62bcd Mon Sep 17 00:00:00 2001 From: Max Tsepkov Date: Mon, 27 May 2013 21:42:11 +0600 Subject: [PATCH 1/4] View: correctly validate input in PartialLoop Change the condition to be more safe and intuitive. Closes #4545 --- library/Zend/View/Helper/PartialLoop.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/library/Zend/View/Helper/PartialLoop.php b/library/Zend/View/Helper/PartialLoop.php index 7b9f9f8201a..89be4e82f08 100644 --- a/library/Zend/View/Helper/PartialLoop.php +++ b/library/Zend/View/Helper/PartialLoop.php @@ -44,10 +44,10 @@ public function __invoke($name = null, $values = null) return $this; } - if (!is_array($values) - && (!$values instanceof Traversable) - && (is_object($values) && !method_exists($values, 'toArray')) - ) { + if (!( is_array($values) + || $values instanceof Traversable + || (is_object($values) && method_exists($values, 'toArray')) + )) { throw new Exception\InvalidArgumentException('PartialLoop helper requires iterable data'); } From 6d783a788e8fad44a6b0296e198a0bf2f2d601a6 Mon Sep 17 00:00:00 2001 From: Max Tsepkov Date: Mon, 27 May 2013 22:26:57 +0600 Subject: [PATCH 2/4] PartialLoop: rewrite input check even better --- library/Zend/View/Helper/PartialLoop.php | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/library/Zend/View/Helper/PartialLoop.php b/library/Zend/View/Helper/PartialLoop.php index 89be4e82f08..49408acddf6 100644 --- a/library/Zend/View/Helper/PartialLoop.php +++ b/library/Zend/View/Helper/PartialLoop.php @@ -44,22 +44,14 @@ public function __invoke($name = null, $values = null) return $this; } - if (!( is_array($values) - || $values instanceof Traversable - || (is_object($values) && method_exists($values, 'toArray')) - )) { - throw new Exception\InvalidArgumentException('PartialLoop helper requires iterable data'); - } - - if (is_object($values) - && (!$values instanceof Traversable) - && method_exists($values, 'toArray') - ) { - $values = $values->toArray(); - } - - if ($values instanceof Iterator) { + if (is_array($values)) { + // it's ok. do nothing + } else if ($values instanceof Traversable) { $values = ArrayUtils::iteratorToArray($values); + } else if (is_object($values) && method_exists($values, 'toArray')) { + $values = $values->toArray(); + } else { + throw new Exception\InvalidArgumentException('PartialLoop helper requires iterable data'); } // reset the counter if it's called again From 48686852f6be7d2473cacfd0fc86c72b1fb742e2 Mon Sep 17 00:00:00 2001 From: Max Tsepkov Date: Tue, 28 May 2013 02:47:21 +0600 Subject: [PATCH 3/4] PartialLoop: simplify check, remove comment --- library/Zend/View/Helper/PartialLoop.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/library/Zend/View/Helper/PartialLoop.php b/library/Zend/View/Helper/PartialLoop.php index 49408acddf6..7ea3b315ad5 100644 --- a/library/Zend/View/Helper/PartialLoop.php +++ b/library/Zend/View/Helper/PartialLoop.php @@ -44,14 +44,14 @@ public function __invoke($name = null, $values = null) return $this; } - if (is_array($values)) { - // it's ok. do nothing - } else if ($values instanceof Traversable) { - $values = ArrayUtils::iteratorToArray($values); - } else if (is_object($values) && method_exists($values, 'toArray')) { - $values = $values->toArray(); - } else { - throw new Exception\InvalidArgumentException('PartialLoop helper requires iterable data'); + if (!is_array($values)) { + if ($values instanceof Traversable) { + $values = ArrayUtils::iteratorToArray($values); + } elseif (is_object($values) && method_exists($values, 'toArray')) { + $values = $values->toArray(); + } else { + throw new Exception\InvalidArgumentException('PartialLoop helper requires iterable data'); + } } // reset the counter if it's called again From a9c4f0b5c28f5d26c203c5ee2aa9c8b046af6a9e Mon Sep 17 00:00:00 2001 From: Max Tsepkov Date: Sun, 9 Jun 2013 17:13:38 +0400 Subject: [PATCH 4/4] PartialLoop: unit test for null values --- tests/ZendTest/View/Helper/PartialLoopTest.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/ZendTest/View/Helper/PartialLoopTest.php b/tests/ZendTest/View/Helper/PartialLoopTest.php index cf3ad4b64cc..d73cff274eb 100644 --- a/tests/ZendTest/View/Helper/PartialLoopTest.php +++ b/tests/ZendTest/View/Helper/PartialLoopTest.php @@ -156,6 +156,22 @@ public function testPartialLoopThrowsExceptionWithBadIterator() } } + /** + * @return void + */ + public function testPassingNullDataThrowsExcpetion() + { + $view = new View(); + $view->resolver()->addPath($this->basePath . '/application/views/scripts'); + $this->helper->setView($view); + + try { + $result = $this->helper->__invoke('partialLoop.phtml', null); + $this->fail('PartialLoop should throw exception when passed null data.'); + } catch (\Zend\View\Exception\InvalidArgumentException $e) { + } + } + public function testPassingNoArgsReturnsHelperInstance() { $test = $this->helper->__invoke();