Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed bug in skipping mode, added test cases and fuxtures for confirm…

…Fields()
  • Loading branch information...
commit 7f55baeb3992f0dcf77bce3e1b08a7d93995d44d 1 parent c8e5fb6
Thomas Ploch authored
View
75 models/behaviors/super_validatable.php
@@ -82,43 +82,12 @@ public function daysInFuture($check, $params = array()) {
}
return false;
}
-
- function inNYC(&$model, $field, $nyc_id = null) {
- $items = array(
- 'Brooklyn' => array('11201', '11203', '11204', '11205', '11206', '11207', '11208', '11209', '11210', '11211', '11212', '11213', '11214', '11215', '11216', '11217', '11218', '11219', '11220', '11221', '11222', '11223', '11224', '11225', '11226', '11228', '11229', '11230', '11231', '11232', '11233', '11234', '11235', '11236', '11237', '11238', '11239'),
- 'Manhattan' => array('10001', '10002', '10003', '10004', '10005', '10006', '10007', '10009', '10010', '10011', '10012', '10013', '10014', '10016', '10017', '10018', '10019', '10020', '10021', '10022', '10023', '10024', '10025', '10026', '10027', '10028', '10029', '10030', '10031', '10032', '10033', '10034', '10035', '10036', '10037', '10038', '10039', '10040', '10041', '10044', '10048', '10069', '10103', '10111', '10112', '10115', '10119', '10128', '10152', '10153', '10154', '10162', '10165', '10167', '10169', '10170', '10171', '10172', '10173', '10177', '10271', '10278', '10279', '10280', '10282'),
- 'Queens' => array('11004', '11101', '11102', '11103', '11104', '11105', '11106', '11354', '11355', '11356', '11357', '11358', '11360', '11361', '11362', '11363', '11364', '11365', '11366', '11367', '11368', '11369', '11371', '11372', '11373', '11374', '11375', '11377', '11378', '11379', '11385', '11411', '11412', '11413', '11414', '11415', '11416', '11417', '11418', '11419', '11420', '11421', '11422', '11423', '11426', '11427', '11428', '11429', '11430', '11432', '11433', '11434', '11435', '11436', '11691', '11692', '11693', '11694', '11697', ),
- 'Staten Island' => array('10301', '10302', '10303', '10304', '10305', '10306', '10307', '10308', '10309', '10310', '10312', '10314'),
- 'The Bronx' => array('10451', '10452', '10453', '10454', '10455', '10456', '10457', '10458', '10459', '10460', '10461', '10462', '10463', '10464', '10465', '10466', '10467', '10468', '10469', '10470', '10471', '10472', '10473', '10474', '10475', '11370')
- );
- if (isset($nyc_id) and ($model->data[$model->alias]['state_id'] != $nyc_id)) {
- return false;
- }
- foreach ($items as $item) {
- if (in_array($field['zip_code'], $item)) {
- return true;
- }
- }
- return false;
- }
-
- function inState(&$model, $check, $state_id) {
- return (array_pop(array_values($check)) === $state_id);
- }
-
- public function stateValidation(&$model, &$field, $params = array()) {
- return true;
- $passed = true;
- if ($model->data[$model->alias]['country_id'] != $params['country_id']) {
- if (empty($model->data[$model->alias][$field])) {
- $passed = false;
- }
- }
- return $passed;
- }
-
-/*
+/**
+ * Returns true if a checkbox is checked, fals otherwise
*
+ * @param object $model
+ * @param array $field
+ * @return boolean
*/
function isChecked(&$model, $field) {
$value = array_values($field);
@@ -136,7 +105,6 @@ public function notEqualTo(&$model, $field = array(), $params = array()) {
}
return true;
}
-
/**
* Makes this field required if some other field is marked as true
*
@@ -153,7 +121,6 @@ public function requiredByFields($check, $params = array()) {
}
return true;
}
-
/**
* Validates that at least one field is not empty
*
@@ -197,6 +164,7 @@ public function validateDependentFields(&$model, $check, $params = array()) {
*/
function confirmFields(&$model, $check, $params = array()) {
$valid = false;
+ // default params
$defaultParams = array(
'fields' => array(),
'hash' => false,
@@ -207,24 +175,33 @@ function confirmFields(&$model, $check, $params = array()) {
),
'skip' => false
);
- $params = array_merge($defaultParams, $params);
+
+ $params = am($defaultParams, $params);
+ $fieldKey = array_pop(array_keys($check));
extract($params);
foreach ($fields as $field) {
+ // If skip option is false return on unset fields
if (!isset($model->data[$model->alias][$field]) && $skip !== true) {
return false;
- }
-
- if ($hash === true && (empty($hashOptions['fields']) || in_array($fields, $hashOptions['fields']))) {
- $confirm = Security::hash($model->data[$model->alias][$field], $hashOptions['method'], $hashOptions['salt']);
+ // If skipping is activated continue
+ } elseif (!isset($model->data[$model->alias][$field]) && $skip === true) {
+ continue;
} else {
- $confirm = $model->data[$model->alias][$field];
- }
-
- $valid = ($confirm == $check[0]);
- if (!$valid) {
- return $valid;
+ // hash if hash is true and either hash all if hashOptions['fields'] is empty, or only those in there
+ if ($hash === true && (empty($hashOptions['fields']) || in_array($fields, $hashOptions['fields']))) {
+ $confirm = Security::hash($model->data[$model->alias][$field], $hashOptions['method'], $hashOptions['salt']);
+ } else {
+ $confirm = $model->data[$model->alias][$field];
+ }
+ // do validation
+ $valid = ($confirm == $model->data[$model->alias][$fieldKey]);
+ // break on false
+ if (!$valid) {
+ return $valid;
+ }
}
}
return $valid;
}
+}
?>
View
153 tests/behaviors/super_validatable.test.php
@@ -0,0 +1,153 @@
+<?php
+App::import('Security');
+class SuperValidatableTestCase extends CakeTestCase {
+ var $fixtures = array( 'app.super_validatable' );
+
+ function startTest() {
+ $this->Validatable =& ClassRegistry::init('Validatable');
+ $this->Validatable->Behaviors->attach('SuperValidatable');
+ }
+
+ function testconfirmFields() {
+
+ // Testing default hash option salted sha1
+ $hashedPw = Security::hash('abcdefg', 'sha1', true);
+ $data = array(
+ 'Validatable' => array(
+ 'password' => $hashedPw,
+ 'pw_confirm' => 'abcdefg',
+ 'another_field' => 'abcdefg'
+ )
+ );
+ $this->Validatable->data = $data;
+ $valid = $this->Validatable->confirmFields(
+ array('password' => $hashedPw),
+ array(
+ 'fields' => array('pw_confirm', 'another_field'),
+ 'hash' => true
+ )
+ );
+ $this->assertTrue($valid);
+
+ // Testing different hash Options unsalted md5
+ $hashedPw = Security::hash('abcdefg', 'md5', false);
+ $data = array(
+ 'Validatable' => array(
+ 'password' => $hashedPw,
+ 'pw_confirm' => 'abcdefg',
+ 'another_field' => 'abcdefg'
+ )
+ );
+ $this->Validatable->data = $data;
+ $valid = $this->Validatable->confirmFields(
+ array('password' => $hashedPw),
+ array(
+ 'fields' => array('pw_confirm', 'another_field'),
+ 'hash' => true,
+ 'hashOptions' => array(
+ 'method' => 'md5',
+ 'salt' => false
+ )
+ )
+ );
+ $this->assertTrue($valid);
+
+ // Testing salt hashOption
+ $valid = $this->Validatable->confirmFields(
+ array('password' => $hashedPw),
+ array(
+ 'fields' => array('pw_confirm', 'another_field'),
+ 'hash' => true,
+ 'hashOptions' => array(
+ 'method' => 'md5',
+ 'salt' => true
+ )
+ )
+ );
+ $this->assertFalse($valid);
+
+ // testing plain default
+ $data = array(
+ 'Validatable' => array(
+ 'password' => 'abcdefg',
+ 'pw_confirm' => 'abcdefg',
+ 'another_field' => 'abcdefg'
+ )
+ );
+ $this->Validatable->data = $data;
+ $valid = $this->Validatable->confirmFields(
+ array('password' => 'abcdefg'),
+ array('fields' => array('pw_confirm', 'another_field'))
+ );
+ $this->assertTrue($valid);
+
+ // testing plain default false
+ $data = array(
+ 'Validatable' => array(
+ 'password' => 'abcdefg',
+ 'pw_confirm' => 'abcdefg',
+ 'another_field' => 'argkifsdföpj'
+ )
+ );
+ $this->Validatable->data = $data;
+ $valid = $this->Validatable->confirmFields(
+ array('password' => 'abcdefg'),
+ array('fields' => array('pw_confirm', 'another_field'))
+ );
+ $this->assertFalse($valid);
+
+ // testing emypty fields
+ $valid = $this->Validatable->confirmFields(
+ array('password' => 'abcdefg'),
+ array('fields' => array())
+ );
+ $this->assertFalse($valid);
+
+ // testing field exclusion when hashing
+ $data = array(
+ 'Validatable' => array(
+ 'password' => $hashedPw,
+ 'pw_confirm' => 'abcdefg',
+ 'another_field' => 'abcdefg'
+ )
+ );
+ $this->Validatable->data = $data;
+ $valid = $this->Validatable->confirmFields(
+ array('password' => $hashedPw),
+ array(
+ 'hash' => true,
+ 'fields' => array('pw_confirm'),
+ 'hashOptions' => array(
+ 'fields' => array('another_field')
+ )
+ )
+ );
+ $this->assertFalse($valid);
+
+ // testing skipping
+ $data = array(
+ 'Validatable' => array(
+ 'password' => 'abcdefg',
+ 'pw_confirm' => 'abcdefg',
+ 'another_field' => 'abcdefg'
+ )
+ );
+ $this->Validatable->data = $data;
+ $valid = $this->Validatable->confirmFields(
+ array('password' => 'abcdefg'),
+ array(
+ 'fields' => array('pw_confirm', 'invalid')
+ )
+ );
+ $this->assertFalse($valid);
+ $valid = $this->Validatable->confirmFields(
+ array('password' => $hashedPw),
+ array(
+ 'fields' => array('pw_confirm', 'invalid'),
+ 'skip' => true
+ )
+ );
+ $this->assertTrue($valid);
+ }
+}
+?>
View
12 tests/fixtures/super_validatable_fixture.php
@@ -0,0 +1,12 @@
+<?php
+class SuperValidatableFixture extends CakeTestFixture {
+ var $name = 'Validatable';
+
+ var $fields = array(
+ 'id' => array('type' => 'integer', 'key' => 'primary'),
+ 'password' => array('type' => 'string', 'length' => 40, 'null' => false),
+ 'pw_confirm' => array('type' => 'string', 'length' => 40, 'null' => false),
+ 'another_field' =>array('type' => 'string', 'length' => 40, 'null' => false),
+ );
+}
+?>
Please sign in to comment.
Something went wrong with that request. Please try again.