trypost/tests/Feature/WorkspaceLabelControllerTest.php

152 lines
4.6 KiB
PHP
Raw Normal View History

2026-01-18 23:33:45 +00:00
<?php
declare(strict_types=1);
2026-01-18 23:33:45 +00:00
use App\Enums\User\Setup;
use App\Models\User;
use App\Models\Workspace;
use App\Models\WorkspaceLabel;
beforeEach(function () {
$this->user = User::factory()->create(['setup' => Setup::Completed]);
$this->workspace = Workspace::factory()->create(['user_id' => $this->user->id]);
$this->user->update(['current_workspace_id' => $this->workspace->id]);
});
// Index tests
test('labels index requires authentication', function () {
$response = $this->get(route('app.labels.index'));
2026-01-18 23:33:45 +00:00
$response->assertRedirect(route('login'));
});
test('labels index shows labels for workspace', function () {
WorkspaceLabel::factory()->count(3)->create(['workspace_id' => $this->workspace->id]);
$response = $this->actingAs($this->user)->get(route('app.labels.index'));
2026-01-18 23:33:45 +00:00
$response->assertOk();
$response->assertInertia(fn ($page) => $page
->component('labels/Index', false)
->has('workspace')
->has('labels', 3)
);
});
test('labels index redirects if no workspace', function () {
$this->user->update(['current_workspace_id' => null]);
$response = $this->actingAs($this->user)->get(route('app.labels.index'));
2026-01-18 23:33:45 +00:00
$response->assertRedirect(route('app.workspaces.create'));
2026-01-18 23:33:45 +00:00
});
// Store tests
test('store label requires authentication', function () {
$response = $this->post(route('app.labels.store'), [
2026-01-18 23:33:45 +00:00
'name' => 'Test Label',
'color' => '#FF5733',
]);
$response->assertRedirect(route('login'));
});
test('store label creates label', function () {
$response = $this->actingAs($this->user)->post(route('app.labels.store'), [
2026-01-18 23:33:45 +00:00
'name' => 'New Label',
'color' => '#FF5733',
]);
$response->assertRedirect(route('app.labels.index'));
2026-01-18 23:33:45 +00:00
$this->assertDatabaseHas('workspace_labels', [
'workspace_id' => $this->workspace->id,
'name' => 'New Label',
'color' => '#FF5733',
]);
});
test('store label validates required fields', function () {
$response = $this->actingAs($this->user)->post(route('app.labels.store'), [
2026-01-18 23:33:45 +00:00
'name' => '',
'color' => '',
]);
$response->assertSessionHasErrors(['name', 'color']);
});
test('store label validates color format', function () {
$response = $this->actingAs($this->user)->post(route('app.labels.store'), [
2026-01-18 23:33:45 +00:00
'name' => 'Valid Name',
'color' => 'invalid-color',
]);
$response->assertSessionHasErrors('color');
});
// Update tests
test('update label requires authentication', function () {
$label = WorkspaceLabel::factory()->create(['workspace_id' => $this->workspace->id]);
$response = $this->put(route('app.labels.update', $label), [
2026-01-18 23:33:45 +00:00
'name' => 'Updated Label',
'color' => '#00FF00',
]);
$response->assertRedirect(route('login'));
});
test('update label updates the label', function () {
$label = WorkspaceLabel::factory()->create(['workspace_id' => $this->workspace->id]);
$response = $this->actingAs($this->user)->put(route('app.labels.update', $label), [
2026-01-18 23:33:45 +00:00
'name' => 'Updated Label',
'color' => '#00FF00',
]);
$response->assertRedirect(route('app.labels.index'));
2026-01-18 23:33:45 +00:00
$label->refresh();
expect($label->name)->toBe('Updated Label');
expect($label->color)->toBe('#00FF00');
});
test('update label returns 404 for other workspace label', function () {
$otherWorkspace = Workspace::factory()->create();
$label = WorkspaceLabel::factory()->create(['workspace_id' => $otherWorkspace->id]);
$response = $this->actingAs($this->user)->put(route('app.labels.update', $label), [
2026-01-18 23:33:45 +00:00
'name' => 'Updated Label',
'color' => '#00FF00',
]);
$response->assertNotFound();
});
// Destroy tests
test('destroy label requires authentication', function () {
$label = WorkspaceLabel::factory()->create(['workspace_id' => $this->workspace->id]);
$response = $this->delete(route('app.labels.destroy', $label));
2026-01-18 23:33:45 +00:00
$response->assertRedirect(route('login'));
});
test('destroy label deletes the label', function () {
$label = WorkspaceLabel::factory()->create(['workspace_id' => $this->workspace->id]);
$response = $this->actingAs($this->user)->delete(route('app.labels.destroy', $label));
2026-01-18 23:33:45 +00:00
$response->assertRedirect(route('app.labels.index'));
2026-01-18 23:33:45 +00:00
expect(WorkspaceLabel::find($label->id))->toBeNull();
});
test('destroy label returns 404 for other workspace label', function () {
$otherWorkspace = Workspace::factory()->create();
$label = WorkspaceLabel::factory()->create(['workspace_id' => $otherWorkspace->id]);
$response = $this->actingAs($this->user)->delete(route('app.labels.destroy', $label));
2026-01-18 23:33:45 +00:00
$response->assertNotFound();
});