Skip to content

Commit

Permalink
Merge branch 'master' into disabled-perf-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Gummibeer authored Mar 10, 2020
2 parents 6002b76 + 4e44214 commit a68fb30
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `spatie/laravel-activitylog` will be documented in this file

## 3.11.2 - 2020-03-10

- fix Laravel 7 array/json casted attributes [#680](https://github.com/spatie/laravel-activitylog/pull/680)

## 3.11.1 - 2020-03-02

- fix requirements
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
},
"require-dev": {
"ext-json": "*",
"orchestra/testbench": "^5.0",
"orchestra/testbench": "^4.0|^5.0",
"phpunit/phpunit": "^8.0|^9.0"
},
"autoload": {
Expand Down
8 changes: 7 additions & 1 deletion src/Traits/DetectsChanges.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ protected static function bootDetectsChanges()

//temporary hold the original attributes on the model
//as we'll need these in the updating event
$oldValues = (new static)->setRawAttributes($model->getOriginal());
if (method_exists(Model::class, 'getRawOriginal')) {
// Laravel >7.0
$oldValues = (new static)->setRawAttributes($model->getRawOriginal());
} else {
// Laravel <7.0
$oldValues = (new static)->setRawAttributes($model->getOriginal());
}

$model->oldAttributes = static::logChanges($oldValues);
});
Expand Down
68 changes: 67 additions & 1 deletion tests/DetectsChangesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ public function it_will_store_the_values_when_deleting_the_model_with_softdelete
}

/** @test */
public function it_can_store_the_changes_of_array_casted_properties()
public function it_can_store_the_changes_of_collection_casted_properties()
{
$articleClass = new class() extends Article {
public static $logAttributes = ['json'];
Expand Down Expand Up @@ -428,6 +428,72 @@ public function it_can_store_the_changes_of_array_casted_properties()
$this->assertEquals($expectedChanges, $this->getLastActivity()->changes()->toArray());
}

/** @test */
public function it_can_store_the_changes_of_array_casted_properties()
{
$articleClass = new class() extends Article {
public static $logAttributes = ['json'];
public static $logOnlyDirty = true;
protected $casts = ['json' => 'array'];

use LogsActivity;
};

$article = $articleClass::create([
'json' => ['value' => 'original'],
]);

$article->json = collect(['value' => 'updated']);
$article->save();

$expectedChanges = [
'attributes' => [
'json' => [
'value' => 'updated',
],
],
'old' => [
'json' => [
'value' => 'original',
],
],
];
$this->assertEquals($expectedChanges, $this->getLastActivity()->changes()->toArray());
}

/** @test */
public function it_can_store_the_changes_of_json_casted_properties()
{
$articleClass = new class() extends Article {
public static $logAttributes = ['json'];
public static $logOnlyDirty = true;
protected $casts = ['json' => 'json'];

use LogsActivity;
};

$article = $articleClass::create([
'json' => ['value' => 'original'],
]);

$article->json = collect(['value' => 'updated']);
$article->save();

$expectedChanges = [
'attributes' => [
'json' => [
'value' => 'updated',
],
],
'old' => [
'json' => [
'value' => 'original',
],
],
];
$this->assertEquals($expectedChanges, $this->getLastActivity()->changes()->toArray());
}

/** @test */
public function it_can_use_nothing_as_loggable_attributes()
{
Expand Down

0 comments on commit a68fb30

Please sign in to comment.