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.
102 lines
5.6 KiB
PHP
102 lines
5.6 KiB
PHP
<?php
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Services\Ticket\TicketQueueQuery;
|
|
use App\Services\Ticket\TicketReadCommand;
|
|
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');
|
|
});
|
|
$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');
|
|
$table->integer('last_message_id'); $table->integer('last_actor_id');
|
|
$table->boolean('last_actor_is_admin');
|
|
});
|
|
$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_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']);
|
|
});
|
|
Capsule::table('user')->insert([
|
|
['id' => 7, 'user_name' => 'owner', 'is_admin' => 0],
|
|
['id' => 9, 'user_name' => 'admin-one', 'is_admin' => 1],
|
|
['id' => 10, 'user_name' => 'admin-two', 'is_admin' => 1],
|
|
]);
|
|
Capsule::table('ticket')->insert([
|
|
['id' => 1, 'title' => 'Help', 'content' => 'root', 'rootid' => 0, 'userid' => 7,
|
|
'datetime' => 100, 'status' => 1, 'ticket_type' => 'technical', 'priority' => 'medium',
|
|
'instance_id' => null, 'last_activity_at' => 120, 'last_message_id' => 3,
|
|
'last_actor_id' => 7, 'last_actor_is_admin' => 0],
|
|
['id' => 2, 'title' => 'Help', 'content' => 'admin', 'rootid' => 1, 'userid' => 9,
|
|
'datetime' => 110, 'status' => 1, 'ticket_type' => 'technical', 'priority' => 'medium',
|
|
'instance_id' => null, 'last_activity_at' => 0, 'last_message_id' => 0,
|
|
'last_actor_id' => 0, 'last_actor_is_admin' => 0],
|
|
['id' => 3, 'title' => 'Help', 'content' => 'owner', 'rootid' => 1, 'userid' => 7,
|
|
'datetime' => 120, 'status' => 1, 'ticket_type' => 'technical', 'priority' => 'medium',
|
|
'instance_id' => null, 'last_activity_at' => 0, 'last_message_id' => 0,
|
|
'last_actor_id' => 0, 'last_actor_is_admin' => 0],
|
|
]);
|
|
Capsule::table('ticket_read_states')->insert([
|
|
['ticket_id' => 1, 'reader_id' => 7, 'last_read_message_id' => 3, 'read_at' => 120],
|
|
['ticket_id' => 1, 'reader_id' => 9, 'last_read_message_id' => 2, 'read_at' => 110],
|
|
]);
|
|
|
|
$model = new TicketReadModel();
|
|
$adminOne = TicketViewer::administrator(9, true);
|
|
$adminTwo = TicketViewer::administrator(10, true);
|
|
$queueOne = $model->queue(new TicketQueueQuery(), $adminOne)['tickets']->items();
|
|
$queueTwo = $model->queue(new TicketQueueQuery(), $adminTwo)['tickets']->items();
|
|
$assert((int) $queueOne[0]->is_unread === 1 && (int) $queueTwo[0]->is_unread === 1,
|
|
'Independent administrators did not receive their own unread state.');
|
|
|
|
$model->thread(new TicketThreadQuery(1), $adminOne);
|
|
$assert((int) Capsule::table('ticket_read_states')->where('ticket_id', 1)
|
|
->where('reader_id', 9)->value('last_read_message_id') === 2,
|
|
'Reading a GET projection mutated the administrator read cursor.');
|
|
$assert($model->markRead(new TicketReadCommand(1), $adminOne),
|
|
'Explicit read acknowledgement failed.');
|
|
$queueOne = $model->queue(new TicketQueueQuery(), $adminOne)['tickets']->items();
|
|
$queueTwo = $model->queue(new TicketQueueQuery(), $adminTwo)['tickets']->items();
|
|
$assert((int) $queueOne[0]->is_unread === 0,
|
|
'The acknowledging administrator is still unread.');
|
|
$assert((int) $queueTwo[0]->is_unread === 1,
|
|
'One administrator acknowledgement cleared another administrator.');
|
|
|
|
$routes = file_get_contents(dirname(__DIR__) . '/config/routes.php');
|
|
$userController = file_get_contents(dirname(__DIR__) . '/app/Controllers/UserController.php');
|
|
$adminController = file_get_contents(dirname(__DIR__) . '/app/Controllers/Admin/TicketController.php');
|
|
$javascript = file_get_contents(dirname(__DIR__) . '/public/theme/venix/js/ticket-page.js');
|
|
$assert(str_contains($routes, "post('/ticket/{id}/read'")
|
|
&& str_contains($userController, 'function ticket_mark_read')
|
|
&& str_contains($adminController, 'function markRead'),
|
|
'Explicit user and administrator read acknowledgement routes are incomplete.');
|
|
$assert(str_contains($userController, 'AdminCsrf::verify($request)')
|
|
&& str_contains($adminController, 'AdminCsrf::verify($request)')
|
|
&& str_contains($javascript, '"/read"'),
|
|
'Read acknowledgement is not protected by CSRF or used by the user page.');
|
|
|
|
echo "Ticket per-reader state checks passed.\n";
|