BannerPublicationTest.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. <?php
  2. namespace Tests\Integration;
  3. use App\Constants\HttpStatus;
  4. use App\Http\Controllers\admin\Banner as AdminBanner;
  5. use App\Http\Middleware\CheckButtonPermission;
  6. use App\Models\Banner;
  7. use App\Services\BannerVisibilityService;
  8. use Illuminate\Config\Repository;
  9. use Illuminate\Database\Capsule\Manager;
  10. use Illuminate\Database\Schema\Blueprint;
  11. use Illuminate\Events\Dispatcher;
  12. use Illuminate\Foundation\Application;
  13. use Illuminate\Http\JsonResponse;
  14. use Illuminate\Http\Request;
  15. use Illuminate\Routing\Router;
  16. use Illuminate\Support\Facades\DB;
  17. use Illuminate\Support\Facades\Facade;
  18. use Illuminate\Support\Facades\Schema;
  19. use Illuminate\Translation\ArrayLoader;
  20. use Illuminate\Translation\Translator;
  21. use Illuminate\Validation\Factory;
  22. use PHPUnit\Framework\TestCase;
  23. /** Exercises real banner controllers and queries against an isolated, prefixed SQLite database. */
  24. class BannerPublicationTest extends TestCase
  25. {
  26. private Application $app;
  27. protected function setUp(): void
  28. {
  29. Facade::clearResolvedInstances();
  30. $this->app = new Application(dirname(__DIR__, 2));
  31. $this->app->instance('config', new Repository([
  32. 'app' => ['locale' => 'zh', 'url' => 'https://banner.example.test', 'timezone' => 'Asia/Shanghai'],
  33. ]));
  34. Facade::setFacadeApplication($this->app);
  35. $db = new Manager($this->app);
  36. $db->addConnection(['driver' => 'sqlite', 'database' => ':memory:', 'prefix' => 'bot_']);
  37. $db->setAsGlobal();
  38. $db->bootEloquent();
  39. $this->app->instance('db', $db->getDatabaseManager());
  40. $this->app->bind('db.schema', fn () => $db->schema());
  41. $translator = new Translator(new ArrayLoader(), 'zh');
  42. $this->app->instance('translator', $translator);
  43. $this->app->instance('validator', new Factory($translator, $this->app));
  44. $this->app->instance(\Illuminate\Contracts\Routing\ResponseFactory::class, new \Tests\Support\JsonResponseFactory());
  45. Request::macro('validate', function (array $rules) {
  46. return app('validator')->make($this->all(), $rules)->validate();
  47. });
  48. $db->schema()->create('banner', function (Blueprint $t) {
  49. $t->id(); $t->string('title'); $t->string('image'); $t->string('link')->nullable();
  50. $t->string('type'); $t->integer('sort')->default(0); $t->string('lang'); $t->timestamps();
  51. });
  52. $this->migration()->up();
  53. $db->schema()->create('activity_rewards', function (Blueprint $t) {
  54. $t->id(); $t->integer('status')->default(1);
  55. $t->integer('start_time'); $t->integer('end_time');
  56. });
  57. $this->request('index');
  58. }
  59. protected function tearDown(): void
  60. {
  61. DB::disconnect();
  62. Facade::clearResolvedInstances();
  63. parent::tearDown();
  64. }
  65. private function migration()
  66. {
  67. return require dirname(__DIR__, 2) . '/database/migrations/2026_10_24_140000_add_status_to_banner.php';
  68. }
  69. private function request(string $action, array $params = []): void
  70. {
  71. $this->app->instance('request', Request::create('/admin/banner/' . $action,
  72. $action === 'index' ? 'GET' : 'POST', $params));
  73. }
  74. private function form(array $overrides = []): array
  75. {
  76. return $overrides + [
  77. 'title' => '轮播图', 'image' => '/banner.png', 'type' => 'pc', 'lang' => 'zh',
  78. ];
  79. }
  80. private function save(array $params = []): array
  81. {
  82. $this->request('update', $this->form($params));
  83. return (new AdminBanner())->update()->getData(true);
  84. }
  85. private function setStatus(array $params): array
  86. {
  87. $this->request('status', $params);
  88. return (new AdminBanner())->status()->getData(true);
  89. }
  90. private function listing(array $params = []): array
  91. {
  92. $this->request('index', $params);
  93. $result = (new AdminBanner())->index()->getData(true);
  94. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  95. return $result['data'];
  96. }
  97. private function banner(array $params = []): int
  98. {
  99. return DB::table('banner')->insertGetId($params + $this->form() + ['link' => null, 'sort' => 0, 'status' => 1]);
  100. }
  101. private function activity(int $id, int $start, int $end, int $status = 1): void
  102. {
  103. DB::table('activity_rewards')->insert([
  104. 'id' => $id, 'status' => $status, 'start_time' => $start, 'end_time' => $end,
  105. ]);
  106. }
  107. private function decorated(int $now): array
  108. {
  109. return collect(BannerVisibilityService::decorate(Banner::query()->orderBy('id')->get(), $now))->keyBy('id')->all();
  110. }
  111. private function assertState(array $row, ?int $manual, int $effective, ?string $reason, ?string $activityId): void
  112. {
  113. $this->assertSame($manual, $row['manual_status']);
  114. $this->assertSame($effective, $row['status']);
  115. $this->assertSame($effective, $row['effective_status']);
  116. $this->assertSame($reason, $row['offline_reason']);
  117. $this->assertSame($activityId, $row['activity_id']);
  118. $this->assertSame($activityId === null, $row['can_manual_toggle']);
  119. }
  120. public function test_migration_publishes_existing_banners_by_default_and_rolls_back_only_its_column(): void
  121. {
  122. $this->migration()->down();
  123. $this->assertFalse(Schema::hasColumn('banner', 'status'));
  124. $id = DB::table('banner')->insertGetId($this->form(['title' => '历史轮播图', 'sort' => 23]) + ['link' => 'https://example.test']);
  125. $this->migration()->up();
  126. $row = DB::table('banner')->find($id);
  127. $this->assertSame(1, (int) $row->status);
  128. $this->assertSame('历史轮播图', $row->title);
  129. $this->assertSame(23, (int) $row->sort);
  130. $this->assertSame('https://example.test', $row->link);
  131. $newId = DB::table('banner')->insertGetId($this->form());
  132. $this->assertSame(1, (int) DB::table('banner')->where('id', $newId)->value('status'));
  133. $this->migration()->down();
  134. $this->assertFalse(Schema::hasColumn('banner', 'status'));
  135. $this->assertSame(2, DB::table('banner')->count());
  136. $this->assertSame('历史轮播图', DB::table('banner')->where('id', $id)->value('title'));
  137. }
  138. public function test_create_defaults_to_manual_on_and_legacy_edit_preserves_omitted_status_link_and_sort(): void
  139. {
  140. $result = $this->save(['sort' => 27, 'link' => 'https://example.test/promotion', 'type' => 'app', 'lang' => 'en']);
  141. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  142. $banner = Banner::query()->firstOrFail();
  143. $this->assertSame(1, $banner->status);
  144. $this->assertSame('app', $banner->type);
  145. $this->assertSame('en', $banner->lang);
  146. $this->assertSame(27, (int) $banner->sort);
  147. $this->assertSame(HttpStatus::OK, $this->setStatus(['id' => $banner->id, 'status' => 0])['code']);
  148. $result = $this->save(['id' => $banner->id, 'title' => '编辑标题', 'type' => 'app', 'lang' => 'en']);
  149. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  150. $banner->refresh();
  151. $this->assertSame('编辑标题', $banner->title);
  152. $this->assertSame(0, $banner->status);
  153. $this->assertSame('https://example.test/promotion', $banner->link);
  154. $this->assertSame(27, (int) $banner->sort);
  155. $this->assertSame('app', $banner->type);
  156. $this->assertSame('en', $banner->lang);
  157. $this->assertSame('/banner.png', $banner->getRawOriginal('image'));
  158. $this->assertSame('https://banner.example.test/banner.png', $banner->image);
  159. }
  160. public function test_status_endpoint_accepts_both_aliases_and_repeated_values_without_changing_banner_content(): void
  161. {
  162. $id = $this->banner(['sort' => 19, 'lang' => 'en', 'link' => 'https://example.test']);
  163. foreach (['status', 'manual_status'] as $field) {
  164. foreach ([0, 0, 1, 1, '0', '1'] as $status) {
  165. $result = $this->setStatus(['id' => $id, $field => $status]);
  166. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  167. $row = DB::table('banner')->find($id);
  168. $this->assertSame((int) $status, (int) $row->status);
  169. $this->assertSame('https://example.test', $row->link);
  170. $this->assertSame(19, (int) $row->sort);
  171. $this->assertSame('en', $row->lang);
  172. }
  173. }
  174. }
  175. public function test_status_takes_precedence_with_manual_status_as_fallback_in_forms_and_status_requests(): void
  176. {
  177. foreach ([
  178. [['manual_status' => 0], 0],
  179. [['status' => 1], 1],
  180. [['manual_status' => null], 1],
  181. [['status' => 1, 'manual_status' => null], 1],
  182. [['status' => 0, 'manual_status' => 1], 0],
  183. [['status' => 1, 'manual_status' => 0], 1],
  184. ] as [$fields, $expected]) {
  185. $result = $this->save($fields);
  186. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  187. $id = DB::table('banner')->latest('id')->value('id');
  188. $this->assertSame($expected, (int) DB::table('banner')->where('id', $id)->value('status'));
  189. }
  190. foreach ([[0, 1], [1, 0]] as [$canonical, $manual]) {
  191. $result = $this->setStatus(['id' => $id, 'status' => $canonical, 'manual_status' => $manual]);
  192. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  193. $this->assertSame($canonical, (int) DB::table('banner')->where('id', $id)->value('status'));
  194. $result = $this->save(['id' => $id, 'title' => '兼容完整编辑', 'status' => $canonical, 'manual_status' => $manual]);
  195. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  196. $this->assertSame($canonical, (int) DB::table('banner')->where('id', $id)->value('status'));
  197. }
  198. }
  199. public function test_unbound_list_row_edits_and_buttons_follow_changed_status_over_stale_manual_alias(): void
  200. {
  201. $ids = [];
  202. foreach ([null, '', '0', 'https://example.test/promotion'] as $link) {
  203. $ids[] = $this->banner(['link' => $link, 'status' => 1]);
  204. }
  205. $orderedIds = array_reverse($ids);
  206. $assertPublication = function (int $id, int $expected) use ($orderedIds): array {
  207. $rows = collect($this->listing()['data'])->keyBy('id')->all();
  208. $this->assertState($rows[$id], $expected, $expected, $expected ? null : 'manual', null);
  209. $this->assertSame($expected, (int) DB::table('banner')->where('id', $id)->value('status'));
  210. $downIds = $expected ? [] : [$id];
  211. $upIds = array_values(array_diff($orderedIds, $downIds));
  212. foreach ([['0', $downIds], ['1', $upIds]] as [$status, $expectedIds]) {
  213. $page = $this->listing(['status' => $status]);
  214. $this->assertSame(count($expectedIds), $page['total']);
  215. $this->assertSame($expectedIds, array_column($page['data'], 'id'));
  216. }
  217. return $rows[$id];
  218. };
  219. foreach ($ids as $id) {
  220. $row = $assertPublication($id, 1);
  221. $originalLink = $row['link'];
  222. $row['status'] = 0;
  223. $row['title'] = '列表编辑下架';
  224. $this->assertSame(1, $row['manual_status']);
  225. $result = $this->save($row);
  226. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  227. $row = $assertPublication($id, 0);
  228. $result = $this->setStatus(['id' => $id, 'status' => 1, 'manual_status' => $row['manual_status']]);
  229. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  230. $row = $assertPublication($id, 1);
  231. $result = $this->setStatus(['id' => $id, 'status' => 0, 'manual_status' => $row['manual_status']]);
  232. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  233. $row = $assertPublication($id, 0);
  234. $row['status'] = 1;
  235. $row['title'] = '列表编辑上架';
  236. $this->assertSame(0, $row['manual_status']);
  237. $result = $this->save($row);
  238. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  239. $row = $assertPublication($id, 1);
  240. $this->assertSame('列表编辑上架', $row['title']);
  241. $this->assertSame($originalLink, $row['link']);
  242. }
  243. }
  244. public function test_create_accepts_manual_off_and_invalid_publication_values_leave_rows_unchanged(): void
  245. {
  246. $result = $this->save(['status' => 0]);
  247. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  248. $id = DB::table('banner')->value('id');
  249. $this->assertSame(0, (int) DB::table('banner')->where('id', $id)->value('status'));
  250. foreach (['status', 'manual_status'] as $field) {
  251. foreach ($field === 'status' ? [null, -1, 2, 'invalid'] : [-1, 2, 'invalid'] as $status) {
  252. $result = $this->save(['id' => $id, 'title' => '不应保存', $field => $status]);
  253. $this->assertSame(HttpStatus::VALIDATION_FAILED, $result['code']);
  254. $row = DB::table('banner')->find($id);
  255. $this->assertSame(0, (int) $row->status);
  256. $this->assertSame('轮播图', $row->title);
  257. }
  258. }
  259. foreach ([['status' => 1, 'manual_status' => 'invalid'], ['status' => 'invalid', 'manual_status' => 1]] as $fields) {
  260. $result = $this->save($fields + ['id' => $id, 'title' => '不应保存']);
  261. $this->assertSame(HttpStatus::VALIDATION_FAILED, $result['code']);
  262. $result = $this->setStatus($fields + ['id' => $id]);
  263. $this->assertSame(HttpStatus::VALIDATION_FAILED, $result['code']);
  264. $this->assertSame(0, (int) DB::table('banner')->where('id', $id)->value('status'));
  265. $this->assertSame('轮播图', DB::table('banner')->where('id', $id)->value('title'));
  266. }
  267. $result = $this->save(['id' => $id, 'manual_status' => null, 'title' => '空值保持手动设置']);
  268. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  269. $this->assertSame(0, (int) DB::table('banner')->where('id', $id)->value('status'));
  270. $this->assertSame(1, DB::table('banner')->count());
  271. }
  272. public function test_status_endpoint_rejects_missing_invalid_status_and_missing_banner(): void
  273. {
  274. $id = $this->banner();
  275. foreach ([[], ['id' => $id], ['status' => 0], ['id' => $id, 'status' => -1],
  276. ['id' => $id, 'status' => 2], ['id' => $id, 'status' => 'invalid'],
  277. ['id' => $id, 'status' => null], ['id' => 0, 'status' => 0],
  278. ['id' => 'invalid', 'status' => 0], ['manual_status' => 0],
  279. ['id' => $id, 'manual_status' => null], ['id' => $id, 'manual_status' => -1],
  280. ['id' => $id, 'manual_status' => 2], ['id' => $id, 'manual_status' => 'invalid']] as $params) {
  281. $this->assertSame(HttpStatus::VALIDATION_FAILED, $this->setStatus($params)['code']);
  282. $this->assertSame(1, (int) DB::table('banner')->where('id', $id)->value('status'));
  283. }
  284. $result = $this->setStatus(['id' => 999999, 'status' => 0]);
  285. $this->assertSame(HttpStatus::CUSTOM_ERROR, $result['code']);
  286. $this->assertStringContainsString('数据不存在', $result['msg']);
  287. $this->assertSame(1, DB::table('banner')->count());
  288. }
  289. public function test_bound_visibility_uses_raw_seconds_and_includes_start_but_excludes_end(): void
  290. {
  291. $now = strtotime('2026-10-24 12:30:00 UTC');
  292. $expected = [];
  293. foreach ([
  294. 1 => [$now - 1, $now + 1, 1, null],
  295. 2 => [$now + 1, $now + 120, 1, 'activity_not_started'],
  296. 3 => [$now - 120, $now - 1, 1, 'activity_ended'],
  297. 4 => [$now, $now + 1, 1, null],
  298. 5 => [$now - 1, $now, 1, 'activity_ended'],
  299. 6 => [$now - 1, $now + 1, 0, 'activity_offline'],
  300. ] as $activityId => [$start, $end, $status, $reason]) {
  301. $this->activity($activityId, $start, $end, $status);
  302. $id = $this->banner(['link' => (string) $activityId]);
  303. $expected[$id] = [$reason, (string) $activityId];
  304. }
  305. $id = $this->banner(['link' => '999']);
  306. $expected[$id] = ['activity_missing', '999'];
  307. $rows = $this->decorated($now);
  308. foreach ($expected as $id => [$reason, $activityId]) {
  309. $this->assertState($rows[$id], null, $reason === null ? 1 : 0, $reason, $activityId);
  310. $this->assertSame(1, (int) DB::table('banner')->where('id', $id)->value('status'));
  311. }
  312. }
  313. public function test_null_empty_zero_and_url_links_follow_only_the_manual_setting(): void
  314. {
  315. $expected = [];
  316. foreach ([null, '', '0', '000', 'https://example.test/activity/7'] as $link) {
  317. foreach ([0, 1] as $manual) {
  318. $id = $this->banner(['link' => $link, 'status' => $manual]);
  319. $expected[$id] = $manual;
  320. }
  321. }
  322. DB::connection()->enableQueryLog();
  323. $rows = $this->decorated(1000);
  324. $queries = DB::connection()->getQueryLog();
  325. DB::connection()->disableQueryLog();
  326. foreach ($expected as $id => $manual) {
  327. $this->assertState($rows[$id], $manual, $manual, $manual ? null : 'manual', null);
  328. }
  329. $this->assertCount(1, $queries, 'Unbound banners should not query activities.');
  330. }
  331. public function test_bound_banners_ignore_archived_manual_status_across_activity_states(): void
  332. {
  333. $now = time();
  334. $this->activity(7, $now - 100, $now - 10);
  335. $on = $this->banner(['link' => '7', 'status' => 1]);
  336. $off = $this->banner(['link' => '7', 'status' => 0]);
  337. $rows = $this->decorated($now);
  338. $this->assertState($rows[$on], null, 0, 'activity_ended', '7');
  339. $this->assertState($rows[$off], null, 0, 'activity_ended', '7');
  340. DB::table('activity_rewards')->where('id', 7)->update(['end_time' => $now + 100]);
  341. $rows = $this->decorated($now);
  342. $this->assertState($rows[$on], null, 1, null, '7');
  343. $this->assertState($rows[$off], null, 1, null, '7');
  344. DB::table('activity_rewards')->where('id', 7)->update(['status' => 0]);
  345. $this->assertState($this->decorated($now)[$on], null, 0, 'activity_offline', '7');
  346. DB::table('activity_rewards')->where('id', 7)->update(['status' => 1]);
  347. $this->assertState($this->decorated($now)[$on], null, 1, null, '7');
  348. $this->assertSame(1, (int) DB::table('banner')->where('id', $on)->value('status'));
  349. $this->assertSame(0, (int) DB::table('banner')->where('id', $off)->value('status'));
  350. }
  351. public function test_get_expired_banner_reports_actual_off_and_full_row_edit_preserves_archive_for_reopening(): void
  352. {
  353. $now = time();
  354. $this->activity(7, $now - 120, $now - 60);
  355. $on = $this->banner(['link' => '7', 'status' => 1, 'sort' => 17, 'lang' => 'en']);
  356. $off = $this->banner(['link' => '7', 'status' => 0]);
  357. $manualOff = $this->banner(['link' => 'https://example.test', 'status' => 0]);
  358. $page = $this->listing();
  359. $rows = collect($page['data'])->keyBy('id')->all();
  360. $this->assertSame(3, $page['total']);
  361. $this->assertState($rows[$on], null, 0, 'activity_ended', '7');
  362. $this->assertState($rows[$off], null, 0, 'activity_ended', '7');
  363. $this->assertState($rows[$manualOff], 0, 0, 'manual', null);
  364. $this->assertSame(1, (int) DB::table('banner')->where('id', $on)->value('status'));
  365. // Resubmitted activity-derived status and null manual_status do not alter the archived database flag.
  366. $edited = $rows[$on];
  367. $edited['title'] = '过期活动轮播图编辑';
  368. $result = $this->save($edited);
  369. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  370. $stored = DB::table('banner')->find($on);
  371. $this->assertSame(1, (int) $stored->status);
  372. $this->assertSame('过期活动轮播图编辑', $stored->title);
  373. $this->assertSame('7', $stored->link);
  374. $this->assertSame(17, (int) $stored->sort);
  375. $this->assertSame('en', $stored->lang);
  376. $this->assertSame('/banner.png', $stored->image);
  377. $rows = collect($this->listing()['data'])->keyBy('id')->all();
  378. $this->assertState($rows[$on], null, 0, 'activity_ended', '7');
  379. DB::table('activity_rewards')->where('id', 7)->update(['end_time' => time() + 600]);
  380. $rows = collect($this->listing()['data'])->keyBy('id')->all();
  381. $this->assertState($rows[$on], null, 1, null, '7');
  382. $this->assertState($rows[$off], null, 1, null, '7');
  383. $this->assertState($rows[$manualOff], 0, 0, 'manual', null);
  384. $this->assertSame(1, (int) DB::table('banner')->where('id', $on)->value('status'));
  385. $this->assertSame(0, (int) DB::table('banner')->where('id', $off)->value('status'));
  386. }
  387. public function test_bound_banners_reject_both_manual_toggle_aliases_without_database_mutation(): void
  388. {
  389. $now = time();
  390. $this->activity(7, $now - 120, $now + 600);
  391. $this->activity(8, $now - 120, $now - 60);
  392. foreach ([['7', 0], ['8', 1]] as [$link, $archivedStatus]) {
  393. $id = $this->banner(['link' => $link, 'status' => $archivedStatus]);
  394. $before = (array) DB::table('banner')->find($id);
  395. foreach (['status', 'manual_status'] as $field) {
  396. foreach ([0, 1] as $value) {
  397. $result = $this->setStatus(['id' => $id, $field => $value]);
  398. $this->assertSame(HttpStatus::CUSTOM_ERROR, $result['code']);
  399. $this->assertStringContainsString('关联活动', $result['msg']);
  400. $this->assertStringContainsString('不能手动上下架', $result['msg']);
  401. $this->assertSame($before, (array) DB::table('banner')->find($id));
  402. }
  403. }
  404. foreach ([['status' => null], ['manual_status' => null], ['status' => 'invalid'], ['manual_status' => []]] as $fields) {
  405. $result = $this->setStatus(['id' => $id] + $fields);
  406. $this->assertSame(HttpStatus::VALIDATION_FAILED, $result['code']);
  407. $this->assertSame($before, (array) DB::table('banner')->find($id));
  408. }
  409. }
  410. }
  411. public function test_bound_full_forms_ignore_status_inputs_and_new_bound_rows_keep_database_default_on(): void
  412. {
  413. $now = time();
  414. $this->activity(7, $now - 120, $now + 600);
  415. $id = $this->banner(['link' => '7', 'status' => 0]);
  416. foreach ([['status' => 1], ['manual_status' => 0], ['status' => 0, 'manual_status' => 1],
  417. ['status' => 0, 'manual_status' => null]] as $fields) {
  418. $result = $this->save($fields + ['id' => $id, 'title' => '活动控制上下架']);
  419. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  420. $this->assertSame(0, (int) DB::table('banner')->where('id', $id)->value('status'));
  421. $this->assertState($this->decorated($now)[$id], null, 1, null, '7');
  422. }
  423. foreach ([['status' => 'invalid'], ['manual_status' => []], ['status' => null]] as $fields) {
  424. $result = $this->save($fields + ['id' => $id, 'title' => '不应保存']);
  425. $this->assertSame(HttpStatus::VALIDATION_FAILED, $result['code']);
  426. $this->assertSame(0, (int) DB::table('banner')->where('id', $id)->value('status'));
  427. $this->assertSame('活动控制上下架', DB::table('banner')->where('id', $id)->value('title'));
  428. }
  429. $result = $this->save(['link' => '7', 'status' => 0, 'manual_status' => 0]);
  430. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  431. $newId = DB::table('banner')->latest('id')->value('id');
  432. $this->assertSame(1, (int) DB::table('banner')->where('id', $newId)->value('status'));
  433. $this->assertState($this->decorated($now)[$newId], null, 1, null, '7');
  434. }
  435. public function test_clearing_activity_link_restores_manual_control_and_rebinding_ignores_manual_off_input(): void
  436. {
  437. $now = time();
  438. $this->activity(7, $now - 120, $now + 600);
  439. $id = $this->banner(['link' => '7', 'status' => 0]);
  440. $this->assertState($this->decorated($now)[$id], null, 1, null, '7');
  441. $result = $this->save(['id' => $id, 'link' => null]);
  442. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  443. $this->assertState($this->decorated($now)[$id], 0, 0, 'manual', null);
  444. $result = $this->setStatus(['id' => $id, 'manual_status' => 1]);
  445. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  446. $this->assertState($this->decorated($now)[$id], 1, 1, null, null);
  447. $result = $this->save(['id' => $id, 'link' => '7', 'status' => 0, 'manual_status' => 0]);
  448. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  449. $this->assertSame(1, (int) DB::table('banner')->where('id', $id)->value('status'));
  450. $this->assertState($this->decorated($now)[$id], null, 1, null, '7');
  451. }
  452. public function test_integer_and_leading_zero_activity_links_are_validated_and_canonicalized(): void
  453. {
  454. $this->activity(7, 1, 4102444799);
  455. foreach ([7, '7', '0007'] as $link) {
  456. $result = $this->save(['link' => $link]);
  457. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  458. $this->assertSame('7', DB::table('banner')->latest('id')->value('link'));
  459. }
  460. foreach ([999, '000999'] as $link) {
  461. $result = $this->save(['link' => $link]);
  462. $this->assertSame(HttpStatus::VALIDATION_FAILED, $result['code']);
  463. $this->assertStringContainsString('绑定的活动不存在', $result['msg']);
  464. }
  465. $this->assertSame(3, DB::table('banner')->count());
  466. $id = DB::table('banner')->value('id');
  467. $result = $this->save(['id' => $id, 'link' => '999']);
  468. $this->assertSame(HttpStatus::VALIDATION_FAILED, $result['code']);
  469. $this->assertSame('7', DB::table('banner')->where('id', $id)->value('link'));
  470. $result = $this->save(['link' => ['id' => 7]]);
  471. $this->assertSame(HttpStatus::VALIDATION_FAILED, $result['code']);
  472. $this->assertSame(3, DB::table('banner')->count());
  473. }
  474. public function test_stale_missing_activity_link_can_be_edited_but_cannot_be_manually_toggled(): void
  475. {
  476. $id = $this->banner(['link' => '500', 'sort' => 31]);
  477. $result = $this->save(['id' => $id, 'title' => '已删除活动的轮播图']);
  478. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  479. $this->assertSame('500', DB::table('banner')->where('id', $id)->value('link'));
  480. $this->assertSame(31, (int) DB::table('banner')->where('id', $id)->value('sort'));
  481. $result = $this->save(['id' => $id, 'link' => '000500', 'status' => 0]);
  482. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  483. $this->assertState($this->decorated(time())[$id], null, 0, 'activity_missing', '500');
  484. foreach (['status', 'manual_status'] as $field) {
  485. foreach ([0, 1] as $value) {
  486. $result = $this->setStatus(['id' => $id, $field => $value]);
  487. $this->assertSame(HttpStatus::CUSTOM_ERROR, $result['code']);
  488. $this->assertStringContainsString('关联活动', $result['msg']);
  489. $this->assertStringContainsString('不能手动上下架', $result['msg']);
  490. $this->assertSame(1, (int) DB::table('banner')->where('id', $id)->value('status'));
  491. }
  492. }
  493. }
  494. public function test_admin_pagination_counts_hidden_rows_and_batches_activity_queries(): void
  495. {
  496. $now = time();
  497. $this->activity(7, $now - 100, $now + 100);
  498. $this->activity(8, $now - 100, $now - 10);
  499. for ($i = 0; $i < 24; $i++) {
  500. $this->banner(['title' => '轮播图 ' . $i, 'link' => $i % 2 ? '7' : '8',
  501. 'status' => $i % 3 ? 1 : 0, 'sort' => $i]);
  502. }
  503. DB::connection()->enableQueryLog();
  504. $page = $this->listing(['limit' => 20, 'page' => 1]);
  505. $queries = DB::connection()->getQueryLog();
  506. DB::connection()->disableQueryLog();
  507. $this->assertSame(24, $page['total']);
  508. $this->assertCount(20, $page['data']);
  509. $this->assertCount(3, $queries, 'Admin list should use one count, one page, and one batched activity query.');
  510. $this->assertSame(range(23, 4), array_column($page['data'], 'sort'));
  511. $this->assertNotEmpty(array_filter($page['data'], fn ($row) => $row['effective_status'] === 0));
  512. $this->assertNotEmpty(array_filter($page['data'], fn ($row) => $row['effective_status'] === 1));
  513. $last = $this->listing(['limit' => 20, 'page' => 2]);
  514. $this->assertSame(24, $last['total']);
  515. $this->assertCount(4, $last['data']);
  516. $this->assertSame(range(3, 0), array_column($last['data'], 'sort'));
  517. }
  518. public function test_admin_type_filter_and_sort_order_preserve_language_and_hidden_rows(): void
  519. {
  520. $first = $this->banner(['type' => 'app', 'lang' => 'en', 'sort' => 9, 'status' => 0]);
  521. $second = $this->banner(['type' => 'app', 'lang' => 'zh', 'sort' => 9]);
  522. $third = $this->banner(['type' => 'app', 'lang' => 'zh', 'sort' => 2]);
  523. $this->banner(['type' => 'pc', 'lang' => 'en', 'sort' => 100]);
  524. $page = $this->listing(['type' => 'app', 'limit' => 20]);
  525. $this->assertSame(3, $page['total']);
  526. $this->assertSame([$second, $first, $third], array_column($page['data'], 'id'));
  527. $this->assertSame(['zh', 'en', 'zh'], array_column($page['data'], 'lang'));
  528. $this->assertSame(['app', 'app', 'app'], array_column($page['data'], 'type'));
  529. $this->assertSame([1, 0, 1], array_column($page['data'], 'effective_status'));
  530. }
  531. public function test_platform_language_and_actual_status_filters_combine_without_mutating_data(): void
  532. {
  533. $now = time();
  534. $this->activity(7, $now - 100, $now + 600);
  535. $this->activity(8, $now - 100, $now - 60);
  536. $this->activity(9, $now + 600, $now + 1200);
  537. $this->activity(10, $now - 100, $now + 600, 0);
  538. foreach ([
  539. ['pc_zh_active', 'pc', 'zh', '7', 0, 110],
  540. ['pc_zh_ended', 'pc', 'zh', '8', 1, 100],
  541. ['pc_en_on', 'pc', 'en', null, 1, 90],
  542. ['pc_en_off', 'pc', 'en', null, 0, 80],
  543. ['app_zh_active', 'app', 'zh', '7', 0, 70],
  544. ['app_zh_ended', 'app', 'zh', '8', 1, 60],
  545. ['app_en_on', 'app', 'en', null, 1, 50],
  546. ['app_en_off', 'app', 'en', null, 0, 40],
  547. ['pc_zh_future', 'pc', 'zh', '9', 1, 30],
  548. ['pc_zh_offline', 'pc', 'zh', '10', 1, 20],
  549. ['pc_zh_missing', 'pc', 'zh', '999', 1, 10],
  550. ] as [$name, $type, $lang, $link, $manual, $sort]) {
  551. $ids[$name] = $this->banner(['type' => $type, 'lang' => $lang, 'link' => $link, 'status' => $manual, 'sort' => $sort]);
  552. }
  553. $bannerBefore = DB::table('banner')->orderBy('id')->get()->toJson();
  554. $activityBefore = DB::table('activity_rewards')->orderBy('id')->get()->toJson();
  555. $cases = [
  556. [[], array_keys($ids)],
  557. [['platform' => 'pc'], ['pc_zh_active', 'pc_zh_ended', 'pc_en_on', 'pc_en_off', 'pc_zh_future', 'pc_zh_offline', 'pc_zh_missing']],
  558. [['platform' => 'h5'], ['app_zh_active', 'app_zh_ended', 'app_en_on', 'app_en_off']],
  559. [['type' => 'app'], ['app_zh_active', 'app_zh_ended', 'app_en_on', 'app_en_off']],
  560. [['platform' => 'h5', 'type' => 'app'], ['app_zh_active', 'app_zh_ended', 'app_en_on', 'app_en_off']],
  561. [['platform' => 'pc', 'type' => 'pc', 'lang' => 'en'], ['pc_en_on', 'pc_en_off']],
  562. [['lang' => 'zh'], ['pc_zh_active', 'pc_zh_ended', 'app_zh_active', 'app_zh_ended', 'pc_zh_future', 'pc_zh_offline', 'pc_zh_missing']],
  563. [['lang' => 'en'], ['pc_en_on', 'pc_en_off', 'app_en_on', 'app_en_off']],
  564. [['status' => 1], ['pc_zh_active', 'pc_en_on', 'app_zh_active', 'app_en_on']],
  565. [['status' => '0'], ['pc_zh_ended', 'pc_en_off', 'app_zh_ended', 'app_en_off', 'pc_zh_future', 'pc_zh_offline', 'pc_zh_missing']],
  566. [['platform' => 'pc', 'lang' => 'zh', 'status' => 0], ['pc_zh_ended', 'pc_zh_future', 'pc_zh_offline', 'pc_zh_missing']],
  567. [['platform' => 'h5', 'lang' => 'en', 'status' => '1'], ['app_en_on']],
  568. [['platform' => 'h5', 'lang' => 'zh', 'status' => '0'], ['app_zh_ended']],
  569. [['platform' => 'h5', 'status' => 0], ['app_zh_ended', 'app_en_off']],
  570. [['platform' => 'pc', 'lang' => 'fr', 'status' => 1], []],
  571. ];
  572. foreach ($cases as [$filters, $names]) {
  573. $page = $this->listing($filters);
  574. $expectedIds = array_map(fn ($name) => $ids[$name], $names);
  575. $this->assertSame(count($expectedIds), $page['total']);
  576. $this->assertSame($expectedIds, array_column($page['data'], 'id'));
  577. if (isset($filters['platform'])) {
  578. foreach ($page['data'] as $row) $this->assertSame($filters['platform'] === 'h5' ? 'app' : 'pc', $row['type']);
  579. }
  580. if (array_key_exists('status', $filters)) {
  581. foreach ($page['data'] as $row) $this->assertSame((int) $filters['status'], $row['status']);
  582. }
  583. }
  584. $this->assertSame($bannerBefore, DB::table('banner')->orderBy('id')->get()->toJson());
  585. $this->assertSame($activityBefore, DB::table('activity_rewards')->orderBy('id')->get()->toJson());
  586. }
  587. public function test_actual_status_is_filtered_before_pagination_and_activity_lookup_stays_batched(): void
  588. {
  589. $now = time();
  590. $this->activity(7, $now - 100, $now + 600);
  591. $this->activity(8, $now - 100, $now - 60);
  592. $up = $down = [];
  593. for ($i = 0; $i < 12; $i++) {
  594. $id = $this->banner(['type' => 'app', 'lang' => 'zh', 'link' => $i % 2 ? '8' : '7', 'status' => $i % 2, 'sort' => $i]);
  595. if ($i % 2) $down[] = $id; else $up[] = $id;
  596. }
  597. $this->banner(['type' => 'app', 'lang' => 'en', 'link' => '7', 'sort' => 100]);
  598. $this->banner(['type' => 'pc', 'lang' => 'zh', 'link' => '7', 'sort' => 101]);
  599. $up = array_reverse($up); $down = array_reverse($down);
  600. $snapshot = DB::table('banner')->orderBy('id')->get()->toJson();
  601. $filters = ['platform' => 'h5', 'lang' => 'zh', 'status' => 1, 'limit' => 2];
  602. DB::connection()->enableQueryLog();
  603. $first = $this->listing($filters);
  604. $queries = DB::connection()->getQueryLog();
  605. DB::connection()->disableQueryLog();
  606. $this->assertCount(2, $queries, 'Status filtering should fetch matching candidates and all referenced activities in two queries.');
  607. $this->assertSame(6, $first['total']);
  608. $this->assertSame(array_slice($up, 0, 2), array_column($first['data'], 'id'));
  609. $seen = array_column($first['data'], 'id');
  610. foreach ([2, 3, 4] as $number) {
  611. $page = $this->listing($filters + ['page' => $number]);
  612. $this->assertSame(6, $page['total']);
  613. $this->assertSame(array_slice($up, ($number - 1) * 2, 2), array_column($page['data'], 'id'));
  614. $seen = array_merge($seen, array_column($page['data'], 'id'));
  615. }
  616. $this->assertSame($up, $seen);
  617. foreach ([1, 2] as $number) {
  618. $page = $this->listing(['platform' => 'h5', 'type' => 'app', 'lang' => 'zh', 'status' => '0', 'page' => $number, 'limit' => 4]);
  619. $this->assertSame(6, $page['total']);
  620. $this->assertSame(array_slice($down, ($number - 1) * 4, 4), array_column($page['data'], 'id'));
  621. }
  622. $this->assertSame($snapshot, DB::table('banner')->orderBy('id')->get()->toJson());
  623. }
  624. public function test_empty_optional_filters_keep_all_languages_and_default_pagination(): void
  625. {
  626. for ($i = 0; $i < 16; $i++) {
  627. $ids[] = $this->banner(['type' => $i % 2 ? 'pc' : 'app', 'lang' => $i % 2 ? 'zh' : 'en', 'status' => $i % 2]);
  628. }
  629. $ids = array_reverse($ids);
  630. $snapshot = DB::table('banner')->orderBy('id')->get()->toJson();
  631. foreach ([[], ['platform' => null, 'type' => null, 'lang' => null, 'status' => null],
  632. ['platform' => '', 'type' => '', 'lang' => '', 'status' => '']] as $filters) {
  633. $page = $this->listing($filters);
  634. $this->assertSame(16, $page['total']);
  635. $this->assertCount(15, $page['data']);
  636. $this->assertSame(array_slice($ids, 0, 15), array_column($page['data'], 'id'));
  637. $this->assertSame(['zh', 'en'], array_values(array_unique(array_column($page['data'], 'lang'))));
  638. $last = $this->listing($filters + ['page' => 2]);
  639. $this->assertSame(16, $last['total']);
  640. $this->assertSame(array_slice($ids, 15), array_column($last['data'], 'id'));
  641. }
  642. $largePage = $this->listing(['limit' => 200]);
  643. $this->assertSame(16, $largePage['total']);
  644. $this->assertSame($ids, array_column($largePage['data'], 'id'));
  645. $this->assertSame($snapshot, DB::table('banner')->orderBy('id')->get()->toJson());
  646. }
  647. public function test_invalid_filters_and_conflicting_platform_aliases_are_rejected_without_mutation(): void
  648. {
  649. $this->banner(['type' => 'app', 'lang' => 'en']);
  650. $snapshot = DB::table('banner')->get()->toJson();
  651. foreach ([
  652. ['platform' => 'app'], ['platform' => 'android'], ['platform' => ['pc']],
  653. ['type' => 'h5'], ['type' => ['app']],
  654. ['platform' => 'pc', 'type' => 'app'], ['platform' => 'h5', 'type' => 'pc'],
  655. ['lang' => ['zh']], ['lang' => 123], ['lang' => 'abcdefghijk'],
  656. ['status' => -1], ['status' => 2], ['status' => 'up'], ['status' => '1.5'], ['status' => ['0']],
  657. ['page' => 0], ['limit' => 0], ['limit' => 201],
  658. ] as $filters) {
  659. $this->request('index', $filters);
  660. $result = (new AdminBanner())->index()->getData(true);
  661. $this->assertSame(HttpStatus::VALIDATION_FAILED, $result['code']);
  662. }
  663. $this->assertSame($snapshot, DB::table('banner')->get()->toJson());
  664. }
  665. public function test_status_route_requires_existing_banner_edit_permission_and_preserves_superadmin_access(): void
  666. {
  667. Schema::create('menus', function (Blueprint $t) {
  668. $t->id(); $t->string('uri'); $t->integer('type'); $t->integer('status');
  669. });
  670. Schema::create('roles', function (Blueprint $t) {
  671. $t->id(); $t->string('name');
  672. });
  673. Schema::create('role_user', function (Blueprint $t) {
  674. $t->id(); $t->integer('user_id'); $t->integer('role_id');
  675. });
  676. Schema::create('role_menu', function (Blueprint $t) {
  677. $t->id(); $t->integer('role_id'); $t->integer('menu_id');
  678. });
  679. $editMenu = DB::table('menus')->insertGetId([
  680. 'uri' => 'admin/banner/update', 'type' => 2, 'status' => 1,
  681. ]);
  682. $otherMenu = DB::table('menus')->insertGetId([
  683. 'uri' => 'admin/banner/delete', 'type' => 2, 'status' => 1,
  684. ]);
  685. $router = new Router(new Dispatcher($this->app), $this->app);
  686. $this->app->instance('router', $router);
  687. $router->group(['prefix' => 'admin'], function () {
  688. require dirname(__DIR__, 2) . '/routes/admin.php';
  689. });
  690. $route = $router->getRoutes()->match(Request::create('/admin/banner/status', 'POST'));
  691. $this->assertSame('admin/banner/update', $route->defaults['permission_uri']);
  692. $this->assertContains('admin.jwt', $route->middleware());
  693. $this->assertContains('check.button.uri', $route->middleware());
  694. $this->assertSame(AdminBanner::class . '@status', $route->getAction('uses'));
  695. $id = $this->banner();
  696. $changeStatus = function (int $userId, int $status) use ($router, $id): array {
  697. $this->request('status', ['id' => $id, 'manual_status' => $status, 'user' => (object) ['id' => $userId]]);
  698. $request = $this->app['request'];
  699. $route = $router->getRoutes()->match($request);
  700. $request->setRouteResolver(fn () => $route);
  701. return (new CheckButtonPermission())->handle($request,
  702. fn () => (new AdminBanner())->status())->getData(true);
  703. };
  704. // The status URI has no separate menu: it must inherit the existing edit button gate.
  705. $this->assertSame(0, DB::table('menus')->where('uri', 'admin/banner/status')->count());
  706. $result = $changeStatus(20, 0);
  707. $this->assertSame(-1, $result['code']);
  708. $this->assertSame('无权限访问', $result['msg']);
  709. $this->assertSame(1, (int) DB::table('banner')->where('id', $id)->value('status'));
  710. DB::table('roles')->insert(['id' => 5, 'name' => 'editor']);
  711. DB::table('role_user')->insert(['user_id' => 20, 'role_id' => 5]);
  712. DB::table('role_menu')->insert(['role_id' => 5, 'menu_id' => $otherMenu]);
  713. $this->assertSame(-1, $changeStatus(20, 0)['code']);
  714. $this->assertSame(1, (int) DB::table('banner')->where('id', $id)->value('status'));
  715. DB::table('role_menu')->insert(['role_id' => 5, 'menu_id' => $editMenu]);
  716. $result = $changeStatus(20, 0);
  717. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  718. $this->assertSame(0, (int) DB::table('banner')->where('id', $id)->value('status'));
  719. $result = $changeStatus(1, 1);
  720. $this->assertSame(HttpStatus::OK, $result['code'], $result['msg']);
  721. $this->assertSame(1, (int) DB::table('banner')->where('id', $id)->value('status'));
  722. $this->assertSame(0, DB::table('role_user')->where('user_id', 1)->count());
  723. }
  724. }