Files
portal/app/tests/Feature/Plan5/Projects/ProjectsActionsTest.php
T
Дмитрий 7408bc4232 feat(projects): hard delete with deals-guard, replace archive
- ProjectService: add delete() with DB-level deals check (bypasses SoftDeletes
  scope via DB::table), captures supplier pivot IDs before cascade, dispatches
  DeleteSupplierProjectJob; add bulkDelete() private method; replace archive
  match arm with delete; remove archive() method
- ProjectController: destroy() calls delete() not archive(); update docblocks
- BulkProjectActionRequest: replace 'archive' with 'delete' in Rule::in for action
- Tests: ProjectDeleteTest (2 new TDD tests), ProjectsActionsTest updated
  (destroy → hard delete, 409-already-archived → 422-has-deals, bulk archive → bulk delete)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-21 08:08:33 +03:00

119 lines
4.4 KiB
PHP

<?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\DB;
use Illuminate\Support\Facades\Queue;
beforeEach(fn () => Queue::fake());
it('destroy hard-deletes a project with no deals', 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();
expect(Project::find($project->id))->toBeNull();
});
it('destroy returns 422 if project has deals', function () {
$tenant = Tenant::factory()->create();
$user = User::factory()->create(['tenant_id' => $tenant->id]);
$project = Project::factory()->create(['tenant_id' => $tenant->id, 'is_active' => true]);
DB::table('deals')->insert([
'tenant_id' => $tenant->id, 'project_id' => $project->id,
'phone' => '79990001100', 'status' => 'new',
'received_at' => now(), 'created_at' => now(),
]);
$this->actingAs($user)->deleteJson("/api/projects/{$project->id}")->assertStatus(422);
expect(Project::find($project->id))->not->toBeNull();
});
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 delete removes project with no deals', 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' => 'delete', 'ids' => [$p1->id],
])->assertOk()->assertJsonPath('updated', 1);
expect(Project::find($p1->id))->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);
});