You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/tests/TicketReadModelTest.php

148 lines
5.8 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Ticket\TicketReadModel;
use App\Services\Ticket\TicketThreadQuery;
use App\Services\Ticket\TicketViewer;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;
$assert = static function ($condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$capsule = new Capsule();
$capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default');
$capsule->setAsGlobal();
$capsule->bootEloquent();
$schema = $capsule->getConnection('default')->getSchemaBuilder();
$schema->create('user', static function (Blueprint $table): void {
$table->increments('id');
$table->string('user_name');
$table->boolean('is_admin')->default(false);
});
$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');
});
$schema->create('cloud_instances', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('user_id');
$table->string('name');
$table->string('status')->default('active');
$table->text('spec_json');
});
Capsule::table('user')->insert([
['id' => 7, 'user_name' => 'customer', 'is_admin' => 0],
['id' => 9, 'user_name' => 'operator', 'is_admin' => 1],
]);
Capsule::table('cloud_instances')->insert([
'id' => 33, 'user_id' => 7, 'name' => 'vm-support', 'status' => 'active',
'spec_json' => json_encode(['public_network' => [
'contract' => 1, 'network_selector' => 'bridge:br0',
'ipv4' => [
'pool_key' => str_repeat('1', 32), 'pool_cidr' => '8.8.8.0/24',
'address_cidr' => '8.8.8.7/24', 'gateway' => '8.8.8.1',
'gateway_on_link' => false,
],
'ipv6' => [
'pool_key' => str_repeat('2', 32),
'pool_cidr' => '2404:c140:156:3a::/64',
'address_cidr' => '2404:c140:156:3a::7/64',
'gateway' => '2404:c140:156::1', 'gateway_on_link' => true,
],
'dns_servers' => ['1.1.1.1', '2606:4700:4700::1111'],
]], JSON_THROW_ON_ERROR),
]);
$messages = [[
'id' => 1, 'title' => 'Cannot connect', 'content' => 'password=secret',
'rootid' => 0, 'userid' => 7, 'datetime' => 100, 'status' => 1,
'ticket_type' => 'technical', 'priority' => 'high', 'instance_id' => 33,
]];
for ($id = 2; $id <= 12; $id += 1) {
$messages[] = [
'id' => $id, 'title' => 'Cannot connect', 'content' => 'Message ' . $id,
'rootid' => 1, 'userid' => $id % 2 === 0 ? 9 : 7,
'datetime' => 100 + $id, 'status' => 1,
'ticket_type' => 'technical', 'priority' => 'high', 'instance_id' => null,
];
}
Capsule::table('ticket')->insert($messages);
Capsule::table('ticket_attachments')->insert([
'ticket_id' => 1, 'message_id' => 2, 'user_id' => 9,
'storage_key' => 'a.png', 'original_name' => 'proof.png',
'mime_type' => 'image/png', 'bytes' => 1536,
'sha256' => str_repeat('a', 64), 'created_at' => 102,
]);
$queryCount = 0;
$capsule->getConnection('default')->listen(static function () use (&$queryCount): void {
$queryCount += 1;
});
$model = new TicketReadModel();
$thread = $model->thread(
new TicketThreadQuery(1, 1, 20),
TicketViewer::user(7)
);
$assert(is_array($thread), 'The owner could not read the ticket thread.');
$assert($thread['messages']->total() === 12, 'The thread did not return every message.');
$assert($queryCount <= 7, 'Thread projection query count grows past the fixed read budget.');
$assert(($thread['instance']->name ?? null) === 'vm-support'
&& ($thread['instance']->public_ipv4 ?? null) === '8.8.8.7'
&& ($thread['instance']->public_ipv6 ?? null) === '2404:c140:156:3a::7'
&& ($thread['instance']->detail_url ?? null) === '/user/cloud/instances/33',
'The related instance is not projected with its owner-safe detail link.');
$adminThread = $model->thread(
new TicketThreadQuery(1, 1, 20),
TicketViewer::administrator(9, true)
);
$assert(($adminThread['instance']->detail_url ?? null) === '/admin/cloud/instances?q=33',
'The administrator instance projection does not link to the reachable inventory page.');
$items = $thread['messages']->items();
$assert($items[0]->id === 12 && $items[0]->author_name === '管理员'
&& $items[0]->author_kind === 'admin',
'The latest administrator reply was not projected first.');
$assert($items[0]->datetime_epoch === 112
&& $items[0]->datetime_iso === '1970-01-01T00:01:52+00:00',
'Ticket messages do not expose a canonical timestamp for browser-local display.');
$messageTwo = null;
foreach ($items as $item) {
if ($item->id === 2) {
$messageTwo = $item;
break;
}
}
$assert($messageTwo !== null && count($messageTwo->attachments) === 1,
'The owner cannot see an attachment in their own thread.');
$assert($messageTwo->attachments[0]->bytes_display === '1.5 KiB',
'Attachment size is not projected in a human-readable form.');
$assert($model->thread(new TicketThreadQuery(1), TicketViewer::user(8)) === null,
'A foreign user can read another user\'s thread.');
echo "Ticket read model checks passed.\n";