From e55b8add8d26c268769672f692e16d41117487f7 Mon Sep 17 00:00:00 2001 From: David Yell Date: Mon, 15 Oct 2018 12:57:05 +0100 Subject: [PATCH 1/3] Fixed a bug with string setters being executed as callables --- src/Model/Behavior/DuplicatableBehavior.php | 2 +- .../Behavior/DuplicatableBehaviorTest.php | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Model/Behavior/DuplicatableBehavior.php b/src/Model/Behavior/DuplicatableBehavior.php index 8fcdcdd..521dafc 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_callable($value) && !is_string($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..a7dc899 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' => 'Example Invoice - copy', + 'contact_name' => function (EntityInterface $entity) { + return strrev($entity->get('contact_name')); + } + ] + ]); + + $result = $this->Invoices->duplicate(1); + $invoice = $this->Invoices->get($result->id); + + $this->assertEquals('Example Invoice - copy', $invoice->name); + $this->assertEquals('eman tcatnoC', $invoice->contact_name); + $this->assertEquals(1, $invoice->copied); + } } From 56786c7bd52995370fb0c83bda7fa0de2d958cbd Mon Sep 17 00:00:00 2001 From: David Yell Date: Tue, 16 Oct 2018 08:48:09 +0100 Subject: [PATCH 2/3] Updated test to correctly reflect the fixed regression --- tests/TestCase/Model/Behavior/DuplicatableBehaviorTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/TestCase/Model/Behavior/DuplicatableBehaviorTest.php b/tests/TestCase/Model/Behavior/DuplicatableBehaviorTest.php index a7dc899..97536a5 100644 --- a/tests/TestCase/Model/Behavior/DuplicatableBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/DuplicatableBehaviorTest.php @@ -188,7 +188,7 @@ public function testDuplicateWithSetters() $this->Invoices->addBehavior('Duplicatable.Duplicatable', [ 'set' => [ 'copied' => true, - 'name' => 'Example Invoice - copy', + 'name' => 'mail', 'contact_name' => function (EntityInterface $entity) { return strrev($entity->get('contact_name')); } @@ -198,7 +198,7 @@ public function testDuplicateWithSetters() $result = $this->Invoices->duplicate(1); $invoice = $this->Invoices->get($result->id); - $this->assertEquals('Example Invoice - copy', $invoice->name); + $this->assertEquals('mail', $invoice->name); $this->assertEquals('eman tcatnoC', $invoice->contact_name); $this->assertEquals(1, $invoice->copied); } From 6318d68005dfbb892af86b3a2af9101f9df1f277 Mon Sep 17 00:00:00 2001 From: David Yell Date: Tue, 16 Oct 2018 11:42:37 +0100 Subject: [PATCH 3/3] Check for a string first to pass the condition quicker --- 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 521dafc..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) && !is_string($value)) { + if (!is_string($value) && is_callable($value)) { $value = $value($entity); } $entity->set($prop, $value);