Skip to content

Commit

Permalink
test + fix
Browse files Browse the repository at this point in the history
  • Loading branch information
marcing committed Feb 23, 2024
1 parent 65b9409 commit c210df9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
19 changes: 17 additions & 2 deletions packages/zend-form/library/Zend/Form/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
}
Expand Down
48 changes: 45 additions & 3 deletions tests/Zend/Form/ElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit c210df9

Please sign in to comment.