| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- * @link https://cakephp.org CakePHP(tm) Project
- * @since 3.0.0
- * @license https://opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\ORM;
- use Cake\Event\Event;
- use Cake\ORM\Entity;
- use Cake\ORM\RulesChecker;
- use Cake\TestSuite\TestCase;
- /**
- * Tests the integration between the ORM and the domain checker
- */
- class RulesCheckerIntegrationTest extends TestCase
- {
- /**
- * Fixtures to be loaded
- *
- * @var array
- */
- public $fixtures = [
- 'core.Articles', 'core.ArticlesTags', 'core.Authors', 'core.Tags',
- 'core.SpecialTags', 'core.Categories', 'core.SiteArticles', 'core.SiteAuthors',
- 'core.Comments',
- ];
- /**
- * Tests saving belongsTo association and get a validation error
- *
- * @group save
- * @return void
- */
- public function testsSaveBelongsToWithValidationError()
- {
- $entity = new Entity([
- 'title' => 'A Title',
- 'body' => 'A body',
- ]);
- $entity->author = new Entity([
- 'name' => 'Jose',
- ]);
- $table = $this->getTableLocator()->get('articles');
- $table->belongsTo('authors');
- $table->getAssociation('authors')
- ->getTarget()
- ->rulesChecker()
- ->add(
- function (Entity $author, array $options) use ($table) {
- $this->assertSame($options['repository'], $table->getAssociation('authors')->getTarget());
- return false;
- },
- ['errorField' => 'name', 'message' => 'This is an error']
- );
- $this->assertFalse($table->save($entity));
- $this->assertTrue($entity->isNew());
- $this->assertTrue($entity->author->isNew());
- $this->assertNull($entity->get('author_id'));
- $this->assertNotEmpty($entity->author->getError('name'));
- $this->assertEquals(['This is an error'], $entity->author->getError('name'));
- }
- /**
- * Tests saving hasOne association and returning a validation error will
- * abort the saving process
- *
- * @group save
- * @return void
- */
- public function testSaveHasOneWithValidationError()
- {
- $entity = new Entity([
- 'name' => 'Jose',
- ]);
- $entity->article = new Entity([
- 'title' => 'A Title',
- 'body' => 'A body',
- ]);
- $table = $this->getTableLocator()->get('authors');
- $table->hasOne('articles');
- $table->getAssociation('articles')
- ->getTarget()
- ->rulesChecker()
- ->add(
- function (Entity $entity) {
- return false;
- },
- ['errorField' => 'title', 'message' => 'This is an error']
- );
- $this->assertFalse($table->save($entity));
- $this->assertTrue($entity->isNew());
- $this->assertTrue($entity->article->isNew());
- $this->assertNull($entity->article->id);
- $this->assertNull($entity->article->get('author_id'));
- $this->assertFalse($entity->article->isDirty('author_id'));
- $this->assertNotEmpty($entity->article->getError('title'));
- $this->assertSame('A Title', $entity->article->getInvalidField('title'));
- }
- /**
- * Tests saving multiple entities in a hasMany association and getting and
- * error while saving one of them. It should abort all the save operation
- * when options are set to defaults
- *
- * @return void
- */
- public function testSaveHasManyWithErrorsAtomic()
- {
- $entity = new Entity([
- 'name' => 'Jose',
- ]);
- $entity->articles = [
- new Entity([
- 'title' => '1',
- 'body' => 'A body',
- ]),
- new Entity([
- 'title' => 'Another Title',
- 'body' => 'Another body',
- ]),
- ];
- $table = $this->getTableLocator()->get('authors');
- $table->hasMany('articles');
- $table->getAssociation('articles')
- ->getTarget()
- ->rulesChecker()
- ->add(
- function (Entity $entity, $options) use ($table) {
- $this->assertSame($table, $options['_sourceTable']);
- return $entity->title === '1';
- },
- ['errorField' => 'title', 'message' => 'This is an error']
- );
- $this->assertFalse($table->save($entity));
- $this->assertTrue($entity->isNew());
- $this->assertTrue($entity->articles[0]->isNew());
- $this->assertTrue($entity->articles[1]->isNew());
- $this->assertNull($entity->articles[0]->id);
- $this->assertNull($entity->articles[1]->id);
- $this->assertNull($entity->articles[0]->author_id);
- $this->assertNull($entity->articles[1]->author_id);
- $this->assertEmpty($entity->articles[0]->getErrors());
- $this->assertNotEmpty($entity->articles[1]->getErrors());
- }
- /**
- * Tests that it is possible to continue saving hasMany associations
- * even if any of the records fail validation when atomic is set
- * to false
- *
- * @return void
- */
- public function testSaveHasManyWithErrorsNonAtomic()
- {
- $entity = new Entity([
- 'name' => 'Jose',
- ]);
- $entity->articles = [
- new Entity([
- 'title' => 'A title',
- 'body' => 'A body',
- ]),
- new Entity([
- 'title' => '1',
- 'body' => 'Another body',
- ]),
- ];
- $table = $this->getTableLocator()->get('authors');
- $table->hasMany('articles');
- $table->getAssociation('articles')
- ->getTarget()
- ->rulesChecker()
- ->add(
- function (Entity $article) {
- return is_numeric($article->title);
- },
- ['errorField' => 'title', 'message' => 'This is an error']
- );
- $result = $table->save($entity, ['atomic' => false]);
- $this->assertSame($entity, $result);
- $this->assertFalse($entity->isNew());
- $this->assertTrue($entity->articles[0]->isNew());
- $this->assertFalse($entity->articles[1]->isNew());
- $this->assertEquals(4, $entity->articles[1]->id);
- $this->assertNull($entity->articles[0]->id);
- $this->assertNotEmpty($entity->articles[0]->getError('title'));
- }
- /**
- * Tests saving belongsToMany records with a validation error in a joint entity
- *
- * @group save
- * @return void
- */
- public function testSaveBelongsToManyWithValidationErrorInJointEntity()
- {
- $entity = new Entity([
- 'title' => 'A Title',
- 'body' => 'A body',
- ]);
- $entity->tags = [
- new Entity([
- 'name' => 'Something New',
- ]),
- new Entity([
- 'name' => '100',
- ]),
- ];
- $table = $this->getTableLocator()->get('articles');
- $table->belongsToMany('tags');
- $table->getAssociation('tags')
- ->junction()
- ->rulesChecker()
- ->add(function (Entity $entity) {
- return $entity->article_id > 4;
- });
- $this->assertFalse($table->save($entity));
- $this->assertTrue($entity->isNew());
- $this->assertTrue($entity->tags[0]->isNew());
- $this->assertTrue($entity->tags[1]->isNew());
- $this->assertNull($entity->tags[0]->id);
- $this->assertNull($entity->tags[1]->id);
- $this->assertNull($entity->tags[0]->_joinData);
- $this->assertNull($entity->tags[1]->_joinData);
- }
- /**
- * Tests saving belongsToMany records with a validation error in a joint entity
- * and atomic set to false
- *
- * @group save
- * @return void
- */
- public function testSaveBelongsToManyWithValidationErrorInJointEntityNonAtomic()
- {
- $entity = new Entity([
- 'title' => 'A Title',
- 'body' => 'A body',
- ]);
- $entity->tags = [
- new Entity([
- 'name' => 'Something New',
- ]),
- new Entity([
- 'name' => 'New one',
- ]),
- ];
- $table = $this->getTableLocator()->get('articles');
- $table->belongsToMany('tags');
- $table->getAssociation('tags')
- ->junction()
- ->rulesChecker()
- ->add(function (Entity $entity) {
- return $entity->tag_id > 4;
- });
- $this->assertSame($entity, $table->save($entity, ['atomic' => false]));
- $this->assertFalse($entity->isNew());
- $this->assertFalse($entity->tags[0]->isNew());
- $this->assertFalse($entity->tags[1]->isNew());
- $this->assertEquals(4, $entity->tags[0]->id);
- $this->assertEquals(5, $entity->tags[1]->id);
- $this->assertTrue($entity->tags[0]->_joinData->isNew());
- $this->assertEquals(4, $entity->tags[1]->_joinData->article_id);
- $this->assertEquals(5, $entity->tags[1]->_joinData->tag_id);
- }
- /**
- * Test adding rule with name
- *
- * @group save
- * @return void
- */
- public function testAddingRuleWithName()
- {
- $entity = new Entity([
- 'name' => 'larry',
- ]);
- $table = $this->getTableLocator()->get('Authors');
- $rules = $table->rulesChecker();
- $rules->add(
- function () {
- return false;
- },
- 'ruleName',
- ['errorField' => 'name']
- );
- $this->assertFalse($table->save($entity));
- $this->assertEquals(['ruleName' => 'invalid'], $entity->getError('name'));
- }
- /**
- * Ensure that add(isUnique()) only invokes a rule once.
- *
- * @return void
- */
- public function testIsUniqueRuleSingleInvocation()
- {
- $entity = new Entity([
- 'name' => 'larry',
- ]);
- $table = $this->getTableLocator()->get('Authors');
- $rules = $table->rulesChecker();
- $rules->add($rules->isUnique(['name']), '_isUnique', ['errorField' => 'title']);
- $this->assertFalse($table->save($entity));
- $this->assertEquals(
- ['_isUnique' => 'This value is already in use'],
- $entity->getError('title'),
- 'Provided field should have errors'
- );
- $this->assertEmpty($entity->getError('name'), 'Errors should not apply to original field.');
- }
- /**
- * Tests the isUnique domain rule
- *
- * @group save
- * @return void
- */
- public function testIsUniqueDomainRule()
- {
- $entity = new Entity([
- 'name' => 'larry',
- ]);
- $table = $this->getTableLocator()->get('Authors');
- $rules = $table->rulesChecker();
- $rules->add($rules->isUnique(['name']));
- $this->assertFalse($table->save($entity));
- $this->assertEquals(['_isUnique' => 'This value is already in use'], $entity->getError('name'));
- $entity->name = 'jose';
- $this->assertSame($entity, $table->save($entity));
- $entity = $table->get(1);
- $entity->setDirty('name', true);
- $this->assertSame($entity, $table->save($entity));
- }
- /**
- * Tests isUnique with multiple fields
- *
- * @group save
- * @return void
- */
- public function testIsUniqueMultipleFields()
- {
- $entity = new Entity([
- 'author_id' => 1,
- 'title' => 'First Article',
- ]);
- $table = $this->getTableLocator()->get('Articles');
- $rules = $table->rulesChecker();
- $rules->add($rules->isUnique(['title', 'author_id'], 'Nope'));
- $this->assertFalse($table->save($entity));
- $this->assertEquals(['title' => ['_isUnique' => 'Nope']], $entity->getErrors());
- $entity->clean();
- $entity->author_id = 2;
- $this->assertSame($entity, $table->save($entity));
- }
- /**
- * Tests isUnique with allowMultipleNulls
- *
- * @group save
- * @return void
- */
- public function testIsUniqueAllowMultipleNulls()
- {
- $entity = new Entity([
- 'article_id' => 11,
- 'tag_id' => 11,
- 'author_id' => null,
- ]);
- $table = $this->getTableLocator()->get('SpecialTags');
- $rules = $table->rulesChecker();
- $rules->add($rules->isUnique(['author_id'], [
- 'allowMultipleNulls' => false,
- 'message' => 'All fields are required',
- ]));
- $this->assertFalse($table->save($entity));
- $this->assertEquals(['_isUnique' => 'All fields are required'], $entity->getError('author_id'));
- $entity->author_id = 11;
- $this->assertSame($entity, $table->save($entity));
- $entity = $table->get(1);
- $entity->setDirty('author_id', true);
- $this->assertSame($entity, $table->save($entity));
- }
- /**
- * Tests isUnique with multiple fields and allowMultipleNulls
- *
- * @group save
- * @return void
- */
- public function testIsUniqueMultipleFieldsAllowMultipleNulls()
- {
- $entity = new Entity([
- 'article_id' => 10,
- 'tag_id' => 12,
- 'author_id' => null,
- ]);
- $table = $this->getTableLocator()->get('SpecialTags');
- $rules = $table->rulesChecker();
- $rules->add($rules->isUnique(['author_id', 'article_id'], [
- 'allowMultipleNulls' => false,
- 'message' => 'Nope',
- ]));
- $this->assertFalse($table->save($entity));
- $this->assertEquals(['author_id' => ['_isUnique' => 'Nope']], $entity->getErrors());
- $entity->clean();
- $entity->article_id = 10;
- $entity->tag_id = 12;
- $entity->author_id = 12;
- $this->assertSame($entity, $table->save($entity));
- }
- /**
- * Tests isUnique with multiple fields emulates SQL UNIQUE keys
- *
- * @group save
- * @return void
- */
- public function testIsUniqueMultipleFieldsOneIsNull()
- {
- $entity = new Entity([
- 'author_id' => null,
- 'title' => 'First Article',
- ]);
- $table = $this->getTableLocator()->get('Articles');
- $rules = $table->rulesChecker();
- $rules->add($rules->isUnique(['title', 'author_id'], 'Nope'));
- $this->assertSame($entity, $table->save($entity));
- // Make a matching record
- $entity = new Entity([
- 'author_id' => null,
- 'title' => 'New Article',
- ]);
- $this->assertSame($entity, $table->save($entity));
- }
- /**
- * Tests the existsIn domain rule
- *
- * @group save
- * @return void
- */
- public function testExistsInDomainRule()
- {
- $entity = new Entity([
- 'title' => 'An Article',
- 'author_id' => 500,
- ]);
- $table = $this->getTableLocator()->get('Articles');
- $table->belongsTo('Authors');
- $rules = $table->rulesChecker();
- $rules->add($rules->existsIn('author_id', 'Authors'));
- $this->assertFalse($table->save($entity));
- $this->assertEquals(['_existsIn' => 'This value does not exist'], $entity->getError('author_id'));
- }
- /**
- * Ensure that add(existsIn()) only invokes a rule once.
- *
- * @return void
- */
- public function testExistsInRuleSingleInvocation()
- {
- $entity = new Entity([
- 'title' => 'larry',
- 'author_id' => 500,
- ]);
- $table = $this->getTableLocator()->get('Articles');
- $table->belongsTo('Authors');
- $rules = $table->rulesChecker();
- $rules->add($rules->existsIn('author_id', 'Authors'), '_existsIn', ['errorField' => 'other']);
- $this->assertFalse($table->save($entity));
- $this->assertEquals(
- ['_existsIn' => 'This value does not exist'],
- $entity->getError('other'),
- 'Provided field should have errors'
- );
- $this->assertEmpty($entity->getError('author_id'), 'Errors should not apply to original field.');
- }
- /**
- * Tests the existsIn domain rule when passing an object
- *
- * @group save
- * @return void
- */
- public function testExistsInDomainRuleWithObject()
- {
- $entity = new Entity([
- 'title' => 'An Article',
- 'author_id' => 500,
- ]);
- $table = $this->getTableLocator()->get('Articles');
- $rules = $table->rulesChecker();
- $rules->add($rules->existsIn('author_id', $this->getTableLocator()->get('Authors'), 'Nope'));
- $this->assertFalse($table->save($entity));
- $this->assertEquals(['_existsIn' => 'Nope'], $entity->getError('author_id'));
- }
- /**
- * ExistsIn uses the schema to verify that nullable fields are ok.
- *
- * @return
- */
- public function testExistsInNullValue()
- {
- $entity = new Entity([
- 'title' => 'An Article',
- 'author_id' => null,
- ]);
- $table = $this->getTableLocator()->get('Articles');
- $table->belongsTo('Authors');
- $rules = $table->rulesChecker();
- $rules->add($rules->existsIn('author_id', 'Authors'));
- $this->assertEquals($entity, $table->save($entity));
- $this->assertEquals([], $entity->getError('author_id'));
- }
- /**
- * Test ExistsIn on a new entity that doesn't have the field populated.
- *
- * This use case is important for saving records and their
- * associated belongsTo records in one pass.
- *
- * @return void
- */
- public function testExistsInNotNullValueNewEntity()
- {
- $entity = new Entity([
- 'name' => 'A Category',
- ]);
- $table = $this->getTableLocator()->get('Categories');
- $table->belongsTo('Categories', [
- 'foreignKey' => 'parent_id',
- 'bindingKey' => 'id',
- ]);
- $rules = $table->rulesChecker();
- $rules->add($rules->existsIn('parent_id', 'Categories'));
- $this->assertTrue($table->checkRules($entity, RulesChecker::CREATE));
- $this->assertEmpty($entity->getError('parent_id'));
- }
- /**
- * Tests exists in uses the bindingKey of the association
- *
- * @return
- */
- public function testExistsInWithBindingKey()
- {
- $entity = new Entity([
- 'title' => 'An Article',
- ]);
- $table = $this->getTableLocator()->get('Articles');
- $table->belongsTo('Authors', [
- 'bindingKey' => 'name',
- 'foreignKey' => 'title',
- ]);
- $rules = $table->rulesChecker();
- $rules->add($rules->existsIn('title', 'Authors'));
- $this->assertFalse($table->save($entity));
- $this->assertNotEmpty($entity->getError('title'));
- $entity->clean();
- $entity->title = 'larry';
- $this->assertEquals($entity, $table->save($entity));
- }
- /**
- * Tests existsIn with invalid associations
- *
- * @group save
- * @return void
- */
- public function testExistsInInvalidAssociation()
- {
- $this->expectException(\RuntimeException::class);
- $this->expectExceptionMessage('ExistsIn rule for \'author_id\' is invalid. \'NotValid\' is not associated with \'Cake\ORM\Table\'.');
- $entity = new Entity([
- 'title' => 'An Article',
- 'author_id' => 500,
- ]);
- $table = $this->getTableLocator()->get('Articles');
- $table->belongsTo('Authors');
- $rules = $table->rulesChecker();
- $rules->add($rules->existsIn('author_id', 'NotValid'));
- $table->save($entity);
- }
- /**
- * Tests existsIn does not prevent new entities from saving if parent entity is new
- *
- * @return void
- */
- public function testExistsInHasManyNewEntities()
- {
- $table = $this->getTableLocator()->get('Articles');
- $table->hasMany('Comments');
- $table->Comments->belongsTo('Articles');
- $rules = $table->Comments->rulesChecker();
- $rules->add($rules->existsIn(['article_id'], $table));
- $article = $table->newEntity([
- 'title' => 'new article',
- 'comments' => [
- $table->Comments->newEntity([
- 'user_id' => 1,
- 'comment' => 'comment 1',
- ]),
- $table->Comments->newEntity([
- 'user_id' => 1,
- 'comment' => 'comment 2',
- ]),
- ],
- ]);
- $this->assertNotFalse($table->save($article));
- }
- /**
- * Tests existsIn does not prevent new entities from saving if parent entity is new,
- * getting the parent entity from the association
- *
- * @return void
- */
- public function testExistsInHasManyNewEntitiesViaAssociation()
- {
- $table = $this->getTableLocator()->get('Articles');
- $table->hasMany('Comments');
- $table->Comments->belongsTo('Articles');
- $rules = $table->Comments->rulesChecker();
- $rules->add($rules->existsIn(['article_id'], 'Articles'));
- $article = $table->newEntity([
- 'title' => 'test',
- ]);
- $article->comments = [
- $table->Comments->newEntity([
- 'user_id' => 1,
- 'comment' => 'test',
- ]),
- ];
- $this->assertNotFalse($table->save($article));
- }
- /**
- * Tests the checkRules save option
- *
- * @group save
- * @return void
- */
- public function testSkipRulesChecking()
- {
- $entity = new Entity([
- 'title' => 'An Article',
- 'author_id' => 500,
- ]);
- $table = $this->getTableLocator()->get('Articles');
- $rules = $table->rulesChecker();
- $rules->add($rules->existsIn('author_id', $this->getTableLocator()->get('Authors'), 'Nope'));
- $this->assertSame($entity, $table->save($entity, ['checkRules' => false]));
- }
- /**
- * Tests the beforeRules event
- *
- * @group save
- * @return void
- */
- public function testUseBeforeRules()
- {
- $entity = new Entity([
- 'title' => 'An Article',
- 'author_id' => 500,
- ]);
- $table = $this->getTableLocator()->get('Articles');
- $rules = $table->rulesChecker();
- $rules->add($rules->existsIn('author_id', $this->getTableLocator()->get('Authors'), 'Nope'));
- $table->getEventManager()->on(
- 'Model.beforeRules',
- function (Event $event, Entity $entity, \ArrayObject $options, $operation) {
- $this->assertEquals(
- [
- 'atomic' => true,
- 'associated' => true,
- 'checkRules' => true,
- 'checkExisting' => true,
- '_primary' => true,
- ],
- $options->getArrayCopy()
- );
- $this->assertEquals('create', $operation);
- $event->stopPropagation();
- return true;
- }
- );
- $this->assertSame($entity, $table->save($entity));
- }
- /**
- * Tests the afterRules event
- *
- * @group save
- * @return void
- */
- public function testUseAfterRules()
- {
- $entity = new Entity([
- 'title' => 'An Article',
- 'author_id' => 500,
- ]);
- $table = $this->getTableLocator()->get('Articles');
- $rules = $table->rulesChecker();
- $rules->add($rules->existsIn('author_id', $this->getTableLocator()->get('Authors'), 'Nope'));
- $table->getEventManager()->on(
- 'Model.afterRules',
- function (Event $event, Entity $entity, \ArrayObject $options, $result, $operation) {
- $this->assertEquals(
- [
- 'atomic' => true,
- 'associated' => true,
- 'checkRules' => true,
- 'checkExisting' => true,
- '_primary' => true,
- ],
- $options->getArrayCopy()
- );
- $this->assertEquals('create', $operation);
- $this->assertFalse($result);
- $event->stopPropagation();
- return true;
- }
- );
- $this->assertSame($entity, $table->save($entity));
- }
- /**
- * Tests that rules can be changed using the buildRules event
- *
- * @group save
- * @return void
- */
- public function testUseBuildRulesEvent()
- {
- $entity = new Entity([
- 'title' => 'An Article',
- 'author_id' => 500,
- ]);
- $table = $this->getTableLocator()->get('Articles');
- $table->getEventManager()->on('Model.buildRules', function (Event $event, RulesChecker $rules) {
- $rules->add($rules->existsIn('author_id', $this->getTableLocator()->get('Authors'), 'Nope'));
- });
- $this->assertFalse($table->save($entity));
- }
- /**
- * Tests isUnique with untouched fields
- *
- * @group save
- * @return void
- */
- public function testIsUniqueWithCleanFields()
- {
- $table = $this->getTableLocator()->get('Articles');
- $entity = $table->get(1);
- $rules = $table->rulesChecker();
- $rules->add($rules->isUnique(['title', 'author_id'], 'Nope'));
- $entity->body = 'Foo';
- $this->assertSame($entity, $table->save($entity));
- $entity->title = 'Third Article';
- $this->assertFalse($table->save($entity));
- }
- /**
- * Tests isUnique rule with conflicting columns
- *
- * @group save
- * @return void
- */
- public function testIsUniqueAliasPrefix()
- {
- $entity = new Entity([
- 'title' => 'An Article',
- 'author_id' => 1,
- ]);
- $table = $this->getTableLocator()->get('Articles');
- $table->belongsTo('Authors');
- $rules = $table->rulesChecker();
- $rules->add($rules->isUnique(['author_id']));
- $table->Authors->getEventManager()->on('Model.beforeFind', function (Event $event, $query) {
- $query->leftJoin(['a2' => 'authors']);
- });
- $this->assertFalse($table->save($entity));
- $this->assertEquals(['_isUnique' => 'This value is already in use'], $entity->getError('author_id'));
- }
- /**
- * Tests the existsIn rule when passing non dirty fields
- *
- * @group save
- * @return void
- */
- public function testExistsInWithCleanFields()
- {
- $table = $this->getTableLocator()->get('Articles');
- $table->belongsTo('Authors');
- $rules = $table->rulesChecker();
- $rules->add($rules->existsIn('author_id', 'Authors'));
- $entity = $table->get(1);
- $entity->title = 'Foo';
- $entity->author_id = 1000;
- $entity->setDirty('author_id', false);
- $this->assertSame($entity, $table->save($entity));
- }
- /**
- * Tests the existsIn with conflicting columns
- *
- * @group save
- * @return void
- */
- public function testExistsInAliasPrefix()
- {
- $entity = new Entity([
- 'title' => 'An Article',
- 'author_id' => 500,
- ]);
- $table = $this->getTableLocator()->get('Articles');
- $table->belongsTo('Authors');
- $rules = $table->rulesChecker();
- $rules->add($rules->existsIn('author_id', 'Authors'));
- $table->Authors->getEventManager()->on('Model.beforeFind', function (Event $event, $query) {
- $query->leftJoin(['a2' => 'authors']);
- });
- $this->assertFalse($table->save($entity));
- $this->assertEquals(['_existsIn' => 'This value does not exist'], $entity->getError('author_id'));
- }
- /**
- * Tests that using an array in existsIn() sets the error message correctly
- *
- * @return void
- */
- public function testExistsInErrorWithArrayField()
- {
- $entity = new Entity([
- 'title' => 'An Article',
- 'author_id' => 500,
- ]);
- $table = $this->getTableLocator()->get('Articles');
- $table->belongsTo('Authors');
- $rules = $table->rulesChecker();
- $rules->add($rules->existsIn(['author_id'], 'Authors'));
- $this->assertFalse($table->save($entity));
- $this->assertEquals(['_existsIn' => 'This value does not exist'], $entity->getError('author_id'));
- }
- /**
- * Tests new allowNullableNulls flag with author id set to null
- *
- * @return void
- */
- public function testExistsInAllowNullableNullsOn()
- {
- $entity = new Entity([
- 'id' => 10,
- 'author_id' => null,
- 'site_id' => 1,
- 'name' => 'New Site Article without Author',
- ]);
- $table = $this->getTableLocator()->get('SiteArticles');
- $table->belongsTo('SiteAuthors');
- $rules = $table->rulesChecker();
- $rules->add($rules->existsIn(['author_id', 'site_id'], 'SiteAuthors', [
- 'allowNullableNulls' => true,
- ]));
- $this->assertInstanceOf('Cake\ORM\Entity', $table->save($entity));
- }
- /**
- * Tests new allowNullableNulls flag with author id set to null
- *
- * @return void
- */
- public function testExistsInAllowNullableNullsOff()
- {
- $entity = new Entity([
- 'id' => 10,
- 'author_id' => null,
- 'site_id' => 1,
- 'name' => 'New Site Article without Author',
- ]);
- $table = $this->getTableLocator()->get('SiteArticles');
- $table->belongsTo('SiteAuthors');
- $rules = $table->rulesChecker();
- $rules->add($rules->existsIn(['author_id', 'site_id'], 'SiteAuthors', [
- 'allowNullableNulls' => false,
- ]));
- $this->assertFalse($table->save($entity));
- }
- /**
- * Tests new allowNullableNulls flag with author id set to null
- *
- * @return
- */
- public function testExistsInAllowNullableNullsDefaultValue()
- {
- $entity = new Entity([
- 'id' => 10,
- 'author_id' => null,
- 'site_id' => 1,
- 'name' => 'New Site Article without Author',
- ]);
- $table = $this->getTableLocator()->get('SiteArticles');
- $table->belongsTo('SiteAuthors');
- $rules = $table->rulesChecker();
- $rules->add($rules->existsIn(['author_id', 'site_id'], 'SiteAuthors'));
- $this->assertFalse($table->save($entity));
- }
- /**
- * Tests new allowNullableNulls flag with author id set to null
- *
- * @return
- */
- public function testExistsInAllowNullableNullsCustomMessage()
- {
- $entity = new Entity([
- 'id' => 10,
- 'author_id' => null,
- 'site_id' => 1,
- 'name' => 'New Site Article without Author',
- ]);
- $table = $this->getTableLocator()->get('SiteArticles');
- $table->belongsTo('SiteAuthors');
- $rules = $table->rulesChecker();
- $rules->add($rules->existsIn(['author_id', 'site_id'], 'SiteAuthors', [
- 'allowNullableNulls' => false,
- 'message' => 'Niente',
- ]));
- $this->assertFalse($table->save($entity));
- $this->assertEquals(['author_id' => ['_existsIn' => 'Niente']], $entity->getErrors());
- }
- /**
- * Tests new allowNullableNulls flag with author id set to 1
- *
- * @return
- */
- public function testExistsInAllowNullableNullsOnAllKeysSet()
- {
- $entity = new Entity([
- 'id' => 10,
- 'author_id' => 1,
- 'site_id' => 1,
- 'name' => 'New Site Article with Author',
- ]);
- $table = $this->getTableLocator()->get('SiteArticles');
- $table->belongsTo('SiteAuthors');
- $rules = $table->rulesChecker();
- $rules->add($rules->existsIn(['author_id', 'site_id'], 'SiteAuthors', ['allowNullableNulls' => true]));
- $this->assertInstanceOf('Cake\ORM\Entity', $table->save($entity));
- }
- /**
- * Tests new allowNullableNulls flag with author id set to 1
- *
- * @return
- */
- public function testExistsInAllowNullableNullsOffAllKeysSet()
- {
- $entity = new Entity([
- 'id' => 10,
- 'author_id' => 1,
- 'site_id' => 1,
- 'name' => 'New Site Article with Author',
- ]);
- $table = $this->getTableLocator()->get('SiteArticles');
- $table->belongsTo('SiteAuthors');
- $rules = $table->rulesChecker();
- $rules->add($rules->existsIn(['author_id', 'site_id'], 'SiteAuthors', ['allowNullableNulls' => false]));
- $this->assertInstanceOf('Cake\ORM\Entity', $table->save($entity));
- }
- /**
- * Tests new allowNullableNulls flag with author id set to 1
- *
- * @return
- */
- public function testExistsInAllowNullableNullsOnAllKeysCustomMessage()
- {
- $entity = new Entity([
- 'id' => 10,
- 'author_id' => 1,
- 'site_id' => 1,
- 'name' => 'New Site Article with Author',
- ]);
- $table = $this->getTableLocator()->get('SiteArticles');
- $table->belongsTo('SiteAuthors');
- $rules = $table->rulesChecker();
- $rules->add($rules->existsIn(['author_id', 'site_id'], 'SiteAuthors', [
- 'allowNullableNulls' => true,
- 'message' => 'will not error']));
- $this->assertInstanceOf('Cake\ORM\Entity', $table->save($entity));
- }
- /**
- * Tests new allowNullableNulls flag with author id set to 99999999 (does not exist)
- *
- * @return
- */
- public function testExistsInAllowNullableNullsOnInvalidKey()
- {
- $entity = new Entity([
- 'id' => 10,
- 'author_id' => 99999999,
- 'site_id' => 1,
- 'name' => 'New Site Article with Author',
- ]);
- $table = $this->getTableLocator()->get('SiteArticles');
- $table->belongsTo('SiteAuthors');
- $rules = $table->rulesChecker();
- $rules->add($rules->existsIn(['author_id', 'site_id'], 'SiteAuthors', [
- 'allowNullableNulls' => true,
- 'message' => 'will error']));
- $this->assertFalse($table->save($entity));
- $this->assertEquals(['author_id' => ['_existsIn' => 'will error']], $entity->getErrors());
- }
- /**
- * Tests new allowNullableNulls flag with author id set to 99999999 (does not exist)
- * and site_id set to 99999999 (does not exist)
- *
- * @return
- */
- public function testExistsInAllowNullableNullsOnInvalidKeys()
- {
- $entity = new Entity([
- 'id' => 10,
- 'author_id' => 99999999,
- 'site_id' => 99999999,
- 'name' => 'New Site Article with Author',
- ]);
- $table = $this->getTableLocator()->get('SiteArticles');
- $table->belongsTo('SiteAuthors');
- $rules = $table->rulesChecker();
- $rules->add($rules->existsIn(['author_id', 'site_id'], 'SiteAuthors', [
- 'allowNullableNulls' => true,
- 'message' => 'will error']));
- $this->assertFalse($table->save($entity));
- $this->assertEquals(['author_id' => ['_existsIn' => 'will error']], $entity->getErrors());
- }
- /**
- * Tests new allowNullableNulls flag with author id set to 1 (does exist)
- * and site_id set to 99999999 (does not exist)
- *
- * @return
- */
- public function testExistsInAllowNullableNullsOnInvalidKeySecond()
- {
- $entity = new Entity([
- 'id' => 10,
- 'author_id' => 1,
- 'site_id' => 99999999,
- 'name' => 'New Site Article with Author',
- ]);
- $table = $this->getTableLocator()->get('SiteArticles');
- $table->belongsTo('SiteAuthors');
- $rules = $table->rulesChecker();
- $rules->add($rules->existsIn(['author_id', 'site_id'], 'SiteAuthors', [
- 'allowNullableNulls' => true,
- 'message' => 'will error']));
- $this->assertFalse($table->save($entity));
- $this->assertEquals(['author_id' => ['_existsIn' => 'will error']], $entity->getErrors());
- }
- /**
- * Tests new allowNullableNulls with saveMany
- *
- * @return
- */
- public function testExistsInAllowNullableNullsSaveMany()
- {
- $entities = [
- new Entity([
- 'id' => 1,
- 'author_id' => null,
- 'site_id' => 1,
- 'name' => 'New Site Article without Author',
- ]),
- new Entity([
- 'id' => 2,
- 'author_id' => 1,
- 'site_id' => 1,
- 'name' => 'New Site Article with Author',
- ]),
- ];
- $table = $this->getTableLocator()->get('SiteArticles');
- $table->belongsTo('SiteAuthors');
- $rules = $table->rulesChecker();
- $rules->add($rules->existsIn(['author_id', 'site_id'], 'SiteAuthors', [
- 'allowNullableNulls' => true,
- 'message' => 'will error with array_combine warning']));
- $result = $table->saveMany($entities);
- $this->assertCount(2, $result);
- $this->assertInstanceOf(Entity::class, $result[0]);
- $this->assertEmpty($result[0]->getErrors());
- $this->assertInstanceOf(Entity::class, $result[1]);
- $this->assertEmpty($result[1]->getErrors());
- }
- /**
- * Tests using rules to prevent delete operations
- *
- * @group delete
- * @return void
- */
- public function testDeleteRules()
- {
- $table = $this->getTableLocator()->get('Articles');
- $rules = $table->rulesChecker();
- $rules->addDelete(function ($entity) {
- return false;
- });
- $entity = $table->get(1);
- $this->assertFalse($table->delete($entity));
- }
- /**
- * Checks that it is possible to pass custom options to rules when saving
- *
- * @group save
- * @return void
- */
- public function testCustomOptionsPassingSave()
- {
- $entity = new Entity([
- 'name' => 'jose',
- ]);
- $table = $this->getTableLocator()->get('Authors');
- $rules = $table->rulesChecker();
- $rules->add(function ($entity, $options) {
- $this->assertEquals('bar', $options['foo']);
- $this->assertEquals('option', $options['another']);
- return false;
- }, ['another' => 'option']);
- $this->assertFalse($table->save($entity, ['foo' => 'bar']));
- }
- /**
- * Tests passing custom options to rules from delete
- *
- * @group delete
- * @return void
- */
- public function testCustomOptionsPassingDelete()
- {
- $table = $this->getTableLocator()->get('Articles');
- $rules = $table->rulesChecker();
- $rules->addDelete(function ($entity, $options) {
- $this->assertEquals('bar', $options['foo']);
- $this->assertEquals('option', $options['another']);
- return false;
- }, ['another' => 'option']);
- $entity = $table->get(1);
- $this->assertFalse($table->delete($entity, ['foo' => 'bar']));
- }
- /**
- * Test adding rules that return error string
- *
- * @group save
- * @return void
- */
- public function testCustomErrorMessageFromRule()
- {
- $entity = new Entity([
- 'name' => 'larry',
- ]);
- $table = $this->getTableLocator()->get('Authors');
- $rules = $table->rulesChecker();
- $rules->add(function () {
- return 'So much nope';
- }, ['errorField' => 'name']);
- $this->assertFalse($table->save($entity));
- $this->assertEquals(['So much nope'], $entity->getError('name'));
- }
- /**
- * Test adding rules with no errorField do not accept strings
- *
- * @group save
- * @return void
- */
- public function testCustomErrorMessageFromRuleNoErrorField()
- {
- $entity = new Entity([
- 'name' => 'larry',
- ]);
- $table = $this->getTableLocator()->get('Authors');
- $rules = $table->rulesChecker();
- $rules->add(function () {
- return 'So much nope';
- });
- $this->assertFalse($table->save($entity));
- $this->assertEmpty($entity->getErrors());
- }
- /**
- * Tests that using existsIn for a hasMany association will not be called
- * as the foreign key for the association was automatically validated already.
- *
- * @group save
- * @return void
- */
- public function testAvoidExistsInOnAutomaticSaving()
- {
- $entity = new Entity([
- 'name' => 'Jose',
- ]);
- $entity->articles = [
- new Entity([
- 'title' => '1',
- 'body' => 'A body',
- ]),
- new Entity([
- 'title' => 'Another Title',
- 'body' => 'Another body',
- ]),
- ];
- $table = $this->getTableLocator()->get('authors');
- $table->hasMany('articles');
- $table->getAssociation('articles')->belongsTo('authors');
- $checker = $table->getAssociation('articles')->getTarget()->rulesChecker();
- $checker->add(function ($entity, $options) use ($checker) {
- $rule = $checker->existsIn('author_id', 'authors');
- $id = $entity->author_id;
- $entity->author_id = 5000;
- $result = $rule($entity, $options);
- $this->assertTrue($result);
- $entity->author_id = $id;
- return true;
- });
- $this->assertSame($entity, $table->save($entity));
- }
- /**
- * Tests the existsIn domain rule respects the conditions set for the associations
- *
- * @group save
- * @return void
- */
- public function testExistsInDomainRuleWithAssociationConditions()
- {
- $entity = new Entity([
- 'title' => 'An Article',
- 'author_id' => 1,
- ]);
- $table = $this->getTableLocator()->get('Articles');
- $table->belongsTo('Authors', [
- 'conditions' => ['Authors.name !=' => 'mariano'],
- ]);
- $rules = $table->rulesChecker();
- $rules->add($rules->existsIn('author_id', 'Authors'));
- $this->assertFalse($table->save($entity));
- $this->assertEquals(['_existsIn' => 'This value does not exist'], $entity->getError('author_id'));
- }
- /**
- * Tests that associated items have a count of X.
- *
- * @return void
- */
- public function testCountOfAssociatedItems()
- {
- $entity = new Entity([
- 'title' => 'A Title',
- 'body' => 'A body',
- ]);
- $entity->tags = [
- new Entity([
- 'name' => 'Something New',
- ]),
- new Entity([
- 'name' => '100',
- ]),
- ];
- $this->getTableLocator()->get('ArticlesTags');
- $table = $this->getTableLocator()->get('articles');
- $table->belongsToMany('tags');
- $rules = $table->rulesChecker();
- $rules->add($rules->validCount('tags', 3));
- $this->assertFalse($table->save($entity));
- $this->assertEquals($entity->getErrors(), [
- 'tags' => [
- '_validCount' => 'The count does not match >3',
- ],
- ]);
- // Testing that undesired types fail
- $entity->tags = null;
- $this->assertFalse($table->save($entity));
- $entity->tags = new \stdClass();
- $this->assertFalse($table->save($entity));
- $entity->tags = 'string';
- $this->assertFalse($table->save($entity));
- $entity->tags = 123456;
- $this->assertFalse($table->save($entity));
- $entity->tags = 0.512;
- $this->assertFalse($table->save($entity));
- }
- }
|