CaiWuXiTong/tests/Feature/Business/MarketingPlatformPromotionBillsTest.php
chenxi 0ae8afb0e8 京准通增加记账时间 (#168)
京准通明细及 CSV 增加数据库记账时间字段。
2026-09-12 13:42:19 +08:00

243 lines
9.6 KiB
PHP

<?php
namespace Tests\Feature\Business;
use App\Models\Auth\Permission;
use Mockery;
use Modules\EcommerceFinance\Repositories\PlatformPromotionAccountRepository;
use Tests\Feature\FeatureTestCase;
class MarketingPlatformPromotionBillsTest extends FeatureTestCase
{
public function test_promotion_platform_is_a_tab_inside_marketing_account_bills(): void
{
$this->allowMarketingAccountBillsPage();
$repository = Mockery::mock(PlatformPromotionAccountRepository::class);
$repository->shouldReceive('latestMonth')->once()->andReturn('2026-08');
$repository->shouldReceive('dashboard')->once()->with([
'month' => '2026-08',
'shop_name' => '',
'account' => '',
], 5000, 'jd')->andReturn($this->dashboardData());
$this->app->instance(PlatformPromotionAccountRepository::class, $repository);
$this->loginAs()
->get(route('business.pages.show', [
'section' => 'billing-transactions',
'page' => 'marketing-account-bills',
'tab' => 'jd-promotion',
]))
->assertOk()
->assertSeeText('京东京准通')
->assertSee('aria-label="京东京准通推广账户筛选"', false)
->assertSeeText('测试店铺')
->assertSeeText('记账时间')
->assertSeeText('2026-08-02 09:10:57')
->assertSeeText('收支类型')
->assertSeeText('出账')
->assertSeeText('导出 CSV')
->assertSee('class="my-6 business-page--marketing-account-bills"', false)
->assertSee(route('business.pages.show', [
'section' => 'billing-transactions',
'page' => 'marketing-account-bills',
'tab' => 'taobao-alipay',
]), false)
->assertDontSeeText('平台推广账户流水');
}
public function test_promotion_platform_export_uses_merged_marketing_account_bills_route(): void
{
$this->allowMarketingAccountBillsPage();
$repository = Mockery::mock(PlatformPromotionAccountRepository::class);
$repository->shouldReceive('latestMonth')->never();
$repository->shouldReceive('dashboard')->once()->with([
'month' => '2026-08',
'shop_name' => '测试',
'account' => 'account-01',
], 5000, 'jd')->andReturn($this->dashboardData());
$this->app->instance(PlatformPromotionAccountRepository::class, $repository);
$response = $this->loginAs()->get(route('business.pages.export', [
'section' => 'billing-transactions',
'page' => 'marketing-account-bills',
'tab' => 'jd-promotion',
'month' => '2026-08',
'shop_name' => '测试',
'account' => 'account-01',
]));
$filename = '店铺营销账户账单_jd_2026-08.csv';
$response->assertOk()
->assertHeader('Content-Type', 'text/csv; charset=UTF-8')
->assertDownload('_jd_2026-08.csv');
$this->assertStringContainsString(
"filename*=utf-8''" . rawurlencode($filename),
(string) $response->headers->get('Content-Disposition'),
);
$csv = $response->streamedContent();
$this->assertStringContainsString('测试店铺', $csv);
$this->assertStringContainsString('收支类型', $csv);
$this->assertStringContainsString('交易类型', $csv);
$this->assertStringContainsString('记账时间', $csv);
$this->assertStringContainsString('2026-08-02 09:10:57', $csv);
$this->assertStringContainsString('出账', $csv);
$this->assertStringContainsString('推广费', $csv);
}
public function test_vip_tab_uses_marketing_daily_data(): void
{
$this->allowMarketingAccountBillsPage();
$repository = Mockery::mock(PlatformPromotionAccountRepository::class);
$repository->shouldReceive('latestMonth')->once()->andReturn('2026-08');
$repository->shouldReceive('dashboard')->once()->with([
'month' => '2026-08',
'shop_name' => '',
'account' => '',
'advertiser_id' => '',
], 5000, 'vip')->andReturn([
'vipMarketingDaily' => [[
'account' => 'vip-account-01',
'accountName' => '唯品营销账户',
'advertiserId' => 'adv-001',
'advertiserTitle' => '唯品广告主',
'reportDate' => '2026-08-31',
'costYuan' => 123.45,
'impressionCount' => 6789,
'updatedAt' => '2026-08-31 23:59:59',
]],
'vipSummary' => [
'cost' => 123.45,
'impressions' => 6789,
'account_count' => 1,
'daily_record_count' => 1,
],
]);
$this->app->instance(PlatformPromotionAccountRepository::class, $repository);
$this->loginAs()
->get(route('business.pages.show', [
'section' => 'billing-transactions',
'page' => 'marketing-account-bills',
'tab' => 'vip-promotion',
]))
->assertOk()
->assertSeeText('唯品会推广流水')
->assertSee('aria-label="唯品会推广流水筛选"', false)
->assertSeeText('营销账户')
->assertSeeText('广告主 ID')
->assertSeeText('唯品营销账户')
->assertSeeText('123.45')
->assertSeeText('6,789')
->assertDontSeeText('申报金额(元)')
->assertDontSeeText('退货订单数');
}
public function test_vip_export_contains_marketing_daily_fields(): void
{
$this->allowMarketingAccountBillsPage();
$repository = Mockery::mock(PlatformPromotionAccountRepository::class);
$repository->shouldReceive('latestMonth')->never();
$repository->shouldReceive('dashboard')->once()->with([
'month' => '2026-08',
'shop_name' => '',
'account' => '',
'advertiser_id' => '',
], 5000, 'vip')->andReturn([
'vipMarketingDaily' => [[
'account' => 'vip-account-01',
'accountName' => '唯品营销账户',
'advertiserId' => 'adv-001',
'advertiserTitle' => '唯品广告主',
'reportDate' => '2026-08-31',
'costYuan' => 123.45,
'impressionCount' => 6789,
'updatedAt' => '2026-08-31 23:59:59',
]],
]);
$this->app->instance(PlatformPromotionAccountRepository::class, $repository);
$response = $this->loginAs()->get(route('business.pages.export', [
'section' => 'billing-transactions',
'page' => 'marketing-account-bills',
'tab' => 'vip-promotion',
'month' => '2026-08',
'advertiser_id' => '',
]));
$response->assertOk()->assertDownload('_vip_2026-08.csv');
$csv = $response->streamedContent();
$this->assertStringContainsString('唯品会推广流水', $csv);
$this->assertStringContainsString('广告主 ID', $csv);
$this->assertStringContainsString('唯品营销账户', $csv);
$this->assertStringContainsString('123.45', $csv);
$this->assertStringNotContainsString('申报金额(元)', $csv);
$this->assertStringNotContainsString('唯品会税务数据', $csv);
}
public function test_removed_platform_promotion_directory_returns_to_the_current_page_with_feedback(): void
{
$this->allowMarketingAccountBillsPage();
$this->withExceptionHandling();
$this->loginAs();
$currentPage = route('business.pages.show', [
'section' => 'billing-transactions',
'page' => 'marketing-account-bills',
'tab' => 'jd-promotion',
]);
$this->from($currentPage)
->get(route('business.pages.show', [
'section' => 'billing-transactions',
'page' => 'platform-promotion-accounts',
'tab' => 'jd',
]))
->assertRedirect($currentPage)
->assertSessionHas('errors');
}
private function dashboardData(): array
{
return [
'jd' => [[
'shop_name' => '测试店铺',
'account' => 'account-01',
'show_date' => '2026-08-01',
'settle_datetime' => '2026-08-02 09:10:57',
'in_out_type_name' => '出账',
'trade_type_name' => '推广费',
'amount' => 100,
'balance_amount' => 100,
'remark' => '测试记录',
]],
'jdSummary' => ['income' => 100, 'expense' => 0, 'record_count' => 1],
'dy' => [],
'dySummary' => [],
'dyStatus' => [],
'dw' => [],
'dwSummary' => [],
'vipSales' => [],
'vipTax' => [],
'vipMarketing' => [],
'vipSummary' => [],
];
}
private function allowMarketingAccountBillsPage(): void
{
$this->user->update(['locale' => 'zh-CN']);
$this->user->attachPermission(Permission::where('name', 'read-admin-panel')->firstOrFail());
$subject = 'user_' . $this->user->id;
setting()->set('system.sidebar_permissions', json_encode([
'configured' => [$subject],
'subjects' => [
$subject => ['billing-transactions/marketing-account-bills'],
],
], JSON_UNESCAPED_UNICODE));
}
}