2026-09-04 13:37:17 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Tests;
|
|
|
|
|
|
2026-09-04 15:22:33 +01:00
|
|
|
use PDOException;
|
|
|
|
|
|
2026-09-04 13:37:17 +01:00
|
|
|
final class CardStatusTest extends ApiTestCase
|
|
|
|
|
{
|
|
|
|
|
/** Create a project and return its id. */
|
|
|
|
|
private function newProject(array $auth, string $title = 'Board'): int
|
|
|
|
|
{
|
|
|
|
|
return $this->decode(
|
|
|
|
|
$this->request('POST', '/api/projects', ['title' => $title], $auth),
|
|
|
|
|
)['project']['id'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @param array<string, string> $auth @return array<int, mixed> */
|
|
|
|
|
private function statuses(int $projectId, array $auth): array
|
|
|
|
|
{
|
|
|
|
|
return $this->decode(
|
|
|
|
|
$this->request('GET', "/api/projects/{$projectId}/statuses", null, $auth),
|
|
|
|
|
)['statuses'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_statuses_require_authentication(): void
|
|
|
|
|
{
|
|
|
|
|
$projectId = $this->newProject($this->authHeader());
|
|
|
|
|
|
|
|
|
|
self::assertSame(401, $this->request('GET', "/api/projects/{$projectId}/statuses")->getStatusCode());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_new_projects_are_seeded_with_the_default_statuses(): void
|
|
|
|
|
{
|
|
|
|
|
$auth = $this->authHeader();
|
|
|
|
|
$projectId = $this->newProject($auth);
|
|
|
|
|
|
|
|
|
|
$statuses = $this->statuses($projectId, $auth);
|
|
|
|
|
|
|
|
|
|
self::assertSame(['To do', 'Doing', 'Done'], array_column($statuses, 'name'));
|
|
|
|
|
self::assertSame([0, 1, 2], array_column($statuses, 'position'));
|
|
|
|
|
self::assertSame([$projectId, $projectId, $projectId], array_column($statuses, 'project_id'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_each_project_gets_its_own_status_rows(): void
|
|
|
|
|
{
|
|
|
|
|
$auth = $this->authHeader();
|
|
|
|
|
$first = $this->newProject($auth, 'One');
|
|
|
|
|
$second = $this->newProject($auth, 'Two');
|
|
|
|
|
|
|
|
|
|
$firstIds = array_column($this->statuses($first, $auth), 'id');
|
|
|
|
|
$secondIds = array_column($this->statuses($second, $auth), 'id');
|
|
|
|
|
|
|
|
|
|
self::assertSame([], array_intersect($firstIds, $secondIds));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_statuses_are_only_visible_to_the_project_owner(): void
|
|
|
|
|
{
|
|
|
|
|
$owner = $this->authHeader('owner@example.com');
|
|
|
|
|
$other = $this->authHeader('other@example.com');
|
|
|
|
|
$projectId = $this->newProject($owner, 'Private');
|
|
|
|
|
|
|
|
|
|
self::assertSame(200, $this->request('GET', "/api/projects/{$projectId}/statuses", null, $owner)->getStatusCode());
|
|
|
|
|
self::assertSame(404, $this->request('GET', "/api/projects/{$projectId}/statuses", null, $other)->getStatusCode());
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-04 15:22:33 +01:00
|
|
|
public function test_a_card_created_directly_in_a_project_starts_in_its_first_status(): void
|
2026-09-04 13:37:17 +01:00
|
|
|
{
|
|
|
|
|
$auth = $this->authHeader();
|
|
|
|
|
$projectId = $this->newProject($auth);
|
2026-09-04 15:22:33 +01:00
|
|
|
$firstStatus = $this->statuses($projectId, $auth)[0];
|
2026-09-04 13:37:17 +01:00
|
|
|
|
|
|
|
|
$card = $this->decode(
|
|
|
|
|
$this->request('POST', "/api/projects/{$projectId}/cards", ['text' => 'First'], $auth),
|
|
|
|
|
)['card'];
|
|
|
|
|
|
2026-09-04 15:22:33 +01:00
|
|
|
self::assertSame($firstStatus['id'], $card['status_id']);
|
|
|
|
|
self::assertSame('To do', $card['status']['name']);
|
|
|
|
|
self::assertSame($projectId, $card['project_id']);
|
2026-09-04 13:37:17 +01:00
|
|
|
}
|
|
|
|
|
|
2026-09-04 15:22:33 +01:00
|
|
|
public function test_a_referenced_status_cannot_be_deleted(): void
|
2026-09-04 13:37:17 +01:00
|
|
|
{
|
2026-09-04 15:22:33 +01:00
|
|
|
// There's no delete-status endpoint; this exercises the FK directly.
|
|
|
|
|
// A card with a project must have a status (the CHECK constraint), so
|
|
|
|
|
// the FK is ON DELETE RESTRICT rather than SET NULL.
|
2026-09-04 13:37:17 +01:00
|
|
|
$auth = $this->authHeader();
|
|
|
|
|
$projectId = $this->newProject($auth);
|
|
|
|
|
$statusId = $this->statuses($projectId, $auth)[0]['id'];
|
2026-09-04 15:22:33 +01:00
|
|
|
$this->request('POST', "/api/projects/{$projectId}/cards", ['text' => 'x'], $auth);
|
2026-09-04 13:37:17 +01:00
|
|
|
|
|
|
|
|
$this->db()->exec('PRAGMA foreign_keys = ON');
|
2026-09-04 15:22:33 +01:00
|
|
|
$this->expectException(PDOException::class);
|
2026-09-04 13:37:17 +01:00
|
|
|
$this->db()->prepare('DELETE FROM card_statuses WHERE id = ?')->execute([$statusId]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_deleting_a_project_cascades_to_its_statuses(): void
|
|
|
|
|
{
|
|
|
|
|
$auth = $this->authHeader();
|
|
|
|
|
$projectId = $this->newProject($auth, 'Temp');
|
|
|
|
|
|
|
|
|
|
$count = fn (): int => (int) $this->db()
|
|
|
|
|
->query("SELECT COUNT(*) FROM card_statuses WHERE project_id = {$projectId}")
|
|
|
|
|
->fetchColumn();
|
|
|
|
|
|
|
|
|
|
self::assertSame(3, $count());
|
|
|
|
|
|
|
|
|
|
$this->request('DELETE', "/api/projects/{$projectId}", null, $auth);
|
|
|
|
|
|
|
|
|
|
self::assertSame(0, $count());
|
|
|
|
|
}
|
|
|
|
|
}
|