|
|
|
|
@ -8,6 +8,13 @@ use Illuminate\Database\Capsule\Manager as Capsule;
|
|
|
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
|
|
|
|
|
|
|
|
$System_Config = ['enable_log' => 'false'];
|
|
|
|
|
if (!defined('BASE_PATH')) {
|
|
|
|
|
define('BASE_PATH', sys_get_temp_dir() . '/virt-cloud-account-delete-test-' . bin2hex(random_bytes(6)));
|
|
|
|
|
}
|
|
|
|
|
$attachmentDirectory = BASE_PATH . '/storage/ticket-attachments';
|
|
|
|
|
if (!mkdir($attachmentDirectory, 0700, true) && !is_dir($attachmentDirectory)) {
|
|
|
|
|
throw new RuntimeException('Could not create account deletion attachment fixture directory.');
|
|
|
|
|
}
|
|
|
|
|
$assert = static function ($condition, $message) {
|
|
|
|
|
if (!$condition) {
|
|
|
|
|
throw new RuntimeException($message);
|
|
|
|
|
@ -30,6 +37,13 @@ $schema->create('ticket', static function (Blueprint $table) {
|
|
|
|
|
$table->integer('userid');
|
|
|
|
|
$table->integer('rootid')->default(0);
|
|
|
|
|
});
|
|
|
|
|
$schema->create('ticket_attachments', static function (Blueprint $table) {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->integer('ticket_id');
|
|
|
|
|
$table->integer('message_id');
|
|
|
|
|
$table->integer('user_id');
|
|
|
|
|
$table->string('storage_key');
|
|
|
|
|
});
|
|
|
|
|
$schema->create('yipay_orders', static function (Blueprint $table) {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->integer('user_id');
|
|
|
|
|
@ -124,6 +138,24 @@ Capsule::table('ticket')->insert([
|
|
|
|
|
['id' => 11, 'userid' => 2, 'rootid' => 10],
|
|
|
|
|
['id' => 20, 'userid' => 3, 'rootid' => 0],
|
|
|
|
|
]);
|
|
|
|
|
$attachmentFiles = [
|
|
|
|
|
'deleted-user.png' => 'deleted user attachment',
|
|
|
|
|
'deleted-admin-reply.png' => 'administrator reply attachment',
|
|
|
|
|
'retained-user.png' => 'retained user attachment',
|
|
|
|
|
];
|
|
|
|
|
foreach ($attachmentFiles as $storageKey => $contents) {
|
|
|
|
|
if (file_put_contents($attachmentDirectory . '/' . $storageKey, $contents) === false) {
|
|
|
|
|
throw new RuntimeException('Could not create ticket attachment fixture.');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Capsule::table('ticket_attachments')->insert([
|
|
|
|
|
['id' => 1, 'ticket_id' => 10, 'message_id' => 10, 'user_id' => 1,
|
|
|
|
|
'storage_key' => 'deleted-user.png'],
|
|
|
|
|
['id' => 2, 'ticket_id' => 10, 'message_id' => 11, 'user_id' => 2,
|
|
|
|
|
'storage_key' => 'deleted-admin-reply.png'],
|
|
|
|
|
['id' => 3, 'ticket_id' => 20, 'message_id' => 20, 'user_id' => 3,
|
|
|
|
|
'storage_key' => 'retained-user.png'],
|
|
|
|
|
]);
|
|
|
|
|
Capsule::table('yipay_orders')->insert([
|
|
|
|
|
['user_id' => 1, 'order_no' => 'deleted-order'],
|
|
|
|
|
['user_id' => 3, 'order_no' => 'retained-order'],
|
|
|
|
|
@ -209,6 +241,12 @@ $assert($result['skipped_user_ids'] === [2, 999], 'Administrator or missing acco
|
|
|
|
|
$assert(User::find(1) === null && User::find(2) !== null && User::find(3) !== null, 'User deletion was not isolated.');
|
|
|
|
|
$assert((int) User::find(3)->ref_by === 0, 'Deleted referrer was not cleared.');
|
|
|
|
|
$assert(Capsule::table('ticket')->pluck('id')->all() === [20], 'Ticket replies from the deleted thread were retained.');
|
|
|
|
|
$assert(Capsule::table('ticket_attachments')->pluck('id')->all() === [3],
|
|
|
|
|
'Ticket attachment deletion was not isolated.');
|
|
|
|
|
$assert(!is_file($attachmentDirectory . '/deleted-user.png')
|
|
|
|
|
&& !is_file($attachmentDirectory . '/deleted-admin-reply.png')
|
|
|
|
|
&& is_file($attachmentDirectory . '/retained-user.png'),
|
|
|
|
|
'Ticket attachment files were not cleaned after a committed account deletion.');
|
|
|
|
|
foreach (['yipay_orders', 'yipay_callback_events', 'yipay_refunds'] as $tableName) {
|
|
|
|
|
$assert(Capsule::table($tableName)->pluck('order_no')->all() === ['retained-order'], 'Payment relation deletion was not isolated.');
|
|
|
|
|
}
|
|
|
|
|
@ -312,4 +350,44 @@ $assert(User::find(6) !== null
|
|
|
|
|
&& $fencedJob->sealed_payload === 'unknown-effect-create-audit',
|
|
|
|
|
'Unknown-effect terminal provisioning fence lost its order or job audit.');
|
|
|
|
|
|
|
|
|
|
Capsule::table('user')->insert([
|
|
|
|
|
'id' => 7, 'email' => 'rollback-attachment@example.com',
|
|
|
|
|
'is_admin' => 0, 'ref_by' => 0,
|
|
|
|
|
]);
|
|
|
|
|
Capsule::table('ticket')->insert([
|
|
|
|
|
'id' => 70, 'userid' => 7, 'rootid' => 0,
|
|
|
|
|
]);
|
|
|
|
|
Capsule::table('ticket_attachments')->insert([
|
|
|
|
|
'id' => 7, 'ticket_id' => 70, 'message_id' => 70, 'user_id' => 7,
|
|
|
|
|
'storage_key' => 'rollback-user.png',
|
|
|
|
|
]);
|
|
|
|
|
$rollbackAttachment = $attachmentDirectory . '/rollback-user.png';
|
|
|
|
|
if (file_put_contents($rollbackAttachment, 'rollback attachment') === false) {
|
|
|
|
|
throw new RuntimeException('Could not create rollback attachment fixture.');
|
|
|
|
|
}
|
|
|
|
|
Capsule::statement(
|
|
|
|
|
"CREATE TRIGGER prevent_user_7_delete BEFORE DELETE ON user "
|
|
|
|
|
. "WHEN OLD.id = 7 BEGIN SELECT RAISE(ABORT, 'forced user deletion failure'); END"
|
|
|
|
|
);
|
|
|
|
|
try {
|
|
|
|
|
(new UserDataDeletionService())->deleteUsers([7], false);
|
|
|
|
|
throw new RuntimeException('Forced account deletion failure did not abort.');
|
|
|
|
|
} catch (Throwable $error) {
|
|
|
|
|
$assert(str_contains($error->getMessage(), 'forced user deletion failure'),
|
|
|
|
|
'Forced account deletion returned the wrong error.');
|
|
|
|
|
}
|
|
|
|
|
$assert(User::find(7) !== null
|
|
|
|
|
&& Capsule::table('ticket')->where('id', 70)->exists()
|
|
|
|
|
&& Capsule::table('ticket_attachments')->where('id', 7)->exists(),
|
|
|
|
|
'Database rows did not roll back after the forced account deletion failure.');
|
|
|
|
|
$assert(is_file($rollbackAttachment),
|
|
|
|
|
'Ticket attachment file was deleted before the account transaction committed.');
|
|
|
|
|
|
|
|
|
|
Capsule::statement('DROP TRIGGER prevent_user_7_delete');
|
|
|
|
|
@unlink($rollbackAttachment);
|
|
|
|
|
@unlink($attachmentDirectory . '/retained-user.png');
|
|
|
|
|
@rmdir($attachmentDirectory);
|
|
|
|
|
@rmdir(BASE_PATH . '/storage');
|
|
|
|
|
@rmdir(BASE_PATH);
|
|
|
|
|
|
|
|
|
|
echo 'Manual account deletion SQLite checks passed.' . PHP_EOL;
|
|
|
|
|
|