Initial commit

This commit is contained in:
mareksebera 2014-09-10 20:20:58 +02:00
commit 3b93da31de
1004 changed files with 265840 additions and 0 deletions

View file

@ -0,0 +1,198 @@
<?php
/**
* CakeValidationRuleTest file
*
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) 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 http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('CakeValidationRule', 'Model/Validator');
/**
* CakeValidationRuleTest
*
* @package Cake.Test.Case.Model.Validator
*/
class CakeValidationRuleTest extends CakeTestCase {
/**
* Auxiliary method to test custom validators
*
* @return bool
*/
public function myTestRule() {
return false;
}
/**
* Auxiliary method to test custom validators
*
* @return bool
*/
public function myTestRule2() {
return true;
}
/**
* Auxiliary method to test custom validators
*
* @return string
*/
public function myTestRule3() {
return 'string';
}
/**
* Test isValid method
*
* @return void
*/
public function testIsValid() {
$def = array('rule' => 'notEmpty', 'message' => 'Can not be empty');
$data = array(
'fieldName' => ''
);
$methods = array();
$Rule = new CakeValidationRule($def);
$Rule->process('fieldName', $data, $methods);
$this->assertFalse($Rule->isValid());
$data = array('fieldName' => 'not empty');
$Rule->process('fieldName', $data, $methods);
$this->assertTrue($Rule->isValid());
}
/**
* tests that passing custom validation methods work
*
* @return void
*/
public function testCustomMethods() {
$def = array('rule' => 'myTestRule');
$data = array(
'fieldName' => 'some data'
);
$methods = array('mytestrule' => array($this, 'myTestRule'));
$Rule = new CakeValidationRule($def);
$Rule->process('fieldName', $data, $methods);
$this->assertFalse($Rule->isValid());
$methods = array('mytestrule' => array($this, 'myTestRule2'));
$Rule->process('fieldName', $data, $methods);
$this->assertTrue($Rule->isValid());
$methods = array('mytestrule' => array($this, 'myTestRule3'));
$Rule->process('fieldName', $data, $methods);
$this->assertFalse($Rule->isValid());
}
/**
* Make sure errors are triggered when validation is missing.
*
* @expectedException PHPUnit_Framework_Error_Warning
* @expectedExceptionMessage Could not find validation handler totallyMissing for fieldName
* @return void
*/
public function testCustomMethodMissingError() {
$def = array('rule' => array('totallyMissing'));
$data = array(
'fieldName' => 'some data'
);
$methods = array('mytestrule' => array($this, 'myTestRule'));
$Rule = new CakeValidationRule($def);
$Rule->process('fieldName', $data, $methods);
}
/**
* Test isRequired method
*
* @return void
*/
public function testIsRequired() {
$def = array('rule' => 'notEmpty', 'required' => true);
$Rule = new CakeValidationRule($def);
$this->assertTrue($Rule->isRequired());
$def = array('rule' => 'notEmpty', 'required' => false);
$Rule = new CakeValidationRule($def);
$this->assertFalse($Rule->isRequired());
$def = array('rule' => 'notEmpty', 'required' => 'create');
$Rule = new CakeValidationRule($def);
$this->assertTrue($Rule->isRequired());
$def = array('rule' => 'notEmpty', 'required' => 'update');
$Rule = new CakeValidationRule($def);
$this->assertFalse($Rule->isRequired());
$Rule->isUpdate(true);
$this->assertTrue($Rule->isRequired());
}
/**
* Test isEmptyAllowed method
*
* @return void
*/
public function testIsEmptyAllowed() {
$def = array('rule' => 'aRule', 'allowEmpty' => true);
$Rule = new CakeValidationRule($def);
$this->assertTrue($Rule->isEmptyAllowed());
$def = array('rule' => 'aRule', 'allowEmpty' => false);
$Rule = new CakeValidationRule($def);
$this->assertFalse($Rule->isEmptyAllowed());
$def = array('rule' => 'notEmpty', 'allowEmpty' => false, 'on' => 'update');
$Rule = new CakeValidationRule($def);
$this->assertTrue($Rule->isEmptyAllowed());
$Rule->isUpdate(true);
$this->assertFalse($Rule->isEmptyAllowed());
$def = array('rule' => 'notEmpty', 'allowEmpty' => false, 'on' => 'create');
$Rule = new CakeValidationRule($def);
$this->assertFalse($Rule->isEmptyAllowed());
$Rule->isUpdate(true);
$this->assertTrue($Rule->isEmptyAllowed());
}
/**
* Test checkRequired method
*
* @return void
*/
public function testCheckRequiredWhenRequiredAndAllowEmpty() {
$Rule = $this->getMock('CakeValidationRule', array('isRequired'));
$Rule->expects($this->any())
->method('isRequired')
->will($this->returnValue(true));
$Rule->allowEmpty = true;
$fieldname = 'field';
$data = array(
$fieldname => null
);
$this->assertFalse($Rule->checkRequired($fieldname, $data), "A null but present field should not fail requirement check if allowEmpty is true");
$Rule->allowEmpty = false;
$this->assertTrue($Rule->checkRequired($fieldname, $data), "A null but present field should fail requirement check if allowEmpty is false");
}
}

