TenantAtlas/apps/platform/tests/Feature/PortfolioCompare/CrossTenantPromotionExecutionRunUxTest.php
ahmido 11247c1537 Add cross-tenant promotion execution (spec 264) (#320)
Automated PR created by Copilot: adds implementation and tests for specs/264 cross-tenant promotion execution.

Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de>
Reviewed-on: #320
2026-05-02 14:38:20 +00:00

69 lines
2.7 KiB
PHP

<?php
declare(strict_types=1);
use App\Filament\Pages\CrossTenantComparePage;
use App\Jobs\Operations\CrossTenantPromotionExecutionJob;
use App\Models\OperationRun;
use App\Support\OperationRunLinks;
use App\Support\Workspaces\WorkspaceContext;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Queue;
use Livewire\Livewire;
use Tests\Feature\Concerns\BuildsPortfolioCompareFixtures;
uses(RefreshDatabase::class, BuildsPortfolioCompareFixtures::class);
it('uses the shared queued and deduped start-result ux for promotion execution', function (): void {
Queue::fake();
$fixture = $this->makeCrossTenantCompareFixture();
$this->createPortfolioCompareSubject(
tenant: $fixture['sourceTenant'],
displayName: 'Run UX Policy',
snapshot: ['settings' => [['key' => 'ux', 'value' => 1]]],
);
$this->setAdminWorkspaceContext($fixture['user'], $fixture['workspace']);
$query = [
'source_tenant_id' => (int) $fixture['sourceTenant']->getKey(),
'target_tenant_id' => (int) $fixture['targetTenant']->getKey(),
'policy_type' => ['deviceConfiguration'],
];
$component = Livewire::withQueryParams($query)
->actingAs($fixture['user'])
->test(CrossTenantComparePage::class)
->call('generatePromotionPreflight')
->mountAction('executePromotion')
->callMountedAction()
->assertHasNoActionErrors()
->assertNotified('Promotion execution queued');
$run = OperationRun::query()->latest('id')->first();
expect($run)->not->toBeNull()
->and($run?->type)->toBe('promotion.execute')
->and(data_get($run?->context, 'required_capability'))->toBe('tenant.manage')
->and(data_get($run?->context, 'workspace_required_capability'))->toBe('workspace_baselines.manage')
->and(data_get($run?->context, 'target_scope.workspace_id'))->toBe((int) $fixture['workspace']->getKey())
->and(data_get($run?->context, 'selection.sourceTenantId'))->toBe((int) $fixture['sourceTenant']->getKey())
->and(data_get($run?->context, 'selection.targetTenantId'))->toBe((int) $fixture['targetTenant']->getKey());
$component
->mountAction('executePromotion')
->callMountedAction()
->assertHasNoActionErrors()
->assertNotified('Promotion execution already running');
expect(OperationRun::query()->where('type', 'promotion.execute')->count())->toBe(1);
Queue::assertPushed(CrossTenantPromotionExecutionJob::class, 1);
$this->actingAs($fixture['user'])
->withSession([WorkspaceContext::SESSION_KEY => (int) $fixture['workspace']->getKey()])
->get(OperationRunLinks::tenantlessView($run))
->assertOk();
});