Files
siakad/tests/Feature/BrandingTest.php
T
2026-10-24 11:45:32 +07:00

157 lines
6.2 KiB
PHP

<?php
namespace Tests\Feature;
use App\Enums\Role;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\File;
use PHPUnit\Framework\Attributes\DataProvider;
use Tests\Concerns\InspectsHtml;
use Tests\TestCase;
class BrandingTest extends TestCase
{
use InspectsHtml;
use RefreshDatabase;
/** @return array<string, array{Role}> */
public static function roles(): array
{
return array_combine(Role::values(), array_map(fn (Role $role): array => [$role], Role::cases()));
}
/** @return array<string, array{int}> */
public static function errorCodes(): array
{
return array_combine([403, 404, 419, 429, 500, 503], array_map(fn (int $code): array => [$code], [403, 404, 419, 429, 500, 503]));
}
public function test_login_page_shows_the_untan_logo_once(): void
{
$xpath = $this->dom($this->get(route('login')));
$this->assertHasUntanLogo($xpath, 'halaman masuk');
$this->assertSame(1, $this->logos($xpath)->length, 'Halaman masuk tidak boleh memuat dua lambang.');
}
#[DataProvider('roles')]
public function test_every_role_dashboard_shows_the_untan_logo(Role $role): void
{
$response = $this->actingAs($this->userWithRole($role))->get(route($role->dashboardRoute()));
$this->assertHasUntanLogo($this->dom($response), "dashboard {$role->value}");
}
public function test_the_403_error_page_shows_the_untan_logo_once(): void
{
$response = $this->actingAs($this->userWithRole(Role::Mahasiswa))->get(route('admin.dashboard'));
$response->assertForbidden();
$xpath = $this->dom($response);
$this->assertHasUntanLogo($xpath, 'halaman 403');
$this->assertSame(1, $this->logos($xpath)->length);
}
public function test_the_404_error_page_shows_the_untan_logo_once_for_guests_and_users(): void
{
$guest = $this->get('/tidak-ada');
$guest->assertNotFound();
$this->assertHasUntanLogo($this->dom($guest), 'halaman 404 (tamu)');
$this->assertSame(1, $this->logos($this->dom($guest))->length);
$user = $this->actingAs($this->userWithRole(Role::Dosen))->get('/tidak-ada');
$user->assertNotFound();
$this->assertHasUntanLogo($this->dom($user), 'halaman 404 (masuk)');
$this->assertSame(1, $this->logos($this->dom($user))->length);
}
#[DataProvider('errorCodes')]
public function test_every_error_view_renders_with_the_logo_and_its_code(int $code): void
{
$html = view("errors.{$code}")->render();
$xpath = $this->domFromHtml($html);
$this->assertHasUntanLogo($xpath, "halaman {$code}");
$this->assertSame(1, $this->logos($xpath)->length);
$this->assertStringContainsString("Kode galat {$code}", $this->textOf($xpath->query('//main')->item(0)));
$this->assertStringContainsString('<html lang="id"', $html);
$this->assertSame(1, $xpath->query('//h1')->length);
$this->assertSame(1, $xpath->query('//main//a')->length, 'Satu tindakan kembali.');
}
public function test_error_page_action_goes_to_login_for_guests_and_dashboard_for_users(): void
{
$guestLink = $this->attr($this->dom($this->get('/tidak-ada'))->query('//main//a'), 'href');
$this->assertSame(route('login'), $guestLink);
$this->actingAs($this->userWithRole(Role::Operator));
$userLink = $this->attr($this->dom($this->get('/tidak-ada'))->query('//main//a'), 'href');
$this->assertSame(route('dashboard'), $userLink);
}
public function test_pages_carry_the_logo_as_favicon_and_never_hotlink_wikipedia(): void
{
$pages = [
$this->get(route('login')),
$this->get('/tidak-ada'),
$this->actingAs($this->userWithRole(Role::Admin))->get(route('admin.dashboard')),
];
foreach ($pages as $response) {
$xpath = $this->dom($response);
$icon = $xpath->query("//link[@rel='icon']");
$this->assertSame(1, $icon->length);
$this->assertStringContainsString('logo-untan', $this->attr($icon, 'href'));
$this->assertStringNotContainsStringIgnoringCase('wikipedia', $response->getContent());
$this->assertStringNotContainsStringIgnoringCase('wikimedia', $response->getContent());
}
}
public function test_logo_files_exist_in_all_sizes(): void
{
foreach (['logo-untan.png', 'logo-untan-64.png', 'logo-untan-128.png', 'logo-untan-256.png', 'logo-untan-512.png'] as $file) {
$this->assertFileExists(public_path("images/{$file}"));
$this->assertSame(IMAGETYPE_PNG, exif_imagetype(public_path("images/{$file}")), "{$file} harus PNG.");
}
}
public function test_the_logo_component_is_the_only_place_that_emits_an_img(): void
{
$offenders = [];
foreach (File::allFiles(resource_path('views')) as $file) {
if ($file->getRelativePathname() === 'components'.DIRECTORY_SEPARATOR.'brand'.DIRECTORY_SEPARATOR.'logo.blade.php') {
continue;
}
if (preg_match('/<img\b/i', File::get($file->getPathname()))) {
$offenders[] = $file->getRelativePathname();
}
}
$this->assertSame([], $offenders, 'Hanya <x-brand.logo> yang boleh memancarkan <img>.');
}
public function test_no_view_or_script_hotlinks_the_wikipedia_logo(): void
{
foreach ([resource_path('views'), resource_path('js'), resource_path('css')] as $directory) {
foreach (File::allFiles($directory) as $file) {
$this->assertDoesNotMatchRegularExpression(
'/wikipedia|wikimedia/i',
File::get($file->getPathname()),
"{$file->getRelativePathname()} merujuk langsung ke Wikipedia.",
);
}
}
}
public function test_skeleton_leftovers_are_gone(): void
{
$this->assertFileDoesNotExist(resource_path('views/welcome.blade.php'));
$this->assertFileDoesNotExist(base_path('tests/Feature/ExampleTest.php'));
$this->assertFileDoesNotExist(base_path('tests/Unit/ExampleTest.php'));
$this->assertFileDoesNotExist(public_path('favicon.ico'));
}
}