From 7dd3ade577b2d856a81b081cf7758f68f6a4349a Mon Sep 17 00:00:00 2001 From: Etienne Bourque <50217898+EtienneBourque@users.noreply.github.com> Date: Fri, 29 Nov 2019 10:59:22 -0500 Subject: [PATCH 1/3] Fixed a regression with the preserveJoinData option --- src/Model/Behavior/DuplicatableBehavior.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Model/Behavior/DuplicatableBehavior.php b/src/Model/Behavior/DuplicatableBehavior.php index bff439c..7ef791f 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 { + } else if (!$object instanceof BelongsToMany) { // unset primary key unset($entity->{$object->getPrimaryKey()}); From 3093559fe28f57752422206792a92f963b0337e1 Mon Sep 17 00:00:00 2001 From: Etienne Bourque <50217898+EtienneBourque@users.noreply.github.com> Date: Fri, 29 Nov 2019 11:19:32 -0500 Subject: [PATCH 2/3] Changed else if for elseif --- src/Model/Behavior/DuplicatableBehavior.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model/Behavior/DuplicatableBehavior.php b/src/Model/Behavior/DuplicatableBehavior.php index 7ef791f..794aaf3 100644 --- a/src/Model/Behavior/DuplicatableBehavior.php +++ b/src/Model/Behavior/DuplicatableBehavior.php @@ -180,7 +180,7 @@ protected function _modifyEntity(EntityInterface $entity, $object) // belongs to many is tricky if ($object instanceof BelongsToMany && !$this->getConfig('preserveJoinData')) { unset($entity->_joinData); - } else if (!$object instanceof BelongsToMany) { + } elseif (!$object instanceof BelongsToMany) { // unset primary key unset($entity->{$object->getPrimaryKey()}); From 5570489a74ec9a6468b184fa4011f8556a6b4f2a Mon Sep 17 00:00:00 2001 From: Etienne Bourque <50217898+EtienneBourque@users.noreply.github.com> Date: Fri, 29 Nov 2019 15:19:32 -0500 Subject: [PATCH 3/3] Improved test cases for preserveJoinData --- .../Behavior/DuplicatableBehaviorTest.php | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) 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()); } }