diff --git a/gump.class.php b/gump.class.php index f2d39ee6..d098f3d3 100644 --- a/gump.class.php +++ b/gump.class.php @@ -399,11 +399,7 @@ public function validate(array $input, array $ruleset, $rules_delimiter='|', $pa if (count(array_intersect($look_for, $rules)) > 0 || (isset($input[$field]))) { if (isset($input[$field])) { - if (is_array($input[$field]) && in_array('required_file', $ruleset)) { - $input_array = $input[$field]; - } else { - $input_array = array($input[$field]); - } + $input_array = array($input[$field]); } else { $input_array = array(''); } @@ -1135,14 +1131,12 @@ protected function validate_max_len($field, $input, $param = null) return; } - if (function_exists('mb_strlen')) { + if (Helpers::functionExists('mb_strlen')) { if (mb_strlen($input[$field]) <= (int) $param) { return; } - } else { - if (strlen($input[$field]) <= (int) $param) { - return; - } + } else if (strlen($input[$field]) <= (int) $param) { + return; } return array( @@ -1170,14 +1164,12 @@ protected function validate_min_len($field, $input, $param = null) return; } - if (function_exists('mb_strlen')) { + if (Helpers::functionExists('mb_strlen')) { if (mb_strlen($input[$field]) >= (int) $param) { return; } - } else { - if (strlen($input[$field]) >= (int) $param) { - return; - } + } else if (strlen($input[$field]) >= (int) $param) { + return; } return array( @@ -1205,14 +1197,12 @@ protected function validate_exact_len($field, $input, $param = null) return; } - if (function_exists('mb_strlen')) { + if (Helpers::functionExists('mb_strlen')) { if (mb_strlen($input[$field]) == (int) $param) { return; } - } else { - if (strlen($input[$field]) == (int) $param) { - return; - } + } else if (strlen($input[$field]) == (int) $param) { + return; } return array( @@ -1646,20 +1636,19 @@ protected function validate_valid_cc($field, $input, $param = null) $number = preg_replace('/\D/', '', $input[$field]); - if (function_exists('mb_strlen')) { + if (Helpers::functionExists('mb_strlen')) { $number_length = mb_strlen($number); } else { $number_length = strlen($number); } - /** * Bail out if $number_length is 0. * This can be the case if a user has entered only alphabets * * @since 1.5 */ - if( $number_length == 0 ) { + if ($number_length == 0 ) { return array( 'field' => $field, 'value' => $input[$field], @@ -1668,7 +1657,6 @@ protected function validate_valid_cc($field, $input, $param = null) ); } - $parity = $number_length % 2; $total = 0; @@ -1972,33 +1960,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 +2005,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 +2015,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, + ); } /** @@ -2095,22 +2083,6 @@ protected function validate_guidv4($field, $input, $param = null) ); } - /** - * Trims whitespace only when the value is a scalar. - * - * @param mixed $value - * - * @return mixed - */ - private function trimScalar($value) - { - if (is_scalar($value)) { - $value = trim($value); - } - - return $value; - } - /** * Determine if the provided value is a valid phone number. * diff --git a/tests/BaseTestCase.php b/tests/BaseTestCase.php index cde04dee..0a3be5b6 100644 --- a/tests/BaseTestCase.php +++ b/tests/BaseTestCase.php @@ -74,4 +74,15 @@ public function validate($rule, $value) 'test' => $rule ]); } + + public function filter($rule, $value) + { + $result = $this->gump->filter([ + 'test' => $value + ], [ + 'test' => $rule + ]); + + return $result['test']; + } } \ No newline at end of file diff --git a/tests/Filters/BasicTagsFilterTest.php b/tests/Filters/BasicTagsFilterTest.php new file mode 100644 index 00000000..636f71e6 --- /dev/null +++ b/tests/Filters/BasicTagsFilterTest.php @@ -0,0 +1,34 @@ +filter(self::FILTER, $input); + + $this->assertEquals($result, $expected); + } + + public function successProvider() + { + return [ + ['hello', 'alert(1);hello'] + ]; + } +} \ No newline at end of file diff --git a/tests/Filters/LowercaseFilterTest.php b/tests/Filters/LowercaseFilterTest.php new file mode 100644 index 00000000..c98ad311 --- /dev/null +++ b/tests/Filters/LowercaseFilterTest.php @@ -0,0 +1,33 @@ +filter(self::FILTER, $input); + $this->assertEquals($result, $expected); + } + + public function successProvider() + { + return [ + ['Hello', 'hello'] + ]; + } +} \ No newline at end of file diff --git a/tests/Filters/SlugFilterTest.php b/tests/Filters/SlugFilterTest.php new file mode 100644 index 00000000..f89beefd --- /dev/null +++ b/tests/Filters/SlugFilterTest.php @@ -0,0 +1,35 @@ +filter(self::FILTER, $input); + $this->assertEquals($result, $expected); + } + + public function successProvider() + { + return [ + ['test spaceñ', 'test-spacen'], + ['test space', 'test-space'], + ['test', 'test'], + ]; + } +} \ No newline at end of file diff --git a/tests/Filters/WholeNumberFilterTest.php b/tests/Filters/WholeNumberFilterTest.php new file mode 100644 index 00000000..7f3fcd6b --- /dev/null +++ b/tests/Filters/WholeNumberFilterTest.php @@ -0,0 +1,37 @@ +filter(self::FILTER, $input); + + $this->assertEquals($result, $expected); + } + + public function successProvider() + { + return [ + [1, 1], + ['-1', '-1'], + [4.2, 4], + ['042', '42'], + ]; + } +} \ No newline at end of file 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/ContainsValidatorTest.php b/tests/Validators/ContainsValidatorTest.php index a73233e0..c6860747 100644 --- a/tests/Validators/ContainsValidatorTest.php +++ b/tests/Validators/ContainsValidatorTest.php @@ -25,16 +25,16 @@ public function testFailure() public function testSuccessWithRegexSeparator() { - $this->assertTrue($this->validate("contains,#'one'##'two'#", 'one')); + $this->assertTrue($this->validate("contains,'one' 'two' 'half three'", 'half three')); } public function testFailureWithRegexSeparator() { - $this->assertNotTrue($this->validate("contains,#'one'##'two'#", 'three')); + $this->assertNotTrue($this->validate("contains,'one' 'two'", 'three')); } public function testFailureWithRegexSeparatora() { - $this->assertNotTrue($this->validate("contains,#'one'##'two'#", '')); + $this->assertNotTrue($this->validate("contains,'one' 'two'", '')); } } \ No newline at end of file diff --git a/tests/Validators/ExactLenValidatorTest.php b/tests/Validators/ExactLenValidatorTest.php index b2333bbb..c6222d73 100644 --- a/tests/Validators/ExactLenValidatorTest.php +++ b/tests/Validators/ExactLenValidatorTest.php @@ -5,6 +5,7 @@ use GUMP; use Exception; use Tests\BaseTestCase; +use Mockery as m; /** * Class ExactLenValidatorTest @@ -13,18 +14,76 @@ */ class ExactLenValidatorTest extends BaseTestCase { - public function testSuccessWhenEqual() + public function testSuccessWhenEqualWithMbStrlen() { - $this->assertTrue($this->validate('exact_len,2', '12')); + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnTrue(); + + $this->assertTrue($this->validate('exact_len,5', 'ñándú')); } - public function testErrorWhenMore() + public function testErrorWhenMoreWithMbStrlen() { - $this->assertNotTrue($this->validate('exact_len,2', '123')); + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnTrue(); + + $this->assertNotTrue($this->validate('exact_len,2', 'ñán')); } - public function testErrorWhenLess() + public function testErrorWhenLessWithMbStrlen() { - $this->assertNotTrue($this->validate('exact_len,2', '1')); + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnTrue(); + + $this->assertNotTrue($this->validate('exact_len,2', 'ñ')); + } + + public function testSuccessWhenEqualWithStrlen() + { + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnFalse(); + + $this->assertTrue($this->validate('exact_len,3', 'ña')); + $this->assertTrue($this->validate('exact_len,2', 'na')); + } + + public function testErrorWhenMoreWithStrlen() + { + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnFalse(); + + $this->assertNotTrue($this->validate('exact_len,2', 'nan')); + } + + public function testErrorWhenLessWithStrlen() + { + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnFalse(); + + $this->assertNotTrue($this->validate('exact_len,2', 'n')); } } \ 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/MaxLenValidatorTest.php b/tests/Validators/MaxLenValidatorTest.php index 72a56856..b1ad86cd 100644 --- a/tests/Validators/MaxLenValidatorTest.php +++ b/tests/Validators/MaxLenValidatorTest.php @@ -5,6 +5,7 @@ use GUMP; use Exception; use Tests\BaseTestCase; +use Mockery as m; /** * Class MaxLenValidatorTest @@ -13,18 +14,76 @@ */ class MaxLenValidatorTest extends BaseTestCase { - public function testSuccessWhenEqual() + public function testSuccessWhenEqualWithMbStrlen() { - $this->assertTrue($this->validate('max_len,2', '12')); + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnTrue(); + + $this->assertTrue($this->validate('max_len,5', 'ñándú')); } - public function testSuccessWhenLess() + public function testSuccessWhenLessWithMbStrlen() { - $this->assertTrue($this->validate('max_len,2', '1')); + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnTrue(); + + $this->assertTrue($this->validate('max_len,2', 'ñ')); } - public function testErrorWhenMore() + public function testErrorWhenMoreWithMbStrlen() { - $this->assertNotTrue($this->validate('max_len,2', '123')); + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnTrue(); + + $this->assertNotTrue($this->validate('max_len,2', 'ñán')); + } + + public function testSuccessWhenEqualWithStrlen() + { + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnFalse(); + + $this->assertTrue($this->validate('max_len,3', 'ña')); + $this->assertTrue($this->validate('max_len,2', 'na')); + } + + public function testSuccessWhenLessWithStrlen() + { + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnFalse(); + + $this->assertTrue($this->validate('max_len,2', 'n')); + } + + public function testErrorWhenMoreWithStrlen() + { + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnFalse(); + + $this->assertNotTrue($this->validate('max_len,2', 'nan')); } } \ No newline at end of file diff --git a/tests/Validators/MinLenValidatorTest.php b/tests/Validators/MinLenValidatorTest.php index 4325fa95..f3ea76f1 100644 --- a/tests/Validators/MinLenValidatorTest.php +++ b/tests/Validators/MinLenValidatorTest.php @@ -5,6 +5,7 @@ use GUMP; use Exception; use Tests\BaseTestCase; +use Mockery as m; /** * Class MinLenValidatorTest @@ -13,18 +14,76 @@ */ class MinLenValidatorTest extends BaseTestCase { - public function testSuccessWhenEqual() + public function testSuccessWhenEqualWithMbStrlen() { - $this->assertTrue($this->validate('min_len,2', '12')); + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnTrue(); + + $this->assertTrue($this->validate('min_len,5', 'ñándú')); } - public function testSuccessWhenMore() + public function testSuccessWhenMoreWithMbStrlen() { - $this->assertTrue($this->validate('min_len,2', '123')); + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnTrue(); + + $this->assertTrue($this->validate('min_len,2', 'ñán')); } - public function testErrorWhenLess() + public function testErrorWhenLessWithMbStrlen() { - $this->assertNotTrue($this->validate('min_len,2', '1')); + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnTrue(); + + $this->assertNotTrue($this->validate('min_len,2', 'ñ')); + } + + public function testSuccessWhenEqualWithStrlen() + { + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnFalse(); + + $this->assertTrue($this->validate('min_len,3', 'ña')); + $this->assertTrue($this->validate('min_len,2', 'na')); + } + + public function testSuccessWhenMoreWithStrlen() + { + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnFalse(); + + $this->assertTrue($this->validate('min_len,2', 'nan')); + } + + public function testErrorWhenLessWithStrlen() + { + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnFalse(); + + $this->assertNotTrue($this->validate('min_len,2', 'n')); } } \ 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 diff --git a/tests/Validators/ValidCcValidatorTest.php b/tests/Validators/ValidCcValidatorTest.php index 0b3066b0..6ac30f36 100644 --- a/tests/Validators/ValidCcValidatorTest.php +++ b/tests/Validators/ValidCcValidatorTest.php @@ -5,6 +5,7 @@ use GUMP; use Exception; use Tests\BaseTestCase; +use Mockery as m; /** * Class ValidCcValidatorTest @@ -18,8 +19,30 @@ class ValidCcValidatorTest extends BaseTestCase /** * @dataProvider successProvider */ - public function testSuccess($input) + public function testSuccessWithMbStrlen($input) { + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnTrue(); + + $this->assertTrue($this->validate(self::RULE, $input)); + } + + /** + * @dataProvider successProvider + */ + public function testSuccessWithStrlen($input) + { + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnFalse(); + $this->assertTrue($this->validate(self::RULE, $input)); } @@ -33,8 +56,30 @@ public function successProvider() /** * @dataProvider errorProvider */ - public function testError($input) + public function testErrorWithMbStrlen($input) { + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnTrue(); + + $this->assertNotTrue($this->validate(self::RULE, $input)); + } + + /** + * @dataProvider errorProvider + */ + public function testErrorWithStrlen($input) + { + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnFalse(); + $this->assertNotTrue($this->validate(self::RULE, $input)); }