150 lines
10 KiB
PHP
150 lines
10 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Business;
|
|
|
|
use App\Models\Auth\Permission;
|
|
use App\Support\CourierBills\CourierBillMatcher;
|
|
use App\Support\CourierBills\WorkPlatformCourierRobots;
|
|
use Illuminate\Support\Facades\Crypt;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Modules\EcommerceFinance\Services\SupplierDingTalkNotifier;
|
|
use Tests\Feature\FeatureTestCase;
|
|
|
|
final class WorkPlatformCourierRobotsTest extends FeatureTestCase
|
|
{
|
|
public function test_matching_uses_group_name_and_accepts_any_name_hit_without_using_other_fields(): void
|
|
{
|
|
$group = fn (string $name, array $aliases = []) => (object) ['name' => $name, 'summary_carrier' => $name, 'express_names' => $aliases];
|
|
$rows = [
|
|
$this->robot(1, '曹县韵达|曹县邮政', '账务群甲'),
|
|
$this->robot(2, '德邦快递|沭阳德邦', '德邦快递+沭阳德邦+睢宁邮政小包账务群乙'),
|
|
$this->robot(3, '韵达', '厂家群'),
|
|
];
|
|
$rows[2]['forModel'] = '供应商应付汇总';
|
|
$result = (new WorkPlatformCourierRobots())->match([
|
|
$group('曹县韵达', ['曹县韵达']), $group('曹县韵达小件'),
|
|
$group('别名分组', ['沭阳德邦']), $group('无匹配+曹县邮政'), $group('韵达'),
|
|
$group('睢宁邮政小包'), $group('曹县韵达+德邦快递'),
|
|
], $rows);
|
|
|
|
$this->assertSame(['曹县韵达', '曹县韵达小件', '无匹配+曹县邮政', '韵达', '睢宁邮政小包'], array_keys($result['robots']));
|
|
$this->assertSame('账务群甲', $result['robots']['曹县韵达']['group_name']);
|
|
$this->assertSame('https://oapi.dingtalk.com/robot/send?access_token=platform-1', $result['robots']['曹县韵达']['webhook']);
|
|
$this->assertSame(['别名分组'], $result['unmatched']);
|
|
$this->assertSame(['曹县韵达+德邦快递'], $result['ambiguous']);
|
|
}
|
|
|
|
public function test_exact_and_more_specific_names_win_over_short_containing_names(): void
|
|
{
|
|
$rows = [$this->robot(1, '常州德邦', '常州德邦群'), $this->robot(2, '常州德邦逆向', '常州德邦逆向群'),
|
|
$this->robot(3, '百世快运', '百世快运群'), $this->robot(4, '霸州百世快运', '霸州群')];
|
|
$groups = array_map(fn ($name) => (object) ['name' => $name], ['常州德邦', '常州德邦逆向', '霸州百世快运-雅柏']);
|
|
$result = (new WorkPlatformCourierRobots())->match($groups, $rows);
|
|
$this->assertSame('1', $result['robots']['常州德邦']['source_id']);
|
|
$this->assertSame('2', $result['robots']['常州德邦逆向']['source_id']);
|
|
$this->assertSame('4', $result['robots']['霸州百世快运-雅柏']['source_id']);
|
|
$this->assertSame([], $result['ambiguous']);
|
|
$onlyForward = (new WorkPlatformCourierRobots())->match($groups, [$rows[0]]);
|
|
$this->assertArrayNotHasKey('常州德邦逆向', $onlyForward['robots']);
|
|
$onlyReverse = (new WorkPlatformCourierRobots())->match($groups, [$rows[1]]);
|
|
$this->assertArrayNotHasKey('常州德邦', $onlyReverse['robots']);
|
|
}
|
|
|
|
public function test_beijing_lexinda_uses_the_explicitly_designated_robot(): void
|
|
{
|
|
$rows = [$this->robot(600, '北京大件韵达|北京大件韵达临时', '北京大件韵达+北京大件韵达临时'),
|
|
$this->robot(601, '北京韵达-乐鑫达', '其他群')];
|
|
$result = (new WorkPlatformCourierRobots())->match([(object) ['name' => '北京韵达-乐鑫达']], $rows);
|
|
$this->assertSame('600', $result['robots']['北京韵达-乐鑫达']['source_id']);
|
|
$this->assertSame('北京大件韵达+北京大件韵达临时', $result['robots']['北京韵达-乐鑫达']['group_name']);
|
|
}
|
|
|
|
public function test_designated_nanchang_changzhou_and_shuyang_groups_use_the_requested_robot(): void
|
|
{
|
|
$rows = [$this->robot(588, '樵舍中通|江西中通快运', '南昌对接群'),
|
|
$this->robot(601, '常州中通快运|武进韵达快运', '常州中通快运+武进韵达快运'),
|
|
$this->robot(624, '沭阳中通快运', '沭阳中通快运群'),
|
|
$this->robot(603, '顺德德邦|廊坊德邦|廊坊德邦其他', '顺德德邦+廊坊德邦+廊坊德邦其他'),
|
|
$this->robot(999, '山东中通快运|宿迁中通快运|中通快运专线', '同名但未指定的群')];
|
|
$names = ['樵舍中通', '樵舍中通-上楼', '樵舍中通上楼', '江西中通快运', '江西新建中通',
|
|
'山东中通快运', '南昌樵舍仓仓储费', '中通快运专线', '宿迁中通快运',
|
|
'山东顺丰干配', '河北中通-江三', '苏北顺丰干配大件'];
|
|
$groups = array_map(fn ($name) => (object) ['name' => $name], $names);
|
|
$result = (new WorkPlatformCourierRobots())->match($groups, $rows);
|
|
foreach (array_slice($names, 0, 7) as $name) $this->assertSame('588', $result['robots'][$name]['source_id']);
|
|
$this->assertSame('601', $result['robots']['中通快运专线']['source_id']);
|
|
$this->assertSame('624', $result['robots']['宿迁中通快运']['source_id']);
|
|
foreach (array_slice($names, 9) as $name) $this->assertSame('603', $result['robots'][$name]['source_id']);
|
|
$this->assertSame([], $result['unmatched']);
|
|
$this->assertSame([], $result['ambiguous']);
|
|
$missing = (new WorkPlatformCourierRobots())->match($groups, [end($rows)]);
|
|
$this->assertSame([], $missing['robots']);
|
|
}
|
|
|
|
public function test_import_is_previewed_backed_up_encrypted_and_used_for_upload_notifications(): void
|
|
{
|
|
Storage::fake('local');
|
|
Http::fake(['*' => Http::response(['errcode' => 0])]);
|
|
$this->company->makeCurrent(true);
|
|
$notifier = app(SupplierDingTalkNotifier::class);
|
|
$notifier->save('另一供应商', 'https://oapi.dingtalk.com/robot/send?access_token=keep-supplier', 'SEC-keep', false);
|
|
$before = setting('supplier_reconciliation.dingtalk_robots');
|
|
$db = CourierBillMatcher::db();
|
|
$groupId = $db->table('courier_bill_groups')->insertGetId(['name' => '曹县韵达', 'express_names' => '["曹县韵达"]', 'created_at' => now(), 'updated_at' => now()]);
|
|
$db->table('courier_bill_groups')->insert(['name' => '未匹配快递', 'express_names' => '[]', 'created_at' => now(), 'updated_at' => now()]);
|
|
Storage::disk('local')->put('robots.json', json_encode([$this->robot(1, '曹县韵达|曹县邮政', '曹县账务对接群')]));
|
|
$arguments = ['file' => Storage::disk('local')->path('robots.json'), '--company' => $this->company->id];
|
|
|
|
$this->artisan('courier-bills:import-platform-robots', $arguments)->assertExitCode(0);
|
|
$this->assertSame($before, setting('supplier_reconciliation.dingtalk_robots'));
|
|
Http::assertNothingSent();
|
|
|
|
$this->artisan('courier-bills:import-platform-robots', $arguments + ['--apply' => true])->assertExitCode(0);
|
|
Http::assertNothingSent();
|
|
$backups = Storage::disk('local')->files('private/courier-robot-backups');
|
|
$this->assertCount(1, $backups);
|
|
$backup = json_decode(Crypt::decryptString(Storage::disk('local')->get($backups[0])), true);
|
|
$this->assertSame($before, $backup['value']);
|
|
$stored = json_decode(setting('supplier_reconciliation.dingtalk_robots'), true);
|
|
$this->assertSame('SEC-platform-1', Crypt::decryptString($stored['曹县韵达']['secret']));
|
|
$this->assertStringNotContainsString('SEC-platform-1', setting('supplier_reconciliation.dingtalk_robots'));
|
|
$this->assertSame('SEC-keep', Crypt::decryptString($stored['另一供应商']['secret']));
|
|
$this->assertArrayNotHasKey('未匹配快递', $stored);
|
|
|
|
$this->user->attachPermission(Permission::where('name', 'read-admin-panel')->firstOrFail());
|
|
$this->loginAs()->get(route('business.pages.show', ['section' => 'courier-payables-final', 'page' => 'courier-bill-reconciliation', 'cb_view' => 'groups']))
|
|
->assertOk()->assertSeeText('曹县账务对接群')->assertSeeText('工作平台应付群')->assertDontSeeText('SEC-platform-1')->assertDontSeeText('platform-1');
|
|
$this->post(route('business.courier-payables-final.courier-bills.push-upload-links'), ['month' => '2026-11', 'group' => $groupId])->assertRedirect();
|
|
Http::assertSentCount(1);
|
|
Http::assertSent(fn ($request) => str_contains($request->url(), 'access_token=platform-1')
|
|
&& str_contains($request->url(), '&sign=') && str_contains($request['markdown']['text'], '运费账单上传 · 曹县韵达'));
|
|
$this->assertSame('work_platform', $notifier->configuration('曹县韵达')['config_source']);
|
|
$this->assertSame('曹县账务对接群', $notifier->configuration('曹县韵达')['group_name']);
|
|
$notifier->save('曹县韵达', '', '', false);
|
|
$this->assertSame('曹县账务对接群', $notifier->configuration('曹县韵达')['group_name']);
|
|
}
|
|
|
|
public function test_invalid_import_does_not_partially_replace_existing_settings(): void
|
|
{
|
|
$notifier = app(SupplierDingTalkNotifier::class);
|
|
$notifier->save('原配置', 'https://oapi.dingtalk.com/robot/send?access_token=original', '', false);
|
|
$before = setting('supplier_reconciliation.dingtalk_robots');
|
|
try {
|
|
$notifier->importCourierRobots([
|
|
'有效' => ['webhook' => 'https://oapi.dingtalk.com/robot/send?access_token=valid', 'secret' => ''],
|
|
'无效' => ['webhook' => 'https://example.test/robot/send?access_token=invalid', 'secret' => ''],
|
|
]);
|
|
$this->fail('Invalid robot must be rejected');
|
|
} catch (\Illuminate\Validation\ValidationException) {
|
|
$this->assertSame($before, setting('supplier_reconciliation.dingtalk_robots'));
|
|
}
|
|
}
|
|
|
|
private function robot(int $id, string $names, string $group): array
|
|
{
|
|
return ['Id' => $id, 'forModel' => '快递应付', 'businessKey' => $names, 'groupname' => $group,
|
|
'webAccesstoken' => 'platform-' . $id, 'Secret' => 'SEC-platform-' . $id, 'modifytime' => '2026-12-03 08:00:00'];
|
|
}
|
|
}
|