Skip to content

Commit

Permalink
[5.3] Prevent calling Model.php methods when calling them as attribut…
Browse files Browse the repository at this point in the history
…es (#15438)

* Prevent calling Model.php methods when calling them as attributes

* styleci

*    more tests
  • Loading branch information
themsaid authored and taylorotwell committed Sep 16, 2016
1 parent b63b52e commit 7ce97be
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2600,6 +2600,10 @@ public function getAttribute($key)
return $this->getAttributeValue($key);
}

if (method_exists(self::class, $key)) {
return;
}

return $this->getRelationValue($key);
}

Expand Down
21 changes: 21 additions & 0 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,27 @@ public function testIssetBehavesCorrectlyWithAttributesAndRelationships()
$this->assertTrue(isset($model->some_relation));
}

public function testNonExistingAttributeWithInternalMethodNameDoesntCallMethod()
{
$model = m::mock('EloquentModelStub[delete,getRelationValue]');
$model->name = 'Spark';
$model->shouldNotReceive('delete');
$model->shouldReceive('getRelationValue')->once()->with('belongsToStub')->andReturn('relation');

// Can return a normal relation
$this->assertEquals('relation', $model->belongsToStub);

// Can return a normal attribute
$this->assertEquals('Spark', $model->name);

// Returns null for a Model.php method name
$this->assertNull($model->delete);

$model = m::mock('EloquentModelStub[delete]');
$model->delete = 123;
$this->assertEquals(123, $model->delete);
}

public function testIntKeyTypePreserved()
{
$model = $this->getMockBuilder('EloquentModelStub')->setMethods(['newQueryWithoutScopes', 'updateTimestamps', 'refresh'])->getMock();
Expand Down

0 comments on commit 7ce97be

Please sign in to comment.