ValidationRuleTest.php 4.3 KB

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