2012-04-23 11:02:26 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2012-05-05 18:13:40 +00:00
|
|
|
* CakeValidationSetTest file
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
|
|
|
* PHP 5
|
|
|
|
*
|
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
|
|
|
|
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice
|
|
|
|
*
|
|
|
|
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
|
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
|
|
|
|
* @package Cake.Test.Case.Model.Validator
|
|
|
|
* @since CakePHP(tm) v 2.2.0
|
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
|
|
|
*/
|
|
|
|
|
2012-05-05 18:13:40 +00:00
|
|
|
App::uses('CakeValidationSet', 'Model/Validator');
|
2012-04-23 11:02:26 +00:00
|
|
|
|
|
|
|
/**
|
2012-05-05 18:13:40 +00:00
|
|
|
* CakeValidationSetTest
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
|
|
|
* @package Cake.Test.Case.Model.Validator
|
|
|
|
*/
|
2012-05-05 18:13:40 +00:00
|
|
|
class CakeValidationSetTest extends CakeTestModel {
|
2012-04-23 11:02:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* setUp method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testValidate method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testValidate() {
|
2012-05-05 18:13:40 +00:00
|
|
|
$Field = new CakeValidationSet('title', 'notEmpty');
|
2012-04-29 21:48:33 +00:00
|
|
|
$data = array(
|
|
|
|
'title' => '',
|
|
|
|
'body' => 'a body'
|
|
|
|
);
|
2012-04-23 11:02:26 +00:00
|
|
|
|
2012-04-29 21:48:33 +00:00
|
|
|
$result = $Field->validate($data);
|
|
|
|
$expected = array('This field cannot be left blank');
|
|
|
|
$this->assertEquals($expected, $result);
|
2012-04-23 11:02:26 +00:00
|
|
|
|
2012-05-05 18:13:40 +00:00
|
|
|
$Field = new CakeValidationSet('body', 'notEmpty');
|
2012-04-23 11:02:26 +00:00
|
|
|
|
2012-04-29 21:48:33 +00:00
|
|
|
$result = $Field->validate($data);
|
|
|
|
$this->assertEmpty($result);
|
2012-04-23 11:02:26 +00:00
|
|
|
|
2012-05-05 18:13:40 +00:00
|
|
|
$Field = new CakeValidationSet('nothere', array('notEmpty' => array('rule' => 'notEmpty', 'required' => true)));
|
2012-04-23 11:02:26 +00:00
|
|
|
|
2012-04-29 21:48:33 +00:00
|
|
|
$result = $Field->validate($data);
|
|
|
|
$expected = array('notEmpty');
|
|
|
|
$this->assertEquals($expected, $result);
|
2012-04-23 11:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testGetRule method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testGetRule() {
|
|
|
|
$rules = array('notEmpty' => array('rule' => 'notEmpty', 'message' => 'Can not be empty'));
|
2012-05-05 18:13:40 +00:00
|
|
|
$Field = new CakeValidationSet('title', $rules);
|
2012-04-29 21:48:33 +00:00
|
|
|
$data = array(
|
|
|
|
'title' => '',
|
|
|
|
'body' => 'a body'
|
|
|
|
);
|
2012-04-23 11:02:26 +00:00
|
|
|
|
|
|
|
$result = $Field->getRule('notEmpty');
|
|
|
|
$this->assertInstanceOf('CakeRule', $result);
|
|
|
|
$this->assertEquals('notEmpty', $result->rule);
|
|
|
|
$this->assertEquals(null, $result->required);
|
|
|
|
$this->assertEquals(false, $result->allowEmpty);
|
|
|
|
$this->assertEquals(null, $result->on);
|
|
|
|
$this->assertEquals(true, $result->last);
|
|
|
|
$this->assertEquals('Can not be empty', $result->message);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testGetRules method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testGetRules() {
|
|
|
|
$rules = array('notEmpty' => array('rule' => 'notEmpty', 'message' => 'Can not be empty'));
|
2012-05-05 18:13:40 +00:00
|
|
|
$Field = new CakeValidationSet('title', $rules);
|
2012-04-23 11:02:26 +00:00
|
|
|
|
|
|
|
$result = $Field->getRules();
|
|
|
|
$this->assertEquals(array('notEmpty'), array_keys($result));
|
|
|
|
$this->assertInstanceOf('CakeRule', $result['notEmpty']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testSetRule method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSetRule() {
|
|
|
|
$rules = array('notEmpty' => array('rule' => 'notEmpty', 'message' => 'Can not be empty'));
|
2012-05-05 18:13:40 +00:00
|
|
|
$Field = new CakeValidationSet('title', $rules);
|
2012-05-05 23:16:58 +00:00
|
|
|
$Rule = new CakeRule($rules['notEmpty']);
|
2012-04-23 11:02:26 +00:00
|
|
|
|
|
|
|
$this->assertEquals($Rule, $Field->getRule('notEmpty'));
|
|
|
|
|
|
|
|
$rules = array('validEmail' => array('rule' => 'email', 'message' => 'Invalid email'));
|
2012-05-05 23:16:58 +00:00
|
|
|
$Rule = new CakeRule($rules['validEmail']);
|
2012-04-23 11:02:26 +00:00
|
|
|
$Field->setRule('validEmail', $Rule);
|
|
|
|
$result = $Field->getRules();
|
|
|
|
$this->assertEquals(array('notEmpty', 'validEmail'), array_keys($result));
|
|
|
|
|
|
|
|
$rules = array('validEmail' => array('rule' => 'email', 'message' => 'Other message'));
|
2012-05-05 23:16:58 +00:00
|
|
|
$Rule = new CakeRule($rules['validEmail']);
|
2012-04-23 11:02:26 +00:00
|
|
|
$Field->setRule('validEmail', $Rule);
|
|
|
|
$result = $Field->getRules();
|
|
|
|
$this->assertEquals(array('notEmpty', 'validEmail'), array_keys($result));
|
|
|
|
$result = $Field->getRule('validEmail');
|
|
|
|
$this->assertInstanceOf('CakeRule', $result);
|
|
|
|
$this->assertEquals('email', $result->rule);
|
|
|
|
$this->assertEquals(null, $result->required);
|
|
|
|
$this->assertEquals(false, $result->allowEmpty);
|
|
|
|
$this->assertEquals(null, $result->on);
|
|
|
|
$this->assertEquals(true, $result->last);
|
|
|
|
$this->assertEquals('Other message', $result->message);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testSetRules method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSetRules() {
|
|
|
|
$rule = array('notEmpty' => array('rule' => 'notEmpty', 'message' => 'Can not be empty'));
|
2012-05-05 18:13:40 +00:00
|
|
|
$Field = new CakeValidationSet('title', $rule);
|
2012-05-05 23:16:58 +00:00
|
|
|
$RuleEmpty = new CakeRule($rule['notEmpty']);
|
2012-04-23 11:02:26 +00:00
|
|
|
|
|
|
|
$rule = array('validEmail' => array('rule' => 'email', 'message' => 'Invalid email'));
|
2012-05-05 23:16:58 +00:00
|
|
|
$RuleEmail = new CakeRule($rule['validEmail']);
|
2012-04-23 11:02:26 +00:00
|
|
|
|
|
|
|
$rules = array('validEmail' => $RuleEmail);
|
|
|
|
$Field->setRules($rules, false);
|
|
|
|
$result = $Field->getRules();
|
|
|
|
$this->assertEquals(array('validEmail'), array_keys($result));
|
|
|
|
|
|
|
|
$rules = array('notEmpty' => $RuleEmpty);
|
|
|
|
$Field->setRules($rules, true);
|
|
|
|
$result = $Field->getRules();
|
|
|
|
$this->assertEquals(array('validEmail', 'notEmpty'), array_keys($result));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|