PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/TestCase/Validation/ValidationRuleTest.php

https://github.com/ceeram/cakephp
PHP | 196 lines | 107 code | 25 blank | 64 comment | 0 complexity | ceeca149d7990b45539aeb62ba1d14aa MD5 | raw file
  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. /**
  25. * Auxiliary method to test custom validators
  26. *
  27. * @return bool
  28. */
  29. public function myTestRule()
  30. {
  31. return false;
  32. }
  33. /**
  34. * Auxiliary method to test custom validators
  35. *
  36. * @return bool
  37. */
  38. public function myTestRule2()
  39. {
  40. return true;
  41. }
  42. /**
  43. * Auxiliary method to test custom validators
  44. *
  45. * @return string
  46. */
  47. public function myTestRule3()
  48. {
  49. return 'string';
  50. }
  51. /**
  52. * tests that passing custom validation methods work
  53. *
  54. * @return void
  55. */
  56. public function testCustomMethods()
  57. {
  58. $data = 'some data';
  59. $providers = ['default' => $this];
  60. $context = ['newRecord' => true];
  61. $Rule = new ValidationRule(['rule' => 'myTestRule']);
  62. $this->assertFalse($Rule->process($data, $providers, $context));
  63. $Rule = new ValidationRule(['rule' => 'myTestRule2']);
  64. $this->assertTrue($Rule->process($data, $providers, $context));
  65. $Rule = new ValidationRule(['rule' => 'myTestRule3']);
  66. $this->assertEquals('string', $Rule->process($data, $providers, $context));
  67. $Rule = new ValidationRule(['rule' => 'myTestRule', 'message' => 'foo']);
  68. $this->assertEquals('foo', $Rule->process($data, $providers, $context));
  69. }
  70. /**
  71. * Test using a custom validation method with no provider declared.
  72. *
  73. * @return void
  74. */
  75. public function testCustomMethodNoProvider()
  76. {
  77. $data = 'some data';
  78. $context = ['field' => 'custom', 'newRecord' => true];
  79. $providers = ['default' => ''];
  80. $rule = new ValidationRule([
  81. 'rule' => [$this, 'myTestRule']
  82. ]);
  83. $this->assertFalse($rule->process($data, $providers, $context));
  84. }
  85. /**
  86. * Make sure errors are triggered when validation is missing.
  87. *
  88. * @expectedException \InvalidArgumentException
  89. * @expectedExceptionMessage Unable to call method "totallyMissing" in "default" provider for field "test"
  90. * @return void
  91. */
  92. public function testCustomMethodMissingError()
  93. {
  94. $def = ['rule' => ['totallyMissing']];
  95. $data = 'some data';
  96. $providers = ['default' => $this];
  97. $Rule = new ValidationRule($def);
  98. $Rule->process($data, $providers, ['newRecord' => true, 'field' => 'test']);
  99. }
  100. /**
  101. * Tests that a rule can be skipped
  102. *
  103. * @return void
  104. */
  105. public function testSkip()
  106. {
  107. $data = 'some data';
  108. $providers = ['default' => $this];
  109. $Rule = new ValidationRule([
  110. 'rule' => 'myTestRule',
  111. 'on' => 'create'
  112. ]);
  113. $this->assertFalse($Rule->process($data, $providers, ['newRecord' => true]));
  114. $Rule = new ValidationRule([
  115. 'rule' => 'myTestRule',
  116. 'on' => 'update'
  117. ]);
  118. $this->assertTrue($Rule->process($data, $providers, ['newRecord' => true]));
  119. $Rule = new ValidationRule([
  120. 'rule' => 'myTestRule',
  121. 'on' => 'update'
  122. ]);
  123. $this->assertFalse($Rule->process($data, $providers, ['newRecord' => false]));
  124. }
  125. /**
  126. * Tests that the 'on' key can be a callable function
  127. *
  128. * @return void
  129. */
  130. public function testCallableOn()
  131. {
  132. $data = 'some data';
  133. $providers = ['default' => $this];
  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 true;
  140. }
  141. ]);
  142. $this->assertFalse($Rule->process($data, $providers, ['newRecord' => true]));
  143. $Rule = new ValidationRule([
  144. 'rule' => 'myTestRule',
  145. 'on' => function ($context) use ($providers) {
  146. $expected = compact('providers') + ['newRecord' => true, 'data' => []];
  147. $this->assertEquals($expected, $context);
  148. return false;
  149. }
  150. ]);
  151. $this->assertTrue($Rule->process($data, $providers, ['newRecord' => true]));
  152. }
  153. /**
  154. * testGet
  155. *
  156. * @return void
  157. */
  158. public function testGet()
  159. {
  160. $Rule = new ValidationRule(['rule' => 'myTestRule', 'message' => 'foo']);
  161. $this->assertEquals('myTestRule', $Rule->get('rule'));
  162. $this->assertEquals('foo', $Rule->get('message'));
  163. $this->assertEquals('default', $Rule->get('provider'));
  164. $this->assertEquals([], $Rule->get('pass'));
  165. $this->assertNull($Rule->get('non-existent'));
  166. $Rule = new ValidationRule(['rule' => ['myTestRule2', 'param'], 'message' => 'bar']);
  167. $this->assertEquals('myTestRule2', $Rule->get('rule'));
  168. $this->assertEquals('bar', $Rule->get('message'));
  169. $this->assertEquals(['param'], $Rule->get('pass'));
  170. }
  171. }