diff --git a/src/Model/Behavior/DuplicatableBehavior.php b/src/Model/Behavior/DuplicatableBehavior.php index bff439c..794aaf3 100644 --- a/src/Model/Behavior/DuplicatableBehavior.php +++ b/src/Model/Behavior/DuplicatableBehavior.php @@ -178,9 +178,9 @@ protected function _getContain() protected function _modifyEntity(EntityInterface $entity, $object) { // belongs to many is tricky - if ($object instanceof BelongsToMany && ! $this->getConfig('preserveJoinData')) { + if ($object instanceof BelongsToMany && !$this->getConfig('preserveJoinData')) { unset($entity->_joinData); - } else { + } elseif (!$object instanceof BelongsToMany) { // unset primary key unset($entity->{$object->getPrimaryKey()}); diff --git a/tests/TestCase/Model/Behavior/DuplicatableBehaviorTest.php b/tests/TestCase/Model/Behavior/DuplicatableBehaviorTest.php index 3e22a71..88b9389 100644 --- a/tests/TestCase/Model/Behavior/DuplicatableBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/DuplicatableBehaviorTest.php @@ -213,28 +213,20 @@ public function testPreserveJoinData() 'contain' => [ 'Tags', ], - 'preserveJoinData' => false, + 'preserveJoinData' => true, ]); - $duplicated = $this->Invoices->duplicateEntity(1); - - $this->assertEquals(false, isset($duplicated->tags[0]->_joinData)); - $this->assertEquals(false, isset($duplicated->tags[1]->_joinData)); - - $this->Invoices->removeBehavior('Duplicatable'); - - $this->Invoices->addBehavior('Duplicatable.Duplicatable', [ + $result = $this->Invoices->duplicate(1); + $invoice = $this->Invoices->get($result->id, [ 'contain' => [ 'Tags', - ], - 'preserveJoinData' => true, + ] ]); - $duplicated = $this->Invoices->duplicateEntity(1); + $this->assertEquals(true, $invoice->tags[0]->_joinData->is_preserved); + $this->assertEquals(true, $invoice->tags[1]->_joinData->is_preserved); - $this->assertEquals(true, isset($duplicated->tags[0]->_joinData)); - $this->assertEquals(true, isset($duplicated->tags[1]->_joinData)); - $this->assertEquals(true, $duplicated->tags[0]->_joinData->is_preserved); - $this->assertEquals(true, $duplicated->tags[1]->_joinData->is_preserved); + // check that tags are not duplicated + $this->assertEquals(2, $this->Invoices->Tags->find()->count()); } }