user->attachPermission( Permission::where('name', 'read-admin-panel')->firstOrFail() ); } public function test_group_robot_configuration_is_encrypted_and_rendered_without_secrets(): void { $this->loginAs() ->post(route('business.supplier-negative-alerts.dingtalk-group.save'), [ 'dingtalk_webhook' => self::WEBHOOK, 'dingtalk_secret' => self::SECRET, 'dingtalk_group_name' => '财务&采购沟通群', ]) ->assertStatus(302) ->assertSessionHas('success', '供应商负数预警群机器人已保存。'); $stored = (string) setting(SupplierNegativeAlertDingTalkWorkflow::SETTING_KEY); $this->assertStringNotContainsString('sna-test-token', $stored); $this->assertStringNotContainsString(self::SECRET, $stored); $this->loginAs() ->get($this->pageUrl()) ->assertOk() ->assertSeeText('群机器人已配置') ->assertSeeText('财务&采购沟通群') ->assertDontSeeText('sna-test-token') ->assertDontSeeText(self::SECRET); } public function test_group_robot_rejects_untrusted_webhook(): void { $this->withExceptionHandling(); $this->loginAs() ->post(route('business.supplier-negative-alerts.dingtalk-group.save'), [ 'dingtalk_webhook' => 'https://example.com/robot/send?access_token=unsafe', 'dingtalk_secret' => self::SECRET, 'dingtalk_group_name' => '财务&采购沟通群', ]) ->assertStatus(302) ->assertSessionHasErrors('dingtalk_webhook'); } public function test_send_to_dingtalk_group_and_public_supplier_form_returns_results_to_page(): void { $this->seedRows(); $this->bindSupplierManagement(); app(SupplierNegativeAlertDingTalkWorkflow::class)->saveConfiguration( self::WEBHOOK, self::SECRET, '财务&采购沟通群', ); Http::fake(['*' => Http::response(['errcode' => 0, 'errmsg' => 'ok'])]); $this->loginAs() ->post(route('business.supplier-negative-alerts.send-dingtalk'), [ 'purchaser' => '采购甲', 'month' => '2026-09', ]) ->assertStatus(302) ->assertSessionHas('success', '已发送至财务&采购沟通群:采购 采购甲,2 家供应商、3 条预警。'); $assignment = DB::table('supplier_negative_alert_assignments')->first(); $this->assertNotNull($assignment); $this->assertSame('sent', $assignment->status); $this->assertSame(2, (int) $assignment->supplier_count); $this->assertSame(3, (int) $assignment->row_count); $taskUrl = null; Http::assertSent(function ($request) use (&$taskUrl): bool { $taskUrl = (string) ($request['actionCard']['singleURL'] ?? ''); return str_contains($request->url(), 'robot/send') && str_contains($request->url(), 'timestamp=') && str_contains($request->url(), 'sign=') && ($request['msgtype'] ?? '') === 'actionCard' && str_contains((string) ($request['actionCard']['text'] ?? ''), '采购:**采购甲**') && str_contains($taskUrl, '/supplier-negative-alert/task/'); }); preg_match('/\/supplier-negative-alert\/task\/([A-Za-z0-9]{64})$/', (string) $taskUrl, $matches); $this->assertNotEmpty($matches[1] ?? null); $token = $matches[1]; $response = $this->get(route('supplier-negative-alert.task', ['token' => $token])) ->assertOk() ->assertSeeText('供应商负数预警处理') ->assertSeeText('三洋木业') ->assertSeeText('久柯家具') ->assertSee('placeholder="请填写该供应商的核对、沟通或处理结果"', false) ->assertSeeText('处理结果'); $this->assertSame(2, substr_count((string) $response->getContent(), 'name="results[]"')); $this->post(route('supplier-negative-alert.task.update', ['token' => $token]), [ 'results' => [ '三洋木业:已核对,等待供应商补货。', '久柯家具:已确认负数由历史退款导致。', ], ]) ->assertRedirect(route('supplier-negative-alert.task', ['token' => $token])) ->assertSessionHas('negative_alert_success'); $assignment = DB::table('supplier_negative_alert_assignments')->first(); $this->assertSame('completed', $assignment->status); $this->assertEqualsCanonicalizing( ['久柯家具', '三洋木业'], DB::table('supplier_negative_alert_results')->orderBy('supplier_name')->pluck('supplier_name')->all(), ); $this->loginAs() ->get($this->pageUrl(['month' => '2026-09', 'purchaser' => '采购甲'])) ->assertOk() ->assertSeeText('三洋木业:已核对,等待供应商补货。') ->assertSeeText('久柯家具:已确认负数由历史退款导致。') ->assertDontSeeText('待采购处理'); } public function test_expired_task_link_is_unavailable(): void { $this->seedRows(); $this->bindSupplierManagement(); app(SupplierNegativeAlertDingTalkWorkflow::class)->saveConfiguration( self::WEBHOOK, self::SECRET, '财务&采购沟通群', ); Http::fake(['*' => Http::response(['errcode' => 0, 'errmsg' => 'ok'])]); $this->loginAs()->post(route('business.supplier-negative-alerts.send-dingtalk'), [ 'purchaser' => '采购甲', 'month' => '2026-09', ]); $taskUrl = ''; Http::assertSent(function ($request) use (&$taskUrl): bool { $taskUrl = (string) ($request['actionCard']['singleURL'] ?? ''); return $taskUrl !== ''; }); preg_match('/\/([A-Za-z0-9]{64})$/', $taskUrl, $matches); DB::table('supplier_negative_alert_assignments')->update([ 'token_expires_at' => now()->subMinute(), ]); $this->get(route('supplier-negative-alert.task', ['token' => $matches[1]])) ->assertNotFound() ->assertSeeText('任务链接无效或已过期'); } private function seedRows(): void { $store = app(SupplierPayableOverrideStore::class); $dataset = PayablesFinalActions::DATASET_PREFIX . 'supplier:supplier-payables-report'; foreach ([ 'sanyang:1' => ['supplier' => '三洋木业', 'supplier_balance' => '-100.00'], 'sanyang:2' => ['supplier' => '三洋木业', 'supplier_balance' => '-50.00'], 'jiuke:1' => ['supplier' => '久柯家具', 'supplier_balance' => '-80.00'], ] as $key => $row) { $store->save($dataset, $key, $row + [ 'summary_month' => '2026-09', 'checker' => '李萌', 'settlement_method' => '订单合同', 'should_pay' => '0.00', 'estimated_price' => '0.00', 'negative_reason' => '历史差异待核对', ], (int) $this->company->id, (int) $this->user->id); } } private function bindSupplierManagement(): void { $supplierManagement = \Mockery::mock(SupplierManagementMirrorRepository::class); $supplierManagement->shouldReceive('suppliersByNames') ->andReturnUsing(static function (array $names): array { return array_map( static fn (string $name): object => (object) ['scmname' => $name, 'buyer' => '采购甲'], $names, ); }); $this->app->instance(SupplierManagementMirrorRepository::class, $supplierManagement); } /** @param array $query */ private function pageUrl(array $query = []): string { return route('business.pages.show', array_merge([ 'section' => 'negative-alerts', 'page' => 'supplier-negative-alert', ], $query)); } }