Files
portal/app/tests/Feature/Plan5/Projects/ProjectsActionsTest.php
T

113 lines
4.2 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
use App\Jobs\SyncSupplierProjectJob;
use App\Models\Project;
use App\Models\Tenant;
use App\Models\User;
use Illuminate\Support\Facades\Queue;
beforeEach(fn () => Queue::fake());
it('destroy archives project (sets archived_at, is_active=false)', function () {
$tenant = Tenant::factory()->create();
$user = User::factory()->create(['tenant_id' => $tenant->id]);
$project = Project::factory()->create(['tenant_id' => $tenant->id, 'is_active' => true]);
$this->actingAs($user)->deleteJson("/api/projects/{$project->id}")->assertNoContent();
$project->refresh();
expect($project->is_active)->toBeFalse();
expect($project->archived_at)->not->toBeNull();
});
it('destroy returns 409 if already archived', function () {
$tenant = Tenant::factory()->create();
$user = User::factory()->create(['tenant_id' => $tenant->id]);
$project = Project::factory()->create(['tenant_id' => $tenant->id, 'archived_at' => now()]);
$this->actingAs($user)->deleteJson("/api/projects/{$project->id}")->assertStatus(409);
});
it('sync re-dispatches SyncSupplierProjectJob', function () {
$tenant = Tenant::factory()->create();
$user = User::factory()->create(['tenant_id' => $tenant->id]);
$project = Project::factory()->create(['tenant_id' => $tenant->id]);
$this->actingAs($user)->postJson("/api/projects/{$project->id}/sync")
->assertStatus(202)
->assertJsonPath('queued', true);
Queue::assertPushed(SyncSupplierProjectJob::class);
});
it('toggle-active flips is_active flag', function () {
$tenant = Tenant::factory()->create();
$user = User::factory()->create(['tenant_id' => $tenant->id]);
$project = Project::factory()->create(['tenant_id' => $tenant->id, 'is_active' => true]);
$this->actingAs($user)->patchJson("/api/projects/{$project->id}/toggle-active", ['is_active' => false])
->assertOk();
expect($project->fresh()->is_active)->toBeFalse();
});
it('bulk pause sets is_active=false on multiple', function () {
$tenant = Tenant::factory()->create();
$user = User::factory()->create(['tenant_id' => $tenant->id]);
$p1 = Project::factory()->create(['tenant_id' => $tenant->id, 'is_active' => true]);
$p2 = Project::factory()->create(['tenant_id' => $tenant->id, 'is_active' => true]);
$this->actingAs($user)->postJson('/api/projects/bulk', [
'action' => 'pause', 'ids' => [$p1->id, $p2->id],
])->assertOk()->assertJsonPath('updated', 2);
expect($p1->fresh()->is_active)->toBeFalse();
expect($p2->fresh()->is_active)->toBeFalse();
});
it('bulk filters out cross-tenant ids silently', function () {
$tenantA = Tenant::factory()->create();
$tenantB = Tenant::factory()->create();
$userA = User::factory()->create(['tenant_id' => $tenantA->id]);
$pA = Project::factory()->create(['tenant_id' => $tenantA->id, 'is_active' => true]);
$pB = Project::factory()->create(['tenant_id' => $tenantB->id, 'is_active' => true]);
$this->actingAs($userA)->postJson('/api/projects/bulk', [
'action' => 'pause', 'ids' => [$pA->id, $pB->id],
])->assertOk()->assertJsonPath('updated', 1);
expect($pB->fresh()->is_active)->toBeTrue();
});
it('bulk archive sets archived_at on multiple', function () {
$tenant = Tenant::factory()->create();
$user = User::factory()->create(['tenant_id' => $tenant->id]);
$p1 = Project::factory()->create(['tenant_id' => $tenant->id]);
$this->actingAs($user)->postJson('/api/projects/bulk', [
'action' => 'archive', 'ids' => [$p1->id],
])->assertOk();
expect($p1->fresh()->archived_at)->not->toBeNull();
});
it('bulk rejects > 500 ids', function () {
$tenant = Tenant::factory()->create();
$user = User::factory()->create(['tenant_id' => $tenant->id]);
$this->actingAs($user)->postJson('/api/projects/bulk', [
'action' => 'pause', 'ids' => range(1, 501),
])->assertStatus(422);
});
it('bulk rejects unknown action', function () {
$tenant = Tenant::factory()->create();
$user = User::factory()->create(['tenant_id' => $tenant->id]);
$this->actingAs($user)->postJson('/api/projects/bulk', [
'action' => 'destroy_all', 'ids' => [1],
])->assertStatus(422);
});