74 lines
2.5 KiB
PHP
74 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Auth;
|
|
|
|
use App\Enums\Role;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
use Tests\Concerns\InspectsHtml;
|
|
use Tests\TestCase;
|
|
|
|
class LogoutTest 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()));
|
|
}
|
|
|
|
#[DataProvider('roles')]
|
|
public function test_post_logout_ends_the_session_and_redirects_to_login(Role $role): void
|
|
{
|
|
$user = $this->userWithRole($role, ['username' => 'akun']);
|
|
|
|
$this->post(route('login.store'), ['username' => 'akun', 'password' => 'password']);
|
|
$this->assertAuthenticatedAs($user);
|
|
$oldSessionId = session()->getId();
|
|
|
|
$this->post(route('logout'))->assertRedirect(route('login'));
|
|
|
|
$this->assertGuest();
|
|
$this->assertNotSame($oldSessionId, session()->getId(), 'Id sesi harus diganti saat keluar.');
|
|
$this->get(route($role->dashboardRoute()))->assertRedirect(route('login'));
|
|
}
|
|
|
|
public function test_logout_invalidates_session_data(): void
|
|
{
|
|
$this->actingAs($this->userWithRole(Role::Admin))
|
|
->withSession(['rahasia' => 'nilai'])
|
|
->post(route('logout'));
|
|
|
|
$this->assertNull(session('rahasia'));
|
|
}
|
|
|
|
public function test_logout_is_post_only(): void
|
|
{
|
|
$this->actingAs($this->userWithRole(Role::Admin));
|
|
|
|
$this->get('/logout')->assertStatus(405);
|
|
$this->assertAuthenticated();
|
|
}
|
|
|
|
public function test_guests_posting_to_logout_are_sent_to_login(): void
|
|
{
|
|
$this->post(route('logout'))->assertRedirect(route('login'));
|
|
}
|
|
|
|
#[DataProvider('roles')]
|
|
public function test_sidebar_has_a_logout_form_posting_to_the_logout_route(Role $role): void
|
|
{
|
|
$response = $this->actingAs($this->userWithRole($role))->get(route($role->dashboardRoute()));
|
|
$xpath = $this->dom($response);
|
|
$formPath = "//aside//form[@action='".route('logout')."']";
|
|
|
|
$form = $xpath->query($formPath);
|
|
$this->assertSame(1, $form->length);
|
|
$this->assertSame('post', strtolower($this->attr($form, 'method')));
|
|
$this->assertSame(1, $xpath->query($formPath."//input[@name='_token']")->length);
|
|
$this->assertSame('Keluar', $this->textOf($xpath->query($formPath.'//button')->item(0)));
|
|
}
|
|
}
|