vacation-planner/tests/Feature/VacationPeriodTest.php
2026-11-28 12:59:18 +08:00

160 lines
4.7 KiB
PHP
Executable File

<?php
namespace Tests\Feature;
use App\Models\Marker;
use App\Models\User;
use App\Models\VacationPeriod;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class VacationPeriodTest extends TestCase
{
use RefreshDatabase;
private User $user;
private Marker $marker;
protected function setUp(): void
{
parent::setUp();
$this->user = User::factory()->create();
$this->marker = Marker::factory()->for($this->user)->create();
}
public function test_guests_cannot_manage_periods(): void
{
$this->post(route('periods.store'), [])->assertRedirect(route('login'));
$this->post(route('periods.toggle'), [])->assertRedirect(route('login'));
}
public function test_user_can_add_range(): void
{
$response = $this->actingAs($this->user)->post(route('periods.store'), [
'marker_id' => $this->marker->id,
'start_date' => '2026-07-06',
'end_date' => '2026-07-19',
]);
$response->assertSessionHasNoErrors();
$this->assertDatabaseHas('vacation_periods', [
'user_id' => $this->user->id,
'marker_id' => $this->marker->id,
'start_date' => '2026-07-06',
'end_date' => '2026-07-19',
]);
}
public function test_end_date_must_not_be_before_start_date(): void
{
$response = $this->actingAs($this->user)->post(route('periods.store'), [
'marker_id' => $this->marker->id,
'start_date' => '2026-07-19',
'end_date' => '2026-07-06',
]);
$response->assertSessionHasErrors('end_date');
}
public function test_cannot_use_foreign_marker(): void
{
$foreign = Marker::factory()->create();
$response = $this->actingAs($this->user)->post(route('periods.store'), [
'marker_id' => $foreign->id,
'start_date' => '2026-07-06',
'end_date' => '2026-07-19',
]);
$response->assertSessionHasErrors('marker_id');
}
public function test_range_length_is_limited(): void
{
$response = $this->actingAs($this->user)->post(route('periods.store'), [
'marker_id' => $this->marker->id,
'start_date' => '2026-01-01',
'end_date' => '2027-12-31',
]);
$response->assertSessionHasErrors('end_date');
}
public function test_toggle_adds_and_removes_day(): void
{
$payload = [
'marker_id' => $this->marker->id,
'date' => '2026-07-06',
];
$this->actingAs($this->user)
->post(route('periods.toggle'), $payload)
->assertSessionHasNoErrors();
$this->assertDatabaseCount('vacation_periods', 1);
$this->actingAs($this->user)
->post(route('periods.toggle'), $payload)
->assertSessionHasNoErrors();
$this->assertDatabaseCount('vacation_periods', 0);
}
public function test_user_can_delete_own_period(): void
{
$period = VacationPeriod::factory()->for($this->user)->for($this->marker)->create();
$this->actingAs($this->user)
->delete(route('periods.destroy', $period))
->assertSessionHasNoErrors();
$this->assertDatabaseMissing('vacation_periods', ['id' => $period->id]);
}
public function test_user_cannot_delete_foreign_period(): void
{
$period = VacationPeriod::factory()->create();
$this->actingAs($this->user)
->delete(route('periods.destroy', $period))
->assertForbidden();
}
public function test_user_can_reschedule_period(): void
{
$period = VacationPeriod::factory()->for($this->user)->for($this->marker)->create([
'start_date' => '2026-07-06',
'end_date' => '2026-07-12',
]);
$this->actingAs($this->user)
->patch(route('periods.update', $period), [
'start_date' => '2026-08-01',
'end_date' => '2026-08-10',
])
->assertSessionHasNoErrors();
$this->assertDatabaseMissing('vacation_periods', ['id' => $period->id]);
$this->assertDatabaseHas('vacation_periods', [
'marker_id' => $this->marker->id,
'start_date' => '2026-08-01',
'end_date' => '2026-08-10',
]);
}
public function test_user_cannot_reschedule_foreign_period(): void
{
$period = VacationPeriod::factory()->create();
$this->actingAs($this->user)
->patch(route('periods.update', $period), [
'start_date' => '2026-08-01',
'end_date' => '2026-08-10',
])
->assertForbidden();
}
}