Skip to content
Permalink
Browse files

Adds containsNonAlphaNumeric validation rule

Fixes regex, increases coverage

Test non-supported array keys

Removes array support (no BC neccessary)

Simplifies return logic
  • Loading branch information...
bravo-kernel committed Sep 27, 2015
1 parent 5eafb81 commit 54f202e0dff9b215524382220f182cd5a6ae3859
Showing with 73 additions and 0 deletions.
  1. +19 −0 src/Validation/Validation.php
  2. +54 −0 tests/TestCase/Validation/ValidationTest.php
@@ -307,6 +307,25 @@ public static function compareWith($check, $field, $context)
return $context['data'][$field] === $check;
}
/**
* Checks if a string contains one or more non-alphanumeric characters.
*
* Returns true if string contains at least the specified number of non-alphanumeric characters
*
* @param string $check Value to check
* @param int $count Number of non-alphanumerics to check for
* @return bool Success
*/
public static function containsNonAlphaNumeric($check, $count = 1)
{
if (!is_string($check)) {
return false;
}
$matches = preg_match_all('/[^a-zA-Z0-9]/', $check);
return $matches >= $count;
}
/**
* Used when a custom regular expression is needed.
*
@@ -2528,6 +2528,60 @@ public function testCompareWith()
$this->assertFalse(Validation::compareWith('a value', 'other', $context));
}
/**
* testContainsNonAlphaNumeric method
*
* @return void
*/
public function testContainNonAlphaNumeric()
{
$this->assertFalse(Validation::containsNonAlphaNumeric('abcdefghijklmnopqrstuvwxyz'));
$this->assertFalse(Validation::containsNonAlphaNumeric('ABCDEFGHIJKLMNOPQRSTUVWXYZ'));
$this->assertFalse(Validation::containsNonAlphaNumeric('0123456789'));
$this->assertFalse(Validation::containsNonAlphaNumeric('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'));
$this->assertTrue(Validation::containsNonAlphaNumeric('#'));
$this->assertTrue(Validation::containsNonAlphaNumeric("0\n"));
$this->assertTrue(Validation::containsNonAlphaNumeric("\n"));
$this->assertTrue(Validation::containsNonAlphaNumeric("\t"));
$this->assertTrue(Validation::containsNonAlphaNumeric("\r"));
$this->assertTrue(Validation::containsNonAlphaNumeric(" "));
$this->assertTrue(Validation::containsNonAlphaNumeric('#abcdef'));
$this->assertTrue(Validation::containsNonAlphaNumeric('abc#def'));
$this->assertTrue(Validation::containsNonAlphaNumeric('abcdef#'));
$this->assertTrue(Validation::containsNonAlphaNumeric('abc def'));
$this->assertTrue(Validation::containsNonAlphaNumeric("abcdef\n"));
$this->assertTrue(Validation::containsNonAlphaNumeric('##abcdef', 2));
$this->assertTrue(Validation::containsNonAlphaNumeric('abcdef##', 2));
$this->assertTrue(Validation::containsNonAlphaNumeric('#abcdef#', 2));
$this->assertTrue(Validation::containsNonAlphaNumeric('#abc#def', 2));
$this->assertTrue(Validation::containsNonAlphaNumeric('abc#def#', 2));
$this->assertTrue(Validation::containsNonAlphaNumeric('#♥abcdef', 2));
$this->assertTrue(Validation::containsNonAlphaNumeric('abcdef#♥', 2));
$this->assertTrue(Validation::containsNonAlphaNumeric('#abcdef♥', 2));
$this->assertTrue(Validation::containsNonAlphaNumeric('#abc♥def', 2));
$this->assertTrue(Validation::containsNonAlphaNumeric('abc#def♥', 2));
$this->assertTrue(Validation::containsNonAlphaNumeric('#♥abcdef', 2));
$this->assertTrue(Validation::containsNonAlphaNumeric('abcdef#♥', 2));
$this->assertTrue(Validation::containsNonAlphaNumeric('#abcdef♥', 2));
$this->assertTrue(Validation::containsNonAlphaNumeric('#abc♥def', 2));
$this->assertTrue(Validation::containsNonAlphaNumeric('abc#def♥', 2));
$this->assertTrue(Validation::containsNonAlphaNumeric('###abcdef', 2));
$this->assertTrue(Validation::containsNonAlphaNumeric('abc###def', 2));
$this->assertTrue(Validation::containsNonAlphaNumeric('abcdef###', 2));
$this->assertTrue(Validation::containsNonAlphaNumeric('#abc#def#', 2));
$this->assertFalse(Validation::containsNonAlphaNumeric('##abcdef', 3));
$this->assertFalse(Validation::containsNonAlphaNumeric('abcdef##', 3));
$this->assertFalse(Validation::containsNonAlphaNumeric('abc##def', 3));
$this->assertFalse(Validation::containsNonAlphaNumeric('ab#cd#ef', 3));
}
/**
* Test the geoCoordinate method.
*

0 comments on commit 54f202e

Please sign in to comment.
You can’t perform that action at this time.