Skip to content

Commit

Permalink
Merge pull request #35 from davidyell/non-callable-setters
Browse files Browse the repository at this point in the history
Fixed a bug with string setters being executed as callables
  • Loading branch information
segy authored Oct 16, 2018
2 parents 3419289 + 6318d68 commit 6ac4f41
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Model/Behavior/DuplicatableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
22 changes: 22 additions & 0 deletions tests/TestCase/Model/Behavior/DuplicatableBehaviorTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Duplicatable\Test\TestCase\Model\Behavior;

use Cake\Datasource\EntityInterface;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;

Expand Down Expand Up @@ -180,4 +181,25 @@ public function testWithTranslation()
->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);
}
}

0 comments on commit 6ac4f41

Please sign in to comment.