132 lines
5.7 KiB
PHP
132 lines
5.7 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Enums\Role;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\Concerns\InspectsHtml;
|
|
use Tests\TestCase;
|
|
|
|
/** Kontrak dashboard yang sama untuk kelima role; tiap role mengisi data profilnya sendiri. */
|
|
abstract class RoleDashboardTestCase extends TestCase
|
|
{
|
|
use InspectsHtml;
|
|
use RefreshDatabase;
|
|
|
|
abstract protected function role(): Role;
|
|
|
|
abstract protected function userName(): string;
|
|
|
|
/** @return array<string, mixed> kolom profil yang dibuat untuk akun uji */
|
|
abstract protected function profileData(): array;
|
|
|
|
/** @return array<string, string> label tampil => nilai yang harus muncul */
|
|
abstract protected function expectedDetails(): array;
|
|
|
|
protected function makeUser(): User
|
|
{
|
|
return $this->userWithRole(
|
|
$this->role(),
|
|
['name' => $this->userName(), 'username' => 'akun-'.$this->role()->value],
|
|
$this->profileData(),
|
|
);
|
|
}
|
|
|
|
public function test_dashboard_requires_authentication(): void
|
|
{
|
|
$this->get(route($this->role()->dashboardRoute()))->assertRedirect(route('login'));
|
|
}
|
|
|
|
public function test_dashboard_renders_the_welcome_heading_exactly(): void
|
|
{
|
|
$response = $this->actingAs($this->makeUser())->get(route($this->role()->dashboardRoute()));
|
|
|
|
$response->assertOk();
|
|
|
|
$headings = $this->dom($response)->query('//h1');
|
|
$this->assertSame(1, $headings->length, 'Tepat satu h1 per halaman.');
|
|
$this->assertSame("Selamat Datang, {$this->userName()}!", $this->textOf($headings->item(0)));
|
|
}
|
|
|
|
public function test_dashboard_shows_the_identifier_and_details_from_the_profile(): void
|
|
{
|
|
$response = $this->actingAs($this->makeUser())->get(route($this->role()->dashboardRoute()));
|
|
$xpath = $this->dom($response);
|
|
|
|
$main = $this->textOf($xpath->query('//main')->item(0));
|
|
|
|
$this->assertStringContainsString('Identitas akademik', $main);
|
|
$this->assertStringContainsString($this->role()->label(), $main);
|
|
$this->assertStringContainsString('akun-'.$this->role()->value, $main);
|
|
|
|
foreach ($this->expectedDetails() as $label => $value) {
|
|
$this->assertStringContainsString($label, $main, "Label {$label} tidak tampil.");
|
|
$this->assertStringContainsString($value, $main, "Nilai {$label} tidak tampil.");
|
|
}
|
|
|
|
$identifierLabel = $this->role()->identifierLabel();
|
|
$this->assertArrayHasKey($identifierLabel, $this->expectedDetails());
|
|
$this->assertStringContainsString($this->expectedDetails()[$identifierLabel], $this->textOf($xpath->query("//p[contains(@class, 'num')]")->item(0)));
|
|
}
|
|
|
|
public function test_dashboard_without_a_profile_row_still_renders_without_the_identifier(): void
|
|
{
|
|
$user = User::factory()->state(['role' => $this->role(), 'name' => $this->userName()])->create();
|
|
|
|
$response = $this->actingAs($user)->get(route($this->role()->dashboardRoute()));
|
|
|
|
$response->assertOk();
|
|
$this->assertSame("Selamat Datang, {$this->userName()}!", $this->textOf($this->dom($response)->query('//h1')->item(0)));
|
|
$this->assertSame(0, $this->dom($response)->query("//p[contains(@class, 'num')]")->length, 'Tanpa profil tidak ada nomor induk.');
|
|
}
|
|
|
|
public function test_sidebar_lists_the_dashboard_as_the_only_active_item(): void
|
|
{
|
|
$response = $this->actingAs($this->makeUser())->get(route($this->role()->dashboardRoute()));
|
|
$xpath = $this->dom($response);
|
|
|
|
$items = $xpath->query("//aside//nav//a[contains(concat(' ', normalize-space(@class), ' '), ' nav-item ')]");
|
|
|
|
$this->assertGreaterThanOrEqual(1, $items->length);
|
|
$this->assertSame('Dashboard', $this->textOf($items->item(0)), 'Dashboard adalah butir pertama.');
|
|
$this->assertSame(route($this->role()->dashboardRoute()), $items->item(0)->getAttribute('href'));
|
|
|
|
$active = $xpath->query("//aside//nav//a[@aria-current='page']");
|
|
$this->assertSame(1, $active->length, 'Tepat satu butir menu aktif.');
|
|
$this->assertSame(route($this->role()->dashboardRoute()), $active->item(0)->getAttribute('href'));
|
|
|
|
$this->assertStringContainsString('Menu '.$this->role()->label(), $this->textOf($xpath->query('//aside//nav')->item(0)));
|
|
}
|
|
|
|
public function test_shell_landmarks_and_language(): void
|
|
{
|
|
$response = $this->actingAs($this->makeUser())->get(route($this->role()->dashboardRoute()));
|
|
$xpath = $this->dom($response);
|
|
|
|
$response->assertSee('<html lang="id"', false);
|
|
$this->assertSame(1, $xpath->query('//main[@id="isi"]')->length);
|
|
$this->assertSame(1, $xpath->query('//a[@class="skip-link"][@href="#isi"]')->length);
|
|
$this->assertSame(1, $xpath->query('//aside//nav')->length);
|
|
$this->assertSame(1, $xpath->query('//header')->length);
|
|
$this->assertStringContainsString('Dashboard', $this->textOf($xpath->query('//title')->item(0)));
|
|
$this->assertStringContainsString($this->userName(), $this->textOf($xpath->query('//header')->item(0)));
|
|
}
|
|
|
|
public function test_dashboard_shows_the_untan_logo(): void
|
|
{
|
|
$response = $this->actingAs($this->makeUser())->get(route($this->role()->dashboardRoute()));
|
|
|
|
$this->assertHasUntanLogo($this->dom($response), "dashboard {$this->role()->value}");
|
|
}
|
|
|
|
public function test_dashboard_does_not_show_data_of_other_accounts(): void
|
|
{
|
|
$this->userWithRole(Role::Admin, ['name' => 'Orang Lain Sekali', 'username' => 'orang-lain']);
|
|
|
|
$response = $this->actingAs($this->makeUser())->get(route($this->role()->dashboardRoute()));
|
|
|
|
$response->assertDontSee('Orang Lain Sekali');
|
|
}
|
|
}
|