Files
portal/app/tests/Feature/Autopodbor/AutopodborBoxApiTest.php
T

82 lines
3.5 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
use App\Models\AutopodborCompetitor;
use App\Models\AutopodborRun;
use App\Models\AutopodborSource;
use App\Models\Tenant;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;
use Tests\Concerns\SharesSupplierPdo;
uses(DatabaseTransactions::class, SharesSupplierPdo::class);
/** @return array{0: Tenant, 1: User, 2: AutopodborRun, 3: AutopodborCompetitor} */
function boxSetup(): array
{
$tenant = Tenant::factory()->create();
$user = User::factory()->create(['tenant_id' => $tenant->id]);
DB::statement('SET app.current_tenant_id = '.$tenant->id);
$run = AutopodborRun::create(['tenant_id' => $tenant->id, 'kind' => 'search', 'status' => 'done', 'region_code' => 16, 'params' => []]);
$comp = AutopodborCompetitor::create(['tenant_id' => $tenant->id, 'search_run_id' => $run->id, 'name' => 'Окна Комфорт', 'dedup_key' => 'okna']);
return [$tenant, $user, $run, $comp];
}
it('PATCH competitors/{id}/box — переносит конкурента в поле и обратно', function () {
[$tenant, $user, $run, $comp] = boxSetup();
$this->actingAs($user)->patchJson("/api/autopodbor/competitors/{$comp->id}/box", ['box' => 'field'])
->assertOk()
->assertJsonPath('data.box', 'field');
expect($comp->fresh()->box)->toBe('field');
$this->actingAs($user)->patchJson("/api/autopodbor/competitors/{$comp->id}/box", ['box' => 'proposal'])
->assertOk()
->assertJsonPath('data.box', 'proposal');
});
it('PATCH competitors/{id}/box — отвергает чужое значение ящика (422)', function () {
[$tenant, $user, $run, $comp] = boxSetup();
$this->actingAs($user)->patchJson("/api/autopodbor/competitors/{$comp->id}/box", ['box' => 'garbage'])
->assertStatus(422);
});
it('PATCH competitors/{id}/box — чужой тенант не видит конкурента (404)', function () {
[$tenant, $user, $run, $comp] = boxSetup();
$other = User::factory()->create(['tenant_id' => Tenant::factory()->create()->id]);
$this->actingAs($other)->patchJson("/api/autopodbor/competitors/{$comp->id}/box", ['box' => 'field'])
->assertStatus(404);
});
it('PATCH sources/{id}/box — переносит источник в работу и обратно', function () {
[$tenant, $user, $run, $comp] = boxSetup();
$src = AutopodborSource::create([
'tenant_id' => $tenant->id, 'competitor_id' => $comp->id, 'study_run_id' => $run->id,
'signal_type' => 'site', 'identifier' => 'okna-komfort.ru', 'dedup_key' => 'site:okna-komfort.ru',
]);
$this->actingAs($user)->patchJson("/api/autopodbor/sources/{$src->id}/box", ['box' => 'field'])
->assertOk()
->assertJsonPath('data.box', 'field');
expect($src->fresh()->box)->toBe('field');
});
it('PATCH sources/{id}/box — чужой тенант не видит источник (404)', function () {
[$tenant, $user, $run, $comp] = boxSetup();
$src = AutopodborSource::create([
'tenant_id' => $tenant->id, 'competitor_id' => $comp->id, 'study_run_id' => $run->id,
'signal_type' => 'site', 'identifier' => 'okna-komfort.ru', 'dedup_key' => 'site:okna-komfort.ru',
]);
$other = User::factory()->create(['tenant_id' => Tenant::factory()->create()->id]);
$this->actingAs($other)->patchJson("/api/autopodbor/sources/{$src->id}/box", ['box' => 'field'])
->assertStatus(404);
});