Permalink
Browse files

Use wildcard ‘*’ to log all attributes when saving activity log. (#259)

Stylefix

Import
  • Loading branch information...
dam1r89 authored and freekmurze committed Oct 16, 2017
1 parent c4ffea9 commit c6a0dc11ef82f2621c37160bd0fa186794060245
Showing with 116 additions and 1 deletion.
  1. +7 −1 src/Traits/DetectsChanges.php
  2. +109 −0 tests/DetectsChangesTest.php
@@ -32,7 +32,13 @@ public function attributesToBeLogged(): array
}
if (isset(static::$logAttributes)) {
$attributes = array_merge($attributes, static::$logAttributes);
if (in_array('*', static::$logAttributes)) {
$withoutWildcard = array_diff(static::$logAttributes, ['*']);
$attributes = array_merge($attributes, array_keys($this->attributes), $withoutWildcard);
} else {
$attributes = array_merge($attributes, static::$logAttributes);
}
}
return $attributes;
@@ -2,6 +2,7 @@
namespace Spatie\Activitylog\Test;
use Carbon\Carbon;
use Spatie\Activitylog\Models\Activity;
use Spatie\Activitylog\Test\Models\User;
use Spatie\Activitylog\Test\Models\Article;
@@ -390,6 +391,114 @@ public function it_can_use_both_fillable_and_log_attributes()
$this->assertEquals($expectedChanges, $this->getLastActivity()->changes()->toArray());
}
/** @test */
public function it_can_use_wildcard_for_loggable_attributes()
{
$articleClass = new class() extends Article {
public static $logAttributes = ['*'];
use LogsActivity;
};
$article = new $articleClass();
$article->name = 'my name';
Carbon::setTestNow(Carbon::create(2017, 1, 1, 12, 0, 0));
$article->save();
$expectedChanges = [
'attributes' => [
'name' => 'my name',
'text' => null,
'deleted_at' => null,
'id' => $article->id,
'user_id' => null,
'json' => null,
'created_at' => '2017-01-01 12:00:00',
'updated_at' => '2017-01-01 12:00:00',
],
];
$this->assertEquals($expectedChanges, $this->getLastActivity()->changes()->toArray());
}
/** @test */
public function it_can_use_wildcard_with_relation()
{
$articleClass = new class() extends Article {
public static $logAttributes = ['*', 'user.name'];
use LogsActivity;
};
$user = User::create([
'name' => 'user name',
]);
Carbon::setTestNow(Carbon::create(2017, 1, 1, 12, 0, 0));
$article = $articleClass::create([
'name' => 'article name',
'text' => 'article text',
'user_id' => $user->id,
]);
$expectedChanges = [
'attributes' => [
'id' => $article->id,
'name' => 'article name',
'text' => 'article text',
'deleted_at' => null,
'user_id' => $user->id,
'json' => null,
'created_at' => '2017-01-01 12:00:00',
'updated_at' => '2017-01-01 12:00:00',
'user.name' => 'user name',
],
];
$this->assertEquals($expectedChanges, $this->getLastActivity()->changes()->toArray());
}
/** @test */
public function it_can_use_wildcard_when_updating_model()
{
$articleClass = new class() extends Article {
public static $logAttributes = ['*'];
public static $logOnlyDirty = true;
use LogsActivity;
};
$user = User::create([
'name' => 'user name',
]);
Carbon::setTestNow(Carbon::create(2017, 1, 1, 12, 0, 0));
$article = $articleClass::create([
'name' => 'article name',
'text' => 'article text',
'user_id' => $user->id,
]);
$article->name = 'changed name';
Carbon::setTestNow(Carbon::create(2018, 1, 1, 12, 0, 0));
$article->save();
$expectedChanges = [
'attributes' => [
'name' => 'changed name',
'updated_at' => '2018-01-01 12:00:00',
],
'old' => [
'name' => 'article name',
'updated_at' => '2017-01-01 12:00:00',
],
];
$this->assertEquals($expectedChanges, $this->getLastActivity()->changes()->toArray());
}
protected function createArticle(): Article
{
$article = new $this->article();

0 comments on commit c6a0dc1

Please sign in to comment.