ValidationRuleTest.php 5.6 KB

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