Skip to content
Permalink
Browse files

Merge branch 'master' into support-larvel-58

  • Loading branch information...
AlexVanderbist committed Feb 27, 2019
2 parents 912a1e7 + 45f437f commit 9b1a125e9835c9d84b9677ec05f0c38e129feddc
Showing with 90 additions and 7 deletions.
  1. +4 −0 .scrutinizer.yml
  2. +1 −0 README.md
  3. +7 −2 composer.json
  4. +4 −2 src/ActivityLogger.php
  5. +10 −2 src/Traits/DetectsChanges.php
  6. +63 −0 tests/DetectsChangesTest.php
  7. +1 −1 tests/TestCase.php
@@ -17,3 +17,7 @@ checks:
fix_identation_4spaces: true
fix_doc_comments: true

tools:
external_code_coverage:
timeout: 120
runs: 4
@@ -2,6 +2,7 @@

[![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/laravel-activitylog.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-activitylog)
[![Build Status](https://img.shields.io/travis/spatie/laravel-activitylog/master.svg?style=flat-square)](https://travis-ci.org/spatie/laravel-activitylog)
[![Code coverage](https://scrutinizer-ci.com/g/spatie/laravel-activitylog/badges/coverage.png)](https://scrutinizer-ci.com/g/spatie/laravel-activitylog)
[![Quality Score](https://img.shields.io/scrutinizer/g/spatie/laravel-activitylog.svg?style=flat-square)](https://scrutinizer-ci.com/g/spatie/laravel-activitylog)
[![StyleCI](https://styleci.io/repos/61802818/shield)](https://styleci.io/repos/61802818)
[![Total Downloads](https://img.shields.io/packagist/dt/spatie/laravel-activitylog.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-activitylog)
@@ -30,11 +30,13 @@
"illuminate/database": "~5.5.0|~5.6.0|~5.7.0|~5.8.0",
"illuminate/support": "~5.5.0|~5.6.0|~5.7.0|~5.8.0",
"spatie/string": "^2.1"

},
"require-dev": {
"phpunit/phpunit": "^7.5",
"phpunit/phpunit": "^7.4 | ^8.0",
"orchestra/testbench": "~3.5.0|~3.6.0|~3.7.0|~3.8.0"
"ext-json": "*",
"orchestra/testbench": "~3.5.0|~3.6.0|~3.7.0",
"scrutinizer/ocular": "^1.5"
},
"autoload": {
"psr-4": {
@@ -59,5 +61,8 @@
"Spatie\\Activitylog\\ActivitylogServiceProvider"
]
}
},
"config": {
"sort-packages": true
}
}
@@ -147,9 +147,11 @@ protected function normalizeCauser($modelOrId): Model
return $modelOrId;
}
$model = $this->auth->guard($this->authDriver)->getProvider()->retrieveById($modelOrId);
$guard = $this->auth->guard($this->authDriver);
$provider = method_exists($guard, 'getProvider') ? $guard->getProvider() : null;
$model = method_exists($provider, 'retrieveById') ? $provider->retrieveById($modelOrId) : null;
if ($model) {
if ($model instanceof Model) {
return $model;
}
@@ -114,11 +114,19 @@ function ($new, $old) {
public static function logChanges(Model $model): array
{
$changes = [];
foreach ($model->attributesToBeLogged() as $attribute) {
$attributes = $model->attributesToBeLogged();
$model = clone $model;
$model->append(array_filter($attributes, function ($key) use ($model) {
return $model->hasGetMutator($key);
}));
$model->setHidden(array_diff($model->getHidden(), $attributes));
$collection = collect($model);
foreach ($attributes as $attribute) {
if (Str::contains($attribute, '.')) {
$changes += self::getRelatedModelAttributeValue($model, $attribute);
} else {
$changes += collect($model)->only($attribute)->toArray();
$changes += $collection->only($attribute)->toArray();
}
}
@@ -656,6 +656,69 @@ public function it_will_store_no_changes_when_wildcard_guard_and_log_unguarded_a
$this->assertEquals([], $this->getLastActivity()->changes()->toArray());
}
/** @test */
public function it_can_use_hidden_as_loggable_attributes()
{
$articleClass = new class() extends Article {
protected $hidden = ['text'];
protected $fillable = ['name', 'text'];
protected static $logAttributes = ['name', 'text'];
use LogsActivity;
};
$article = new $articleClass();
$article->name = 'my name';
$article->text = 'my text';
$article->save();
$expectedChanges = [
'attributes' => [
'name' => 'my name',
'text' => 'my text',
],
];
$this->assertEquals($expectedChanges, $this->getLastActivity()->changes()->toArray());
}
/** @test */
public function it_can_use_overloaded_as_loggable_attributes()
{
$articleClass = new class() extends Article {
protected $fillable = ['name', 'text'];
protected static $logAttributes = ['name', 'text', 'description'];
use LogsActivity;
public function setDescriptionAttribute($value)
{
$this->attributes['json'] = json_encode(['description' => $value]);
}
public function getDescriptionAttribute()
{
return array_get(json_decode($this->attributes['json'], true), 'description');
}
};
$article = new $articleClass();
$article->name = 'my name';
$article->text = 'my text';
$article->description = 'my description';
$article->save();
$expectedChanges = [
'attributes' => [
'name' => 'my name',
'text' => 'my text',
'description' => 'my description',
],
];
$this->assertEquals($expectedChanges, $this->getLastActivity()->changes()->toArray());
}
protected function createArticle(): Article
{
$article = new $this->article();
@@ -52,7 +52,7 @@ protected function setUpDatabase()
protected function createActivityLogTable()
{
include_once '__DIR__'.'/../migrations/create_activity_log_table.php.stub';
include_once __DIR__.'/../migrations/create_activity_log_table.php.stub';
(new CreateActivityLogTable())->up();
}

0 comments on commit 9b1a125

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.