ValidationRuleTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <https://book.cakephp.org/view/1196/Testing>
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  12. * @since 2.2.0
  13. * @license https://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. class ValidationRuleTest extends TestCase
  22. {
  23. /**
  24. * Auxiliary method to test custom validators
  25. *
  26. * @return bool
  27. */
  28. public function willFail()
  29. {
  30. return false;
  31. }
  32. /**
  33. * Auxiliary method to test custom validators
  34. *
  35. * @return bool
  36. */
  37. public function willPass()
  38. {
  39. return true;
  40. }
  41. /**
  42. * Auxiliary method to test custom validators
  43. *
  44. * @return string
  45. */
  46. public function willFail3()
  47. {
  48. return 'string';
  49. }
  50. /**
  51. * tests that passing custom validation methods work
  52. *
  53. * @return void
  54. */
  55. public function testCustomMethods()
  56. {
  57. $data = 'some data';
  58. $providers = ['default' => $this];
  59. $context = ['newRecord' => true];
  60. $Rule = new ValidationRule(['rule' => 'willFail']);
  61. $this->assertFalse($Rule->process($data, $providers, $context));
  62. $Rule = new ValidationRule(['rule' => 'willPass', 'pass' => ['key' => 'value']]);
  63. $this->assertTrue($Rule->process($data, $providers, $context));
  64. $Rule = new ValidationRule(['rule' => 'willFail3']);
  65. $this->assertEquals('string', $Rule->process($data, $providers, $context));
  66. $Rule = new ValidationRule(['rule' => 'willFail', 'message' => 'foo']);
  67. $this->assertEquals('foo', $Rule->process($data, $providers, $context));
  68. }
  69. /**
  70. * Test using a custom validation method with no provider declared.
  71. *
  72. * @return void
  73. */
  74. public function testCustomMethodNoProvider()
  75. {
  76. $data = 'some data';
  77. $context = ['field' => 'custom', 'newRecord' => true];
  78. $providers = ['default' => ''];
  79. $rule = new ValidationRule([
  80. 'rule' => [$this, 'willFail']
  81. ]);
  82. $this->assertFalse($rule->process($data, $providers, $context));
  83. }
  84. /**
  85. * Make sure errors are triggered when validation is missing.
  86. *
  87. * @return void
  88. */
  89. public function testCustomMethodMissingError()
  90. {
  91. $this->expectException(\InvalidArgumentException::class);
  92. $this->expectExceptionMessage('Unable to call method "totallyMissing" in "default" provider for field "test"');
  93. $def = ['rule' => ['totallyMissing']];
  94. $data = 'some data';
  95. $providers = ['default' => $this];
  96. $Rule = new ValidationRule($def);
  97. $Rule->process($data, $providers, ['newRecord' => true, 'field' => 'test']);
  98. }
  99. /**
  100. * Tests that a rule can be skipped
  101. *
  102. * @return void
  103. */
  104. public function testSkip()
  105. {
  106. $data = 'some data';
  107. $providers = ['default' => $this];
  108. $Rule = new ValidationRule([
  109. 'rule' => 'willFail',
  110. 'on' => 'create'
  111. ]);
  112. $this->assertFalse($Rule->process($data, $providers, ['newRecord' => true]));
  113. $Rule = new ValidationRule([
  114. 'rule' => 'willFail',
  115. 'on' => 'update'
  116. ]);
  117. $this->assertTrue($Rule->process($data, $providers, ['newRecord' => true]));
  118. $Rule = new ValidationRule([
  119. 'rule' => 'willFail',
  120. 'on' => 'update'
  121. ]);
  122. $this->assertFalse($Rule->process($data, $providers, ['newRecord' => false]));
  123. }
  124. /**
  125. * Tests that the 'on' key can be a callable function
  126. *
  127. * @return void
  128. */
  129. public function testCallableOn()
  130. {
  131. $data = 'some data';
  132. $providers = ['default' => $this];
  133. $Rule = new ValidationRule([
  134. 'rule' => 'willFail',
  135. 'on' => function ($context) use ($providers) {
  136. $expected = compact('providers') + ['newRecord' => true, 'data' => []];
  137. $this->assertEquals($expected, $context);
  138. return true;
  139. }
  140. ]);
  141. $this->assertFalse($Rule->process($data, $providers, ['newRecord' => true]));
  142. $Rule = new ValidationRule([
  143. 'rule' => 'willFail',
  144. 'on' => function ($context) use ($providers) {
  145. $expected = compact('providers') + ['newRecord' => true, 'data' => []];
  146. $this->assertEquals($expected, $context);
  147. return false;
  148. }
  149. ]);
  150. $this->assertTrue($Rule->process($data, $providers, ['newRecord' => true]));
  151. }
  152. /**
  153. * testGet
  154. *
  155. * @return void
  156. */
  157. public function testGet()
  158. {
  159. $Rule = new ValidationRule(['rule' => 'willFail', 'message' => 'foo']);
  160. $this->assertEquals('willFail', $Rule->get('rule'));
  161. $this->assertEquals('foo', $Rule->get('message'));
  162. $this->assertEquals('default', $Rule->get('provider'));
  163. $this->assertEquals([], $Rule->get('pass'));
  164. $this->assertNull($Rule->get('non-existent'));
  165. $Rule = new ValidationRule(['rule' => ['willPass', 'param'], 'message' => 'bar']);
  166. $this->assertEquals('willPass', $Rule->get('rule'));
  167. $this->assertEquals('bar', $Rule->get('message'));
  168. $this->assertEquals(['param'], $Rule->get('pass'));
  169. }
  170. }