Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Fix failing tests in Zend\InputFilter
Browse files Browse the repository at this point in the history
- Cleaned up BaseInputFilterTest to use a data provider; makes isolating failing
  cases simpler.
- Rewrote logic in validateInputs() to capture discrete use cases; more verbose,
  but easier to determine what is happening.
- Made executive decision: non-required input that is empty is valid.
  • Loading branch information
weierophinney committed Mar 26, 2013
1 parent a4055c6 commit ef30a66
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 25 deletions.
103 changes: 82 additions & 21 deletions src/BaseInputFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,37 +169,96 @@ protected function validateInputs(array $inputs)
$valid = true;

foreach ($inputs as $name) {
$input = $this->inputs[$name];
// Check for missing data on non-required input
if ((!array_key_exists($name, $this->data)
|| (null === $this->data[$name]))
$input = $this->inputs[$name];
$dataExists = array_key_exists($name, $this->data);

// key doesn't exist, but input is not required; valid
if (!$dataExists
&& $input instanceof InputInterface
&& !$input->isRequired()
) {
$this->validInputs[$name] = $input;
continue;
}
if (!array_key_exists($name, $this->data)
|| (null === $this->data[$name])
|| (is_string($this->data[$name]) && strlen($this->data[$name]) === 0)
// Single and Multi File Uploads
|| (is_array($this->data[$name])
&& isset($this->data[$name]['error']) && $this->data[$name]['error'] === UPLOAD_ERR_NO_FILE)
|| (is_array($this->data[$name]) && count($this->data[$name]) === 1
&& isset($this->data[$name][0]) && is_array($this->data[$name][0])
&& isset($this->data[$name][0]['error']) && $this->data[$name][0]['error'] === UPLOAD_ERR_NO_FILE)

// key doesn't exist, input is required, allows empty; valid
if (!$dataExists
&& $input instanceof InputInterface
&& $input->isRequired()
&& $input->allowEmpty()
) {
if ($input instanceof InputInterface) {
// - test if input allows empty
if ($input->allowEmpty()) {
$this->validInputs[$name] = $input;
continue;
}
}
// make sure we have a value (empty) for validation
$this->validInputs[$name] = $input;
continue;
}

// key exists, is null, input is not required; valid
if ($dataExists
&& null === $this->data[$name]
&& $input instanceof InputInterface
&& !$input->isRequired()
) {
$this->validInputs[$name] = $input;
continue;
}

// key exists, is null, input is required, allows empty; valid
if ($dataExists
&& null === $this->data[$name]
&& $input instanceof InputInterface
&& $input->isRequired()
&& $input->allowEmpty()
) {
$this->validInputs[$name] = $input;
continue;
}

// key exists, empty string, input is not required, allows empty; valid
if ($dataExists
&& '' === $this->data[$name]
&& $input instanceof InputInterface
&& !$input->isRequired()
) {
$this->validInputs[$name] = $input;
continue;
}

// key exists, empty string, input is required, allows empty; valid
if ($dataExists
&& '' === $this->data[$name]
&& $input instanceof InputInterface
&& $input->isRequired()
&& $input->allowEmpty()
) {
$this->validInputs[$name] = $input;
continue;
}

// key exists, is array representing file, no file present, input not
// required or allows empty; valid
if ($dataExists
&& is_array($this->data[$name])
&& (
(isset($this->data[$name]['error'])
&& $this->data[$name]['error'] === UPLOAD_ERR_NO_FILE)
|| (count($this->data[$name]) === 1
&& isset($this->data[$name][0])
&& is_array($this->data[$name][0])
&& isset($this->data[$name][0]['error'])
&& $this->data[$name][0]['error'] === UPLOAD_ERR_NO_FILE)
)
&& $input instanceof InputInterface
&& (!$input->isRequired() || $input->allowEmpty())
) {
$this->validInputs[$name] = $input;
continue;
}

// make sure we have a value (empty) for validation
if (!$dataExists) {
$this->data[$name] = null;
}

// Validate an input filter
if ($input instanceof InputFilterInterface) {
if (!$input->isValid()) {
$this->invalidInputs[$name] = $input;
Expand All @@ -209,6 +268,8 @@ protected function validateInputs(array $inputs)
$this->validInputs[$name] = $input;
continue;
}

// Validate an input
if ($input instanceof InputInterface) {
if (!$input->isValid($this->data)) {
// Validation failure
Expand Down
5 changes: 3 additions & 2 deletions test/BaseInputFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@ public function dataSets()
array(
'foo' => ' bazbat ',
'bar' => '12345',
'baz' => '',
'baz' => 'thisistoolong',
'nest' => array(
'foo' => ' bazbat ',
'bar' => '12345',
'baz' => '',
'baz' => 'thisistoolong',
),
),
false,
Expand All @@ -184,6 +184,7 @@ public function dataSets()

/**
* @dataProvider dataSets
* @group fmlife
*/
public function testCanValidateEntireDataset($dataset, $expected)
{
Expand Down
2 changes: 0 additions & 2 deletions test/CollectionInputFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public function getBaseInputFilter()

$baz = new Input();
$baz->setRequired(false);
$baz->setAllowEmpty(true);
$baz->getFilterChain()->attachByName('stringtrim');
$baz->getValidatorChain()->attach(new Validator\StringLength(1, 6));

Expand All @@ -69,7 +68,6 @@ public function getChildInputFilter()

$baz = new Input();
$baz->setRequired(false);
$baz->setAllowEmpty(true);
$baz->getFilterChain()->attachByName('stringtrim');
$baz->getValidatorChain()->attach(new Validator\StringLength(1, 6));

Expand Down

0 comments on commit ef30a66

Please sign in to comment.