Skip to content

Commit

Permalink
Removed 6.x support, and thus no need for handling the differend asse…
Browse files Browse the repository at this point in the history
…rt locations

Co-authored-by: Morten D. Hansen <morten@visia.dk>
  • Loading branch information
erikn69 and MortenDHansen committed Dec 26, 2024
1 parent 2d5f2b1 commit e60933e
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 101 deletions.
13 changes: 0 additions & 13 deletions tests/AuditingTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,4 @@ protected function getPackageProviders($app)
AuditingServiceProvider::class,
];
}

/**
* Locate the Illuminate testing class. It changed namespace with v7
* @see https://readouble.com/laravel/7.x/en/upgrade.html
* @return class-string<\Illuminate\Foundation\Testing\Assert|\Illuminate\Testing\Assert>
*/
public static function Assert(): string
{
if (class_exists('Illuminate\Foundation\Testing\Assert')) {
return '\Illuminate\Foundation\Testing\Assert';
}
return '\Illuminate\Testing\Assert';
}
}
76 changes: 46 additions & 30 deletions tests/Functional/AuditingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

use Carbon\Carbon;
use Illuminate\Database\QueryException;
use Illuminate\Foundation\Testing\Assert;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Event;
use Illuminate\Testing\Assert;
use InvalidArgumentException;
use OwenIt\Auditing\Events\AuditCustom;
use OwenIt\Auditing\Events\Audited;
Expand Down Expand Up @@ -127,8 +127,6 @@ public function itWillAuditTheRetrievedEvent()

$audit = Audit::first();

$this->assertNotNull($audit);

$this->assertEmpty($audit->old_values);

$this->assertEmpty($audit->new_values);
Expand All @@ -152,11 +150,9 @@ public function itWillAuditTheCreatedEvent()

$audit = Audit::first();

$this->assertNotNull($audit);

$this->assertEmpty($audit->old_values);

