85 lines
3.0 KiB
PHP
85 lines
3.0 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
use App\Models\ClientTg\Campaign;
|
||
|
|
use App\Models\Tenant;
|
||
|
|
use App\Services\ClientTg\RobotResult;
|
||
|
|
use App\Services\ClientTg\TelegramModerationVerdictApplier;
|
||
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
|
|
use Illuminate\Support\Facades\DB;
|
||
|
|
|
||
|
|
uses(RefreshDatabase::class);
|
||
|
|
|
||
|
|
function verdictCampaign(int $tenantId): Campaign
|
||
|
|
{
|
||
|
|
return Campaign::query()->create([
|
||
|
|
'tenant_id' => $tenantId,
|
||
|
|
'status' => Campaign::STATUS_MODERATING,
|
||
|
|
'ad_text' => 'Приходите к нам за услугой',
|
||
|
|
'ad_link' => 'https://example.test/promo',
|
||
|
|
'ord_category' => 'Размещение рекламы',
|
||
|
|
'budget_cap_rub' => '500.00',
|
||
|
|
'audience_kind' => Campaign::AUDIENCE_LIST,
|
||
|
|
'planned_count' => 2,
|
||
|
|
'estimated_cost_rub' => '315.00',
|
||
|
|
'mts_campaign_id' => '777',
|
||
|
|
'created_by' => 1,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
beforeEach(function () {
|
||
|
|
config()->set('client_tg.sandbox', true);
|
||
|
|
$this->tenant = Tenant::factory()->create(['balance_rub' => '1000.00']);
|
||
|
|
DB::statement('SET LOCAL app.current_tenant_id = '.$this->tenant->id);
|
||
|
|
$this->campaign = verdictCampaign($this->tenant->id);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('одобрение переводит кампанию в показы', function () {
|
||
|
|
app(TelegramModerationVerdictApplier::class)->apply(
|
||
|
|
$this->tenant->id,
|
||
|
|
$this->campaign->id,
|
||
|
|
RobotResult::fromRobotJson(['ok' => true, 'moderationStatus' => 'approved']),
|
||
|
|
true,
|
||
|
|
);
|
||
|
|
|
||
|
|
expect(Campaign::find($this->campaign->id)->status)->toBe(Campaign::STATUS_LAUNCHED);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('отказ переводит кампанию в отклонённые и пишет причину', function () {
|
||
|
|
app(TelegramModerationVerdictApplier::class)->apply(
|
||
|
|
$this->tenant->id,
|
||
|
|
$this->campaign->id,
|
||
|
|
RobotResult::fromRobotJson(['ok' => true, 'moderationStatus' => 'rejected', 'reason' => 'нет лицензии']),
|
||
|
|
true,
|
||
|
|
);
|
||
|
|
|
||
|
|
$fresh = Campaign::find($this->campaign->id);
|
||
|
|
expect($fresh->status)->toBe(Campaign::STATUS_REJECTED);
|
||
|
|
expect($fresh->status_reason)->toBe('нет лицензии');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('вердикт «ещё на модерации» кампанию не трогает', function () {
|
||
|
|
app(TelegramModerationVerdictApplier::class)->apply(
|
||
|
|
$this->tenant->id,
|
||
|
|
$this->campaign->id,
|
||
|
|
RobotResult::fromRobotJson(['ok' => true, 'moderationStatus' => 'moderating']),
|
||
|
|
true,
|
||
|
|
);
|
||
|
|
|
||
|
|
expect(Campaign::find($this->campaign->id)->status)->toBe(Campaign::STATUS_MODERATING);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('не трогает кампанию, которая уже ушла с модерации', function () {
|
||
|
|
Campaign::where('id', $this->campaign->id)->update(['status' => Campaign::STATUS_LAUNCHED]);
|
||
|
|
|
||
|
|
app(TelegramModerationVerdictApplier::class)->apply(
|
||
|
|
$this->tenant->id,
|
||
|
|
$this->campaign->id,
|
||
|
|
RobotResult::fromRobotJson(['ok' => true, 'moderationStatus' => 'rejected']),
|
||
|
|
true,
|
||
|
|
);
|
||
|
|
|
||
|
|
expect(Campaign::find($this->campaign->id)->status)->toBe(Campaign::STATUS_LAUNCHED);
|
||
|
|
});
|