diff --git a/gump.class.php b/gump.class.php index f4330c02..766bf9d4 100644 --- a/gump.class.php +++ b/gump.class.php @@ -400,7 +400,7 @@ public function validate(array $input, array $ruleset, $rules_delimiter='|', $pa if (isset($input[$field])) { if (is_array($input[$field]) && in_array('required_file', $ruleset)) { - $input_array = $input[$field]; + $input_array = array($input[$field]); } else { $input_array = array($input[$field]); } @@ -1972,33 +1972,33 @@ protected function validate_starts($field, $input, $param = null) } } - /** - * Checks if a file was uploaded. - * - * Usage: '' => 'required_file' - * - * @param string $field - * @param array $input - * - * @return mixed - */ - protected function validate_required_file($field, $input, $param = null) - { - if (!isset($input[$field])) { - return; - } - - if (is_array($input[$field]) && $input[$field]['error'] !== 4) { - return; - } - - return array( - 'field' => $field, - 'value' => $input[$field], - 'rule' => __FUNCTION__, - 'param' => $param, - ); - } + /** + * Checks if a file was uploaded. + * + * Usage: '' => 'required_file' + * + * @param string $field + * @param array $input + * + * @return mixed + */ + protected function validate_required_file($field, $input, $param = null) + { + if (!isset($input[$field])) { + return; + } + + if (is_array($input[$field]) && $input[$field]['error'] === 0) { + return; + } + + return array( + 'field' => $field, + 'value' => $input[$field], + 'rule' => __FUNCTION__, + 'param' => $param, + ); + } /** * Check the uploaded file for extension for now @@ -2017,7 +2017,7 @@ protected function validate_extension($field, $input, $param = null) return; } - if (is_array($input[$field]) && $input[$field]['error'] !== 4) { + if (is_array($input[$field]) && $input[$field]['error'] === 0) { $param = trim(strtolower($param)); $allowed_extensions = explode(';', $param); @@ -2027,14 +2027,14 @@ protected function validate_extension($field, $input, $param = null) if ($extension && in_array(strtolower($extension), $allowed_extensions)) { return; } - - return array( - 'field' => $field, - 'value' => $input[$field], - 'rule' => __FUNCTION__, - 'param' => $param, - ); } + + return array( + 'field' => $field, + 'value' => $input[$field], + 'rule' => __FUNCTION__, + 'param' => $param, + ); } /** diff --git a/tests/ValidateTest.php b/tests/ValidateTest.php index 8facf331..c1ab01e1 100644 --- a/tests/ValidateTest.php +++ b/tests/ValidateTest.php @@ -174,14 +174,15 @@ public function testCustomValidatorsReturnRightErrorStructure() ]], $result); } - public function testRequired() + public function testRequiredAndRequiredFile() { $result = $this->gump->validate([ - 'test' => 'failing' + 'field_without_validation_rules' => '123' ], [ - 'nonexistent' => 'required' + 'some_field' => 'required', + 'file_field' => 'required_file', ]); - $this->assertTrue(count($result) ===1); + $this->assertTrue(count($result) === 2); } } \ No newline at end of file diff --git a/tests/Validators/ExtensionValidatorTest.php b/tests/Validators/ExtensionValidatorTest.php new file mode 100644 index 00000000..7d91b75e --- /dev/null +++ b/tests/Validators/ExtensionValidatorTest.php @@ -0,0 +1,57 @@ + 'screenshot.png', + 'type' => 'image/png', + 'tmp_name' => '/tmp/phphjatI9', + 'error' => 0, + 'size' => 22068 + ]; + + $this->assertTrue($this->validate(self::RULE, $input)); + } + + public function testItFailsWhenExtensionDoesntMatch() + { + $input = [ + 'name' => 'document.pdf', + 'type' => 'application/pdf', + 'tmp_name' => '/tmp/phphjatI9', + 'error' => 0, + 'size' => 22068 + ]; + + $this->assertNotTrue($this->validate(self::RULE, $input)); + } + + public function testItFailsWhenFileWasNotSuccessfullyUploaded() + { + $input = [ + 'name' => 'screenshot.png', + 'type' => 'image/png', + 'tmp_name' => '/tmp/phphjatI9', + 'error' => 4, + 'size' => 22068 + ]; + + $this->assertNotTrue($this->validate(self::RULE, $input)); + } + +} \ No newline at end of file diff --git a/tests/Validators/RequiredFileValidatorTest.php b/tests/Validators/RequiredFileValidatorTest.php new file mode 100644 index 00000000..4193d343 --- /dev/null +++ b/tests/Validators/RequiredFileValidatorTest.php @@ -0,0 +1,52 @@ +gump->validate([], [ + 'test' => self::RULE + ]); + + $this->assertNotTrue($result); + } + + public function testWhenFileIsSuccessfullyUploadedItSuccesses() + { + $input = [ + 'name' => 'screenshot.png', + 'type' => 'image/png', + 'tmp_name' => '/tmp/phphjatI9', + 'error' => 0, + 'size' => 22068 + ]; + + $this->assertTrue($this->validate(self::RULE, $input)); + } + + public function testWhenFileIsNotSuccessfullyUploadedItFails() + { + $input = [ + 'name' => 'document.pdf', + 'type' => 'application/pdf', + 'tmp_name' => '/tmp/phphjatI9', + 'error' => 4, + 'size' => 22068 + ]; + + $this->assertNotTrue($this->validate(self::RULE, $input)); + } +} \ No newline at end of file