TenantAtlas/tests/Unit/BulkRestoreRunRestoreJobTest.php
Ahmed Darrazi e603a1245e feat: restore runs bulk archive/restore/force delete
- Add bulk restore + archived-only force delete actions
- Add jobs + tests for bulk restore/force delete
- Treat restore_run status 'partial' as deletable for hygiene
- Update feature tasks checklist
2025-12-25 02:59:31 +01:00

103 lines
2.9 KiB
PHP

<?php
use App\Jobs\BulkRestoreRunRestoreJob;
use App\Models\BackupSet;
use App\Models\BulkOperationRun;
use App\Models\RestoreRun;
use App\Models\Tenant;
use App\Models\User;
use App\Services\BulkOperationService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
uses(TestCase::class, RefreshDatabase::class);
test('bulk restore run restore restores archived runs', function () {
$tenant = Tenant::factory()->create(['is_current' => true]);
$user = User::factory()->create();
$backupSet = BackupSet::create([
'tenant_id' => $tenant->id,
'name' => 'Backup',
'status' => 'completed',
'item_count' => 0,
]);
$restoreRun = RestoreRun::create([
'tenant_id' => $tenant->id,
'backup_set_id' => $backupSet->id,
'status' => 'completed',
'is_dry_run' => true,
'requested_by' => 'tester@example.com',
]);
$restoreRun->delete();
expect($restoreRun->trashed())->toBeTrue();
$run = BulkOperationRun::factory()->create([
'tenant_id' => $tenant->id,
'user_id' => $user->id,
'resource' => 'restore_run',
'action' => 'restore',
'status' => 'pending',
'total_items' => 1,
'item_ids' => [$restoreRun->id],
'failures' => [],
]);
(new BulkRestoreRunRestoreJob($run->id))->handle(app(BulkOperationService::class));
$restoreRun->refresh();
expect($restoreRun->trashed())->toBeFalse();
$run->refresh();
expect($run->status)->toBe('completed')
->and($run->succeeded)->toBe(1)
->and($run->skipped)->toBe(0)
->and($run->failed)->toBe(0);
});
test('bulk restore run restore skips active runs', function () {
$tenant = Tenant::factory()->create(['is_current' => true]);
$user = User::factory()->create();
$backupSet = BackupSet::create([
'tenant_id' => $tenant->id,
'name' => 'Backup',
'status' => 'completed',
'item_count' => 0,
]);
$restoreRun = RestoreRun::create([
'tenant_id' => $tenant->id,
'backup_set_id' => $backupSet->id,
'status' => 'completed',
'is_dry_run' => true,
'requested_by' => 'tester@example.com',
]);
$run = BulkOperationRun::factory()->create([
'tenant_id' => $tenant->id,
'user_id' => $user->id,
'resource' => 'restore_run',
'action' => 'restore',
'status' => 'pending',
'total_items' => 1,
'item_ids' => [$restoreRun->id],
'failures' => [],
]);
(new BulkRestoreRunRestoreJob($run->id))->handle(app(BulkOperationService::class));
$restoreRun->refresh();
expect($restoreRun->trashed())->toBeFalse();
$run->refresh();
expect($run->status)->toBe('completed')
->and($run->succeeded)->toBe(0)
->and($run->skipped)->toBe(1)
->and($run->failed)->toBe(0);
expect(collect($run->failures)->pluck('reason')->all())->toContain('Not archived');
});