View file

@ -0,0 +1,337 @@
<?php
/**
* CakeValidationSetTest file
*
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) 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 http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('CakeValidationSet', 'Model/Validator');
/**
* CakeValidationSetTest
*
* @package Cake.Test.Case.Model.Validator
*/
class CakeValidationSetTest extends CakeTestCase {
/**
* override locale to the default (eng).
*
* @return void
*/
public function setUp() {
parent::setUp();
Configure::write('Config.language', 'eng');
}
/**
* testValidate method
*
* @return void
*/
public function testValidate() {
$Field = new CakeValidationSet('title', 'notEmpty');
$data = array(
'title' => '',
'body' => 'a body'
);
$result = $Field->validate($data);
$expected = array('This field cannot be left blank');
$this->assertEquals($expected, $result);
$Field = new CakeValidationSet('body', 'notEmpty');
$result = $Field->validate($data);
$this->assertEmpty($result);
$Field = new CakeValidationSet('nothere', array(
'notEmpty' => array(
'rule' => 'notEmpty',
'required' => true
)
));
$result = $Field->validate($data);
$expected = array('notEmpty');
$this->assertEquals($expected, $result);
$Field = new CakeValidationSet('body', array(
'inList' => array(
'rule' => array('inList', array('test'))
)
));
$result = $Field->validate($data);
$expected = array('inList');
$this->assertEquals($expected, $result);
}
/**
* testGetRule method
*
* @return void
*/
public function testGetRule() {
$rules = array('notEmpty' => array('rule' => 'notEmpty', 'message' => 'Can not be empty'));
$Field = new CakeValidationSet('title', $rules);
$result = $Field->getRule('notEmpty');
$this->assertInstanceOf('CakeValidationRule', $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'));
$Field = new CakeValidationSet('title', $rules);
$result = $Field->getRules();
$this->assertEquals(array('notEmpty'), array_keys($result));
$this->assertInstanceOf('CakeValidationRule', $result['notEmpty']);
}
/**
* testSetRule method
*
* @return void
*/
public function testSetRule() {
$rules = array('notEmpty' => array('rule' => 'notEmpty', 'message' => 'Can not be empty'));
$Field = new CakeValidationSet('title', $rules);
$Rule = new CakeValidationRule($rules['notEmpty']);
$this->assertEquals($Rule, $Field->getRule('notEmpty'));
$rules = array('validEmail' => array('rule' => 'email', 'message' => 'Invalid email'));
$Rule = new CakeValidationRule($rules['validEmail']);
$Field->setRule('validEmail', $Rule);
$result = $Field->getRules();
$this->assertEquals(array('notEmpty', 'validEmail'), array_keys($result));
$rules = array('validEmail' => array('rule' => 'email', 'message' => 'Other message'));
$Rule = new CakeValidationRule($rules['validEmail']);
$Field->setRule('validEmail', $Rule);
$result = $Field->getRules();
$this->assertEquals(array('notEmpty', 'validEmail'), array_keys($result));
$result = $Field->getRule('validEmail');
$this->assertInstanceOf('CakeValidationRule', $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'));
$Field = new CakeValidationSet('title', $rule);
$RuleEmpty = new CakeValidationRule($rule['notEmpty']);
$rule = array('validEmail' => array('rule' => 'email', 'message' => 'Invalid email'));
$RuleEmail = new CakeValidationRule($rule['validEmail']);
$rules = array('validEmail' => $RuleEmail);
$Field->setRules($rules, false);
$result = $Field->getRules();
$this->assertEquals(array('validEmail'), array_keys($result));
$Field->setRules(array('validEmail' => $rule), false);
$result = $Field->getRules();
$this->assertEquals(array('validEmail'), array_keys($result));
$this->assertTrue(array_pop($result) instanceof CakeValidationRule);
$rules = array('notEmpty' => $RuleEmpty);
$Field->setRules($rules, true);
$result = $Field->getRules();
$this->assertEquals(array('validEmail', 'notEmpty'), array_keys($result));
$rules = array('notEmpty' => array('rule' => 'notEmpty'));
$Field->setRules($rules, true);
$result = $Field->getRules();
$this->assertEquals(array('validEmail', 'notEmpty'), array_keys($result));
$this->assertTrue(array_pop($result) instanceof CakeValidationRule);
$this->assertTrue(array_pop($result) instanceof CakeValidationRule);
}
/**
* Tests getting a rule from the set using array access
*
* @return void
*/
public function testArrayAccessGet() {
$Set = new CakeValidationSet('title', array(
'notEmpty' => array('rule' => 'notEmpty', 'required' => true),
'numeric' => array('rule' => 'numeric'),
'other' => array('rule' => array('other', 1)),
));
$rule = $Set['notEmpty'];
$this->assertInstanceOf('CakeValidationRule', $rule);
$this->assertEquals('notEmpty', $rule->rule);
$rule = $Set['numeric'];
$this->assertInstanceOf('CakeValidationRule', $rule);
$this->assertEquals('numeric', $rule->rule);
$rule = $Set['other'];
$this->assertInstanceOf('CakeValidationRule', $rule);
$this->assertEquals(array('other', 1), $rule->rule);
}
/**
* Tests checking a rule from the set using array access
*
* @return void
*/
public function testArrayAccessExists() {
$Set = new CakeValidationSet('title', array(
'notEmpty' => array('rule' => 'notEmpty', 'required' => true),
'numeric' => array('rule' => 'numeric'),
'other' => array('rule' => array('other', 1)),
));
$this->assertTrue(isset($Set['notEmpty']));
$this->assertTrue(isset($Set['numeric']));
$this->assertTrue(isset($Set['other']));
$this->assertFalse(isset($Set['fail']));
}
/**
* Tests setting a rule in the set using array access
*
* @return void
*/
public function testArrayAccessSet() {
$Set = new CakeValidationSet('title', array(
'notEmpty' => array('rule' => 'notEmpty', 'required' => true),
));
$this->assertFalse(isset($Set['other']));
$Set['other'] = array('rule' => array('other', 1));
$rule = $Set['other'];
$this->assertInstanceOf('CakeValidationRule', $rule);
$this->assertEquals(array('other', 1), $rule->rule);
$this->assertFalse(isset($Set['numeric']));
$Set['numeric'] = new CakeValidationRule(array('rule' => 'numeric'));
$rule = $Set['numeric'];
$this->assertInstanceOf('CakeValidationRule', $rule);
$this->assertEquals('numeric', $rule->rule);
}
/**
* Tests unseting a rule from the set using array access
*
* @return void
*/
public function testArrayAccessUnset() {
$Set = new CakeValidationSet('title', array(
'notEmpty' => array('rule' => 'notEmpty', 'required' => true),
'numeric' => array('rule' => 'numeric'),
'other' => array('rule' => array('other', 1)),
));
unset($Set['notEmpty']);
$this->assertFalse(isset($Set['notEmpty']));
unset($Set['numeric']);
$this->assertFalse(isset($Set['notEmpty']));
unset($Set['other']);
$this->assertFalse(isset($Set['notEmpty']));
}
/**
* Tests it is possible to iterate a validation set object
*
* @return void
*/
public function testIterator() {
$Set = new CakeValidationSet('title', array(
'notEmpty' => array('rule' => 'notEmpty', 'required' => true),
'numeric' => array('rule' => 'numeric'),
'other' => array('rule' => array('other', 1)),
));
$i = 0;
foreach ($Set as $name => $rule) {
if ($i === 0) {
$this->assertEquals('notEmpty', $name);
}
if ($i === 1) {
$this->assertEquals('numeric', $name);
}
if ($i === 2) {
$this->assertEquals('other', $name);
}
$this->assertInstanceOf('CakeValidationRule', $rule);
$i++;
}
$this->assertEquals(3, $i);
}
/**
* Tests countable interface
*
* @return void
*/
public function testCount() {
$Set = new CakeValidationSet('title', array(
'notEmpty' => array('rule' => 'notEmpty', 'required' => true),
'numeric' => array('rule' => 'numeric'),
'other' => array('rule' => array('other', 1)),
));
$this->assertCount(3, $Set);
unset($Set['other']);
$this->assertCount(2, $Set);
}
/**
* Test removeRule method
*
* @return void
*/
public function testRemoveRule() {
$Set = new CakeValidationSet('title', array(
'notEmpty' => array('rule' => 'notEmpty', 'required' => true),
'numeric' => array('rule' => 'numeric'),
'other' => array('rule' => array('other', 1)),
));
$Set->removeRule('notEmpty');
$this->assertFalse(isset($Set['notEmpty']));
$Set->removeRule('numeric');
$this->assertFalse(isset($Set['numeric']));
$Set->removeRule('other');
$this->assertFalse(isset($Set['other']));
}
}