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

Read Audit Implementation from Auditable #874

Merged
merged 1 commit into from
Apr 22, 2024
Merged
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: 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
{

}