ValidationRuleTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * ValidationRuleTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  16. * @since CakePHP(tm) v 2.2.0
  17. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  18. */
  19. namespace Cake\Test\TestCase\Validation;
  20. use Cake\TestSuite\TestCase;
  21. use Cake\Validation\ValidationRule;
  22. /**
  23. * ValidationRuleTest
  24. *
  25. */
  26. class ValidationRuleTest extends TestCase {
  27. /**
  28. * Auxiliary method to test custom validators
  29. *
  30. * @return boolean
  31. */
  32. public function myTestRule() {
  33. return false;
  34. }
  35. /**
  36. * Auxiliary method to test custom validators
  37. *
  38. * @return boolean
  39. */
  40. public function myTestRule2() {
  41. return true;
  42. }
  43. /**
  44. * Auxiliary method to test custom validators
  45. *
  46. * @return string
  47. */
  48. public function myTestRule3() {
  49. return 'string';
  50. }
  51. /**
  52. * tests that passing custom validation methods work
  53. *
  54. * @return void
  55. */
  56. public function testCustomMethods() {
  57. $data = 'some data';
  58. $providers = ['default' => $this];
  59. $context = ['newRecord' => true];
  60. $Rule = new ValidationRule(['rule' => 'myTestRule']);
  61. $this->assertFalse($Rule->process($data, $providers, $context));
  62. $Rule = new ValidationRule(['rule' => 'myTestRule2']);
  63. $this->assertTrue($Rule->process($data, $providers, $context));
  64. $Rule = new ValidationRule(['rule' => 'myTestRule3']);
  65. $this->assertEquals('string', $Rule->process($data, $providers, $context));
  66. $Rule = new ValidationRule(['rule' => 'myTestRule', 'message' => 'foo']);
  67. $this->assertEquals('foo', $Rule->process($data, $providers, $context));
  68. }
  69. /**
  70. * Make sure errors are triggered when validation is missing.
  71. *
  72. * @expectedException \InvalidArgumentException
  73. * @expectedExceptionMessage Unable to call method totallyMissing in default provider
  74. * @return void
  75. */
  76. public function testCustomMethodMissingError() {
  77. $def = ['rule' => ['totallyMissing']];
  78. $data = 'some data';
  79. $providers = ['default' => $this];
  80. $Rule = new ValidationRule($def);
  81. $Rule->process($data, $providers, ['newRecord' => true]);
  82. }
  83. /**
  84. * Tests that a rule can be skipped
  85. *
  86. * @return void
  87. */
  88. public function testSkip() {
  89. $data = 'some data';
  90. $providers = ['default' => $this];
  91. $Rule = new ValidationRule([
  92. 'rule' => 'myTestRule',
  93. 'on' => 'create'
  94. ]);
  95. $this->assertFalse($Rule->process($data, $providers, ['newRecord' => true]));
  96. $Rule = new ValidationRule([
  97. 'rule' => 'myTestRule',
  98. 'on' => 'update'
  99. ]);
  100. $this->assertTrue($Rule->process($data, $providers, ['newRecord' => true]));
  101. $Rule = new ValidationRule([
  102. 'rule' => 'myTestRule',
  103. 'on' => 'update'
  104. ]);
  105. $this->assertFalse($Rule->process($data, $providers, ['newRecord' => false]));
  106. }
  107. /**
  108. * Tests that the 'on' key can be a callable function
  109. *
  110. * @return void
  111. */
  112. public function testCallableOn() {
  113. $data = 'some data';
  114. $providers = ['default' => $this];
  115. $Rule = new ValidationRule([
  116. 'rule' => 'myTestRule',
  117. 'on' => function($context) use ($providers) {
  118. $expected = compact('providers') + ['newRecord' => true, 'data' => []];
  119. $this->assertEquals($expected, $context);
  120. return true;
  121. }
  122. ]);
  123. $this->assertFalse($Rule->process($data, $providers, ['newRecord' => true]));
  124. $Rule = new ValidationRule([
  125. 'rule' => 'myTestRule',
  126. 'on' => function($context) use ($providers) {
  127. $expected = compact('providers') + ['newRecord' => true, 'data' => []];
  128. $this->assertEquals($expected, $context);
  129. return false;
  130. }
  131. ]);
  132. $this->assertTrue($Rule->process($data, $providers, ['newRecord' => true]));
  133. }
  134. }