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')->default(0); $table->integer('userid'); $table->integer('datetime'); $table->integer('status')->default(1); $table->string('ticket_type')->default('technical'); $table->string('priority')->default('medium'); $table->integer('instance_id')->nullable(); }); $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'); }); Capsule::table('ticket')->insert([ 'id' => 1, 'title' => 'Root ticket', 'content' => 'Initial message', 'rootid' => 0, 'userid' => 7, 'datetime' => time(), 'status' => 1, 'ticket_type' => 'technical', 'priority' => 'medium', ]); $png = base64_decode( 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk' . 'YAAAAAYAAjCB0C8AAAAASUVORK5CYII=', true ); $uploaded = static function (string $name, string $bytes): array { $path = tempnam(sys_get_temp_dir(), 'ticket-upload-'); if ($path === false || file_put_contents($path, $bytes) === false) { throw new RuntimeException('Could not create uploaded fixture.'); } return [ 'name' => $name, 'tmp_name' => $path, 'error' => UPLOAD_ERR_OK, 'size' => filesize($path), 'type' => 'image/png', ]; }; $request = static function (array $files): object { return new class($files) { public function __construct(private array $files) {} public function getUploadedFiles(): array { return ['attachments' => $this->files]; } }; }; // A later invalid attachment must roll back the root status and reply and // remove the first file that was already moved into private storage. $first = $uploaded('first.png', $png); $secondPath = tempnam(sys_get_temp_dir(), 'ticket-upload-invalid-'); if ($secondPath === false || file_put_contents($secondPath, 'not an image') === false) { throw new RuntimeException('Could not create invalid upload fixture.'); } $second = [ 'name' => 'second.png', 'tmp_name' => $secondPath, 'error' => UPLOAD_ERR_OK, 'size' => filesize($secondPath), 'type' => 'image/png', ]; try { TicketReplyService::create(1, 7, 'reply that must roll back', 0, $request([$first, $second]), 7); throw new RuntimeException('Invalid attachment unexpectedly succeeded.'); } catch (InvalidArgumentException $exception) { // Expected validation failure; the assertions below verify the rollback. } $assert((int) Capsule::table('ticket')->where('id', 1)->value('status') === 1, 'Root status changed after attachment validation failure.'); $assert(Capsule::table('ticket')->where('rootid', 1)->count() === 0, 'Reply row survived attachment validation failure.'); $assert(Capsule::table('ticket_attachments')->count() === 0, 'Attachment row survived transaction rollback.'); $assert(glob(BASE_PATH . '/storage/ticket-attachments/*') === [], 'A partially stored attachment file survived rollback.'); @unlink($secondPath); $result = TicketReplyService::create(1, 7, 'successful reply', 0, $request([$uploaded('ok.png', $png)]), 7); $assert(is_array($result) && $result['reply']->rootid == 1, 'Successful reply was not created.'); $assert((int) Capsule::table('ticket')->where('id', 1)->value('status') === 0, 'Root status was not updated by a successful reply.'); $assert(Capsule::table('ticket')->where('rootid', 1)->count() === 1, 'Successful reply row is missing.'); $assert(Capsule::table('ticket_attachments')->count() === 1, 'Successful attachment row is missing.'); $attachment = Capsule::table('ticket_attachments')->first(); $assert((string) $attachment->original_name === 'ok.png', 'Successful attachment did not preserve its sanitized original name.'); $assert(is_file(BASE_PATH . '/storage/ticket-attachments/' . $attachment->storage_key), 'Successful attachment file is missing.'); // The lock must be acquired in the transaction, not by a preflight query. $serviceSource = file_get_contents(dirname(__DIR__) . '/app/Services/TicketReplyService.php'); $assert(str_contains($serviceSource, 'transaction(function') && str_contains($serviceSource, 'lockForUpdate()->first()') && str_contains($serviceSource, 'TicketAttachmentService::store'), 'Reply service does not keep root locking and attachment persistence in one transaction.'); $attachmentSource = file_get_contents(dirname(__DIR__) . '/app/Services/TicketAttachmentService.php'); $assert(str_contains($attachmentSource, '$paths[] = $path') && str_contains($attachmentSource, 'self::discardPaths($paths)'), 'Attachment writes do not register and clean up every newly-created path.'); foreach ([ dirname(__DIR__) . '/app/Controllers/UserController.php', dirname(__DIR__) . '/app/Controllers/Admin/TicketController.php', ] as $controllerPath) { $controllerSource = file_get_contents($controllerPath); $assert(str_contains($controllerSource, 'TicketReplyService::create') && !str_contains($controllerSource, '$previousStatus'), 'A ticket controller still uses the stale-status compensation flow.'); } TicketAttachmentService::discardPaths([ BASE_PATH . '/storage/ticket-attachments/' . $attachment->storage_key, ]); Capsule::table('ticket_attachments')->delete(); Capsule::table('ticket')->where('rootid', 1)->delete(); @rmdir(BASE_PATH . '/storage/ticket-attachments'); @rmdir(BASE_PATH . '/storage'); @rmdir(BASE_PATH); echo "Ticket reply transaction and attachment cleanup checks passed.\n";