Files
coruna-lab/tests/Feature/DeviceCountryTest.php
2026-09-19 01:03:23 +08:00

102 lines
3.4 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\Admin;
use App\Models\Device;
use App\Models\PageVisit;
use App\Services\CorunaCrypto;
use Illuminate\Foundation\Testing\RefreshDatabase;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
class DeviceCountryTest extends TestCase
{
use RefreshDatabase;
#[Test]
public function xxbb_profile_stores_cf_country_and_show_renders_it(): void
{
$ts = '1786468227899';
$enc = (new CorunaCrypto('Ek8pl31K2yeHgQwy'))->encryptJson([
'd' => '000C30D83CD0402E',
'c' => '202700cfb1ad3de68e11239dcc26c30b',
'deviceInfo' => ['productType' => 'iPhone12,8', 'productVersion' => '16.6'],
], $ts);
$this->call('POST', '/a', [], [], [], [
'CONTENT_TYPE' => 'text/plain',
'HTTP_X_TS' => $ts,
'HTTP_CF_IPCOUNTRY' => 'US',
'HTTP_CF_CONNECTING_IP' => '203.0.113.44',
'HTTP_X_FORWARDED_FOR' => '1.2.3.4',
], $enc['body'])->assertOk();
$device = Device::query()->where('device_id', '000C30D83CD0402E')->first();
$this->assertNotNull($device);
$this->assertSame('US', $device->country);
$this->assertSame('203.0.113.44', $device->ip);
$this->assertStringContainsString('(美国)', $device->formattedIp());
$admin = Admin::query()->create(['username' => 'admin', 'password' => 'admin123']);
$this->actingAs($admin, 'admin')
->get(route('admin.devices.show', $device))
->assertOk()
->assertSee('(美国)', false);
}
#[Test]
public function show_falls_back_to_visit_country_when_device_country_empty(): void
{
$device = Device::query()->create([
'device_id' => '000C30D83CD0402E',
'ip' => '1.2.3.4',
]);
PageVisit::query()->create([
'channel_id' => '0.0.01',
'client_uid' => 'aaaaaaaaaaaaaaaa',
'ip' => '1.2.3.4',
'country' => 'FR',
]);
$this->assertSame('FR', $device->resolvedCountry());
$this->assertSame('1.2.3.4(法国)', $device->formattedIp());
}
#[Test]
public function device_list_includes_country_column(): void
{
Device::query()->create([
'device_id' => 'dev-country-cn',
'ip' => '8.8.8.8',
'country' => 'CN',
]);
Device::query()->create([
'device_id' => 'dev-country-visit',
'ip' => '1.2.3.4',
]);
PageVisit::query()->create([
'channel_id' => '0.0.01',
'client_uid' => 'bbbbbbbbbbbbbbbb',
'ip' => '1.2.3.4',
'country' => 'FR',
]);
$admin = Admin::query()->create(['username' => 'admin', 'password' => 'admin123']);
$this->actingAs($admin, 'admin')
->get(route('admin.devices.index'))
->assertOk()
->assertSee('国家');
$rows = $this->actingAs($admin, 'admin')
->getJson(route('admin.devices.data'))
->assertOk()
->json('data');
$byId = collect($rows)->keyBy('device_id');
$this->assertSame('CN', $byId['dev-country-cn']['country']);
$this->assertSame('中国', $byId['dev-country-cn']['country_label']);
$this->assertSame('FR', $byId['dev-country-visit']['country']);
$this->assertSame('法国', $byId['dev-country-visit']['country_label']);
}
}