17_glossaries.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. use \WikipaliApi\Api\GlossariesApi;
  3. /*
  4. * 术语词汇表:GET /v3/glossaries/{grammar|community}(取代 v2 term-vocabulary)。只读、整表、公开。
  5. * 各服务器的数据量不同,只核对形状与回退规则;排序、已删条目、不暴露自增 id 由 api-v13 的 GlossaryV3Test 覆盖。
  6. */
  7. test('GET /v3/glossaries/community?lang=zh-Hans → 200,整表带 meta', function () {
  8. $res = request(fn () => api(GlossariesApi::class)->getApiV3GlossariesKindWithHttpInfo('community', 'zh-Hans'));
  9. assert_status(200, $res, 'community glossary');
  10. foreach (['kind', 'lang', 'total'] as $key) {
  11. assert_has_key($key, $res['body']['meta'], 'glossary meta');
  12. }
  13. assert_eq('community', $res['body']['meta']['kind'], 'meta.kind');
  14. assert_eq(count($res['body']['data']), $res['body']['meta']['total'], '整表不分页:total 与条数相等');
  15. $first = $res['body']['data'][0] ?? null;
  16. if ($first) {
  17. assert_eq(['id', 'word', 'tag', 'meaning', 'other_meaning'], array_keys($first), '条目字段');
  18. }
  19. });
  20. test('GET /v3/glossaries/grammar?lang=xx(没有这种语言的表)→ 200,回退到 en', function () {
  21. $res = raw('GET', '/v3/glossaries/grammar', ['lang' => 'xx']);
  22. assert_status(200, $res, 'grammar glossary 回退');
  23. assert_eq('en', $res['body']['meta']['lang'] ?? null, '回退语言');
  24. });
  25. test('GET /v3/glossaries/{非法 kind} → 404;lang 格式不对 → 422', function () {
  26. assert_status(404, raw('GET', '/v3/glossaries/other'), '非法 kind');
  27. $bad = raw('GET', '/v3/glossaries/community', ['lang' => '../x']);
  28. assert_status(422, $bad, 'lang 格式不对');
  29. assert_field_error('lang', $bad, 'lang 格式不对');
  30. });