EntityValidatorTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  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://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\ORM;
  16. use Cake\ORM\Entity;
  17. use Cake\ORM\EntityValidator;
  18. use Cake\ORM\Table;
  19. use Cake\ORM\TableRegistry;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * EntityValidator test
  23. */
  24. class EntityValidatorTest extends TestCase {
  25. /**
  26. * setup
  27. *
  28. * @return void
  29. */
  30. public function setUp() {
  31. parent::setUp();
  32. $articles = TableRegistry::get('Articles');
  33. $users = TableRegistry::get('Users');
  34. $articles->belongsTo('Users');
  35. $articles->hasMany('Comments');
  36. $comments = TableRegistry::get('Comments');
  37. $comments->belongsTo('Articles');
  38. $comments->belongsTo('Users');
  39. $this->articles = $articles;
  40. $this->comments = $comments;
  41. $this->users = $users;
  42. }
  43. /**
  44. * Teardown
  45. *
  46. * @return void
  47. */
  48. public function tearDown() {
  49. parent::tearDown();
  50. TableRegistry::clear();
  51. unset($this->articles, $this->comments, $this->users);
  52. }
  53. /**
  54. * Test one() with successful validate
  55. *
  56. * @return void
  57. */
  58. public function testOneSuccess() {
  59. $entity = $this->getMock('\Cake\ORM\Entity', ['validate']);
  60. $validator = $this->getMock('\Cake\Validation\Validator');
  61. $this->articles->validator('default', $validator);
  62. $entityValidator = new EntityValidator($this->articles);
  63. $validator->expects($this->once())
  64. ->method('count')
  65. ->will($this->returnValue(1));
  66. $entity->expects($this->once())
  67. ->method('validate')
  68. ->with($validator)
  69. ->will($this->returnValue(true));
  70. $this->assertTrue($entityValidator->one($entity));
  71. }
  72. /**
  73. * Test one() with failing validate
  74. *
  75. * @return void
  76. */
  77. public function testOneFail() {
  78. $entity = $this->getMock('\Cake\ORM\Entity', ['validate']);
  79. $validator = $this->getMock('\Cake\Validation\Validator');
  80. $this->articles->validator('default', $validator);
  81. $entityValidator = new EntityValidator($this->articles);
  82. $validator->expects($this->once())
  83. ->method('count')
  84. ->will($this->returnValue(1));
  85. $entity->expects($this->once())
  86. ->method('validate')
  87. ->with($validator)
  88. ->will($this->returnValue(false));
  89. $this->assertFalse($entityValidator->one($entity));
  90. }
  91. /**
  92. * test one() with association data.
  93. *
  94. * @return void
  95. */
  96. public function testOneAssociationsSuccess() {
  97. $article = $this->getMock('\Cake\ORM\Entity', ['validate']);
  98. $comment1 = $this->getMock('\Cake\ORM\Entity', ['validate']);
  99. $comment2 = $this->getMock('\Cake\ORM\Entity', ['validate']);
  100. $user = $this->getMock('\Cake\ORM\Entity', ['validate']);
  101. $article->set('comments', [$comment1, $comment2]);
  102. $article->set('user', $user);
  103. $validator1 = $this->getMock('\Cake\Validation\Validator');
  104. $validator2 = $this->getMock('\Cake\Validation\Validator');
  105. $validator3 = $this->getMock('\Cake\Validation\Validator');
  106. $validator1->expects($this->once())
  107. ->method('count')
  108. ->will($this->returnValue(1));
  109. $validator2->expects($this->exactly(2))
  110. ->method('count')
  111. ->will($this->returnValue(1));
  112. $validator3->expects($this->once())
  113. ->method('count')
  114. ->will($this->returnValue(1));
  115. $this->articles->validator('default', $validator1);
  116. $this->comments->validator('default', $validator2);
  117. $this->users->validator('default', $validator3);
  118. $entityValidator = new EntityValidator($this->articles);
  119. $article->expects($this->once())
  120. ->method('validate')
  121. ->with($validator1)
  122. ->will($this->returnValue(true));
  123. $comment1->expects($this->once())
  124. ->method('validate')
  125. ->with($validator2)
  126. ->will($this->returnValue(true));
  127. $comment2->expects($this->once())
  128. ->method('validate')
  129. ->with($validator2)
  130. ->will($this->returnValue(true));
  131. $user->expects($this->once())
  132. ->method('validate')
  133. ->with($validator3)
  134. ->will($this->returnValue(true));
  135. $options = ['associated' => ['Comments', 'Users']];
  136. $this->assertTrue($entityValidator->one($article, $options));
  137. }
  138. /**
  139. * test one() with association data and one of them failing validation.
  140. *
  141. * @return void
  142. */
  143. public function testOneAssociationsFail() {
  144. $article = $this->getMock('\Cake\ORM\Entity', ['validate']);
  145. $comment1 = $this->getMock('\Cake\ORM\Entity', ['validate']);
  146. $comment2 = $this->getMock('\Cake\ORM\Entity', ['validate']);
  147. $user = $this->getMock('\Cake\ORM\Entity', ['validate']);
  148. $article->set('comments', [$comment1, $comment2]);
  149. $article->set('user', $user);
  150. $validator1 = $this->getMock('\Cake\Validation\Validator');
  151. $validator2 = $this->getMock('\Cake\Validation\Validator');
  152. $validator3 = $this->getMock('\Cake\Validation\Validator');
  153. $validator1->expects($this->once())
  154. ->method('count')
  155. ->will($this->returnValue(1));
  156. $validator2->expects($this->exactly(2))
  157. ->method('count')
  158. ->will($this->returnValue(1));
  159. $validator3->expects($this->once())
  160. ->method('count')
  161. ->will($this->returnValue(1));
  162. $this->articles->validator('default', $validator1);
  163. $this->comments->validator('default', $validator2);
  164. $this->users->validator('default', $validator3);
  165. $entityValidator = new EntityValidator($this->articles);
  166. $article->expects($this->once())
  167. ->method('validate')
  168. ->with($validator1)
  169. ->will($this->returnValue(true));
  170. $comment1->expects($this->once())
  171. ->method('validate')
  172. ->with($validator2)
  173. ->will($this->returnValue(true));
  174. $comment2->expects($this->once())
  175. ->method('validate')
  176. ->with($validator2)
  177. ->will($this->returnValue(false));
  178. $user->expects($this->once())
  179. ->method('validate')
  180. ->with($validator3)
  181. ->will($this->returnValue(true));
  182. $options = ['associated' => ['Comments', 'Users']];
  183. $this->assertFalse($entityValidator->one($article, $options));
  184. }
  185. /**
  186. * Test one() with deeper associations and passing the name for custom
  187. * validators
  188. *
  189. * @return void
  190. */
  191. public function testOneDeepAssociationsAndCustomValidators() {
  192. $comment = $this->getMock('\Cake\ORM\Entity', ['validate']);
  193. $article = $this->getMock('\Cake\ORM\Entity', ['validate']);
  194. $user = $this->getMock('\Cake\ORM\Entity', ['validate']);
  195. $comment->set('article', $article);
  196. $article->set('user', $user);
  197. $validator1 = $this->getMock('\Cake\Validation\Validator');
  198. $validator2 = $this->getMock('\Cake\Validation\Validator');
  199. $validator3 = $this->getMock('\Cake\Validation\Validator');
  200. $validator1->expects($this->once())
  201. ->method('count')
  202. ->will($this->returnValue(1));
  203. $validator2->expects($this->once())
  204. ->method('count')
  205. ->will($this->returnValue(1));
  206. $validator3->expects($this->once())
  207. ->method('count')
  208. ->will($this->returnValue(1));
  209. $this->articles->validator('crazy', $validator1);
  210. $this->comments->validator('default', $validator2);
  211. $this->users->validator('funky', $validator3);
  212. $entityValidator = new EntityValidator($this->comments);
  213. $comment->expects($this->once())
  214. ->method('validate')
  215. ->with($validator2)
  216. ->will($this->returnValue(true));
  217. $article->expects($this->once())
  218. ->method('validate')
  219. ->with($validator1)
  220. ->will($this->returnValue(true));
  221. $user->expects($this->once())
  222. ->method('validate')
  223. ->with($validator3)
  224. ->will($this->returnValue(true));
  225. $this->assertTrue($entityValidator->one($comment, [
  226. 'associated' => [
  227. 'Articles' => [
  228. 'validate' => 'crazy',
  229. 'associated' => ['Users' => ['validate' => 'funky']]
  230. ]
  231. ]
  232. ]));
  233. }
  234. /**
  235. * Test many() with successful validate
  236. *
  237. * @return void
  238. */
  239. public function testManySuccess() {
  240. $comment1 = $this->getMock('\Cake\ORM\Entity', ['validate']);
  241. $comment2 = $this->getMock('\Cake\ORM\Entity', ['validate']);
  242. $validator = $this->getMock('\Cake\Validation\Validator');
  243. $data = [$comment1, $comment2];
  244. $this->comments->validator('default', $validator);
  245. $entityValidator = new EntityValidator($this->comments);
  246. $validator->expects($this->exactly(2))
  247. ->method('count')
  248. ->will($this->returnValue(1));
  249. $comment1->expects($this->once())
  250. ->method('validate')
  251. ->with($validator)
  252. ->will($this->returnValue(true));
  253. $comment2->expects($this->once())
  254. ->method('validate')
  255. ->with($validator)
  256. ->will($this->returnValue(true));
  257. $this->assertTrue($entityValidator->many($data));
  258. }
  259. /**
  260. * Test many() with failure
  261. *
  262. * @return void
  263. */
  264. public function testManyFailure() {
  265. $comment1 = $this->getMock('\Cake\ORM\Entity', ['validate']);
  266. $comment2 = $this->getMock('\Cake\ORM\Entity', ['validate']);
  267. $validator = $this->getMock('\Cake\Validation\Validator');
  268. $data = [$comment1, $comment2];
  269. $this->comments->validator('default', $validator);
  270. $entityValidator = new EntityValidator($this->comments);
  271. $validator->expects($this->exactly(2))
  272. ->method('count')
  273. ->will($this->returnValue(1));
  274. $comment1->expects($this->once())
  275. ->method('validate')
  276. ->with($validator)
  277. ->will($this->returnValue(false));
  278. $comment2->expects($this->once())
  279. ->method('validate')
  280. ->with($validator)
  281. ->will($this->returnValue(true));
  282. $this->assertFalse($entityValidator->many($data));
  283. }
  284. }