Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include Attribute Mutator on Audit Formatting #855

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Audit.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ protected function getFormattedValue(Model $model, string $key, $value)
return $model->mutateAttribute($key, $value);
}

if (method_exists($model, 'hasAttributeMutator') && $model->hasAttributeMutator($key)) {
return $model->mutateAttributeMarkedAttribute($key, $value);
}

if (array_key_exists(
$key,
$model->getCasts()
Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/AuditingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public function itWillAuditTheUpdatedEvent()
], $audit->old_values, true);

self::Assert()::assertArraySubset([
'content' => 'First step: install the laravel-auditing package.',
'content' => Article::contentMutate('First step: install the laravel-auditing package.'),
'published_at' => $now->toDateTimeString(),
'reviewed' => 1,
], $audit->new_values, true);
Expand Down
25 changes: 25 additions & 0 deletions tests/Models/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace OwenIt\Auditing\Tests\Models;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use OwenIt\Auditing\Contracts\Auditable;
Expand All @@ -11,6 +12,8 @@ class Article extends Model implements Auditable
use \OwenIt\Auditing\Auditable;
use SoftDeletes;

protected $laravel_version;

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -62,4 +65,26 @@ public function getTitleAttribute(string $value): string
{
return strtoupper($value);
}

/**
* Uppercase Content accessor.
*
* @return Attribute
*/
public function content(): Attribute
{
return new Attribute(
function ($value) { return $value; },
function ($value) { return ucwords($value); }
);
}

public static function contentMutate($value)
{
if (! method_exists(self::class, 'hasAttributeMutator')) {
return $value;
}

return ucwords($value);
}
}
10 changes: 5 additions & 5 deletions tests/Unit/AuditTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function itResolvesAuditData()
'user_id' => null,
'user_type' => null,
'new_title' => 'How To Audit Eloquent Models',
'new_content' => 'First step: install the laravel-auditing package.',
'new_content' => Article::contentMutate('First step: install the laravel-auditing package.'),
'new_published_at' => $now->toDateTimeString(),
'new_reviewed' => 1,
'new_id' => 1,
Expand Down Expand Up @@ -95,7 +95,7 @@ public function itResolvesAuditDataIncludingUserAttributes()
'user_created_at' => $user->created_at->toDateTimeString(),
'user_updated_at' => $user->updated_at->toDateTimeString(),
'new_title' => 'How To Audit Eloquent Models',
'new_content' => 'First step: install the laravel-auditing package.',
'new_content' => Article::contentMutate('First step: install the laravel-auditing package.'),
'new_published_at' => $now->toDateTimeString(),
'new_reviewed' => 1,
'new_id' => 1,
Expand Down Expand Up @@ -141,7 +141,7 @@ public function itReturnsTheAppropriateAuditableDataValues()
$this->assertInstanceOf(DateTimeInterface::class, $audit->getDataValue('new_published_at'));

// Original value
$this->assertSame('First step: install the laravel-auditing package.', $audit->getDataValue('new_content'));
$this->assertSame(Article::contentMutate('First step: install the laravel-auditing package.'), $audit->getDataValue('new_content'));
$this->assertSame('Sanchez', $audit->getDataValue('user_last_name'));

// Invalid value
Expand Down Expand Up @@ -307,7 +307,7 @@ public function itReturnsAuditableModifiedAttributesAsArray()
'new' => 'HOW TO AUDIT ELOQUENT MODELS',
],
'content' => [
'new' => 'First step: install the laravel-auditing package.',
'new' => Article::contentMutate('First step: install the laravel-auditing package.'),
],
'published_at' => [
'new' => $audit->getSerializedDate($now),
Expand Down Expand Up @@ -345,7 +345,7 @@ public function itReturnsAuditableModifiedAttributesAsJsonString()
"new" => "HOW TO AUDIT ELOQUENT MODELS"
],
"content" => [
"new" => "First step: install the laravel-auditing package."
"new" => Article::contentMutate('First step: install the laravel-auditing package.')
],
"published_at" => [
"new" => "$serializedDate"
Expand Down
14 changes: 7 additions & 7 deletions tests/Unit/AuditableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ public function itReturnsTheAuditData()
'old_values' => [],
'new_values' => [
'title' => 'How To Audit Eloquent Models',
'content' => 'First step: install the laravel-auditing package.',
'content' => Article::contentMutate('First step: install the laravel-auditing package.'),
'reviewed' => 1,
'published_at' => $now->toDateTimeString(),
],
Expand Down Expand Up @@ -469,7 +469,7 @@ public function itReturnsTheAuditDataIncludingUserAttributes(
'old_values' => [],
'new_values' => [
'title' => 'How To Audit Eloquent Models',
'content' => 'First step: install the laravel-auditing package.',
'content' => Article::contentMutate('First step: install the laravel-auditing package.'),
'reviewed' => 1,
'published_at' => $now->toDateTimeString(),
],
Expand Down Expand Up @@ -552,7 +552,7 @@ public function itExcludesAttributesFromTheAuditDataWhenInStrictMode()
'old_values' => [],
'new_values' => [
'title' => 'How To Audit Eloquent Models',
'content' => 'First step: install the laravel-auditing package.',
'content' => Article::contentMutate('First step: install the laravel-auditing package.'),
],
'event' => 'created',
'auditable_id' => null,
Expand Down Expand Up @@ -1246,7 +1246,7 @@ public function auditableTransitionTestProvider(): array
// Expectation when transitioning with new values
[
'title' => 'NULLAM EGESTAS INTERDUM ELEIFEND.',
'content' => 'Morbi consectetur laoreet sem, eu tempus odio tempor id.',
'content' => Article::contentMutate('Morbi consectetur laoreet sem, eu tempus odio tempor id.'),
],
],

Expand All @@ -1272,13 +1272,13 @@ public function auditableTransitionTestProvider(): array
// Expectation when transitioning with old values
[
'title' => 'VIVAMUS A URNA ET LOREM FAUCIBUS MALESUADA NEC NEC MAGNA.',
'content' => 'Mauris ipsum erat, semper non quam vel, sodales tincidunt ligula.',
'content' => Article::contentMutate('Mauris ipsum erat, semper non quam vel, sodales tincidunt ligula.'),
],

// Expectation when transitioning with new values
[
'title' => 'NULLAM EGESTAS INTERDUM ELEIFEND.',
'content' => 'Morbi consectetur laoreet sem, eu tempus odio tempor id.',
'content' => Article::contentMutate('Morbi consectetur laoreet sem, eu tempus odio tempor id.'),
],
],

Expand All @@ -1301,7 +1301,7 @@ public function auditableTransitionTestProvider(): array
// Expectation when transitioning with old values
[
'title' => 'VIVAMUS A URNA ET LOREM FAUCIBUS MALESUADA NEC NEC MAGNA.',
'content' => 'Mauris ipsum erat, semper non quam vel, sodales tincidunt ligula.',
'content' => Article::contentMutate('Mauris ipsum erat, semper non quam vel, sodales tincidunt ligula.'),
],

// Expectation when transitioning with new values
Expand Down