Files
portal-api/tests/Feature/Api/Admin/CustomerDomainServiceSyncTest.php
baodev0andClaude Sonnet 5.5 7136bec265 feat(domain): chỉnh ngày bắt đầu/hết hạn tên miền và điền hạn từ dữ liệu cũ
PUT administrator/domains/{id}/term, LegacyDomainTerms và migration backfill hạn cho dịch vụ domain chưa có expires_at.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-28 17:26:41 +07:00

97 lines
4.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\Api\Admin;
use App\Models\Customer;
use App\Models\DnsDomain;
use App\Models\DomainPricing;
use App\Models\Product;
use App\Models\Service;
use App\Services\Domain\CustomerDomainServiceSync;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class CustomerDomainServiceSyncTest extends TestCase
{
use RefreshDatabase;
public function test_assign_update_and_detach_keep_domain_service_in_sync(): void
{
$customer = Customer::factory()->create();
$serverId = DB::table('dns_manager_servers')->insertGetId([
'ip_address' => '10.0.0.1',
'password' => 'x',
]);
$domain = DnsDomain::create([
'domain_name' => 'devro-tech.com',
'dns_manager_server_id' => $serverId,
'is_created' => DnsDomain::STATUS_CREATED,
]);
DomainPricing::query()->delete();
DomainPricing::create(['type' => 'domain', 'tld' => '.com', 'setup_fee' => '0', 'first_year' => 320_000, 'recurring_fee' => '340000', 'is_active' => true]);
$product = Product::factory()->create(['product_type' => 'domain', 'is_active' => true]);
$sync = app(CustomerDomainServiceSync::class);
$sync->syncCustomers($domain, [$customer->id]);
$service = Service::query()
->where('customer_id', $customer->id)
->where('type', 'domain')
->where('server_name', 'devro-tech.com')
->firstOrFail();
$this->assertSame('active', $service->status);
$this->assertSame(CustomerDomainServiceSync::AUTO_CREATED_NOTE, $service->note);
$this->assertSame($product->id, $service->product_id);
$this->assertSame('devro-tech.com', $service->orderItem->domain_name);
$this->assertSame(320_000, (int) $service->orderItem->price);
$this->assertSame('annually', $service->orderItem->billing_cycle);
$this->assertSame(1, (int) $service->invoice->status);
$this->assertSame(320_000, (int) $service->invoice->total);
$sync->syncCustomers($domain, [$customer->id]);
$this->assertSame(1, Service::query()->where('customer_id', $customer->id)->count());
$this->assertSame(1, $service->orderItem->order()->count());
$this->assertDatabaseCount('invoices', 1);
$sync->updateCustomerDomain($domain, $customer->id, '2027-07-21');
$this->assertSame('2027-07-21', $service->fresh()->expires_at?->toDateString());
$sync->syncCustomers($domain, []);
$this->assertSoftDeleted('services', ['id' => $service->id]);
$this->assertSame(3, (int) $service->invoice->fresh()->status);
$this->assertDatabaseMissing('dns_domain_user', [
'domain_id' => $domain->id,
'user_id' => $customer->id,
]);
}
public function test_apply_term_sets_dates_and_cycle_for_domain_services(): void
{
$customer = Customer::factory()->create();
$serverId = DB::table('dns_manager_servers')->insertGetId(['ip_address' => '10.0.0.1', 'password' => 'x']);
$domain = DnsDomain::create([
'domain_name' => 'devro-tech.com',
'dns_manager_server_id' => $serverId,
'is_created' => DnsDomain::STATUS_CREATED,
]);
Product::factory()->create(['product_type' => 'domain', 'is_active' => true]);
$sync = app(CustomerDomainServiceSync::class);
$sync->syncCustomers($domain, [$customer->id]);
$updated = $sync->applyTerm($domain, '2025-06-17', '2027-06-17', 2);
$service = Service::where('customer_id', $customer->id)->where('type', 'domain')->firstOrFail();
$this->assertSame(1, $updated);
$this->assertSame('2025-06-17', $service->started_at->toDateString());
$this->assertSame('2027-06-17', $service->expires_at->toDateString());
$this->assertSame(24, (int) $service->orderItem->months);
$this->assertSame('biennially', $service->orderItem->billing_cycle);
$this->assertSame(0, $sync->applyTerm(DnsDomain::create(['domain_name' => 'solo.vn', 'dns_manager_server_id' => $serverId]), null, null, 1));
}
}