addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default'); $capsule->setAsGlobal(); $capsule->bootEloquent(); $schema = $capsule->getConnection('default')->getSchemaBuilder(); $schema->create('ticket', static function (Blueprint $table): void { $table->increments('id'); $table->text('title'); $table->text('content'); $table->integer('rootid'); $table->integer('userid'); $table->integer('datetime'); $table->integer('status'); $table->string('ticket_type'); $table->string('priority'); $table->integer('instance_id')->nullable(); $table->integer('last_activity_at')->default(0); $table->integer('last_message_id')->default(0); $table->integer('last_actor_id')->default(0); $table->boolean('last_actor_is_admin')->default(false); }); $schema->create('ticket_attachments', static function (Blueprint $table): void { $table->increments('id'); $table->integer('ticket_id'); $table->integer('message_id'); $table->integer('user_id'); $table->string('storage_key'); $table->string('original_name'); $table->string('mime_type'); $table->integer('bytes'); $table->string('sha256'); $table->integer('created_at'); }); $schema->create('ticket_write_requests', static function (Blueprint $table): void { $table->increments('id'); $table->integer('actor_user_id'); $table->string('request_key'); $table->string('request_hash'); $table->string('operation'); $table->integer('ticket_id')->nullable(); $table->integer('message_id')->nullable(); $table->integer('created_at'); $table->unique(['actor_user_id', 'request_key']); }); $schema->create('ticket_user_guards', static function (Blueprint $table): void { $table->integer('user_id')->primary(); $table->integer('updated_at'); }); $schema->create('ticket_read_states', static function (Blueprint $table): void { $table->integer('ticket_id'); $table->integer('reader_id'); $table->integer('last_read_message_id'); $table->integer('read_at'); $table->primary(['ticket_id', 'reader_id']); }); $schema->create('cloud_instances', static function (Blueprint $table): void { $table->increments('id'); $table->integer('user_id'); $table->string('status'); }); $now = time(); for ($id = 1; $id <= 5; $id += 1) { Capsule::table('ticket')->insert([ 'id' => $id, 'title' => 'Open ' . $id, 'content' => 'x', 'rootid' => 0, 'userid' => 7, 'datetime' => $now - $id, 'status' => 1, 'ticket_type' => 'technical', 'priority' => 'medium', 'instance_id' => null, ]); } $service = new TicketThreadService(); try { $service->open(new OpenTicketCommand( 7, 'Sixth open ticket', 'Must fail', 'technical', 'medium', null, $emptyRequest(), 'policy-open-limit-1' )); throw new RuntimeException('The sixth open ticket unexpectedly succeeded.'); } catch (InvalidArgumentException $exception) { $assert(str_contains($exception->getMessage(), '处理中工单'), 'Open-ticket limit returned an unclear error.'); } $assert(Capsule::table('ticket')->count() === 5 && Capsule::table('ticket_write_requests')->count() === 0, 'A rejected policy write left a ticket or idempotency intent behind.'); Capsule::table('ticket')->where('userid', 7)->update(['status' => 0]); try { $service->open(new OpenTicketCommand( 7, 'Rate limited', 'Must fail', 'technical', 'medium', null, $emptyRequest(), 'policy-rate-limit-1' )); throw new RuntimeException('The create rate limit was not enforced.'); } catch (InvalidArgumentException $exception) { $assert(str_contains($exception->getMessage(), '提交过于频繁'), 'Create rate limit returned an unclear error.'); } try { $service->open(new OpenTicketCommand( 8, str_repeat('题', 121), 'Must fail', 'technical', 'medium', null, $emptyRequest(), 'policy-title-limit-1' )); throw new RuntimeException('An overlong title unexpectedly succeeded.'); } catch (InvalidArgumentException $exception) { $assert(str_contains($exception->getMessage(), '120'), 'Title length error omitted its limit.'); } // Administrators remain exempt from user reply rate and account attachment // quota. Field and per-request file safety limits still apply. Capsule::table('ticket')->insert([ 'id' => 20, 'title' => 'Root', 'content' => 'x', 'rootid' => 0, 'userid' => 8, 'datetime' => $now - 5000, 'status' => 1, 'ticket_type' => 'technical', 'priority' => 'medium', 'instance_id' => null, ]); for ($id = 21; $id <= 50; $id += 1) { Capsule::table('ticket')->insert([ 'id' => $id, 'title' => 'Root', 'content' => 'admin', 'rootid' => 20, 'userid' => 9, 'datetime' => $now - 10, 'status' => 1, 'ticket_type' => 'technical', 'priority' => 'medium', 'instance_id' => null, ]); } $adminReply = $service->reply(new ReplyTicketCommand( 20, 9, 'Allowed administrator reply', 1, $emptyRequest(), null, true, 'policy-admin-reply-1' )); $assert($adminReply !== null && $adminReply->wasCreated, 'Administrator reply was incorrectly blocked by user rate policy.'); $png = base64_decode( 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk' . 'YAAAAAYAAjCB0C8AAAAASUVORK5CYII=', true ); $uploadPath = tempnam(sys_get_temp_dir(), 'ticket-policy-upload-'); if ($uploadPath === false || file_put_contents($uploadPath, $png) === false) { throw new RuntimeException('Could not create quota upload fixture.'); } $uploadRequest = new class($uploadPath) { public function __construct(private string $path) {} public function getUploadedFiles(): array { return ['attachments' => [[ 'name' => 'proof.png', 'tmp_name' => $this->path, 'error' => UPLOAD_ERR_OK, 'size' => filesize($this->path), 'type' => 'image/png', ]]]; } }; Capsule::table('ticket_attachments')->insert([ 'ticket_id' => 999, 'message_id' => 999, 'user_id' => 11, 'storage_key' => 'existing.png', 'original_name' => 'existing.png', 'mime_type' => 'image/png', 'bytes' => 100 * 1024 * 1024, 'sha256' => str_repeat('a', 64), 'created_at' => $now, ]); try { $service->open(new OpenTicketCommand( 11, 'Quota', 'Must fail', 'technical', 'medium', null, $uploadRequest, 'policy-quota-limit-1' )); throw new RuntimeException('Attachment account quota was not enforced.'); } catch (InvalidArgumentException $exception) { $assert(str_contains($exception->getMessage(), '100 MiB'), 'Attachment quota returned an unclear error.'); } $assert(Capsule::table('ticket')->where('userid', 11)->count() === 0 && Capsule::table('ticket_write_requests')->where('actor_user_id', 11)->count() === 0, 'An attachment-quota rejection left durable write state behind.'); @unlink($uploadPath); echo "Ticket usage policy checks passed.\n";