14_channels.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. use \WikipaliApi\Api\ChannelsApi;
  3. use \WikipaliApi\Api\StudiosApi;
  4. use \WikipaliApi\Api\TipitakaChannelsApi;
  5. use \WikipaliApi\Api\TipitakaParagraphChannelsApi;
  6. use \WikipaliApi\Model\PatchApiV3ChannelsChannelRequest;
  7. use \WikipaliApi\Model\PostApiV3ChannelsBatchProgressRequest;
  8. use \WikipaliApi\Model\PostApiV3StudiosStudioChannelsRequest;
  9. /*
  10. * v3 channel:公开目录、详情、我能写的、studio 列表、建 → 改 → 删、阅读领域的两个查询、batch-progress。
  11. * 「建 → 改 → 删」用临时 channel(e2e-tmp-*),删是软删除。
  12. */
  13. /** 测试账号自己的 uuid */
  14. function my_uid(string $token): string
  15. {
  16. $me = raw('GET', '/v3/me', null, null, $token);
  17. assert_status(200, $me, 'me');
  18. return $me['body']['data']['id'];
  19. }
  20. /* ---- 公开读 ---- */
  21. test('GET /v3/channels → 200,只有公开的', function () {
  22. $res = request(fn () => api(ChannelsApi::class)->getApiV3ChannelsWithHttpInfo());
  23. assert_status(200, $res, 'channels');
  24. assert_has_key('meta', $res['body'], 'channels');
  25. foreach ($res['body']['data'] as $channel) {
  26. assert_eq(30, $channel['status'], 'channels 只列公开的');
  27. assert_has_key('abilities', $channel, 'channels.data[]');
  28. }
  29. });
  30. test('GET /v3/channels?system=true → 200,都是系统 channel', function () {
  31. $res = request(fn () => api(ChannelsApi::class)->getApiV3ChannelsWithHttpInfo(system: true, per_page: 50));
  32. assert_status(200, $res, 'channels system');
  33. foreach ($res['body']['data'] as $channel) {
  34. assert_eq(true, $channel['is_system'], 'system=true 只列系统 channel');
  35. }
  36. });
  37. test('GET /v3/channels?para=(缺 book)→ 422', function () {
  38. $res = raw('GET', '/v3/channels', ['para' => 1]);
  39. assert_status(422, $res, 'channels 缺 book');
  40. assert_field_error('book', $res, 'channels 缺 book');
  41. });
  42. test('GET /v3/channels/{channel} → 200;不存在 → 404', function () {
  43. $res = request(fn () => api(ChannelsApi::class)->getApiV3ChannelsChannelWithHttpInfo(fixtures()['channel']));
  44. assert_status(200, $res, 'channel 详情');
  45. assert_has_key('owner', $res['body']['data'], 'channel 详情');
  46. $missing = request(fn () => api(ChannelsApi::class)->getApiV3ChannelsChannelWithHttpInfo('11111111-1111-4111-8111-111111111111'));
  47. assert_status(404, $missing, 'channel 不存在');
  48. });
  49. test('GET /v3/tipitaka-channels?lang=zh → 200,带 chapter_count', function () {
  50. $res = request(fn () => api(TipitakaChannelsApi::class)->getApiV3TipitakaChannelsWithHttpInfo(lang: 'zh', type: 'translation'));
  51. assert_status(200, $res, 'tipitaka-channels');
  52. $first = $res['body']['data'][0] ?? null;
  53. if ($first) {
  54. assert_has_key('chapter_count', $first, 'tipitaka-channels.data[0]');
  55. }
  56. });
  57. test('GET /v3/tipitaka-paragraph-channels/{book}/{para} → 200', function () {
  58. $res = request(fn () => api(TipitakaParagraphChannelsApi::class)->getApiV3TipitakaParagraphChannelsBookParaWithHttpInfo(fixtures()['book'], fixtures()['para']));
  59. assert_status(200, $res, 'tipitaka-paragraph-channels');
  60. assert_has_key('meta', $res['body'], 'tipitaka-paragraph-channels');
  61. });
  62. test('POST /v3/channels/batch-progress → 200;格式不对 → 422', function () {
  63. $body = new PostApiV3ChannelsBatchProgressRequest();
  64. $body->setSentences(['1-2-1-10']);
  65. $res = request(fn () => api(ChannelsApi::class)->postApiV3ChannelsBatchProgressWithHttpInfo($body));
  66. assert_status(200, $res, 'batch-progress');
  67. $bad = raw('POST', '/v3/channels/batch-progress', null, ['sentences' => ['1-2']]);
  68. assert_status(422, $bad, 'batch-progress 格式不对');
  69. });
  70. /* ---- 要登录 ---- */
  71. test('GET /v3/channels/editable(无 token)→ 401;带 token → 200', function () {
  72. $anon = request(fn () => api(ChannelsApi::class)->getApiV3ChannelsEditableWithHttpInfo());
  73. assert_status(401, $anon, 'editable 无 token');
  74. assert_problem($anon, 'editable 无 token');
  75. $res = request(fn () => api(ChannelsApi::class, required_token())->getApiV3ChannelsEditableWithHttpInfo(per_page: 5));
  76. assert_status(200, $res, 'editable');
  77. foreach ($res['body']['data'] as $channel) {
  78. assert_eq(true, $channel['abilities']['write'], 'editable 里的都能写');
  79. }
  80. });
  81. test('GET /v3/studios/{studio}/channels:本人 200,无 token 401', function () {
  82. $token = required_token();
  83. $me = my_uid($token);
  84. $res = request(fn () => api(StudiosApi::class, $token)->getApiV3StudiosStudioChannelsWithHttpInfo($me, relation: 'own', per_page: 5));
  85. assert_status(200, $res, 'studio channels');
  86. assert_has_key('meta', $res['body'], 'studio channels');
  87. $anon = request(fn () => api(StudiosApi::class)->getApiV3StudiosStudioChannelsWithHttpInfo($me));
  88. assert_status(401, $anon, 'studio channels 无 token');
  89. });
  90. test('POST /v3/studios/{studio}/channels(缺 name)→ 422', function () {
  91. $token = required_token();
  92. $res = raw('POST', '/v3/studios/'.my_uid($token).'/channels', null, ['type' => 'translation', 'lang' => 'zh'], $token);
  93. assert_status(422, $res, 'channels 缺 name');
  94. assert_field_error('name', $res, 'channels 缺 name');
  95. });
  96. test('channels 一轮:建 201 → 改 200(只改传了的)→ 删 200(软删)→ 再看 404', function () {
  97. $token = required_token();
  98. $me = my_uid($token);
  99. $body = new PostApiV3StudiosStudioChannelsRequest();
  100. $body->setName('e2e-tmp-'.time());
  101. $body->setType('translation');
  102. $body->setLang('zh-Hans');
  103. $body->setSummary('api-test');
  104. $create = request(fn () => api(StudiosApi::class, $token)->postApiV3StudiosStudioChannelsWithHttpInfo($me, $body));
  105. assert_status(201, $create, '建 channel');
  106. assert_eq(10, $create['body']['data']['status'] ?? null, '新建默认私有');
  107. $channel = $create['body']['data']['id'];
  108. $patch = new PatchApiV3ChannelsChannelRequest();
  109. $patch->setStatus(30);
  110. $update = request(fn () => api(ChannelsApi::class, $token)->patchApiV3ChannelsChannelWithHttpInfo($channel, $patch));
  111. assert_status(200, $update, '改 channel');
  112. assert_eq(30, $update['body']['data']['status'] ?? null, '改后的状态');
  113. assert_eq('api-test', $update['body']['data']['summary'] ?? null, '没传的字段不变');
  114. $delete = request(fn () => api(ChannelsApi::class, $token)->deleteApiV3ChannelsChannelWithHttpInfo($channel));
  115. assert_status(200, $delete, '删 channel');
  116. $gone = request(fn () => api(ChannelsApi::class, $token)->getApiV3ChannelsChannelWithHttpInfo($channel));
  117. assert_status(404, $gone, '删后再看');
  118. });
  119. test('tipitaka-reading 私有 channel:无 token 403,拥有者 200', function () {
  120. $token = required_token();
  121. $body = new PostApiV3StudiosStudioChannelsRequest();
  122. $body->setName('e2e-tmp-private-'.time());
  123. $body->setType('translation');
  124. $body->setLang('zh-Hans');
  125. $create = request(fn () => api(StudiosApi::class, $token)->postApiV3StudiosStudioChannelsWithHttpInfo(my_uid($token), $body));
  126. assert_status(201, $create, '建私有 channel');
  127. $channel = $create['body']['data']['id'];
  128. $anon = raw('GET', "/v3/tipitaka-reading/{$channel}");
  129. assert_status(403, $anon, '无 token 读私有正文');
  130. assert_problem($anon, '无 token 读私有正文');
  131. $mine = raw('GET', "/v3/tipitaka-reading/{$channel}", null, null, $token);
  132. assert_status(200, $mine, '拥有者读私有正文');
  133. $chapter = raw('GET', "/v3/search/tipitaka_chapter_1-1_{$channel}");
  134. assert_status(403, $chapter, '无 token 取私有 channel 的整章文档');
  135. request(fn () => api(ChannelsApi::class, $token)->deleteApiV3ChannelsChannelWithHttpInfo($channel));
  136. });