Skip to content

Commit

Permalink
do not map empty string in morph type to instance (#35824)
Browse files Browse the repository at this point in the history
Co-authored-by: Rhys <rhys@iseekplant.com.au>
  • Loading branch information
rhysemmerson and Rhys committed Jan 8, 2021
1 parent bd7aaa8 commit 74bb56a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
Expand Up @@ -256,7 +256,7 @@ public function morphTo($name = null, $type = null, $id = null, $ownerKey = null
// If the type value is null it is probably safe to assume we're eager loading
// the relationship. In this case we'll just pass in a dummy query where we
// need to remove any eager loads that may already be defined on a model.
return is_null($class = $this->getAttributeFromArray($type))
return is_null($class = $this->getAttributeFromArray($type)) || $class === ''
? $this->morphEagerTo($name, $type, $id, $ownerKey)
: $this->morphInstanceTo($class, $name, $type, $id, $ownerKey);
}
Expand Down
10 changes: 10 additions & 0 deletions tests/Database/DatabaseEloquentMorphToTest.php
Expand Up @@ -100,6 +100,16 @@ public function testMorphToWithZeroMorphType()
$parent->relation();
}

public function testMorphToWithEmptyStringMorphType()
{
$parent = $this->getMockBuilder(EloquentMorphToModelStub::class)->onlyMethods(['getAttributeFromArray', 'morphEagerTo', 'morphInstanceTo'])->getMock();
$parent->method('getAttributeFromArray')->with('relation_type')->willReturn('');
$parent->expects($this->once())->method('morphEagerTo');
$parent->expects($this->never())->method('morphInstanceTo');

$parent->relation();
}

public function testMorphToWithSpecifiedClassDefault()
{
$parent = new EloquentMorphToModelStub;
Expand Down

0 comments on commit 74bb56a

Please sign in to comment.