CaiWuXiTong/tests/Feature/Business/WarningPageTest.php
苏木 26a46d5be1 feat: 接入售后退货退款预警数据
Co-authored-by: 苏木 <594326199@qq.com>
Co-committed-by: 苏木 <594326199@qq.com>
2026-10-29 13:45:33 +08:00

174 lines
6.2 KiB
PHP

<?php
namespace Tests\Feature\Business;
use App\Models\Auth\Permission;
use App\Models\Auth\Role;
use App\Models\Auth\User;
use App\Utilities\BusinessNavigation;
use App\Utilities\SidebarPermissions;
use Modules\EcommerceFinance\Repositories\NonJingxiRefundMirrorRepository;
use Tests\Feature\FeatureTestCase;
class WarningPageTest extends FeatureTestCase
{
protected function setUp(): void
{
parent::setUp();
$this->user->attachPermission(
Permission::where('name', 'read-admin-panel')->firstOrFail()
);
}
public function test_warning_page_contains_after_sales_return_refund_child(): void
{
$warnings = collect(BusinessNavigation::all())
->firstWhere('slug', 'other')['pages'];
$warnings = collect($warnings)->firstWhere('slug', 'warnings');
$this->assertSame(
['warning-overview', 'after-sales-return-refund'],
array_column($warnings['children'], 'slug'),
);
$this->assertSame(
['预警总览', '售后退货退款'],
array_column($warnings['children'], 'title'),
);
}
public function test_warning_children_render_and_are_linked_from_the_sidebar(): void
{
$this->loginAs();
$this->bindReadyRefundMirror();
$overviewUrl = route('business.pages.show', [
'section' => 'other',
'page' => 'warning-overview',
]);
$afterSalesRefundUrl = route('business.pages.show', [
'section' => 'other',
'page' => 'after-sales-return-refund',
]);
$this->get(route('business.pages.show', [
'section' => 'other',
'page' => 'warnings',
]))
->assertRedirect($overviewUrl);
$this->get($overviewUrl)
->assertOk()
->assertSeeText('预警总览')
->assertSeeText('售后退货退款')
->assertSeeText('风险等级')
->assertSee($afterSalesRefundUrl, false);
$this->get($afterSalesRefundUrl)
->assertOk()
->assertSeeText('售后退货退款')
->assertSeeText('聚水潭售后数据已接入')
->assertSeeText('AS-WARNING-001')
->assertSeeText('售后测试店')
->assertSeeText('退款总金额')
->assertSee('data-refund-page="after-sales-warning"', false)
->assertSee('action="' . $afterSalesRefundUrl . '"', false)
->assertDontSee(route('business.refunds.non-jingxi-refund-data.export'), false)
->assertDontSeeText('售后退货退款预警规则待接入');
}
public function test_existing_warning_page_permission_keeps_both_child_entries(): void
{
$regular = $this->regularAccount();
$subject = 'user_' . $regular->id;
setting()->set('system.sidebar_permissions', [
'configured' => [$subject],
'subjects' => [$subject => ['other/warnings']],
]);
$other = collect(SidebarPermissions::navigationFor($regular))
->firstWhere('slug', 'other');
$warnings = collect($other['pages'])->firstWhere('slug', 'warnings');
$this->assertSame(
['warning-overview', 'after-sales-return-refund'],
array_column($warnings['children'], 'slug'),
);
$this->bindReadyRefundMirror();
$this->loginAs($regular)
->get(route('business.pages.show', [
'section' => 'other',
'page' => 'after-sales-return-refund',
]))
->assertOk()
->assertSeeText('聚水潭售后数据已接入')
->assertSeeText('AS-WARNING-001');
}
private function regularAccount(): User
{
$name = 'fixture-warning-page-' . User::count();
$user = User::create([
'name' => $name,
'email' => $name . '@example.test',
'password' => 'fixture-unused-password',
'enabled' => true,
'locale' => 'zh-CN',
'landing_page' => 'dashboard',
]);
$user->roles()->attach(Role::where('name', 'user')->firstOrFail()->id);
$user->permissions()->attach(Permission::where('name', 'read-admin-panel')->firstOrFail()->id);
$user->companies()->attach($this->company->id);
return $user;
}
private function bindReadyRefundMirror(): void
{
$states = [
NonJingxiRefundMirrorRepository::INFO_DATASET => [
'status' => 'succeeded',
'initial_completed_at' => '2026-10-01 00:00:00',
],
NonJingxiRefundMirrorRepository::ITEM_DATASET => [
'status' => 'succeeded',
'initial_completed_at' => '2026-10-01 00:00:00',
],
];
$repository = \Mockery::mock(NonJingxiRefundMirrorRepository::class);
$repository->shouldReceive('available')->once()->andReturnTrue();
$repository->shouldReceive('states')->once()->andReturn($states);
$repository->shouldReceive('syncStatus')->once()->with($states)->andReturn([
'code' => 'healthy',
'last_succeeded_at' => '2026-10-29 09:00:00',
]);
$repository->shouldReceive('ready')->once()->andReturnTrue();
$repository->shouldReceive('queryReady')->once()->andReturnTrue();
$repository->shouldReceive('paginate')->once()->andReturn([
'data' => [(object) [
'Id' => 9001,
'refundDate' => '2026-10-28 10:00:00',
'dataSource' => '售后资料',
'detailStatus' => '已关联',
'as_id' => 8001,
'outer_as_id' => 'AS-WARNING-001',
'o_id' => 'ORDER-WARNING-001',
'so_id' => 'SUB-WARNING-001',
'type' => '退货退款',
'shop_name' => '售后测试店',
'platform' => '天猫',
'totalRefundMoney' => 128.5,
'sku_id' => 'SKU-WARNING-001',
'detailRefundMoney' => 128.5,
'refundQty' => 1,
'status' => '退款完成',
]],
'total' => 1,
]);
$this->app->instance(NonJingxiRefundMirrorRepository::class, $repository);
}
}