ActivityCatalogTest.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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. Schema::create('announcements', function (Blueprint $t) { $t->id(); $t->string('click_type'); $t->string('link', 2048)->nullable(); });
  68. (require dirname(__DIR__, 2) . '/database/migrations/2026_10_30_120000_add_platform_links_to_announcements_and_activities.php')->up();
  69. $this->request('');
  70. }
  71. protected function tearDown(): void
  72. {
  73. DB::disconnect(); Facade::clearResolvedInstances(); parent::tearDown();
  74. }
  75. private function migration()
  76. {
  77. return require dirname(__DIR__, 2) . '/database/migrations/2026_10_25_120000_add_game_category_to_activity_rewards.php';
  78. }
  79. private function request(string $action, array $params = [], string $method = 'GET'): void
  80. {
  81. $this->app->instance('request', Request::create('/admin/ActivityReward/' . $action, $method, $params));
  82. }
  83. private function form(array $overrides = []): array
  84. {
  85. return $overrides + [
  86. 'title' => '活动分类测试', 'sub_title' => '分类说明', 'type' => 3, 'status' => 1, 'lang' => 'zh',
  87. 'start_time' => '2026-10-01 12:00:00', 'end_time' => '2026-12-31 12:00:00',
  88. 'detail_image' => '/app.png', 'pc_image' => '/pc.png',
  89. 'detail_banner' => '/app-banner.png', 'pc_banner' => '/pc-banner.png',
  90. ];
  91. }
  92. private function save(array $params = []): array
  93. {
  94. $this->request('submit', $this->form($params), 'POST');
  95. return (new AdminActivityReward())->store()->getData(true);
  96. }
  97. private function listing(array $filters = []): array
  98. {
  99. $this->request('', $filters + ['page' => 1, 'limit' => 100]);
  100. $result = (new AdminActivityReward())->index()->getData(true);
  101. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  102. return $result['data'];
  103. }
  104. private function rawActivity(array $overrides = []): int
  105. {
  106. $data = $this->form($overrides);
  107. $data['start_time'] = strtotime($data['start_time']);
  108. $data['end_time'] = strtotime($data['end_time']);
  109. if (isset($data['publish_platforms']) && is_array($data['publish_platforms'])) {
  110. $data['publish_platforms'] = json_encode($data['publish_platforms']);
  111. }
  112. return DB::table('activity_rewards')->insertGetId($data);
  113. }
  114. public function test_dual_routes_round_trip_through_admin_save_and_listing_with_cover_and_detail(): void
  115. {
  116. foreach (['cover', 'detail'] as $position) {
  117. $saved = $this->save(['type' => 2, 'click_type' => 'internal', 'link_pc' => '/pc/promotion?id=5', 'link_h5' => '/h5/promotion?id=5', 'click_position' => $position]);
  118. $this->assertSame(HttpStatus::OK, $saved['code'], $saved['msg']);
  119. $row = $this->listing()['data'][0]; $id = $row['id'];
  120. $this->assertNull($row['link']); $this->assertTrue($row['can_jump']);
  121. $this->assertSame('/pc/promotion?id=5', $row['link_pc']); $this->assertSame('/h5/promotion?id=5', $row['link_h5']);
  122. $this->assertSame($position, $row['click_position']);
  123. $this->assertSame(HttpStatus::OK, $this->save(['id' => $id, 'title' => '仅改标题'])['code']);
  124. $this->assertSame('/h5/promotion?id=5', ActivityReward::findOrFail($id)->link_h5);
  125. $this->assertSame(HttpStatus::OK, $this->save(['id' => $id, 'link_h5' => '/mobile'])['code']);
  126. $this->assertSame('/pc/promotion?id=5', ActivityReward::findOrFail($id)->link_pc);
  127. $this->assertSame('/mobile', ActivityReward::findOrFail($id)->link_h5);
  128. $before = DB::table('activity_rewards')->where('id', $id)->first();
  129. foreach (['link_pc', 'link_h5'] as $field) {
  130. foreach ([null, '', '//external.test', '/\\bad', 'https://example.com', ['bad'], '/' . str_repeat('x', 2048)] as $value) {
  131. $this->assertSame(HttpStatus::CUSTOM_ERROR, $this->save(['id' => $id, $field => $value])['code']);
  132. $this->assertEquals($before, DB::table('activity_rewards')->where('id', $id)->first());
  133. }
  134. }
  135. $this->assertSame(HttpStatus::OK, $this->save(['id' => $id, 'click_type' => 'external', 'link' => 'https://example.com'])['code']);
  136. $external = ActivityReward::findOrFail($id)->toArray();
  137. $this->assertSame('https://example.com', $external['link']);
  138. $this->assertNull($external['link_pc']); $this->assertNull($external['link_h5']);
  139. $this->assertSame(HttpStatus::OK, $this->save(['id' => $id, 'click_type' => 'none'])['code']);
  140. $plain = ActivityReward::findOrFail($id)->toArray();
  141. foreach (['link', 'link_pc', 'link_h5', 'click_position'] as $field) $this->assertNull($plain[$field]);
  142. $this->assertFalse($plain['can_jump']);
  143. }
  144. $this->assertSame(HttpStatus::CUSTOM_ERROR, $this->save(['click_type' => 'internal', 'link_pc' => '/pc', 'click_position' => 'cover'])['code']);
  145. }
  146. public function test_bot_member_list_resolves_the_dual_route_for_pc_h5_and_app(): void
  147. {
  148. $this->assertSame(HttpStatus::OK, $this->save(['type' => 1, 'click_type' => 'internal', 'link_pc' => '/pc/promo', 'link_h5' => '/h5/promo', 'click_position' => 'cover',
  149. 'start_time' => '2000-01-01', 'end_time' => '2099-12-31'])['code']);
  150. foreach (['PC' => '/pc/promo', 'H5' => '/h5/promo', 'APP' => '/h5/promo', 'h5' => '/h5/promo', '' => '/pc/promo'] as $platform => $target) {
  151. $request = Request::create('/api/ActivityReward', 'GET', ['platform' => $platform === 'PC' ? 'H5' : 'PC']);
  152. if ($platform !== '') $request->headers->set('Platform', $platform);
  153. $this->app->instance('request', $request);
  154. $body = (new \App\Http\Controllers\api\ActivityReward())->index()->getData(true);
  155. $this->assertSame(0, $body['code'], $body['msg']);
  156. $this->assertSame($target, $body['data'][0]['link']);
  157. $this->assertTrue($body['data'][0]['can_jump']);
  158. $this->assertArrayNotHasKey('link_pc', $body['data'][0]);
  159. $this->assertArrayNotHasKey('link_h5', $body['data'][0]);
  160. }
  161. }
  162. public function test_options_match_all_shared_game_categories_and_five_activity_types_on_an_authenticated_route(): void
  163. {
  164. $this->rawActivity(['type' => 1]);
  165. $before = DB::table('activity_rewards')->get()->toJson();
  166. $this->request('options');
  167. $body = (new AdminActivityReward())->options()->getData(true);
  168. $this->assertSame(HttpStatus::OK, $body['code'], $body['msg']);
  169. $financialCategories = ['third:1' => '真人', 'third:2' => '电子', 'third:3' => '彩票',
  170. 'third:4' => '体育', 'third:5' => '电竞', 'third:6' => '捕鱼', 'third:7' => '棋牌',
  171. 'self:sport' => 'F1体育', 'self:lottery' => 'F1彩票'];
  172. $this->assertSame($financialCategories, ShareDefaults::categories());
  173. $this->assertCount(9, ShareDefaults::categories());
  174. $this->assertArrayNotHasKey('other', ShareDefaults::categories());
  175. $this->assertArrayNotHasKey('hot', ShareDefaults::categories());
  176. $this->assertSame(['hot' => '热门'] + $financialCategories + ['other' => '其他'], ActivityCatalog::gameTypes());
  177. $expectedGames = [];
  178. foreach (['hot' => '热门'] + $financialCategories + ['other' => '其他'] as $value => $label) $expectedGames[] = compact('value', 'label');
  179. $this->assertCount(11, $expectedGames);
  180. $this->assertSame($expectedGames, $body['data']['game_types']);
  181. $this->assertNotContains('', array_column($body['data']['game_types'], 'value'));
  182. $this->assertNotContains('wallet', array_column($body['data']['game_types'], 'value'));
  183. $this->assertNotContains('all', array_column($body['data']['game_types'], 'value'));
  184. $this->assertSame([
  185. ['value' => 2, 'label' => '普通活动'],
  186. ['value' => 3, 'label' => '即充即送'],
  187. ['value' => 4, 'label' => '老用户召集令'],
  188. ['value' => 5, 'label' => 'USDT使用教学'],
  189. ['value' => 6, 'label' => '余额宝'],
  190. ], $body['data']['activity_types']);
  191. $this->assertNotContains(1, array_column($body['data']['activity_types'], 'value'));
  192. $router = new Router(new Dispatcher($this->app), $this->app);
  193. $this->app->instance('router', $router);
  194. $router->group(['prefix' => 'admin'], function () {
  195. require dirname(__DIR__, 2) . '/routes/admin.php';
  196. });
  197. $route = $router->getRoutes()->match(Request::create('/admin/ActivityReward/options', 'GET'));
  198. $this->assertSame(AdminActivityReward::class . '@options', $route->getAction('uses'));
  199. $this->assertContains('admin.jwt', $route->middleware());
  200. $this->assertContains('check.button.uri', $route->middleware());
  201. $this->assertSame($before, DB::table('activity_rewards')->get()->toJson());
  202. $this->assertSame(0, DB::table('activity_reward_recipients')->count());
  203. }
  204. public function test_removed_banners_are_not_required_saved_or_returned_and_images_remain(): void
  205. {
  206. $input = $this->form(); unset($input['detail_banner'], $input['pc_banner']);
  207. $this->request('submit', $input, 'POST');
  208. $body = (new AdminActivityReward())->store()->getData(true);
  209. $this->assertSame(HttpStatus::OK, $body['code']);
  210. $id = (int) DB::table('activity_rewards')->value('id');
  211. $this->assertSame('', DB::table('activity_rewards')->where('id', $id)->value('detail_banner'));
  212. $this->assertSame('', DB::table('activity_rewards')->where('id', $id)->value('pc_banner'));
  213. $this->assertSame('/app.png', DB::table('activity_rewards')->where('id', $id)->value('detail_image'));
  214. $this->assertSame('/pc.png', DB::table('activity_rewards')->where('id', $id)->value('pc_image'));
  215. DB::table('activity_rewards')->where('id', $id)->update(['detail_banner' => '/old.png', 'pc_banner' => '/old-pc.png']);
  216. $body = $this->save(['id' => $id, 'detail_banner' => ['ignored legacy input'], 'pc_banner' => ['ignored legacy input']]);
  217. $this->assertSame(HttpStatus::OK, $body['code']);
  218. $this->assertSame('/old.png', DB::table('activity_rewards')->where('id', $id)->value('detail_banner'));
  219. $this->assertSame('/old-pc.png', DB::table('activity_rewards')->where('id', $id)->value('pc_banner'));
  220. foreach (['detail_image', 'pc_image'] as $required) {
  221. $missing = $this->form(['id' => $id]); unset($missing[$required]);
  222. $this->request('submit', $missing, 'POST');
  223. $this->assertSame(HttpStatus::CUSTOM_ERROR, (new AdminActivityReward())->store()->getData(true)['code']);
  224. }
  225. foreach ([ActivityReward::findOrFail($id)->toArray(), $this->listing()['data'][0]] as $row) {
  226. $this->assertArrayNotHasKey('detail_banner', $row); $this->assertArrayNotHasKey('pc_banner', $row);
  227. $this->assertArrayHasKey('detail_image', $row); $this->assertArrayHasKey('pc_image', $row);
  228. }
  229. }
  230. public function test_activity_click_behavior_saves_preserves_and_clears_for_normal_and_type_two_activities(): void
  231. {
  232. $this->request('options');
  233. $this->assertSame(\App\Support\AnnouncementCatalog::options(\App\Support\AnnouncementCatalog::CLICKS),
  234. (new AdminActivityReward())->options()->getData(true)['data']['click_types']);
  235. foreach ([2, 3] as $type) {
  236. $body = $this->save(['type' => $type]);
  237. $this->assertSame(HttpStatus::OK, $body['code']);
  238. $id = (int) DB::table('activity_rewards')->latest('id')->value('id');
  239. $this->assertSame('none', DB::table('activity_rewards')->where('id', $id)->value('click_type'));
  240. foreach (['external' => 'https://example.com/promotion?from=activity', 'internal' => '/promotion/detail?id=3'] as $mode => $link) {
  241. $body = $this->save(['id' => $id, 'type' => $type, 'click_type' => $mode, 'link' => $link, 'click_position' => $mode === 'external' ? 'cover' : 'detail']);
  242. $this->assertSame(HttpStatus::OK, $body['code'], $body['msg']);
  243. $this->assertSame(HttpStatus::OK, $this->save(['id' => $id, 'type' => $type, 'title' => '只改标题'])['code']);
  244. $row = collect($this->listing()['data'])->keyBy('id')->get($id);
  245. $this->assertSame($mode, $row['click_type']); $this->assertSame($link, $row['link']); $this->assertTrue($row['can_jump']);
  246. $this->assertSame($mode === 'external' ? 'cover' : 'detail', $row['click_position']);
  247. }
  248. $this->assertSame(HttpStatus::OK, $this->save(['id' => $id, 'type' => $type, 'click_type' => 'none', 'link' => ['unused'], 'click_position' => ['unused']])['code']);
  249. $row = ActivityReward::findOrFail($id)->toArray();
  250. $this->assertSame('none', $row['click_type']); $this->assertNull($row['link']); $this->assertFalse($row['can_jump']);
  251. $this->assertNull($row['click_position']);
  252. $this->assertNull(DB::table('activity_rewards')->where('id', $id)->value('click_position'));
  253. $this->assertNull(DB::table('activity_rewards')->where('id', $id)->value('link'));
  254. }
  255. }
  256. public function test_nonempty_links_require_a_trigger_position_and_can_switch_position_independently(): void
  257. {
  258. $this->request('options');
  259. $this->assertSame([['value' => 'cover', 'label' => '活动列表封面'], ['value' => 'detail', 'label' => '活动详情按钮']],
  260. (new AdminActivityReward())->options()->getData(true)['data']['click_positions']);
  261. foreach (['external' => 'https://example.com', 'internal' => '/detail'] as $mode => $link) {
  262. foreach ([null, '', 'invalid', ['cover']] as $position) {
  263. $this->assertSame(HttpStatus::CUSTOM_ERROR, $this->save(['click_type' => $mode, 'link' => $link, 'click_position' => $position])['code']);
  264. }
  265. $this->assertSame(HttpStatus::CUSTOM_ERROR, $this->save(['click_type' => $mode, 'link' => $link])['code']);
  266. $this->assertSame(0, DB::table('activity_rewards')->count());
  267. }
  268. $this->save(['click_type' => 'internal', 'link' => '/detail', 'click_position' => 'cover']);
  269. $id = (int) DB::table('activity_rewards')->value('id');
  270. $this->assertSame(HttpStatus::OK, $this->save(['id' => $id, 'click_position' => 'detail'])['code']);
  271. $row = ActivityReward::findOrFail($id)->toArray();
  272. $this->assertSame('detail', $row['click_position']); $this->assertSame('/detail', $row['link']);
  273. $this->assertSame('internal', $row['click_type']); $this->assertTrue($row['can_jump']);
  274. }
  275. public function test_trigger_position_migration_keeps_old_links_on_the_cover(): void
  276. {
  277. $migration = require dirname(__DIR__, 2) . '/database/migrations/2026_10_28_130000_add_activity_click_position.php';
  278. $migration->down();
  279. $linked = $this->rawActivity(['click_type' => 'external', 'link' => 'https://example.com']);
  280. $plain = $this->rawActivity();
  281. $before = DB::table('activity_rewards')->orderBy('id')->get();
  282. $migration->up();
  283. $this->assertSame('cover', DB::table('activity_rewards')->where('id', $linked)->value('click_position'));
  284. $this->assertNull(DB::table('activity_rewards')->where('id', $plain)->value('click_position'));
  285. foreach ($before as $old) {
  286. $new = DB::table('activity_rewards')->where('id', $old->id)->first();
  287. foreach ((array) $old as $field => $value) $this->assertSame($value, $new->$field);
  288. }
  289. }
  290. public function test_invalid_activity_links_do_not_modify_the_activity(): void
  291. {
  292. $this->save(['click_type' => 'internal', 'link' => '/original', 'click_position' => 'cover']);
  293. $id = (int) DB::table('activity_rewards')->value('id');
  294. $before = DB::table('activity_rewards')->where('id', $id)->first();
  295. foreach ([['click_type' => 'activity'], ['click_type' => null], ['click_type' => ['none']],
  296. ['click_type' => 'external', 'link' => null], ['click_type' => 'external', 'link' => 'javascript:alert(1)'],
  297. ['click_type' => 'external', 'link' => 'https://user:pass@example.com'], ['click_type' => 'external', 'link' => '/relative'],
  298. ['click_type' => 'internal', 'link' => '//example.com'], ['click_type' => 'internal', 'link' => '/\\evil'],
  299. ['click_type' => 'internal', 'link' => 'https://example.com'], ['click_type' => 'external', 'link' => ['bad']]] as $values) {
  300. $body = $this->save(['id' => $id] + $values + ['click_position' => 'cover']);
  301. $this->assertSame(HttpStatus::CUSTOM_ERROR, $body['code']);
  302. $this->assertEquals($before, DB::table('activity_rewards')->where('id', $id)->first());
  303. }
  304. }
  305. public function test_hot_category_is_saved_filtered_and_preserved_when_editing_other_fields(): void
  306. {
  307. $this->assertSame(HttpStatus::OK, $this->save(['game_category' => 'hot'])['code']);
  308. $id = (int) DB::table('activity_rewards')->value('id');
  309. $this->assertSame(HttpStatus::OK, $this->save(['id' => $id, 'title' => '热门活动'])['code']);
  310. $row = $this->listing(['game_category' => 'hot']);
  311. $this->assertSame(1, $row['total']);
  312. $this->assertSame('hot', $row['data'][0]['game_category']);
  313. $this->assertSame('热门', $row['data'][0]['game_category_name']);
  314. $this->assertSame(0, $this->listing(['game_category' => 'other'])['total']);
  315. }
  316. public function test_new_omission_is_null_edit_omission_preserves_category_and_explicit_values_clear_it(): void
  317. {
  318. $result = $this->save();
  319. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  320. $id = DB::table('activity_rewards')->value('id');
  321. $this->assertNull(DB::table('activity_rewards')->where('id', $id)->value('game_category'));
  322. $this->assertNull(ActivityReward::findOrFail($id)->toArray()['game_category_name']);
  323. $result = $this->save(['id' => $id, 'game_category' => 'third:1', 'sort' => 37, 'publish_platforms' => ['PC']]);
  324. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  325. $result = $this->save(['id' => $id, 'title' => '只修改标题']);
  326. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  327. $row = ActivityReward::findOrFail($id)->toArray();
  328. $this->assertSame('third:1', $row['game_category']);
  329. $this->assertSame('真人', $row['game_category_name']);
  330. $this->assertSame('即充即送', $row['type_name']);
  331. $this->assertSame(37, $row['sort']);
  332. $this->assertSame(['PC'], $row['publish_platforms']);
  333. foreach ([null, ''] as $clear) {
  334. $this->assertSame(HttpStatus::OK, $this->save(['id' => $id, 'game_category' => 'self:sport'])['code']);
  335. $result = $this->save(['id' => $id, 'game_category' => $clear]);
  336. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  337. $this->assertNull(DB::table('activity_rewards')->where('id', $id)->value('game_category'));
  338. $row = collect($this->listing()['data'])->keyBy('id')->get($id);
  339. $this->assertNull($row['game_category']);
  340. $this->assertNull($row['game_category_name']);
  341. }
  342. $this->assertSame(1, DB::table('activity_rewards')->count());
  343. $this->assertSame(0, DB::table('activity_reward_recipients')->count());
  344. }
  345. public function test_every_game_category_is_saved_and_named_for_normal_and_type_two_activities(): void
  346. {
  347. foreach ([2 => '普通活动', 3 => '即充即送'] as $type => $typeName) {
  348. foreach (ActivityCatalog::gameTypes() as $category => $label) {
  349. $result = $this->save(['type' => $type, 'game_category' => $category]);
  350. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  351. $row = ActivityReward::query()->latest('id')->firstOrFail()->toArray();
  352. $this->assertSame($type, (int) $row['type']);
  353. $this->assertSame($typeName, $row['type_name']);
  354. $this->assertSame($category, $row['game_category']);
  355. $this->assertSame($label, $row['game_category_name']);
  356. }
  357. }
  358. $this->assertSame(22, $this->listing()['total']);
  359. }
  360. public function test_other_category_is_preserved_on_omitted_edit_and_can_be_cleared_for_type_two_and_three(): void
  361. {
  362. foreach ([2, 3] as $type) {
  363. $result = $this->save(['type' => $type, 'game_category' => 'other']);
  364. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  365. $id = DB::table('activity_rewards')->latest('id')->value('id');
  366. $this->assertSame('other', DB::table('activity_rewards')->where('id', $id)->value('game_category'));
  367. $result = $this->save(['id' => $id, 'type' => $type, 'title' => '编辑其他分类活动']);
  368. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  369. $page = $this->listing(['game_category' => 'other', 'type' => (string) $type]);
  370. $this->assertSame(1, $page['total']);
  371. $this->assertSame($id, $page['data'][0]['id']);
  372. $this->assertSame('other', $page['data'][0]['game_category']);
  373. $this->assertSame('其他', $page['data'][0]['game_category_name']);
  374. $this->assertSame('其他', ActivityReward::findOrFail($id)->toArray()['game_category_name']);
  375. foreach ([null, ''] as $clear) {
  376. $this->assertSame(HttpStatus::OK, $this->save(['id' => $id, 'type' => $type, 'game_category' => 'other'])['code']);
  377. $result = $this->save(['id' => $id, 'type' => $type, 'game_category' => $clear]);
  378. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  379. $this->assertNull(DB::table('activity_rewards')->where('id', $id)->value('game_category'));
  380. $row = collect($this->listing()['data'])->keyBy('id')->get($id);
  381. $this->assertNull($row['game_category']);
  382. $this->assertNull($row['game_category_name']);
  383. $this->assertSame(0, $this->listing(['game_category' => 'other', 'type' => (string) $type])['total']);
  384. }
  385. }
  386. $this->assertSame(2, DB::table('activity_rewards')->count());
  387. $this->assertSame(0, DB::table('activity_reward_recipients')->count());
  388. }
  389. public function test_invalid_categories_are_rejected_by_controller_and_service_even_for_type_two(): void
  390. {
  391. $id = $this->rawActivity(['game_category' => 'third:1']);
  392. $before = DB::table('activity_rewards')->get()->toJson();
  393. foreach (['unknown', 'third:8', 'wallet', 'all', '其他', '1', 1, ['third:1']] as $category) {
  394. foreach ([2, 3] as $type) {
  395. $result = $this->save(['id' => $id, 'type' => $type, 'title' => '不应保存', 'game_category' => $category]);
  396. $this->assertSame(HttpStatus::CUSTOM_ERROR, $result['code']);
  397. try {
  398. ActivityRewardService::submit($this->form(['id' => $id, 'type' => $type, 'game_category' => $category]));
  399. $this->fail('Expected direct service category validation');
  400. } catch (ValidationException $e) {
  401. $this->assertArrayHasKey('game_category', $e->errors());
  402. }
  403. }
  404. $this->request('', ['page' => 1, 'limit' => 10, 'game_category' => $category]);
  405. $result = (new AdminActivityReward())->index()->getData(true);
  406. $this->assertSame(HttpStatus::CUSTOM_ERROR, $result['code']);
  407. }
  408. $this->assertSame($before, DB::table('activity_rewards')->get()->toJson());
  409. }
  410. public function test_combined_category_filters_apply_before_count_and_pagination_with_type_two_platform_exception(): void
  411. {
  412. $base = ['title' => '目标活动', 'type' => 3, 'status' => 0, 'lang' => 'zh', 'game_category' => 'third:1', 'publish_platforms' => ['PC']];
  413. $matching = [];
  414. for ($i = 1; $i <= 5; $i++) $matching[] = $this->rawActivity(['sort' => $i] + $base);
  415. foreach ([['game_category' => 'third:2'], ['publish_platforms' => ['H5']], ['status' => 1],
  416. ['lang' => 'en'], ['title' => '其他活动'], ['type' => 4], ['game_category' => null]] as $extra) {
  417. $this->rawActivity($extra + ['sort' => 100] + $base);
  418. }
  419. $ordinary = $this->rawActivity(['type' => 2, 'publish_platforms' => ['H5'], 'sort' => 101] + $base);
  420. $before = DB::table('activity_rewards')->orderBy('id')->get()->toJson();
  421. $matching = array_reverse($matching);
  422. $filters = ['title' => '目标', 'type' => '3', 'status' => '0', 'lang' => 'zh', 'platform' => 'pc', 'game_category' => 'third:1', 'limit' => 2];
  423. $seen = [];
  424. foreach ([1, 2, 3, 4] as $page) {
  425. $result = $this->listing($filters + ['page' => $page]);
  426. $this->assertSame(5, $result['total']);
  427. $this->assertSame(array_slice($matching, ($page - 1) * 2, 2), array_column($result['data'], 'id'));
  428. foreach ($result['data'] as $row) {
  429. $this->assertSame('third:1', $row['game_category']);
  430. $this->assertSame('真人', $row['game_category_name']);
  431. $this->assertSame('即充即送', $row['type_name']);
  432. }
  433. $seen = array_merge($seen, array_column($result['data'], 'id'));
  434. }
  435. $this->assertSame($matching, $seen);
  436. $ordinaryPage = $this->listing(['type' => '2', 'platform' => 'pc', 'status' => '0', 'lang' => 'zh', 'game_category' => 'third:1']);
  437. $this->assertSame(1, $ordinaryPage['total']);
  438. $this->assertSame([$ordinary], array_column($ordinaryPage['data'], 'id'));
  439. $this->assertSame('普通活动', $ordinaryPage['data'][0]['type_name']);
  440. $all = $this->listing();
  441. foreach ([null, ''] as $category) {
  442. $result = $this->listing(['game_category' => $category]);
  443. $this->assertSame($all['total'], $result['total']);
  444. $this->assertSame(array_column($all['data'], 'id'), array_column($result['data'], 'id'));
  445. }
  446. $this->assertSame($before, DB::table('activity_rewards')->orderBy('id')->get()->toJson());
  447. }
  448. public function test_other_category_filter_combines_with_existing_filters_before_count_and_pagination(): void
  449. {
  450. $base = ['title' => '目标其他活动', 'type' => 3, 'status' => 0, 'lang' => 'zh', 'game_category' => 'other', 'publish_platforms' => ['H5']];
  451. $matching = [];
  452. for ($i = 1; $i <= 5; $i++) $matching[] = $this->rawActivity(['sort' => $i] + $base);
  453. foreach ([['game_category' => 'third:1'], ['game_category' => null], ['publish_platforms' => ['PC']],
  454. ['status' => 1], ['lang' => 'en'], ['title' => '不同标题'], ['type' => 4]] as $extra) {
  455. $this->rawActivity($extra + ['sort' => 100] + $base);
  456. }
  457. $before = DB::table('activity_rewards')->orderBy('id')->get()->toJson();
  458. $matching = array_reverse($matching);
  459. $filters = ['title' => '目标', 'type' => '3', 'status' => '0', 'lang' => 'zh', 'platform' => 'h5', 'game_category' => 'other', 'limit' => 2];
  460. $seen = [];
  461. foreach ([1, 2, 3, 4] as $page) {
  462. $result = $this->listing($filters + ['page' => $page]);
  463. $this->assertSame(5, $result['total']);
  464. $this->assertSame(array_slice($matching, ($page - 1) * 2, 2), array_column($result['data'], 'id'));
  465. foreach ($result['data'] as $row) {
  466. $this->assertSame('other', $row['game_category']);
  467. $this->assertSame('其他', $row['game_category_name']);
  468. }
  469. $seen = array_merge($seen, array_column($result['data'], 'id'));
  470. }
  471. $this->assertSame($matching, $seen);
  472. $all = $this->listing();
  473. $this->assertSame(12, $all['total']);
  474. foreach ([null, ''] as $category) {
  475. $page = $this->listing(['game_category' => $category]);
  476. $this->assertSame($all['total'], $page['total']);
  477. $this->assertSame(array_column($all['data'], 'id'), array_column($page['data'], 'id'));
  478. }
  479. $defaultRows = array_filter($all['data'], fn ($row) => $row['game_category'] === null);
  480. $this->assertCount(1, $defaultRows);
  481. $this->assertNull(array_values($defaultRows)[0]['game_category_name']);
  482. $this->assertSame($before, DB::table('activity_rewards')->orderBy('id')->get()->toJson());
  483. }
  484. public function test_legacy_type_one_remains_readable_and_editable_without_becoming_an_option(): void
  485. {
  486. $result = $this->save(['type' => 1]);
  487. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  488. $id = DB::table('activity_rewards')->value('id');
  489. DB::table('activity_rewards')->where('id', $id)->update(['budget_used' => '12.34', 'rewarded_count' => 2]);
  490. $result = $this->save(['id' => $id, 'type' => 1, 'title' => '旧类型活动', 'game_category' => 'self:lottery']);
  491. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  492. $row = $this->listing(['type' => '1'])['data'][0];
  493. $this->assertSame(1, (int) $row['type']);
  494. $this->assertNull($row['type_name']);
  495. $this->assertSame('self:lottery', $row['game_category']);
  496. $this->assertSame('F1彩票', $row['game_category_name']);
  497. $this->assertSame('12.34', $row['budget_used']);
  498. $this->assertSame(2, $row['rewarded_count']);
  499. $legacy = $this->rawActivity(['type' => 99, 'game_category' => null]);
  500. $this->assertNull(ActivityReward::findOrFail($legacy)->toArray()['type_name']);
  501. $this->request('options');
  502. $options = (new AdminActivityReward())->options()->getData(true)['data']['activity_types'];
  503. $this->assertSame([2, 3, 4, 5, 6], array_column($options, 'value'));
  504. }
  505. public function test_category_migration_adds_nullable_index_without_changing_existing_rows_and_rolls_back(): void
  506. {
  507. $this->migration()->down();
  508. $this->assertFalse(Schema::hasColumn('activity_rewards', 'game_category'));
  509. $id = $this->rawActivity(['type' => 1, 'sort' => 19]);
  510. $before = (array) DB::table('activity_rewards')->find($id);
  511. $this->migration()->up();
  512. $after = (array) DB::table('activity_rewards')->find($id);
  513. $this->assertNull($after['game_category']);
  514. unset($after['game_category']);
  515. $this->assertSame($before, $after);
  516. $column = collect(DB::select('PRAGMA table_info(bot_activity_rewards)'))->firstWhere('name', 'game_category');
  517. $this->assertSame(0, (int) $column->notnull);
  518. $indexes = DB::select("SELECT sql FROM sqlite_master WHERE type = 'index' AND tbl_name = ?", ['bot_activity_rewards']);
  519. $this->assertNotEmpty(array_filter($indexes, fn ($index) => str_contains((string) $index->sql, 'game_category')));
  520. $newId = $this->rawActivity();
  521. $this->assertNull(DB::table('activity_rewards')->where('id', $newId)->value('game_category'));
  522. $this->migration()->down();
  523. $this->assertFalse(Schema::hasColumn('activity_rewards', 'game_category'));
  524. $this->assertTrue(Schema::hasColumn('activity_rewards', 'reward_quota'));
  525. $this->assertSame($before, (array) DB::table('activity_rewards')->find($id));
  526. $this->assertSame(2, DB::table('activity_rewards')->count());
  527. }
  528. }