getUserByUsername($username), ['tickets:write']); }; $tools = new TicketWriteTools($conn); $pass = 0; $fail = 0; $check = function (string $name, bool $ok, $detail = '') use (&$pass, &$fail): void { $ok ? $pass++ : $fail++; echo ($ok ? 'PASS ' : 'FAIL ') . $name . ($ok ? '' : ' ' . json_encode($detail)) . "\n"; }; $err = function (callable $fn): ?string { try { $fn(); return null; } catch (\Mcp\Exception\ToolCallException $e) { return $e->getMessage(); } }; $row = fn(int $id) => $conn->query( "SELECT comment_text, markdown_enabled FROM ticket_comments WHERE comment_id = $id" )->fetch_assoc(); $audit = fn(string $action, int $id) => $conn->query( "SELECT details FROM audit_log WHERE action_type = '$action' AND entity_type = 'comment' AND entity_id = '$id' ORDER BY audit_id DESC LIMIT 1" )->fetch_assoc(); echo "== comments\n"; $check('edit_comment is a write tool', ToolCatalog::isWriteTool('edit_comment')); $check('delete_comment is a write tool', ToolCatalog::isWriteTool('delete_comment')); // Edit own comment $as('alice'); $a = $tools->addComment('100000001', 'first draft', true)['comment_id']; $r = $tools->editComment($a, " **final** text ", false); $check('alice edits her comment', $r === ['comment_id' => $a, 'ticket_id' => '100000001', 'edited' => true], $r); $check( 'text trimmed and stored, markdown off', $row($a) === ['comment_text' => '**final** text', 'markdown_enabled' => '0'], $row($a) ); $check('edit is audit-logged', str_contains($audit('update', $a)['details'] ?? '', '100000001'), $audit('update', $a)); $e = $err(fn() => $tools->editComment($a, ' ')); $check('empty text rejected', $e === 'Comment text cannot be empty', $e); $e = $err(fn() => $tools->editComment(999999, 'x')); $check('missing comment: not found', $e === 'Comment 999999 not found', $e); // Someone else's comment $as('bob'); $e = $err(fn() => $tools->editComment($a, 'hijack')); $check('bob cannot edit alice\'s comment', $e === 'You do not have permission to edit this comment', $e); $e = $err(fn() => $tools->deleteComment($a)); $check('bob cannot delete alice\'s comment', $e === 'You do not have permission to delete this comment', $e); $check('alice\'s text unchanged', $row($a)['comment_text'] === '**final** text', $row($a)); // A comment on a ticket alice can't see reads as not found $as('ci.tester'); $c = $tools->addComment('100000009', 'confidential note')['comment_id']; $as('alice'); $e = $err(fn() => $tools->editComment($c, 'peek')); $check('hidden ticket: edit says not found', $e === "Comment $c not found", $e); $e = $err(fn() => $tools->deleteComment($c)); $check('hidden ticket: delete says not found', $e === "Comment $c not found", $e); $check('hidden comment untouched', $row($c)['comment_text'] === 'confidential note', $row($c)); // An admin can edit anyone's comment, including a system comment with no user_id $as('ci.tester'); $sys = (int)$conn->query("SELECT comment_id FROM ticket_comments WHERE user_id IS NULL LIMIT 1")->fetch_row()[0]; $r = $tools->editComment($sys, 'edited by admin'); $check('admin edits a system comment', $r['edited'] === true && $row($sys)['comment_text'] === 'edited by admin', $r); $as('alice'); $e = $err(fn() => $tools->editComment($sys, 'x')); $check('alice cannot edit a system comment', $e === 'You do not have permission to edit this comment', $e); // Delete with nested replies $as('bob'); $r1 = $tools->addComment('100000001', 'reply 1', true, $a)['comment_id']; $as('alice'); $r2 = $tools->addComment('100000001', 'reply 2', true, $r1)['comment_id']; $r = $tools->deleteComment($a); $check( 'alice deletes her comment, 2 replies go with it', $r === ['comment_id' => $a, 'ticket_id' => '100000001', 'deleted' => true, 'replies_removed' => 2], $r ); $check('comment and replies gone', $row($a) === null && $row($r1) === null && $row($r2) === null); $check( 'delete audit has replies_removed', str_contains($audit('delete', $a)['details'] ?? '', '"replies_removed":2'), $audit('delete', $a) ); $e = $err(fn() => $tools->deleteComment($a)); $check('deleting again: not found', $e === "Comment $a not found", $e); $as('ci.tester'); $tools->deleteComment($c); echo "== links\n"; $conn->query( "DELETE FROM ticket_dependencies WHERE ticket_id IN ('100000001','100000002','100000003') OR depends_on_id IN ('100000001','100000002','100000003')" ); $as('alice'); $r = $tools->linkTickets('100000001', 'Blocks', ' 100000002 '); $check( 'link (relation case and id spaces normalised)', $r === ['ticket_id' => '100000001', 'relation' => 'blocks', 'other_ticket_id' => '100000002'], $r ); $e = $err(fn() => $tools->linkTickets('100000001', 'blocks', '100000002')); $check('duplicate link refused', $e !== null, $e); $e = $err(fn() => $tools->linkTickets('100000002', 'blocked_by', '100000001')); $check('inverse of an existing link refused', $e !== null, $e); $e = $err(fn() => $tools->linkTickets('100000001', 'causes', '100000002')); $check('bad relation message', $e === 'relation must be one of: blocks, blocked_by, relates_to, duplicates', $e); $e = $err(fn() => $tools->linkTickets('100000001', 'relates_to', '100000001')); $check('self link message', $e === 'A ticket cannot be linked to itself', $e); $e = $err(fn() => $tools->linkTickets('abc', 'relates_to', '100000001')); $check('non-digit id: not found', $e === 'Ticket abc not found', $e); $e = $err(fn() => $tools->linkTickets('100000001', 'relates_to', '100000009')); $check('hidden target: not found', $e === 'Ticket 100000009 not found', $e); $e = $err(fn() => $tools->linkTickets('999999998', 'relates_to', '100000001')); $check('missing source: not found', $e === 'Ticket 999999998 not found', $e); $r = $tools->unlinkTickets('100000002', 'blocked_by', '100000001'); $check('unlink stated from the other side', $r['removed'] === true, $r); $e = $err(fn() => $tools->unlinkTickets('100000001', 'blocks', '100000002')); $check('unlink again: no such link', $e === 'No blocks link between 100000001 and 100000002', $e); $tools->linkTickets('100000003', 'duplicates', '100000001'); $r = $tools->unlinkTickets('100000003', 'duplicates', '100000001'); $check('duplicates link unlinks (one-way relation)', $r['removed'] === true, $r); echo "\n$pass passed, $fail failed\n"; exit($fail || !$pass ? 1 : 0);