Skip to content

Commit 2d3366a

Browse files
authored
Merge pull request #46 from UseMuffin/on-dirty-with-update-not-dirty
Fix non-dirty, non-empty slug value being incorrectly regenerated on update.
2 parents dba1c1d + 1eb054f commit 2d3366a

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

src/Model/Behavior/SlugBehavior.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,18 +225,14 @@ public function buildValidator(EventInterface $event, Validator $validator, stri
225225
*/
226226
public function beforeSave(EventInterface $event, EntityInterface $entity, ArrayObject $options)
227227
{
228-
$onUpdate = $this->getConfig('onUpdate');
229-
if (!$entity->isNew() && !$onUpdate) {
228+
$isNew = $entity->isNew();
229+
if (!$isNew && !$this->getConfig('onUpdate')) {
230230
return;
231231
}
232232

233233
$onDirty = $this->getConfig('onDirty');
234234
$field = $this->getConfig('field');
235-
if (
236-
!$onDirty
237-
&& $entity->isDirty($field)
238-
&& (!$entity->isNew() || (!empty($entity->{$field})))
239-
) {
235+
if ($onDirty && !$entity->isDirty($field) && !$isNew) {
240236
return;
241237
}
242238

tests/TestCase/Model/Behavior/SlugBehaviorTest.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
class SlugBehaviorTest extends TestCase
1111
{
12-
public $fixtures = [
12+
protected $fixtures = [
1313
'plugin.Muffin/Slug.Tags',
1414
'plugin.Muffin/Slug.Articles',
1515
'plugin.Muffin/Slug.ArticlesTags',
@@ -20,7 +20,7 @@ public function setUp(): void
2020
{
2121
parent::setUp();
2222

23-
$this->Tags = TableRegistry::get('Muffin/Slug.Tags', ['table' => 'slug_tags']);
23+
$this->Tags = $this->getTableLocator()->get('Muffin/Slug.Tags', ['table' => 'slug_tags']);
2424
$this->Tags->setDisplayField('name');
2525
$this->Tags->addBehavior('Muffin/Slug.Slug');
2626

@@ -30,7 +30,8 @@ public function setUp(): void
3030
public function tearDown(): void
3131
{
3232
parent::tearDown();
33-
TableRegistry::clear();
33+
34+
$this->getTableLocator()->clear();
3435
unset($this->Behavior, $this->Tags);
3536
}
3637

@@ -99,7 +100,7 @@ public function testBeforeSaveOnUpdate()
99100
$expected = 'baz';
100101
$this->assertEquals($expected, $result);
101102

102-
$tag->unsetProperty('name');
103+
unset($tag['name']);
103104
$tag->namespace = 'foobar';
104105
$result = $this->Tags->save($tag)->slug;
105106
$this->assertEquals($expected, $result);
@@ -147,6 +148,32 @@ public function testBeforeSaveDirtyField()
147148
$this->assertEquals($expected, $result);
148149
}
149150

151+
/**
152+
* Test with onUpdate = true, onDirty = true and slug field is not dirty and
153+
* but unrelated field is dirty.
154+
*
155+
* @return void
156+
*/
157+
public function testBeforeSaveOnUpdateTrueAndOnDirtyTrue()
158+
{
159+
$this->Tags->behaviors()->Slug->setConfig([
160+
'onUpdate' => true,
161+
'onDirty' => true,
162+
]);
163+
164+
$data = ['name' => 'foo', 'slug' => 'bar', 'counter' => 1];
165+
$tag = $this->Tags->newEntity($data);
166+
167+
$tag = $this->Tags->save($tag);
168+
$this->assertEquals('bar', $tag->slug);
169+
$this->assertSame(1, $tag->counter);
170+
171+
$tag->counter = 2;
172+
$tag = $this->Tags->save($tag);
173+
$this->assertEquals('bar', $tag->slug);
174+
$this->assertSame(2, $tag->counter);
175+
}
176+
150177
/**
151178
* Make sure no slug is generated when `displayField` is empty.
152179
*/

0 commit comments

Comments
 (0)