Browse Source

Merge pull request #8862 from cakephp/revert-8788-bugfix

Revert "Force ExistsIn rule in new Entity even with a not dirty field"
José Lorenzo Rodríguez 9 years ago
parent
commit
eaccf14c8a
2 changed files with 11 additions and 9 deletions
  1. 1 1
      src/ORM/Rule/ExistsIn.php
  2. 10 8
      tests/TestCase/ORM/RulesCheckerIntegrationTest.php

+ 1 - 1
src/ORM/Rule/ExistsIn.php

@@ -94,7 +94,7 @@ class ExistsIn
             return true;
         }
 
-        if (!$entity->extract($this->_fields, true) && !$entity->isNew()) {
+        if (!$entity->extract($this->_fields, true)) {
             return true;
         }
 

+ 10 - 8
tests/TestCase/ORM/RulesCheckerIntegrationTest.php

@@ -15,6 +15,7 @@
 namespace Cake\Test\TestCase\ORM;
 
 use Cake\ORM\Entity;
+use Cake\ORM\RulesChecker;
 use Cake\ORM\TableRegistry;
 use Cake\TestSuite\TestCase;
 
@@ -442,7 +443,7 @@ class RulesCheckerIntegrationTest extends TestCase
     /**
      * ExistsIn uses the schema to verify that nullable fields are ok.
      *
-     * @return void
+     * @return
      */
     public function testExistsInNullValue()
     {
@@ -461,16 +462,18 @@ class RulesCheckerIntegrationTest extends TestCase
     }
 
     /**
-     * Test ExistsIn on not dirty field in new Entity
+     * 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 testExistsInNotNullValue()
+    public function testExistsInNotNullValueNewEntity()
     {
         $entity = new Entity([
             'name' => 'A Category',
         ]);
-
         $table = TableRegistry::get('Categories');
         $table->belongsTo('Categories', [
             'foreignKey' => 'parent_id',
@@ -478,15 +481,14 @@ class RulesCheckerIntegrationTest extends TestCase
         ]);
         $rules = $table->rulesChecker();
         $rules->add($rules->existsIn('parent_id', 'Categories'));
-
-        $this->assertFalse($table->save($entity));
-        $this->assertEquals(['_existsIn' => 'This value does not exist'], $entity->errors('parent_id'));
+        $this->assertTrue($table->checkRules($entity, RulesChecker::CREATE));
+        $this->assertEmpty($entity->errors('parent_id'));
     }
 
     /**
      * Tests exists in uses the bindingKey of the association
      *
-     * @return void
+     * @return
      */
     public function testExistsInWithBindingKey()
     {