Skip to content

Commit

Permalink
Read Audit Implementation from Auditable (#874)
Browse files Browse the repository at this point in the history
Co-authored-by: gisostallenberg <[email protected]>
  • Loading branch information
erikn69 and gisostallenberg authored Apr 22, 2024
1 parent 27e1b31 commit a08c673
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 57 deletions.
4 changes: 1 addition & 3 deletions src/Drivers/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ class Database implements AuditDriver
*/
public function audit(Auditable $model): ?Audit
{
$implementation = Config::get('audit.implementation', \OwenIt\Auditing\Models\Audit::class);

return call_user_func([$implementation, 'create'], $model->toAudit());
return call_user_func([get_class($model->audits()->getModel()), 'create'], $model->toAudit());
}

/**
Expand Down
23 changes: 23 additions & 0 deletions tests/Functional/AuditingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
use Illuminate\Support\Facades\Event;
use InvalidArgumentException;
use OwenIt\Auditing\Events\AuditCustom;
use OwenIt\Auditing\Events\Audited;
use OwenIt\Auditing\Events\Auditing;
use OwenIt\Auditing\Exceptions\AuditingException;
use OwenIt\Auditing\Models\Audit;
use OwenIt\Auditing\Tests\AuditingTestCase;
use OwenIt\Auditing\Tests\fixtures\TenantResolver;
use OwenIt\Auditing\Tests\Models\Article;
use OwenIt\Auditing\Tests\Models\ArticleCustomAuditMorph;
use OwenIt\Auditing\Tests\Models\ArticleExcludes;
use OwenIt\Auditing\Tests\Models\Category;
use OwenIt\Auditing\Tests\Models\User;
Expand Down Expand Up @@ -814,4 +816,25 @@ public function canAuditAnyCustomEvent()
'old_values' => '{"customExample":"Anakin Skywalker"}'
]);
}

/**
* @test
* @return void
*/
public function canAuditCustomAuditModelImplementation()
{
$audit = null;
Event::listen(Audited::class, function ($event) use (&$audit) {
$audit = $event->audit;
});

$article = new ArticleCustomAuditMorph();
$article->title = $this->faker->unique()->sentence;
$article->content = $this->faker->unique()->paragraph(6);
$article->reviewed = 0;
$article->save();

$this->assertNotEmpty($audit);
$this->assertSame(get_class($audit), \OwenIt\Auditing\Tests\Models\CustomAudit::class);
}
}
18 changes: 18 additions & 0 deletions tests/Models/ArticleCustomAuditMorph.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace OwenIt\Auditing\Tests\Models;

use Illuminate\Database\Eloquent\Relations\MorphMany;

class ArticleCustomAuditMorph extends Article
{
protected $table = 'articles';

/**
* @return MorphMany<CustomAudit>
*/
public function audits(): MorphMany
{
return $this->morphMany(CustomAudit::class, 'auditable');
}
}
55 changes: 1 addition & 54 deletions tests/Models/ArticleExcludes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,9 @@

namespace OwenIt\Auditing\Tests\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use OwenIt\Auditing\Contracts\Auditable;

class ArticleExcludes extends Model implements Auditable
class ArticleExcludes extends Article
{
use \OwenIt\Auditing\Auditable;
use SoftDeletes;

protected $table = 'articles';

/**
* {@inheritdoc}
*/
protected $casts = [
'reviewed' => 'bool',
'config' => 'json'
];

public $auditExclude = ['title'];

/**
* {@inheritdoc}
*/
protected $dates = [
'published_at',
];

/**
* {@inheritdoc}
*/
protected $fillable = [
'title',
'content',
'published_at',
'reviewed',
];


public function __construct(array $attributes = [])
{
if (class_exists(\Illuminate\Database\Eloquent\Casts\AsArrayObject::class)) {
$this->casts['config'] = \Illuminate\Database\Eloquent\Casts\AsArrayObject::class;
}
parent::__construct($attributes);
}

/**
* Uppercase Title accessor.
*
* @param string $value
*
* @return string
*/
public function getTitleAttribute(string $value): string
{
return strtoupper($value);
}
}
10 changes: 10 additions & 0 deletions tests/Models/CustomAudit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace OwenIt\Auditing\Tests\Models;

use OwenIt\Auditing\Models\Audit;

class CustomAudit extends Audit
{

}

0 comments on commit a08c673

Please sign in to comment.