diff --git a/src/Model/Behavior/DuplicatableBehavior.php b/src/Model/Behavior/DuplicatableBehavior.php index 8fcdcdd..21dc089 100644 --- a/src/Model/Behavior/DuplicatableBehavior.php +++ b/src/Model/Behavior/DuplicatableBehavior.php @@ -292,7 +292,7 @@ protected function _doAction($action, EntityInterface $entity, $prop, $value = n break; case 'set': - if (is_callable($value)) { + if (!is_string($value) && is_callable($value)) { $value = $value($entity); } $entity->set($prop, $value); diff --git a/tests/TestCase/Model/Behavior/DuplicatableBehaviorTest.php b/tests/TestCase/Model/Behavior/DuplicatableBehaviorTest.php index 3db2e1c..97536a5 100644 --- a/tests/TestCase/Model/Behavior/DuplicatableBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/DuplicatableBehaviorTest.php @@ -1,6 +1,7 @@ all(); $this->assertEquals(2, $records->count()); } + + public function testDuplicateWithSetters() + { + $this->Invoices->removeBehavior('Duplicatable'); + $this->Invoices->addBehavior('Duplicatable.Duplicatable', [ + 'set' => [ + 'copied' => true, + 'name' => 'mail', + 'contact_name' => function (EntityInterface $entity) { + return strrev($entity->get('contact_name')); + } + ] + ]); + + $result = $this->Invoices->duplicate(1); + $invoice = $this->Invoices->get($result->id); + + $this->assertEquals('mail', $invoice->name); + $this->assertEquals('eman tcatnoC', $invoice->contact_name); + $this->assertEquals(1, $invoice->copied); + } }