Skip to content
Permalink
Browse files

Fixing issue where whitelist would not be used for validation. Test c…

…ase added. Fixes #1037
  • Loading branch information...
markstory committed Aug 22, 2010
1 parent 6d7a92d commit 0d4a50002b185f7eb1903c96257ea1d75763159a
Showing with 378 additions and 2 deletions.
  1. +1 −1 cake/libs/model/model.php
  2. +377 −1 cake/tests/cases/libs/model/model_validation.test.php
@@ -2431,7 +2431,7 @@ function invalidFields($options = array()) {
$_validate = $this->validate;
$whitelist = $this->whitelist;
if (array_key_exists('fieldList', $options)) {
if (!empty($options['fieldList'])) {
$whitelist = $options['fieldList'];
}
@@ -166,10 +166,386 @@ function testInvalidFieldsWithFieldListParams() {
$TestModel->invalidFields();
$expected = array('name' => 'This field cannot be left blank');
$this->assertEqual($TestModel->validationErrors, $expected);
$TestModel->validationErrors = array();
$this->assertEqual($TestModel->validate, $validate);
}
/**
* Test that invalidFields() integrates well with save(). And that fieldList can be an empty type.
*
* @return void
*/
function testInvalidFieldsWhitelist() {
$TestModel =& new ValidationTest1();
$TestModel->validate = $validate = array(
'title' => array(
'rule' => 'customValidator',
'required' => true
),
'name' => array(
'rule' => 'alphaNumeric',
'required' => true
));
$TestModel->whitelist = array('name');
$TestModel->save(array('name' => '#$$#'));
$expected = array('name' => 'This field cannot be left blank');
$this->assertEqual($TestModel->validationErrors, $expected);
}
/**
* testValidates method
*
* @access public
* @return void
*/
function testValidates() {
$TestModel =& new TestValidate();
$TestModel->validate = array(
'user_id' => 'numeric',
'title' => array('allowEmpty' => false, 'rule' => 'notEmpty'),
'body' => 'notEmpty'
);
$data = array('TestValidate' => array(
'user_id' => '1',
'title' => '',
'body' => 'body'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertFalse($result);
$data = array('TestValidate' => array(
'user_id' => '1',
'title' => 'title',
'body' => 'body'
));
$result = $TestModel->create($data) && $TestModel->validates();
$this->assertTrue($result);
$data = array('TestValidate' => array(
'user_id' => '1',
'title' => '0',
'body' => 'body'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertTrue($result);
$data = array('TestValidate' => array(
'user_id' => '1',
'title' => 0,
'body' => 'body'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertTrue($result);
$TestModel->validate['modified'] = array('allowEmpty' => true, 'rule' => 'date');
$data = array('TestValidate' => array(
'user_id' => '1',
'title' => 0,
'body' => 'body',
'modified' => ''
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertTrue($result);
$data = array('TestValidate' => array(
'user_id' => '1',
'title' => 0,
'body' => 'body',
'modified' => '2007-05-01'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertTrue($result);
$data = array('TestValidate' => array(
'user_id' => '1',
'title' => 0,
'body' => 'body',
'modified' => 'invalid-date-here'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertFalse($result);
$data = array('TestValidate' => array(
'user_id' => '1',
'title' => 0,
'body' => 'body',
'modified' => 0
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertFalse($result);
$data = array('TestValidate' => array(
'user_id' => '1',
'title' => 0,
'body' => 'body',
'modified' => '0'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertFalse($result);
$TestModel->validate['modified'] = array('allowEmpty' => false, 'rule' => 'date');
$data = array('TestValidate' => array('modified' => null));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertFalse($result);
$data = array('TestValidate' => array('modified' => false));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertFalse($result);
$data = array('TestValidate' => array('modified' => ''));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertFalse($result);
$data = array('TestValidate' => array(
'modified' => '2007-05-01'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertTrue($result);
$TestModel->validate['slug'] = array('allowEmpty' => false, 'rule' => array('maxLength', 45));
$data = array('TestValidate' => array(
'user_id' => '1',
'title' => 0,
'body' => 'body',
'slug' => ''
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertFalse($result);
$data = array('TestValidate' => array(
'user_id' => '1',
'title' => 0,
'body' => 'body',
'slug' => 'slug-right-here'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertTrue($result);
$data = array('TestValidate' => array(
'user_id' => '1',
'title' => 0,
'body' => 'body',
'slug' => 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertFalse($result);
$TestModel->validate = array(
'number' => array(
'rule' => 'validateNumber',
'min' => 3,
'max' => 5
),
'title' => array(
'allowEmpty' => false,
'rule' => 'notEmpty'
));
$data = array('TestValidate' => array(
'title' => 'title',
'number' => '0'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertFalse($result);
$data = array('TestValidate' => array(
'title' => 'title',
'number' => 0
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertFalse($result);
$data = array('TestValidate' => array(
'title' => 'title',
'number' => '3'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertTrue($result);
$data = array('TestValidate' => array(
'title' => 'title',
'number' => 3
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertTrue($result);
$TestModel->validate = array(
'number' => array(
'rule' => 'validateNumber',
'min' => 5,
'max' => 10
),
'title' => array(
'allowEmpty' => false,
'rule' => 'notEmpty'
));
$data = array('TestValidate' => array(
'title' => 'title',
'number' => '3'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertFalse($result);
$data = array('TestValidate' => array(
'title' => 'title',
'number' => 3
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertFalse($result);
$TestModel->validate = array(
'title' => array(
'allowEmpty' => false,
'rule' => 'validateTitle'
));
$data = array('TestValidate' => array('title' => ''));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertFalse($result);
$data = array('TestValidate' => array('title' => 'new title'));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertFalse($result);
$data = array('TestValidate' => array('title' => 'title-new'));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertTrue($result);
$TestModel->validate = array('title' => array(
'allowEmpty' => true,
'rule' => 'validateTitle'
));
$data = array('TestValidate' => array('title' => ''));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertTrue($result);
$TestModel->validate = array(
'title' => array(
'length' => array(
'allowEmpty' => true,
'rule' => array('maxLength', 10)
)));
$data = array('TestValidate' => array('title' => ''));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertTrue($result);
$TestModel->validate = array(
'title' => array(
'rule' => array('userDefined', 'Article', 'titleDuplicate')
));
$data = array('TestValidate' => array('title' => 'My Article Title'));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertFalse($result);
$data = array('TestValidate' => array(
'title' => 'My Article With a Different Title'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertTrue($result);
$TestModel->validate = array(
'title' => array(
'tooShort' => array('rule' => array('minLength', 50)),
'onlyLetters' => array('rule' => '/^[a-z]+$/i')
),
);
$data = array('TestValidate' => array(
'title' => 'I am a short string'
));
$TestModel->create($data);
$result = $TestModel->validates();
$this->assertFalse($result);
$result = $TestModel->validationErrors;
$expected = array(
'title' => 'onlyLetters'
);
$this->assertEqual($result, $expected);
$TestModel->validate = array(
'title' => array(
'tooShort' => array(
'rule' => array('minLength', 50),
'last' => true
),
'onlyLetters' => array('rule' => '/^[a-z]+$/i')
),
);
$data = array('TestValidate' => array(
'title' => 'I am a short string'
));
$TestModel->create($data);
$result = $TestModel->validates();
$this->assertFalse($result);
$result = $TestModel->validationErrors;
$expected = array(
'title' => 'tooShort'
);
$this->assertEqual($result, $expected);
}
/**
* test that validates() checks all the 'with' associations as well for validation
* as this can cause partial/wrong data insertion.

0 comments on commit 0d4a500

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