- Nạp tiền: mã thanh toán sống 30 phút (ManualDepositDraft), khôi phục/huỷ mã, lệnh dọn mã hết hạn; total_pending chỉ tính giao dịch đã nộp chứng từ, admin duyệt/từ chối/xoá giao dịch - Gia hạn dịch vụ: phát hành invoice gia hạn trên order gốc và trừ số dư ngay - Đồng bộ domain từ Plesk theo server, ServiceOrderRecorder tạo order/invoice cho dịch vụ admin dựng - Danh sách dịch vụ trả field theo loại (service_plan, dns_records_count), đếm dịch vụ theo loại Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
104 lines
4.3 KiB
PHP
104 lines
4.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Feature\Api\Admin\DnsManager;
|
|
|
|
use App\Events\DnsSyncCompleted;
|
|
use App\Jobs\DnsManager\SyncDnsFromPlesk;
|
|
use App\Models\DnsDomain;
|
|
use App\Models\User;
|
|
use App\Services\Plesk\PleskDomainService;
|
|
use App\Services\ServerService;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Event;
|
|
use Illuminate\Support\Facades\Queue;
|
|
use Mockery;
|
|
use Spatie\Permission\Models\Role;
|
|
use Tests\TestCase;
|
|
|
|
class SyncDomainsFromPleskTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private int $serverId;
|
|
|
|
private int $otherServerId;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->serverId = DB::table('dns_manager_servers')->insertGetId(['ip_address' => '10.0.0.1', 'password' => 'x']);
|
|
$this->otherServerId = DB::table('dns_manager_servers')->insertGetId(['ip_address' => '10.0.0.2', 'password' => 'x']);
|
|
}
|
|
|
|
private function runJob(array|false $pleskDomains): void
|
|
{
|
|
$plesk = Mockery::mock(PleskDomainService::class);
|
|
$plesk->shouldReceive('listDomains')->once()->andReturn($pleskDomains);
|
|
$plesk->shouldReceive('getLastError')->andReturn('boom');
|
|
|
|
$server = Mockery::mock(ServerService::class);
|
|
$server->shouldReceive('addServerById')->andReturn(true);
|
|
$server->shouldReceive('listRecords')->andReturn(['error' => false, 'data' => []]);
|
|
|
|
$this->app->instance(PleskDomainService::class, $plesk);
|
|
$this->app->instance(ServerService::class, $server);
|
|
|
|
$this->app->call([new SyncDnsFromPlesk($this->serverId), 'handle']);
|
|
}
|
|
|
|
public function test_imports_new_domains_and_links_pending_ones_on_selected_server(): void
|
|
{
|
|
Event::fake([DnsSyncCompleted::class]);
|
|
$pending = DnsDomain::create(['domain_name' => 'Pending.vn', 'dns_manager_server_id' => $this->serverId, 'is_created' => 0]);
|
|
$otherServer = DnsDomain::create(['domain_name' => 'other.vn', 'dns_manager_server_id' => $this->otherServerId, 'is_created' => 0]);
|
|
$cancelled = DnsDomain::create(['domain_name' => 'cancelled.vn', 'dns_manager_server_id' => $this->serverId, 'is_created' => DnsDomain::STATUS_CANCELLED]);
|
|
|
|
$this->runJob([
|
|
['id' => 1, 'name' => 'new.vn', 'base_domain_id' => 0],
|
|
['id' => 2, 'name' => 'pending.vn', 'base_domain_id' => 0],
|
|
['id' => 3, 'name' => 'other.vn', 'base_domain_id' => 0],
|
|
['id' => 4, 'name' => 'cancelled.vn', 'base_domain_id' => 0],
|
|
['id' => 5, 'name' => 'sub.new.vn', 'base_domain_id' => 1],
|
|
]);
|
|
|
|
$this->assertDatabaseHas('dns_manager_domain', ['domain_name' => 'new.vn', 'plesk_id' => 1, 'dns_manager_server_id' => $this->serverId, 'is_created' => 1]);
|
|
$this->assertDatabaseHas('dns_manager_domain', ['id' => $pending->id, 'plesk_id' => 2, 'is_created' => 1]);
|
|
$this->assertDatabaseHas('dns_manager_domain', ['id' => $otherServer->id, 'plesk_id' => null]);
|
|
$this->assertDatabaseHas('dns_manager_domain', ['id' => $cancelled->id, 'plesk_id' => null, 'is_created' => DnsDomain::STATUS_CANCELLED]);
|
|
$this->assertDatabaseMissing('dns_manager_domain', ['domain_name' => 'sub.new.vn']);
|
|
Event::assertDispatched(DnsSyncCompleted::class, fn ($e) => $e->imported === 2 && $e->failedDomains === []);
|
|
}
|
|
|
|
public function test_reports_failure_when_plesk_domain_list_unavailable(): void
|
|
{
|
|
Event::fake([DnsSyncCompleted::class]);
|
|
|
|
$this->runJob(false);
|
|
|
|
$this->assertDatabaseCount('dns_manager_domain', 0);
|
|
Event::assertDispatched(DnsSyncCompleted::class, fn ($e) => $e->failedDomains !== []);
|
|
}
|
|
|
|
public function test_endpoint_queues_sync_for_selected_server(): void
|
|
{
|
|
Queue::fake();
|
|
Role::firstOrCreate(['name' => 'Full Administrator', 'guard_name' => 'admin']);
|
|
$admin = User::factory()->create();
|
|
$admin->assignRole('Full Administrator');
|
|
|
|
$this->actingAs($admin, 'admin')
|
|
->postJson('/api/v1/administrator/domains/sync-dns', ['dns_manager_server_id' => 999999])
|
|
->assertStatus(422);
|
|
|
|
$this->actingAs($admin, 'admin')
|
|
->postJson('/api/v1/administrator/domains/sync-dns', ['dns_manager_server_id' => $this->serverId])
|
|
->assertOk();
|
|
|
|
Queue::assertPushed(SyncDnsFromPlesk::class, 1);
|
|
}
|
|
}
|