From c210df9cfb78e1c07dfb162856eaa0a43f199cd5 Mon Sep 17 00:00:00 2001 From: Marcin Gil Date: Fri, 23 Feb 2024 17:38:21 +0100 Subject: [PATCH] test + fix --- .../zend-form/library/Zend/Form/Element.php | 19 +++++++- tests/Zend/Form/ElementTest.php | 48 +++++++++++++++++-- 2 files changed, 62 insertions(+), 5 deletions(-) diff --git a/packages/zend-form/library/Zend/Form/Element.php b/packages/zend-form/library/Zend/Form/Element.php index b1f7c17fa..939e1bbe1 100644 --- a/packages/zend-form/library/Zend/Form/Element.php +++ b/packages/zend-form/library/Zend/Form/Element.php @@ -1743,7 +1743,7 @@ public function getFilter($name) if (0 === substr_compare($localName, $name, -$len, $len, true)) { if (is_array($filter)) { - return $this->_loadFilter($filter); + return $this->_loadValidator($filter); } return $filter; } @@ -2113,7 +2113,22 @@ protected function _loadFilter(array $filter) } else { $r = new ReflectionClass($name); if ($r->hasMethod('__construct')) { - $instance = $r->newInstanceArgs((array) $filter['options']); + $numeric = false; + if (is_array($filter['options'])) { + $keys = array_keys($filter['options']); + foreach($keys as $key) { + if (is_numeric($key)) { + $numeric = true; + break; + } + } + } + + if ($numeric) { + $instance = $r->newInstanceArgs((array) $filter['options']); + } else { + $instance = $r->newInstance($filter['options']); + } } else { $instance = $r->newInstance(); } diff --git a/tests/Zend/Form/ElementTest.php b/tests/Zend/Form/ElementTest.php index 1eadcc4d6..8d395dfa4 100644 --- a/tests/Zend/Form/ElementTest.php +++ b/tests/Zend/Form/ElementTest.php @@ -1226,15 +1226,57 @@ public function testOptionsAreCastToArrayWhenAddingFilter() $this->assertTrue($filter->allowWhiteSpace); } - public function testShouldUseFilterConstructorOptionsAsPassedToAddFilter() + public function testCanPassFirstAndSecondArgument() { - $this->element->addFilter('HtmlEntities', array(array('quotestyle' => ENT_QUOTES, 'charset' => 'UTF-8'))); + try { + // public function __construct($searchSeparator = ' ', $replacementSeparator = '-') + $this->element->addFilter('Word_SeparatorToSeparator', array(array('-', '_'))); + } catch (Exception $e) { + $this->fail('Should be able to add array filter options'); + } + $filter = $this->element->getFilter('SeparatorToSeparator'); + $this->assertEquals('-', $filter->getSearchSeparator()); + $this->assertEquals('_', $filter->getReplacementSeparator()); + } + + public function testCanPassFirstOptionAsFirstArgument() + { + try { + // public function __construct($options = null) + $this->element->addFilter('Boolean', array('type' => Zend_Filter_Boolean::PHP + Zend_Filter_Boolean::FALSE_STRING)); + } catch (Exception $e) { + $this->fail('Should be able to add array filter options'); + } + $filter = $this->element->getFilter('Boolean'); + $this->assertTrue($filter instanceof Zend_Filter_Boolean); + $this->assertSame($filter->getType(), Zend_Filter_Boolean::PHP + Zend_Filter_Boolean::FALSE_STRING); + } + + public function testCanPassMultipleOptionsAsAnAssociativeArray() + { + // public function __construct($options = array()) + $this->element->addFilter('HtmlEntities', array(array('quotestyle' => ENT_QUOTES, 'charset' => 'MacRoman'))); $filter = $this->element->getFilter('HtmlEntities'); $this->assertTrue($filter instanceof Zend_Filter_HtmlEntities); $this->assertEquals(ENT_QUOTES, $filter->getQuoteStyle()); - $this->assertEquals('UTF-8', $filter->getCharSet()); + $this->assertEquals('MacRoman', $filter->getCharSet()); } + public function testAnyOptionIsHandledByConstructorWhenPassedInOptionsArray() + { + try { + // pass an option, which is not the same as first argument, and it's value is not the same as default one + // to check if it's really set + // public function __construct($options = array()) + $this->element->addFilter('HtmlEntities', array(array('encoding' => 'MacRoman'))); + } catch (Exception $e) { + $this->fail('Should be able to add array filter options'); + } + $filter = $this->element->getFilter('HtmlEntities'); + $this->assertTrue($filter instanceof Zend_Filter_HtmlEntities); + $this->assertSame($filter->getEncoding(), 'MacRoman'); + } + public function testCanAddMultipleFilters() { $this->_checkZf2794();