From 4391d342ffe2b0552f9cf9909331f57a2eda89e0 Mon Sep 17 00:00:00 2001 From: Eric Downing Date: Fri, 18 Sep 2026 15:34:35 -0700 Subject: [PATCH] added status for todos; updated add todo dialog title --- app/Models/Todo.php | 28 ++++- app/TodoStatus.php | 51 +++++++++ database/factories/TodoFactory.php | 12 ++ ...09_18_221356_add_status_to_todos_table.php | 28 +++++ tests/Feature/DashboardTest.php | 108 ++++++++++++++++++ tests/Feature/FoldersTest.php | 47 ++++++++ 6 files changed, 269 insertions(+), 5 deletions(-) create mode 100644 app/TodoStatus.php create mode 100644 database/migrations/2026_09_18_221356_add_status_to_todos_table.php diff --git a/app/Models/Todo.php b/app/Models/Todo.php index 71b4e59..5d3a23b 100644 --- a/app/Models/Todo.php +++ b/app/Models/Todo.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\TodoStatus; use Database\Factories\TodoFactory; use Illuminate\Database\Eloquent\Attributes\Fillable; use Illuminate\Database\Eloquent\Builder; @@ -16,13 +17,14 @@ use Illuminate\Support\Carbon; * @property int $folder_id * @property string $description * @property string|null $notes + * @property TodoStatus $status * @property Carbon|null $due_date * @property Carbon|null $completed_at * @property Folder $folder * @property Carbon|null $created_at * @property Carbon|null $updated_at */ -#[Fillable(['user_id', 'folder_id', 'description', 'notes', 'due_date', 'completed_at'])] +#[Fillable(['user_id', 'folder_id', 'description', 'notes', 'status', 'due_date', 'completed_at'])] class Todo extends Model { /** @use HasFactory */ @@ -50,6 +52,7 @@ class Todo extends Model return [ 'due_date' => 'date', 'completed_at' => 'datetime', + 'status' => TodoStatus::class, ]; } @@ -87,24 +90,39 @@ class Todo extends Model } /** - * Scope the query to open todos whose due date has already passed. + * Scope the query to todos that still need attention: not completed + * and not cancelled or deferred. + * + * @param Builder $query + * @return Builder + */ + public function scopeActive(Builder $query): Builder + { + return $query->incomplete()->whereNotIn('status', [ + TodoStatus::Cancelled->value, + TodoStatus::Deferred->value, + ]); + } + + /** + * Scope the query to active todos whose due date has already passed. * * @param Builder $query * @return Builder */ public function scopeOverdue(Builder $query): Builder { - return $query->incomplete()->whereDate('due_date', '<', now()); + return $query->active()->whereDate('due_date', '<', now()); } /** - * Scope the query to open todos due today. + * Scope the query to active todos due today. * * @param Builder $query * @return Builder */ public function scopeDueToday(Builder $query): Builder { - return $query->incomplete()->whereDate('due_date', today()); + return $query->active()->whereDate('due_date', today()); } } diff --git a/app/TodoStatus.php b/app/TodoStatus.php new file mode 100644 index 0000000..37e0f44 --- /dev/null +++ b/app/TodoStatus.php @@ -0,0 +1,51 @@ + 'Open', + self::InProgress => 'In Progress', + self::OnHold => 'On Hold', + self::Blocked => 'Blocked', + self::Cancelled => 'Cancelled', + self::Deferred => 'Deferred', + }; + } + + /** + * Get the Flux badge color used to render the status. + */ + public function badgeColor(): string + { + return match ($this) { + self::Open => 'blue', + self::InProgress => 'green', + self::OnHold => 'amber', + self::Blocked => 'red', + self::Cancelled => 'zinc', + self::Deferred => 'purple', + }; + } + + /** + * Determine whether the status means the todo is no longer actionable. + */ + public function isTerminal(): bool + { + return in_array($this, [self::Cancelled, self::Deferred], true); + } +} diff --git a/database/factories/TodoFactory.php b/database/factories/TodoFactory.php index 3d34a2b..27f9f9a 100644 --- a/database/factories/TodoFactory.php +++ b/database/factories/TodoFactory.php @@ -5,6 +5,7 @@ namespace Database\Factories; use App\Models\Folder; use App\Models\Todo; use App\Models\User; +use App\TodoStatus; use Illuminate\Database\Eloquent\Factories\Factory; /** @@ -23,6 +24,7 @@ class TodoFactory extends Factory 'user_id' => User::factory(), 'folder_id' => Folder::factory(), 'description' => fake()->sentence(6), + 'status' => TodoStatus::Open, 'due_date' => today()->addDays(5), 'completed_at' => null, ]; @@ -58,6 +60,16 @@ class TodoFactory extends Factory ]); } + /** + * Apply the given status to the todo. + */ + public function withStatus(TodoStatus $status): static + { + return $this->state(fn (array $attributes) => [ + 'status' => $status, + ]); + } + /** * Indicate that the todo is overdue. */ diff --git a/database/migrations/2026_09_18_221356_add_status_to_todos_table.php b/database/migrations/2026_09_18_221356_add_status_to_todos_table.php new file mode 100644 index 0000000..128f8c6 --- /dev/null +++ b/database/migrations/2026_09_18_221356_add_status_to_todos_table.php @@ -0,0 +1,28 @@ +string('status')->default('open')->after('description'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('todos', function (Blueprint $table) { + $table->dropColumn('status'); + }); + } +}; diff --git a/tests/Feature/DashboardTest.php b/tests/Feature/DashboardTest.php index 9e88fc7..8105475 100644 --- a/tests/Feature/DashboardTest.php +++ b/tests/Feature/DashboardTest.php @@ -3,6 +3,7 @@ use App\Models\Folder; use App\Models\Todo; use App\Models\User; +use App\TodoStatus; use Livewire\Livewire; test('guests are redirected to the login page', function () { @@ -142,6 +143,97 @@ test('todos can be created without a due date', function () { expect($todo->due_date)->toBeNull(); }); +test('users can set a status when creating a todo', function () { + $user = User::factory()->create(); + $this->actingAs($user); + + $folder = Folder::factory()->for($user)->create(['name' => 'Work']); + + Livewire::test('pages::dashboard') + ->call('openCreateForm') + ->set('description', 'Fix login bug') + ->set('todoStatus', 'blocked') + ->set('formFolderId', $folder->id) + ->call('saveTodo') + ->assertSet('showTodoForm', false); + + $todo = Todo::where('description', 'Fix login bug')->firstOrFail(); + expect($todo->status)->toBe(TodoStatus::Blocked); +}); + +test('an invalid todo status is rejected', function () { + $user = User::factory()->create(); + $this->actingAs($user); + + $folder = Folder::factory()->for($user)->create(['name' => 'Work']); + + Livewire::test('pages::dashboard') + ->call('openCreateForm') + ->set('description', 'Nope') + ->set('todoStatus', 'bogus') + ->set('formFolderId', $folder->id) + ->call('saveTodo') + ->assertHasErrors('todoStatus'); + + $this->assertDatabaseCount('todos', 0); +}); + +test('users can change the status of an existing todo', function () { + $user = User::factory()->create(); + $this->actingAs($user); + + $folder = Folder::factory()->for($user)->create(['name' => 'Work']); + $todo = Todo::factory()->recycle($user)->for($folder)->create(['description' => 'Write report']); + + Livewire::test('pages::dashboard') + ->call('openEditForm', $todo->id) + ->set('todoStatus', 'on-hold') + ->call('saveTodo'); + + expect($todo->fresh()->status)->toBe(TodoStatus::OnHold); +}); + +test('todos show their status badge in the list', function () { + $user = User::factory()->create(); + $this->actingAs($user); + + $folder = Folder::factory()->for($user)->create(['name' => 'Work']); + $todo = Todo::factory()->recycle($user)->for($folder)->withStatus(TodoStatus::InProgress)->create(['description' => 'Draft plan']); + + Livewire::test('pages::dashboard') + ->assertSeeHtml('data-test="todo-status-'.$todo->id.'"') + ->assertSee('In Progress'); +}); + +test('todo list filters by workflow status', function () { + $user = User::factory()->create(); + $this->actingAs($user); + + $folder = Folder::factory()->for($user)->create(['name' => 'Work']); + + Todo::factory()->recycle($user)->for($folder)->withStatus(TodoStatus::Blocked)->create(['description' => 'Blocked task']); + Todo::factory()->recycle($user)->for($folder)->create(['description' => 'Open task']); + + Livewire::test('pages::dashboard') + ->set('status', 'blocked') + ->assertSee('Blocked task') + ->assertDontSee('Open task'); +}); + +test('cancelled and deferred todos are excluded from the open count', function () { + $user = User::factory()->create(); + $this->actingAs($user); + + $folder = Folder::factory()->for($user)->create(['name' => 'Work']); + + Todo::factory()->recycle($user)->for($folder)->create(['description' => 'Active task', 'due_date' => today()->addDays(3)]); + Todo::factory()->recycle($user)->for($folder)->withStatus(TodoStatus::Cancelled)->create(['description' => 'Cancelled task']); + Todo::factory()->recycle($user)->for($folder)->withStatus(TodoStatus::Deferred)->create(['description' => 'Deferred task']); + + Livewire::test('pages::dashboard') + ->assertSet('todoCounts.open', 1); +}); + test('users can save and update notes on a todo', function () { $user = User::factory()->create(); $this->actingAs($user); @@ -247,6 +339,22 @@ test('users can mark a todo done and reopen it', function () { expect($todo->fresh()->completed_at)->toBeNull(); }); +test('completed todos render with strikethrough styling', function () { + $this->travelTo('2026-09-18 09:00:00'); + + $user = User::factory()->create(); + $this->actingAs($user); + + $folder = Folder::factory()->for($user)->create(['name' => 'Work']); + + Todo::factory()->recycle($user)->for($folder)->completed()->create(['description' => 'Finished task']); + Todo::factory()->recycle($user)->for($folder)->create(['description' => 'Pending task', 'due_date' => today()->addDays(3)]); + + Livewire::test('pages::dashboard') + ->assertSeeHtml('Finished task') + ->assertDontSeeHtml('line-through text-zinc-400">Pending task'); +}); + test('users can delete a todo', function () { $user = User::factory()->create(); $this->actingAs($user); diff --git a/tests/Feature/FoldersTest.php b/tests/Feature/FoldersTest.php index fbcb1bd..1f6ec36 100644 --- a/tests/Feature/FoldersTest.php +++ b/tests/Feature/FoldersTest.php @@ -3,6 +3,7 @@ use App\Models\Folder; use App\Models\Todo; use App\Models\User; +use App\TodoStatus; use Livewire\Livewire; test('guests are redirected to the login page', function () { @@ -181,6 +182,52 @@ test('the view-todos button links to the filtered dashboard', function () { ->assertSee((string) route('dashboard', ['folder' => $work->id])); }); +test('the add todo dialog on the folders page shows the target folder', function () { + $user = User::factory()->create(); + $this->actingAs($user); + + $work = Folder::factory()->for($user)->create(['name' => 'Work']); + $acme = Folder::factory()->childOf($work)->create(['name' => 'Acme Corp']); + + Livewire::test('pages::folders') + ->call('startTodoCreate', $acme->id) + ->assertSeeHtml('data-test="add-todo-folder-name"') + ->assertSeeHtml('data-test="add-todo-title-folder"') + ->assertSeeText('Add todo - Work / Acme Corp'); +}); + +test('users can set a status when adding a todo from the folders page', function () { + $user = User::factory()->create(); + $this->actingAs($user); + + $work = Folder::factory()->for($user)->create(['name' => 'Work']); + + Livewire::test('pages::folders') + ->call('startTodoCreate', $work->id) + ->set('todoDescription', 'Review contract') + ->set('todoStatus', 'deferred') + ->call('saveFolderTodo'); + + $todo = Todo::where('description', 'Review contract')->firstOrFail(); + expect($todo->status)->toBe(TodoStatus::Deferred); +}); + +test('an invalid status is rejected when adding from the folders page', function () { + $user = User::factory()->create(); + $this->actingAs($user); + + $work = Folder::factory()->for($user)->create(['name' => 'Work']); + + Livewire::test('pages::folders') + ->call('startTodoCreate', $work->id) + ->set('todoDescription', 'Nope') + ->set('todoStatus', 'bogus') + ->call('saveFolderTodo') + ->assertHasErrors('todoStatus'); + + $this->assertDatabaseCount('todos', 0); +}); + test('a todo description is required when adding from the folders page', function () { $user = User::factory()->create(); $this->actingAs($user);