ActivityCatalogTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 Illuminate\Config\Repository;
  10. use Illuminate\Database\Capsule\Manager;
  11. use Illuminate\Database\Schema\Blueprint;
  12. use Illuminate\Events\Dispatcher;
  13. use Illuminate\Foundation\Application;
  14. use Illuminate\Http\JsonResponse;
  15. use Illuminate\Http\Request;
  16. use Illuminate\Routing\Router;
  17. use Illuminate\Support\Facades\DB;
  18. use Illuminate\Support\Facades\Facade;
  19. use Illuminate\Support\Facades\Schema;
  20. use Illuminate\Translation\ArrayLoader;
  21. use Illuminate\Translation\Translator;
  22. use Illuminate\Validation\Factory;
  23. use Illuminate\Validation\ValidationException;
  24. use PHPUnit\Framework\TestCase;
  25. /** Real controllers and service validation against SQLite only; no application boot or external databases. */
  26. class ActivityCatalogTest extends TestCase
  27. {
  28. private Application $app;
  29. protected function setUp(): void
  30. {
  31. Facade::clearResolvedInstances();
  32. $this->app = new Application(dirname(__DIR__, 2));
  33. $this->app->instance('config', new Repository([
  34. 'app' => ['locale' => 'zh', 'url' => 'https://activity.example.test', 'timezone' => 'Asia/Shanghai'],
  35. ]));
  36. Facade::setFacadeApplication($this->app);
  37. $db = new Manager($this->app);
  38. $db->addConnection(['driver' => 'sqlite', 'database' => ':memory:', 'prefix' => 'bot_']);
  39. $db->setAsGlobal(); $db->bootEloquent();
  40. $this->app->instance('db', $db->getDatabaseManager());
  41. $this->app->bind('db.schema', fn () => $db->schema());
  42. $translator = new Translator(new ArrayLoader(), 'zh');
  43. $this->app->instance('translator', $translator);
  44. $this->app->instance('validator', new Factory($translator, $this->app));
  45. $this->app->instance(\Illuminate\Contracts\Routing\ResponseFactory::class, new class {
  46. public function json($data, $status = 200, $headers = [], $options = 0) {
  47. return new JsonResponse($data, $status, $headers, $options);
  48. }
  49. });
  50. Request::macro('validate', function (array $rules) {
  51. return app('validator')->make($this->all(), $rules)->validate();
  52. });
  53. $db->schema()->create('activity_rewards', function (Blueprint $t) {
  54. $t->id(); $t->string('title'); $t->string('sub_title');
  55. $t->integer('start_time'); $t->integer('end_time'); $t->integer('status')->default(1);
  56. $t->integer('type')->default(1); $t->string('lang')->default('zh');
  57. foreach (['detail_image', 'pc_image', 'detail_banner', 'pc_banner', 'content', 'params'] as $field) {
  58. $t->text($field)->nullable();
  59. }
  60. $t->timestamps();
  61. });
  62. (require dirname(__DIR__, 2) . '/database/migrations/2026_10_13_120000_add_activity_eligibility_fields.php')->up();
  63. (require dirname(__DIR__, 2) . '/database/migrations/2026_10_14_120000_add_activity_reward_quotas.php')->up();
  64. $this->migration()->up();
  65. (require dirname(__DIR__, 2) . '/database/migrations/2026_10_28_120000_add_activity_click_behavior.php')->up();
  66. (require dirname(__DIR__, 2) . '/database/migrations/2026_10_28_130000_add_activity_click_position.php')->up();
  67. $this->request('');
  68. }
  69. protected function tearDown(): void
  70. {
  71. DB::disconnect(); Facade::clearResolvedInstances(); parent::tearDown();
  72. }
  73. private function migration()
  74. {
  75. return require dirname(__DIR__, 2) . '/database/migrations/2026_10_25_120000_add_game_category_to_activity_rewards.php';
  76. }
  77. private function request(string $action, array $params = [], string $method = 'GET'): void
  78. {
  79. $this->app->instance('request', Request::create('/admin/ActivityReward/' . $action, $method, $params));
  80. }
  81. private function form(array $overrides = []): array
  82. {
  83. return $overrides + [
  84. 'title' => '活动分类测试', 'sub_title' => '分类说明', 'type' => 3, 'status' => 1, 'lang' => 'zh',
  85. 'start_time' => '2026-10-01 12:00:00', 'end_time' => '2026-12-31 12:00:00',
  86. 'detail_image' => '/app.png', 'pc_image' => '/pc.png',
  87. 'detail_banner' => '/app-banner.png', 'pc_banner' => '/pc-banner.png',
  88. ];
  89. }
  90. private function save(array $params = []): array
  91. {
  92. $this->request('submit', $this->form($params), 'POST');
  93. return (new AdminActivityReward())->store()->getData(true);
  94. }
  95. private function listing(array $filters = []): array
  96. {
  97. $this->request('', $filters + ['page' => 1, 'limit' => 100]);
  98. $result = (new AdminActivityReward())->index()->getData(true);
  99. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  100. return $result['data'];
  101. }
  102. private function rawActivity(array $overrides = []): int
  103. {
  104. $data = $this->form($overrides);
  105. $data['start_time'] = strtotime($data['start_time']);
  106. $data['end_time'] = strtotime($data['end_time']);
  107. if (isset($data['publish_platforms']) && is_array($data['publish_platforms'])) {
  108. $data['publish_platforms'] = json_encode($data['publish_platforms']);
  109. }
  110. return DB::table('activity_rewards')->insertGetId($data);
  111. }
  112. public function test_options_match_all_shared_game_categories_and_five_activity_types_on_an_authenticated_route(): void
  113. {
  114. $this->rawActivity(['type' => 1]);
  115. $before = DB::table('activity_rewards')->get()->toJson();
  116. $this->request('options');
  117. $body = (new AdminActivityReward())->options()->getData(true);
  118. $this->assertSame(HttpStatus::OK, $body['code'], $body['msg']);
  119. $financialCategories = ['third:1' => '真人', 'third:2' => '电子', 'third:3' => '彩票',
  120. 'third:4' => '体育', 'third:5' => '电竞', 'third:6' => '捕鱼', 'third:7' => '棋牌',
  121. 'self:sport' => 'F1体育', 'self:lottery' => 'F1彩票'];
  122. $this->assertSame($financialCategories, ShareDefaults::categories());
  123. $this->assertCount(9, ShareDefaults::categories());
  124. $this->assertArrayNotHasKey('other', ShareDefaults::categories());
  125. $this->assertSame($financialCategories + ['other' => '其他'], ActivityCatalog::gameTypes());
  126. $expectedGames = [];
  127. foreach ($financialCategories + ['other' => '其他'] as $value => $label) $expectedGames[] = compact('value', 'label');
  128. $this->assertCount(10, $expectedGames);
  129. $this->assertSame($expectedGames, $body['data']['game_types']);
  130. $this->assertNotContains('', array_column($body['data']['game_types'], 'value'));
  131. $this->assertNotContains('wallet', array_column($body['data']['game_types'], 'value'));
  132. $this->assertNotContains('all', array_column($body['data']['game_types'], 'value'));
  133. $this->assertSame([
  134. ['value' => 2, 'label' => '普通活动'],
  135. ['value' => 3, 'label' => '即充即送'],
  136. ['value' => 4, 'label' => '老用户召集令'],
  137. ['value' => 5, 'label' => 'USDT使用教学'],
  138. ['value' => 6, 'label' => '余额宝'],
  139. ], $body['data']['activity_types']);
  140. $this->assertNotContains(1, array_column($body['data']['activity_types'], 'value'));
  141. $router = new Router(new Dispatcher($this->app), $this->app);
  142. $this->app->instance('router', $router);
  143. $router->group(['prefix' => 'admin'], function () {
  144. require dirname(__DIR__, 2) . '/routes/admin.php';
  145. });
  146. $route = $router->getRoutes()->match(Request::create('/admin/ActivityReward/options', 'GET'));
  147. $this->assertSame(AdminActivityReward::class . '@options', $route->getAction('uses'));
  148. $this->assertContains('admin.jwt', $route->middleware());
  149. $this->assertContains('check.button.uri', $route->middleware());
  150. $this->assertSame($before, DB::table('activity_rewards')->get()->toJson());
  151. $this->assertSame(0, DB::table('activity_reward_recipients')->count());
  152. }
  153. public function test_activity_click_behavior_saves_preserves_and_clears_for_normal_and_type_two_activities(): void
  154. {
  155. $this->request('options');
  156. $this->assertSame(\App\Support\AnnouncementCatalog::options(\App\Support\AnnouncementCatalog::CLICKS),
  157. (new AdminActivityReward())->options()->getData(true)['data']['click_types']);
  158. foreach ([2, 3] as $type) {
  159. $body = $this->save(['type' => $type]);
  160. $this->assertSame(HttpStatus::OK, $body['code']);
  161. $id = (int) DB::table('activity_rewards')->latest('id')->value('id');
  162. $this->assertSame('none', DB::table('activity_rewards')->where('id', $id)->value('click_type'));
  163. foreach (['external' => 'https://example.com/promotion?from=activity', 'internal' => '/promotion/detail?id=3'] as $mode => $link) {
  164. $body = $this->save(['id' => $id, 'type' => $type, 'click_type' => $mode, 'link' => $link, 'click_position' => $mode === 'external' ? 'cover' : 'detail']);
  165. $this->assertSame(HttpStatus::OK, $body['code'], $body['msg']);
  166. $this->assertSame(HttpStatus::OK, $this->save(['id' => $id, 'type' => $type, 'title' => '只改标题'])['code']);
  167. $row = collect($this->listing()['data'])->keyBy('id')->get($id);
  168. $this->assertSame($mode, $row['click_type']); $this->assertSame($link, $row['link']); $this->assertTrue($row['can_jump']);
  169. $this->assertSame($mode === 'external' ? 'cover' : 'detail', $row['click_position']);
  170. }
  171. $this->assertSame(HttpStatus::OK, $this->save(['id' => $id, 'type' => $type, 'click_type' => 'none', 'link' => ['unused'], 'click_position' => ['unused']])['code']);
  172. $row = ActivityReward::findOrFail($id)->toArray();
  173. $this->assertSame('none', $row['click_type']); $this->assertNull($row['link']); $this->assertFalse($row['can_jump']);
  174. $this->assertNull($row['click_position']);
  175. $this->assertNull(DB::table('activity_rewards')->where('id', $id)->value('click_position'));
  176. $this->assertNull(DB::table('activity_rewards')->where('id', $id)->value('link'));
  177. }
  178. }
  179. public function test_nonempty_links_require_a_trigger_position_and_can_switch_position_independently(): void
  180. {
  181. $this->request('options');
  182. $this->assertSame([['value' => 'cover', 'label' => '活动列表封面'], ['value' => 'detail', 'label' => '活动详情按钮']],
  183. (new AdminActivityReward())->options()->getData(true)['data']['click_positions']);
  184. foreach (['external' => 'https://example.com', 'internal' => '/detail'] as $mode => $link) {
  185. foreach ([null, '', 'invalid', ['cover']] as $position) {
  186. $this->assertSame(HttpStatus::CUSTOM_ERROR, $this->save(['click_type' => $mode, 'link' => $link, 'click_position' => $position])['code']);
  187. }
  188. $this->assertSame(HttpStatus::CUSTOM_ERROR, $this->save(['click_type' => $mode, 'link' => $link])['code']);
  189. $this->assertSame(0, DB::table('activity_rewards')->count());
  190. }
  191. $this->save(['click_type' => 'internal', 'link' => '/detail', 'click_position' => 'cover']);
  192. $id = (int) DB::table('activity_rewards')->value('id');
  193. $this->assertSame(HttpStatus::OK, $this->save(['id' => $id, 'click_position' => 'detail'])['code']);
  194. $row = ActivityReward::findOrFail($id)->toArray();
  195. $this->assertSame('detail', $row['click_position']); $this->assertSame('/detail', $row['link']);
  196. $this->assertSame('internal', $row['click_type']); $this->assertTrue($row['can_jump']);
  197. }
  198. public function test_trigger_position_migration_keeps_old_links_on_the_cover(): void
  199. {
  200. $migration = require dirname(__DIR__, 2) . '/database/migrations/2026_10_28_130000_add_activity_click_position.php';
  201. $migration->down();
  202. $linked = $this->rawActivity(['click_type' => 'external', 'link' => 'https://example.com']);
  203. $plain = $this->rawActivity();
  204. $before = DB::table('activity_rewards')->orderBy('id')->get();
  205. $migration->up();
  206. $this->assertSame('cover', DB::table('activity_rewards')->where('id', $linked)->value('click_position'));
  207. $this->assertNull(DB::table('activity_rewards')->where('id', $plain)->value('click_position'));
  208. foreach ($before as $old) {
  209. $new = DB::table('activity_rewards')->where('id', $old->id)->first();
  210. foreach ((array) $old as $field => $value) $this->assertSame($value, $new->$field);
  211. }
  212. }
  213. public function test_invalid_activity_links_do_not_modify_the_activity(): void
  214. {
  215. $this->save(['click_type' => 'internal', 'link' => '/original', 'click_position' => 'cover']);
  216. $id = (int) DB::table('activity_rewards')->value('id');
  217. $before = DB::table('activity_rewards')->where('id', $id)->first();
  218. foreach ([['click_type' => 'activity'], ['click_type' => null], ['click_type' => ['none']],
  219. ['click_type' => 'external', 'link' => null], ['click_type' => 'external', 'link' => 'javascript:alert(1)'],
  220. ['click_type' => 'external', 'link' => 'https://user:pass@example.com'], ['click_type' => 'external', 'link' => '/relative'],
  221. ['click_type' => 'internal', 'link' => '//example.com'], ['click_type' => 'internal', 'link' => '/\\evil'],
  222. ['click_type' => 'internal', 'link' => 'https://example.com'], ['click_type' => 'external', 'link' => ['bad']]] as $values) {
  223. $body = $this->save(['id' => $id] + $values + ['click_position' => 'cover']);
  224. $this->assertSame(HttpStatus::CUSTOM_ERROR, $body['code']);
  225. $this->assertEquals($before, DB::table('activity_rewards')->where('id', $id)->first());
  226. }
  227. }
  228. public function test_new_omission_is_null_edit_omission_preserves_category_and_explicit_values_clear_it(): void
  229. {
  230. $result = $this->save();
  231. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  232. $id = DB::table('activity_rewards')->value('id');
  233. $this->assertNull(DB::table('activity_rewards')->where('id', $id)->value('game_category'));
  234. $this->assertNull(ActivityReward::findOrFail($id)->toArray()['game_category_name']);
  235. $result = $this->save(['id' => $id, 'game_category' => 'third:1', 'sort' => 37, 'publish_platforms' => ['PC']]);
  236. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  237. $result = $this->save(['id' => $id, 'title' => '只修改标题']);
  238. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  239. $row = ActivityReward::findOrFail($id)->toArray();
  240. $this->assertSame('third:1', $row['game_category']);
  241. $this->assertSame('真人', $row['game_category_name']);
  242. $this->assertSame('即充即送', $row['type_name']);
  243. $this->assertSame(37, $row['sort']);
  244. $this->assertSame(['PC'], $row['publish_platforms']);
  245. foreach ([null, ''] as $clear) {
  246. $this->assertSame(HttpStatus::OK, $this->save(['id' => $id, 'game_category' => 'self:sport'])['code']);
  247. $result = $this->save(['id' => $id, 'game_category' => $clear]);
  248. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  249. $this->assertNull(DB::table('activity_rewards')->where('id', $id)->value('game_category'));
  250. $row = collect($this->listing()['data'])->keyBy('id')->get($id);
  251. $this->assertNull($row['game_category']);
  252. $this->assertNull($row['game_category_name']);
  253. }
  254. $this->assertSame(1, DB::table('activity_rewards')->count());
  255. $this->assertSame(0, DB::table('activity_reward_recipients')->count());
  256. }
  257. public function test_every_game_category_is_saved_and_named_for_normal_and_type_two_activities(): void
  258. {
  259. foreach ([2 => '普通活动', 3 => '即充即送'] as $type => $typeName) {
  260. foreach (ActivityCatalog::gameTypes() as $category => $label) {
  261. $result = $this->save(['type' => $type, 'game_category' => $category]);
  262. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  263. $row = ActivityReward::query()->latest('id')->firstOrFail()->toArray();
  264. $this->assertSame($type, (int) $row['type']);
  265. $this->assertSame($typeName, $row['type_name']);
  266. $this->assertSame($category, $row['game_category']);
  267. $this->assertSame($label, $row['game_category_name']);
  268. }
  269. }
  270. $this->assertSame(20, $this->listing()['total']);
  271. }
  272. public function test_other_category_is_preserved_on_omitted_edit_and_can_be_cleared_for_type_two_and_three(): void
  273. {
  274. foreach ([2, 3] as $type) {
  275. $result = $this->save(['type' => $type, 'game_category' => 'other']);
  276. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  277. $id = DB::table('activity_rewards')->latest('id')->value('id');
  278. $this->assertSame('other', DB::table('activity_rewards')->where('id', $id)->value('game_category'));
  279. $result = $this->save(['id' => $id, 'type' => $type, 'title' => '编辑其他分类活动']);
  280. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  281. $page = $this->listing(['game_category' => 'other', 'type' => (string) $type]);
  282. $this->assertSame(1, $page['total']);
  283. $this->assertSame($id, $page['data'][0]['id']);
  284. $this->assertSame('other', $page['data'][0]['game_category']);
  285. $this->assertSame('其他', $page['data'][0]['game_category_name']);
  286. $this->assertSame('其他', ActivityReward::findOrFail($id)->toArray()['game_category_name']);
  287. foreach ([null, ''] as $clear) {
  288. $this->assertSame(HttpStatus::OK, $this->save(['id' => $id, 'type' => $type, 'game_category' => 'other'])['code']);
  289. $result = $this->save(['id' => $id, 'type' => $type, 'game_category' => $clear]);
  290. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  291. $this->assertNull(DB::table('activity_rewards')->where('id', $id)->value('game_category'));
  292. $row = collect($this->listing()['data'])->keyBy('id')->get($id);
  293. $this->assertNull($row['game_category']);
  294. $this->assertNull($row['game_category_name']);
  295. $this->assertSame(0, $this->listing(['game_category' => 'other', 'type' => (string) $type])['total']);
  296. }
  297. }
  298. $this->assertSame(2, DB::table('activity_rewards')->count());
  299. $this->assertSame(0, DB::table('activity_reward_recipients')->count());
  300. }
  301. public function test_invalid_categories_are_rejected_by_controller_and_service_even_for_type_two(): void
  302. {
  303. $id = $this->rawActivity(['game_category' => 'third:1']);
  304. $before = DB::table('activity_rewards')->get()->toJson();
  305. foreach (['unknown', 'third:8', 'wallet', 'all', '其他', '1', 1, ['third:1']] as $category) {
  306. foreach ([2, 3] as $type) {
  307. $result = $this->save(['id' => $id, 'type' => $type, 'title' => '不应保存', 'game_category' => $category]);
  308. $this->assertSame(HttpStatus::CUSTOM_ERROR, $result['code']);
  309. try {
  310. ActivityRewardService::submit($this->form(['id' => $id, 'type' => $type, 'game_category' => $category]));
  311. $this->fail('Expected direct service category validation');
  312. } catch (ValidationException $e) {
  313. $this->assertArrayHasKey('game_category', $e->errors());
  314. }
  315. }
  316. $this->request('', ['page' => 1, 'limit' => 10, 'game_category' => $category]);
  317. $result = (new AdminActivityReward())->index()->getData(true);
  318. $this->assertSame(HttpStatus::CUSTOM_ERROR, $result['code']);
  319. }
  320. $this->assertSame($before, DB::table('activity_rewards')->get()->toJson());
  321. }
  322. public function test_combined_category_filters_apply_before_count_and_pagination_with_type_two_platform_exception(): void
  323. {
  324. $base = ['title' => '目标活动', 'type' => 3, 'status' => 0, 'lang' => 'zh', 'game_category' => 'third:1', 'publish_platforms' => ['PC']];
  325. $matching = [];
  326. for ($i = 1; $i <= 5; $i++) $matching[] = $this->rawActivity(['sort' => $i] + $base);
  327. foreach ([['game_category' => 'third:2'], ['publish_platforms' => ['H5']], ['status' => 1],
  328. ['lang' => 'en'], ['title' => '其他活动'], ['type' => 4], ['game_category' => null]] as $extra) {
  329. $this->rawActivity($extra + ['sort' => 100] + $base);
  330. }
  331. $ordinary = $this->rawActivity(['type' => 2, 'publish_platforms' => ['H5'], 'sort' => 101] + $base);
  332. $before = DB::table('activity_rewards')->orderBy('id')->get()->toJson();
  333. $matching = array_reverse($matching);
  334. $filters = ['title' => '目标', 'type' => '3', 'status' => '0', 'lang' => 'zh', 'platform' => 'pc', 'game_category' => 'third:1', 'limit' => 2];
  335. $seen = [];
  336. foreach ([1, 2, 3, 4] as $page) {
  337. $result = $this->listing($filters + ['page' => $page]);
  338. $this->assertSame(5, $result['total']);
  339. $this->assertSame(array_slice($matching, ($page - 1) * 2, 2), array_column($result['data'], 'id'));
  340. foreach ($result['data'] as $row) {
  341. $this->assertSame('third:1', $row['game_category']);
  342. $this->assertSame('真人', $row['game_category_name']);
  343. $this->assertSame('即充即送', $row['type_name']);
  344. }
  345. $seen = array_merge($seen, array_column($result['data'], 'id'));
  346. }
  347. $this->assertSame($matching, $seen);
  348. $ordinaryPage = $this->listing(['type' => '2', 'platform' => 'pc', 'status' => '0', 'lang' => 'zh', 'game_category' => 'third:1']);
  349. $this->assertSame(1, $ordinaryPage['total']);
  350. $this->assertSame([$ordinary], array_column($ordinaryPage['data'], 'id'));
  351. $this->assertSame('普通活动', $ordinaryPage['data'][0]['type_name']);
  352. $all = $this->listing();
  353. foreach ([null, ''] as $category) {
  354. $result = $this->listing(['game_category' => $category]);
  355. $this->assertSame($all['total'], $result['total']);
  356. $this->assertSame(array_column($all['data'], 'id'), array_column($result['data'], 'id'));
  357. }
  358. $this->assertSame($before, DB::table('activity_rewards')->orderBy('id')->get()->toJson());
  359. }
  360. public function test_other_category_filter_combines_with_existing_filters_before_count_and_pagination(): void
  361. {
  362. $base = ['title' => '目标其他活动', 'type' => 3, 'status' => 0, 'lang' => 'zh', 'game_category' => 'other', 'publish_platforms' => ['H5']];
  363. $matching = [];
  364. for ($i = 1; $i <= 5; $i++) $matching[] = $this->rawActivity(['sort' => $i] + $base);
  365. foreach ([['game_category' => 'third:1'], ['game_category' => null], ['publish_platforms' => ['PC']],
  366. ['status' => 1], ['lang' => 'en'], ['title' => '不同标题'], ['type' => 4]] as $extra) {
  367. $this->rawActivity($extra + ['sort' => 100] + $base);
  368. }
  369. $before = DB::table('activity_rewards')->orderBy('id')->get()->toJson();
  370. $matching = array_reverse($matching);
  371. $filters = ['title' => '目标', 'type' => '3', 'status' => '0', 'lang' => 'zh', 'platform' => 'h5', 'game_category' => 'other', 'limit' => 2];
  372. $seen = [];
  373. foreach ([1, 2, 3, 4] as $page) {
  374. $result = $this->listing($filters + ['page' => $page]);
  375. $this->assertSame(5, $result['total']);
  376. $this->assertSame(array_slice($matching, ($page - 1) * 2, 2), array_column($result['data'], 'id'));
  377. foreach ($result['data'] as $row) {
  378. $this->assertSame('other', $row['game_category']);
  379. $this->assertSame('其他', $row['game_category_name']);
  380. }
  381. $seen = array_merge($seen, array_column($result['data'], 'id'));
  382. }
  383. $this->assertSame($matching, $seen);
  384. $all = $this->listing();
  385. $this->assertSame(12, $all['total']);
  386. foreach ([null, ''] as $category) {
  387. $page = $this->listing(['game_category' => $category]);
  388. $this->assertSame($all['total'], $page['total']);
  389. $this->assertSame(array_column($all['data'], 'id'), array_column($page['data'], 'id'));
  390. }
  391. $defaultRows = array_filter($all['data'], fn ($row) => $row['game_category'] === null);
  392. $this->assertCount(1, $defaultRows);
  393. $this->assertNull(array_values($defaultRows)[0]['game_category_name']);
  394. $this->assertSame($before, DB::table('activity_rewards')->orderBy('id')->get()->toJson());
  395. }
  396. public function test_legacy_type_one_remains_readable_and_editable_without_becoming_an_option(): void
  397. {
  398. $result = $this->save(['type' => 1]);
  399. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  400. $id = DB::table('activity_rewards')->value('id');
  401. DB::table('activity_rewards')->where('id', $id)->update(['budget_used' => '12.34', 'rewarded_count' => 2]);
  402. $result = $this->save(['id' => $id, 'type' => 1, 'title' => '旧类型活动', 'game_category' => 'self:lottery']);
  403. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  404. $row = $this->listing(['type' => '1'])['data'][0];
  405. $this->assertSame(1, (int) $row['type']);
  406. $this->assertNull($row['type_name']);
  407. $this->assertSame('self:lottery', $row['game_category']);
  408. $this->assertSame('F1彩票', $row['game_category_name']);
  409. $this->assertSame('12.34', $row['budget_used']);
  410. $this->assertSame(2, $row['rewarded_count']);
  411. $legacy = $this->rawActivity(['type' => 99, 'game_category' => null]);
  412. $this->assertNull(ActivityReward::findOrFail($legacy)->toArray()['type_name']);
  413. $this->request('options');
  414. $options = (new AdminActivityReward())->options()->getData(true)['data']['activity_types'];
  415. $this->assertSame([2, 3, 4, 5, 6], array_column($options, 'value'));
  416. }
  417. public function test_category_migration_adds_nullable_index_without_changing_existing_rows_and_rolls_back(): void
  418. {
  419. $this->migration()->down();
  420. $this->assertFalse(Schema::hasColumn('activity_rewards', 'game_category'));
  421. $id = $this->rawActivity(['type' => 1, 'sort' => 19]);
  422. $before = (array) DB::table('activity_rewards')->find($id);
  423. $this->migration()->up();
  424. $after = (array) DB::table('activity_rewards')->find($id);
  425. $this->assertNull($after['game_category']);
  426. unset($after['game_category']);
  427. $this->assertSame($before, $after);
  428. $column = collect(DB::select('PRAGMA table_info(bot_activity_rewards)'))->firstWhere('name', 'game_category');
  429. $this->assertSame(0, (int) $column->notnull);
  430. $indexes = DB::select("SELECT sql FROM sqlite_master WHERE type = 'index' AND tbl_name = ?", ['bot_activity_rewards']);
  431. $this->assertNotEmpty(array_filter($indexes, fn ($index) => str_contains((string) $index->sql, 'game_category')));
  432. $newId = $this->rawActivity();
  433. $this->assertNull(DB::table('activity_rewards')->where('id', $newId)->value('game_category'));
  434. $this->migration()->down();
  435. $this->assertFalse(Schema::hasColumn('activity_rewards', 'game_category'));
  436. $this->assertTrue(Schema::hasColumn('activity_rewards', 'reward_quota'));
  437. $this->assertSame($before, (array) DB::table('activity_rewards')->find($id));
  438. $this->assertSame(2, DB::table('activity_rewards')->count());
  439. }
  440. }