EntityValidatorTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. /**
  27. * setup
  28. *
  29. * @return void
  30. */
  31. public function setUp()
  32. {
  33. parent::setUp();
  34. $articles = TableRegistry::get('Articles');
  35. $users = TableRegistry::get('Users');
  36. $articles->belongsTo('Users');
  37. $articles->hasMany('Comments');
  38. $comments = TableRegistry::get('Comments');
  39. $comments->belongsTo('Articles');
  40. $comments->belongsTo('Users');
  41. $this->articles = $articles;
  42. $this->comments = $comments;
  43. $this->users = $users;
  44. }
  45. /**
  46. * Teardown
  47. *
  48. * @return void
  49. */
  50. public function tearDown()
  51. {
  52. parent::tearDown();
  53. TableRegistry::clear();
  54. unset($this->articles, $this->comments, $this->users);
  55. }
  56. /**
  57. * Test one() with successful validate
  58. *
  59. * @return void
  60. */
  61. public function testOneSuccess()
  62. {
  63. $entity = $this->getMock('TestApp\Model\Entity\ValidatableEntity', ['validate']);
  64. $validator = $this->getMock('\Cake\Validation\Validator');
  65. $this->articles->validator('default', $validator);
  66. $entityValidator = new EntityValidator($this->articles);
  67. $validator->expects($this->once())
  68. ->method('count')
  69. ->will($this->returnValue(1));
  70. $entity->expects($this->once())
  71. ->method('validate')
  72. ->with($validator)
  73. ->will($this->returnValue([]));
  74. $this->assertTrue($entityValidator->one($entity));
  75. }
  76. /**
  77. * Test one() with failing validate
  78. *
  79. * @return void
  80. */
  81. public function testOneFail()
  82. {
  83. $entity = $this->getMock('TestApp\Model\Entity\ValidatableEntity', ['validate']);
  84. $validator = $this->getMock('\Cake\Validation\Validator');
  85. $this->articles->validator('default', $validator);
  86. $entityValidator = new EntityValidator($this->articles);
  87. $validator->expects($this->once())
  88. ->method('count')
  89. ->will($this->returnValue(1));
  90. $entity->expects($this->once())
  91. ->method('validate')
  92. ->with($validator)
  93. ->will($this->returnValue(['one' => ['error']]));
  94. $this->assertFalse($entityValidator->one($entity));
  95. }
  96. /**
  97. * test one() with association data.
  98. *
  99. * @return void
  100. */
  101. public function testOneAssociationsSuccess()
  102. {
  103. $class = 'TestApp\Model\Entity\ValidatableEntity';
  104. $article = $this->getMock($class, ['validate']);
  105. $comment1 = $this->getMock($class, ['validate']);
  106. $comment2 = $this->getMock($class, ['validate']);
  107. $user = $this->getMock($class, ['validate']);
  108. $article->set('comments', [$comment1, $comment2]);
  109. $article->set('user', $user);
  110. $validator1 = $this->getMock('\Cake\Validation\Validator');
  111. $validator2 = $this->getMock('\Cake\Validation\Validator');
  112. $validator3 = $this->getMock('\Cake\Validation\Validator');
  113. $validator1->expects($this->once())
  114. ->method('count')
  115. ->will($this->returnValue(1));
  116. $validator2->expects($this->exactly(2))
  117. ->method('count')
  118. ->will($this->returnValue(1));
  119. $validator3->expects($this->once())
  120. ->method('count')
  121. ->will($this->returnValue(1));
  122. $this->articles->validator('default', $validator1);
  123. $this->comments->validator('default', $validator2);
  124. $this->users->validator('default', $validator3);
  125. $entityValidator = new EntityValidator($this->articles);
  126. $article->expects($this->once())
  127. ->method('validate')
  128. ->with($validator1)
  129. ->will($this->returnValue([]));
  130. $comment1->expects($this->once())
  131. ->method('validate')
  132. ->with($validator2)
  133. ->will($this->returnValue([]));
  134. $comment2->expects($this->once())
  135. ->method('validate')
  136. ->with($validator2)
  137. ->will($this->returnValue([]));
  138. $user->expects($this->once())
  139. ->method('validate')
  140. ->with($validator3)
  141. ->will($this->returnValue([]));
  142. $options = ['associated' => ['Comments', 'Users']];
  143. $this->assertTrue($entityValidator->one($article, $options));
  144. }
  145. /**
  146. * test one() with associations that are not entities.
  147. *
  148. * This can happen when request data is not completely marshalled.
  149. * incomplete associations should not cause warnings or fatal errors.
  150. *
  151. * @return void
  152. */
  153. public function testOneAssociationsNoEntities()
  154. {
  155. $class = 'TestApp\Model\Entity\ValidatableEntity';
  156. $article = $this->getMock($class, ['validate']);
  157. $comment1 = ['comment' => 'test'];
  158. $comment2 = ['comment' => 'omg'];
  159. $user = $this->getMock($class, ['validate']);
  160. $article->set('comments', [$comment1, $comment2]);
  161. $validator1 = $this->getMock('\Cake\Validation\Validator');
  162. $validator2 = $this->getMock('\Cake\Validation\Validator');
  163. $validator1->expects($this->once())
  164. ->method('count')
  165. ->will($this->returnValue(1));
  166. // Should not be called as comments are not entities.
  167. $validator2->expects($this->never())
  168. ->method('count');
  169. $this->articles->validator('default', $validator1);
  170. $this->comments->validator('default', $validator2);
  171. $entityValidator = new EntityValidator($this->articles);
  172. $article->expects($this->once())
  173. ->method('validate')
  174. ->with($validator1)
  175. ->will($this->returnValue([]));
  176. $options = ['associated' => ['Comments']];
  177. $this->assertFalse($entityValidator->one($article, $options));
  178. }
  179. /**
  180. * test one() with association data and one of them failing validation.
  181. *
  182. * @return void
  183. */
  184. public function testOneAssociationsFail()
  185. {
  186. $class = 'TestApp\Model\Entity\ValidatableEntity';
  187. $article = $this->getMock($class, ['validate']);
  188. $comment1 = $this->getMock($class, ['validate']);
  189. $comment2 = $this->getMock($class, ['validate']);
  190. $user = $this->getMock($class, ['validate']);
  191. $article->set('comments', [$comment1, $comment2]);
  192. $article->set('user', $user);
  193. $validator1 = $this->getMock('\Cake\Validation\Validator');
  194. $validator2 = $this->getMock('\Cake\Validation\Validator');
  195. $validator3 = $this->getMock('\Cake\Validation\Validator');
  196. $validator1->expects($this->once())
  197. ->method('count')
  198. ->will($this->returnValue(1));
  199. $validator2->expects($this->exactly(2))
  200. ->method('count')
  201. ->will($this->returnValue(1));
  202. $validator3->expects($this->once())
  203. ->method('count')
  204. ->will($this->returnValue(1));
  205. $this->articles->validator('default', $validator1);
  206. $this->comments->validator('default', $validator2);
  207. $this->users->validator('default', $validator3);
  208. $entityValidator = new EntityValidator($this->articles);
  209. $article->expects($this->once())
  210. ->method('validate')
  211. ->with($validator1)
  212. ->will($this->returnValue([]));
  213. $comment1->expects($this->once())
  214. ->method('validate')
  215. ->with($validator2)
  216. ->will($this->returnValue([]));
  217. $comment2->expects($this->once())
  218. ->method('validate')
  219. ->with($validator2)
  220. ->will($this->returnValue(['some' => ['error']]));
  221. $user->expects($this->once())
  222. ->method('validate')
  223. ->with($validator3)
  224. ->will($this->returnValue([]));
  225. $options = ['associated' => ['Comments', 'Users']];
  226. $this->assertFalse($entityValidator->one($article, $options));
  227. }
  228. /**
  229. * Test one() with deeper associations and passing the name for custom
  230. * validators
  231. *
  232. * @return void
  233. */
  234. public function testOneDeepAssociationsAndCustomValidators()
  235. {
  236. $class = 'TestApp\Model\Entity\ValidatableEntity';
  237. $comment = $this->getMock($class, ['validate']);
  238. $article = $this->getMock($class, ['validate']);
  239. $user = $this->getMock($class, ['validate']);
  240. $comment->set('article', $article);
  241. $article->set('user', $user);
  242. $validator1 = $this->getMock('\Cake\Validation\Validator');
  243. $validator2 = $this->getMock('\Cake\Validation\Validator');
  244. $validator3 = $this->getMock('\Cake\Validation\Validator');
  245. $validator1->expects($this->once())
  246. ->method('count')
  247. ->will($this->returnValue(1));
  248. $validator2->expects($this->once())
  249. ->method('count')
  250. ->will($this->returnValue(1));
  251. $validator3->expects($this->once())
  252. ->method('count')
  253. ->will($this->returnValue(1));
  254. $this->articles->validator('crazy', $validator1);
  255. $this->comments->validator('default', $validator2);
  256. $this->users->validator('funky', $validator3);
  257. $entityValidator = new EntityValidator($this->comments);
  258. $comment->expects($this->once())
  259. ->method('validate')
  260. ->with($validator2)
  261. ->will($this->returnValue([]));
  262. $article->expects($this->once())
  263. ->method('validate')
  264. ->with($validator1)
  265. ->will($this->returnValue([]));
  266. $user->expects($this->once())
  267. ->method('validate')
  268. ->with($validator3)
  269. ->will($this->returnValue([]));
  270. $this->assertTrue($entityValidator->one($comment, [
  271. 'associated' => [
  272. 'Articles' => [
  273. 'validate' => 'crazy',
  274. 'associated' => ['Users' => ['validate' => 'funky']]
  275. ]
  276. ]
  277. ]));
  278. }
  279. /**
  280. * Test many() with successful validate
  281. *
  282. * @return void
  283. */
  284. public function testManySuccess()
  285. {
  286. $class = 'TestApp\Model\Entity\ValidatableEntity';
  287. $comment1 = $this->getMock($class, ['validate']);
  288. $comment2 = $this->getMock($class, ['validate']);
  289. $validator = $this->getMock('\Cake\Validation\Validator');
  290. $data = [$comment1, $comment2];
  291. $this->comments->validator('default', $validator);
  292. $entityValidator = new EntityValidator($this->comments);
  293. $validator->expects($this->exactly(2))
  294. ->method('count')
  295. ->will($this->returnValue(1));
  296. $comment1->expects($this->once())
  297. ->method('validate')
  298. ->with($validator)
  299. ->will($this->returnValue([]));
  300. $comment2->expects($this->once())
  301. ->method('validate')
  302. ->with($validator)
  303. ->will($this->returnValue([]));
  304. $this->assertTrue($entityValidator->many($data));
  305. }
  306. /**
  307. * Test many() with failure
  308. *
  309. * @return void
  310. */
  311. public function testManyFailure()
  312. {
  313. $class = 'TestApp\Model\Entity\ValidatableEntity';
  314. $comment1 = $this->getMock($class, ['validate']);
  315. $comment2 = $this->getMock($class, ['validate']);
  316. $validator = $this->getMock('\Cake\Validation\Validator');
  317. $data = [$comment1, $comment2];
  318. $this->comments->validator('default', $validator);
  319. $entityValidator = new EntityValidator($this->comments);
  320. $validator->expects($this->exactly(2))
  321. ->method('count')
  322. ->will($this->returnValue(1));
  323. $comment1->expects($this->once())
  324. ->method('validate')
  325. ->with($validator)
  326. ->will($this->returnValue(['some' => ['error']]));
  327. $comment2->expects($this->once())
  328. ->method('validate')
  329. ->with($validator)
  330. ->will($this->returnValue([]));
  331. $this->assertFalse($entityValidator->many($data));
  332. }
  333. }