350 lines
19 KiB
PHP
350 lines
19 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Business;
|
|
|
|
use App\Business\CourierProfileData;
|
|
use App\Exceptions\CourierOnboardingDingTalkException;
|
|
use App\Models\Business\CourierOnboarding;
|
|
use App\Models\Business\CourierOnboardingEvent;
|
|
use App\Models\Business\CourierProfile;
|
|
use App\Models\Business\CourierProfileEvent;
|
|
use App\Support\CourierOnboardingDingTalkProfiles;
|
|
use Illuminate\Http\Client\Request;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Tests\Feature\FeatureTestCase;
|
|
|
|
class CourierOnboardingDingTalkProfilesTest extends FeatureTestCase
|
|
{
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
$this->company->makeCurrent();
|
|
config(['courier_onboarding.dingtalk' => [
|
|
'enabled' => true, 'client_id' => 'test-app', 'client_secret' => 'test-secret',
|
|
'process_code' => 'PROC-TEST', 'originator_user_id' => 'originator-test', 'dept_id' => 123,
|
|
]]);
|
|
Http::preventStrayRequests();
|
|
}
|
|
|
|
public function test_approved_profile_flows_to_courier_information_without_copying_passwords_or_changing_review(): void
|
|
{
|
|
$record = $this->record();
|
|
$snapshot = $this->snapshot($record);
|
|
$snapshot['formComponentValues'][] = ['name' => '财务账单密码', 'value' => 'must-not-be-saved'];
|
|
$this->fake([$record->dingtalk_instance_id => $snapshot]);
|
|
$this->assertSame('synced', $this->service()->synchronize($record->company_id, $record->id, 'test-token'));
|
|
$fresh = $record->fresh();
|
|
foreach ($this->profile() as $field => $value) {
|
|
$this->assertSame($value, $fresh->{$field});
|
|
}
|
|
$this->assertSame(CourierOnboarding::STATUS_SUBMITTED, $fresh->status);
|
|
$row = (object) ['undefined3' => null, '_info' => ['onboarding' => $fresh]];
|
|
$this->assertSame('运费', app(CourierProfileData::class)->fillFromOnboarding($row)->undefined3);
|
|
$this->assertSame('00123', app(CourierProfileData::class)->onboardingNetwork($fresh)->netcode);
|
|
$this->assertSame(1, $record->events()->where('event_type', 'dingtalk_profile_synced')->count());
|
|
$this->assertStringNotContainsString('must-not-be-saved', json_encode($fresh->getAttributes()));
|
|
$this->assertStringNotContainsString('must-not-be-saved', $record->events()->firstOrFail()->note);
|
|
// 审批完成只同步一次,之后的人工修正不会被轮询覆盖。
|
|
$fresh->update(['courier_email' => 'corrected@example.com']);
|
|
$this->assertSame('skipped', $this->service()->synchronize($record->company_id, $record->id, 'test-token'));
|
|
$this->assertSame('corrected@example.com', $record->fresh()->courier_email);
|
|
Http::assertSentCount(1);
|
|
}
|
|
|
|
public function test_pending_refused_and_terminated_approvals_do_not_apply_profile(): void
|
|
{
|
|
foreach ([['RUNNING', 'agree', 'pending'], ['COMPLETED', 'refuse', 'closed'], ['TERMINATED', '', 'closed']] as [$status, $result, $expected]) {
|
|
$record = $this->record();
|
|
$record->update(['network_region' => '原地区']);
|
|
$snapshots[$record->dingtalk_instance_id] = [...$this->shippingSnapshot($record), 'status' => $status, 'result' => $result];
|
|
$records[] = [$record, $expected];
|
|
}
|
|
$this->fake($snapshots);
|
|
foreach ($records as [$record, $expected]) {
|
|
$this->assertSame($expected, $this->service()->synchronize($record->company_id, $record->id, 'test-token'));
|
|
$this->assertSame('原地区', $record->fresh()->network_region);
|
|
$this->assertSame(CourierOnboarding::STATUS_SUBMITTED, $record->fresh()->status);
|
|
$this->assertFalse(CourierProfile::query()->where('expcode', $record->workplatform_expcode)->exists());
|
|
}
|
|
}
|
|
|
|
public function test_new_shipping_fields_merge_without_losing_manual_values_or_crossing_companies(): void
|
|
{
|
|
$record = $this->record();
|
|
$otherCompany = \App\Models\Common\Company::create(['name' => '其他公司', 'domain' => 'other.example.test', 'enabled' => 1]);
|
|
$other = CourierProfile::create([
|
|
'company_id' => $otherCompany->id, 'expcode' => $record->workplatform_expcode,
|
|
'overrides' => ['shipper_name' => '其他公司发货人'],
|
|
]);
|
|
$saved = CourierProfile::create([
|
|
'company_id' => $record->company_id, 'expcode' => $record->workplatform_expcode,
|
|
'overrides' => ['linker' => '人工联系人'],
|
|
]);
|
|
$snapshot = $this->shippingSnapshot($record);
|
|
$snapshot['formComponentValues'][] = ['name' => '快递网点代码', 'value' => 'legacy-ignored'];
|
|
$this->fake([$record->dingtalk_instance_id => $snapshot]);
|
|
|
|
$this->assertSame('synced', $this->service()->synchronize($record->company_id, $record->id, 'test-token'));
|
|
$this->assertSame('00123', $record->fresh()->network_code);
|
|
$this->assertSame(['linker' => '人工联系人', 'shipper_name' => '张三', 'shipping_address' => '江苏省常州市测试路1号'], $saved->fresh()->overrides);
|
|
$this->assertSame(['shipper_name' => '其他公司发货人'], $other->fresh()->overrides);
|
|
$event = CourierProfileEvent::query()->where('action', 'dingtalk_sync')->firstOrFail();
|
|
$this->assertSame($record->company_id, $event->company_id);
|
|
$this->assertSame(['shipper_name', 'shipping_address'], array_keys($event->changes));
|
|
$this->assertSame('张三', app(CourierProfileData::class)->apply((object) [], $saved->fresh()->overrides)->shipper_name);
|
|
|
|
$saved->update(['overrides' => ['shipper_name' => '人工修正']]);
|
|
$this->assertSame('skipped', $this->service()->synchronize($record->company_id, $record->id, 'test-token'));
|
|
$this->assertSame(['shipper_name' => '人工修正'], $saved->fresh()->overrides);
|
|
$this->assertSame(1, CourierProfileEvent::query()->where('action', 'dingtalk_sync')->count());
|
|
Http::assertSentCount(1);
|
|
}
|
|
|
|
public function test_legacy_approval_uses_old_outlet_code_and_preserves_existing_shipping_fields(): void
|
|
{
|
|
$record = $this->record();
|
|
$saved = CourierProfile::create([
|
|
'company_id' => $record->company_id, 'expcode' => $record->workplatform_expcode,
|
|
'overrides' => ['shipper_name' => '保留发货人', 'shipping_address' => '保留地址'],
|
|
]);
|
|
$snapshot = $this->snapshot($record);
|
|
foreach ($snapshot['formComponentValues'] as &$field) {
|
|
if ($field['name'] === '网点代码') {
|
|
$field['name'] = '快递网点代码';
|
|
}
|
|
}
|
|
unset($field);
|
|
$this->fake([$record->dingtalk_instance_id => $snapshot]);
|
|
$this->assertSame('synced', $this->service()->synchronize($record->company_id, $record->id, 'test-token'));
|
|
$this->assertSame('00123', $record->fresh()->network_code);
|
|
$this->assertSame(['shipper_name' => '保留发货人', 'shipping_address' => '保留地址'], $saved->fresh()->overrides);
|
|
$this->assertSame(0, CourierProfileEvent::query()->count());
|
|
}
|
|
|
|
/** @dataProvider invalidShippingValues */
|
|
public function test_invalid_new_fields_block_all_writes_and_can_be_corrected(string $name, mixed $value): void
|
|
{
|
|
$record = $this->record();
|
|
$invalid = $this->shippingSnapshot($record);
|
|
foreach ($invalid['formComponentValues'] as &$field) {
|
|
if ($field['name'] === $name) {
|
|
$field['value'] = $value;
|
|
}
|
|
}
|
|
unset($field);
|
|
$invalid['formComponentValues'][] = ['name' => '快递网点代码', 'value' => 'do-not-fall-back'];
|
|
Http::fake(['*/workflow/processInstances*' => Http::sequence()->push(['result' => $invalid])->push(['result' => $this->shippingSnapshot($record)])]);
|
|
try {
|
|
$this->service()->synchronize($record->company_id, $record->id, 'test-token');
|
|
$this->fail('新增资料无效时不能部分回填');
|
|
} catch (CourierOnboardingDingTalkException $exception) {
|
|
$this->assertStringContainsString($name, $exception->getMessage());
|
|
$this->assertNull($record->fresh()->courier_category);
|
|
$this->assertNull($record->fresh()->network_code);
|
|
$this->assertFalse(CourierProfile::query()->where('expcode', $record->workplatform_expcode)->exists());
|
|
$this->assertSame(0, $record->events()->count());
|
|
}
|
|
$this->assertSame('synced', $this->service()->synchronize($record->company_id, $record->id, 'test-token'));
|
|
$this->assertSame('张三', CourierProfile::query()->where('expcode', $record->workplatform_expcode)->firstOrFail()->overrides['shipper_name']);
|
|
}
|
|
|
|
public static function invalidShippingValues(): array
|
|
{
|
|
return [['发货人', ' '], ['发货地址', ['not-text']], ['网点代码', ''], ['网点代码', str_repeat('0', 65)]];
|
|
}
|
|
|
|
public function test_shipping_fields_are_visible_editable_and_exported_with_the_existing_outlet_column(): void
|
|
{
|
|
$this->user->attachPermission(\App\Models\Auth\Permission::where('name', 'read-admin-panel')->firstOrFail());
|
|
$record = $this->record();
|
|
$record->update(['status' => CourierOnboarding::STATUS_ACTIVATED]);
|
|
$this->fake([$record->dingtalk_instance_id => $this->shippingSnapshot($record)]);
|
|
$this->assertSame('synced', $this->service()->synchronize($record->company_id, $record->id, 'test-token'));
|
|
$mirror = \Mockery::mock(\Modules\EcommerceFinance\Repositories\CourierInformationMirrorRepository::class)->shouldIgnoreMissing();
|
|
$mirror->shouldReceive('relatedAvailability')->andReturn([]);
|
|
app()->instance(\Modules\EcommerceFinance\Repositories\CourierInformationMirrorRepository::class, $mirror);
|
|
$this->loginAs();
|
|
$page = route('business.pages.show', ['section' => 'courier-payables', 'page' => 'courier-information', 'selected' => $record->workplatform_expcode, 'edit' => 1]);
|
|
$this->loginAs()->get($page)->assertOk()
|
|
->assertSeeText('张三')->assertSeeText('江苏省常州市测试路1号')->assertSeeText('00123')
|
|
->assertSee('name="shipper_name"', false)->assertSee('name="shipping_address"', false)
|
|
->assertDontSeeText('快递网点代码');
|
|
$this->loginAs()->post(route('business.courier-profiles.update', $record->workplatform_expcode), [
|
|
'form' => 'edit', 'expname' => '测试快递', 'shipper_name' => '李四', 'shipping_address' => '江苏省常州市测试路2号',
|
|
])->assertSessionHasNoErrors()->assertRedirect();
|
|
|
|
$response = $this->loginAs()->get(route('business.courier-payables.courier-information.export'));
|
|
$response->assertOk();
|
|
$stream = fopen('php://temp', 'w+');
|
|
fwrite($stream, ltrim($response->streamedContent(), "\xEF\xBB\xBF"));
|
|
rewind($stream);
|
|
$headers = fgetcsv($stream);
|
|
$row = fgetcsv($stream);
|
|
fclose($stream);
|
|
$this->assertCount(count($headers), $row);
|
|
$export = array_combine($headers, $row);
|
|
$this->assertStringContainsString('李四', $export['发货人']);
|
|
$this->assertStringContainsString('江苏省常州市测试路2号', $export['发货地址']);
|
|
$this->assertStringContainsString('00123', $export['网点代码']);
|
|
$this->assertArrayNotHasKey('快递网点代码', $export);
|
|
}
|
|
|
|
public function test_shipping_fields_require_a_courier_company_code_before_any_write(): void
|
|
{
|
|
$record = $this->record();
|
|
$record->update(['workplatform_expcode' => null]);
|
|
$this->fake([$record->dingtalk_instance_id => $this->shippingSnapshot($record)]);
|
|
try {
|
|
$this->service()->synchronize($record->company_id, $record->id, 'test-token');
|
|
$this->fail('缺少关联代码不可部分回填');
|
|
} catch (CourierOnboardingDingTalkException $exception) {
|
|
$this->assertStringContainsString('缺少快递公司代码', $exception->getMessage());
|
|
$this->assertNull($record->fresh()->courier_category);
|
|
$this->assertSame(0, CourierProfile::query()->count());
|
|
$this->assertSame(0, $record->events()->count());
|
|
}
|
|
}
|
|
|
|
public function test_wrong_company_originator_and_marker_never_write_profile(): void
|
|
{
|
|
$record = $this->record();
|
|
$wrongOriginator = $this->record();
|
|
$wrongMarker = $this->record();
|
|
$this->fake([
|
|
$record->dingtalk_instance_id => $this->snapshot($record),
|
|
$wrongOriginator->dingtalk_instance_id => [...$this->snapshot($wrongOriginator), 'originatorUserId' => 'other-user'],
|
|
$wrongMarker->dingtalk_instance_id => $this->snapshot($record),
|
|
]);
|
|
$this->assertSame('skipped', $this->service()->synchronize($record->company_id + 1, $record->id, 'test-token'));
|
|
foreach ([$wrongOriginator, $wrongMarker] as $candidate) {
|
|
try {
|
|
$this->service()->synchronize($candidate->company_id, $candidate->id, 'test-token');
|
|
$this->fail('不匹配的审批不可写入资料');
|
|
} catch (CourierOnboardingDingTalkException) {
|
|
$this->assertNull($candidate->fresh()->courier_category);
|
|
}
|
|
}
|
|
$this->assertSame(0, CourierOnboardingEvent::query()->count());
|
|
Http::assertSentCount(2);
|
|
}
|
|
|
|
public function test_incomplete_or_invalid_profile_is_atomic_and_can_sync_after_it_is_corrected(): void
|
|
{
|
|
$record = $this->record();
|
|
$invalid = $this->snapshot($record);
|
|
$invalid['formComponentValues'] = array_values(array_filter($invalid['formComponentValues'], fn (array $field): bool => $field['name'] !== '快递邮箱'));
|
|
$invalid['formComponentValues'][] = ['name' => '快递邮箱', 'value' => 'invalid-email'];
|
|
Http::fake(['*/workflow/processInstances*' => Http::sequence()->push(['result' => $invalid])->push(['result' => $this->snapshot($record)])]);
|
|
try {
|
|
$this->service()->synchronize($record->company_id, $record->id, 'test-token');
|
|
$this->fail('无效资料不可部分写入');
|
|
} catch (CourierOnboardingDingTalkException $exception) {
|
|
$this->assertStringContainsString('快递邮箱', $exception->getMessage());
|
|
$this->assertNull($record->fresh()->courier_category);
|
|
$this->assertNull($record->fresh()->network_code);
|
|
}
|
|
$this->assertSame('synced', $this->service()->synchronize($record->company_id, $record->id, 'test-token'));
|
|
}
|
|
|
|
public function test_polling_rotates_a_bounded_batch_and_does_not_create_any_approval(): void
|
|
{
|
|
$first = $this->record();
|
|
$second = $this->record();
|
|
$this->fake([
|
|
$first->dingtalk_instance_id => [...$this->snapshot($first), 'status' => 'RUNNING'],
|
|
$second->dingtalk_instance_id => $this->snapshot($second),
|
|
]);
|
|
$this->artisan('courier-onboarding:sync-dingtalk-profiles', ['--limit' => 1])->assertSuccessful();
|
|
$this->assertNull($first->fresh()->courier_category);
|
|
$this->assertNull($second->fresh()->courier_category);
|
|
$this->artisan('courier-onboarding:sync-dingtalk-profiles', ['--limit' => 1])->assertSuccessful();
|
|
$this->assertSame('内采快递', $second->fresh()->courier_category);
|
|
$this->artisan('courier-onboarding:sync-dingtalk-profiles', ['--limit' => 1])->assertSuccessful();
|
|
$this->assertSame($first->id, Cache::get('courier-onboarding:dingtalk-profile-cursor'));
|
|
Http::assertNotSent(fn (Request $request): bool => $request->method() === 'POST' && str_contains($request->url(), '/workflow/'));
|
|
}
|
|
|
|
public function test_polling_records_one_sanitized_failure_and_keeps_processing_other_records(): void
|
|
{
|
|
$failed = $this->record();
|
|
$success = $this->record();
|
|
$snapshots = [$success->dingtalk_instance_id => $this->snapshot($success)];
|
|
$this->fake($snapshots);
|
|
$this->artisan('courier-onboarding:sync-dingtalk-profiles')->assertFailed();
|
|
$this->assertSame('内采快递', $success->fresh()->courier_category);
|
|
$this->artisan('courier-onboarding:sync-dingtalk-profiles')->assertFailed();
|
|
$this->assertSame(1, $failed->events()->where('event_type', 'dingtalk_profile_failed')->count());
|
|
$this->assertStringNotContainsString('sensitive-data', $failed->events()->firstOrFail()->note);
|
|
}
|
|
|
|
public function test_disabled_or_no_candidates_make_no_api_calls_and_closed_local_records_are_excluded(): void
|
|
{
|
|
config(['courier_onboarding.dingtalk.enabled' => false]);
|
|
$this->artisan('courier-onboarding:sync-dingtalk-profiles')->assertSuccessful();
|
|
config(['courier_onboarding.dingtalk.enabled' => true]);
|
|
$record = $this->record();
|
|
$record->update(['status' => CourierOnboarding::STATUS_CANCELLED]);
|
|
$this->artisan('courier-onboarding:sync-dingtalk-profiles')->assertSuccessful();
|
|
Http::assertNothingSent();
|
|
}
|
|
|
|
private function record(): CourierOnboarding
|
|
{
|
|
return CourierOnboarding::create([
|
|
'company_id' => $this->company->id, 'number' => 'RZ-SYNC-' . uniqid(), 'token' => bin2hex(random_bytes(20)),
|
|
'status' => CourierOnboarding::STATUS_SUBMITTED, 'courier_name' => '测试快递', 'workplatform_expcode' => 'SYNC-' . uniqid(),
|
|
'dingtalk_instance_id' => 'instance-' . uniqid(),
|
|
]);
|
|
}
|
|
|
|
private function profile(): array
|
|
{
|
|
return [
|
|
'courier_category' => '内采快递', 'cooperation_mode' => '保丢保损', 'courier_attribute' => '简尚',
|
|
'deduction_method' => '运费', 'waybill_type' => '系统对接', 'courier_email' => 'courier@example.com',
|
|
'need_logistics_check' => '否', 'shipping_mode' => '一单一货', 'workplatform_account' => '新开',
|
|
'network_code' => '00123', 'network_region' => '江苏省常州市',
|
|
];
|
|
}
|
|
|
|
private function snapshot(CourierOnboarding $record): array
|
|
{
|
|
$fields = [['name' => '原因', 'value' => '[财务入驻:' . $record->company_id . ':' . $record->id . ':' . $record->number . ']']];
|
|
foreach ($this->profile() as $field => $value) {
|
|
$fields[] = ['name' => CourierOnboarding::PROFILE_FIELDS[$field][0], 'value' => $value];
|
|
}
|
|
|
|
return ['status' => 'COMPLETED', 'result' => 'agree', 'originatorUserId' => 'originator-test', 'formComponentValues' => $fields];
|
|
}
|
|
|
|
private function fake(array $snapshots): void
|
|
{
|
|
Http::fake(function (Request $request) use ($snapshots) {
|
|
if (str_ends_with($request->url(), '/oauth2/accessToken')) {
|
|
return Http::response(['accessToken' => 'test-token']);
|
|
}
|
|
if ($request->method() === 'GET' && str_contains($request->url(), '/workflow/processInstances')) {
|
|
$id = $request['processInstanceId'];
|
|
|
|
return isset($snapshots[$id]) ? Http::response(['result' => $snapshots[$id]]) : Http::response(['message' => 'sensitive-data'], 503);
|
|
}
|
|
throw new \LogicException('测试中禁止创建审批或请求其他接口');
|
|
});
|
|
}
|
|
|
|
private function shippingSnapshot(CourierOnboarding $record): array
|
|
{
|
|
$snapshot = $this->snapshot($record);
|
|
$snapshot['formComponentValues'][] = ['name' => '发货人', 'value' => ' 张三 '];
|
|
$snapshot['formComponentValues'][] = ['name' => '发货地址', 'value' => ' 江苏省常州市测试路1号 '];
|
|
|
|
return $snapshot;
|
|
}
|
|
|
|
private function service(): CourierOnboardingDingTalkProfiles
|
|
{
|
|
return app(CourierOnboardingDingTalkProfiles::class);
|
|
}
|
|
}
|