ValidationRuleTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. * Make sure errors are triggered when validation is missing.
  67. *
  68. * @expectedException \InvalidArgumentException
  69. * @expectedExceptionMessage Unable to call method "totallyMissing" in "default" provider for field "test"
  70. * @return void
  71. */
  72. public function testCustomMethodMissingError() {
  73. $def = ['rule' => ['totallyMissing']];
  74. $data = 'some data';
  75. $providers = ['default' => $this];
  76. $Rule = new ValidationRule($def);
  77. $Rule->process($data, $providers, ['newRecord' => true, 'field' => 'test']);
  78. }
  79. /**
  80. * Tests that a rule can be skipped
  81. *
  82. * @return void
  83. */
  84. public function testSkip() {
  85. $data = 'some data';
  86. $providers = ['default' => $this];
  87. $Rule = new ValidationRule([
  88. 'rule' => 'myTestRule',
  89. 'on' => 'create'
  90. ]);
  91. $this->assertFalse($Rule->process($data, $providers, ['newRecord' => true]));
  92. $Rule = new ValidationRule([
  93. 'rule' => 'myTestRule',
  94. 'on' => 'update'
  95. ]);
  96. $this->assertTrue($Rule->process($data, $providers, ['newRecord' => true]));
  97. $Rule = new ValidationRule([
  98. 'rule' => 'myTestRule',
  99. 'on' => 'update'
  100. ]);
  101. $this->assertFalse($Rule->process($data, $providers, ['newRecord' => false]));
  102. }
  103. /**
  104. * Tests that the 'on' key can be a callable function
  105. *
  106. * @return void
  107. */
  108. public function testCallableOn() {
  109. $data = 'some data';
  110. $providers = ['default' => $this];
  111. $Rule = new ValidationRule([
  112. 'rule' => 'myTestRule',
  113. 'on' => function($context) use ($providers) {
  114. $expected = compact('providers') + ['newRecord' => true, 'data' => []];
  115. $this->assertEquals($expected, $context);
  116. return true;
  117. }
  118. ]);
  119. $this->assertFalse($Rule->process($data, $providers, ['newRecord' => true]));
  120. $Rule = new ValidationRule([
  121. 'rule' => 'myTestRule',
  122. 'on' => function($context) use ($providers) {
  123. $expected = compact('providers') + ['newRecord' => true, 'data' => []];
  124. $this->assertEquals($expected, $context);
  125. return false;
  126. }
  127. ]);
  128. $this->assertTrue($Rule->process($data, $providers, ['newRecord' => true]));
  129. }
  130. }