ActivityCatalogTest.php 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. <?php
  2. namespace Tests\Integration;
  3. use App\Constants\HttpStatus;
  4. use App\Http\Controllers\admin\ActivityReward as AdminActivityReward;
  5. use App\Models\ActivityReward;
  6. use App\Services\ActivityRewardService;
  7. use App\Services\Sharing\ShareDefaults;
  8. use App\Support\ActivityCatalog;
  9. use Carbon\Carbon;
  10. use Illuminate\Config\Repository;
  11. use Illuminate\Database\Capsule\Manager;
  12. use Illuminate\Database\Schema\Blueprint;
  13. use Illuminate\Events\Dispatcher;
  14. use Illuminate\Foundation\Application;
  15. use Illuminate\Http\JsonResponse;
  16. use Illuminate\Http\Request;
  17. use Illuminate\Routing\Router;
  18. use Illuminate\Support\Facades\DB;
  19. use Illuminate\Support\Facades\Facade;
  20. use Illuminate\Support\Facades\Schema;
  21. use Illuminate\Translation\ArrayLoader;
  22. use Illuminate\Translation\Translator;
  23. use Illuminate\Validation\Factory;
  24. use Illuminate\Validation\ValidationException;
  25. use PHPUnit\Framework\TestCase;
  26. /** Real controllers and service validation against SQLite only; no application boot or external databases. */
  27. class ActivityCatalogTest extends TestCase
  28. {
  29. private Application $app;
  30. protected function setUp(): void
  31. {
  32. Facade::clearResolvedInstances();
  33. $this->app = new Application(dirname(__DIR__, 2));
  34. $this->app->instance('config', new Repository([
  35. 'app' => ['locale' => 'zh', 'url' => 'https://activity.example.test', 'timezone' => 'Asia/Shanghai'],
  36. ]));
  37. Facade::setFacadeApplication($this->app);
  38. $db = new Manager($this->app);
  39. $db->addConnection(['driver' => 'sqlite', 'database' => ':memory:', 'prefix' => 'bot_']);
  40. $db->setAsGlobal(); $db->bootEloquent();
  41. $this->app->instance('db', $db->getDatabaseManager());
  42. $this->app->bind('db.schema', fn () => $db->schema());
  43. $translator = new Translator(new ArrayLoader(), 'zh');
  44. $this->app->instance('translator', $translator);
  45. $this->app->instance('validator', new Factory($translator, $this->app));
  46. $this->app->instance(\Illuminate\Contracts\Routing\ResponseFactory::class, new \Tests\Support\JsonResponseFactory());
  47. Request::macro('validate', function (array $rules) {
  48. return app('validator')->make($this->all(), $rules)->validate();
  49. });
  50. $db->schema()->create('activity_rewards', function (Blueprint $t) {
  51. $t->id(); $t->string('title'); $t->string('sub_title');
  52. $t->integer('start_time'); $t->integer('end_time'); $t->integer('status')->default(1);
  53. $t->integer('type')->default(1); $t->string('lang')->default('zh');
  54. foreach (['detail_image', 'pc_image', 'detail_banner', 'pc_banner', 'content', 'params'] as $field) {
  55. $t->text($field)->nullable();
  56. }
  57. $t->timestamps();
  58. });
  59. (require dirname(__DIR__, 2) . '/database/migrations/2026_10_13_120000_add_activity_eligibility_fields.php')->up();
  60. (require dirname(__DIR__, 2) . '/database/migrations/2026_10_14_120000_add_activity_reward_quotas.php')->up();
  61. $this->migration()->up();
  62. (require dirname(__DIR__, 2) . '/database/migrations/2026_10_28_120000_add_activity_click_behavior.php')->up();
  63. (require dirname(__DIR__, 2) . '/database/migrations/2026_10_28_130000_add_activity_click_position.php')->up();
  64. Schema::create('announcements', function (Blueprint $t) { $t->id(); $t->string('click_type'); $t->string('link', 2048)->nullable(); });
  65. (require dirname(__DIR__, 2) . '/database/migrations/2026_10_30_120000_add_platform_links_to_announcements_and_activities.php')->up();
  66. (require dirname(__DIR__, 2) . '/database/migrations/2026_11_07_180000_allow_permanent_activities.php')->up();
  67. $this->request('');
  68. }
  69. protected function tearDown(): void
  70. {
  71. Carbon::setTestNow();
  72. DB::disconnect(); Facade::clearResolvedInstances(); parent::tearDown();
  73. }
  74. private function migration()
  75. {
  76. return require dirname(__DIR__, 2) . '/database/migrations/2026_10_25_120000_add_game_category_to_activity_rewards.php';
  77. }
  78. private function request(string $action, array $params = [], string $method = 'GET'): void
  79. {
  80. $this->app->instance('request', Request::create('/admin/ActivityReward/' . $action, $method, $params));
  81. }
  82. private function form(array $overrides = []): array
  83. {
  84. return $overrides + [
  85. 'title' => '活动分类测试', 'sub_title' => '分类说明', 'type' => 3, 'status' => 1, 'lang' => 'zh',
  86. 'start_time' => '2026-10-01 12:00:00', 'end_time' => '2026-12-31 12:00:00',
  87. 'detail_image' => '/app.png', 'pc_image' => '/pc.png',
  88. 'detail_banner' => '/app-banner.png', 'pc_banner' => '/pc-banner.png',
  89. ];
  90. }
  91. private function save(array $params = []): array
  92. {
  93. $this->request('submit', $this->form($params), 'POST');
  94. return (new AdminActivityReward())->store()->getData(true);
  95. }
  96. private function listing(array $filters = []): array
  97. {
  98. $this->request('', $filters + ['page' => 1, 'limit' => 100]);
  99. $result = (new AdminActivityReward())->index()->getData(true);
  100. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  101. return $result['data'];
  102. }
  103. private function rawActivity(array $overrides = []): int
  104. {
  105. $data = $this->form($overrides);
  106. $data['start_time'] = strtotime($data['start_time']);
  107. $data['end_time'] = strtotime($data['end_time']);
  108. if (isset($data['publish_platforms']) && is_array($data['publish_platforms'])) {
  109. $data['publish_platforms'] = json_encode($data['publish_platforms']);
  110. }
  111. return DB::table('activity_rewards')->insertGetId($data);
  112. }
  113. public function test_ordinary_activities_save_sort_and_publication_platforms(): void
  114. {
  115. foreach ([['PC'], ['H5'], ['APP'], []] as $platforms) {
  116. $result = $this->save(['type' => 2, 'sort' => 37, 'publish_platforms' => $platforms]);
  117. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  118. $id = DB::table('activity_rewards')->latest('id')->value('id');
  119. $this->assertSame($platforms, ActivityReward::findOrFail($id)->publish_platforms);
  120. $this->assertSame(37, ActivityReward::findOrFail($id)->sort);
  121. $this->assertSame(HttpStatus::OK, $this->save(['id' => $id, 'type' => 2, 'title' => '仅修改标题'])['code']);
  122. $this->assertSame($platforms, ActivityReward::findOrFail($id)->publish_platforms);
  123. foreach (['pc' => 'PC', 'h5' => 'H5'] as $filter => $platform) {
  124. $ids = array_column($this->listing(['platform' => $filter])['data'], 'id');
  125. $this->assertSame(!$platforms || in_array($platform, $platforms, true), in_array($id, $ids, true));
  126. }
  127. }
  128. $sorted = $this->save(['type' => 2, 'sort' => 99]);
  129. $this->assertSame(HttpStatus::OK, $sorted['code']);
  130. $this->assertSame(99, $this->listing()['data'][0]['sort']);
  131. foreach ([['sort' => -1], ['sort' => 'bad'], ['publish_platforms' => null], ['publish_platforms' => 'PC'], ['publish_platforms' => ['PC', 'PC']], ['publish_platforms' => ['IOS']]] as $invalid) {
  132. $this->assertSame(HttpStatus::CUSTOM_ERROR, $this->save(['type' => 2] + $invalid)['code']);
  133. }
  134. }
  135. public function test_omitted_dates_create_permanent_activities_and_preserve_existing_dates_on_edit(): void
  136. {
  137. $form = $this->form(['type' => 2]);
  138. unset($form['start_time'], $form['end_time']);
  139. $this->request('submit', $form, 'POST');
  140. $this->assertSame(HttpStatus::OK, (new AdminActivityReward())->store()->getData(true)['code']);
  141. $row = ActivityReward::query()->latest('id')->firstOrFail();
  142. $this->assertNull($row->start_time); $this->assertNull($row->end_time);
  143. $this->assertSame(HttpStatus::OK, $this->save(['id' => $row->id, 'type' => 2])['code']);
  144. $before = $row->fresh()->only(['start_time', 'end_time']);
  145. ActivityRewardService::submit(['id' => $row->id, 'title' => '仅改标题']);
  146. $this->assertSame($before, $row->fresh()->only(['start_time', 'end_time']));
  147. foreach (['start_time', 'end_time'] as $field) {
  148. $this->request('submit', $form + [$field => '2026-12-01'], 'POST');
  149. $this->assertSame(HttpStatus::CUSTOM_ERROR, (new AdminActivityReward())->store()->getData(true)['code']);
  150. }
  151. }
  152. public function test_legacy_activity_dates_are_paired_nullable_and_permanent_rows_do_not_expire(): void
  153. {
  154. foreach ([1, 2, 3, 4, 5, 6] as $type) {
  155. foreach ([[null, null], ['', '']] as [$start, $end]) {
  156. $result = $this->save(['type' => $type, 'start_time' => $start, 'end_time' => $end]);
  157. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  158. $row = ActivityReward::query()->latest('id')->firstOrFail();
  159. $this->assertNull($row->start_time); $this->assertNull($row->end_time);
  160. $this->assertNull($row->getRawOriginal('start_time')); $this->assertNull($row->getRawOriginal('end_time'));
  161. $this->assertTrue(\App\Support\ActivityPeriod::contains($row->getRawOriginal('start_time'), $row->getRawOriginal('end_time'), time()));
  162. $before = DB::table('activity_rewards')->where('id', $row->id)->first();
  163. foreach ([['start_time' => '2026-10-01', 'end_time' => null], ['start_time' => '', 'end_time' => '2026-12-31']] as $half) {
  164. $this->assertSame(HttpStatus::CUSTOM_ERROR, $this->save(['id' => $row->id, 'type' => $type] + $half)['code']);
  165. $this->assertEquals($before, DB::table('activity_rewards')->where('id', $row->id)->first());
  166. }
  167. }
  168. }
  169. $this->assertSame(0, ActivityRewardService::expire());
  170. $this->assertSame(12, $this->listing(['status' => 1])['total']);
  171. $this->assertCount(12, \App\Support\ActivityPeriod::active(ActivityReward::query(), time())->get());
  172. $this->request('');
  173. $this->assertCount(2, (new \App\Http\Controllers\api\ActivityReward())->index()->getData(true)['data']);
  174. }
  175. public function test_ordinary_platform_migration_normalizes_only_type_two_and_is_repeatable(): void
  176. {
  177. $ids = [];
  178. foreach ([null, [], ['H5'], ['APP'], ['PC', 'H5', 'APP']] as $platforms) {
  179. $ids[] = $this->rawActivity(['type' => 2, 'publish_platforms' => $platforms]);
  180. }
  181. foreach ([1, 3, 4, 5, 6] as $type) {
  182. $this->rawActivity(['type' => $type, 'publish_platforms' => ['H5']]);
  183. }
  184. $others = DB::table('activity_rewards')->where('type', '!=', 2)->orderBy('id')->get()->toJson();
  185. $ordinary = DB::table('activity_rewards')->where('type', 2)->orderBy('id')->get()->map(function ($row) {
  186. unset($row->publish_platforms);
  187. return $row;
  188. })->toJson();
  189. $migration = require dirname(__DIR__, 2) . '/database/migrations/2026_11_07_150000_normalize_ordinary_activity_platforms.php';
  190. foreach ([1, 2] as $run) {
  191. $migration->up();
  192. foreach ($ids as $id) {
  193. $this->assertSame(['PC', 'H5', 'APP'], json_decode(DB::table('activity_rewards')->where('id', $id)->value('publish_platforms'), true));
  194. }
  195. $this->assertSame($others, DB::table('activity_rewards')->where('type', '!=', 2)->orderBy('id')->get()->toJson());
  196. $this->assertSame($ordinary, DB::table('activity_rewards')->where('type', 2)->orderBy('id')->get()->map(function ($row) {
  197. unset($row->publish_platforms);
  198. return $row;
  199. })->toJson());
  200. }
  201. }
  202. public function test_expiry_updates_only_ended_published_activities_and_is_repeatable(): void
  203. {
  204. Carbon::setTestNow(Carbon::parse('2026-11-05 12:00:00', 'Asia/Shanghai'));
  205. $expired = [];
  206. foreach ([1, 2, 3, 4, 5, 6] as $type) {
  207. $id = $this->rawActivity(['type'=>$type, 'lang'=>$type % 2 ? 'zh' : 'en']);
  208. DB::table('activity_rewards')->where('id', $id)->update(['end_time'=>now()->timestamp - 1, 'rewarded_count'=>2, 'budget_used'=>'12.34']);
  209. $expired[] = $id;
  210. }
  211. $unchanged = [];
  212. foreach ([[1, 0], [1, 3600], [0, -1]] as [$status, $offset]) {
  213. $id = $this->rawActivity(['status'=>$status]);
  214. DB::table('activity_rewards')->where('id', $id)->update(['end_time'=>now()->timestamp + $offset]);
  215. $unchanged[] = $id;
  216. }
  217. $before = DB::table('activity_rewards')->get()->keyBy('id');
  218. $this->assertSame(6, ActivityRewardService::expire());
  219. foreach ($expired as $id) {
  220. $expected = (array)$before[$id];
  221. $expected['status'] = 0;
  222. $expected['updated_at'] = now()->format('Y-m-d H:i:s');
  223. $this->assertSame($expected, (array)DB::table('activity_rewards')->where('id', $id)->first());
  224. }
  225. foreach ($unchanged as $id) $this->assertSame((array)$before[$id], (array)DB::table('activity_rewards')->where('id', $id)->first());
  226. $this->assertSame(0, ActivityRewardService::expire());
  227. Carbon::setTestNow(now()->addSecond());
  228. $this->assertSame(1, ActivityRewardService::expire()); // The end timestamp itself is still inclusive.
  229. }
  230. public function test_admin_list_expires_before_status_filter_count_and_pagination(): void
  231. {
  232. Carbon::setTestNow(Carbon::parse('2026-11-05 12:00:00', 'Asia/Shanghai'));
  233. $expired = [];
  234. for ($i = 0; $i < 3; ++$i) {
  235. $id = $this->rawActivity(['sort'=>100]);
  236. DB::table('activity_rewards')->where('id', $id)->update(['end_time'=>now()->timestamp - 1]);
  237. $expired[] = $id;
  238. }
  239. $active = [$this->rawActivity(), $this->rawActivity()];
  240. $manual = $this->rawActivity(['status'=>0]);
  241. $page = $this->listing(['status'=>0, 'page'=>1, 'limit'=>2]);
  242. $this->assertSame(4, $page['total']);
  243. $this->assertSame([$expired[2], $expired[1]], array_column($page['data'], 'id'));
  244. $second = $this->listing(['status'=>0, 'page'=>2, 'limit'=>2]);
  245. $this->assertSame(4, $second['total']);
  246. $this->assertSame([$expired[0], $manual], array_column($second['data'], 'id'));
  247. $up = $this->listing(['status'=>1]);
  248. $this->assertSame(2, $up['total']);
  249. $this->assertSame(array_reverse($active), array_column($up['data'], 'id'));
  250. $this->assertSame(6, $this->listing()['total']);
  251. $this->assertSame(0, (int)DB::table('activity_rewards')->where('id', $expired[0])->value('status'));
  252. }
  253. public function test_scheduled_sync_expires_the_legacy_catalog_without_promotion_tables(): void
  254. {
  255. Carbon::setTestNow(Carbon::parse('2026-11-05 12:00:00', 'Asia/Shanghai'));
  256. $id = $this->rawActivity();
  257. DB::table('activity_rewards')->where('id', $id)->update(['end_time'=>now()->timestamp - 1]);
  258. $command = new \App\Console\Commands\SyncPromotions();
  259. $command->setLaravel($this->app);
  260. $tester = new \Symfony\Component\Console\Tester\CommandTester($command);
  261. $this->assertSame(0, $tester->execute([]));
  262. $this->assertSame(0, (int)DB::table('activity_rewards')->where('id', $id)->value('status'));
  263. $this->assertStringContainsString('到期下架:1', $tester->getDisplay());
  264. $this->assertSame(0, $tester->execute([]));
  265. $this->assertStringContainsString('到期下架:0', $tester->getDisplay());
  266. }
  267. public function test_dual_routes_round_trip_through_admin_save_and_listing_with_cover_and_detail(): void
  268. {
  269. foreach (['cover', 'detail'] as $position) {
  270. $saved = $this->save(['type' => 2, 'click_type' => 'internal', 'link_pc' => '/pc/promotion?id=5', 'link_h5' => '/h5/promotion?id=5', 'click_position' => $position]);
  271. $this->assertSame(HttpStatus::OK, $saved['code'], $saved['msg']);
  272. $row = $this->listing()['data'][0]; $id = $row['id'];
  273. $this->assertNull($row['link']); $this->assertTrue($row['can_jump']);
  274. $this->assertSame('/pc/promotion?id=5', $row['link_pc']); $this->assertSame('/h5/promotion?id=5', $row['link_h5']);
  275. $this->assertSame($position, $row['click_position']);
  276. $this->assertSame(HttpStatus::OK, $this->save(['id' => $id, 'title' => '仅改标题'])['code']);
  277. $this->assertSame('/h5/promotion?id=5', ActivityReward::findOrFail($id)->link_h5);
  278. $this->assertSame(HttpStatus::OK, $this->save(['id' => $id, 'link_h5' => '/mobile'])['code']);
  279. $this->assertSame('/pc/promotion?id=5', ActivityReward::findOrFail($id)->link_pc);
  280. $this->assertSame('/mobile', ActivityReward::findOrFail($id)->link_h5);
  281. $before = DB::table('activity_rewards')->where('id', $id)->first();
  282. foreach (['link_pc', 'link_h5'] as $field) {
  283. foreach ([null, '', '//external.test', '/\\bad', 'https://example.com', ['bad'], '/' . str_repeat('x', 2048)] as $value) {
  284. $this->assertSame(HttpStatus::CUSTOM_ERROR, $this->save(['id' => $id, $field => $value])['code']);
  285. $this->assertEquals($before, DB::table('activity_rewards')->where('id', $id)->first());
  286. }
  287. }
  288. $this->assertSame(HttpStatus::OK, $this->save(['id' => $id, 'click_type' => 'external', 'link' => 'https://example.com'])['code']);
  289. $external = ActivityReward::findOrFail($id)->toArray();
  290. $this->assertSame('https://example.com', $external['link']);
  291. $this->assertNull($external['link_pc']); $this->assertNull($external['link_h5']);
  292. $this->assertSame(HttpStatus::OK, $this->save(['id' => $id, 'click_type' => 'none'])['code']);
  293. $plain = ActivityReward::findOrFail($id)->toArray();
  294. foreach (['link', 'link_pc', 'link_h5', 'click_position'] as $field) $this->assertNull($plain[$field]);
  295. $this->assertFalse($plain['can_jump']);
  296. }
  297. $this->assertSame(HttpStatus::CUSTOM_ERROR, $this->save(['click_type' => 'internal', 'link_pc' => '/pc', 'click_position' => 'cover'])['code']);
  298. }
  299. public function test_bot_member_list_resolves_the_dual_route_for_pc_h5_and_app(): void
  300. {
  301. $this->assertSame(HttpStatus::OK, $this->save(['type' => 1, 'click_type' => 'internal', 'link_pc' => '/pc/promo', 'link_h5' => '/h5/promo', 'click_position' => 'cover',
  302. 'start_time' => '2000-01-01', 'end_time' => '2099-12-31'])['code']);
  303. foreach (['PC' => '/pc/promo', 'H5' => '/h5/promo', 'APP' => '/h5/promo', 'h5' => '/h5/promo', '' => '/pc/promo'] as $platform => $target) {
  304. $request = Request::create('/api/ActivityReward', 'GET', ['platform' => $platform === 'PC' ? 'H5' : 'PC']);
  305. if ($platform !== '') $request->headers->set('Platform', $platform);
  306. $this->app->instance('request', $request);
  307. $body = (new \App\Http\Controllers\api\ActivityReward())->index()->getData(true);
  308. $this->assertSame(0, $body['code'], $body['msg']);
  309. $this->assertSame($target, $body['data'][0]['link']);
  310. $this->assertTrue($body['data'][0]['can_jump']);
  311. $this->assertArrayNotHasKey('link_pc', $body['data'][0]);
  312. $this->assertArrayNotHasKey('link_h5', $body['data'][0]);
  313. }
  314. }
  315. public function test_activity_options_keep_only_f1_lottery_without_changing_financial_categories(): void
  316. {
  317. $this->rawActivity(['type' => 1]);
  318. $before = DB::table('activity_rewards')->get()->toJson();
  319. $this->request('options');
  320. $body = (new AdminActivityReward())->options()->getData(true);
  321. $this->assertSame(HttpStatus::OK, $body['code'], $body['msg']);
  322. $financialCategories = ['third:1' => '真人', 'third:2' => '电子', 'third:3' => '彩票',
  323. 'third:4' => '体育', 'third:5' => '电竞', 'third:6' => '捕鱼', 'third:7' => '棋牌',
  324. 'self:sport' => 'F1体育', 'self:lottery' => 'F1彩票'];
  325. $this->assertSame($financialCategories, ShareDefaults::categories());
  326. $this->assertCount(9, ShareDefaults::categories());
  327. $this->assertArrayNotHasKey('other', ShareDefaults::categories());
  328. $this->assertArrayNotHasKey('hot', ShareDefaults::categories());
  329. $activityCategories = $financialCategories;
  330. unset($activityCategories['third:3']);
  331. $this->assertSame(['hot' => '热门'] + $activityCategories + ['other' => '其他'], ActivityCatalog::gameTypes());
  332. $expectedGames = [];
  333. foreach (['hot' => '热门'] + $activityCategories + ['other' => '其他'] as $value => $label) $expectedGames[] = compact('value', 'label');
  334. $this->assertCount(10, $expectedGames);
  335. $this->assertSame($expectedGames, $body['data']['game_types']);
  336. $this->assertNotContains('third:3', array_column($body['data']['game_types'], 'value'));
  337. $this->assertNotContains('彩票', array_column($body['data']['game_types'], 'label'));
  338. $this->assertContains(['value' => 'self:lottery', 'label' => 'F1彩票'], $body['data']['game_types']);
  339. $this->assertNotContains('', array_column($body['data']['game_types'], 'value'));
  340. $this->assertNotContains('wallet', array_column($body['data']['game_types'], 'value'));
  341. $this->assertNotContains('all', array_column($body['data']['game_types'], 'value'));
  342. $this->assertSame([
  343. ['value' => 2, 'label' => '普通活动'],
  344. ['value' => 3, 'label' => '即充即送'],
  345. ['value' => 4, 'label' => '老用户召集令'],
  346. ['value' => 5, 'label' => 'USDT使用教学'],
  347. ['value' => 6, 'label' => '余额宝'],
  348. ], $body['data']['activity_types']);
  349. $this->assertNotContains(1, array_column($body['data']['activity_types'], 'value'));
  350. $router = new Router(new Dispatcher($this->app), $this->app);
  351. $this->app->instance('router', $router);
  352. $router->group(['prefix' => 'admin'], function () {
  353. require dirname(__DIR__, 2) . '/routes/admin.php';
  354. });
  355. $route = $router->getRoutes()->match(Request::create('/admin/ActivityReward/options', 'GET'));
  356. $this->assertSame(AdminActivityReward::class . '@options', $route->getAction('uses'));
  357. $this->assertContains('admin.jwt', $route->middleware());
  358. $this->assertContains('check.button.uri', $route->middleware());
  359. $this->assertSame($before, DB::table('activity_rewards')->get()->toJson());
  360. $this->assertSame(0, DB::table('activity_reward_recipients')->count());
  361. }
  362. public function test_removed_banners_are_not_required_saved_or_returned_and_images_remain(): void
  363. {
  364. $input = $this->form(); unset($input['detail_banner'], $input['pc_banner']);
  365. $this->request('submit', $input, 'POST');
  366. $body = (new AdminActivityReward())->store()->getData(true);
  367. $this->assertSame(HttpStatus::OK, $body['code']);
  368. $id = (int) DB::table('activity_rewards')->value('id');
  369. $this->assertSame('', DB::table('activity_rewards')->where('id', $id)->value('detail_banner'));
  370. $this->assertSame('', DB::table('activity_rewards')->where('id', $id)->value('pc_banner'));
  371. $this->assertSame('/app.png', DB::table('activity_rewards')->where('id', $id)->value('detail_image'));
  372. $this->assertSame('/pc.png', DB::table('activity_rewards')->where('id', $id)->value('pc_image'));
  373. DB::table('activity_rewards')->where('id', $id)->update(['detail_banner' => '/old.png', 'pc_banner' => '/old-pc.png']);
  374. $body = $this->save(['id' => $id, 'detail_banner' => ['ignored legacy input'], 'pc_banner' => ['ignored legacy input']]);
  375. $this->assertSame(HttpStatus::OK, $body['code']);
  376. $this->assertSame('/old.png', DB::table('activity_rewards')->where('id', $id)->value('detail_banner'));
  377. $this->assertSame('/old-pc.png', DB::table('activity_rewards')->where('id', $id)->value('pc_banner'));
  378. foreach (['detail_image', 'pc_image'] as $required) {
  379. $missing = $this->form(['id' => $id]); unset($missing[$required]);
  380. $this->request('submit', $missing, 'POST');
  381. $this->assertSame(HttpStatus::CUSTOM_ERROR, (new AdminActivityReward())->store()->getData(true)['code']);
  382. }
  383. foreach ([ActivityReward::findOrFail($id)->toArray(), $this->listing()['data'][0]] as $row) {
  384. $this->assertArrayNotHasKey('detail_banner', $row); $this->assertArrayNotHasKey('pc_banner', $row);
  385. $this->assertArrayHasKey('detail_image', $row); $this->assertArrayHasKey('pc_image', $row);
  386. }
  387. }
  388. public function test_activity_click_behavior_saves_preserves_and_clears_for_normal_and_type_two_activities(): void
  389. {
  390. $this->request('options');
  391. $this->assertSame(\App\Support\AnnouncementCatalog::options(\App\Support\AnnouncementCatalog::CLICKS),
  392. (new AdminActivityReward())->options()->getData(true)['data']['click_types']);
  393. foreach ([2, 3] as $type) {
  394. $body = $this->save(['type' => $type]);
  395. $this->assertSame(HttpStatus::OK, $body['code']);
  396. $id = (int) DB::table('activity_rewards')->latest('id')->value('id');
  397. $this->assertSame('none', DB::table('activity_rewards')->where('id', $id)->value('click_type'));
  398. foreach (['external' => 'https://example.com/promotion?from=activity', 'internal' => '/promotion/detail?id=3'] as $mode => $link) {
  399. $body = $this->save(['id' => $id, 'type' => $type, 'click_type' => $mode, 'link' => $link, 'click_position' => $mode === 'external' ? 'cover' : 'detail']);
  400. $this->assertSame(HttpStatus::OK, $body['code'], $body['msg']);
  401. $this->assertSame(HttpStatus::OK, $this->save(['id' => $id, 'type' => $type, 'title' => '只改标题'])['code']);
  402. $row = collect($this->listing()['data'])->keyBy('id')->get($id);
  403. $this->assertSame($mode, $row['click_type']); $this->assertSame($link, $row['link']); $this->assertTrue($row['can_jump']);
  404. $this->assertSame($mode === 'external' ? 'cover' : 'detail', $row['click_position']);
  405. }
  406. $this->assertSame(HttpStatus::OK, $this->save(['id' => $id, 'type' => $type, 'click_type' => 'none', 'link' => ['unused'], 'click_position' => ['unused']])['code']);
  407. $row = ActivityReward::findOrFail($id)->toArray();
  408. $this->assertSame('none', $row['click_type']); $this->assertNull($row['link']); $this->assertFalse($row['can_jump']);
  409. $this->assertNull($row['click_position']);
  410. $this->assertNull(DB::table('activity_rewards')->where('id', $id)->value('click_position'));
  411. $this->assertNull(DB::table('activity_rewards')->where('id', $id)->value('link'));
  412. }
  413. }
  414. public function test_nonempty_links_require_a_trigger_position_and_can_switch_position_independently(): void
  415. {
  416. $this->request('options');
  417. $this->assertSame([['value' => 'cover', 'label' => '活动列表封面'], ['value' => 'detail', 'label' => '活动详情按钮']],
  418. (new AdminActivityReward())->options()->getData(true)['data']['click_positions']);
  419. foreach (['external' => 'https://example.com', 'internal' => '/detail'] as $mode => $link) {
  420. foreach ([null, '', 'invalid', ['cover']] as $position) {
  421. $this->assertSame(HttpStatus::CUSTOM_ERROR, $this->save(['click_type' => $mode, 'link' => $link, 'click_position' => $position])['code']);
  422. }
  423. $this->assertSame(HttpStatus::CUSTOM_ERROR, $this->save(['click_type' => $mode, 'link' => $link])['code']);
  424. $this->assertSame(0, DB::table('activity_rewards')->count());
  425. }
  426. $this->save(['click_type' => 'internal', 'link' => '/detail', 'click_position' => 'cover']);
  427. $id = (int) DB::table('activity_rewards')->value('id');
  428. $this->assertSame(HttpStatus::OK, $this->save(['id' => $id, 'click_position' => 'detail'])['code']);
  429. $row = ActivityReward::findOrFail($id)->toArray();
  430. $this->assertSame('detail', $row['click_position']); $this->assertSame('/detail', $row['link']);
  431. $this->assertSame('internal', $row['click_type']); $this->assertTrue($row['can_jump']);
  432. }
  433. public function test_trigger_position_migration_keeps_old_links_on_the_cover(): void
  434. {
  435. $migration = require dirname(__DIR__, 2) . '/database/migrations/2026_10_28_130000_add_activity_click_position.php';
  436. $migration->down();
  437. $linked = $this->rawActivity(['click_type' => 'external', 'link' => 'https://example.com']);
  438. $plain = $this->rawActivity();
  439. $before = DB::table('activity_rewards')->orderBy('id')->get();
  440. $migration->up();
  441. $this->assertSame('cover', DB::table('activity_rewards')->where('id', $linked)->value('click_position'));
  442. $this->assertNull(DB::table('activity_rewards')->where('id', $plain)->value('click_position'));
  443. foreach ($before as $old) {
  444. $new = DB::table('activity_rewards')->where('id', $old->id)->first();
  445. foreach ((array) $old as $field => $value) $this->assertSame($value, $new->$field);
  446. }
  447. }
  448. public function test_invalid_activity_links_do_not_modify_the_activity(): void
  449. {
  450. $this->save(['click_type' => 'internal', 'link' => '/original', 'click_position' => 'cover']);
  451. $id = (int) DB::table('activity_rewards')->value('id');
  452. $before = DB::table('activity_rewards')->where('id', $id)->first();
  453. foreach ([['click_type' => 'activity'], ['click_type' => null], ['click_type' => ['none']],
  454. ['click_type' => 'external', 'link' => null], ['click_type' => 'external', 'link' => 'javascript:alert(1)'],
  455. ['click_type' => 'external', 'link' => 'https://user:pass@example.com'], ['click_type' => 'external', 'link' => '/relative'],
  456. ['click_type' => 'internal', 'link' => '//example.com'], ['click_type' => 'internal', 'link' => '/\\evil'],
  457. ['click_type' => 'internal', 'link' => 'https://example.com'], ['click_type' => 'external', 'link' => ['bad']]] as $values) {
  458. $body = $this->save(['id' => $id] + $values + ['click_position' => 'cover']);
  459. $this->assertSame(HttpStatus::CUSTOM_ERROR, $body['code']);
  460. $this->assertEquals($before, DB::table('activity_rewards')->where('id', $id)->first());
  461. }
  462. }
  463. public function test_hot_category_is_saved_filtered_and_preserved_when_editing_other_fields(): void
  464. {
  465. $this->assertSame(HttpStatus::OK, $this->save(['game_category' => 'hot'])['code']);
  466. $id = (int) DB::table('activity_rewards')->value('id');
  467. $this->assertSame(HttpStatus::OK, $this->save(['id' => $id, 'title' => '热门活动'])['code']);
  468. $row = $this->listing(['game_category' => 'hot']);
  469. $this->assertSame(1, $row['total']);
  470. $this->assertSame('hot', $row['data'][0]['game_category']);
  471. $this->assertSame('热门', $row['data'][0]['game_category_name']);
  472. $this->assertSame(0, $this->listing(['game_category' => 'other'])['total']);
  473. }
  474. public function test_new_omission_is_null_edit_omission_preserves_category_and_explicit_values_clear_it(): void
  475. {
  476. $result = $this->save();
  477. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  478. $id = DB::table('activity_rewards')->value('id');
  479. $this->assertNull(DB::table('activity_rewards')->where('id', $id)->value('game_category'));
  480. $this->assertNull(ActivityReward::findOrFail($id)->toArray()['game_category_name']);
  481. $result = $this->save(['id' => $id, 'game_category' => 'third:1', 'sort' => 37, 'publish_platforms' => ['PC']]);
  482. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  483. $result = $this->save(['id' => $id, 'title' => '只修改标题']);
  484. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  485. $row = ActivityReward::findOrFail($id)->toArray();
  486. $this->assertSame('third:1', $row['game_category']);
  487. $this->assertSame('真人', $row['game_category_name']);
  488. $this->assertSame('即充即送', $row['type_name']);
  489. $this->assertSame(37, $row['sort']);
  490. $this->assertSame(['PC'], $row['publish_platforms']);
  491. foreach ([null, ''] as $clear) {
  492. $this->assertSame(HttpStatus::OK, $this->save(['id' => $id, 'game_category' => 'self:sport'])['code']);
  493. $result = $this->save(['id' => $id, 'game_category' => $clear]);
  494. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  495. $this->assertNull(DB::table('activity_rewards')->where('id', $id)->value('game_category'));
  496. $row = collect($this->listing()['data'])->keyBy('id')->get($id);
  497. $this->assertNull($row['game_category']);
  498. $this->assertNull($row['game_category_name']);
  499. }
  500. $this->assertSame(1, DB::table('activity_rewards')->count());
  501. $this->assertSame(0, DB::table('activity_reward_recipients')->count());
  502. }
  503. public function test_every_game_category_is_saved_and_named_for_normal_and_type_two_activities(): void
  504. {
  505. foreach ([2 => '普通活动', 3 => '即充即送'] as $type => $typeName) {
  506. foreach (ActivityCatalog::gameTypes() as $category => $label) {
  507. $result = $this->save(['type' => $type, 'game_category' => $category]);
  508. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  509. $row = ActivityReward::query()->latest('id')->firstOrFail()->toArray();
  510. $this->assertSame($type, (int) $row['type']);
  511. $this->assertSame($typeName, $row['type_name']);
  512. $this->assertSame($category, $row['game_category']);
  513. $this->assertSame($label, $row['game_category_name']);
  514. }
  515. }
  516. $this->assertSame(20, $this->listing()['total']);
  517. }
  518. public function test_other_category_is_preserved_on_omitted_edit_and_can_be_cleared_for_type_two_and_three(): void
  519. {
  520. foreach ([2, 3] as $type) {
  521. $result = $this->save(['type' => $type, 'game_category' => 'other']);
  522. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  523. $id = DB::table('activity_rewards')->latest('id')->value('id');
  524. $this->assertSame('other', DB::table('activity_rewards')->where('id', $id)->value('game_category'));
  525. $result = $this->save(['id' => $id, 'type' => $type, 'title' => '编辑其他分类活动']);
  526. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  527. $page = $this->listing(['game_category' => 'other', 'type' => (string) $type]);
  528. $this->assertSame(1, $page['total']);
  529. $this->assertSame($id, $page['data'][0]['id']);
  530. $this->assertSame('other', $page['data'][0]['game_category']);
  531. $this->assertSame('其他', $page['data'][0]['game_category_name']);
  532. $this->assertSame('其他', ActivityReward::findOrFail($id)->toArray()['game_category_name']);
  533. foreach ([null, ''] as $clear) {
  534. $this->assertSame(HttpStatus::OK, $this->save(['id' => $id, 'type' => $type, 'game_category' => 'other'])['code']);
  535. $result = $this->save(['id' => $id, 'type' => $type, 'game_category' => $clear]);
  536. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  537. $this->assertNull(DB::table('activity_rewards')->where('id', $id)->value('game_category'));
  538. $row = collect($this->listing()['data'])->keyBy('id')->get($id);
  539. $this->assertNull($row['game_category']);
  540. $this->assertNull($row['game_category_name']);
  541. $this->assertSame(0, $this->listing(['game_category' => 'other', 'type' => (string) $type])['total']);
  542. }
  543. }
  544. $this->assertSame(2, DB::table('activity_rewards')->count());
  545. $this->assertSame(0, DB::table('activity_reward_recipients')->count());
  546. }
  547. public function test_invalid_categories_are_rejected_by_controller_and_service_even_for_type_two(): void
  548. {
  549. $id = $this->rawActivity(['game_category' => 'third:1']);
  550. $before = DB::table('activity_rewards')->get()->toJson();
  551. foreach (['unknown', 'third:3', 'third:8', 'wallet', 'all', '其他', '1', 1, ['third:1']] as $category) {
  552. foreach ([2, 3] as $type) {
  553. $result = $this->save(['id' => $id, 'type' => $type, 'title' => '不应保存', 'game_category' => $category]);
  554. $this->assertSame(HttpStatus::CUSTOM_ERROR, $result['code']);
  555. try {
  556. ActivityRewardService::submit($this->form(['id' => $id, 'type' => $type, 'game_category' => $category]));
  557. $this->fail('Expected direct service category validation');
  558. } catch (ValidationException $e) {
  559. $this->assertArrayHasKey('game_category', $e->errors());
  560. }
  561. }
  562. $this->request('', ['page' => 1, 'limit' => 10, 'game_category' => $category]);
  563. $result = (new AdminActivityReward())->index()->getData(true);
  564. $this->assertSame(HttpStatus::CUSTOM_ERROR, $result['code']);
  565. }
  566. $this->assertSame($before, DB::table('activity_rewards')->get()->toJson());
  567. }
  568. public function test_combined_category_filters_apply_before_count_and_pagination_for_ordinary_activities(): void
  569. {
  570. $base = ['title' => '目标活动', 'type' => 3, 'status' => 0, 'lang' => 'zh', 'game_category' => 'third:1', 'publish_platforms' => ['PC']];
  571. $matching = [];
  572. for ($i = 1; $i <= 5; $i++) $matching[] = $this->rawActivity(['sort' => $i] + $base);
  573. foreach ([['game_category' => 'third:2'], ['publish_platforms' => ['H5']], ['status' => 1],
  574. ['lang' => 'en'], ['title' => '其他活动'], ['type' => 4], ['game_category' => null]] as $extra) {
  575. $this->rawActivity($extra + ['sort' => 100] + $base);
  576. }
  577. $ordinary = $this->rawActivity(['type' => 2, 'publish_platforms' => ['H5'], 'sort' => 101] + $base);
  578. $before = DB::table('activity_rewards')->orderBy('id')->get()->toJson();
  579. $matching = array_reverse($matching);
  580. $filters = ['title' => '目标', 'type' => '3', 'status' => '0', 'lang' => 'zh', 'platform' => 'pc', 'game_category' => 'third:1', 'limit' => 2];
  581. $seen = [];
  582. foreach ([1, 2, 3, 4] as $page) {
  583. $result = $this->listing($filters + ['page' => $page]);
  584. $this->assertSame(5, $result['total']);
  585. $this->assertSame(array_slice($matching, ($page - 1) * 2, 2), array_column($result['data'], 'id'));
  586. foreach ($result['data'] as $row) {
  587. $this->assertSame('third:1', $row['game_category']);
  588. $this->assertSame('真人', $row['game_category_name']);
  589. $this->assertSame('即充即送', $row['type_name']);
  590. }
  591. $seen = array_merge($seen, array_column($result['data'], 'id'));
  592. }
  593. $this->assertSame($matching, $seen);
  594. $ordinaryPage = $this->listing(['type' => '2', 'platform' => 'h5', 'status' => '0', 'lang' => 'zh', 'game_category' => 'third:1']);
  595. $this->assertSame(1, $ordinaryPage['total']);
  596. $this->assertSame([$ordinary], array_column($ordinaryPage['data'], 'id'));
  597. $this->assertSame('普通活动', $ordinaryPage['data'][0]['type_name']);
  598. $all = $this->listing();
  599. foreach ([null, ''] as $category) {
  600. $result = $this->listing(['game_category' => $category]);
  601. $this->assertSame($all['total'], $result['total']);
  602. $this->assertSame(array_column($all['data'], 'id'), array_column($result['data'], 'id'));
  603. }
  604. $this->assertSame($before, DB::table('activity_rewards')->orderBy('id')->get()->toJson());
  605. }
  606. public function test_other_category_filter_combines_with_existing_filters_before_count_and_pagination(): void
  607. {
  608. $base = ['title' => '目标其他活动', 'type' => 3, 'status' => 0, 'lang' => 'zh', 'game_category' => 'other', 'publish_platforms' => ['H5']];
  609. $matching = [];
  610. for ($i = 1; $i <= 5; $i++) $matching[] = $this->rawActivity(['sort' => $i] + $base);
  611. foreach ([['game_category' => 'third:1'], ['game_category' => null], ['publish_platforms' => ['PC']],
  612. ['status' => 1], ['lang' => 'en'], ['title' => '不同标题'], ['type' => 4]] as $extra) {
  613. $this->rawActivity($extra + ['sort' => 100] + $base);
  614. }
  615. $before = DB::table('activity_rewards')->orderBy('id')->get()->toJson();
  616. $matching = array_reverse($matching);
  617. $filters = ['title' => '目标', 'type' => '3', 'status' => '0', 'lang' => 'zh', 'platform' => 'h5', 'game_category' => 'other', 'limit' => 2];
  618. $seen = [];
  619. foreach ([1, 2, 3, 4] as $page) {
  620. $result = $this->listing($filters + ['page' => $page]);
  621. $this->assertSame(5, $result['total']);
  622. $this->assertSame(array_slice($matching, ($page - 1) * 2, 2), array_column($result['data'], 'id'));
  623. foreach ($result['data'] as $row) {
  624. $this->assertSame('other', $row['game_category']);
  625. $this->assertSame('其他', $row['game_category_name']);
  626. }
  627. $seen = array_merge($seen, array_column($result['data'], 'id'));
  628. }
  629. $this->assertSame($matching, $seen);
  630. $all = $this->listing();
  631. $this->assertSame(12, $all['total']);
  632. foreach ([null, ''] as $category) {
  633. $page = $this->listing(['game_category' => $category]);
  634. $this->assertSame($all['total'], $page['total']);
  635. $this->assertSame(array_column($all['data'], 'id'), array_column($page['data'], 'id'));
  636. }
  637. $defaultRows = array_filter($all['data'], fn ($row) => $row['game_category'] === null);
  638. $this->assertCount(1, $defaultRows);
  639. $this->assertNull(array_values($defaultRows)[0]['game_category_name']);
  640. $this->assertSame($before, DB::table('activity_rewards')->orderBy('id')->get()->toJson());
  641. }
  642. public function test_legacy_type_one_remains_readable_and_editable_without_becoming_an_option(): void
  643. {
  644. $result = $this->save(['type' => 1]);
  645. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  646. $id = DB::table('activity_rewards')->value('id');
  647. DB::table('activity_rewards')->where('id', $id)->update(['budget_used' => '12.34', 'rewarded_count' => 2]);
  648. $result = $this->save(['id' => $id, 'type' => 1, 'title' => '旧类型活动', 'game_category' => 'self:lottery']);
  649. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  650. $row = $this->listing(['type' => '1'])['data'][0];
  651. $this->assertSame(1, (int) $row['type']);
  652. $this->assertNull($row['type_name']);
  653. $this->assertSame('self:lottery', $row['game_category']);
  654. $this->assertSame('F1彩票', $row['game_category_name']);
  655. $this->assertSame('12.34', $row['budget_used']);
  656. $this->assertSame(2, $row['rewarded_count']);
  657. $legacy = $this->rawActivity(['type' => 99, 'game_category' => null]);
  658. $this->assertNull(ActivityReward::findOrFail($legacy)->toArray()['type_name']);
  659. $this->request('options');
  660. $options = (new AdminActivityReward())->options()->getData(true)['data']['activity_types'];
  661. $this->assertSame([2, 3, 4, 5, 6], array_column($options, 'value'));
  662. }
  663. public function test_lottery_merge_migration_preserves_activities_and_their_business_settings(): void
  664. {
  665. $ordinary = $this->rawActivity(['type' => 2, 'game_category' => 'third:3', 'params' => '{"reward_ratio":5,"game_category":"third:3"}']);
  666. $promotion = $this->rawActivity(['type' => 3, 'game_category' => 'third:3', 'sort' => 21, 'show_ineligible' => false]);
  667. $existingF1 = $this->rawActivity(['game_category' => 'self:lottery']);
  668. $unrelated = $this->rawActivity(['game_category' => 'third:1']);
  669. $before = DB::table('activity_rewards')->orderBy('id')->get()->keyBy('id');
  670. $migration = require dirname(__DIR__, 2) . '/database/migrations/2026_11_02_120000_merge_activity_lottery_category.php';
  671. $migration->up();
  672. $this->assertSame(0, DB::table('activity_rewards')->where('game_category', 'third:3')->count());
  673. foreach ([$ordinary, $promotion, $existingF1, $unrelated] as $id) {
  674. $expected = (array) $before[$id];
  675. if (in_array($id, [$ordinary, $promotion], true)) $expected['game_category'] = 'self:lottery';
  676. $this->assertSame($expected, (array) DB::table('activity_rewards')->where('id', $id)->first());
  677. }
  678. $result = $this->listing(['game_category' => 'self:lottery']);
  679. $this->assertSame(3, $result['total']);
  680. foreach ($result['data'] as $row) $this->assertSame('F1彩票', $row['game_category_name']);
  681. $this->assertSame(HttpStatus::OK, $this->save(['game_category' => 'self:lottery'])['code']);
  682. $this->assertSame(4, $this->listing(['game_category' => 'self:lottery'])['total']);
  683. $snapshot = DB::table('activity_rewards')->orderBy('id')->get()->toJson();
  684. $migration->up(); $migration->down();
  685. $this->assertSame($snapshot, DB::table('activity_rewards')->orderBy('id')->get()->toJson());
  686. }
  687. public function test_category_migration_adds_nullable_index_without_changing_existing_rows_and_rolls_back(): void
  688. {
  689. $this->migration()->down();
  690. $this->assertFalse(Schema::hasColumn('activity_rewards', 'game_category'));
  691. $id = $this->rawActivity(['type' => 1, 'sort' => 19]);
  692. $before = (array) DB::table('activity_rewards')->find($id);
  693. $this->migration()->up();
  694. $after = (array) DB::table('activity_rewards')->find($id);
  695. $this->assertNull($after['game_category']);
  696. unset($after['game_category']);
  697. $this->assertSame($before, $after);
  698. $column = collect(DB::select('PRAGMA table_info(bot_activity_rewards)'))->firstWhere('name', 'game_category');
  699. $this->assertSame(0, (int) $column->notnull);
  700. $indexes = DB::select("SELECT sql FROM sqlite_master WHERE type = 'index' AND tbl_name = ?", ['bot_activity_rewards']);
  701. $this->assertNotEmpty(array_filter($indexes, fn ($index) => str_contains((string) $index->sql, 'game_category')));
  702. $newId = $this->rawActivity();
  703. $this->assertNull(DB::table('activity_rewards')->where('id', $newId)->value('game_category'));
  704. $this->migration()->down();
  705. $this->assertFalse(Schema::hasColumn('activity_rewards', 'game_category'));
  706. $this->assertTrue(Schema::hasColumn('activity_rewards', 'reward_quota'));
  707. $this->assertSame($before, (array) DB::table('activity_rewards')->find($id));
  708. $this->assertSame(2, DB::table('activity_rewards')->count());
  709. }
  710. }