| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- <?php
- namespace Tests\Integration;
- use App\Http\Controllers\admin\Maintenance as MaintenanceController;
- use App\Http\Middleware\MemberMaintenance;
- use App\Http\Middleware\OperationsAdminMiddleware;
- use App\Services\MaintenanceService;
- use App\Services\Sharing\ShareDefaults;
- use App\Support\MaintenancePolicy as Policy;
- use Carbon\Carbon;
- use Illuminate\Config\Repository;
- use Illuminate\Database\Capsule\Manager;
- use Illuminate\Database\Schema\Blueprint;
- use Illuminate\Events\Dispatcher;
- use Illuminate\Foundation\Application;
- use Illuminate\Http\JsonResponse;
- use Illuminate\Http\Request;
- use Illuminate\Routing\Router;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Facade;
- use Illuminate\Support\Facades\Schema;
- use Illuminate\Translation\ArrayLoader;
- use Illuminate\Translation\Translator;
- use Illuminate\Validation\Factory;
- use Illuminate\Validation\ValidationException;
- use PHPUnit\Framework\Attributes\DataProvider;
- use PHPUnit\Framework\TestCase;
- class MaintenanceTest extends TestCase
- {
- /** Production game_list: list `type` is a display id, `params.type` is the game's own type. */
- private const GAME_LIST = [['type' => 1, 'name' => '加拿大28', 'game' => 'canada28', 'status' => 1, 'sort' => 0],
- ['type' => 2, 'name' => '极速28', 'game' => 'jisu28', 'status' => 1, 'sort' => 0],
- ['type' => 3, 'name' => '澳门六合彩', 'game' => 'lhc', 'params' => ['type' => 3], 'status' => 1, 'sort' => 0],
- ['type' => 4, 'name' => '新澳门六合彩', 'game' => 'lhc', 'params' => ['type' => 1], 'status' => 1, 'sort' => 0],
- ['type' => 5, 'name' => '极速六合彩', 'game' => 'lhc', 'params' => ['type' => 4], 'status' => 1, 'sort' => 0],
- ['type' => 6, 'name' => '香港六合彩', 'game' => 'lhc', 'params' => ['type' => 2], 'status' => 1, 'sort' => 0],
- ['type' => 7, 'name' => '极速赛车', 'game' => 'jisu', 'params' => ['type' => 1], 'status' => 1, 'sort' => 0],
- ['type' => 8, 'name' => '极速时时彩', 'game' => 'jisu', 'params' => ['type' => 2], 'status' => 1, 'sort' => 0],
- ['type' => 9, 'name' => '极速飞艇', 'game' => 'jisu', 'params' => ['type' => 3], 'status' => 1, 'sort' => 0],
- ['type' => 13, 'name' => '极速快3', 'game' => 'jisu', 'params' => ['type' => 6], 'status' => 1, 'sort' => 0],
- ['type' => 10, 'name' => 'SG时时彩', 'game' => 'jisu', 'params' => ['type' => 4], 'status' => 1, 'sort' => 0],
- ['type' => 11, 'name' => 'SG飞艇', 'game' => 'jisu', 'params' => ['type' => 5], 'status' => 1, 'sort' => 0],
- ['type' => 12, 'name' => 'SG快3', 'game' => 'jisu', 'params' => ['type' => 7], 'status' => 0, 'sort' => 0]];
- private Application $app;
- private MaintenanceService $service;
- protected function setUp(): void
- {
- Carbon::setTestNow(Carbon::parse('2026-10-26 10:00:00', 'Asia/Shanghai'));
- Facade::clearResolvedInstances();
- $this->app = new Application(dirname(__DIR__, 2));
- $this->app->instance('config', new Repository(['app' => ['locale' => 'zh', 'timezone' => 'Asia/Shanghai'], 'operations' => ['admin_ids' => [1]]]));
- Facade::setFacadeApplication($this->app);
- $db = new Manager($this->app); $db->addConnection(['driver' => 'sqlite', 'database' => ':memory:', 'prefix' => 'bot_', 'foreign_key_constraints' => true]);
- $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('log', new \Psr\Log\NullLogger());
- $this->app->instance(\Illuminate\Contracts\Routing\ResponseFactory::class, new \Tests\Support\JsonResponseFactory());
- Request::macro('validate', function (array $rules) { return app('validator')->make($this->all(), $rules)->validate(); });
- Schema::create('operation_audits', function (Blueprint $t) {
- $t->id(); $t->string('resource_type'); $t->integer('resource_id'); $t->string('action'); $t->json('changes');
- $t->integer('operator_id'); $t->string('operator_name'); $t->timestamp('created_at')->useCurrent();
- });
- Schema::create('users', function (Blueprint $t) { $t->id(); $t->string('user_id'); $t->string('language'); $t->bigInteger('delete_time')->nullable(); $t->integer('is_banned')->default(0); });
- Schema::create('user_session', function (Blueprint $t) { $t->id(); $t->string('user_id'); $t->string('token'); $t->bigInteger('expire_time'); $t->bigInteger('create_time'); $t->bigInteger('update_time'); });
- Schema::create('config', function (Blueprint $t) { $t->id(); $t->string('field'); $t->text('val'); $t->string('remark')->nullable(); $t->integer('group_id')->default(1); $t->timestamps(); });
- DB::table('config')->insert([['field' => 'game_list', 'val' => json_encode(self::GAME_LIST, JSON_UNESCAPED_UNICODE), 'group_id' => 5],
- ['field' => 'pc28_switch', 'val' => '0', 'group_id' => 5]]);
- (require dirname(__DIR__, 2) . '/database/migrations/2026_11_10_130000_add_sport_list_config.php')->up();
- $this->migration()->up(); (require dirname(__DIR__, 2) . '/database/migrations/2026_10_27_150000_create_site_realtime_events.php')->up(); $this->service = new MaintenanceService(); $this->request();
- }
- protected function tearDown(): void { Carbon::setTestNow(); DB::disconnect(); Facade::clearResolvedInstances(); parent::tearDown(); }
- private function migration() { return require dirname(__DIR__, 2) . '/database/migrations/2026_10_26_120000_create_maintenance_settings.php'; }
- private function request(array $params = []): Request
- {
- $request = Request::create('/admin/maintenance/submit', 'POST', $params); $request->user = (object) ['id' => 1, 'username' => 'admin'];
- $this->app->instance('request', $request); return $request;
- }
- private function id(string $scope = 'site'): int { return (int) DB::table('maintenance_settings')->where('scope_code', $scope)->value('id'); }
- private function save(array $values = [], string $scope = 'site'): array
- {
- return $this->service->save($values + ['id' => $this->id($scope), 'enabled' => 1, 'starts_at' => '2026-10-26 09:00:00']);
- }
- private function decide(?string $platform = 'PC', ?string $category = null, ?string $subcategory = null): ?array
- {
- return Policy::select($this->service->runtimeRows('zh'), $platform, $category, now()->getTimestamp(), $subcategory);
- }
- public function test_websocket_events_use_local_delivery_times_for_utc_maintenance_and_cancel_disabled_boundaries(): void
- {
- $row = $this->save(['starts_at' => '2026-10-26 11:00:00', 'ends_at' => '2026-10-26 12:00:00']);
- $this->assertSame(['update', 'start', 'end'], DB::table('site_realtime_events')->orderBy('id')->pluck('action')->all());
- $this->assertSame(['2026-10-26 10:00:00', '2026-10-26 11:00:00', '2026-10-26 12:00:00'], DB::table('site_realtime_events')->orderBy('id')->pluck('available_at')->all());
- $this->service->status($row['id'], 0); $this->service->status($row['id'], 0);
- $this->assertSame(['update', 'update'], DB::table('site_realtime_events')->orderBy('id')->pluck('action')->all());
- DB::beginTransaction(); $this->save(); DB::rollBack();
- $this->assertSame(2, DB::table('site_realtime_events')->count());
- }
- public function test_migration_defaults_and_catalog_match_commission_types(): void
- {
- $this->assertSame(10, DB::table('maintenance_settings')->count());
- $this->assertSame(0, DB::table('maintenance_settings')->where('enabled', 1)->count());
- $this->assertSame(ShareDefaults::categories(), Policy::CATEGORIES);
- $all = $this->service->listing([]);
- $this->assertSame(10, $all['total'], 'only site and categories are paged');
- $this->assertSame(count(self::GAME_LIST) + count(Policy::SPORTS), array_sum(array_map(fn ($r) => count($r['children']), $all['data'])), 'subcategories are nested');
- $this->assertSame([], $all['data'][0]['children'], 'site has no children');
- $this->assertSame(0, DB::table('maintenance_settings')->where('enabled', 1)->count());
- $this->assertNull($this->decide());
- $this->assertContains('UTC', $this->service->options()['timezones']);
- $this->assertSame(['zh', 'en', 'vi'], array_column($this->service->options()['languages'], 'value'));
- $this->migration()->down(); $this->assertFalse(Schema::hasTable('maintenance_settings')); $this->assertTrue(Schema::hasTable('users'));
- }
- public function test_utc_conversion_and_exact_start_end_without_scheduler(): void
- {
- $row = $this->save(['starts_at' => '2026-10-26 10:00:00', 'ends_at' => '2026-10-26 11:00:00']);
- $this->assertSame('active', $row['state']); $this->assertSame('2026-10-26T02:00:00Z', $row['starts_at_utc']);
- $this->assertSame('2026-10-26 02:00:00', DB::table('maintenance_settings')->where('id', $row['id'])->value('starts_at'));
- Carbon::setTestNow(Carbon::parse('2026-10-26 09:59:59', 'Asia/Shanghai')); $this->assertNull($this->decide());
- Carbon::setTestNow(Carbon::parse('2026-10-26 10:00:00', 'Asia/Shanghai')); $this->assertSame('site', $this->decide()['scope_code']);
- Carbon::setTestNow(Carbon::parse('2026-10-26 11:00:00', 'Asia/Shanghai')); $this->assertNull($this->decide());
- $this->assertSame('expired', $this->service->detail($row['id'])['state']);
- $this->assertSame(1, DB::table('maintenance_settings')->where('id', $row['id'])->value('enabled'));
- $this->assertSame('expired', $this->service->status($row['id'], 1)['state']);
- $this->assertSame('active', $this->service->save(['id' => $row['id'], 'start_now' => true, 'ends_at' => null])['state']);
- }
- public function test_site_precedence_applies_only_to_effective_platform_and_time(): void
- {
- $this->save(['platforms' => ['H5']]); $this->save(['platforms' => ['PC', 'H5']], 'third:4');
- $this->assertSame('site', $this->decide('H5', 'third:4')['scope_code']);
- $this->assertSame('third:4', $this->decide('PC', 'third:4')['scope_code']);
- $this->assertNull($this->decide('APP', 'third:4')); $this->assertNull($this->decide('PC', 'self:sport'));
- $this->assertSame('site', $this->decide(null, 'third:4')['scope_code']);
- $this->save(['starts_at' => '2026-10-27 00:00:00']);
- $this->assertSame('third:4', $this->decide('H5', 'third:4')['scope_code']);
- $this->save(['ends_at' => '2026-10-26 10:00:00']);
- $this->assertSame('third:4', $this->decide('H5', 'third:4')['scope_code']);
- $this->assertSame('third:4', $this->decide(null, 'third:4')['scope_code']);
- }
- public function test_translations_blank_fallback_partial_update_and_audit_history(): void
- {
- $this->save(['translations' => ['zh' => ['content' => '<b>维护</b>'], 'en' => ['content' => 'Maintenance'], 'vi' => ['content' => ' ']]]);
- $this->assertSame(2, DB::table('maintenance_translations')->count());
- $this->assertSame('<b>维护</b>', $this->decide()['content']);
- $this->assertSame('Maintenance', Policy::failure($this->service->runtimeRows('en')[0], 'PC', 'en', now()->getTimestamp())['msg']);
- $fallback = Policy::failure($this->service->runtimeRows('vi')[0], 'PC', 'vi', now()->getTimestamp());
- $this->assertSame(11001, $fallback['code']); $this->assertStringContainsString('bảo trì', $fallback['msg']);
- $this->service->save(['id' => $this->id(), 'platforms' => ['PC']]); $this->assertSame(2, DB::table('maintenance_translations')->count());
- $before = DB::table('operation_audits')->count(); $this->service->status($this->id(), 1); $this->assertSame($before, DB::table('operation_audits')->count());
- $this->service->save(['id' => $this->id(), 'translations' => ['zh' => ['content' => '中文']]]);
- $logs = $this->service->logs(['language' => 'en', 'operator_name' => 'adm', 'resource_id' => $this->id()]);
- $this->assertSame(3, $logs['total']);
- $this->assertSame('整站', ((array) $logs['data'][0]['old_values'])['translations']['en']['title']);
- $audit = json_decode(DB::table('operation_audits')->orderBy('id')->value('changes'), true);
- $this->assertSame('site', $audit['after']['scope_code']);
- $this->assertSame(0, $this->service->listing(['language' => 'en'])['total']);
- }
- public function test_filters_are_applied_before_pagination_and_timezone_edit_preserves_instants(): void
- {
- $row = $this->save(['platforms' => ['APP'], 'translations' => ['en' => ['content' => 'Text']]]);
- $this->save(['platforms' => ['APP'], 'translations' => ['en' => ['content' => 'Game']]], 'third:2');
- $list = $this->service->listing(['enabled' => 1, 'state' => 'active', 'platform' => 'APP', 'language' => 'en', 'limit' => 1, 'page' => 2]);
- $this->assertSame(2, $list['total']); $this->assertSame('third:2', $list['data'][0]['scope_code']);
- $edited = $this->service->save(['id' => $row['id'], 'timezone' => 'America/New_York']);
- $this->assertSame($row['starts_at_utc'], $edited['starts_at_utc']); $this->assertSame('2026-10-25 21:00:00', $edited['starts_at']);
- $this->assertSame(1, $this->service->listing(['scope_code' => 'third:2'])['total']);
- $this->assertSame([], $this->service->listing(['scope_code' => 'third:2'])['data'][0]['children'], 'third:2 has no subcategories in this fixture');
- }
- public function test_enabled_child_shows_under_its_parent_even_when_parent_is_off(): void
- {
- $this->service->listing([]); // 子项的维护行在第一次读取列表时补齐
- $this->save([], 'self:lottery:pc28');
- $list = $this->service->listing(['enabled' => 1]);
- $this->assertSame(['self:lottery'], array_column($list['data'], 'scope_code'));
- $this->assertFalse($list['data'][0]['matched'], 'parent itself is off');
- $this->assertSame(['self:lottery:pc28'], array_column($list['data'][0]['children'], 'scope_code'));
- $this->assertSame(count(self::GAME_LIST), $list['data'][0]['children_count'], 'children_count counts all subcategories');
- $off = collect($this->service->listing(['enabled' => 0])['data'])->firstWhere('scope_code', 'self:lottery');
- $this->assertTrue($off['matched']);
- $this->assertNotContains('self:lottery:pc28', array_column($off['children'], 'scope_code'));
- }
- public function test_subcategory_can_be_maintained_without_closing_its_category(): void
- {
- Schema::create('egame_items', function (Blueprint $t) {
- $t->id(); $t->string('item_type'); $t->string('plat_type'); $t->integer('game_type'); $t->string('name')->default(''); $t->integer('status')->default(1);
- });
- DB::table('egame_items')->insert([
- ['item_type' => 'platform', 'plat_type' => 'PG', 'game_type' => 2, 'name' => 'PG电子', 'status' => 1],
- ['item_type' => 'platform', 'plat_type' => 'JDB', 'game_type' => 2, 'name' => '', 'status' => 1],
- ['item_type' => 'game', 'plat_type' => 'pg', 'game_type' => 2, 'name' => '某个游戏', 'status' => 1],
- ['item_type' => 'platform', 'plat_type' => 'bad code', 'game_type' => 2, 'name' => '忽略', 'status' => 1],
- ]);
- $list = $this->service->listing(['parent_scope' => 'third:2']);
- $this->assertSame(1, $list['total']);
- $this->assertSame(['third:2', 'category', 2], [$list['data'][0]['scope_code'], $list['data'][0]['scope_level'], $list['data'][0]['children_count']]);
- $kids = $list['data'][0]['children'];
- $this->assertSame(['third:2:pg', 'third:2:jdb'], array_column($kids, 'scope_code'));
- $this->assertSame('电子 / PG电子', $kids[0]['scope_name']);
- $this->assertSame('电子 / JDB', $kids[1]['scope_name']);
- $this->assertSame('subcategory', $kids[0]['scope_level']);
- $row = $this->save(['translations' => ['zh' => ['content' => 'PG维护']]], 'third:2:pg');
- $this->assertSame('third:2', $row['parent_scope']);
- $this->assertSame('third:2:pg', $row['subcategory']);
- $this->assertSame('PG维护', $this->decide('PC', 'third:2', 'third:2:pg')['content']);
- $this->assertNull($this->decide('PC', 'third:2', 'third:2:jdb'));
- $this->save([], 'third:2');
- $this->assertSame('third:2:pg', $this->decide('PC', 'third:2', 'third:2:pg')['scope_code']);
- $this->assertSame('third:2', $this->decide('PC', 'third:2', 'third:2:jdb')['scope_code']);
- $this->save(['platforms' => ['H5']]);
- $this->assertSame('site', $this->decide('H5', 'third:2', 'third:2:pg')['scope_code']);
- $this->assertSame('third:2:pg', $this->decide('PC', 'third:2', 'third:2:pg')['scope_code']);
- $parent = $this->service->listing(['scope_code' => 'self:lottery:pc28'])['data'][0];
- $this->assertSame(['self:lottery', false], [$parent['scope_code'], $parent['matched']], 'searching a child returns its parent as container');
- $lottery = $parent['children'][0];
- $this->assertSame('F1彩票 / 加拿大28', $lottery['scope_name']);
- $this->assertSame('self:lottery', $lottery['parent_scope']);
- $this->assertSame('site', $this->service->detail($this->id())['scope_level']);
- $this->assertSame('category', $this->service->detail($this->id('third:2'))['scope_level']);
- $this->assertContains(['value' => 'third:2:pg', 'label' => '电子 / PG电子', 'parent' => 'third:2'], $this->service->options()['subcategories']);
- $this->assertSame(['self:lottery:pc28', 'self:lottery:jisu28', 'self:lottery:lhc:3', 'self:lottery:lhc:1', 'self:lottery:lhc:4',
- 'self:lottery:lhc:2', 'self:lottery:jisu:1', 'self:lottery:jisu:2', 'self:lottery:jisu:3', 'self:lottery:jisu:6',
- 'self:lottery:jisu:4', 'self:lottery:jisu:5', 'self:lottery:jisu:7'], array_column($this->service->listing(['parent_scope' => 'self:lottery'])['data'][0]['children'], 'scope_code'));
- $this->assertSame('F1彩票 / 新澳门六合彩', $this->service->listing(['scope_code' => 'self:lottery:lhc:1'])['data'][0]['children'][0]['scope_name']);
- $sports = $this->service->listing(['parent_scope' => 'self:sport'])['data'][0]['children'];
- $this->assertSame(['足球', '篮球', '乒乓球', '排球', '板球', '网球'], array_map(fn ($r) => explode(' / ', $r['scope_name'])[1], $sports));
- $this->save([], 'self:sport:basketball');
- $this->assertSame('self:sport:basketball', $this->decide('PC', 'self:sport', 'self:sport:basketball')['scope_code']);
- $this->assertNull($this->decide('PC', 'self:sport', 'self:sport:football'));
- $this->assertNull($this->decide('PC', 'self:lottery', 'self:sport:basketball'));
- $long = str_repeat('a', 32);
- $this->assertSame('third:2:' . $long, Policy::thirdSubcategory(2, $long));
- $this->assertNull(Policy::thirdSubcategory(2, str_repeat('a', 33)));
- }
- public function test_telegram_28_bets_follow_the_game_selected_by_pc28_switch(): void
- {
- $this->service->options();
- $this->assertNull($this->service->telegramPc28Failure('zh'));
- $this->save(['platforms' => ['APP'], 'translations' => ['en' => ['content' => 'Jisu28 down']]], 'self:lottery:jisu28');
- $this->assertNull($this->service->telegramPc28Failure('en'), 'switch 0 plays 加拿大28, so 极速28 maintenance is ignored');
- DB::table('config')->where('field', 'pc28_switch')->update(['val' => '1']);
- $failure = $this->service->telegramPc28Failure('en');
- $this->assertSame([11002, 'Jisu28 down', 'self:lottery:jisu28'], [$failure['code'], $failure['msg'], $failure['data']['subcategory']], 'Telegram matches any platform');
- $this->service->status($this->id('self:lottery:jisu28'), 0);
- $this->assertNull($this->service->telegramPc28Failure(null));
- $this->save([], 'self:lottery');
- $this->assertSame('self:lottery', $this->service->telegramPc28Failure('xx')['data']['category']);
- $this->service->status($this->id('self:lottery'), 0);
- $this->save(['platforms' => ['H5']]);
- $this->assertSame(11001, $this->service->telegramPc28Failure('zh')['code']);
- }
- public function test_subcategory_rollback_preserves_legacy_settings_and_translations(): void
- {
- $migration = require dirname(__DIR__, 2) . '/database/migrations/2026_11_09_120000_extend_maintenance_scope_code.php';
- $migration->up();
- $this->service->options();
- $legacy = $this->save(['translations' => ['zh' => ['content' => '大类维护']]], 'third:2');
- $child = $this->save(['translations' => ['zh' => ['content' => '彩种维护']]], 'self:lottery:pc28');
- DB::table('maintenance_settings')->insert([
- 'scope_code' => 'third:2:' . str_repeat('a', 32), 'platforms' => '["PC"]',
- 'starts_at' => '2026-10-26 01:00:00', 'timezone' => 'Asia/Shanghai',
- ]);
- $migration->down();
- $this->assertSame(array_keys(Policy::scopes()), DB::table('maintenance_settings')->orderBy('id')->pluck('scope_code')->all());
- $this->assertEquals($legacy, $this->service->detail($legacy['id']));
- $this->assertFalse(DB::table('maintenance_translations')->where('maintenance_id', $child['id'])->exists());
- $migration->up();
- $this->assertSame(0, $this->service->listing(['scope_code' => 'self:lottery:pc28'])['data'][0]['children'][0]['enabled']);
- }
- #[DataProvider('invalidInputs')]
- public function test_invalid_schedules_scopes_and_payloads_leave_no_changes(array $input): void
- {
- $before = (array) DB::table('maintenance_settings')->where('id', $this->id())->first();
- try { $this->save($input); $this->fail('Expected validation failure'); }
- catch (ValidationException $e) { $this->assertNotEmpty($e->errors()); }
- $this->assertSame($before, (array) DB::table('maintenance_settings')->where('id', $this->id())->first());
- $this->assertSame(0, DB::table('operation_audits')->count());
- }
- public static function invalidInputs(): array
- {
- return array_map(fn ($input) => [$input], [
- ['id' => 999], ['scope_code' => 'third:4'], ['platforms' => []], ['platforms' => ['IOS']], ['platforms' => ['PC', 'PC']],
- ['enabled' => 2], ['timezone' => '+08:00'], ['timezone' => 'Not/AZone'], ['starts_at' => '2026-02-30 00:00:00'],
- ['ends_at' => '2026-10-26 08:00:00'], ['translations' => ['th' => ['content' => 'bad']]], ['translations' => ['zh' => 'bad']],
- ['translations' => ['en' => ['content' => str_repeat('x', 5001)]]],
- ['timezone' => 'America/New_York', 'starts_at' => '2026-03-08 02:30:00'],
- ['timezone' => 'America/New_York', 'starts_at' => '2026-11-01 01:30:00'],
- ]);
- }
- public function test_audit_failure_rolls_back_rule_and_translations(): void
- {
- DB::statement("CREATE TRIGGER reject_maintenance_audit BEFORE INSERT ON bot_operation_audits BEGIN SELECT RAISE(ABORT, 'audit failed'); END");
- try { $this->save(['translations' => ['zh' => ['content' => '维护']]]); $this->fail('Expected audit failure'); }
- catch (\Illuminate\Database\QueryException $e) { $this->assertStringContainsString('audit failed', $e->getMessage()); }
- $this->assertSame(0, DB::table('maintenance_settings')->where('enabled', 1)->count());
- $this->assertSame(0, DB::table('maintenance_translations')->count());
- }
- public function test_legacy_middleware_blocks_before_business_and_uses_saved_language_without_renewal(): void
- {
- $this->save(['translations' => ['en' => ['content' => 'Saved English']]]);
- DB::table('users')->insert(['user_id' => '101', 'language' => 'en']);
- DB::table('user_session')->insert(['user_id' => '101', 'token' => 'valid', 'create_time' => now()->getTimestamp() - 3600,
- 'update_time' => now()->getTimestamp() - 3600, 'expire_time' => now()->getTimestamp() + 3600]);
- $before = (array) DB::table('user_session')->first(); $called = false;
- $request = Request::create('/api/wallet/createPay', 'POST'); $request->headers->add(['Authorization' => 'valid', 'Lang' => 'vi', 'Platform' => 'PC']);
- $response = (new MemberMaintenance())->handle($request, function () use (&$called) { $called = true; return new JsonResponse(['code' => 0]); });
- $this->assertFalse($called); $this->assertSame(200, $response->getStatusCode());
- $this->assertSame(11001, $response->getData(true)['code']); $this->assertSame('Saved English', $response->getData(true)['msg']);
- $this->assertSame($before, (array) DB::table('user_session')->first());
- $this->assertStringContainsString('no-store', $response->headers->get('Cache-Control'));
- $this->service->status($this->id(), 0);
- $this->assertSame(0, (new MemberMaintenance())->handle($request, fn () => new JsonResponse(['code' => 0]))->getData(true)['code']);
- }
- public function test_routes_permissions_and_callbacks_settlement_and_admin_remain_outside_gate(): void
- {
- $router = new Router(new Dispatcher($this->app), $this->app); $this->app->instance('router', $router);
- if (!class_exists('Route')) class_alias(\Illuminate\Support\Facades\Route::class, 'Route');
- $router->group(['prefix' => 'admin'], function () { require dirname(__DIR__, 2) . '/routes/admin.php'; });
- $router->group(['prefix' => 'api'], function () { require dirname(__DIR__, 2) . '/routes/api.php'; });
- foreach (['' => 'GET', '/options' => 'GET', '/detail' => 'GET', '/logs' => 'GET', '/submit' => 'POST', '/status' => 'POST'] as $suffix => $method) {
- $route = $router->getRoutes()->match(Request::create('/admin/maintenance' . $suffix, $method));
- foreach (['admin.jwt', 'check.button.uri', 'operations.admin'] as $middleware) $this->assertContains($middleware, $route->middleware());
- $this->assertNotContains(MemberMaintenance::class, $route->middleware());
- }
- $this->assertSame('admin/maintenance/submit', $route->defaults['permission_uri']);
- foreach (['/api/wallet/createPay' => 'POST', '/api/ActivityReward' => 'GET', '/api/newPc' => 'GET', '/api/issue' => 'GET', '/api/pcissue' => 'GET'] as $uri => $method) {
- $this->assertContains(MemberMaintenance::class, $router->getRoutes()->match(Request::create($uri, $method))->middleware());
- }
- foreach (['/api/pay/notify', '/api/pay/harvest', '/api/onMessage'] as $uri) {
- $this->assertNotContains(MemberMaintenance::class, $router->getRoutes()->match(Request::create($uri, 'POST'))->middleware());
- }
- $request = $this->request(); $request->user->id = 99;
- $this->assertSame(-1, (new OperationsAdminMiddleware())->handle($request, fn () => new JsonResponse(['code' => 0]))->getData(true)['code']);
- $this->request(['id' => $this->id(), 'enabled' => 1]);
- $this->assertSame(0, (new MaintenanceController())->submit($this->service)->getData(true)['code']);
- }
- }
|