CakeValidationRuleTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * CakeValidationRuleTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package Cake.Test.Case.Model.Validator
  16. * @since CakePHP(tm) v 2.2.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('CakeValidationRule', 'Model/Validator');
  20. /**
  21. * CakeValidationRuleTest
  22. *
  23. * @package Cake.Test.Case.Model.Validator
  24. */
  25. class CakeValidationRuleTest extends CakeTestCase {
  26. /**
  27. * Auxiliary method to test custom validators
  28. *
  29. * @return boolean
  30. */
  31. public function myTestRule() {
  32. return false;
  33. }
  34. /**
  35. * Auxiliary method to test custom validators
  36. *
  37. * @return boolean
  38. */
  39. public function myTestRule2() {
  40. return true;
  41. }
  42. /**
  43. * Auxiliary method to test custom validators
  44. *
  45. * @return string
  46. */
  47. public function myTestRule3() {
  48. return 'string';
  49. }
  50. /**
  51. * Test isValid method
  52. *
  53. * @return void
  54. */
  55. public function testIsValid() {
  56. $def = array('rule' => 'notEmpty', 'message' => 'Can not be empty');
  57. $data = array(
  58. 'fieldName' => ''
  59. );
  60. $methods = array();
  61. $Rule = new CakeValidationRule($def);
  62. $Rule->process('fieldName', $data, $methods);
  63. $this->assertFalse($Rule->isValid());
  64. $data = array('fieldName' => 'not empty');
  65. $Rule->process('fieldName', $data, $methods);
  66. $this->assertTrue($Rule->isValid());
  67. }
  68. /**
  69. * tests that passing custom validation methods work
  70. *
  71. * @return void
  72. */
  73. public function testCustomMethods() {
  74. $def = array('rule' => 'myTestRule');
  75. $data = array(
  76. 'fieldName' => 'some data'
  77. );
  78. $methods = array('mytestrule' => array($this, 'myTestRule'));
  79. $Rule = new CakeValidationRule($def);
  80. $Rule->process('fieldName', $data, $methods);
  81. $this->assertFalse($Rule->isValid());
  82. $methods = array('mytestrule' => array($this, 'myTestRule2'));
  83. $Rule->process('fieldName', $data, $methods);
  84. $this->assertTrue($Rule->isValid());
  85. $methods = array('mytestrule' => array($this, 'myTestRule3'));
  86. $Rule->process('fieldName', $data, $methods);
  87. $this->assertFalse($Rule->isValid());
  88. }
  89. /**
  90. * Make sure errors are triggered when validation is missing.
  91. *
  92. * @expectedException PHPUnit_Framework_Error_Warning
  93. * @expectedExceptionMessage Could not find validation handler totallyMissing for fieldName
  94. * @return void
  95. */
  96. public function testCustomMethodMissingError() {
  97. $def = array('rule' => array('totallyMissing'));
  98. $data = array(
  99. 'fieldName' => 'some data'
  100. );
  101. $methods = array('mytestrule' => array($this, 'myTestRule'));
  102. $Rule = new CakeValidationRule($def);
  103. $Rule->process('fieldName', $data, $methods);
  104. }
  105. /**
  106. * Test isRequired method
  107. *
  108. * @return void
  109. */
  110. public function testIsRequired() {
  111. $def = array('rule' => 'notEmpty', 'required' => true);
  112. $Rule = new CakeValidationRule($def);
  113. $this->assertTrue($Rule->isRequired());
  114. $def = array('rule' => 'notEmpty', 'required' => false);
  115. $Rule = new CakeValidationRule($def);
  116. $this->assertFalse($Rule->isRequired());
  117. $def = array('rule' => 'notEmpty', 'required' => 'create');
  118. $Rule = new CakeValidationRule($def);
  119. $this->assertTrue($Rule->isRequired());
  120. $def = array('rule' => 'notEmpty', 'required' => 'update');
  121. $Rule = new CakeValidationRule($def);
  122. $this->assertFalse($Rule->isRequired());
  123. $Rule->isUpdate(true);
  124. $this->assertTrue($Rule->isRequired());
  125. }
  126. /**
  127. * Test isEmptyAllowed method
  128. *
  129. * @return void
  130. */
  131. public function testIsEmptyAllowed() {
  132. $def = array('rule' => 'aRule', 'allowEmpty' => true);
  133. $Rule = new CakeValidationRule($def);
  134. $this->assertTrue($Rule->isEmptyAllowed());
  135. $def = array('rule' => 'aRule', 'allowEmpty' => false);
  136. $Rule = new CakeValidationRule($def);
  137. $this->assertFalse($Rule->isEmptyAllowed());
  138. $def = array('rule' => 'notEmpty', 'allowEmpty' => false, 'on' => 'update');
  139. $Rule = new CakeValidationRule($def);
  140. $this->assertTrue($Rule->isEmptyAllowed());
  141. $Rule->isUpdate(true);
  142. $this->assertFalse($Rule->isEmptyAllowed());
  143. $def = array('rule' => 'notEmpty', 'allowEmpty' => false, 'on' => 'create');
  144. $Rule = new CakeValidationRule($def);
  145. $this->assertFalse($Rule->isEmptyAllowed());
  146. $Rule->isUpdate(true);
  147. $this->assertTrue($Rule->isEmptyAllowed());
  148. }
  149. /**
  150. * Test checkRequired method
  151. *
  152. * @return void
  153. */
  154. public function testCheckRequiredWhenRequiredAndAllowEmpty() {
  155. $Rule = $this->getMock('CakeValidationRule', array('isRequired'));
  156. $Rule->expects($this->any())
  157. ->method('isRequired')
  158. ->will($this->returnValue(true));
  159. $Rule->allowEmpty = true;
  160. $fieldname = 'field';
  161. $data = array(
  162. $fieldname => null
  163. );
  164. $this->assertFalse($Rule->checkRequired($fieldname, $data), "A null but present field should not fail requirement check if allowEmpty is true");
  165. $Rule->allowEmpty = false;
  166. $this->assertTrue($Rule->checkRequired($fieldname, $data), "A null but present field should fail requirement check if allowEmpty is false");
  167. }
  168. }