Skip to content

Commit

Permalink
Merge pull request #143 from celearning/master
Browse files Browse the repository at this point in the history
Correct renaming issue in TagService::renameTags()
  • Loading branch information
cviebrock committed Jan 11, 2024
2 parents b9b1fc2 + 3679819 commit 8b5007f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Services/TagService.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ public function renameTags(string $oldName, string $newName, $class = null): int

return $pivot
->where($relatedKeyName, $oldTag->getKey())
->where($relatedMorphType, get_class($class))
->where($relatedMorphType, $class->getMorphClass())
->update([
$relatedKeyName => $newTag->getKey(),
]);
Expand Down
26 changes: 26 additions & 0 deletions tests/TagServiceTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,32 @@ public function testRenamingTag(): void
);
}

/**
* Test renaming a tag with a custom morph class key.
*/
public function testRenamingTagWithCustomMorphClass(): void
{
// Create a model with a custom morph class key
$morphModel = new class extends TestModel {
protected $attributes = [
'title' => 'testing morph model'
];
public function getMorphClass()
{
return 'test-morph-model'; // can any custom key that is different from the class name
}
};
$morphModel->save();

$morphModel->tag('Apple');
$morphModel->tag('Banana');
$morphModel->tag('Cherry');

// Rename the tags the morphModel class should have exactly 1 update
$count = $this->service->renameTags('Apple', 'Apricot', $morphModel);
$this->assertEquals(1, $count);
}

/**
* Test renaming a tag across all models.
*/
Expand Down

0 comments on commit 8b5007f

Please sign in to comment.