first
This commit is contained in:
257
tests/Feature/Admin/Phase5Test.php
Normal file
257
tests/Feature/Admin/Phase5Test.php
Normal file
@@ -0,0 +1,257 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Admin;
|
||||
|
||||
use App\Models\AuditLog;
|
||||
use App\Models\Department;
|
||||
use App\Models\TranscriptionProject;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class Phase5Test extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private function makeAdmin(): User
|
||||
{
|
||||
return User::factory()->admin()->create();
|
||||
}
|
||||
|
||||
private function makeUser(): User
|
||||
{
|
||||
return User::factory()->create();
|
||||
}
|
||||
|
||||
private function makeProject(User $owner, array $attrs = []): TranscriptionProject
|
||||
{
|
||||
return TranscriptionProject::factory()->create(array_merge([
|
||||
'owner_user_id' => $owner->id,
|
||||
], $attrs));
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
// Dashboard
|
||||
// -------------------------------------------------------
|
||||
|
||||
public function test_admin_dashboard_loads_with_full_stats(): void
|
||||
{
|
||||
$admin = $this->makeAdmin();
|
||||
|
||||
$this->actingAs($admin)
|
||||
->get(route('admin.dashboard'))
|
||||
->assertOk()
|
||||
->assertViewIs('admin.dashboard')
|
||||
->assertViewHas('stats')
|
||||
->assertViewHas('topUsers')
|
||||
->assertViewHas('trendRaw')
|
||||
->assertViewHas('deptStats');
|
||||
}
|
||||
|
||||
public function test_user_cannot_access_admin_dashboard(): void
|
||||
{
|
||||
$user = $this->makeUser();
|
||||
|
||||
$this->actingAs($user)
|
||||
->get(route('admin.dashboard'))
|
||||
->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_dashboard_shows_storage_stats(): void
|
||||
{
|
||||
$admin = $this->makeAdmin();
|
||||
$owner = $this->makeUser();
|
||||
|
||||
TranscriptionProject::factory()->create([
|
||||
'owner_user_id' => $owner->id,
|
||||
'file_size' => 10485760, // 10 MB
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($admin)
|
||||
->get(route('admin.dashboard'))
|
||||
->assertOk();
|
||||
|
||||
$stats = $response->viewData('stats');
|
||||
$this->assertGreaterThanOrEqual(10485760, $stats['storage_bytes']);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
// Transfer Ownership
|
||||
// -------------------------------------------------------
|
||||
|
||||
public function test_admin_can_transfer_project_ownership(): void
|
||||
{
|
||||
$admin = $this->makeAdmin();
|
||||
$owner = $this->makeUser();
|
||||
$newOwner = $this->makeUser();
|
||||
$project = $this->makeProject($owner);
|
||||
|
||||
$this->actingAs($admin)
|
||||
->post(route('admin.projects.transfer-owner', $project), [
|
||||
'new_owner_id' => $newOwner->id,
|
||||
'justification' => 'Pekerja telah berpindah jabatan dan projek perlu dipindahkan.',
|
||||
])->assertRedirect(route('admin.projects.index'));
|
||||
|
||||
$project->refresh();
|
||||
$this->assertEquals($newOwner->id, $project->owner_user_id);
|
||||
}
|
||||
|
||||
public function test_transfer_ownership_creates_audit_log(): void
|
||||
{
|
||||
$admin = $this->makeAdmin();
|
||||
$owner = $this->makeUser();
|
||||
$newOwner = $this->makeUser();
|
||||
$project = $this->makeProject($owner);
|
||||
|
||||
$this->actingAs($admin)
|
||||
->post(route('admin.projects.transfer-owner', $project), [
|
||||
'new_owner_id' => $newOwner->id,
|
||||
'justification' => 'Sebab pemindahan yang sah dan lengkap.',
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('audit_logs', [
|
||||
'action' => 'project_ownership_transferred',
|
||||
'project_id' => $project->id,
|
||||
'target_user_id' => $newOwner->id,
|
||||
]);
|
||||
|
||||
$log = AuditLog::where('action', 'project_ownership_transferred')->first();
|
||||
$this->assertStringContainsString('Sebab pemindahan', $log->justification);
|
||||
}
|
||||
|
||||
public function test_user_cannot_transfer_ownership(): void
|
||||
{
|
||||
$user = $this->makeUser();
|
||||
$owner = $this->makeUser();
|
||||
$newOwner = $this->makeUser();
|
||||
$project = $this->makeProject($owner);
|
||||
|
||||
$this->actingAs($user)
|
||||
->post(route('admin.projects.transfer-owner', $project), [
|
||||
'new_owner_id' => $newOwner->id,
|
||||
'justification' => 'Cuba pindah oleh pengguna biasa.',
|
||||
])->assertForbidden();
|
||||
|
||||
$project->refresh();
|
||||
$this->assertEquals($owner->id, $project->owner_user_id);
|
||||
}
|
||||
|
||||
public function test_transfer_requires_justification(): void
|
||||
{
|
||||
$admin = $this->makeAdmin();
|
||||
$owner = $this->makeUser();
|
||||
$newOwner = $this->makeUser();
|
||||
$project = $this->makeProject($owner);
|
||||
|
||||
$this->actingAs($admin)
|
||||
->post(route('admin.projects.transfer-owner', $project), [
|
||||
'new_owner_id' => $newOwner->id,
|
||||
'justification' => 'Singkat', // less than 10 chars
|
||||
])->assertSessionHasErrors('justification');
|
||||
|
||||
$project->refresh();
|
||||
$this->assertEquals($owner->id, $project->owner_user_id);
|
||||
}
|
||||
|
||||
public function test_transfer_requires_valid_new_owner(): void
|
||||
{
|
||||
$admin = $this->makeAdmin();
|
||||
$owner = $this->makeUser();
|
||||
$project = $this->makeProject($owner);
|
||||
|
||||
$this->actingAs($admin)
|
||||
->post(route('admin.projects.transfer-owner', $project), [
|
||||
'new_owner_id' => 99999,
|
||||
'justification' => 'Justifikasi yang sah untuk pemindahan projek ini.',
|
||||
])->assertSessionHasErrors('new_owner_id');
|
||||
}
|
||||
|
||||
public function test_cannot_transfer_to_admin_user(): void
|
||||
{
|
||||
$admin = $this->makeAdmin();
|
||||
$admin2 = $this->makeAdmin();
|
||||
$owner = $this->makeUser();
|
||||
$project = $this->makeProject($owner);
|
||||
|
||||
$this->actingAs($admin)
|
||||
->post(route('admin.projects.transfer-owner', $project), [
|
||||
'new_owner_id' => $admin2->id,
|
||||
'justification' => 'Justifikasi yang sah untuk pemindahan projek ini.',
|
||||
])->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_cannot_transfer_to_same_owner(): void
|
||||
{
|
||||
$admin = $this->makeAdmin();
|
||||
$owner = $this->makeUser();
|
||||
$project = $this->makeProject($owner);
|
||||
|
||||
$this->actingAs($admin)
|
||||
->post(route('admin.projects.transfer-owner', $project), [
|
||||
'new_owner_id' => $owner->id,
|
||||
'justification' => 'Justifikasi yang sah untuk pemindahan projek ini.',
|
||||
])->assertStatus(422);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
// Audit Log Export
|
||||
// -------------------------------------------------------
|
||||
|
||||
public function test_admin_can_export_audit_log_csv(): void
|
||||
{
|
||||
$admin = $this->makeAdmin();
|
||||
|
||||
AuditLog::create([
|
||||
'actor_user_id' => $admin->id,
|
||||
'actor_role' => 'admin',
|
||||
'action' => 'user_created',
|
||||
'ip_address' => '127.0.0.1',
|
||||
'created_at' => now(),
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($admin)
|
||||
->get(route('admin.audit-logs.export'));
|
||||
|
||||
$response->assertOk();
|
||||
$this->assertStringContainsString('text/csv', $response->headers->get('Content-Type'));
|
||||
$this->assertStringContainsString('audit-log-', $response->headers->get('Content-Disposition'));
|
||||
}
|
||||
|
||||
public function test_user_cannot_export_audit_log(): void
|
||||
{
|
||||
$user = $this->makeUser();
|
||||
|
||||
$this->actingAs($user)
|
||||
->get(route('admin.audit-logs.export'))
|
||||
->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_audit_log_export_respects_filters(): void
|
||||
{
|
||||
$admin = $this->makeAdmin();
|
||||
|
||||
AuditLog::create([
|
||||
'actor_user_id' => $admin->id,
|
||||
'actor_role' => 'admin',
|
||||
'action' => 'user_activated',
|
||||
'ip_address' => '127.0.0.1',
|
||||
'created_at' => now(),
|
||||
]);
|
||||
|
||||
AuditLog::create([
|
||||
'actor_user_id' => $admin->id,
|
||||
'actor_role' => 'admin',
|
||||
'action' => 'user_deactivated',
|
||||
'ip_address' => '127.0.0.1',
|
||||
'created_at' => now(),
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($admin)
|
||||
->get(route('admin.audit-logs.export', ['action' => 'user_activated']));
|
||||
|
||||
$response->assertOk();
|
||||
$content = $response->streamedContent();
|
||||
$this->assertStringContainsString('user_activated', $content);
|
||||
$this->assertStringNotContainsString('user_deactivated', $content);
|
||||
}
|
||||
}
|
||||
166
tests/Feature/Admin/UserManagementTest.php
Normal file
166
tests/Feature/Admin/UserManagementTest.php
Normal file
@@ -0,0 +1,166 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Admin;
|
||||
|
||||
use App\Models\AuditLog;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class UserManagementTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private User $admin;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->admin = User::factory()->admin()->create();
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
// Access Control
|
||||
// -------------------------------------------------------
|
||||
|
||||
public function test_regular_user_cannot_access_admin_user_list(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$this->actingAs($user)->get(route('admin.users.index'))->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_admin_can_view_user_list(): void
|
||||
{
|
||||
$this->actingAs($this->admin)
|
||||
->get(route('admin.users.index'))
|
||||
->assertOk()
|
||||
->assertViewIs('admin.users.index');
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
// Create User
|
||||
// -------------------------------------------------------
|
||||
|
||||
public function test_admin_can_create_user(): void
|
||||
{
|
||||
$this->actingAs($this->admin)
|
||||
->post(route('admin.users.store'), [
|
||||
'name' => 'Pengguna Baru',
|
||||
'email' => 'baru@mbip.my',
|
||||
'password' => 'Password@123',
|
||||
'password_confirmation' => 'Password@123',
|
||||
'role' => 'user',
|
||||
'department_id' => null,
|
||||
])->assertRedirect(route('admin.users.index'));
|
||||
|
||||
$this->assertDatabaseHas('users', ['email' => 'baru@mbip.my', 'role' => 'user']);
|
||||
$this->assertDatabaseHas('audit_logs', ['action' => 'user_created']);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
// Deactivate / Activate
|
||||
// -------------------------------------------------------
|
||||
|
||||
public function test_admin_can_deactivate_user(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->actingAs($this->admin)
|
||||
->patch(route('admin.users.deactivate', $user))
|
||||
->assertRedirect(route('admin.users.index'));
|
||||
|
||||
$this->assertFalse($user->fresh()->is_active);
|
||||
$this->assertDatabaseHas('audit_logs', ['action' => 'user_deactivated', 'target_user_id' => $user->id]);
|
||||
}
|
||||
|
||||
public function test_admin_can_activate_user(): void
|
||||
{
|
||||
$user = User::factory()->inactive()->create();
|
||||
|
||||
$this->actingAs($this->admin)
|
||||
->patch(route('admin.users.activate', $user))
|
||||
->assertRedirect(route('admin.users.index'));
|
||||
|
||||
$this->assertTrue($user->fresh()->is_active);
|
||||
$this->assertDatabaseHas('audit_logs', ['action' => 'user_reactivated']);
|
||||
}
|
||||
|
||||
public function test_deactivated_user_cannot_login(): void
|
||||
{
|
||||
$user = User::factory()->inactive()->create();
|
||||
|
||||
$this->post(route('login'), [
|
||||
'email' => $user->email,
|
||||
'password' => 'password',
|
||||
])->assertSessionHasErrors('email');
|
||||
|
||||
$this->assertGuest();
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
// Email Change
|
||||
// -------------------------------------------------------
|
||||
|
||||
public function test_admin_can_change_user_email(): void
|
||||
{
|
||||
$user = User::factory()->create(['email' => 'lama@mbip.my']);
|
||||
|
||||
$this->actingAs($this->admin)
|
||||
->patch(route('admin.users.update-email', $user), [
|
||||
'email' => 'baru@mbip.my',
|
||||
'justification' => 'Pertukaran emel rasmi',
|
||||
])->assertRedirect(route('admin.users.index'));
|
||||
|
||||
$this->assertEquals('baru@mbip.my', $user->fresh()->email);
|
||||
$this->assertDatabaseHas('audit_logs', [
|
||||
'action' => 'user_email_changed',
|
||||
'target_user_id' => $user->id,
|
||||
]);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
// Delete
|
||||
// -------------------------------------------------------
|
||||
|
||||
public function test_admin_can_delete_user_without_usage(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->actingAs($this->admin)
|
||||
->delete(route('admin.users.destroy', $user))
|
||||
->assertRedirect(route('admin.users.index'));
|
||||
|
||||
$this->assertNull(User::find($user->id));
|
||||
$this->assertDatabaseHas('audit_logs', ['action' => 'user_deleted']);
|
||||
}
|
||||
|
||||
public function test_admin_cannot_delete_user_with_usage(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
// Cipta audit log untuk simulasi usage
|
||||
AuditLog::create([
|
||||
'actor_user_id' => $user->id,
|
||||
'actor_role' => 'user',
|
||||
'action' => 'project_created',
|
||||
'created_at' => now(),
|
||||
]);
|
||||
|
||||
$this->actingAs($this->admin)
|
||||
->delete(route('admin.users.destroy', $user))
|
||||
->assertForbidden();
|
||||
|
||||
$this->assertNotNull(User::find($user->id));
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
// Admin cannot access own account
|
||||
// -------------------------------------------------------
|
||||
|
||||
public function test_admin_cannot_deactivate_self(): void
|
||||
{
|
||||
$this->actingAs($this->admin)
|
||||
->patch(route('admin.users.deactivate', $this->admin))
|
||||
->assertForbidden();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user