app = new Application(dirname(__DIR__, 2)); $this->app->instance('config', new Repository(['app' => ['locale' => 'zh']])); Facade::setFacadeApplication($this->app); $db = new Manager($this->app); $db->addConnection(['driver' => 'sqlite', 'database' => ':memory:', 'prefix' => 'bot_']); $db->setAsGlobal(); $db->bootEloquent(); $this->app->instance('db', $db->getDatabaseManager()); $this->app->bind('db.schema', fn () => $db->schema()); $translator = new Translator(new ArrayLoader(), 'zh'); $this->app->instance('translator', $translator); $this->app->instance('validator', new Factory($translator, $this->app)); $this->app->instance(\Illuminate\Contracts\Routing\ResponseFactory::class, new class { public function json($data, $status = 200, $headers = [], $options = 0) { return new JsonResponse($data, $status, $headers, $options); } }); Request::macro('validate', function (array $rules) { return app('validator')->make($this->all(), $rules)->validate(); }); $db->schema()->create('recharge_channel', function (Blueprint $t) { $t->id(); $t->integer('from'); $t->integer('data_type'); $t->string('key'); $t->string('name'); $t->string('type'); $t->decimal('rate', 8, 4); $t->decimal('min')->nullable(); $t->decimal('max')->nullable(); $t->string('fixed')->nullable(); $t->integer('status'); $t->integer('sort'); $t->timestamps(); }); $db->schema()->create('recharge_channel_group', function (Blueprint $t) { $t->id(); $t->string('name'); $t->text('recharge_type')->nullable(); $t->text('withdraw_type')->nullable(); $t->text('activity_type')->nullable(); $t->timestamps(); }); $fixture = require dirname(__DIR__) . '/fixtures/payment_provider_channels.php'; DB::table('recharge_channel')->insert($fixture['channels']); DB::table('recharge_channel_group')->insert($fixture['groups']); (require dirname(__DIR__, 2) . '/database/migrations/2026_09_08_160000_add_payment_provider_status_and_channel_soft_deletes.php')->up(); (require dirname(__DIR__, 2) . '/database/migrations/2026_09_08_170000_add_soft_deletes_to_recharge_channel_groups.php')->up(); $db->schema()->create('operation_audits', function (Blueprint $t) { $t->id(); $t->string('resource_type'); $t->unsignedBigInteger('resource_id')->nullable(); $t->string('action'); $t->json('changes'); $t->unsignedBigInteger('operator_id')->nullable(); $t->string('operator_name'); $t->timestamp('created_at')->useCurrent(); }); } protected function tearDown(): void { DB::disconnect(); Facade::clearResolvedInstances(); parent::tearDown(); } private function request(string $path, array $params): void { $this->app->instance('request', Request::create($path, 'GET', $params)); } private function channelList(array $params): array { $this->request('/admin/rechargeChannel/list', $params); $response = (new RechargeChannel())->list()->getData(true); $this->assertSame(0, $response['code']); return $response['data']; } public function test_company_options_use_all_rows_not_first_page(): void { $page1 = $this->channelList(['data_type' => '1']); $this->assertSame(19, $page1['total']); $this->assertCount(15, $page1['data']); $this->assertNotContains('jd', array_column($page1['data'], 'payment_company')); $page2 = $this->channelList(['data_type' => '1', 'page' => 2]); $this->assertSame(['zimu', 'no', 'no', 'jd'], array_column($page2['data'], 'payment_company')); $this->request('/admin/paymentConfig/options', ['kind' => 'deposit']); $data = (new PaymentConfiguration(new PaymentChannelLinkService()))->options()->getData(true); $this->assertSame(0, $data['code']); $this->assertSame([false, false, false, false], array_column($data['data']['payment_companies'], 'disabled')); } public function test_channel_list_displays_provider_separately_from_channel_name(): void { $data = $this->channelList(['data_type' => '1', 'payment_company' => 'jd']); $this->assertSame(1, $data['total']); $this->assertSame('JD钱包', $data['data'][0]['name']); $this->assertSame('JD支付', $data['data'][0]['payment_company_label']); $this->assertSame(1, $data['data'][0]['from']); $this->assertSame(24, $data['data'][0]['id']); $withdraw = $this->channelList(['data_type' => '2', 'payment_company' => 'jd']); $this->assertSame(25, $withdraw['data'][0]['id']); $this->assertSame(0, $this->channelList(['data_type' => '2', 'payment_company' => 'sanjin'])['total']); } public function test_missing_group_returns_reason_without_enabling_or_rewriting_config(): void { DB::table('recharge_channel_group')->update(['recharge_type' => 'wxsm,zfbsm']); $this->request('/admin/paymentConfig/options', ['kind' => 'deposit']); $data = (new PaymentConfiguration(new PaymentChannelLinkService()))->options()->getData(true)['data']; $this->assertSame([false, true, true, true], array_column($data['payment_companies'], 'disabled')); $this->assertSame(['', 'group_missing', 'group_missing', 'group_missing'], array_column($data['payment_companies'], 'disabled_reason_code')); $this->assertSame('wxsm,zfbsm', DB::table('recharge_channel_group')->value('recharge_type')); $this->assertSame(0, DB::table('recharge_channel')->where('id', 31)->value('status')); } public function test_group_editor_options_include_new_types_even_before_any_group_uses_them(): void { DB::table('recharge_channel_group')->update(['recharge_type' => 'wxsm', 'withdraw_type' => 'DF001']); $this->request('/admin/rechargeChannel/getChannel', ['data_type' => 1]); $response = (new RechargeChannel())->getChannel()->getData(true); $this->assertSame(0, $response['code']); $names = array_column($response['data']['data'], 'name', 'type'); $this->assertSame('JD钱包', $names['JDpay']); $this->assertSame('NO快捷充值-扫码支付', $names['NOpay12']); $this->assertSame('NO快捷充值-余额支付', $names['NOpay13']); $this->assertSame('808充值', $names['ZIMUpay']); $this->assertSame(13, $response['data']['total']); $this->request('/admin/rechargeChannel/getChannel', ['data_type' => 2]); $names = array_column((new RechargeChannel())->getChannel()->getData(true)['data']['data'], 'name', 'type'); $this->assertSame('JD钱包', $names['JDpay']); $this->assertSame('NO快捷提现', $names['NOwithdraw']); $this->assertSame('808账户提现', $names['ZIMUwithdraw']); $this->assertArrayNotHasKey('ZIMUcash', $names); $this->assertSame('wxsm', DB::table('recharge_channel_group')->value('recharge_type')); } private function formOptions(int $dataType = 1, ?int $id = null): array { $this->request('/admin/rechargeChannel/options', ['data_type' => $dataType] + ($id ? ['id' => $id] : [])); $response = (new RechargeChannel())->options(new RechargeChannelConfigurationService())->getData(true); $this->assertSame(0, $response['code']); return $response['data']; } private function saveChannel(array $params): array { $this->app->instance('request', Request::create('/admin/rechargeChannel/update', 'POST', $params)); return (new RechargeChannel())->update(new RechargeChannelConfigurationService())->getData(true); } private function newChannel(array $overrides = []): array { return $overrides + ['data_type' => 1, 'payment_company' => 'no', 'type' => 'NOpay12', 'rate' => '0.0300', 'name' => '测试通道', 'key' => 'test-key', 'min' => 1, 'max' => 100]; } public function test_creation_catalog_works_without_any_database_channels_or_groups(): void { DB::table('recharge_channel')->delete(); DB::table('recharge_channel_group')->delete(); $data = $this->formOptions(); $this->assertSame(['sanjin', 'jd', 'no', 'zimu'], array_column($data['payment_companies'], 'value')); $this->assertTrue($data['identity_editable']); $this->assertSame('', $data['identity_edit_reason']); $this->assertContains('NOpay12', array_column($data['types'], 'value')); $this->assertSame(['qianbao', 'jd', 'no', 'zimu'], array_column($this->formOptions(2)['payment_companies'], 'value')); $this->assertSame([], $this->formOptions(3)['payment_companies']); } public function test_create_selected_company_then_switch_company_and_type_on_same_record(): void { $result = $this->saveChannel($this->newChannel()); $this->assertSame(0, $result['code']); $this->assertSame([], $result['data']); $row = DB::table('recharge_channel')->where('key', 'test-key')->first(); $this->assertSame(1, $row->from); $this->assertSame(1, $row->status); $this->assertSame(0, $row->sort); $result = $this->saveChannel(['id' => $row->id, 'data_type' => 1, 'payment_company' => 'jd', 'type' => 'JDpay', 'rate' => '0.02']); $this->assertSame(0, $result['code']); $data = $this->channelList(['data_type' => '1', 'key' => 'test-key'])['data'][0]; $this->assertSame($row->id, $data['id']); $this->assertSame('jd', $data['payment_company']); $this->assertSame('JD支付 / 测试通道', $data['display_name']); } public function test_invalid_company_type_or_source_is_rejected_without_writing(): void { $before = DB::table('recharge_channel')->count(); foreach ([ ['payment_company' => 'jd', 'type' => 'NOpay12'], ['payment_company' => 'no', 'type' => '12'], ['payment_company' => 'no', 'type' => 'usdt'], ['payment_company' => 'no', 'from' => 2], ['payment_company' => 'sanjin', 'type' => 'DF001', 'data_type' => 2], ] as $override) { $this->assertSame(-3, $this->saveChannel($this->newChannel($override))['code']); } $this->assertSame($before, DB::table('recharge_channel')->count()); } public function test_native_and_activity_channels_do_not_require_or_fabricate_company(): void { foreach ([[1, 'usdt', 2], [1, 'rgcz', 3], [2, 'rgtx', 3], [3, 'recharge', 1]] as [$direction, $type, $from]) { $result = $this->saveChannel($this->newChannel(['payment_company' => null, 'data_type' => $direction, 'type' => $type, 'key' => 'local-' . $type])); $this->assertSame(0, $result['code']); $row = $this->channelList(['key' => 'local-' . $type])['data'][0]; $this->assertSame($from, $row['from']); $this->assertNull($row['payment_company']); $this->assertSame('测试通道', $row['display_name']); } } public function test_existing_valid_clients_can_omit_derived_company_and_from(): void { $params = $this->newChannel(); unset($params['payment_company']); $this->assertSame(0, $this->saveChannel($params)['code']); $row = $this->channelList(['key' => 'test-key'])['data'][0]; $this->assertSame('no', $row['payment_company']); $this->assertSame(1, $row['from']); } public function test_linked_channel_locks_identity_but_keeps_regular_edit_available(): void { DB::connection()->getSchemaBuilder()->create('payment_gateways', function (Blueprint $t) { $t->id(); $t->unsignedBigInteger('recharge_channel_id'); $t->softDeletes(); }); DB::table('payment_gateways')->insert(['recharge_channel_id' => 24]); $options = $this->formOptions(1, 24); $this->assertFalse($options['identity_editable']); $this->assertNotSame('', $options['identity_edit_reason']); $result = $this->saveChannel(['id' => 24, 'data_type' => 1, 'payment_company' => 'no', 'type' => 'NOpay12', 'rate' => '0.01']); $this->assertSame(-3, $result['code']); $this->assertSame('JDpay', DB::table('recharge_channel')->where('id', 24)->value('type')); $result = $this->saveChannel(['id' => 24, 'data_type' => 1, 'payment_company' => 'jd', 'type' => 'JDpay', 'rate' => '0.01', 'name' => 'JD新名称']); $this->assertSame(0, $result['code']); DB::table('payment_gateways')->update(['deleted_at' => now()]); $this->assertTrue($this->formOptions(1, 24)['identity_editable']); $this->assertSame(0, $this->saveChannel(['id' => 24, 'data_type' => 1, 'payment_company' => 'no', 'type' => 'NOpay12', 'rate' => '0.01'])['code']); } public function test_switching_company_does_not_rewrite_group_permissions(): void { $groups = DB::table('recharge_channel_group')->orderBy('id')->get()->toJson(); $this->assertSame(0, $this->saveChannel(['id' => 24, 'data_type' => 1, 'payment_company' => 'no', 'type' => 'NOpay12', 'rate' => '0.01'])['code']); $this->assertSame($groups, DB::table('recharge_channel_group')->orderBy('id')->get()->toJson()); $this->assertSame(-3, $this->saveChannel(['id' => 24, 'data_type' => 2, 'payment_company' => 'no', 'type' => 'NOwithdraw', 'rate' => '0.01'])['code']); } public function test_group_option_names_include_company_without_changing_values(): void { $this->request('/admin/rechargeChannel/getChannel', ['data_type' => 1]); $rows = array_column((new RechargeChannel())->getChannel()->getData(true)['data']['data'], null, 'type'); $this->assertSame('JD钱包', $rows['JDpay']['name']); $this->assertSame('jd', $rows['JDpay']['payment_company']); $this->assertSame('JD支付 / JD钱包', $rows['JDpay']['display_name']); $this->assertSame('USDT充值', $rows['usdt']['display_name']); } public function test_merchant_save_rechecks_channel_identity_inside_transaction(): void { DB::table('recharge_channel')->where('id', 26)->update(['type' => 'JDpay']); $this->request('/admin/paymentConfig/gateways/save', []); $controller = new PaymentConfiguration(new PaymentChannelLinkService()); $method = new \ReflectionMethod($controller, 'saveModel'); $method->setAccessible(true); $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('支付公司与所选通道不匹配'); $method->invoke($controller, PaymentGateway::class, ['kind' => 'deposit', 'recharge_channel_id' => 26, 'recharge_channel_group_ids' => [1], 'payment_company' => 'no', 'payment_method' => 'NOpay12'], 'payment_gateway'); } private function setCompanyStatus(string $code, int $status): array { $this->app->instance('request', Request::create('/admin/paymentCompany/status', 'POST', ['payment_company' => $code, 'status' => $status])); return (new PaymentCompany())->status(new PaymentProviderService())->getData(true); } private function deleteChannel(int $id): array { $this->app->instance('request', Request::create('/admin/rechargeChannel/delete', 'POST', ['id' => $id])); return (new RechargeChannel())->delete(new RechargeChannelConfigurationService())->getData(true); } public function test_company_management_lists_registered_providers_and_filters_status(): void { $this->request('/admin/paymentCompany/list', []); $result = (new PaymentCompany())->list(new PaymentProviderService())->getData(true); $this->assertSame(0, $result['code']); $this->assertSame(5, $result['data']['total']); $rows = array_column($result['data']['data'], null, 'payment_company'); $this->assertSame('SHA256', $rows['no']['signature_algorithm']); $this->assertSame([1, 2], $rows['no']['data_types']); $this->assertSame(3, $rows['no']['channel_count']); $this->assertSame(0, $this->setCompanyStatus('no', 0)['code']); $this->assertSame(0, $this->setCompanyStatus('no', 0)['code']); $this->assertSame(1, DB::table('operation_audits')->where('resource_type', 'payment_company')->count()); $this->request('/admin/paymentCompany/list', ['status' => 0]); $this->assertSame(1, (new PaymentCompany())->list(new PaymentProviderService())->getData(true)['data']['total']); $this->assertSame(-3, $this->setCompanyStatus('unknown', 0)['code']); $this->assertSame(-3, $this->setCompanyStatus('no', 9)['code']); } public function test_company_switch_disables_options_and_runtime_without_rewriting_channels(): void { $groups = DB::table('recharge_channel_group')->get()->toJson(); $this->assertSame(0, $this->setCompanyStatus('no', 0)['code']); $this->assertFalse(ChannelModel::checkRechargeChannel('NOpay12', 1)); $this->assertFalse(ChannelModel::checkWithdrawChannel('NOwithdraw', 1)); $this->assertNotFalse(ChannelModel::checkRechargeChannel('JDpay', 1)); $this->assertNotFalse(ChannelModel::checkRechargeChannel('usdt', 1)); $this->assertArrayNotHasKey('NOpay12', ChannelModel::product(1)); $this->assertSame(1, DB::table('recharge_channel')->where('id', 26)->value('status')); $this->assertSame($groups, DB::table('recharge_channel_group')->get()->toJson()); $this->request('/admin/paymentConfig/options', ['kind' => 'deposit']); $options = (new PaymentConfiguration(new PaymentChannelLinkService()))->options()->getData(true)['data']; $companies = array_column($options['payment_companies'], null, 'value'); $this->assertSame('provider_disabled', $companies['no']['disabled_reason_code']); $this->assertFalse(array_column($options['recharge_channels'], null, 'id')[26]['selectable']); $form = $this->formOptions(); $this->assertTrue(array_column($form['payment_companies'], null, 'value')['no']['disabled']); $row = $this->channelList(['data_type' => '1', 'type' => 'NOpay12'])['data'][0]; $this->assertSame(1, $row['status']); $this->assertSame(0, $row['company_status']); $this->assertSame(0, $row['effective_status']); $this->assertSame(-3, $this->saveChannel($this->newChannel())['code']); $this->assertSame(0, $this->setCompanyStatus('no', 1)['code']); $this->assertNotFalse(ChannelModel::checkRechargeChannel('NOpay12', 1)); } public function test_reenable_company_does_not_enable_individually_disabled_channels(): void { $this->setCompanyStatus('zimu', 0); $this->setCompanyStatus('zimu', 1); $this->assertSame(0, DB::table('recharge_channel')->where('id', 31)->value('status')); $this->assertFalse(ChannelModel::checkWithdrawChannel('ZIMUcash', 1)); $this->assertNotFalse(ChannelModel::checkWithdrawChannel('ZIMUwithdraw', 1)); } public function test_disabled_company_blocks_new_external_requests_before_any_wallet_or_network_work(): void { $this->setCompanyStatus('no', 0); $this->assertSame('支付公司已禁用', PaymentOrderService::createPay('test-user', 100, 'NOpay12')['text']); $this->assertSame('支付公司已禁用', PaymentOrderService::autoCreatePayout('test-user', 100, 'NOwithdraw', '', '', '')['text']); $this->assertFalse(PaymentProviderService::requestEnabled('nopay12', 1)); $this->assertTrue(PaymentProviderService::requestEnabled('JDpay', 1)); } public function test_delete_last_deposit_type_soft_deletes_and_cleans_only_its_group_membership(): void { $beforeWithdraw = DB::table('recharge_channel_group')->where('id', 1)->value('withdraw_type'); $this->assertSame(0, $this->deleteChannel(24)['code']); $this->assertNull(ChannelModel::query()->find(24)); $deleted = ChannelModel::withTrashed()->findOrFail(24); $this->assertTrue($deleted->trashed()); $this->assertSame(0, (int)$deleted->status); $this->assertSame('JD钱包', $deleted->name); $types = explode(',', DB::table('recharge_channel_group')->where('id', 1)->value('recharge_type')); $this->assertNotContains('JDpay', $types); $this->assertContains('NOpay12', $types); $this->assertSame($beforeWithdraw, DB::table('recharge_channel_group')->where('id', 1)->value('withdraw_type')); $this->assertNotFalse(ChannelModel::checkWithdrawChannel('JDpay', 1)); $this->assertFalse(ChannelModel::checkRechargeChannel('JDpay', 1)); $this->assertSame(0, $this->deleteChannel(24)['code']); $this->assertSame(1, DB::table('operation_audits')->where('resource_type', 'recharge_channel')->count()); $this->setCompanyStatus('jd', 0); $this->setCompanyStatus('jd', 1); $this->assertNull(ChannelModel::query()->find(24)); } public function test_delete_one_of_several_same_type_channels_preserves_groups_until_last_removed(): void { $this->assertSame(0, $this->deleteChannel(6)['code']); $this->assertStringContainsString('zfbsm', DB::table('recharge_channel_group')->where('id', 1)->value('recharge_type')); $this->assertSame(3, ChannelModel::query()->where('data_type', 1)->where('type', 'zfbsm')->count()); foreach ([7, 8, 9] as $id) $this->assertSame(0, $this->deleteChannel($id)['code']); $this->assertStringNotContainsString('zfbsm', DB::table('recharge_channel_group')->where('id', 1)->value('recharge_type')); } public function test_delete_is_blocked_by_payment_config_reference_and_does_not_touch_activity(): void { DB::connection()->getSchemaBuilder()->create('payment_gateways', function (Blueprint $t) { $t->id(); $t->unsignedBigInteger('recharge_channel_id'); $t->softDeletes(); }); DB::table('payment_gateways')->insert(['recharge_channel_id' => 24]); $this->assertSame(-3, $this->deleteChannel(24)['code']); $this->assertNotNull(ChannelModel::query()->find(24)); $this->assertSame(-3, $this->deleteChannel(21)['code']); $this->assertNotNull(ChannelModel::query()->find(21)); DB::table('payment_gateways')->update(['deleted_at' => now()]); $this->assertSame(0, $this->deleteChannel(24)['code']); } public function test_migration_retry_keeps_disabled_company_and_deleted_channel(): void { $this->setCompanyStatus('no', 0); $this->deleteChannel(24); (require dirname(__DIR__, 2) . '/database/migrations/2026_09_08_160000_add_payment_provider_status_and_channel_soft_deletes.php')->up(); $this->assertSame(0, PaymentProviderService::statuses()['no']); $this->assertTrue(ChannelModel::withTrashed()->findOrFail(24)->trashed()); $this->assertSame(5, DB::table('payment_providers')->count()); } private function paymentOrderFixture(int $type, string $channel, int $status): \App\Models\PaymentOrder { DB::connection()->getSchemaBuilder()->create('payment_orders', function (Blueprint $t) { $t->id(); $t->integer('type'); $t->string('channel'); $t->integer('status'); $t->string('order_no'); $t->string('member_id'); $t->decimal('amount', 18, 4); $t->string('state')->nullable(); $t->text('callback_data')->nullable(); $t->timestamps(); }); $this->app->instance('log', new class { public function channel($channel) { return $this; } public function error($message, array $context = []) {} }); return \App\Models\PaymentOrder::query()->create(['type' => $type, 'channel' => $channel, 'status' => $status, 'order_no' => 'test-order', 'member_id' => 'test-user', 'amount' => 10]); } public function test_pending_payout_cannot_be_sent_after_company_disabled(): void { $order = $this->paymentOrderFixture(2, 'NOwithdraw', PaymentOrderService::STATUS_STAY); $this->setCompanyStatus('no', 0); $result = PaymentOrderService::createPayout($order->id); $this->assertSame(-3, $result['code']); $this->assertSame('支付公司已禁用', $result['msg']); $this->assertSame(PaymentOrderService::STATUS_STAY, $order->fresh()->status); } public function test_pending_payout_cannot_be_sent_after_last_channel_deleted(): void { $order = $this->paymentOrderFixture(2, 'JDpay', PaymentOrderService::STATUS_STAY); $this->assertSame(0, $this->deleteChannel(25)['code']); $result = PaymentOrderService::createPayout($order->id); $this->assertSame(-3, $result['code']); $this->assertSame('提现通道已停用或删除', $result['msg']); $this->assertSame(PaymentOrderService::STATUS_STAY, $order->fresh()->status); } public function test_existing_pay_callback_is_not_blocked_by_company_switch(): void { $order = $this->paymentOrderFixture(1, 'NOpay12', PaymentOrderService::STATUS_PROCESS); $this->setCompanyStatus('no', 0); $method = new \ReflectionMethod(PaymentOrderService::class, 'applyPayCallback'); $method->setAccessible(true); $this->assertTrue($method->invoke(null, $order, '10', 'failed', 'success', 'failed', ['status' => 'failed'])); $this->assertSame(PaymentOrderService::STATUS_FAIL, $order->fresh()->status); $this->assertNotNull($order->fresh()->callback_data); } public function test_company_disabled_updates_saved_gateway_effective_status(): void { $channel = ChannelModel::query()->findOrFail(26); $gateway = new PaymentGateway(['payment_company' => 'no', 'payment_method' => 'NOpay12', 'status' => 1, 'recharge_channel_group_ids' => [1]]); $gateway->setRelation('rechargeChannel', $channel); $this->setCompanyStatus('no', 0); $data = (new PaymentChannelLinkService())->formatOne($gateway); $this->assertSame(1, $data['config_status']); $this->assertSame(1, $data['channel_status']); $this->assertSame(0, $data['company_status']); $this->assertSame(0, $data['effective_status']); $this->expectException(\RuntimeException::class); $this->expectExceptionMessage('NO支付已禁用'); (new PaymentChannelLinkService())->validate(26, [1], 1); } public function test_empty_group_types_can_be_saved_after_last_channel_removed(): void { $this->app->instance('request', Request::create('/admin/rechargeChannel/updateGroup', 'POST', ['id' => 1, 'name' => '全部充值', 'recharge_type' => [], 'withdraw_type' => []])); $this->assertSame(0, (new RechargeChannel())->updateGroup()->getData(true)['code']); $this->assertSame('', DB::table('recharge_channel_group')->where('id', 1)->value('recharge_type')); $this->assertSame('yuebao,old_user,recharge', DB::table('recharge_channel_group')->where('id', 1)->value('activity_type')); } private function saveGroup(array $params): array { $this->app->instance('request', Request::create('/admin/rechargeChannel/updateGroup', 'POST', $params)); return (new RechargeChannel())->updateGroup()->getData(true); } private function groupPayload(int $id = 1): array { $group = DB::table('recharge_channel_group')->where('id', $id)->first(); return ['id' => $id, 'name' => $group->name, 'recharge_type' => explode(',', $group->recharge_type), 'withdraw_type' => explode(',', $group->withdraw_type)]; } public function test_legacy_group_can_be_saved_and_only_invalid_existing_types_are_cleaned(): void { $payload = $this->groupPayload(); $result = $this->saveGroup($payload); $this->assertSame(0, $result['code']); $this->assertSame([], $result['data']); $this->assertStringContainsString('已清理', $result['msg']); $saved = $this->groupPayload(); $this->assertSame(array_values(array_diff($payload['recharge_type'], ['1', '2', '3'])), $saved['recharge_type']); $this->assertSame(array_values(array_diff($payload['withdraw_type'], ['ylk', 'zfb', 'szrmb'])), $saved['withdraw_type']); $this->assertSame('yuebao,old_user,recharge', DB::table('recharge_channel_group')->where('id', 1)->value('activity_type')); $audit = DB::table('operation_audits')->where('resource_type', 'recharge_channel_group')->first(); $this->assertNotNull($audit); $this->assertSame(['1', '2', '3'], json_decode($audit->changes, true)['after']['removed_types']['recharge_type']); } public function test_new_unknown_type_in_existing_group_is_rejected_atomically(): void { $payload = $this->groupPayload(); $payload['recharge_type'][] = 'not_a_type'; $before = DB::table('recharge_channel_group')->where('id', 1)->first(); $result = $this->saveGroup($payload); $this->assertSame(-3, $result['code']); $this->assertStringContainsString('not_a_type', $result['msg']); $this->assertSame($before->recharge_type, DB::table('recharge_channel_group')->where('id', 1)->value('recharge_type')); $this->assertSame(0, DB::table('operation_audits')->count()); } public function test_new_group_cannot_reuse_invalid_values_from_another_group(): void { $payload = $this->groupPayload(); unset($payload['id']); $result = $this->saveGroup($payload); $this->assertSame(-3, $result['code']); $this->assertStringContainsString('1,2,3', $result['msg']); $this->assertSame(2, DB::table('recharge_channel_group')->count()); } public function test_group_types_trim_deduplicate_and_keep_disabled_company_membership(): void { $this->setCompanyStatus('no', 0); $result = $this->saveGroup(['id' => 1, 'name' => '组合', 'recharge_type' => [' JDpay ', 'JDpay', ' NOpay12 '], 'withdraw_type' => [' DF001 ']]); $this->assertSame(0, $result['code']); $this->assertSame('JDpay,NOpay12', DB::table('recharge_channel_group')->where('id', 1)->value('recharge_type')); $this->assertSame('DF001', DB::table('recharge_channel_group')->where('id', 1)->value('withdraw_type')); } public function test_nested_option_objects_are_rejected_without_writing(): void { $result = $this->saveGroup(['id' => 1, 'name' => '组合', 'recharge_type' => [['type' => 'JDpay', 'name' => 'JD钱包']], 'withdraw_type' => ['DF001']]); $this->assertSame(-3, $result['code']); $this->assertStringContainsString('type', $result['msg']); $this->assertSame(0, DB::table('operation_audits')->count()); } public function test_existing_deleted_type_is_removed_and_not_restored(): void { DB::table('recharge_channel')->where('id', 24)->update(['deleted_at' => now(), 'status' => 0]); $this->assertSame(0, $this->saveGroup($this->groupPayload())['code']); $saved = $this->groupPayload(); $this->assertNotContains('JDpay', $saved['recharge_type']); $this->assertContains('JDpay', $saved['withdraw_type']); $this->assertTrue(ChannelModel::withTrashed()->findOrFail(24)->trashed()); } private function deleteGroup(int $id): array { $this->app->instance('request', Request::create('/admin/rechargeChannel/deleteGroup', 'POST', ['id' => $id])); return (new RechargeChannel())->deleteGroup(new RechargeChannelConfigurationService())->getData(true); } public function test_unreferenced_group_can_be_soft_deleted_without_deleting_channels(): void { $count = ChannelModel::query()->count(); $this->assertSame(0, $this->deleteGroup(2)['code']); $this->assertNull(\App\Models\RechargeChannelGroup::query()->find(2)); $this->assertTrue(\App\Models\RechargeChannelGroup::withTrashed()->findOrFail(2)->trashed()); $this->assertSame($count, ChannelModel::query()->count()); $this->assertSame(0, $this->deleteGroup(2)['code']); $this->assertSame(1, DB::table('operation_audits')->where('action', 'delete')->count()); $this->request('/admin/rechargeChannel/groupList', []); $this->assertSame(1, (new RechargeChannel())->groupList()->getData(true)['data']['total']); $this->assertSame(-3, $this->saveGroup(['id' => 2, 'name' => '已删除', 'recharge_type' => [], 'withdraw_type' => []])['code']); } public function test_default_group_and_member_assigned_group_cannot_be_deleted(): void { $this->assertSame(-3, $this->deleteGroup(1)['code']); $this->assertSame(-3, $this->deleteGroup(999)['code']); DB::connection()->getSchemaBuilder()->create('users', function (Blueprint $t) { $t->id(); $t->string('member_id'); $t->unsignedBigInteger('recharge_channel_group_id')->nullable(); $t->timestamps(); }); DB::table('users')->insert(['member_id' => 'test', 'recharge_channel_group_id' => 2]); $result = $this->deleteGroup(2); $this->assertSame(-3, $result['code']); $this->assertStringContainsString('会员', $result['msg']); $this->assertNotNull(\App\Models\RechargeChannelGroup::query()->find(2)); $this->assertSame(0, DB::table('operation_audits')->count()); } public function test_numeric_and_string_payment_config_group_references_block_delete(): void { DB::connection()->getSchemaBuilder()->create('payment_collection_channels', function (Blueprint $t) { $t->id(); $t->json('recharge_channel_group_ids'); $t->softDeletes(); }); foreach (['[2]', '["2"]', '["02"]'] as $json) { DB::table('payment_collection_channels')->delete(); DB::table('payment_collection_channels')->insert(['recharge_channel_group_ids' => $json]); $result = $this->deleteGroup(2); $this->assertSame(-3, $result['code']); $this->assertStringContainsString('被支付配置引用', $result['msg']); $this->assertNotNull(\App\Models\RechargeChannelGroup::query()->find(2)); } DB::table('payment_collection_channels')->update(['deleted_at' => now()]); $this->assertSame(0, $this->deleteGroup(2)['code']); } public function test_deleted_group_cannot_be_assigned_to_members_or_new_payment_config(): void { DB::connection()->getSchemaBuilder()->create('users', function (Blueprint $t) { $t->id(); $t->string('member_id'); $t->unsignedBigInteger('recharge_channel_group_id')->nullable(); $t->timestamps(); }); DB::table('users')->insert(['member_id' => 'test', 'recharge_channel_group_id' => 1]); $this->assertSame(0, $this->deleteGroup(2)['code']); $this->app->instance('request', Request::create('/admin/user/setRechargeChannelGroup', 'POST', ['member_id' => ['test'], 'recharge_channel_group_id' => 2])); $this->assertSame(-3, (new \App\Http\Controllers\admin\User())->setRechargeChannelGroup()->getData(true)['code']); $this->assertSame(1, DB::table('users')->where('member_id', 'test')->value('recharge_channel_group_id')); $this->expectException(\RuntimeException::class); $this->expectExceptionMessage('部分通道组合不存在'); (new PaymentChannelLinkService())->validate(24, [2], 1, true); } }