183 lines
5.6 KiB
PHP
Executable File
183 lines
5.6 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 Inertia\Testing\AssertableInertia as Assert;
|
|
use Tests\TestCase;
|
|
|
|
class MarkerTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_guests_cannot_manage_markers(): void
|
|
{
|
|
$this->post(route('markers.store'), [])->assertRedirect(route('login'));
|
|
}
|
|
|
|
public function test_user_can_create_marker(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
|
|
$response = $this->actingAs($user)->post(route('markers.store'), [
|
|
'name' => 'Анна',
|
|
'color' => '#db2777',
|
|
]);
|
|
|
|
$response->assertSessionHasNoErrors()->assertRedirect();
|
|
|
|
$this->assertDatabaseHas('markers', [
|
|
'user_id' => $user->id,
|
|
'name' => 'Анна',
|
|
'color' => '#db2777',
|
|
]);
|
|
}
|
|
|
|
public function test_marker_name_must_be_unique_per_user(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
Marker::factory()->for($user)->create(['name' => 'Анна']);
|
|
|
|
$response = $this->actingAs($user)->post(route('markers.store'), [
|
|
'name' => 'Анна',
|
|
'color' => '#db2777',
|
|
]);
|
|
|
|
$response->assertSessionHasErrors('name');
|
|
|
|
// Another user may use the same name.
|
|
$other = User::factory()->create();
|
|
$this->actingAs($other)->post(route('markers.store'), [
|
|
'name' => 'Анна',
|
|
'color' => '#db2777',
|
|
])->assertSessionHasNoErrors();
|
|
}
|
|
|
|
public function test_marker_color_must_be_hex(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
|
|
$response = $this->actingAs($user)->post(route('markers.store'), [
|
|
'name' => 'Анна',
|
|
'color' => 'red',
|
|
]);
|
|
|
|
$response->assertSessionHasErrors('color');
|
|
}
|
|
|
|
public function test_marker_limit_is_enforced(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
Marker::factory()->for($user)->count(20)->create();
|
|
|
|
$response = $this->actingAs($user)->post(route('markers.store'), [
|
|
'name' => 'Лишний',
|
|
'color' => '#123456',
|
|
]);
|
|
|
|
$response->assertSessionHasErrors('name');
|
|
}
|
|
|
|
public function test_user_can_update_own_marker(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
$marker = Marker::factory()->for($user)->create();
|
|
|
|
$response = $this->actingAs($user)->patch(route('markers.update', $marker), [
|
|
'name' => 'Новое имя',
|
|
'color' => '#16a34a',
|
|
]);
|
|
|
|
$response->assertSessionHasNoErrors();
|
|
$this->assertSame('Новое имя', $marker->refresh()->name);
|
|
}
|
|
|
|
public function test_user_cannot_update_foreign_marker(): void
|
|
{
|
|
$owner = User::factory()->create();
|
|
$marker = Marker::factory()->for($owner)->create();
|
|
|
|
$response = $this->actingAs(User::factory()->create())
|
|
->patch(route('markers.update', $marker), [
|
|
'name' => 'Взлом',
|
|
'color' => '#000000',
|
|
]);
|
|
|
|
$response->assertForbidden();
|
|
}
|
|
|
|
public function test_deleting_marker_cascades_periods(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
$marker = Marker::factory()->for($user)->create();
|
|
VacationPeriod::factory()->for($user)->for($marker)->create();
|
|
|
|
$response = $this->actingAs($user)->delete(route('markers.destroy', $marker));
|
|
|
|
$response->assertSessionHasNoErrors();
|
|
$this->assertDatabaseMissing('markers', ['id' => $marker->id]);
|
|
$this->assertDatabaseCount('vacation_periods', 0);
|
|
}
|
|
|
|
public function test_user_cannot_delete_foreign_marker(): void
|
|
{
|
|
$owner = User::factory()->create();
|
|
$marker = Marker::factory()->for($owner)->create();
|
|
|
|
$this->actingAs(User::factory()->create())
|
|
->delete(route('markers.destroy', $marker))
|
|
->assertForbidden();
|
|
}
|
|
|
|
public function test_planner_page_contains_only_own_data(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
$marker = Marker::factory()->for($user)->create();
|
|
VacationPeriod::factory()->for($user)->for($marker)->create([
|
|
'start_date' => '2026-07-06',
|
|
'end_date' => '2026-07-19',
|
|
]);
|
|
|
|
$foreign = User::factory()->create();
|
|
Marker::factory()->for($foreign)->create();
|
|
|
|
$this->actingAs($user)
|
|
->get(route('planner', ['year' => 2026]))
|
|
->assertOk()
|
|
->assertInertia(fn (Assert $page) => $page
|
|
->component('planner')
|
|
->where('year', 2026)
|
|
->has('markers', 1)
|
|
->has('periods', 1)
|
|
->where('periods.0.start_date', '2026-07-06')
|
|
->where('periods.0.end_date', '2026-07-19'),
|
|
);
|
|
}
|
|
|
|
public function test_planner_includes_cross_year_periods(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
$marker = Marker::factory()->for($user)->create();
|
|
VacationPeriod::factory()->for($user)->for($marker)->create([
|
|
'start_date' => '2026-12-28',
|
|
'end_date' => '2027-01-08',
|
|
]);
|
|
|
|
$this->actingAs($user)
|
|
->get(route('planner', ['year' => 2027]))
|
|
->assertInertia(fn (Assert $page) => $page->has('periods', 1));
|
|
}
|
|
|
|
public function test_planner_rejects_out_of_range_year(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
|
|
$this->actingAs($user)
|
|
->get(route('planner', ['year' => 1900]))
|
|
->assertInertia(fn (Assert $page) => $page->where('year', (int) date('Y')));
|
|
}
|
|
}
|