refactor: decouple activity log tests from system log tests into a dedicated feature test file

This commit is contained in:
Yoga Pangestu 2026-04-30 08:48:42 +07:00
parent f979e18bd0
commit 11469e7a04
2 changed files with 73 additions and 40 deletions

View File

@ -0,0 +1,56 @@
<?php
use Spatie\Activitylog\Models\Activity;
use function Pest\Laravel\actingAs;
use function Pest\Laravel\get;
/*
|--------------------------------------------------------------------------
| Activity Log Module Tests
|--------------------------------------------------------------------------
| This module tracks system-wide activities using Spatie Activitylog.
|--------------------------------------------------------------------------
*/
describe('Activity Log Module - Authorization', function () {
it('redirects to login when accessing activity logs unauthenticated', function () {
get(route('system.activity-logs.index'))
->assertRedirect(route('login'));
});
it('returns 403 when user has no permission to view activity logs', function () {
actingAs(createUnauthorizedUser())
->get(route('system.activity-logs.index'))
->assertStatus(403);
});
});
describe('Activity Log Module - Authorized Actions', function () {
beforeEach(function () {
$user = createAuthorizedUser(['View:Activity']);
actingAs($user);
});
it('can access activity logs index page', function () {
// Create a dummy activity to ensure data is present
activity()
->useLog('Testing')
->log('Test Activity Log Entry');
get(route('system.activity-logs.index'))
->assertOk()
->assertInertia(fn ($page) => $page
->component('admin/system/activity-log/index')
->has('activities')
);
});
});
describe('Activity Log Module - Unauthorized Actions', function () {
it('cannot view activity logs without permission', function () {
actingAs(createUnauthorizedUser())
->get(route('system.activity-logs.index'))
->assertStatus(403);
});
});

View File

@ -1,47 +1,44 @@
<?php
use Illuminate\Support\Facades\File;
use Spatie\Activitylog\Models\Activity;
use function Pest\Laravel\actingAs;
use function Pest\Laravel\get;
/*
|--------------------------------------------------------------------------
| Log Module Tests
| System Log Module Tests
|--------------------------------------------------------------------------
| This module reads and parses Laravel log files from storage/logs.
|--------------------------------------------------------------------------
*/
describe('Log Module - Authorization', function () {
describe('System Log Module - Authorization', function () {
it('redirects to login when accessing logs unauthenticated', function () {
get(route('system.logs.index'))->assertRedirect(route('login'));
get(route('system.activity-logs.index'))->assertRedirect(route('login'));
get(route('system.logs.index'))
->assertRedirect(route('login'));
});
it('returns 403 when user has no permission to view logs', function () {
actingAs(createUnauthorizedUser());
get(route('system.logs.index'))->assertStatus(403);
get(route('system.activity-logs.index'))->assertStatus(403);
actingAs(createUnauthorizedUser())
->get(route('system.logs.index'))
->assertStatus(403);
});
});
describe('Log Module - Authorized Actions', function () {
describe('System Log Module - Authorized Actions', function () {
beforeEach(function () {
$user = createAuthorizedUser([
'View:Log',
'View:Activity',
]);
$user = createAuthorizedUser(['View:Log']);
actingAs($user);
});
it('can access system logs index page', function () {
// Create a dummy log file to ensure it doesn't crash if no logs exist
// Create a dummy log file to ensure it doesn't crash
$path = storage_path('logs/laravel.log');
if (! File::exists(storage_path('logs'))) {
File::makeDirectory(storage_path('logs'), 0755, true);
}
File::put($path, "[2026-04-30 08:00:00] local.INFO: Test log message\n");
File::put($path, "[2026-04-30 08:00:00] local.INFO: Test system log entry\n");
get(route('system.logs.index'))
->assertOk()
@ -51,32 +48,12 @@
->has('logs')
);
});
it('can access activity logs index page', function () {
// Create a dummy activity
activity()
->useLog('TestModule')
->log('Test Activity');
get(route('system.activity-logs.index'))
->assertOk()
->assertInertia(fn ($page) => $page
->component('admin/system/activity-log/index')
->has('activities')
);
});
});
describe('Log Module - Unauthorized Actions', function () {
beforeEach(function () {
actingAs(createUnauthorizedUser());
});
describe('System Log Module - Unauthorized Actions', function () {
it('cannot view system logs without permission', function () {
get(route('system.logs.index'))->assertStatus(403);
});
it('cannot view activity logs without permission', function () {
get(route('system.activity-logs.index'))->assertStatus(403);
actingAs(createUnauthorizedUser())
->get(route('system.logs.index'))
->assertStatus(403);
});
});