2009-07-24 22:17:14 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ModelValidationTest file
|
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* PHP 5
|
2009-07-24 22:17:14 +00:00
|
|
|
*
|
2012-04-27 02:49:18 +00:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
|
2012-03-13 02:46:07 +00:00
|
|
|
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-07-24 22:17:14 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice
|
2009-07-24 22:17:14 +00:00
|
|
|
*
|
2012-03-13 02:46:07 +00:00
|
|
|
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2012-04-27 02:49:18 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Model
|
2009-07-24 22:17:14 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.4206
|
2010-10-03 16:27:27 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2009-07-24 22:17:14 +00:00
|
|
|
*/
|
2011-04-10 21:10:30 +00:00
|
|
|
require_once dirname(__FILE__) . DS . 'ModelTestBase.php';
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* ModelValidationTest
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Model
|
2009-07-24 22:17:14 +00:00
|
|
|
*/
|
|
|
|
class ModelValidationTest extends BaseModelTest {
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* Tests validation parameter order in custom validation methods
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testValidationParams() {
|
2010-11-13 04:05:44 +00:00
|
|
|
$TestModel = new ValidationTest1();
|
2009-07-24 22:17:14 +00:00
|
|
|
$TestModel->validate['title'] = array(
|
|
|
|
'rule' => 'customValidatorWithParams',
|
|
|
|
'required' => true
|
|
|
|
);
|
|
|
|
$TestModel->create(array('title' => 'foo'));
|
|
|
|
$TestModel->invalidFields();
|
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
'data' => array(
|
|
|
|
'title' => 'foo'
|
|
|
|
),
|
|
|
|
'validator' => array(
|
|
|
|
'rule' => 'customValidatorWithParams',
|
|
|
|
'on' => null,
|
2010-12-26 06:40:02 +00:00
|
|
|
'last' => true,
|
2009-07-24 22:17:14 +00:00
|
|
|
'allowEmpty' => false,
|
|
|
|
'required' => true
|
|
|
|
),
|
|
|
|
'or' => true,
|
2012-03-19 01:20:17 +00:00
|
|
|
'ignoreOnSame' => 'id'
|
2009-07-24 22:17:14 +00:00
|
|
|
);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $TestModel->validatorParams);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$TestModel->validate['title'] = array(
|
|
|
|
'rule' => 'customValidatorWithMessage',
|
|
|
|
'required' => true
|
|
|
|
);
|
|
|
|
$expected = array(
|
2010-12-26 06:40:02 +00:00
|
|
|
'title' => array('This field will *never* validate! Muhahaha!')
|
2009-07-24 22:17:14 +00:00
|
|
|
);
|
|
|
|
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $TestModel->invalidFields());
|
2010-01-02 04:34:11 +00:00
|
|
|
|
|
|
|
$TestModel->validate['title'] = array(
|
|
|
|
'rule' => array('customValidatorWithSixParams', 'one', 'two', null, 'four'),
|
|
|
|
'required' => true
|
|
|
|
);
|
|
|
|
$TestModel->create(array('title' => 'foo'));
|
|
|
|
$TestModel->invalidFields();
|
|
|
|
$expected = array(
|
|
|
|
'data' => array(
|
|
|
|
'title' => 'foo'
|
|
|
|
),
|
|
|
|
'one' => 'one',
|
|
|
|
'two' => 'two',
|
|
|
|
'three' => null,
|
|
|
|
'four' => 'four',
|
|
|
|
'five' => array(
|
|
|
|
'rule' => array(1 => 'one', 2 => 'two', 3 => null, 4 => 'four'),
|
|
|
|
'on' => null,
|
2010-12-26 06:40:02 +00:00
|
|
|
'last' => true,
|
2010-01-02 04:34:11 +00:00
|
|
|
'allowEmpty' => false,
|
|
|
|
'required' => true
|
|
|
|
),
|
|
|
|
'six' => 6
|
|
|
|
);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $TestModel->validatorParams);
|
2010-01-02 04:34:11 +00:00
|
|
|
|
|
|
|
$TestModel->validate['title'] = array(
|
|
|
|
'rule' => array('customValidatorWithSixParams', 'one', array('two'), null, 'four', array('five' => 5)),
|
|
|
|
'required' => true
|
|
|
|
);
|
|
|
|
$TestModel->create(array('title' => 'foo'));
|
|
|
|
$TestModel->invalidFields();
|
|
|
|
$expected = array(
|
|
|
|
'data' => array(
|
|
|
|
'title' => 'foo'
|
|
|
|
),
|
|
|
|
'one' => 'one',
|
|
|
|
'two' => array('two'),
|
|
|
|
'three' => null,
|
|
|
|
'four' => 'four',
|
|
|
|
'five' => array('five' => 5),
|
|
|
|
'six' => array(
|
|
|
|
'rule' => array(1 => 'one', 2 => array('two'), 3 => null, 4 => 'four', 5 => array('five' => 5)),
|
|
|
|
'on' => null,
|
2010-12-26 06:40:02 +00:00
|
|
|
'last' => true,
|
2010-01-02 04:34:11 +00:00
|
|
|
'allowEmpty' => false,
|
|
|
|
'required' => true
|
|
|
|
)
|
|
|
|
);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $TestModel->validatorParams);
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* Tests validation parameter fieldList in invalidFields
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testInvalidFieldsWithFieldListParams() {
|
2010-11-13 04:05:44 +00:00
|
|
|
$TestModel = new ValidationTest1();
|
2009-07-24 22:17:14 +00:00
|
|
|
$TestModel->validate = $validate = array(
|
|
|
|
'title' => array(
|
2011-02-13 17:22:29 +00:00
|
|
|
'rule' => 'alphaNumeric',
|
2009-07-24 22:17:14 +00:00
|
|
|
'required' => true
|
|
|
|
),
|
|
|
|
'name' => array(
|
2011-02-13 17:22:29 +00:00
|
|
|
'rule' => 'alphaNumeric',
|
2009-07-24 22:17:14 +00:00
|
|
|
'required' => true
|
|
|
|
));
|
2011-02-13 17:22:29 +00:00
|
|
|
$TestModel->set(array('title' => '$$', 'name' => '##'));
|
2009-07-24 22:17:14 +00:00
|
|
|
$TestModel->invalidFields(array('fieldList' => array('title')));
|
|
|
|
$expected = array(
|
2010-12-26 06:40:02 +00:00
|
|
|
'title' => array('This field cannot be left blank')
|
2009-07-24 22:17:14 +00:00
|
|
|
);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $TestModel->validationErrors);
|
2009-07-24 22:17:14 +00:00
|
|
|
$TestModel->validationErrors = array();
|
|
|
|
|
|
|
|
$TestModel->invalidFields(array('fieldList' => array('name')));
|
|
|
|
$expected = array(
|
2010-12-26 06:40:02 +00:00
|
|
|
'name' => array('This field cannot be left blank')
|
2009-07-24 22:17:14 +00:00
|
|
|
);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $TestModel->validationErrors);
|
2009-07-24 22:17:14 +00:00
|
|
|
$TestModel->validationErrors = array();
|
|
|
|
|
|
|
|
$TestModel->invalidFields(array('fieldList' => array('name', 'title')));
|
|
|
|
$expected = array(
|
2010-12-26 06:40:02 +00:00
|
|
|
'name' => array('This field cannot be left blank'),
|
|
|
|
'title' => array('This field cannot be left blank')
|
2009-07-24 22:17:14 +00:00
|
|
|
);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $TestModel->validationErrors);
|
2009-07-24 22:17:14 +00:00
|
|
|
$TestModel->validationErrors = array();
|
|
|
|
|
|
|
|
$TestModel->whitelist = array('name');
|
|
|
|
$TestModel->invalidFields();
|
2010-12-26 06:40:02 +00:00
|
|
|
$expected = array('name' => array('This field cannot be left blank'));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $TestModel->validationErrors);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($TestModel->validate, $validate);
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
|
|
|
|
2010-08-22 02:48:59 +00:00
|
|
|
/**
|
|
|
|
* Test that invalidFields() integrates well with save(). And that fieldList can be an empty type.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testInvalidFieldsWhitelist() {
|
2010-11-13 04:05:44 +00:00
|
|
|
$TestModel = new ValidationTest1();
|
2011-02-13 17:22:29 +00:00
|
|
|
$TestModel->validate = array(
|
2010-08-22 02:48:59 +00:00
|
|
|
'title' => array(
|
2011-02-13 17:22:29 +00:00
|
|
|
'rule' => 'alphaNumeric',
|
2010-08-22 02:48:59 +00:00
|
|
|
'required' => true
|
|
|
|
),
|
|
|
|
'name' => array(
|
|
|
|
'rule' => 'alphaNumeric',
|
|
|
|
'required' => true
|
|
|
|
));
|
|
|
|
|
|
|
|
$TestModel->whitelist = array('name');
|
2011-02-13 17:22:29 +00:00
|
|
|
$TestModel->save(array('name' => '#$$#', 'title' => '$$$$'));
|
2010-08-22 02:48:59 +00:00
|
|
|
|
2010-12-26 06:40:02 +00:00
|
|
|
$expected = array('name' => array('This field cannot be left blank'));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $TestModel->validationErrors);
|
2010-08-22 02:48:59 +00:00
|
|
|
}
|
|
|
|
|
2010-01-20 20:12:54 +00:00
|
|
|
/**
|
|
|
|
* testValidates method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testValidates() {
|
2010-11-13 04:05:44 +00:00
|
|
|
$TestModel = new TestValidate();
|
2010-01-20 20:12:54 +00:00
|
|
|
|
|
|
|
$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);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($data, $result);
|
2010-01-20 20:12:54 +00:00
|
|
|
$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);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($data, $result);
|
2010-01-20 20:12:54 +00:00
|
|
|
$result = $TestModel->validates();
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$data = array('TestValidate' => array(
|
|
|
|
'user_id' => '1',
|
|
|
|
'title' => 0,
|
|
|
|
'body' => 'body'
|
|
|
|
));
|
|
|
|
$result = $TestModel->create($data);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($data, $result);
|
2010-01-20 20:12:54 +00:00
|
|
|
$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);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($data, $result);
|
2010-01-20 20:12:54 +00:00
|
|
|
$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);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($data, $result);
|
2010-01-20 20:12:54 +00:00
|
|
|
$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);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($data, $result);
|
2010-01-20 20:12:54 +00:00
|
|
|
$result = $TestModel->validates();
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$data = array('TestValidate' => array(
|
|
|
|
'user_id' => '1',
|
|
|
|
'title' => 0,
|
|
|
|
'body' => 'body',
|
|
|
|
'modified' => 0
|
|
|
|
));
|
|
|
|
$result = $TestModel->create($data);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($data, $result);
|
2010-01-20 20:12:54 +00:00
|
|
|
$result = $TestModel->validates();
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$data = array('TestValidate' => array(
|
|
|
|
'user_id' => '1',
|
|
|
|
'title' => 0,
|
|
|
|
'body' => 'body',
|
|
|
|
'modified' => '0'
|
|
|
|
));
|
|
|
|
$result = $TestModel->create($data);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($data, $result);
|
2010-01-20 20:12:54 +00:00
|
|
|
$result = $TestModel->validates();
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$TestModel->validate['modified'] = array('allowEmpty' => false, 'rule' => 'date');
|
|
|
|
|
|
|
|
$data = array('TestValidate' => array('modified' => null));
|
|
|
|
$result = $TestModel->create($data);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($data, $result);
|
2010-01-20 20:12:54 +00:00
|
|
|
$result = $TestModel->validates();
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$data = array('TestValidate' => array('modified' => false));
|
|
|
|
$result = $TestModel->create($data);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($data, $result);
|
2010-01-20 20:12:54 +00:00
|
|
|
$result = $TestModel->validates();
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$data = array('TestValidate' => array('modified' => ''));
|
|
|
|
$result = $TestModel->create($data);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($data, $result);
|
2010-01-20 20:12:54 +00:00
|
|
|
$result = $TestModel->validates();
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$data = array('TestValidate' => array(
|
|
|
|
'modified' => '2007-05-01'
|
|
|
|
));
|
|
|
|
$result = $TestModel->create($data);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($data, $result);
|
2010-01-20 20:12:54 +00:00
|
|
|
$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);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($data, $result);
|
2010-01-20 20:12:54 +00:00
|
|
|
$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);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($data, $result);
|
2010-01-20 20:12:54 +00:00
|
|
|
$result = $TestModel->validates();
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$data = array('TestValidate' => array(
|
|
|
|
'user_id' => '1',
|
|
|
|
'title' => 0,
|
|
|
|
'body' => 'body',
|
|
|
|
'slug' => 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'
|
|
|
|
));
|
|
|
|
$result = $TestModel->create($data);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($data, $result);
|
2010-01-20 20:12:54 +00:00
|
|
|
$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);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($data, $result);
|
2010-01-20 20:12:54 +00:00
|
|
|
$result = $TestModel->validates();
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$data = array('TestValidate' => array(
|
|
|
|
'title' => 'title',
|
|
|
|
'number' => 0
|
|
|
|
));
|
|
|
|
$result = $TestModel->create($data);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($data, $result);
|
2010-01-20 20:12:54 +00:00
|
|
|
$result = $TestModel->validates();
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$data = array('TestValidate' => array(
|
|
|
|
'title' => 'title',
|
|
|
|
'number' => '3'
|
|
|
|
));
|
|
|
|
$result = $TestModel->create($data);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($data, $result);
|
2010-01-20 20:12:54 +00:00
|
|
|
$result = $TestModel->validates();
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$data = array('TestValidate' => array(
|
|
|
|
'title' => 'title',
|
|
|
|
'number' => 3
|
|
|
|
));
|
|
|
|
$result = $TestModel->create($data);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($data, $result);
|
2010-01-20 20:12:54 +00:00
|
|
|
$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);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($data, $result);
|
2010-01-20 20:12:54 +00:00
|
|
|
$result = $TestModel->validates();
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$data = array('TestValidate' => array(
|
|
|
|
'title' => 'title',
|
|
|
|
'number' => 3
|
|
|
|
));
|
|
|
|
$result = $TestModel->create($data);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($data, $result);
|
2010-01-20 20:12:54 +00:00
|
|
|
$result = $TestModel->validates();
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$TestModel->validate = array(
|
|
|
|
'title' => array(
|
|
|
|
'allowEmpty' => false,
|
|
|
|
'rule' => 'validateTitle'
|
|
|
|
));
|
|
|
|
|
|
|
|
$data = array('TestValidate' => array('title' => ''));
|
|
|
|
$result = $TestModel->create($data);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($data, $result);
|
2010-01-20 20:12:54 +00:00
|
|
|
$result = $TestModel->validates();
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$data = array('TestValidate' => array('title' => 'new title'));
|
|
|
|
$result = $TestModel->create($data);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($data, $result);
|
2010-01-20 20:12:54 +00:00
|
|
|
$result = $TestModel->validates();
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$data = array('TestValidate' => array('title' => 'title-new'));
|
|
|
|
$result = $TestModel->create($data);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($data, $result);
|
2010-01-20 20:12:54 +00:00
|
|
|
$result = $TestModel->validates();
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$TestModel->validate = array('title' => array(
|
|
|
|
'allowEmpty' => true,
|
|
|
|
'rule' => 'validateTitle'
|
|
|
|
));
|
|
|
|
$data = array('TestValidate' => array('title' => ''));
|
|
|
|
$result = $TestModel->create($data);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($data, $result);
|
2010-01-20 20:12:54 +00:00
|
|
|
$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);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($data, $result);
|
2010-01-20 20:12:54 +00:00
|
|
|
$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);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($data, $result);
|
2010-01-20 20:12:54 +00:00
|
|
|
$result = $TestModel->validates();
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$data = array('TestValidate' => array(
|
|
|
|
'title' => 'My Article With a Different Title'
|
|
|
|
));
|
|
|
|
$result = $TestModel->create($data);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($data, $result);
|
2010-01-20 20:12:54 +00:00
|
|
|
$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(
|
2010-12-26 06:40:02 +00:00
|
|
|
'title' => array('tooShort')
|
2010-01-20 20:12:54 +00:00
|
|
|
);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2010-01-20 20:12:54 +00:00
|
|
|
|
|
|
|
$TestModel->validate = array(
|
|
|
|
'title' => array(
|
|
|
|
'tooShort' => array(
|
|
|
|
'rule' => array('minLength', 50),
|
2010-12-26 06:40:02 +00:00
|
|
|
'last' => false
|
2010-01-20 20:12:54 +00:00
|
|
|
),
|
|
|
|
'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(
|
2010-12-26 06:40:02 +00:00
|
|
|
'title' => array('tooShort', 'onlyLetters')
|
2010-01-20 20:12:54 +00:00
|
|
|
);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2010-01-20 20:12:54 +00:00
|
|
|
}
|
|
|
|
|
2009-12-15 04:21:26 +00:00
|
|
|
/**
|
|
|
|
* test that validates() checks all the 'with' associations as well for validation
|
|
|
|
* as this can cause partial/wrong data insertion.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testValidatesWithAssociations() {
|
2010-06-01 04:49:11 +00:00
|
|
|
$this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
|
2009-12-15 04:21:26 +00:00
|
|
|
$data = array(
|
|
|
|
'Something' => array(
|
|
|
|
'id' => 5,
|
|
|
|
'title' => 'Extra Fields',
|
|
|
|
'body' => 'Extra Fields Body',
|
|
|
|
'published' => '1'
|
|
|
|
),
|
|
|
|
'SomethingElse' => array(
|
|
|
|
array('something_else_id' => 1, 'doomed' => '')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2010-11-13 04:05:44 +00:00
|
|
|
$Something = new Something();
|
2011-04-29 15:49:33 +00:00
|
|
|
$JoinThing = $Something->JoinThing;
|
2009-12-15 04:21:26 +00:00
|
|
|
|
|
|
|
$JoinThing->validate = array('doomed' => array('rule' => 'notEmpty'));
|
|
|
|
|
2010-12-26 06:40:02 +00:00
|
|
|
$expectedError = array('doomed' => array('This field cannot be left blank'));
|
2009-12-15 04:21:26 +00:00
|
|
|
|
|
|
|
$Something->create();
|
|
|
|
$result = $Something->save($data);
|
2012-02-23 23:29:53 +00:00
|
|
|
$this->assertFalse($result, 'Save occurred even when with models failed. %s');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expectedError, $JoinThing->validationErrors);
|
2009-12-15 04:21:26 +00:00
|
|
|
$count = $Something->find('count', array('conditions' => array('Something.id' => $data['Something']['id'])));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertSame($count, 0);
|
2009-12-15 04:21:26 +00:00
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Something' => array(
|
|
|
|
'id' => 5,
|
|
|
|
'title' => 'Extra Fields',
|
|
|
|
'body' => 'Extra Fields Body',
|
|
|
|
'published' => '1'
|
|
|
|
),
|
|
|
|
'SomethingElse' => array(
|
|
|
|
array('something_else_id' => 1, 'doomed' => 1),
|
|
|
|
array('something_else_id' => 1, 'doomed' => '')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$Something->create();
|
|
|
|
$result = $Something->save($data);
|
2012-02-23 23:29:53 +00:00
|
|
|
$this->assertFalse($result, 'Save occurred even when with models failed. %s');
|
2009-12-15 04:21:26 +00:00
|
|
|
|
|
|
|
$joinRecords = $JoinThing->find('count', array(
|
|
|
|
'conditions' => array('JoinThing.something_id' => $data['Something']['id'])
|
|
|
|
));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(0, $joinRecords, 'Records were saved on the join table. %s');
|
2009-12-15 04:21:26 +00:00
|
|
|
}
|
2009-12-27 17:34:27 +00:00
|
|
|
|
2009-12-15 04:21:26 +00:00
|
|
|
/**
|
|
|
|
* test that saveAll and with models with validation interact well
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testValidatesWithModelsAndSaveAll() {
|
2009-12-15 04:21:26 +00:00
|
|
|
$data = array(
|
|
|
|
'Something' => array(
|
|
|
|
'id' => 5,
|
|
|
|
'title' => 'Extra Fields',
|
|
|
|
'body' => 'Extra Fields Body',
|
|
|
|
'published' => '1'
|
|
|
|
),
|
|
|
|
'SomethingElse' => array(
|
|
|
|
array('something_else_id' => 1, 'doomed' => '')
|
|
|
|
)
|
|
|
|
);
|
2010-11-13 04:05:44 +00:00
|
|
|
$Something = new Something();
|
2011-04-29 15:49:33 +00:00
|
|
|
$JoinThing = $Something->JoinThing;
|
2009-12-15 04:21:26 +00:00
|
|
|
|
|
|
|
$JoinThing->validate = array('doomed' => array('rule' => 'notEmpty'));
|
2010-12-26 06:40:02 +00:00
|
|
|
$expectedError = array('doomed' => array('This field cannot be left blank'));
|
2009-12-15 04:21:26 +00:00
|
|
|
|
|
|
|
$Something->create();
|
|
|
|
$result = $Something->saveAll($data, array('validate' => 'only'));
|
|
|
|
$this->assertFalse($result);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expectedError, $JoinThing->validationErrors);
|
2009-12-15 04:21:26 +00:00
|
|
|
|
|
|
|
$Something->create();
|
|
|
|
$result = $Something->saveAll($data, array('validate' => 'first'));
|
2010-10-14 03:09:55 +00:00
|
|
|
$this->assertFalse($result);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expectedError, $JoinThing->validationErrors);
|
2009-07-24 22:17:14 +00:00
|
|
|
|
2009-12-15 04:21:26 +00:00
|
|
|
$count = $Something->find('count', array('conditions' => array('Something.id' => $data['Something']['id'])));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertSame($count, 0);
|
2009-12-15 04:21:26 +00:00
|
|
|
|
|
|
|
$joinRecords = $JoinThing->find('count', array(
|
|
|
|
'conditions' => array('JoinThing.something_id' => $data['Something']['id'])
|
|
|
|
));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(0, $joinRecords, 'Records were saved on the join table. %s');
|
2009-12-15 04:21:26 +00:00
|
|
|
}
|
2009-12-27 17:34:27 +00:00
|
|
|
|
2011-09-23 13:23:26 +00:00
|
|
|
/**
|
|
|
|
* test that saveAll and with models at initial insert (no id has set yet)
|
|
|
|
* with validation interact well
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testValidatesWithModelsAndSaveAllWithoutId() {
|
|
|
|
$this->loadFixtures('Post', 'Author');
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Author' => array(
|
|
|
|
'name' => 'Foo Bar',
|
|
|
|
),
|
|
|
|
'Post' => array(
|
|
|
|
array('title' => 'Hello'),
|
|
|
|
array('title' => 'World'),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$Author = new Author();
|
|
|
|
$Post = $Author->Post;
|
|
|
|
|
|
|
|
$Post->validate = array('author_id' => array('rule' => 'numeric'));
|
|
|
|
|
|
|
|
$Author->create();
|
|
|
|
$result = $Author->saveAll($data, array('validate' => 'only'));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$Author->create();
|
|
|
|
$result = $Author->saveAll($data, array('validate' => 'first'));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
$this->assertFalse(is_null($Author->id));
|
|
|
|
|
|
|
|
$id = $Author->id;
|
|
|
|
$count = $Author->find('count', array('conditions' => array('Author.id' => $id)));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertSame($count, 1);
|
2011-09-23 13:23:26 +00:00
|
|
|
|
|
|
|
$count = $Post->find('count', array(
|
|
|
|
'conditions' => array('Post.author_id' => $id)
|
|
|
|
));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($count, count($data['Post']));
|
2011-09-23 13:23:26 +00:00
|
|
|
}
|
|
|
|
|
2009-10-01 02:34:29 +00:00
|
|
|
/**
|
|
|
|
* Test that missing validation methods trigger errors in development mode.
|
2012-02-23 23:29:53 +00:00
|
|
|
* Helps to make development easier.
|
2009-10-01 02:34:29 +00:00
|
|
|
*
|
2011-09-10 16:23:28 +00:00
|
|
|
* @expectedException PHPUnit_Framework_Error
|
2009-10-01 02:34:29 +00:00
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testMissingValidationErrorTriggering() {
|
2009-10-01 02:34:29 +00:00
|
|
|
Configure::write('debug', 2);
|
|
|
|
|
2010-11-13 04:05:44 +00:00
|
|
|
$TestModel = new ValidationTest1();
|
2009-10-01 02:34:29 +00:00
|
|
|
$TestModel->create(array('title' => 'foo'));
|
|
|
|
$TestModel->validate = array(
|
|
|
|
'title' => array(
|
|
|
|
'rule' => array('thisOneBringsThePain'),
|
|
|
|
'required' => true
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$TestModel->invalidFields(array('fieldList' => array('title')));
|
2010-06-01 04:49:11 +00:00
|
|
|
}
|
2009-10-01 02:34:29 +00:00
|
|
|
|
2010-06-01 04:49:11 +00:00
|
|
|
/**
|
|
|
|
* Test that missing validation methods does not trigger errors in production mode.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testMissingValidationErrorNoTriggering() {
|
2009-10-01 02:34:29 +00:00
|
|
|
Configure::write('debug', 0);
|
2010-11-13 04:05:44 +00:00
|
|
|
$TestModel = new ValidationTest1();
|
2010-06-01 04:49:11 +00:00
|
|
|
$TestModel->create(array('title' => 'foo'));
|
|
|
|
$TestModel->validate = array(
|
|
|
|
'title' => array(
|
|
|
|
'rule' => array('thisOneBringsThePain'),
|
|
|
|
'required' => true
|
|
|
|
)
|
|
|
|
);
|
2009-10-01 02:34:29 +00:00
|
|
|
$TestModel->invalidFields(array('fieldList' => array('title')));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array(), $TestModel->validationErrors);
|
2009-10-01 02:34:29 +00:00
|
|
|
}
|
|
|
|
|
2011-07-31 01:03:56 +00:00
|
|
|
/**
|
|
|
|
* Test placeholder replacement when validation message is an array
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testValidationMessageAsArray() {
|
|
|
|
$TestModel = new ValidationTest1();
|
|
|
|
$TestModel->validate = array(
|
|
|
|
'title' => array(
|
|
|
|
'minLength' => array(
|
|
|
|
'rule' => array('minLength', 6),
|
2011-09-11 23:40:34 +00:00
|
|
|
'required' => true,
|
|
|
|
'message' => 'Minimum length allowed is %d chars',
|
2011-07-31 01:03:56 +00:00
|
|
|
'last' => false
|
|
|
|
),
|
|
|
|
'between' => array(
|
|
|
|
'rule' => array('between', 5, 15),
|
|
|
|
'message' => array('You may enter up to %s chars (minimum is %s chars)', 14, 6)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2011-09-11 23:40:34 +00:00
|
|
|
|
|
|
|
$TestModel->create();
|
|
|
|
$TestModel->invalidFields();
|
|
|
|
$expected = array(
|
|
|
|
'title' => array(
|
|
|
|
'Minimum length allowed is 6 chars',
|
|
|
|
)
|
|
|
|
);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $TestModel->validationErrors);
|
2011-09-11 23:40:34 +00:00
|
|
|
|
|
|
|
$TestModel->create(array('title' => 'foo'));
|
2011-07-31 01:03:56 +00:00
|
|
|
$TestModel->invalidFields();
|
|
|
|
$expected = array(
|
|
|
|
'title' => array(
|
|
|
|
'Minimum length allowed is 6 chars',
|
|
|
|
'You may enter up to 14 chars (minimum is 6 chars)'
|
|
|
|
)
|
|
|
|
);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $TestModel->validationErrors);
|
2011-07-31 01:03:56 +00:00
|
|
|
}
|
|
|
|
|
2012-04-22 17:55:07 +00:00
|
|
|
/**
|
|
|
|
* Test validation message translation
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testValidationMessageTranslation() {
|
|
|
|
$lang = Configure::read('Config.language');
|
|
|
|
Configure::write('Config.language', 'en');
|
|
|
|
App::build(array(
|
|
|
|
'Locale' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Locale' . DS),
|
|
|
|
), App::RESET);
|
|
|
|
|
|
|
|
$TestModel = new ValidationTest1();
|
|
|
|
$TestModel->validationDomain = 'validation_messages';
|
|
|
|
$TestModel->validate = array(
|
|
|
|
'title' => array(
|
|
|
|
array(
|
|
|
|
'rule' => array('customValidationMethod', 'arg1'),
|
|
|
|
'required' => true,
|
|
|
|
'message' => 'Validation failed: %s'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$TestModel->create();
|
|
|
|
$TestModel->invalidFields();
|
|
|
|
$expected = array(
|
|
|
|
'title' => array(
|
|
|
|
'Translated validation failed: Translated arg1',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $TestModel->validationErrors);
|
|
|
|
|
|
|
|
$TestModel->validationDomain = 'default';
|
|
|
|
Configure::write('Config.language', $lang);
|
|
|
|
App::build();
|
|
|
|
}
|
|
|
|
|
2012-01-20 03:50:40 +00:00
|
|
|
/**
|
|
|
|
* Test for 'on' => [create|update] in validation rules.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2012-02-17 07:13:12 +00:00
|
|
|
public function testStateValidation() {
|
2012-01-20 03:50:40 +00:00
|
|
|
$this->loadFixtures('Article');
|
|
|
|
$Article = new Article();
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Article' => array(
|
|
|
|
'title' => '',
|
|
|
|
'body' => 'Extra Fields Body',
|
|
|
|
'published' => '1'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$Article->validate = array(
|
|
|
|
'title' => array(
|
|
|
|
'notempty' => array(
|
|
|
|
'rule' => 'notEmpty',
|
|
|
|
'on' => 'create'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$Article->create($data);
|
|
|
|
$this->assertFalse($Article->validates());
|
|
|
|
|
|
|
|
$Article->save(null, array('validate' => false));
|
|
|
|
$data['Article']['id'] = $Article->id;
|
|
|
|
$Article->set($data);
|
|
|
|
$this->assertTrue($Article->validates());
|
|
|
|
|
|
|
|
unset($data['Article']['id']);
|
|
|
|
$Article->validate = array(
|
|
|
|
'title' => array(
|
|
|
|
'notempty' => array(
|
|
|
|
'rule' => 'notEmpty',
|
|
|
|
'on' => 'update'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$Article->create($data);
|
|
|
|
$this->assertTrue($Article->validates());
|
|
|
|
|
|
|
|
$Article->save(null, array('validate' => false));
|
|
|
|
$data['Article']['id'] = $Article->id;
|
|
|
|
$Article->set($data);
|
|
|
|
$this->assertFalse($Article->validates());
|
|
|
|
}
|
|
|
|
|
2012-02-07 11:15:04 +00:00
|
|
|
/**
|
|
|
|
* Test for 'required' => [create|update] in validation rules.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2012-02-23 13:45:15 +00:00
|
|
|
public function testStateRequiredValidation() {
|
2012-02-07 11:15:04 +00:00
|
|
|
$this->loadFixtures('Article');
|
|
|
|
$Article = new Article();
|
|
|
|
|
|
|
|
// no title field present
|
|
|
|
$data = array(
|
|
|
|
'Article' => array(
|
|
|
|
'body' => 'Extra Fields Body',
|
|
|
|
'published' => '1'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$Article->validate = array(
|
|
|
|
'title' => array(
|
|
|
|
'notempty' => array(
|
|
|
|
'rule' => 'notEmpty',
|
|
|
|
'required' => 'create'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$Article->create($data);
|
|
|
|
$this->assertFalse($Article->validates());
|
|
|
|
|
|
|
|
$Article->save(null, array('validate' => false));
|
|
|
|
$data['Article']['id'] = $Article->id;
|
|
|
|
$Article->set($data);
|
|
|
|
$this->assertTrue($Article->validates());
|
|
|
|
|
|
|
|
unset($data['Article']['id']);
|
|
|
|
$Article->validate = array(
|
|
|
|
'title' => array(
|
|
|
|
'notempty' => array(
|
|
|
|
'rule' => 'notEmpty',
|
|
|
|
'required' => 'update'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$Article->create($data);
|
|
|
|
$this->assertTrue($Article->validates());
|
|
|
|
|
|
|
|
$Article->save(null, array('validate' => false));
|
|
|
|
$data['Article']['id'] = $Article->id;
|
|
|
|
$Article->set($data);
|
|
|
|
$this->assertFalse($Article->validates());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that 'required' and 'on' are not conflicting
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2012-02-23 13:45:15 +00:00
|
|
|
public function testOnRequiredConflictValidation() {
|
2012-02-07 11:15:04 +00:00
|
|
|
$this->loadFixtures('Article');
|
|
|
|
$Article = new Article();
|
|
|
|
|
|
|
|
// no title field present
|
|
|
|
$data = array(
|
|
|
|
'Article' => array(
|
|
|
|
'body' => 'Extra Fields Body',
|
|
|
|
'published' => '1'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$Article->validate = array(
|
|
|
|
'title' => array(
|
|
|
|
'notempty' => array(
|
|
|
|
'rule' => 'notEmpty',
|
|
|
|
'required' => 'create',
|
|
|
|
'on' => 'create'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$Article->create($data);
|
|
|
|
$this->assertFalse($Article->validates());
|
|
|
|
|
|
|
|
$Article->validate = array(
|
|
|
|
'title' => array(
|
|
|
|
'notempty' => array(
|
|
|
|
'rule' => 'notEmpty',
|
|
|
|
'required' => 'update',
|
|
|
|
'on' => 'create'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$Article->create($data);
|
|
|
|
$this->assertTrue($Article->validates());
|
|
|
|
|
|
|
|
$Article->validate = array(
|
|
|
|
'title' => array(
|
|
|
|
'notempty' => array(
|
|
|
|
'rule' => 'notEmpty',
|
|
|
|
'required' => 'create',
|
|
|
|
'on' => 'update'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$Article->create($data);
|
|
|
|
$this->assertTrue($Article->validates());
|
|
|
|
|
|
|
|
$Article->validate = array(
|
|
|
|
'title' => array(
|
|
|
|
'notempty' => array(
|
|
|
|
'rule' => 'notEmpty',
|
|
|
|
'required' => 'update',
|
|
|
|
'on' => 'update'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$Article->create($data);
|
|
|
|
$this->assertTrue($Article->validates());
|
|
|
|
|
|
|
|
$Article->validate = array(
|
|
|
|
'title' => array(
|
|
|
|
'notempty' => array(
|
|
|
|
'rule' => 'notEmpty',
|
|
|
|
'required' => 'create',
|
|
|
|
'on' => 'create'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$Article->save(null, array('validate' => false));
|
|
|
|
$data['Article']['id'] = $Article->id;
|
|
|
|
$Article->set($data);
|
|
|
|
$this->assertTrue($Article->validates());
|
|
|
|
|
|
|
|
$Article->validate = array(
|
|
|
|
'title' => array(
|
|
|
|
'notempty' => array(
|
|
|
|
'rule' => 'notEmpty',
|
|
|
|
'required' => 'update',
|
|
|
|
'on' => 'create'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$Article->set($data);
|
|
|
|
$this->assertTrue($Article->validates());
|
|
|
|
|
|
|
|
$Article->validate = array(
|
|
|
|
'title' => array(
|
|
|
|
'notempty' => array(
|
|
|
|
'rule' => 'notEmpty',
|
|
|
|
'required' => 'create',
|
|
|
|
'on' => 'update'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$Article->set($data);
|
|
|
|
$this->assertTrue($Article->validates());
|
|
|
|
|
|
|
|
$Article->validate = array(
|
|
|
|
'title' => array(
|
|
|
|
'notempty' => array(
|
|
|
|
'rule' => 'notEmpty',
|
|
|
|
'required' => 'update',
|
|
|
|
'on' => 'update'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$Article->set($data);
|
|
|
|
$this->assertFalse($Article->validates());
|
|
|
|
}
|
|
|
|
|
2012-05-10 04:21:27 +00:00
|
|
|
/**
|
|
|
|
* Tests that altering data in a beforeValidate callback will lead to saving those
|
|
|
|
* values in database
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testValidateFirstWithBeforeValidate() {
|
|
|
|
$this->loadFixtures('Article', 'User');
|
|
|
|
$model = new CustomArticle();
|
|
|
|
$model->validate = array(
|
|
|
|
'title' => array(
|
|
|
|
'notempty' => array(
|
|
|
|
'rule' => 'notEmpty',
|
|
|
|
'required' => true,
|
|
|
|
'allowEmpty' => false
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$data = array(
|
|
|
|
'CustomArticle' => array(
|
|
|
|
'body' => 'foo0'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = $model->saveAll($data, array('validate' => 'first'));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$title = $model->field('title', array('body' => 'foo0'));
|
|
|
|
$this->assertEquals('foo', $title);
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
array('body' => 'foo1'),
|
|
|
|
array('body' => 'foo2'),
|
|
|
|
array('body' => 'foo3')
|
|
|
|
);
|
|
|
|
|
|
|
|
$result = $model->saveAll($data, array('validate' => 'first'));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$this->assertEquals('foo', $model->field('title', array('body' => 'foo1')));
|
|
|
|
$this->assertEquals('foo', $model->field('title', array('body' => 'foo2')));
|
|
|
|
$this->assertEquals('foo', $model->field('title', array('body' => 'foo3')));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that altering data in a beforeValidate callback will lead to saving those
|
|
|
|
* values in database
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testValidateAssociatedWithBeforeValidate() {
|
|
|
|
$this->loadFixtures('Article', 'User');
|
|
|
|
$model = new CustomArticle();
|
|
|
|
$model->validate = array(
|
|
|
|
'title' => array(
|
|
|
|
'notempty' => array(
|
|
|
|
'rule' => 'notEmpty',
|
|
|
|
'required' => true
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$articles = array(
|
|
|
|
array('body' => 'foo1'),
|
|
|
|
array('body' => 'foo2'),
|
|
|
|
array('body' => 'foo3')
|
|
|
|
);
|
|
|
|
$user = new User();
|
|
|
|
$user->hasMany['CustomArticle'] = array('foreignKey' => 'user_id');
|
|
|
|
$data = array(
|
|
|
|
'User' => array('user' => 'foo', 'password' => 'bar'),
|
|
|
|
'CustomArticle' => $articles
|
|
|
|
);
|
|
|
|
$result = $user->saveAll($data, array('validate' => 'first'));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$this->assertEquals('foo', $model->field('title', array('body' => 'foo1')));
|
|
|
|
$this->assertEquals('foo', $model->field('title', array('body' => 'foo2')));
|
|
|
|
$this->assertEquals('foo', $model->field('title', array('body' => 'foo3')));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|