Skip to content

Commit

Permalink
Case insensitive validation inList option. Resolves ticket 3521. Adde…
Browse files Browse the repository at this point in the history
…d two tests to proof the faulty behavior of $strict here. Also address the faulty behavior in multiple() and replace it with case sensitivity.
  • Loading branch information
euromark committed Sep 24, 2013
1 parent a7a6fca commit f7e5071
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 16 deletions.
26 changes: 22 additions & 4 deletions lib/Cake/Test/Case/Utility/ValidationTest.php
Expand Up @@ -1945,8 +1945,15 @@ public function testInList() {
$this->assertFalse(Validation::inList('three', array('one', 'two')));
$this->assertFalse(Validation::inList('1one', array(0, 1, 2, 3)));
$this->assertFalse(Validation::inList('one', array(0, 1, 2, 3)));
$this->assertFalse(Validation::inList('2', array(1, 2, 3)));
$this->assertTrue(Validation::inList('2', array(1, 2, 3), false));
$this->assertTrue(Validation::inList('2', array(1, 2, 3)));
$this->assertFalse(Validation::inList('2x', array(1, 2, 3)));
$this->assertFalse(Validation::inList(2, array('1', '2x', '3')));
$this->assertFalse(Validation::inList('One', array('one', 'two')));

// case insensitive
$this->assertTrue(Validation::inList('one', array('One', 'Two'), true));
$this->assertTrue(Validation::inList('Two', array('one', 'two'), true));
$this->assertFalse(Validation::inList('three', array('one', 'two'), true));
}

/**
Expand Down Expand Up @@ -2065,14 +2072,24 @@ public function testMultiple() {
$this->assertFalse(Validation::multiple(array('foo', 'bar', 'baz', 'squirrel'), array('min' => 10)));

$this->assertTrue(Validation::multiple(array(0, 5, 9), array('in' => range(0, 10), 'max' => 5)));
$this->assertFalse(Validation::multiple(array('0', '5', '9'), array('in' => range(0, 10), 'max' => 5)));
$this->assertTrue(Validation::multiple(array('0', '5', '9'), array('in' => range(0, 10), 'max' => 5), false));
$this->assertTrue(Validation::multiple(array('0', '5', '9'), array('in' => range(0, 10), 'max' => 5)));

$this->assertFalse(Validation::multiple(array(0, 5, 9, 8, 6, 2, 1), array('in' => range(0, 10), 'max' => 5)));
$this->assertFalse(Validation::multiple(array(0, 5, 9, 8, 11), array('in' => range(0, 10), 'max' => 5)));

$this->assertFalse(Validation::multiple(array(0, 5, 9), array('in' => range(0, 10), 'max' => 5, 'min' => 3)));
$this->assertFalse(Validation::multiple(array(0, 5, 9, 8, 6, 2, 1), array('in' => range(0, 10), 'max' => 5, 'min' => 2)));
$this->assertFalse(Validation::multiple(array(0, 5, 9, 8, 11), array('in' => range(0, 10), 'max' => 5, 'min' => 2)));

$this->assertFalse(Validation::multiple(array('2x', '3x'), array('in' => array(1, 2, 3, 4, 5))));
$this->assertFalse(Validation::multiple(array(2, 3), array('in' => array('1x', '2x', '3x', '4x'))));
$this->assertFalse(Validation::multiple(array('one'), array('in' => array('One', 'Two'))));
$this->assertFalse(Validation::multiple(array('Two'), array('in' => array('one', 'two'))));

// case insensitive
$this->assertTrue(Validation::multiple(array('one'), array('in' => array('One', 'Two')), true));
$this->assertTrue(Validation::multiple(array('Two'), array('in' => array('one', 'two')), true));
$this->assertFalse(Validation::multiple(array('three'), array('in' => array('one', 'two')), true));
}

/**
Expand Down Expand Up @@ -2333,6 +2350,7 @@ public function testMimeType() {
$this->assertTrue(Validation::mimeType($image, array('image/gif')));
$this->assertTrue(Validation::mimeType(array('tmp_name' => $image), array('image/gif')));

$this->assertFalse(Validation::mimeType($image, array('image/GIF')));
$this->assertFalse(Validation::mimeType($image, array('image/png')));
$this->assertFalse(Validation::mimeType(array('tmp_name' => $image), array('image/png')));
}
Expand Down
39 changes: 27 additions & 12 deletions lib/Cake/Utility/Validation.php
Expand Up @@ -540,7 +540,7 @@ public static function money($check, $symbolPosition = 'left') {
}

/**
* Validate a multiple select.
* Validate a multiple select. Comparison is case sensitive by default.
*
* Valid Options
*
Expand All @@ -550,12 +550,13 @@ public static function money($check, $symbolPosition = 'left') {
*
* @param array $check Value to check
* @param array $options Options for the check.
* @param boolean $strict Defaults to true, set to false to disable strict type check
* @param boolean $caseInsensitive Set to true for case insensitive comparison.
* @return boolean Success
*/
public static function multiple($check, $options = array(), $strict = true) {
public static function multiple($check, $options = array(), $caseInsensitive = false) {
$defaults = array('in' => null, 'max' => null, 'min' => null);
$options = array_merge($defaults, $options);

$check = array_filter((array)$check);
if (empty($check)) {
return false;
Expand All @@ -567,8 +568,15 @@ public static function multiple($check, $options = array(), $strict = true) {
return false;
}
if ($options['in'] && is_array($options['in'])) {
if ($caseInsensitive) {
$options['in'] = array_map('mb_strtolower', $options['in']);
}
foreach ($check as $val) {
if (!in_array($val, $options['in'], $strict)) {
$strict = !is_numeric($val);
if ($caseInsensitive) {
$val = mb_strtolower($val);
}
if (!in_array((string)$val, $options['in'], $strict)) {
return false;
}
}
Expand Down Expand Up @@ -766,15 +774,22 @@ public static function url($check, $strict = false) {
}

/**
* Checks if a value is in a given list.
* Checks if a value is in a given list. Comparison is case sensitive by default.
*
* @param string $check Value to check
* @param array $list List to check against
* @param boolean $strict Defaults to true, set to false to disable strict type check
* @return boolean Success
* @param string $check Value to check.
* @param array $list List to check against.
* @param boolean $caseInsensitive Set to true for case insensitive comparison.
* @return boolean Success.
*/
public static function inList($check, $list, $strict = true) {
return in_array($check, $list, $strict);
public static function inList($check, $list, $caseInsensitive = false) {
$strict = !is_numeric($check);

if ($caseInsensitive) {
$list = array_map('mb_strtolower', $list);
$check = mb_strtolower($check);
}

return in_array((string)$check, $list, $strict);
}

/**
Expand Down Expand Up @@ -896,7 +911,7 @@ public static function luhn($check, $deep = false) {
}

/**
* Checks the mime type of a file
* Checks the mime type of a file. Comparison is case sensitive.
*
* @param string|array $check
* @param array $mimeTypes to check for
Expand Down

0 comments on commit f7e5071

Please sign in to comment.