10_tipitaka_chapter_channels.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. use \WikipaliApi\Api\TipitakaChapterChannelsApi;
  3. /*
  4. * 一章有哪些 channel 及完成度:GET /v3/tipitaka-chapter-channels/{book}/{para}
  5. *
  6. * 只读、无写操作。章节坐标从 fixture channel 自己的进度里取(各服务器数据不同),
  7. * 再核对这一章的列表里有它。fixture channel 是公开的,匿名就看得到。
  8. * 可见性矩阵(私有 / 共享 / 群组)、排序、N+1 由 api-v13 的 TipitakaChapterChannelV3Test 覆盖。
  9. */
  10. /** fixture channel 有进度的某一章的 [book, para] */
  11. function chapter_channel_coord(): array
  12. {
  13. $res = raw('GET', '/v3/progress', ['channels' => fixtures()['channel'], 'per_page' => 1]);
  14. assert_status(200, $res, '取 fixture channel 的章节进度');
  15. $row = $res['body']['data'][0] ?? null;
  16. if (! $row) {
  17. skip('fixture channel 在这台服务器上没有章节进度');
  18. }
  19. return [(int) $row['book'], (int) $row['para']];
  20. }
  21. test('GET /v3/tipitaka-chapter-channels/{book}/{para} → 200,含 fixture channel', function () {
  22. [$book, $para] = chapter_channel_coord();
  23. $res = request(fn () => api(TipitakaChapterChannelsApi::class)
  24. ->getApiV3TipitakaChapterChannelsBookParaWithHttpInfo($book, $para, 200));
  25. assert_status(200, $res, 'tipitaka-chapter-channels');
  26. assert_has_key('data', $res['body'], 'tipitaka-chapter-channels');
  27. assert_has_key('meta', $res['body'], 'tipitaka-chapter-channels');
  28. $hit = null;
  29. foreach ($res['body']['data'] as $row) {
  30. if (($row['channel']['id'] ?? null) === fixtures()['channel']) {
  31. $hit = $row;
  32. }
  33. }
  34. if (! $hit) {
  35. fail('tipitaka-chapter-channels:应包含 fixture channel');
  36. }
  37. assert_eq($book, $hit['book'] ?? null, 'book');
  38. assert_eq($para, $hit['para'] ?? null, 'para');
  39. foreach (['id', 'progress', 'views', 'updated_at', 'studio'] as $key) {
  40. assert_has_key($key, $hit, 'tipitaka-chapter-channels 行');
  41. }
  42. // 按完成度降序
  43. $progress = array_column($res['body']['data'], 'progress');
  44. $sorted = $progress;
  45. rsort($sorted);
  46. assert_eq($sorted, $progress, '按 progress 降序');
  47. });
  48. test('GET /v3/tipitaka-chapter-channels/{book}/{para}(没有进度的章节)→ 200 空列表', function () {
  49. $res = raw('GET', '/v3/tipitaka-chapter-channels/1/999999');
  50. assert_status(200, $res, '空章节');
  51. assert_eq([], $res['body']['data'] ?? null, '空章节 data');
  52. });
  53. test('GET /v3/tipitaka-chapter-channels/{book}/{para}?per_page=500 → 422', function () {
  54. $res = raw('GET', '/v3/tipitaka-chapter-channels/1/2', ['per_page' => 500]);
  55. assert_status(422, $res, 'per_page 超限');
  56. assert_field_error('per_page', $res, 'per_page 超限');
  57. });
  58. test('GET /v3/tipitaka-chapter-channels/{非数字}/{para} → 404', function () {
  59. assert_status(404, raw('GET', '/v3/tipitaka-chapter-channels/abc/2'), '非数字 book');
  60. });