self::Assert()::assertArraySubset([
Assert::assertArraySubset([
'title' => 'How To Audit Eloquent Models',
'content' => 'N/A',
'published_at' => null,
Expand Down Expand Up @@ -191,15 +187,13 @@ public function itWillAuditTheUpdatedEvent()

$audit = Audit::first();

$this->assertNotNull($audit);

self::Assert()::assertArraySubset([
Assert::assertArraySubset([
'content' => 'N/A',
'published_at' => null,
'reviewed' => 0,
], $audit->old_values, true);

self::Assert()::assertArraySubset([
Assert::assertArraySubset([
'content' => Article::contentMutate('First step: install the laravel-auditing package.'),
'published_at' => $now->toDateTimeString(),
'reviewed' => 1,
Expand All @@ -226,9 +220,7 @@ public function itWillAuditTheDeletedEvent()

$audit = Audit::first();

$this->assertNotNull($audit);

self::Assert()::assertArraySubset([
Assert::assertArraySubset([
'title' => 'How To Audit Eloquent Models',
'content' => 'N/A',
'published_at' => null,
Expand Down Expand Up @@ -260,11 +252,9 @@ public function itWillAuditTheRestoredEvent()

$audit = Audit::first();

$this->assertNotNull($audit);

$this->assertEmpty($audit->old_values);

self::Assert()::assertArraySubset([
Assert::assertArraySubset([
'title' => 'How To Audit Eloquent Models',
'content' => 'N/A',
'published_at' => null,
Expand Down Expand Up @@ -326,6 +316,41 @@ public function itWillRemoveOlderAuditsAboveTheThreshold()
$this->assertSame('Title #20', $audits->last()->new_values['title']);
}

/**
* @test
*/
public function itWillRemoveOlderAuditsAboveTheThresholdOnDelete()
{
$this->app['config']->set('audit.events', [
'updated', 'deleted',
]);

$article = factory(Article::class)->create([
'title' => 'Title #0',
]);

foreach (range(1, 20) as $count) {
if ($count === 11) {
sleep(1);
}

$article->update([
'title' => 'Title #' . $count,
]);
}

$audits = $article->audits()->get();
$this->assertSame(20, $audits->count());

$this->app['config']->set('audit.threshold', 10);
$article->forceDelete();

$audits = $article->audits()->get();
$this->assertSame(10, $audits->count());
$this->assertSame('Title #11', $audits->first()->new_values['title']);
$this->assertSame('Title #20', $audits->last()->new_values['title']);
}

/**
* @test
*/
Expand Down Expand Up @@ -369,11 +394,9 @@ public function itWillAuditUsingTheDefaultDriver()

$audit = Audit::first();

$this->assertNotNull($audit);

$this->assertEmpty($audit->old_values);

self::Assert()::assertArraySubset([
Assert::assertArraySubset([
'title' => 'How To Audit Using The Fallback Driver',
'content' => 'N/A',
'published_at' => null,
Expand Down Expand Up @@ -488,12 +511,10 @@ public function itHandlesJsonColumnsCorrectly()
$article->config = ['articleIsGood' => false, 'authorsJob' => 'vampire'];
$article->save();

/** @var Audit $audit */
$audit = $article->audits()->skip(1)->first();

$this->assertNotNull($audit);

$this->assertFalse($audit->getModified()['config']['new']['articleIsGood']);
$this->assertTrue($audit->getModified()['config']['old']['articleIsGood']);
$this->assertSame(false, $audit->getModified()['config']['new']['articleIsGood']);
$this->assertSame(true, $audit->getModified()['config']['old']['articleIsGood']);
}

/**
Expand All @@ -507,10 +528,8 @@ public function canAddAdditionalResolver()

$article = factory(Article::class)->create();

$this->assertTrue(true);
$audit = $article->audits()->first();

$this->assertNotNull($audit);

$this->assertSame(1, (int)$audit->tenant_id);
}

Expand All @@ -526,9 +545,6 @@ public function canDisableResolver()
$article = factory(Article::class)->create();

$audit = $article->audits()->first();

$this->assertNotNull($audit);

$this->assertEmpty($audit->ip_address);
}

Expand Down
17 changes: 9 additions & 8 deletions tests/Unit/AuditTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Carbon\Carbon;
use DateTimeInterface;
use Illuminate\Testing\Assert;
use OwenIt\Auditing\Encoders\Base64Encoder;
use OwenIt\Auditing\Models\Audit;
use OwenIt\Auditing\Redactors\LeftRedactor;
Expand Down Expand Up @@ -36,7 +37,7 @@ public function itResolvesAuditData()
$resolvedData = $audit->resolveData();
$this->assertCount(15, $resolvedData);

self::Assert()::assertArraySubset([
Assert::assertArraySubset([
'audit_id' => 1,
'audit_event' => 'created',
'audit_url' => UrlResolver::resolveCommandLine(),
Expand Down Expand Up @@ -85,7 +86,7 @@ public function itResolvesAuditDataIncludingUserAttributes()

$this->assertCount(21, $resolvedData = $audit->resolveData());

self::Assert()::assertArraySubset([
Assert::assertArraySubset([
'audit_id' => 2,
'audit_event' => 'created',
'audit_url' => UrlResolver::resolveCommandLine(),
Expand Down Expand Up @@ -203,7 +204,7 @@ public function itReturnsAuditMetadataAsArray()

$this->assertCount(10, $metadata = $audit->getMetadata());

self::Assert()::assertArraySubset([
Assert::assertArraySubset([
'audit_id' => 1,
'audit_event' => 'created',
'audit_url' => UrlResolver::resolveCommandLine(),
Expand All @@ -229,7 +230,7 @@ public function itReturnsProperCommandLineInUrlAuditMetadata()

$this->assertNotNull($audit);

self::Assert()::assertEquals($audit->getMetadata()['audit_url'], 'vendor/bin/phpunit tests/Unit/AuditTest.php --group command-line-url-resolver');
Assert::assertEquals($audit->getMetadata()['audit_url'], 'vendor/bin/phpunit tests/Unit/AuditTest.php --group command-line-url-resolver');
}

/**
Expand All @@ -253,7 +254,7 @@ public function itReturnsAuditMetadataIncludingUserAttributesAsArray()

$this->assertCount(16, $metadata = $audit->getMetadata());

self::Assert()::assertArraySubset([
Assert::assertArraySubset([
'audit_id' => 2,
'audit_event' => 'created',
'audit_url' => UrlResolver::resolveCommandLine(),
Expand Down Expand Up @@ -369,7 +370,7 @@ public function itReturnsAuditableModifiedAttributesAsArray()

$this->assertCount(5, $modified = $audit->getModified());

self::Assert()::assertArraySubset([
Assert::assertArraySubset([
'title' => [
'new' => 'HOW TO AUDIT ELOQUENT MODELS',
],
Expand Down Expand Up @@ -458,7 +459,7 @@ public function itReturnsDecodedAuditableAttributes()

$this->assertCount(3, $modified = $audit->getModified());

self::Assert()::assertArraySubset([
Assert::assertArraySubset([
'title' => [
'new' => 'HOW TO AUDIT ELOQUENT MODELS',
'old' => 'HOW TO AUDIT MODELS',
Expand Down Expand Up @@ -490,7 +491,7 @@ public function itReturnsTags()
]);

$this->assertIsArray($audit->getTags());
self::Assert()::assertArraySubset([
Assert::assertArraySubset([
'foo',
'bar',
'baz',
Expand Down
Loading

0 comments on commit e60933e

Please sign in to comment.