16_terms.php 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. use \WikipaliApi\Api\ChannelsApi;
  3. use \WikipaliApi\Api\StudiosApi;
  4. use \WikipaliApi\Api\TermsApi;
  5. use \WikipaliApi\Model\PostApiV3ChannelsChannelTermsRequest;
  6. use \WikipaliApi\Model\PostApiV3StudiosStudioChannelsRequest;
  7. /*
  8. * v3 术语:/v3/terms、/v3/channels/{c}/terms、/v3/studios/{s}/terms。
  9. * 写操作用临时 channel(e2e-tmp-terms-*):建 → 读(ETag)→ 改(If-Match,412 / 428)→ 删(软删),最后删掉 channel。
  10. * 权限矩阵(团队角色、AI 代写)、分类抽取、N+1 由 api-v13 的 TermV3Test 覆盖。
  11. */
  12. test('GET /v3/terms?word= → 200;缺 word → 422', function () {
  13. $res = request(fn () => api(TermsApi::class)->getApiV3TermsWithHttpInfo('dhamma', 5));
  14. assert_status(200, $res, 'terms lookup');
  15. assert_has_key('meta', $res['body'], 'terms lookup');
  16. $bad = raw('GET', '/v3/terms');
  17. assert_status(422, $bad, 'terms 缺 word');
  18. assert_field_error('word', $bad, 'terms 缺 word');
  19. });
  20. test('GET /v3/channels/{channel}/terms → 200;GET /v3/terms/{不存在} → 404', function () {
  21. $res = request(fn () => api(ChannelsApi::class)->getApiV3ChannelsChannelTermsWithHttpInfo(fixtures()['channel'], 5));
  22. assert_status(200, $res, 'channel terms');
  23. assert_has_key('meta', $res['body'], 'channel terms');
  24. assert_status(404, raw('GET', '/v3/terms/11111111-1111-4111-8111-111111111111'), '术语不存在');
  25. });
  26. test('写术语(无 token)→ 401', function () {
  27. $res = raw('POST', '/v3/channels/'.fixtures()['channel'].'/terms', null, ['word' => 'x', 'meaning' => 'y']);
  28. assert_status(401, $res, 'terms 无 token');
  29. assert_problem($res, 'terms 无 token');
  30. });
  31. test('terms 一轮:建 201 → 读带 ETag → 不带 If-Match 428 → 旧 etag 412 → 改 200 → 删 200(软删)→ 再看 404', function () {
  32. $token = required_token();
  33. $me = raw('GET', '/v3/me', null, null, $token);
  34. assert_status(200, $me, 'me');
  35. $body = new PostApiV3StudiosStudioChannelsRequest();
  36. $body->setName('e2e-tmp-terms-'.time());
  37. $body->setType('translation');
  38. $body->setLang('zh-Hans');
  39. $channel = request(fn () => api(StudiosApi::class, $token)->postApiV3StudiosStudioChannelsWithHttpInfo($me['body']['data']['id'], $body));
  40. assert_status(201, $channel, '建临时 channel');
  41. $channelId = $channel['body']['data']['id'];
  42. $termBody = new PostApiV3ChannelsChannelTermsRequest();
  43. $termBody->setWord('apitest');
  44. $termBody->setMeaning('接口测试');
  45. $termBody->setNote("第一段。\n\n{{category|api-test}}");
  46. $termBody->setQuality('draft');
  47. $create = request(fn () => api(ChannelsApi::class, $token)->postApiV3ChannelsChannelTermsWithHttpInfo($channelId, $termBody));
  48. assert_status(201, $create, '建术语');
  49. $term = $create['body']['data'];
  50. assert_eq('markdown-e2', $term['content_type'] ?? null, '新建默认 markdown-e2');
  51. assert_eq(['api-test'], $term['categories'] ?? null, '分类从正文抽出');
  52. $dup = raw('POST', "/v3/channels/{$channelId}/terms", null, ['word' => 'apitest', 'meaning' => '重复'], $token);
  53. assert_status(409, $dup, '同一 word@tag 重复');
  54. $show = raw('GET', "/v3/terms/{$term['id']}", null, null, $token);
  55. assert_status(200, $show, '读术语');
  56. assert_eq($term['etag'], $show['headers']['etag'][0] ?? null, 'ETag 头与 etag 字段一致');
  57. assert_has_key('body', $show['body']['data'], '单条带渲染后的正文');
  58. assert_status(428, raw('PATCH', "/v3/terms/{$term['id']}", null, ['meaning' => '改'], $token), '不带 If-Match');
  59. assert_status(412, raw('PATCH', "/v3/terms/{$term['id']}", null, ['meaning' => '改'], $token, ['If-Match' => '"0.0"']), '旧 etag');
  60. $word = raw('PATCH', "/v3/terms/{$term['id']}", null, ['word' => 'other'], $token, ['If-Match' => $term['etag']]);
  61. assert_status(422, $word, '词头不能改');
  62. assert_field_error('word', $word, '词头不能改');
  63. $update = raw('PATCH', "/v3/terms/{$term['id']}", null, ['meaning' => '改过的'], $token, ['If-Match' => $term['etag']]);
  64. assert_status(200, $update, '改术语');
  65. assert_eq('改过的', $update['body']['data']['meaning'] ?? null, '改后的释义');
  66. $delete = request(fn () => api(TermsApi::class, $token)->deleteApiV3TermsTermWithHttpInfo($term['id']));
  67. assert_status(200, $delete, '删术语');
  68. assert_status(404, raw('GET', "/v3/terms/{$term['id']}", null, null, $token), '删后再看');
  69. assert_status(200, request(fn () => api(ChannelsApi::class, $token)->deleteApiV3ChannelsChannelWithHttpInfo($channelId)), '删临时 channel');
  70. });