Skip to content

Commit

Permalink
fix wildcard guarded case (#444)
Browse files Browse the repository at this point in the history
* fix wildcard guarded case

fixes #442

* fix php cs

* add unittest

it_will_store_no_changes_when_wildcard_guard_and_log_unguarded_attributes
  • Loading branch information
Gummibeer authored and freekmurze committed Oct 17, 2018
1 parent a958311 commit 2383a9d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Traits/DetectsChanges.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function attributesToBeLogged(): array
$attributes = array_merge($attributes, $this->getFillable());
}

if (isset(static::$logUnguarded) && static::$logUnguarded) {
if (isset(static::$logUnguarded) && static::$logUnguarded && ! in_array('*', $this->getGuarded())) {
$attributes = array_merge($attributes, array_diff(array_keys($this->getAttributes()), $this->getGuarded()));
}

Expand Down
18 changes: 18 additions & 0 deletions tests/DetectsChangesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,24 @@ public function it_can_use_unguarded_as_loggable_attributes()
$this->assertEquals($expectedChanges, $this->getLastActivity()->changes()->toArray());
}

/** @test */
public function it_will_store_no_changes_when_wildcard_guard_and_log_unguarded_attributes()
{
$articleClass = new class() extends Article {
protected $guarded = ['*'];
protected static $logUnguarded = true;

use LogsActivity;
};

$article = new $articleClass();
$article->name = 'my name';
$article->text = 'my new text';
$article->save();

$this->assertEquals([], $this->getLastActivity()->changes()->toArray());
}

protected function createArticle(): Article
{
$article = new $this->article();
Expand Down

0 comments on commit 2383a9d

Please sign in to comment.