75 lines
2.4 KiB
PHP
75 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\Consultant;
|
|
use App\Models\DataCentre;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
class AssignmentTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
$this->seedRolesAndPermissions();
|
|
}
|
|
|
|
public function test_jpp_can_assign_consultant_to_data_centre(): void
|
|
{
|
|
$jpp = $this->makeUserWithRole('Pegawai JPP');
|
|
$dc = DataCentre::factory()->create();
|
|
$consultant = Consultant::factory()->create();
|
|
|
|
$this->actingAs($jpp)->post(route('data-centres.assign', $dc), [
|
|
'consultant_id' => $consultant->id,
|
|
'start_date' => now()->toDateString(),
|
|
'reason' => 'Penugasan awal',
|
|
])->assertRedirect();
|
|
|
|
$dc->refresh();
|
|
$this->assertEquals($consultant->id, $dc->current_consultant_id);
|
|
$this->assertDatabaseHas('data_centre_consultant_assignments', [
|
|
'data_centre_id' => $dc->id,
|
|
'consultant_id' => $consultant->id,
|
|
'is_active' => true,
|
|
]);
|
|
}
|
|
|
|
public function test_only_one_active_consultant_per_data_centre(): void
|
|
{
|
|
$jpp = $this->makeUserWithRole('Pegawai JPP');
|
|
$dc = DataCentre::factory()->create();
|
|
$first = Consultant::factory()->create();
|
|
$second = Consultant::factory()->create();
|
|
|
|
$this->actingAs($jpp)->post(route('data-centres.assign', $dc), [
|
|
'consultant_id' => $first->id,
|
|
]);
|
|
|
|
// Tugaskan perunding kedua — penugasan pertama mesti ditamatkan.
|
|
$this->actingAs($jpp)->post(route('data-centres.assign', $dc), [
|
|
'consultant_id' => $second->id,
|
|
'reason' => 'Tukar perunding',
|
|
]);
|
|
|
|
$dc->refresh();
|
|
$this->assertEquals($second->id, $dc->current_consultant_id);
|
|
|
|
// Hanya satu penugasan aktif.
|
|
$activeCount = $dc->assignments()->where('is_active', true)->count();
|
|
$this->assertEquals(1, $activeCount);
|
|
|
|
// Penugasan pertama ditamatkan (ada end_date).
|
|
$this->assertDatabaseHas('data_centre_consultant_assignments', [
|
|
'data_centre_id' => $dc->id,
|
|
'consultant_id' => $first->id,
|
|
'is_active' => false,
|
|
]);
|
|
// Sejarah dikekalkan: dua rekod penugasan.
|
|
$this->assertEquals(2, $dc->assignments()->count());
|
|
}
|
|
}
|