2009-07-24 22:17:14 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ModelValidationTest file
|
|
|
|
*
|
2012-04-27 02:49:18 +00:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) 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
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2010-10-03 16:31:21 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice
|
2009-07-24 22:17:14 +00:00
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) 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
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2009-07-24 22:17:14 +00:00
|
|
|
*/
|
2013-05-30 22:11: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
|
|
|
|
2013-06-11 18:03:05 +00:00
|
|
|
/**
|
|
|
|
* override locale to the default (eng).
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
Configure::write('Config.language', 'eng');
|
|
|
|
}
|
|
|
|
|
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,
|
2012-03-22 11:18:57 +00:00
|
|
|
'required' => true,
|
|
|
|
'message' => null
|
2009-07-24 22:17:14 +00:00
|
|
|
),
|
|
|
|
'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,
|
2012-03-22 11:18:57 +00:00
|
|
|
'required' => true,
|
|
|
|
'message' => null
|
2010-01-02 04:34:11 +00:00
|
|
|
),
|
|
|
|
'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,
|
2012-03-22 11:18:57 +00:00
|
|
|
'required' => true,
|
|
|
|
'message' => null
|
2010-01-02 04:34:11 +00:00
|
|
|
)
|
|
|
|
);
|
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);
|
2012-04-29 05:16:56 +00:00
|
|
|
$TestModel->validationErrors = array();
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$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);
|
2012-04-29 05:16:56 +00:00
|
|
|
$TestModel->validationErrors = array();
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$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);
|
2012-04-29 05:16:56 +00:00
|
|
|
$TestModel->validationErrors = array();
|
2009-07-24 22:17:14 +00:00
|
|
|
|
|
|
|
$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
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Test that invalidFields() integrates well with save(). And that fieldList can be an empty type.
|
2010-08-22 02:48:59 +00:00
|
|
|
*
|
|
|
|
* @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',
|
2015-05-17 20:27:16 +00:00
|
|
|
'title' => array('allowEmpty' => false, 'rule' => 'notBlank'),
|
|
|
|
'body' => 'notBlank'
|
2010-01-20 20:12:54 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$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();
|
2012-04-23 11:02:26 +00:00
|
|
|
$this->assertTrue($result);
|
2010-01-20 20:12:54 +00:00
|
|
|
|
|
|
|
$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,
|
2015-05-17 20:27:16 +00:00
|
|
|
'rule' => 'notBlank'
|
2010-01-20 20:12:54 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
$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,
|
2015-05-17 20:27:16 +00:00
|
|
|
'rule' => 'notBlank'
|
2010-01-20 20:12:54 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
$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);
|
2012-04-23 11:02:26 +00:00
|
|
|
$result = $TestModel->validationErrors;
|
|
|
|
$this->assertEquals($expected, $result);
|
2010-01-20 20:12:54 +00:00
|
|
|
}
|
|
|
|
|
2015-08-17 11:19:02 +00:00
|
|
|
/**
|
2015-08-17 12:15:35 +00:00
|
|
|
* test that validates() still performs correctly when useTable = false on the model.
|
2015-08-17 11:19:02 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testValidatesWithNoTable() {
|
|
|
|
$TestModel = new TheVoid();
|
|
|
|
$TestModel->validate = array(
|
|
|
|
'title' => array(
|
|
|
|
'notEmpty' => array(
|
|
|
|
'rule' => array('notBlank'),
|
|
|
|
'required' => true,
|
|
|
|
),
|
|
|
|
'tooShort' => array(
|
|
|
|
'rule' => array('minLength', 10),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$data = array(
|
|
|
|
'TheVoid' => array(
|
|
|
|
'title' => 'too short',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$TestModel->create($data);
|
|
|
|
$result = $TestModel->validates();
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'TheVoid' => array(
|
|
|
|
'id' => '1',
|
|
|
|
'title' => 'A good title',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$TestModel->create($data);
|
|
|
|
$result = $TestModel->validates();
|
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2015-05-17 20:27:16 +00:00
|
|
|
$JoinThing->validate = array('doomed' => array('rule' => 'notBlank'));
|
2009-12-15 04:21:26 +00:00
|
|
|
|
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'])));
|
2013-08-16 18:51:11 +00:00
|
|
|
$this->assertSame(0, $count);
|
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
|
|
|
|
2013-10-07 22:32:11 +00:00
|
|
|
/**
|
|
|
|
* Test that if a behavior modifies the model's whitelist validation gets triggered
|
|
|
|
* properly for those fields.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2013-10-07 21:45:27 +00:00
|
|
|
public function testValidateWithFieldListAndBehavior() {
|
|
|
|
$TestModel = new ValidationTest1();
|
|
|
|
$TestModel->validate = array(
|
|
|
|
'title' => array(
|
2015-05-17 20:27:16 +00:00
|
|
|
'rule' => 'notBlank',
|
2013-10-07 21:45:27 +00:00
|
|
|
),
|
|
|
|
'name' => array(
|
2015-05-17 20:27:16 +00:00
|
|
|
'rule' => 'notBlank',
|
2013-10-07 21:45:27 +00:00
|
|
|
));
|
|
|
|
$TestModel->Behaviors->attach('ValidationRule', array('fields' => array('name')));
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'title' => '',
|
|
|
|
'name' => '',
|
|
|
|
);
|
|
|
|
$result = $TestModel->save($data, array('fieldList' => array('title')));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$expected = array('title' => array('This field cannot be left blank'), 'name' => array('This field cannot be left blank'));
|
|
|
|
$this->assertEquals($expected, $TestModel->validationErrors);
|
|
|
|
}
|
|
|
|
|
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() {
|
2012-04-29 05:39:36 +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
|
|
|
|
2015-05-17 20:27:16 +00:00
|
|
|
$JoinThing->validate = array('doomed' => array('rule' => 'notBlank'));
|
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-04-23 11:02:26 +00:00
|
|
|
$result = $Something->validateAssociated($data);
|
|
|
|
$this->assertFalse($result);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expectedError, $JoinThing->validationErrors);
|
2012-04-29 00:39:29 +00:00
|
|
|
$result = $Something->validator()->validateAssociated($data);
|
2012-04-23 11:02:26 +00:00
|
|
|
$this->assertFalse($result);
|
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'])));
|
2013-08-16 18:51:11 +00:00
|
|
|
$this->assertSame(0, $count);
|
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);
|
2012-04-23 11:02:26 +00:00
|
|
|
$result = $Author->validateAssociated($data);
|
|
|
|
$this->assertTrue($result);
|
|
|
|
$this->assertTrue($result);
|
2011-09-23 13:23:26 +00:00
|
|
|
|
|
|
|
$Author->create();
|
|
|
|
$result = $Author->saveAll($data, array('validate' => 'first'));
|
|
|
|
$this->assertTrue($result);
|
2013-08-16 18:51:11 +00:00
|
|
|
$this->assertNotNull($Author->id);
|
2011-09-23 13:23:26 +00:00
|
|
|
|
|
|
|
$id = $Author->id;
|
|
|
|
$count = $Author->find('count', array('conditions' => array('Author.id' => $id)));
|
2013-08-16 18:51:11 +00:00
|
|
|
$this->assertSame(1, $count);
|
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
|
|
|
|
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(
|
2014-05-13 07:31:03 +00:00
|
|
|
'rule' => array('lengthBetween', 5, 15),
|
2011-07-31 01:03:56 +00:00
|
|
|
'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();
|
|
|
|
$expected = array(
|
|
|
|
'title' => array(
|
|
|
|
'Minimum length allowed is 6 chars',
|
|
|
|
)
|
|
|
|
);
|
2012-04-23 11:02:26 +00:00
|
|
|
$TestModel->invalidFields();
|
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
|
|
|
$expected = array(
|
|
|
|
'title' => array(
|
|
|
|
'Minimum length allowed is 6 chars',
|
|
|
|
'You may enter up to 14 chars (minimum is 6 chars)'
|
|
|
|
)
|
|
|
|
);
|
2012-04-23 11:02:26 +00:00
|
|
|
$TestModel->invalidFields();
|
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();
|
|
|
|
$expected = array(
|
|
|
|
'title' => array(
|
|
|
|
'Translated validation failed: Translated arg1',
|
|
|
|
)
|
|
|
|
);
|
2012-04-23 11:02:26 +00:00
|
|
|
$TestModel->invalidFields();
|
2012-04-22 17:55:07 +00:00
|
|
|
$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(
|
2015-05-17 20:27:16 +00:00
|
|
|
'notBlank' => array(
|
|
|
|
'rule' => 'notBlank',
|
2012-01-20 03:50:40 +00:00
|
|
|
'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(
|
2015-05-17 20:27:16 +00:00
|
|
|
'notBlank' => array(
|
|
|
|
'rule' => 'notBlank',
|
2012-01-20 03:50:40 +00:00
|
|
|
'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(
|
2015-05-17 20:27:16 +00:00
|
|
|
'notBlank' => array(
|
|
|
|
'rule' => 'notBlank',
|
2012-02-07 11:15:04 +00:00
|
|
|
'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(
|
2015-05-17 20:27:16 +00:00
|
|
|
'notBlank' => array(
|
|
|
|
'rule' => 'notBlank',
|
2012-02-07 11:15:04 +00:00
|
|
|
'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(
|
2015-05-17 20:27:16 +00:00
|
|
|
'notBlank' => array(
|
|
|
|
'rule' => 'notBlank',
|
2012-02-07 11:15:04 +00:00
|
|
|
'required' => 'create',
|
|
|
|
'on' => 'create'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$Article->create($data);
|
|
|
|
$this->assertFalse($Article->validates());
|
|
|
|
|
|
|
|
$Article->validate = array(
|
|
|
|
'title' => array(
|
2015-05-17 20:27:16 +00:00
|
|
|
'notBlank' => array(
|
|
|
|
'rule' => 'notBlank',
|
2012-02-07 11:15:04 +00:00
|
|
|
'required' => 'update',
|
|
|
|
'on' => 'create'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$Article->create($data);
|
|
|
|
$this->assertTrue($Article->validates());
|
|
|
|
|
|
|
|
$Article->validate = array(
|
|
|
|
'title' => array(
|
2015-05-17 20:27:16 +00:00
|
|
|
'notBlank' => array(
|
|
|
|
'rule' => 'notBlank',
|
2012-02-07 11:15:04 +00:00
|
|
|
'required' => 'create',
|
|
|
|
'on' => 'update'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$Article->create($data);
|
|
|
|
$this->assertTrue($Article->validates());
|
|
|
|
|
|
|
|
$Article->validate = array(
|
|
|
|
'title' => array(
|
2015-05-17 20:27:16 +00:00
|
|
|
'notBlank' => array(
|
|
|
|
'rule' => 'notBlank',
|
2012-02-07 11:15:04 +00:00
|
|
|
'required' => 'update',
|
|
|
|
'on' => 'update'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$Article->create($data);
|
|
|
|
$this->assertTrue($Article->validates());
|
|
|
|
|
|
|
|
$Article->validate = array(
|
|
|
|
'title' => array(
|
2015-05-17 20:27:16 +00:00
|
|
|
'notBlank' => array(
|
|
|
|
'rule' => 'notBlank',
|
2012-02-07 11:15:04 +00:00
|
|
|
'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(
|
2015-05-17 20:27:16 +00:00
|
|
|
'notBlank' => array(
|
|
|
|
'rule' => 'notBlank',
|
2012-02-07 11:15:04 +00:00
|
|
|
'required' => 'update',
|
|
|
|
'on' => 'create'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$Article->set($data);
|
|
|
|
$this->assertTrue($Article->validates());
|
|
|
|
|
|
|
|
$Article->validate = array(
|
|
|
|
'title' => array(
|
2015-05-17 20:27:16 +00:00
|
|
|
'notBlank' => array(
|
|
|
|
'rule' => 'notBlank',
|
2012-02-07 11:15:04 +00:00
|
|
|
'required' => 'create',
|
|
|
|
'on' => 'update'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$Article->set($data);
|
|
|
|
$this->assertTrue($Article->validates());
|
|
|
|
|
|
|
|
$Article->validate = array(
|
|
|
|
'title' => array(
|
2015-05-17 20:27:16 +00:00
|
|
|
'notBlank' => array(
|
|
|
|
'rule' => 'notBlank',
|
2012-02-07 11:15:04 +00:00
|
|
|
'required' => 'update',
|
|
|
|
'on' => 'update'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$Article->set($data);
|
|
|
|
$this->assertFalse($Article->validates());
|
|
|
|
}
|
|
|
|
|
2012-04-23 11:02:26 +00:00
|
|
|
/**
|
|
|
|
* testSaveAllDeepValidateOnly
|
|
|
|
* tests the validate methods with deeper recursive data
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAllDeepValidateOnly() {
|
|
|
|
$this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
|
|
|
|
$TestModel = new Article();
|
|
|
|
$TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
|
|
|
|
$TestModel->hasAndBelongsToMany = array();
|
2015-05-17 20:27:16 +00:00
|
|
|
$TestModel->Comment->Attachment->validate['attachment'] = 'notBlank';
|
|
|
|
$TestModel->Comment->validate['comment'] = 'notBlank';
|
2012-04-23 11:02:26 +00:00
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
|
|
|
|
array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = $TestModel->saveAll($data, array('validate' => 'only', 'deep' => true));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
$result = $TestModel->validateAssociated($data, array('deep' => true));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
|
|
|
|
array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = $TestModel->saveAll($data, array('validate' => 'only', 'deep' => true));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
$result = $TestModel->validateAssociated($data, array('deep' => true));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
|
|
|
|
array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$expected = array(
|
|
|
|
'Article' => true,
|
|
|
|
'Comment' => array(
|
2012-05-20 19:51:09 +00:00
|
|
|
true,
|
|
|
|
true
|
2012-04-23 11:02:26 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = $TestModel->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
$result = $TestModel->validateAssociated($data, array('atomic' => false, 'deep' => true));
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
|
|
|
|
array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$expected = array(
|
|
|
|
'Article' => true,
|
|
|
|
'Comment' => array(
|
2012-05-20 19:51:09 +00:00
|
|
|
false,
|
|
|
|
true
|
2012-04-23 11:02:26 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = $TestModel->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
$result = $TestModel->validateAssociated($data, array('atomic' => false, 'deep' => true));
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
|
|
|
|
array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsaved'))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = $TestModel->saveAll($data, array('validate' => 'only', 'deep' => true));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
$result = $TestModel->validateAssociated($data, array('deep' => true));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
|
|
|
|
array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = $TestModel->saveAll($data, array('validate' => 'only', 'deep' => true));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
$result = $TestModel->validateAssociated($data, array('deep' => true));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
|
|
|
|
array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsave'))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$expected = array(
|
|
|
|
'Article' => true,
|
|
|
|
'Comment' => array(
|
2012-05-20 19:51:09 +00:00
|
|
|
true,
|
|
|
|
true
|
2012-04-23 11:02:26 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = $TestModel->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
$result = $TestModel->validateAssociated($data, array('atomic' => false, 'deep' => true));
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
|
|
|
|
array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$expected = array(
|
|
|
|
'Article' => true,
|
|
|
|
'Comment' => array(
|
2012-05-20 19:51:09 +00:00
|
|
|
true,
|
|
|
|
false
|
2012-04-23 11:02:26 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = $TestModel->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
$result = $TestModel->validateAssociated($data, array('atomic' => false, 'deep' => true));
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
'Comment' => array(
|
|
|
|
1 => array(
|
|
|
|
'Attachment' => array(
|
|
|
|
'attachment' => array('This field cannot be left blank')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = $TestModel->validationErrors;
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Attachment' => array(
|
|
|
|
'attachment' => 'deepsave insert',
|
|
|
|
),
|
|
|
|
'Comment' => array(
|
|
|
|
'comment' => 'First comment deepsave insert',
|
|
|
|
'published' => 'Y',
|
|
|
|
'user_id' => 5,
|
|
|
|
'Article' => array(
|
|
|
|
'title' => 'First Article deepsave insert',
|
|
|
|
'body' => 'First Article Body deepsave insert',
|
|
|
|
'User' => array(
|
|
|
|
'user' => 'deepsave',
|
|
|
|
'password' => 'magic'
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
$result = $TestModel->Comment->Attachment->validateAssociated($data, array('deep' => true));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
'Attachment' => true,
|
|
|
|
'Comment' => true
|
|
|
|
);
|
|
|
|
$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
$result = $TestModel->Comment->Attachment->validateAssociated($data, array('atomic' => false, 'deep' => true));
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Attachment' => array(
|
|
|
|
'attachment' => 'deepsave insert',
|
|
|
|
),
|
|
|
|
'Comment' => array(
|
|
|
|
'comment' => 'First comment deepsave insert',
|
|
|
|
'published' => 'Y',
|
|
|
|
'user_id' => 5,
|
|
|
|
'Article' => array(
|
|
|
|
'title' => 'First Article deepsave insert',
|
|
|
|
'body' => 'First Article Body deepsave insert',
|
|
|
|
'User' => array(
|
|
|
|
'user' => '',
|
|
|
|
'password' => 'magic'
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
$result = $TestModel->Comment->Attachment->validateAssociated($data, array('deep' => true));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$result = $TestModel->Comment->Attachment->validationErrors;
|
|
|
|
$expected = array(
|
|
|
|
'Comment' => array(
|
|
|
|
'Article' => array(
|
|
|
|
'User' => array(
|
|
|
|
'user' => array('This field cannot be left blank')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
'Attachment' => true,
|
|
|
|
'Comment' => false
|
|
|
|
);
|
|
|
|
$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
$result = $TestModel->Comment->Attachment->validateAssociated($data, array('atomic' => false, 'deep' => true));
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
|
|
|
|
$data['Comment']['Article']['body'] = '';
|
|
|
|
$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
$result = $TestModel->Comment->Attachment->validateAssociated($data, array('deep' => true));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$result = $TestModel->Comment->Attachment->validationErrors;
|
|
|
|
$expected = array(
|
|
|
|
'Comment' => array(
|
|
|
|
'Article' => array(
|
2012-06-18 11:46:19 +00:00
|
|
|
'body' => array('This field cannot be left blank'),
|
|
|
|
'User' => array(
|
|
|
|
'user' => array('This field cannot be left blank')
|
|
|
|
)
|
2012-04-23 11:02:26 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
'Attachment' => true,
|
|
|
|
'Comment' => false
|
|
|
|
);
|
|
|
|
$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
$result = $TestModel->Comment->Attachment->validateAssociated($data, array('atomic' => false, 'deep' => true));
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
|
|
|
|
$data['Comment']['comment'] = '';
|
|
|
|
$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
$result = $TestModel->Comment->Attachment->validateAssociated($data, array('deep' => true));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$result = $TestModel->Comment->Attachment->validationErrors;
|
|
|
|
$expected = array(
|
|
|
|
'Comment' => array(
|
2012-06-18 11:46:19 +00:00
|
|
|
'comment' => array('This field cannot be left blank'),
|
|
|
|
'Article' => array(
|
|
|
|
'body' => array('This field cannot be left blank'),
|
|
|
|
'User' => array(
|
|
|
|
'user' => array('This field cannot be left blank')
|
|
|
|
)
|
|
|
|
)
|
2012-04-23 11:02:26 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
'Attachment' => true,
|
|
|
|
'Comment' => false
|
|
|
|
);
|
|
|
|
$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
$result = $TestModel->Comment->Attachment->validateAssociated($data, array('atomic' => false, 'deep' => true));
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
|
|
|
|
$data['Attachment']['attachment'] = '';
|
|
|
|
$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
$result = $TestModel->Comment->Attachment->validateAssociated($data, array('deep' => true));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$result = $TestModel->Comment->Attachment->validationErrors;
|
2012-06-18 11:46:19 +00:00
|
|
|
$expected = array(
|
|
|
|
'attachment' => array('This field cannot be left blank'),
|
|
|
|
'Comment' => array(
|
|
|
|
'comment' => array('This field cannot be left blank'),
|
|
|
|
'Article' => array(
|
|
|
|
'body' => array('This field cannot be left blank'),
|
|
|
|
'User' => array(
|
|
|
|
'user' => array('This field cannot be left blank')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2012-04-23 11:02:26 +00:00
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
$result = $TestModel->Comment->validationErrors;
|
2012-06-18 11:46:19 +00:00
|
|
|
$expected = array(
|
|
|
|
'comment' => array('This field cannot be left blank'),
|
|
|
|
'Article' => array(
|
|
|
|
'body' => array('This field cannot be left blank'),
|
|
|
|
'User' => array(
|
|
|
|
'user' => array('This field cannot be left blank')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2012-04-23 11:02:26 +00:00
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
'Attachment' => false,
|
|
|
|
'Comment' => false
|
|
|
|
);
|
|
|
|
$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
$result = $TestModel->Comment->Attachment->validateAssociated($data, array('atomic' => false, 'deep' => true));
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testSaveAllNotDeepValidateOnly
|
|
|
|
* tests the validate methods to not validate deeper recursive data
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSaveAllNotDeepValidateOnly() {
|
|
|
|
$this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
|
|
|
|
$TestModel = new Article();
|
|
|
|
$TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
|
|
|
|
$TestModel->hasAndBelongsToMany = array();
|
2015-05-17 20:27:16 +00:00
|
|
|
$TestModel->Comment->Attachment->validate['attachment'] = 'notBlank';
|
|
|
|
$TestModel->Comment->validate['comment'] = 'notBlank';
|
2012-04-23 11:02:26 +00:00
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Article' => array('id' => 2, 'body' => ''),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
|
|
|
|
array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = $TestModel->saveAll($data, array('validate' => 'only', 'deep' => false));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
$result = $TestModel->validateAssociated($data, array('deep' => false));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$expected = array('body' => array('This field cannot be left blank'));
|
|
|
|
$result = $TestModel->validationErrors;
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Article' => array('id' => 2, 'body' => 'Ignore invalid user data'),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
|
|
|
|
array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = $TestModel->saveAll($data, array('validate' => 'only', 'deep' => false));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
$result = $TestModel->validateAssociated($data, array('deep' => false));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Article' => array('id' => 2, 'body' => 'Ignore invalid user data'),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
|
|
|
|
array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$expected = array(
|
|
|
|
'Article' => true,
|
|
|
|
'Comment' => array(
|
2012-05-20 19:51:09 +00:00
|
|
|
true,
|
|
|
|
true
|
2012-04-23 11:02:26 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = $TestModel->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
$result = $TestModel->validateAssociated($data, array('atomic' => false, 'deep' => false));
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Article' => array('id' => 2, 'body' => 'Ignore invalid attachment data'),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
|
|
|
|
array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = $TestModel->saveAll($data, array('validate' => 'only', 'deep' => false));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
$result = $TestModel->validateAssociated($data, array('deep' => false));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Article' => array('id' => 2, 'body' => 'Ignore invalid attachment data'),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
|
|
|
|
array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$expected = array(
|
|
|
|
'Article' => true,
|
|
|
|
'Comment' => array(
|
2012-05-20 19:51:09 +00:00
|
|
|
true,
|
|
|
|
true
|
2012-04-23 11:02:26 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = $TestModel->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
$result = $TestModel->validateAssociated($data, array('atomic' => false, 'deep' => false));
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
$expected = array();
|
|
|
|
$result = $TestModel->validationErrors;
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Attachment' => array(
|
|
|
|
'attachment' => 'deepsave insert',
|
|
|
|
),
|
|
|
|
'Comment' => array(
|
|
|
|
'comment' => 'First comment deepsave insert',
|
|
|
|
'published' => 'Y',
|
|
|
|
'user_id' => 5,
|
|
|
|
'Article' => array(
|
|
|
|
'title' => 'First Article deepsave insert ignored',
|
|
|
|
'body' => 'First Article Body deepsave insert',
|
|
|
|
'User' => array(
|
|
|
|
'user' => '',
|
|
|
|
'password' => 'magic'
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => false));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
$result = $TestModel->Comment->Attachment->validateAssociated($data, array('deep' => false));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$result = $TestModel->Comment->Attachment->validationErrors;
|
|
|
|
$expected = array();
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
'Attachment' => true,
|
|
|
|
'Comment' => true
|
|
|
|
);
|
|
|
|
$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
$result = $TestModel->Comment->Attachment->validateAssociated($data, array('atomic' => false, 'deep' => false));
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
|
|
|
|
$data['Comment']['Article']['body'] = '';
|
|
|
|
$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => false));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
$result = $TestModel->Comment->Attachment->validateAssociated($data, array('deep' => false));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$result = $TestModel->Comment->Attachment->validationErrors;
|
|
|
|
$expected = array();
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
'Attachment' => true,
|
|
|
|
'Comment' => true
|
|
|
|
);
|
|
|
|
$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
$result = $TestModel->Comment->Attachment->validateAssociated($data, array('atomic' => false, 'deep' => false));
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testValidateAssociated method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testValidateAssociated() {
|
2013-08-02 21:19:41 +00:00
|
|
|
$this->loadFixtures('Comment', 'Attachment', 'Article', 'User');
|
2012-04-23 11:02:26 +00:00
|
|
|
$TestModel = new Comment();
|
2015-05-17 20:27:16 +00:00
|
|
|
$TestModel->Attachment->validate = array('attachment' => 'notBlank');
|
2012-04-23 11:02:26 +00:00
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Comment' => array(
|
|
|
|
'comment' => 'This is the comment'
|
|
|
|
),
|
|
|
|
'Attachment' => array(
|
|
|
|
'attachment' => ''
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = $TestModel->saveAll($data, array('validate' => 'only'));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
$result = $TestModel->validateAssociated($data);
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
2013-08-02 21:19:41 +00:00
|
|
|
$fieldList = array(
|
|
|
|
'Attachment' => array('comment_id')
|
|
|
|
);
|
|
|
|
$result = $TestModel->saveAll($data, array(
|
|
|
|
'fieldList' => $fieldList, 'validate' => 'only'
|
|
|
|
));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
$this->assertEmpty($TestModel->validationErrors);
|
|
|
|
$result = $TestModel->validateAssociated($data, array('fieldList' => $fieldList));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
$this->assertEmpty($TestModel->validationErrors);
|
|
|
|
|
2015-05-17 20:27:16 +00:00
|
|
|
$TestModel->validate = array('comment' => 'notBlank');
|
2012-04-23 11:02:26 +00:00
|
|
|
$record = array(
|
|
|
|
'Comment' => array(
|
|
|
|
'user_id' => 1,
|
|
|
|
'article_id' => 1,
|
|
|
|
'comment' => '',
|
|
|
|
),
|
|
|
|
'Attachment' => array(
|
|
|
|
'attachment' => ''
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = $TestModel->saveAll($record, array('validate' => 'only'));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
$result = $TestModel->validateAssociated($record);
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$fieldList = array(
|
|
|
|
'Comment' => array('id', 'article_id', 'user_id'),
|
|
|
|
'Attachment' => array('comment_id')
|
|
|
|
);
|
|
|
|
$result = $TestModel->saveAll($record, array(
|
|
|
|
'fieldList' => $fieldList, 'validate' => 'only'
|
|
|
|
));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
$this->assertEmpty($TestModel->validationErrors);
|
|
|
|
$result = $TestModel->validateAssociated($record, array('fieldList' => $fieldList));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
$this->assertEmpty($TestModel->validationErrors);
|
|
|
|
|
|
|
|
$TestModel = new Article();
|
|
|
|
$TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
|
2015-05-17 20:27:16 +00:00
|
|
|
$TestModel->Comment->validate = array('comment' => 'notBlank');
|
2012-04-23 11:02:26 +00:00
|
|
|
$data = array(
|
|
|
|
'Article' => array('id' => 2),
|
|
|
|
'Comment' => array(
|
|
|
|
array(
|
|
|
|
'id' => 1,
|
|
|
|
'comment' => '',
|
|
|
|
'published' => 'Y',
|
|
|
|
'user_id' => 1,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id' => 2,
|
|
|
|
'comment' =>
|
|
|
|
'comment',
|
|
|
|
'published' => 'Y',
|
2012-05-20 00:29:32 +00:00
|
|
|
'user_id' => 1
|
|
|
|
),
|
2012-04-23 11:02:26 +00:00
|
|
|
array(
|
|
|
|
'id' => 3,
|
|
|
|
'comment' => '',
|
|
|
|
'published' => 'Y',
|
|
|
|
'user_id' => 1
|
|
|
|
)));
|
|
|
|
$result = $TestModel->saveAll($data, array('validate' => 'only'));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
$result = $TestModel->validateAssociated($data);
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
'Article' => true,
|
|
|
|
'Comment' => array(false, true, false)
|
|
|
|
);
|
|
|
|
$result = $TestModel->saveAll($data, array('atomic' => false, 'validate' => 'only'));
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
$result = $TestModel->validateAssociated($data, array('atomic' => false));
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
|
|
|
$expected = array('Comment' => array(
|
|
|
|
0 => array('comment' => array('This field cannot be left blank')),
|
|
|
|
2 => array('comment' => array('This field cannot be left blank'))
|
|
|
|
));
|
|
|
|
$this->assertEquals($expected['Comment'], $TestModel->Comment->validationErrors);
|
|
|
|
|
|
|
|
$model = new Comment();
|
|
|
|
$model->deleteAll(true);
|
2015-05-17 20:27:16 +00:00
|
|
|
$model->validate = array('comment' => 'notBlank');
|
|
|
|
$model->Attachment->validate = array('attachment' => 'notBlank');
|
2012-04-23 11:02:26 +00:00
|
|
|
$model->Attachment->bindModel(array('belongsTo' => array('Comment')));
|
|
|
|
$expected = array(
|
2012-06-18 11:46:19 +00:00
|
|
|
'comment' => array('This field cannot be left blank'),
|
|
|
|
'Attachment' => array(
|
|
|
|
'attachment' => array('This field cannot be left blank')
|
|
|
|
)
|
2012-04-23 11:02:26 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Comment' => array('comment' => '', 'article_id' => 1, 'user_id' => 1),
|
|
|
|
'Attachment' => array('attachment' => '')
|
|
|
|
);
|
|
|
|
$result = $model->saveAll($data, array('validate' => 'only'));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
$result = $model->validateAssociated($data);
|
|
|
|
$this->assertFalse($result);
|
2012-06-18 11:46:19 +00:00
|
|
|
$this->assertEquals($expected, $model->validationErrors);
|
2012-04-23 11:02:26 +00:00
|
|
|
$this->assertEquals($expected['Attachment'], $model->Attachment->validationErrors);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testValidateMany method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testValidateMany() {
|
|
|
|
$TestModel = new Article();
|
2015-05-17 20:27:16 +00:00
|
|
|
$TestModel->validate = array('title' => 'notBlank');
|
2012-04-23 11:02:26 +00:00
|
|
|
$data = array(
|
|
|
|
0 => array('title' => ''),
|
|
|
|
1 => array('title' => 'title 1'),
|
|
|
|
2 => array('title' => 'title 2'),
|
|
|
|
);
|
|
|
|
$expected = array(
|
|
|
|
0 => array('title' => array('This field cannot be left blank')),
|
|
|
|
);
|
|
|
|
|
|
|
|
$result = $TestModel->saveAll($data, array('validate' => 'only'));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
$this->assertEquals($expected, $TestModel->validationErrors);
|
|
|
|
$result = $TestModel->validateMany($data);
|
|
|
|
$this->assertFalse($result);
|
|
|
|
$this->assertEquals($expected, $TestModel->validationErrors);
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
0 => array('title' => 'title 0'),
|
|
|
|
1 => array('title' => ''),
|
|
|
|
2 => array('title' => 'title 2'),
|
|
|
|
);
|
|
|
|
$expected = array(
|
|
|
|
1 => array('title' => array('This field cannot be left blank')),
|
|
|
|
);
|
|
|
|
$result = $TestModel->saveAll($data, array('validate' => 'only'));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
$this->assertEquals($expected, $TestModel->validationErrors);
|
|
|
|
$result = $TestModel->validateMany($data);
|
|
|
|
$this->assertFalse($result);
|
|
|
|
$this->assertEquals($expected, $TestModel->validationErrors);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testGetMethods method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testGetMethods() {
|
|
|
|
$this->loadFixtures('Article', 'Comment');
|
|
|
|
$TestModel = new Article();
|
2012-04-29 00:39:29 +00:00
|
|
|
$Validator = $TestModel->validator();
|
2012-04-23 11:02:26 +00:00
|
|
|
|
|
|
|
$result = $Validator->getMethods();
|
|
|
|
|
|
|
|
$expected = array_map('strtolower', get_class_methods('Article'));
|
2012-04-29 21:48:33 +00:00
|
|
|
$this->assertEquals($expected, array_keys($result));
|
2012-04-23 11:02:26 +00:00
|
|
|
}
|
|
|
|
|
2012-08-25 12:05:19 +00:00
|
|
|
/**
|
|
|
|
* Tests that methods are refreshed when the list of behaviors change
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testGetMethodsRefresh() {
|
|
|
|
$this->loadFixtures('Article', 'Comment');
|
|
|
|
$TestModel = new Article();
|
|
|
|
$Validator = $TestModel->validator();
|
|
|
|
|
|
|
|
$result = $Validator->getMethods();
|
|
|
|
|
|
|
|
$expected = array_map('strtolower', get_class_methods('Article'));
|
|
|
|
$this->assertEquals($expected, array_keys($result));
|
|
|
|
|
2013-10-08 18:23:57 +00:00
|
|
|
$TestModel->Behaviors->load('Containable');
|
2012-08-25 12:05:19 +00:00
|
|
|
$newList = array(
|
|
|
|
'contain',
|
|
|
|
'resetbindings',
|
|
|
|
'containments',
|
|
|
|
'fielddependencies',
|
|
|
|
'containmentsmap'
|
|
|
|
);
|
|
|
|
$this->assertEquals(array_merge($expected, $newList), array_keys($Validator->getMethods()));
|
|
|
|
|
2013-10-08 18:23:57 +00:00
|
|
|
$TestModel->Behaviors->unload('Containable');
|
2012-08-25 12:05:19 +00:00
|
|
|
$this->assertEquals($expected, array_keys($Validator->getMethods()));
|
|
|
|
}
|
|
|
|
|
2012-04-23 11:02:26 +00:00
|
|
|
/**
|
|
|
|
* testSetValidationDomain method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSetValidationDomain() {
|
|
|
|
$this->loadFixtures('Article', 'Comment');
|
|
|
|
$TestModel = new Article();
|
2012-04-29 00:39:29 +00:00
|
|
|
$Validator = $TestModel->validator();
|
2012-04-23 11:02:26 +00:00
|
|
|
|
2012-04-29 04:54:35 +00:00
|
|
|
$result = $Validator->setValidationDomain('default');
|
|
|
|
$this->assertEquals('default', $TestModel->validationDomain);
|
2012-04-23 11:02:26 +00:00
|
|
|
|
|
|
|
$result = $Validator->setValidationDomain('other');
|
2012-04-29 04:54:35 +00:00
|
|
|
$this->assertEquals('other', $TestModel->validationDomain);
|
2012-04-23 11:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testGetModel method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testGetModel() {
|
|
|
|
$TestModel = new Article();
|
2012-04-29 00:39:29 +00:00
|
|
|
$Validator = $TestModel->validator();
|
2012-04-23 11:02:26 +00:00
|
|
|
|
|
|
|
$result = $Validator->getModel();
|
|
|
|
$this->assertInstanceOf('Article', $result);
|
|
|
|
}
|
|
|
|
|
2012-05-05 20:22:02 +00:00
|
|
|
/**
|
|
|
|
* Tests it is possible to get validation sets for a field using an array inteface
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testArrayAccessGet() {
|
|
|
|
$TestModel = new Article();
|
|
|
|
$Validator = $TestModel->validator();
|
|
|
|
|
|
|
|
$titleValidator = $Validator['title'];
|
|
|
|
$this->assertEquals('title', $titleValidator->field);
|
|
|
|
$this->assertCount(1, $titleValidator->getRules());
|
|
|
|
$rule = current($titleValidator->getRules());
|
2015-05-17 20:27:16 +00:00
|
|
|
$this->assertEquals('notBlank', $rule->rule);
|
2012-05-05 20:22:02 +00:00
|
|
|
|
|
|
|
$titleValidator = $Validator['body'];
|
|
|
|
$this->assertEquals('body', $titleValidator->field);
|
|
|
|
$this->assertCount(1, $titleValidator->getRules());
|
|
|
|
$rule = current($titleValidator->getRules());
|
2015-05-17 20:27:16 +00:00
|
|
|
$this->assertEquals('notBlank', $rule->rule);
|
2012-05-05 20:22:02 +00:00
|
|
|
|
|
|
|
$titleValidator = $Validator['user_id'];
|
|
|
|
$this->assertEquals('user_id', $titleValidator->field);
|
|
|
|
$this->assertCount(1, $titleValidator->getRules());
|
|
|
|
$rule = current($titleValidator->getRules());
|
|
|
|
$this->assertEquals('numeric', $rule->rule);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests it is possible to check for validation sets for a field using an array inteface
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testArrayAccessExists() {
|
|
|
|
$TestModel = new Article();
|
|
|
|
$Validator = $TestModel->validator();
|
|
|
|
|
|
|
|
$this->assertTrue(isset($Validator['title']));
|
|
|
|
$this->assertTrue(isset($Validator['body']));
|
|
|
|
$this->assertTrue(isset($Validator['user_id']));
|
|
|
|
$this->assertFalse(isset($Validator['other']));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests it is possible to set validation rules for a field using an array inteface
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testArrayAccessSet() {
|
|
|
|
$TestModel = new Article();
|
|
|
|
$Validator = $TestModel->validator();
|
|
|
|
|
|
|
|
$set = array(
|
|
|
|
'numeric' => array('rule' => 'numeric', 'allowEmpty' => false),
|
2014-05-13 08:46:25 +00:00
|
|
|
'between' => array('rule' => array('lengthBetween', 1, 5), 'allowEmpty' => false),
|
2012-05-05 20:22:02 +00:00
|
|
|
);
|
|
|
|
$Validator['other'] = $set;
|
|
|
|
$rules = $Validator['other'];
|
|
|
|
$this->assertEquals('other', $rules->field);
|
|
|
|
|
|
|
|
$validators = $rules->getRules();
|
|
|
|
$this->assertCount(2, $validators);
|
|
|
|
$this->assertEquals('numeric', $validators['numeric']->rule);
|
2014-05-13 08:46:25 +00:00
|
|
|
$this->assertEquals(array('lengthBetween', 1, 5), $validators['between']->rule);
|
2012-05-05 20:22:02 +00:00
|
|
|
|
|
|
|
$Validator['new'] = new CakeValidationSet('new', $set, array());
|
|
|
|
$rules = $Validator['new'];
|
|
|
|
$this->assertEquals('new', $rules->field);
|
|
|
|
|
|
|
|
$validators = $rules->getRules();
|
|
|
|
$this->assertCount(2, $validators);
|
|
|
|
$this->assertEquals('numeric', $validators['numeric']->rule);
|
2014-05-13 08:46:25 +00:00
|
|
|
$this->assertEquals(array('lengthBetween', 1, 5), $validators['between']->rule);
|
2012-05-05 20:22:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests it is possible to unset validation rules
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testArrayAccessUset() {
|
|
|
|
$TestModel = new Article();
|
|
|
|
$Validator = $TestModel->validator();
|
|
|
|
|
|
|
|
$this->assertTrue(isset($Validator['title']));
|
|
|
|
unset($Validator['title']);
|
|
|
|
$this->assertFalse(isset($Validator['title']));
|
|
|
|
}
|
|
|
|
|
2012-05-05 22:04:35 +00:00
|
|
|
/**
|
|
|
|
* Tests it is possible to iterate a validation object
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testIterator() {
|
|
|
|
$TestModel = new Article();
|
|
|
|
$Validator = $TestModel->validator();
|
|
|
|
|
|
|
|
$i = 0;
|
|
|
|
foreach ($Validator as $field => $rules) {
|
|
|
|
if ($i === 0) {
|
|
|
|
$this->assertEquals('user_id', $field);
|
|
|
|
}
|
|
|
|
if ($i === 1) {
|
|
|
|
$this->assertEquals('title', $field);
|
|
|
|
}
|
|
|
|
if ($i === 2) {
|
|
|
|
$this->assertEquals('body', $field);
|
|
|
|
}
|
|
|
|
$this->assertInstanceOf('CakeValidationSet', $rules);
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
$this->assertEquals(3, $i);
|
|
|
|
}
|
|
|
|
|
2012-05-05 22:16:09 +00:00
|
|
|
/**
|
|
|
|
* Tests countable interface in ModelValidator
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testCount() {
|
|
|
|
$TestModel = new Article();
|
|
|
|
$Validator = $TestModel->validator();
|
|
|
|
$this->assertCount(3, $Validator);
|
|
|
|
|
|
|
|
$set = array(
|
|
|
|
'numeric' => array('rule' => 'numeric', 'allowEmpty' => false),
|
2014-05-13 08:46:25 +00:00
|
|
|
'range' => array('rule' => array('lengthBetween', 1, 5), 'allowEmpty' => false),
|
2012-05-05 22:16:09 +00:00
|
|
|
);
|
|
|
|
$Validator['other'] = $set;
|
|
|
|
$this->assertCount(4, $Validator);
|
|
|
|
|
|
|
|
unset($Validator['title']);
|
|
|
|
$this->assertCount(3, $Validator);
|
|
|
|
unset($Validator['body']);
|
|
|
|
$this->assertCount(2, $Validator);
|
|
|
|
}
|
|
|
|
|
2012-05-07 03:36:28 +00:00
|
|
|
/**
|
|
|
|
* Tests it is possible to add validation rules
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testAddRule() {
|
|
|
|
$TestModel = new Article();
|
|
|
|
$Validator = $TestModel->validator();
|
|
|
|
|
|
|
|
$Validator->add('other', 'numeric', array('rule' => 'numeric', 'allowEmpty' => false));
|
2014-05-13 08:46:25 +00:00
|
|
|
$Validator->add('other', 'between', array('rule' => array('lengthBetween', 1, 5), 'allowEmpty' => false));
|
2012-05-07 03:36:28 +00:00
|
|
|
$rules = $Validator['other'];
|
|
|
|
$this->assertEquals('other', $rules->field);
|
|
|
|
|
|
|
|
$validators = $rules->getRules();
|
|
|
|
$this->assertCount(2, $validators);
|
|
|
|
$this->assertEquals('numeric', $validators['numeric']->rule);
|
2014-05-13 08:46:25 +00:00
|
|
|
$this->assertEquals(array('lengthBetween', 1, 5), $validators['between']->rule);
|
2012-05-07 03:36:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests it is possible to remove validation rules
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testRemoveRule() {
|
|
|
|
$TestModel = new Article();
|
|
|
|
$Validator = $TestModel->validator();
|
|
|
|
|
|
|
|
$this->assertTrue(isset($Validator['title']));
|
|
|
|
$Validator->remove('title');
|
|
|
|
$this->assertFalse(isset($Validator['title']));
|
|
|
|
|
|
|
|
$Validator->add('other', 'numeric', array('rule' => 'numeric', 'allowEmpty' => false));
|
2014-05-13 08:46:25 +00:00
|
|
|
$Validator->add('other', 'between', array('rule' => array('lengthBetween', 1, 5), 'allowEmpty' => false));
|
2012-05-07 03:36:28 +00:00
|
|
|
$this->assertTrue(isset($Validator['other']));
|
|
|
|
|
|
|
|
$Validator->remove('other', 'numeric');
|
|
|
|
$this->assertTrue(isset($Validator['other']));
|
|
|
|
$this->assertFalse(isset($Validator['other']['numeric']));
|
2014-05-13 08:46:25 +00:00
|
|
|
$this->assertTrue(isset($Validator['other']['between']));
|
2012-05-07 03:36:28 +00:00
|
|
|
}
|
2012-05-07 04:08:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests validation callbacks are triggered
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testValidateCallbacks() {
|
|
|
|
$TestModel = $this->getMock('Article', array('beforeValidate', 'afterValidate'));
|
|
|
|
$TestModel->expects($this->once())->method('beforeValidate');
|
|
|
|
$TestModel->expects($this->once())->method('afterValidate');
|
|
|
|
|
|
|
|
$TestModel->set(array('title' => '', 'body' => 'body'));
|
|
|
|
$TestModel->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(
|
2015-05-17 20:27:16 +00:00
|
|
|
'notBlank' => array(
|
|
|
|
'rule' => 'notBlank',
|
2012-05-10 04:21:27 +00:00
|
|
|
'required' => true,
|
|
|
|
'allowEmpty' => false
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$data = array(
|
|
|
|
'CustomArticle' => array(
|
|
|
|
'body' => 'foo0'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = $model->saveAll($data, array('validate' => 'first'));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
2012-05-15 14:57:42 +00:00
|
|
|
$this->assertFalse($model->findMethods['unPublished'], 'beforeValidate was run twice');
|
2012-05-10 04:21:27 +00:00
|
|
|
|
2012-05-15 14:57:42 +00:00
|
|
|
$model->findMethods['unPublished'] = true;
|
|
|
|
$data = array(
|
|
|
|
'CustomArticle' => array(
|
|
|
|
'body' => 'foo1'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = $model->saveAll($data, array('validate' => 'first', 'deep' => true));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
$title = $model->field('title', array('body' => 'foo1'));
|
2012-05-10 04:21:27 +00:00
|
|
|
$this->assertEquals('foo', $title);
|
2012-05-15 14:57:42 +00:00
|
|
|
$this->assertFalse($model->findMethods['unPublished'], 'beforeValidate was run twice');
|
2012-05-10 04:21:27 +00:00
|
|
|
|
|
|
|
$data = array(
|
|
|
|
array('body' => 'foo2'),
|
2012-05-15 14:57:42 +00:00
|
|
|
array('body' => 'foo3'),
|
|
|
|
array('body' => 'foo4')
|
2012-05-10 04:21:27 +00:00
|
|
|
);
|
|
|
|
|
2012-05-15 14:57:42 +00:00
|
|
|
$result = $model->saveAll($data, array('validate' => 'first', 'deep' => true));
|
2012-05-10 04:21:27 +00:00
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$this->assertEquals('foo', $model->field('title', array('body' => 'foo2')));
|
|
|
|
$this->assertEquals('foo', $model->field('title', array('body' => 'foo3')));
|
2012-05-15 14:57:42 +00:00
|
|
|
$this->assertEquals('foo', $model->field('title', array('body' => 'foo4')));
|
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
|
|
|
|
*/
|
2012-05-15 14:57:42 +00:00
|
|
|
public function testValidateFirstAssociatedWithBeforeValidate() {
|
2012-05-10 04:21:27 +00:00
|
|
|
$this->loadFixtures('Article', 'User');
|
|
|
|
$model = new CustomArticle();
|
|
|
|
$model->validate = array(
|
|
|
|
'title' => array(
|
2015-05-17 20:27:16 +00:00
|
|
|
'notBlank' => array(
|
|
|
|
'rule' => 'notBlank',
|
2012-05-10 04:21:27 +00:00
|
|
|
'required' => true
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$articles = array(
|
|
|
|
array('body' => 'foo1'),
|
|
|
|
array('body' => 'foo2'),
|
|
|
|
array('body' => 'foo3')
|
|
|
|
);
|
|
|
|
$user = new User();
|
2012-05-30 22:26:59 +00:00
|
|
|
$user->bindModel(array('hasMany' => array('CustomArticle')));
|
2012-05-10 04:21:27 +00:00
|
|
|
$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')));
|
|
|
|
}
|
|
|
|
|
2012-05-15 14:57:42 +00:00
|
|
|
/**
|
|
|
|
* testValidateFirstWithDefaults method
|
|
|
|
*
|
2014-04-02 00:23:43 +00:00
|
|
|
* @return void
|
2012-05-15 14:57:42 +00:00
|
|
|
*/
|
|
|
|
public function testFirstWithDefaults() {
|
|
|
|
$this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
|
|
|
|
$TestModel = new Article();
|
|
|
|
|
|
|
|
$result = $TestModel->find('first', array(
|
|
|
|
'conditions' => array('Article.id' => 1)
|
|
|
|
));
|
|
|
|
$expected = array(
|
|
|
|
'Article' => array(
|
|
|
|
'id' => 1,
|
|
|
|
'user_id' => 1,
|
|
|
|
'title' => 'First Article',
|
|
|
|
'body' => 'First Article Body',
|
|
|
|
'published' => 'Y',
|
|
|
|
'created' => '2007-03-18 10:39:23'
|
|
|
|
),
|
|
|
|
);
|
|
|
|
unset($result['Article']['updated']);
|
|
|
|
$this->assertEquals($expected['Article'], $result['Article']);
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Article' => array(
|
|
|
|
'id' => 1,
|
|
|
|
'title' => 'First Article (modified)'
|
|
|
|
),
|
|
|
|
'Comment' => array(
|
|
|
|
array('comment' => 'Article comment', 'user_id' => 1)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = $TestModel->saveAll($data, array('validate' => 'first'));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$result = $TestModel->find('first', array(
|
|
|
|
'conditions' => array('Article.id' => 1)
|
|
|
|
));
|
|
|
|
$expected['Article']['title'] = 'First Article (modified)';
|
|
|
|
unset($result['Article']['updated']);
|
|
|
|
$this->assertEquals($expected['Article'], $result['Article']);
|
|
|
|
}
|
2012-05-11 02:49:35 +00:00
|
|
|
|
2012-05-20 23:15:46 +00:00
|
|
|
public function testAddMultipleRules() {
|
|
|
|
$TestModel = new Article();
|
|
|
|
$Validator = $TestModel->validator();
|
|
|
|
|
|
|
|
$set = array(
|
|
|
|
'numeric' => array('rule' => 'numeric', 'allowEmpty' => false),
|
2014-05-13 08:46:25 +00:00
|
|
|
'between' => array('rule' => array('lengthBetween', 1, 5), 'allowEmpty' => false),
|
2012-05-20 23:15:46 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$Validator->add('other', $set);
|
|
|
|
$rules = $Validator['other'];
|
|
|
|
$this->assertEquals('other', $rules->field);
|
|
|
|
|
|
|
|
$validators = $rules->getRules();
|
|
|
|
$this->assertCount(2, $validators);
|
|
|
|
$this->assertEquals('numeric', $validators['numeric']->rule);
|
2014-05-13 08:46:25 +00:00
|
|
|
$this->assertEquals(array('lengthBetween', 1, 5), $validators['between']->rule);
|
2012-05-20 23:15:46 +00:00
|
|
|
|
|
|
|
$set = new CakeValidationSet('other', array(
|
|
|
|
'a' => array('rule' => 'numeric', 'allowEmpty' => false),
|
2014-05-13 08:46:25 +00:00
|
|
|
'b' => array('rule' => array('lengthBetween', 1, 5), 'allowEmpty' => false),
|
2012-05-20 23:15:46 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
$Validator->add('other', $set);
|
|
|
|
$this->assertSame($set, $Validator->getField('other'));
|
|
|
|
}
|
|
|
|
|
2012-06-20 12:39:18 +00:00
|
|
|
/**
|
|
|
|
* Test that rules are parsed correctly when calling getField()
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testValidator() {
|
|
|
|
$TestModel = new Article();
|
|
|
|
$Validator = $TestModel->validator();
|
|
|
|
|
|
|
|
$result = $Validator->getField();
|
|
|
|
$expected = array('user_id', 'title', 'body');
|
|
|
|
$this->assertEquals($expected, array_keys($result));
|
|
|
|
$this->assertTrue($result['user_id'] instanceof CakeValidationSet);
|
|
|
|
|
|
|
|
$result = $TestModel->validator()->getField('title');
|
|
|
|
$this->assertTrue($result instanceof CakeValidationSet);
|
|
|
|
}
|
|
|
|
|
2012-11-27 07:15:23 +00:00
|
|
|
/**
|
|
|
|
* Test that validator override works as expected
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testValidatorOverride() {
|
|
|
|
$TestModel = new Article();
|
|
|
|
$ValidatorA = new ModelValidator($TestModel);
|
|
|
|
$ValidatorB = new ModelValidator($TestModel);
|
|
|
|
|
|
|
|
$TestModel->validator($ValidatorA);
|
|
|
|
$TestModel->validator($ValidatorB);
|
|
|
|
|
|
|
|
$this->assertSame($ValidatorB, $TestModel->validator());
|
|
|
|
$this->assertNotSame($ValidatorA, $TestModel->validator());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that type hint exception is thrown
|
|
|
|
*
|
|
|
|
* @expectedException PHPUnit_Framework_Error
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testValidatorTypehintException() {
|
2013-01-23 12:45:50 +00:00
|
|
|
new ModelValidator('asdasds');
|
2012-11-27 07:15:23 +00:00
|
|
|
}
|
|
|
|
|
2012-06-06 14:37:01 +00:00
|
|
|
/**
|
|
|
|
* Tests that altering data in a beforeValidate callback will lead to saving those
|
|
|
|
* values in database, this time with belongsTo associations
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testValidateFirstAssociatedWithBeforeValidate2() {
|
|
|
|
$this->loadFixtures('Article', 'User');
|
|
|
|
$model = new CustomArticle();
|
|
|
|
$model->validate = array(
|
|
|
|
'title' => array(
|
2015-05-17 20:27:16 +00:00
|
|
|
'notBlank' => array(
|
|
|
|
'rule' => 'notBlank',
|
2012-06-06 14:37:01 +00:00
|
|
|
'required' => true
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'User' => array('user' => 'foo', 'password' => 'bar'),
|
|
|
|
'CustomArticle' => array(
|
|
|
|
'body' => 'a test'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = $model->saveAll($data, array('validate' => 'first'));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$this->assertEquals('foo', $model->field('title', array('body' => 'a test')));
|
|
|
|
}
|
2012-06-09 19:57:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Testing you can dynamically add rules to a field, added this to dispel doubts
|
|
|
|
* after a presentation made to show off this new feature
|
|
|
|
*
|
|
|
|
* @return void
|
2013-01-11 14:06:54 +00:00
|
|
|
*/
|
2012-06-09 19:57:22 +00:00
|
|
|
public function testDynamicValidationRuleBuilding() {
|
|
|
|
$model = new Article;
|
|
|
|
$validator = $model->validator();
|
|
|
|
$validator->add('body', 'isSpecial', array('rule' => 'special'));
|
|
|
|
$rules = $validator['body']->getRules();
|
|
|
|
$this->assertCount(2, $rules);
|
|
|
|
$this->assertEquals('special', $rules['isSpecial']->rule);
|
|
|
|
$validator['body']->setRule('isAwesome', array('rule' => 'awesome'));
|
|
|
|
$rules = $validator['body']->getRules();
|
|
|
|
$this->assertCount(3, $rules);
|
|
|
|
$this->assertEquals('awesome', $rules['isAwesome']->rule);
|
|
|
|
}
|
|
|
|
|
2012-06-12 06:24:14 +00:00
|
|
|
/**
|
|
|
|
* Test to ensure custom validation methods work with CakeValidationSet
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testCustomMethodsWithCakeValidationSet() {
|
|
|
|
$TestModel = new TestValidate();
|
|
|
|
$Validator = $TestModel->validator();
|
|
|
|
|
|
|
|
$Validator->add('title', 'validateTitle', array(
|
|
|
|
'rule' => 'validateTitle',
|
|
|
|
'message' => 'That aint right',
|
|
|
|
));
|
|
|
|
$data = array('title' => 'notatitle');
|
|
|
|
$result = $Validator->getField('title')->validate($data);
|
|
|
|
$expected = array(0 => 'That aint right');
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
|
|
|
|
$data = array('title' => 'title-is-good');
|
|
|
|
$result = $Validator->getField('title')->validate($data);
|
|
|
|
$expected = array();
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
|
|
|
|
2012-06-25 00:06:14 +00:00
|
|
|
public function testCustomMethodWithEmptyValue() {
|
|
|
|
$this->loadFixtures('Article');
|
|
|
|
|
|
|
|
$model = $this->getMock('Article', array('isLegit'));
|
|
|
|
$model->validate = array(
|
|
|
|
'title' => array(
|
|
|
|
'custom' => array(
|
|
|
|
'rule' => array('isLegit'),
|
|
|
|
'message' => 'is no good'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$model->expects($this->once())
|
|
|
|
->method('isLegit')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
|
|
|
$model->set(array('title' => ''));
|
|
|
|
$this->assertFalse($model->validates());
|
|
|
|
}
|
|
|
|
|
2012-11-29 02:26:40 +00:00
|
|
|
/**
|
|
|
|
* Test validateAssociated with atomic=false & deep=true
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testValidateAssociatedAtomicFalseDeepTrueWithErrors() {
|
|
|
|
$this->loadFixtures('Comment', 'Article', 'User', 'Attachment');
|
|
|
|
$Attachment = ClassRegistry::init('Attachment');
|
|
|
|
$Attachment->Comment->validator()->add('comment', array(
|
2015-05-17 20:27:16 +00:00
|
|
|
array('rule' => 'notBlank')
|
2012-11-29 02:26:40 +00:00
|
|
|
));
|
|
|
|
$Attachment->Comment->User->bindModel(array(
|
|
|
|
'hasMany' => array(
|
|
|
|
'Article',
|
|
|
|
'Comment'
|
|
|
|
)),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Attachment' => array(
|
|
|
|
'attachment' => 'text',
|
|
|
|
'Comment' => array(
|
|
|
|
'comment' => '',
|
|
|
|
'published' => 'N',
|
|
|
|
'User' => array(
|
|
|
|
'user' => 'Foo',
|
|
|
|
'password' => 'mypassword',
|
|
|
|
'Comment' => array(
|
|
|
|
array(
|
|
|
|
'comment' => ''
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = $Attachment->validateAssociated($data, array('atomic' => false, 'deep' => true));
|
|
|
|
|
|
|
|
$result = $Attachment->validationErrors;
|
|
|
|
$expected = array(
|
|
|
|
'Comment' => array(
|
|
|
|
'comment' => array(
|
|
|
|
0 => 'This field cannot be left blank',
|
|
|
|
),
|
|
|
|
'User' => array(
|
|
|
|
'Comment' => array(
|
|
|
|
0 => array(
|
|
|
|
'comment' => array(
|
|
|
|
0 => 'This field cannot be left blank',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2013-08-16 18:51:11 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2012-11-29 02:26:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test validateMany with atomic=false & deep=true
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testValidateManyAtomicFalseDeepTrueWithErrors() {
|
|
|
|
$this->loadFixtures('Comment', 'Article', 'User');
|
|
|
|
$Article = ClassRegistry::init('Article');
|
|
|
|
$Article->Comment->validator()->add('comment', array(
|
2015-05-17 20:27:16 +00:00
|
|
|
array('rule' => 'notBlank')
|
2012-11-29 02:26:40 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
array(
|
|
|
|
'Article' => array(
|
|
|
|
'user_id' => 1,
|
|
|
|
'title' => 'Foo',
|
|
|
|
'body' => 'text',
|
|
|
|
'published' => 'N'
|
|
|
|
),
|
|
|
|
'Comment' => array(
|
|
|
|
array(
|
|
|
|
'user_id' => 1,
|
|
|
|
'comment' => 'Baz',
|
|
|
|
'published' => 'N',
|
|
|
|
)
|
|
|
|
),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'Article' => array(
|
|
|
|
'user_id' => 1,
|
|
|
|
'title' => 'Bar',
|
|
|
|
'body' => 'text',
|
|
|
|
'published' => 'N'
|
|
|
|
),
|
|
|
|
'Comment' => array(
|
|
|
|
array(
|
|
|
|
'user_id' => 1,
|
|
|
|
'comment' => '',
|
|
|
|
'published' => 'N',
|
|
|
|
)
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$Article->validateMany($data, array('atomic' => false, 'deep' => true));
|
|
|
|
|
|
|
|
$result = $Article->validationErrors;
|
|
|
|
$expected = array(
|
|
|
|
1 => array(
|
|
|
|
'Comment' => array(
|
|
|
|
0 => array(
|
|
|
|
'comment' => array(
|
|
|
|
0 => 'This field cannot be left blank',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2013-08-16 18:51:11 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2012-11-29 02:26:40 +00:00
|
|
|
}
|
|
|
|
|
2014-10-10 02:49:17 +00:00
|
|
|
/**
|
|
|
|
* Test the isUnique method when used as a validator for multiple fields.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testIsUniqueValidator() {
|
|
|
|
$this->loadFixtures('Article');
|
|
|
|
$Article = ClassRegistry::init('Article');
|
|
|
|
$Article->validate = array(
|
|
|
|
'user_id' => array(
|
|
|
|
'duplicate' => array(
|
|
|
|
'rule' => array('isUnique', array('user_id', 'title'), false)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$data = array(
|
|
|
|
'user_id' => 1,
|
|
|
|
'title' => 'First Article',
|
|
|
|
);
|
|
|
|
$Article->create($data);
|
|
|
|
$this->assertFalse($Article->validates(), 'Contains a dupe');
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'user_id' => 1,
|
|
|
|
'title' => 'Unique Article',
|
|
|
|
);
|
|
|
|
$Article->create($data);
|
|
|
|
$this->assertTrue($Article->validates(), 'Should pass');
|
|
|
|
|
|
|
|
$Article->validate = array(
|
|
|
|
'user_id' => array(
|
|
|
|
'duplicate' => array(
|
|
|
|
'rule' => array('isUnique', array('user_id', 'title'))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$data = array(
|
|
|
|
'user_id' => 1,
|
|
|
|
'title' => 'Unique Article',
|
|
|
|
);
|
|
|
|
$Article->create($data);
|
|
|
|
$this->assertFalse($Article->validates(), 'Should fail, conditions are combined with or');
|
|
|
|
}
|
|
|
|
|
2014-10-13 12:40:30 +00:00
|
|
|
/**
|
2014-10-13 21:59:31 +00:00
|
|
|
* Test backward compatibility of the isUnique method when used as a validator for a single field.
|
2014-10-13 12:40:30 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testBackwardCompatIsUniqueValidator() {
|
|
|
|
$this->loadFixtures('Article');
|
|
|
|
$Article = ClassRegistry::init('Article');
|
|
|
|
$Article->validate = array(
|
|
|
|
'title' => array(
|
|
|
|
'duplicate' => array(
|
|
|
|
'rule' => 'isUnique',
|
|
|
|
'message' => 'Title must be unique',
|
|
|
|
),
|
|
|
|
'minLength' => array(
|
|
|
|
'rule' => array('minLength', 1),
|
|
|
|
'message' => 'Title cannot be empty',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$data = array(
|
|
|
|
'title' => 'First Article',
|
|
|
|
);
|
|
|
|
$data = $Article->create($data);
|
|
|
|
$this->assertFalse($Article->validates(), 'Contains a dupe');
|
|
|
|
}
|
|
|
|
|
2009-07-24 22:17:14 +00:00
|
|
|
}
|
2013-10-07 21:45:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Behavior for testing validation rules.
|
|
|
|
*/
|
|
|
|
class ValidationRuleBehavior extends ModelBehavior {
|
|
|
|
|
|
|
|
public function setup(Model $Model, $config = array()) {
|
|
|
|
$this->settings[$Model->alias] = $config;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function beforeValidate(Model $Model, $options = array()) {
|
|
|
|
$fields = $this->settings[$Model->alias]['fields'];
|
|
|
|
foreach ($fields as $field) {
|
|
|
|
$Model->whitelist[] = $field;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-07 22:32:11 +00:00
|
|
|
}
|