RulesCheckerIntegrationTest.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\ORM;
  16. use Cake\Event\Event;
  17. use Cake\ORM\Entity;
  18. use Cake\ORM\RulesChecker;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * Tests the integration between the ORM and the domain checker
  22. */
  23. class RulesCheckerIntegrationTest extends TestCase
  24. {
  25. /**
  26. * Fixtures to be loaded
  27. *
  28. * @var array
  29. */
  30. public $fixtures = [
  31. 'core.articles', 'core.articles_tags', 'core.authors', 'core.tags',
  32. 'core.special_tags', 'core.categories', 'core.site_articles', 'core.site_authors'
  33. ];
  34. /**
  35. * Tear down
  36. *
  37. * @return void
  38. */
  39. public function tearDown()
  40. {
  41. parent::tearDown();
  42. $this->getTableLocator()->clear();
  43. }
  44. /**
  45. * Tests saving belongsTo association and get a validation error
  46. *
  47. * @group save
  48. * @return void
  49. */
  50. public function testsSaveBelongsToWithValidationError()
  51. {
  52. $entity = new Entity([
  53. 'title' => 'A Title',
  54. 'body' => 'A body'
  55. ]);
  56. $entity->author = new Entity([
  57. 'name' => 'Jose'
  58. ]);
  59. $table = $this->getTableLocator()->get('articles');
  60. $table->belongsTo('authors');
  61. $table->getAssociation('authors')
  62. ->getTarget()
  63. ->rulesChecker()
  64. ->add(
  65. function (Entity $author, array $options) use ($table) {
  66. $this->assertSame($options['repository'], $table->getAssociation('authors')->getTarget());
  67. return false;
  68. },
  69. ['errorField' => 'name', 'message' => 'This is an error']
  70. );
  71. $this->assertFalse($table->save($entity));
  72. $this->assertTrue($entity->isNew());
  73. $this->assertTrue($entity->author->isNew());
  74. $this->assertNull($entity->get('author_id'));
  75. $this->assertNotEmpty($entity->author->getError('name'));
  76. $this->assertEquals(['This is an error'], $entity->author->getError('name'));
  77. }
  78. /**
  79. * Tests saving hasOne association and returning a validation error will
  80. * abort the saving process
  81. *
  82. * @group save
  83. * @return void
  84. */
  85. public function testSaveHasOneWithValidationError()
  86. {
  87. $entity = new Entity([
  88. 'name' => 'Jose'
  89. ]);
  90. $entity->article = new Entity([
  91. 'title' => 'A Title',
  92. 'body' => 'A body'
  93. ]);
  94. $table = $this->getTableLocator()->get('authors');
  95. $table->hasOne('articles');
  96. $table->getAssociation('articles')
  97. ->getTarget()
  98. ->rulesChecker()
  99. ->add(
  100. function (Entity $entity) {
  101. return false;
  102. },
  103. ['errorField' => 'title', 'message' => 'This is an error']
  104. );
  105. $this->assertFalse($table->save($entity));
  106. $this->assertTrue($entity->isNew());
  107. $this->assertTrue($entity->article->isNew());
  108. $this->assertNull($entity->article->id);
  109. $this->assertNull($entity->article->get('author_id'));
  110. $this->assertFalse($entity->article->isDirty('author_id'));
  111. $this->assertNotEmpty($entity->article->getError('title'));
  112. $this->assertSame('A Title', $entity->article->getInvalidField('title'));
  113. }
  114. /**
  115. * Tests saving multiple entities in a hasMany association and getting and
  116. * error while saving one of them. It should abort all the save operation
  117. * when options are set to defaults
  118. *
  119. * @return void
  120. */
  121. public function testSaveHasManyWithErrorsAtomic()
  122. {
  123. $entity = new Entity([
  124. 'name' => 'Jose'
  125. ]);
  126. $entity->articles = [
  127. new Entity([
  128. 'title' => '1',
  129. 'body' => 'A body'
  130. ]),
  131. new Entity([
  132. 'title' => 'Another Title',
  133. 'body' => 'Another body'
  134. ])
  135. ];
  136. $table = $this->getTableLocator()->get('authors');
  137. $table->hasMany('articles');
  138. $table->getAssociation('articles')
  139. ->getTarget()
  140. ->rulesChecker()
  141. ->add(
  142. function (Entity $entity, $options) use ($table) {
  143. $this->assertSame($table, $options['_sourceTable']);
  144. return $entity->title === '1';
  145. },
  146. ['errorField' => 'title', 'message' => 'This is an error']
  147. );
  148. $this->assertFalse($table->save($entity));
  149. $this->assertTrue($entity->isNew());
  150. $this->assertTrue($entity->articles[0]->isNew());
  151. $this->assertTrue($entity->articles[1]->isNew());
  152. $this->assertNull($entity->articles[0]->id);
  153. $this->assertNull($entity->articles[1]->id);
  154. $this->assertNull($entity->articles[0]->author_id);
  155. $this->assertNull($entity->articles[1]->author_id);
  156. $this->assertEmpty($entity->articles[0]->getErrors());
  157. $this->assertNotEmpty($entity->articles[1]->getErrors());
  158. }
  159. /**
  160. * Tests that it is possible to continue saving hasMany associations
  161. * even if any of the records fail validation when atomic is set
  162. * to false
  163. *
  164. * @return void
  165. */
  166. public function testSaveHasManyWithErrorsNonAtomic()
  167. {
  168. $entity = new Entity([
  169. 'name' => 'Jose'
  170. ]);
  171. $entity->articles = [
  172. new Entity([
  173. 'title' => 'A title',
  174. 'body' => 'A body'
  175. ]),
  176. new Entity([
  177. 'title' => '1',
  178. 'body' => 'Another body'
  179. ])
  180. ];
  181. $table = $this->getTableLocator()->get('authors');
  182. $table->hasMany('articles');
  183. $table->getAssociation('articles')
  184. ->getTarget()
  185. ->rulesChecker()
  186. ->add(
  187. function (Entity $article) {
  188. return is_numeric($article->title);
  189. },
  190. ['errorField' => 'title', 'message' => 'This is an error']
  191. );
  192. $result = $table->save($entity, ['atomic' => false]);
  193. $this->assertSame($entity, $result);
  194. $this->assertFalse($entity->isNew());
  195. $this->assertTrue($entity->articles[0]->isNew());
  196. $this->assertFalse($entity->articles[1]->isNew());
  197. $this->assertEquals(4, $entity->articles[1]->id);
  198. $this->assertNull($entity->articles[0]->id);
  199. $this->assertNotEmpty($entity->articles[0]->getError('title'));
  200. }
  201. /**
  202. * Tests saving belongsToMany records with a validation error in a joint entity
  203. *
  204. * @group save
  205. * @return void
  206. */
  207. public function testSaveBelongsToManyWithValidationErrorInJointEntity()
  208. {
  209. $entity = new Entity([
  210. 'title' => 'A Title',
  211. 'body' => 'A body'
  212. ]);
  213. $entity->tags = [
  214. new Entity([
  215. 'name' => 'Something New'
  216. ]),
  217. new Entity([
  218. 'name' => '100'
  219. ])
  220. ];
  221. $table = $this->getTableLocator()->get('articles');
  222. $table->belongsToMany('tags');
  223. $table->getAssociation('tags')
  224. ->junction()
  225. ->rulesChecker()
  226. ->add(function (Entity $entity) {
  227. return $entity->article_id > 4;
  228. });
  229. $this->assertFalse($table->save($entity));
  230. $this->assertTrue($entity->isNew());
  231. $this->assertTrue($entity->tags[0]->isNew());
  232. $this->assertTrue($entity->tags[1]->isNew());
  233. $this->assertNull($entity->tags[0]->id);
  234. $this->assertNull($entity->tags[1]->id);
  235. $this->assertNull($entity->tags[0]->_joinData);
  236. $this->assertNull($entity->tags[1]->_joinData);
  237. }
  238. /**
  239. * Tests saving belongsToMany records with a validation error in a joint entity
  240. * and atomic set to false
  241. *
  242. * @group save
  243. * @return void
  244. */
  245. public function testSaveBelongsToManyWithValidationErrorInJointEntityNonAtomic()
  246. {
  247. $entity = new Entity([
  248. 'title' => 'A Title',
  249. 'body' => 'A body'
  250. ]);
  251. $entity->tags = [
  252. new Entity([
  253. 'name' => 'Something New'
  254. ]),
  255. new Entity([
  256. 'name' => 'New one'
  257. ])
  258. ];
  259. $table = $this->getTableLocator()->get('articles');
  260. $table->belongsToMany('tags');
  261. $table->getAssociation('tags')
  262. ->junction()
  263. ->rulesChecker()
  264. ->add(function (Entity $entity) {
  265. return $entity->tag_id > 4;
  266. });
  267. $this->assertSame($entity, $table->save($entity, ['atomic' => false]));
  268. $this->assertFalse($entity->isNew());
  269. $this->assertFalse($entity->tags[0]->isNew());
  270. $this->assertFalse($entity->tags[1]->isNew());
  271. $this->assertEquals(4, $entity->tags[0]->id);
  272. $this->assertEquals(5, $entity->tags[1]->id);
  273. $this->assertTrue($entity->tags[0]->_joinData->isNew());
  274. $this->assertEquals(4, $entity->tags[1]->_joinData->article_id);
  275. $this->assertEquals(5, $entity->tags[1]->_joinData->tag_id);
  276. }
  277. /**
  278. * Test adding rule with name
  279. *
  280. * @group save
  281. * @return void
  282. */
  283. public function testAddingRuleWithName()
  284. {
  285. $entity = new Entity([
  286. 'name' => 'larry'
  287. ]);
  288. $table = $this->getTableLocator()->get('Authors');
  289. $rules = $table->rulesChecker();
  290. $rules->add(
  291. function () {
  292. return false;
  293. },
  294. 'ruleName',
  295. ['errorField' => 'name']
  296. );
  297. $this->assertFalse($table->save($entity));
  298. $this->assertEquals(['ruleName' => 'invalid'], $entity->getError('name'));
  299. }
  300. /**
  301. * Ensure that add(isUnique()) only invokes a rule once.
  302. *
  303. * @return void
  304. */
  305. public function testIsUniqueRuleSingleInvocation()
  306. {
  307. $entity = new Entity([
  308. 'name' => 'larry'
  309. ]);
  310. $table = $this->getTableLocator()->get('Authors');
  311. $rules = $table->rulesChecker();
  312. $rules->add($rules->isUnique(['name']), '_isUnique', ['errorField' => 'title']);
  313. $this->assertFalse($table->save($entity));
  314. $this->assertEquals(
  315. ['_isUnique' => 'This value is already in use'],
  316. $entity->getError('title'),
  317. 'Provided field should have errors'
  318. );
  319. $this->assertEmpty($entity->getError('name'), 'Errors should not apply to original field.');
  320. }
  321. /**
  322. * Tests the isUnique domain rule
  323. *
  324. * @group save
  325. * @return void
  326. */
  327. public function testIsUniqueDomainRule()
  328. {
  329. $entity = new Entity([
  330. 'name' => 'larry'
  331. ]);
  332. $table = $this->getTableLocator()->get('Authors');
  333. $rules = $table->rulesChecker();
  334. $rules->add($rules->isUnique(['name']));
  335. $this->assertFalse($table->save($entity));
  336. $this->assertEquals(['_isUnique' => 'This value is already in use'], $entity->getError('name'));
  337. $entity->name = 'jose';
  338. $this->assertSame($entity, $table->save($entity));
  339. $entity = $table->get(1);
  340. $entity->setDirty('name', true);
  341. $this->assertSame($entity, $table->save($entity));
  342. }
  343. /**
  344. * Tests isUnique with multiple fields
  345. *
  346. * @group save
  347. * @return void
  348. */
  349. public function testIsUniqueMultipleFields()
  350. {
  351. $entity = new Entity([
  352. 'author_id' => 1,
  353. 'title' => 'First Article'
  354. ]);
  355. $table = $this->getTableLocator()->get('Articles');
  356. $rules = $table->rulesChecker();
  357. $rules->add($rules->isUnique(['title', 'author_id'], 'Nope'));
  358. $this->assertFalse($table->save($entity));
  359. $this->assertEquals(['title' => ['_isUnique' => 'Nope']], $entity->getErrors());
  360. $entity->clean();
  361. $entity->author_id = 2;
  362. $this->assertSame($entity, $table->save($entity));
  363. }
  364. /**
  365. * Tests isUnique with allowMultipleNulls
  366. *
  367. * @group save
  368. * @return void
  369. */
  370. public function testIsUniqueAllowMultipleNulls()
  371. {
  372. $entity = new Entity([
  373. 'article_id' => 11,
  374. 'tag_id' => 11,
  375. 'author_id' => null
  376. ]);
  377. $table = $this->getTableLocator()->get('SpecialTags');
  378. $rules = $table->rulesChecker();
  379. $rules->add($rules->isUnique(['author_id'], [
  380. 'allowMultipleNulls' => false,
  381. 'message' => 'All fields are required'
  382. ]));
  383. $this->assertFalse($table->save($entity));
  384. $this->assertEquals(['_isUnique' => 'All fields are required'], $entity->getError('author_id'));
  385. $entity->author_id = 11;
  386. $this->assertSame($entity, $table->save($entity));
  387. $entity = $table->get(1);
  388. $entity->setDirty('author_id', true);
  389. $this->assertSame($entity, $table->save($entity));
  390. }
  391. /**
  392. * Tests isUnique with multiple fields and allowMultipleNulls
  393. *
  394. * @group save
  395. * @return void
  396. */
  397. public function testIsUniqueMultipleFieldsAllowMultipleNulls()
  398. {
  399. $entity = new Entity([
  400. 'article_id' => 10,
  401. 'tag_id' => 12,
  402. 'author_id' => null
  403. ]);
  404. $table = $this->getTableLocator()->get('SpecialTags');
  405. $rules = $table->rulesChecker();
  406. $rules->add($rules->isUnique(['author_id', 'article_id'], [
  407. 'allowMultipleNulls' => false,
  408. 'message' => 'Nope'
  409. ]));
  410. $this->assertFalse($table->save($entity));
  411. $this->assertEquals(['author_id' => ['_isUnique' => 'Nope']], $entity->getErrors());
  412. $entity->clean();
  413. $entity->article_id = 10;
  414. $entity->tag_id = 12;
  415. $entity->author_id = 12;
  416. $this->assertSame($entity, $table->save($entity));
  417. }
  418. /**
  419. * Tests isUnique with multiple fields emulates SQL UNIQUE keys
  420. *
  421. * @group save
  422. * @return void
  423. */
  424. public function testIsUniqueMultipleFieldsOneIsNull()
  425. {
  426. $entity = new Entity([
  427. 'author_id' => null,
  428. 'title' => 'First Article'
  429. ]);
  430. $table = $this->getTableLocator()->get('Articles');
  431. $rules = $table->rulesChecker();
  432. $rules->add($rules->isUnique(['title', 'author_id'], 'Nope'));
  433. $this->assertSame($entity, $table->save($entity));
  434. // Make a matching record
  435. $entity = new Entity([
  436. 'author_id' => null,
  437. 'title' => 'New Article'
  438. ]);
  439. $this->assertSame($entity, $table->save($entity));
  440. }
  441. /**
  442. * Tests the existsIn domain rule
  443. *
  444. * @group save
  445. * @return void
  446. */
  447. public function testExistsInDomainRule()
  448. {
  449. $entity = new Entity([
  450. 'title' => 'An Article',
  451. 'author_id' => 500
  452. ]);
  453. $table = $this->getTableLocator()->get('Articles');
  454. $table->belongsTo('Authors');
  455. $rules = $table->rulesChecker();
  456. $rules->add($rules->existsIn('author_id', 'Authors'));
  457. $this->assertFalse($table->save($entity));
  458. $this->assertEquals(['_existsIn' => 'This value does not exist'], $entity->getError('author_id'));
  459. }
  460. /**
  461. * Ensure that add(existsIn()) only invokes a rule once.
  462. *
  463. * @return void
  464. */
  465. public function testExistsInRuleSingleInvocation()
  466. {
  467. $entity = new Entity([
  468. 'title' => 'larry',
  469. 'author_id' => 500,
  470. ]);
  471. $table = $this->getTableLocator()->get('Articles');
  472. $table->belongsTo('Authors');
  473. $rules = $table->rulesChecker();
  474. $rules->add($rules->existsIn('author_id', 'Authors'), '_existsIn', ['errorField' => 'other']);
  475. $this->assertFalse($table->save($entity));
  476. $this->assertEquals(
  477. ['_existsIn' => 'This value does not exist'],
  478. $entity->getError('other'),
  479. 'Provided field should have errors'
  480. );
  481. $this->assertEmpty($entity->getError('author_id'), 'Errors should not apply to original field.');
  482. }
  483. /**
  484. * Tests the existsIn domain rule when passing an object
  485. *
  486. * @group save
  487. * @return void
  488. */
  489. public function testExistsInDomainRuleWithObject()
  490. {
  491. $entity = new Entity([
  492. 'title' => 'An Article',
  493. 'author_id' => 500
  494. ]);
  495. $table = $this->getTableLocator()->get('Articles');
  496. $rules = $table->rulesChecker();
  497. $rules->add($rules->existsIn('author_id', $this->getTableLocator()->get('Authors'), 'Nope'));
  498. $this->assertFalse($table->save($entity));
  499. $this->assertEquals(['_existsIn' => 'Nope'], $entity->getError('author_id'));
  500. }
  501. /**
  502. * ExistsIn uses the schema to verify that nullable fields are ok.
  503. *
  504. * @return
  505. */
  506. public function testExistsInNullValue()
  507. {
  508. $entity = new Entity([
  509. 'title' => 'An Article',
  510. 'author_id' => null
  511. ]);
  512. $table = $this->getTableLocator()->get('Articles');
  513. $table->belongsTo('Authors');
  514. $rules = $table->rulesChecker();
  515. $rules->add($rules->existsIn('author_id', 'Authors'));
  516. $this->assertEquals($entity, $table->save($entity));
  517. $this->assertEquals([], $entity->getError('author_id'));
  518. }
  519. /**
  520. * Test ExistsIn on a new entity that doesn't have the field populated.
  521. *
  522. * This use case is important for saving records and their
  523. * associated belongsTo records in one pass.
  524. *
  525. * @return void
  526. */
  527. public function testExistsInNotNullValueNewEntity()
  528. {
  529. $entity = new Entity([
  530. 'name' => 'A Category',
  531. ]);
  532. $table = $this->getTableLocator()->get('Categories');
  533. $table->belongsTo('Categories', [
  534. 'foreignKey' => 'parent_id',
  535. 'bindingKey' => 'id',
  536. ]);
  537. $rules = $table->rulesChecker();
  538. $rules->add($rules->existsIn('parent_id', 'Categories'));
  539. $this->assertTrue($table->checkRules($entity, RulesChecker::CREATE));
  540. $this->assertEmpty($entity->getError('parent_id'));
  541. }
  542. /**
  543. * Tests exists in uses the bindingKey of the association
  544. *
  545. * @return
  546. */
  547. public function testExistsInWithBindingKey()
  548. {
  549. $entity = new Entity([
  550. 'title' => 'An Article',
  551. ]);
  552. $table = $this->getTableLocator()->get('Articles');
  553. $table->belongsTo('Authors', [
  554. 'bindingKey' => 'name',
  555. 'foreignKey' => 'title'
  556. ]);
  557. $rules = $table->rulesChecker();
  558. $rules->add($rules->existsIn('title', 'Authors'));
  559. $this->assertFalse($table->save($entity));
  560. $this->assertNotEmpty($entity->getError('title'));
  561. $entity->clean();
  562. $entity->title = 'larry';
  563. $this->assertEquals($entity, $table->save($entity));
  564. }
  565. /**
  566. * Tests existsIn with invalid associations
  567. *
  568. * @group save
  569. * @return void
  570. */
  571. public function testExistsInInvalidAssociation()
  572. {
  573. $this->expectException(\RuntimeException::class);
  574. $this->expectExceptionMessage('ExistsIn rule for \'author_id\' is invalid. \'NotValid\' is not associated with \'Cake\ORM\Table\'.');
  575. $entity = new Entity([
  576. 'title' => 'An Article',
  577. 'author_id' => 500
  578. ]);
  579. $table = $this->getTableLocator()->get('Articles');
  580. $table->belongsTo('Authors');
  581. $rules = $table->rulesChecker();
  582. $rules->add($rules->existsIn('author_id', 'NotValid'));
  583. $table->save($entity);
  584. }
  585. /**
  586. * Tests the checkRules save option
  587. *
  588. * @group save
  589. * @return void
  590. */
  591. public function testSkipRulesChecking()
  592. {
  593. $entity = new Entity([
  594. 'title' => 'An Article',
  595. 'author_id' => 500
  596. ]);
  597. $table = $this->getTableLocator()->get('Articles');
  598. $rules = $table->rulesChecker();
  599. $rules->add($rules->existsIn('author_id', $this->getTableLocator()->get('Authors'), 'Nope'));
  600. $this->assertSame($entity, $table->save($entity, ['checkRules' => false]));
  601. }
  602. /**
  603. * Tests the beforeRules event
  604. *
  605. * @group save
  606. * @return void
  607. */
  608. public function testUseBeforeRules()
  609. {
  610. $entity = new Entity([
  611. 'title' => 'An Article',
  612. 'author_id' => 500
  613. ]);
  614. $table = $this->getTableLocator()->get('Articles');
  615. $rules = $table->rulesChecker();
  616. $rules->add($rules->existsIn('author_id', $this->getTableLocator()->get('Authors'), 'Nope'));
  617. $table->getEventManager()->on(
  618. 'Model.beforeRules',
  619. function (Event $event, Entity $entity, \ArrayObject $options, $operation) {
  620. $this->assertEquals(
  621. [
  622. 'atomic' => true,
  623. 'associated' => true,
  624. 'checkRules' => true,
  625. 'checkExisting' => true,
  626. '_primary' => true,
  627. ],
  628. $options->getArrayCopy()
  629. );
  630. $this->assertEquals('create', $operation);
  631. $event->stopPropagation();
  632. return true;
  633. }
  634. );
  635. $this->assertSame($entity, $table->save($entity));
  636. }
  637. /**
  638. * Tests the afterRules event
  639. *
  640. * @group save
  641. * @return void
  642. */
  643. public function testUseAfterRules()
  644. {
  645. $entity = new Entity([
  646. 'title' => 'An Article',
  647. 'author_id' => 500
  648. ]);
  649. $table = $this->getTableLocator()->get('Articles');
  650. $rules = $table->rulesChecker();
  651. $rules->add($rules->existsIn('author_id', $this->getTableLocator()->get('Authors'), 'Nope'));
  652. $table->getEventManager()->on(
  653. 'Model.afterRules',
  654. function (Event $event, Entity $entity, \ArrayObject $options, $result, $operation) {
  655. $this->assertEquals(
  656. [
  657. 'atomic' => true,
  658. 'associated' => true,
  659. 'checkRules' => true,
  660. 'checkExisting' => true,
  661. '_primary' => true,
  662. ],
  663. $options->getArrayCopy()
  664. );
  665. $this->assertEquals('create', $operation);
  666. $this->assertFalse($result);
  667. $event->stopPropagation();
  668. return true;
  669. }
  670. );
  671. $this->assertSame($entity, $table->save($entity));
  672. }
  673. /**
  674. * Tests that rules can be changed using the buildRules event
  675. *
  676. * @group save
  677. * @return void
  678. */
  679. public function testUseBuildRulesEvent()
  680. {
  681. $entity = new Entity([
  682. 'title' => 'An Article',
  683. 'author_id' => 500
  684. ]);
  685. $table = $this->getTableLocator()->get('Articles');
  686. $table->getEventManager()->on('Model.buildRules', function (Event $event, RulesChecker $rules) {
  687. $rules->add($rules->existsIn('author_id', $this->getTableLocator()->get('Authors'), 'Nope'));
  688. });
  689. $this->assertFalse($table->save($entity));
  690. }
  691. /**
  692. * Tests isUnique with untouched fields
  693. *
  694. * @group save
  695. * @return void
  696. */
  697. public function testIsUniqueWithCleanFields()
  698. {
  699. $table = $this->getTableLocator()->get('Articles');
  700. $entity = $table->get(1);
  701. $rules = $table->rulesChecker();
  702. $rules->add($rules->isUnique(['title', 'author_id'], 'Nope'));
  703. $entity->body = 'Foo';
  704. $this->assertSame($entity, $table->save($entity));
  705. $entity->title = 'Third Article';
  706. $this->assertFalse($table->save($entity));
  707. }
  708. /**
  709. * Tests isUnique rule with conflicting columns
  710. *
  711. * @group save
  712. * @return void
  713. */
  714. public function testIsUniqueAliasPrefix()
  715. {
  716. $entity = new Entity([
  717. 'title' => 'An Article',
  718. 'author_id' => 1
  719. ]);
  720. $table = $this->getTableLocator()->get('Articles');
  721. $table->belongsTo('Authors');
  722. $rules = $table->rulesChecker();
  723. $rules->add($rules->isUnique(['author_id']));
  724. $table->Authors->getEventManager()->on('Model.beforeFind', function (Event $event, $query) {
  725. $query->leftJoin(['a2' => 'authors']);
  726. });
  727. $this->assertFalse($table->save($entity));
  728. $this->assertEquals(['_isUnique' => 'This value is already in use'], $entity->getError('author_id'));
  729. }
  730. /**
  731. * Tests the existsIn rule when passing non dirty fields
  732. *
  733. * @group save
  734. * @return void
  735. */
  736. public function testExistsInWithCleanFields()
  737. {
  738. $table = $this->getTableLocator()->get('Articles');
  739. $table->belongsTo('Authors');
  740. $rules = $table->rulesChecker();
  741. $rules->add($rules->existsIn('author_id', 'Authors'));
  742. $entity = $table->get(1);
  743. $entity->title = 'Foo';
  744. $entity->author_id = 1000;
  745. $entity->setDirty('author_id', false);
  746. $this->assertSame($entity, $table->save($entity));
  747. }
  748. /**
  749. * Tests the existsIn with conflicting columns
  750. *
  751. * @group save
  752. * @return void
  753. */
  754. public function testExistsInAliasPrefix()
  755. {
  756. $entity = new Entity([
  757. 'title' => 'An Article',
  758. 'author_id' => 500
  759. ]);
  760. $table = $this->getTableLocator()->get('Articles');
  761. $table->belongsTo('Authors');
  762. $rules = $table->rulesChecker();
  763. $rules->add($rules->existsIn('author_id', 'Authors'));
  764. $table->Authors->getEventManager()->on('Model.beforeFind', function (Event $event, $query) {
  765. $query->leftJoin(['a2' => 'authors']);
  766. });
  767. $this->assertFalse($table->save($entity));
  768. $this->assertEquals(['_existsIn' => 'This value does not exist'], $entity->getError('author_id'));
  769. }
  770. /**
  771. * Tests that using an array in existsIn() sets the error message correctly
  772. *
  773. * @return void
  774. */
  775. public function testExistsInErrorWithArrayField()
  776. {
  777. $entity = new Entity([
  778. 'title' => 'An Article',
  779. 'author_id' => 500
  780. ]);
  781. $table = $this->getTableLocator()->get('Articles');
  782. $table->belongsTo('Authors');
  783. $rules = $table->rulesChecker();
  784. $rules->add($rules->existsIn(['author_id'], 'Authors'));
  785. $this->assertFalse($table->save($entity));
  786. $this->assertEquals(['_existsIn' => 'This value does not exist'], $entity->getError('author_id'));
  787. }
  788. /**
  789. * Tests new allowNullableNulls flag with author id set to null
  790. *
  791. * @return
  792. */
  793. public function testExistsInAllowNullableNullsWithAuthorIdNullA()
  794. {
  795. $entity = new Entity([
  796. 'id' => 10,
  797. 'author_id' => null,
  798. 'site_id' => 1,
  799. 'name' => 'New Site Article without Author',
  800. ]);
  801. $table = $this->getTableLocator()->get('SiteArticles');
  802. $table->belongsTo('SiteAuthors');
  803. $rules = $table->rulesChecker();
  804. $rules->add($rules->existsIn(['author_id', 'site_id'], 'SiteAuthors', [
  805. 'allowNullableNulls' => true
  806. ]));
  807. $this->assertInstanceOf('Cake\ORM\Entity', $table->save($entity));
  808. }
  809. /**
  810. * Tests new allowNullableNulls flag with author id set to null
  811. *
  812. * @return
  813. */
  814. public function testExistsInAllowNullableNullsWithAuthorIdNullB()
  815. {
  816. $entity = new Entity([
  817. 'id' => 10,
  818. 'author_id' => null,
  819. 'site_id' => 1,
  820. 'name' => 'New Site Article without Author',
  821. ]);
  822. $table = $this->getTableLocator()->get('SiteArticles');
  823. $table->belongsTo('SiteAuthors');
  824. $rules = $table->rulesChecker();
  825. $rules->add($rules->existsIn(['author_id', 'site_id'], 'SiteAuthors', [
  826. 'allowNullableNulls' => false
  827. ]));
  828. $this->assertFalse($table->save($entity));
  829. }
  830. /**
  831. * Tests new allowNullableNulls flag with author id set to null
  832. *
  833. * @return
  834. */
  835. public function testExistsInAllowNullableNullsWithAuthorIdNullC()
  836. {
  837. $entity = new Entity([
  838. 'id' => 10,
  839. 'author_id' => null,
  840. 'site_id' => 1,
  841. 'name' => 'New Site Article without Author',
  842. ]);
  843. $table = $this->getTableLocator()->get('SiteArticles');
  844. $table->belongsTo('SiteAuthors');
  845. $rules = $table->rulesChecker();
  846. $rules->add($rules->existsIn(['author_id', 'site_id'], 'SiteAuthors'));
  847. $this->assertFalse($table->save($entity));
  848. }
  849. /**
  850. * Tests new allowNullableNulls flag with author id set to null
  851. *
  852. * @return
  853. */
  854. public function testExistsInAllowNullableNullsWithAuthorIdNullD()
  855. {
  856. $entity = new Entity([
  857. 'id' => 10,
  858. 'author_id' => null,
  859. 'site_id' => 1,
  860. 'name' => 'New Site Article without Author',
  861. ]);
  862. $table = $this->getTableLocator()->get('SiteArticles');
  863. $table->belongsTo('SiteAuthors');
  864. $rules = $table->rulesChecker();
  865. $rules->add($rules->existsIn(['author_id', 'site_id'], 'SiteAuthors', [
  866. 'allowNullableNulls' => false,
  867. 'message' => 'Niente'
  868. ]));
  869. $this->assertFalse($table->save($entity));
  870. $this->assertEquals(['author_id' => ['_existsIn' => 'Niente']], $entity->getErrors());
  871. }
  872. /**
  873. * Tests new allowNullableNulls flag with author id set to null
  874. *
  875. * @return
  876. */
  877. public function testExistsInAllowNullableNullsWithAuthorIdNullE()
  878. {
  879. $entity = new Entity([
  880. 'id' => 10,
  881. 'author_id' => null,
  882. 'site_id' => 1,
  883. 'name' => 'New Site Article without Author',
  884. ]);
  885. $table = $this->getTableLocator()->get('SiteArticles');
  886. $table->belongsTo('SiteAuthors');
  887. $rules = $table->rulesChecker();
  888. $rules->add($rules->existsIn(['author_id', 'site_id'], 'SiteAuthors', [
  889. 'allowNullableNulls' => true,
  890. 'message' => 'Niente'
  891. ]));
  892. $this->assertInstanceOf('Cake\ORM\Entity', $table->save($entity));
  893. }
  894. /**
  895. * Tests new allowNullableNulls flag with author id set to 1
  896. *
  897. * @return
  898. */
  899. public function testExistsInAllowNullableNullsWithAuthorId1A()
  900. {
  901. $entity = new Entity([
  902. 'id' => 10,
  903. 'author_id' => 1,
  904. 'site_id' => 1,
  905. 'name' => 'New Site Article with Author',
  906. ]);
  907. $table = $this->getTableLocator()->get('SiteArticles');
  908. $table->belongsTo('SiteAuthors');
  909. $rules = $table->rulesChecker();
  910. $rules->add($rules->existsIn(['author_id', 'site_id'], 'SiteAuthors', ['allowNullableNulls' => true]));
  911. $this->assertInstanceOf('Cake\ORM\Entity', $table->save($entity));
  912. }
  913. /**
  914. * Tests new allowNullableNulls flag with author id set to 1
  915. *
  916. * @return
  917. */
  918. public function testExistsInAllowNullableNullsWithAuthorIdB()
  919. {
  920. $entity = new Entity([
  921. 'id' => 10,
  922. 'author_id' => 1,
  923. 'site_id' => 1,
  924. 'name' => 'New Site Article with Author',
  925. ]);
  926. $table = $this->getTableLocator()->get('SiteArticles');
  927. $table->belongsTo('SiteAuthors');
  928. $rules = $table->rulesChecker();
  929. $rules->add($rules->existsIn(['author_id', 'site_id'], 'SiteAuthors', ['allowNullableNulls' => false]));
  930. $this->assertInstanceOf('Cake\ORM\Entity', $table->save($entity));
  931. }
  932. /**
  933. * Tests new allowNullableNulls flag with author id set to 1
  934. *
  935. * @return
  936. */
  937. public function testExistsInAllowNullableNullsWithAuthorId1C()
  938. {
  939. $entity = new Entity([
  940. 'id' => 10,
  941. 'author_id' => 1,
  942. 'site_id' => 1,
  943. 'name' => 'New Site Article with Author',
  944. ]);
  945. $table = $this->getTableLocator()->get('SiteArticles');
  946. $table->belongsTo('SiteAuthors');
  947. $rules = $table->rulesChecker();
  948. $rules->add($rules->existsIn(['author_id', 'site_id'], 'SiteAuthors'));
  949. $this->assertInstanceOf('Cake\ORM\Entity', $table->save($entity));
  950. }
  951. /**
  952. * Tests new allowNullableNulls flag with author id set to 1
  953. *
  954. * @return
  955. */
  956. public function testExistsInAllowNullableNullsWithAuthorId1E()
  957. {
  958. $entity = new Entity([
  959. 'id' => 10,
  960. 'author_id' => 1,
  961. 'site_id' => 1,
  962. 'name' => 'New Site Article with Author',
  963. ]);
  964. $table = $this->getTableLocator()->get('SiteArticles');
  965. $table->belongsTo('SiteAuthors');
  966. $rules = $table->rulesChecker();
  967. $rules->add($rules->existsIn(['author_id', 'site_id'], 'SiteAuthors', [
  968. 'allowNullableNulls' => true,
  969. 'message' => 'will not error']));
  970. $this->assertInstanceOf('Cake\ORM\Entity', $table->save($entity));
  971. }
  972. /**
  973. * Tests new allowNullableNulls flag with author id set to 1
  974. *
  975. * @return
  976. */
  977. public function testExistsInAllowNullableNullsWithAuthorId1F()
  978. {
  979. $entity = new Entity([
  980. 'id' => 10,
  981. 'author_id' => 1,
  982. 'site_id' => 1,
  983. 'name' => 'New Site Article with Author',
  984. ]);
  985. $table = $this->getTableLocator()->get('SiteArticles');
  986. $table->belongsTo('SiteAuthors');
  987. $rules = $table->rulesChecker();
  988. $rules->add($rules->existsIn(['author_id', 'site_id'], 'SiteAuthors', [
  989. 'allowNullableNulls' => false,
  990. 'message' => 'will not error']));
  991. $this->assertInstanceOf('Cake\ORM\Entity', $table->save($entity));
  992. }
  993. /**
  994. * Tests new allowNullableNulls flag with author id set to 99999999 (does not exist)
  995. *
  996. * @return
  997. */
  998. public function testExistsInAllowNullableNullsWithAuthorId1G()
  999. {
  1000. $entity = new Entity([
  1001. 'id' => 10,
  1002. 'author_id' => 99999999,
  1003. 'site_id' => 1,
  1004. 'name' => 'New Site Article with Author',
  1005. ]);
  1006. $table = $this->getTableLocator()->get('SiteArticles');
  1007. $table->belongsTo('SiteAuthors');
  1008. $rules = $table->rulesChecker();
  1009. $rules->add($rules->existsIn(['author_id', 'site_id'], 'SiteAuthors', [
  1010. 'allowNullableNulls' => true,
  1011. 'message' => 'will error']));
  1012. $this->assertFalse($table->save($entity));
  1013. $this->assertEquals(['author_id' => ['_existsIn' => 'will error']], $entity->getErrors());
  1014. }
  1015. /**
  1016. * Tests new allowNullableNulls flag with author id set to 99999999 (does not exist)
  1017. * and site_id set to 99999999 (does not exist)
  1018. *
  1019. * @return
  1020. */
  1021. public function testExistsInAllowNullableNullsWithAuthorId1H()
  1022. {
  1023. $entity = new Entity([
  1024. 'id' => 10,
  1025. 'author_id' => 99999999,
  1026. 'site_id' => 99999999,
  1027. 'name' => 'New Site Article with Author',
  1028. ]);
  1029. $table = $this->getTableLocator()->get('SiteArticles');
  1030. $table->belongsTo('SiteAuthors');
  1031. $rules = $table->rulesChecker();
  1032. $rules->add($rules->existsIn(['author_id', 'site_id'], 'SiteAuthors', [
  1033. 'allowNullableNulls' => true,
  1034. 'message' => 'will error']));
  1035. $this->assertFalse($table->save($entity));
  1036. $this->assertEquals(['author_id' => ['_existsIn' => 'will error']], $entity->getErrors());
  1037. }
  1038. /**
  1039. * Tests new allowNullableNulls flag with author id set to 1 (does exist)
  1040. * and site_id set to 99999999 (does not exist)
  1041. *
  1042. * @return
  1043. */
  1044. public function testExistsInAllowNullableNullsWithAuthorId1I()
  1045. {
  1046. $entity = new Entity([
  1047. 'id' => 10,
  1048. 'author_id' => 1,
  1049. 'site_id' => 99999999,
  1050. 'name' => 'New Site Article with Author',
  1051. ]);
  1052. $table = $this->getTableLocator()->get('SiteArticles');
  1053. $table->belongsTo('SiteAuthors');
  1054. $rules = $table->rulesChecker();
  1055. $rules->add($rules->existsIn(['author_id', 'site_id'], 'SiteAuthors', [
  1056. 'allowNullableNulls' => true,
  1057. 'message' => 'will error']));
  1058. $this->assertFalse($table->save($entity));
  1059. $this->assertEquals(['author_id' => ['_existsIn' => 'will error']], $entity->getErrors());
  1060. }
  1061. /**
  1062. * Tests using rules to prevent delete operations
  1063. *
  1064. * @group delete
  1065. * @return void
  1066. */
  1067. public function testDeleteRules()
  1068. {
  1069. $table = $this->getTableLocator()->get('Articles');
  1070. $rules = $table->rulesChecker();
  1071. $rules->addDelete(function ($entity) {
  1072. return false;
  1073. });
  1074. $entity = $table->get(1);
  1075. $this->assertFalse($table->delete($entity));
  1076. }
  1077. /**
  1078. * Checks that it is possible to pass custom options to rules when saving
  1079. *
  1080. * @group save
  1081. * @return void
  1082. */
  1083. public function testCustomOptionsPassingSave()
  1084. {
  1085. $entity = new Entity([
  1086. 'name' => 'jose'
  1087. ]);
  1088. $table = $this->getTableLocator()->get('Authors');
  1089. $rules = $table->rulesChecker();
  1090. $rules->add(function ($entity, $options) {
  1091. $this->assertEquals('bar', $options['foo']);
  1092. $this->assertEquals('option', $options['another']);
  1093. return false;
  1094. }, ['another' => 'option']);
  1095. $this->assertFalse($table->save($entity, ['foo' => 'bar']));
  1096. }
  1097. /**
  1098. * Tests passing custom options to rules from delete
  1099. *
  1100. * @group delete
  1101. * @return void
  1102. */
  1103. public function testCustomOptionsPassingDelete()
  1104. {
  1105. $table = $this->getTableLocator()->get('Articles');
  1106. $rules = $table->rulesChecker();
  1107. $rules->addDelete(function ($entity, $options) {
  1108. $this->assertEquals('bar', $options['foo']);
  1109. $this->assertEquals('option', $options['another']);
  1110. return false;
  1111. }, ['another' => 'option']);
  1112. $entity = $table->get(1);
  1113. $this->assertFalse($table->delete($entity, ['foo' => 'bar']));
  1114. }
  1115. /**
  1116. * Test adding rules that return error string
  1117. *
  1118. * @group save
  1119. * @return void
  1120. */
  1121. public function testCustomErrorMessageFromRule()
  1122. {
  1123. $entity = new Entity([
  1124. 'name' => 'larry'
  1125. ]);
  1126. $table = $this->getTableLocator()->get('Authors');
  1127. $rules = $table->rulesChecker();
  1128. $rules->add(function () {
  1129. return 'So much nope';
  1130. }, ['errorField' => 'name']);
  1131. $this->assertFalse($table->save($entity));
  1132. $this->assertEquals(['So much nope'], $entity->getError('name'));
  1133. }
  1134. /**
  1135. * Test adding rules with no errorField do not accept strings
  1136. *
  1137. * @group save
  1138. * @return void
  1139. */
  1140. public function testCustomErrorMessageFromRuleNoErrorField()
  1141. {
  1142. $entity = new Entity([
  1143. 'name' => 'larry'
  1144. ]);
  1145. $table = $this->getTableLocator()->get('Authors');
  1146. $rules = $table->rulesChecker();
  1147. $rules->add(function () {
  1148. return 'So much nope';
  1149. });
  1150. $this->assertFalse($table->save($entity));
  1151. $this->assertEmpty($entity->getErrors());
  1152. }
  1153. /**
  1154. * Tests that using existsIn for a hasMany association will not be called
  1155. * as the foreign key for the association was automatically validated already.
  1156. *
  1157. * @group save
  1158. * @return void
  1159. */
  1160. public function testAvoidExistsInOnAutomaticSaving()
  1161. {
  1162. $entity = new Entity([
  1163. 'name' => 'Jose'
  1164. ]);
  1165. $entity->articles = [
  1166. new Entity([
  1167. 'title' => '1',
  1168. 'body' => 'A body'
  1169. ]),
  1170. new Entity([
  1171. 'title' => 'Another Title',
  1172. 'body' => 'Another body'
  1173. ])
  1174. ];
  1175. $table = $this->getTableLocator()->get('authors');
  1176. $table->hasMany('articles');
  1177. $table->getAssociation('articles')->belongsTo('authors');
  1178. $checker = $table->getAssociation('articles')->getTarget()->rulesChecker();
  1179. $checker->add(function ($entity, $options) use ($checker) {
  1180. $rule = $checker->existsIn('author_id', 'authors');
  1181. $id = $entity->author_id;
  1182. $entity->author_id = 5000;
  1183. $result = $rule($entity, $options);
  1184. $this->assertTrue($result);
  1185. $entity->author_id = $id;
  1186. return true;
  1187. });
  1188. $this->assertSame($entity, $table->save($entity));
  1189. }
  1190. /**
  1191. * Tests the existsIn domain rule respects the conditions set for the associations
  1192. *
  1193. * @group save
  1194. * @return void
  1195. */
  1196. public function testExistsInDomainRuleWithAssociationConditions()
  1197. {
  1198. $entity = new Entity([
  1199. 'title' => 'An Article',
  1200. 'author_id' => 1
  1201. ]);
  1202. $table = $this->getTableLocator()->get('Articles');
  1203. $table->belongsTo('Authors', [
  1204. 'conditions' => ['Authors.name !=' => 'mariano']
  1205. ]);
  1206. $rules = $table->rulesChecker();
  1207. $rules->add($rules->existsIn('author_id', 'Authors'));
  1208. $this->assertFalse($table->save($entity));
  1209. $this->assertEquals(['_existsIn' => 'This value does not exist'], $entity->getError('author_id'));
  1210. }
  1211. /**
  1212. * Tests that associated items have a count of X.
  1213. *
  1214. * @return void
  1215. */
  1216. public function testCountOfAssociatedItems()
  1217. {
  1218. $entity = new Entity([
  1219. 'title' => 'A Title',
  1220. 'body' => 'A body'
  1221. ]);
  1222. $entity->tags = [
  1223. new Entity([
  1224. 'name' => 'Something New'
  1225. ]),
  1226. new Entity([
  1227. 'name' => '100'
  1228. ])
  1229. ];
  1230. $this->getTableLocator()->get('ArticlesTags');
  1231. $table = $this->getTableLocator()->get('articles');
  1232. $table->belongsToMany('tags');
  1233. $rules = $table->rulesChecker();
  1234. $rules->add($rules->validCount('tags', 3));
  1235. $this->assertFalse($table->save($entity));
  1236. $this->assertEquals($entity->getErrors(), [
  1237. 'tags' => [
  1238. '_validCount' => 'The count does not match >3'
  1239. ]
  1240. ]);
  1241. // Testing that undesired types fail
  1242. $entity->tags = null;
  1243. $this->assertFalse($table->save($entity));
  1244. $entity->tags = new \stdClass();
  1245. $this->assertFalse($table->save($entity));
  1246. $entity->tags = 'string';
  1247. $this->assertFalse($table->save($entity));
  1248. $entity->tags = 123456;
  1249. $this->assertFalse($table->save($entity));
  1250. $entity->tags = 0.512;
  1251. $this->assertFalse($table->save($entity));
  1252. }
  1253. }