GlossaryV3Test.php 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. use App\Models\DhammaTerm;
  3. use Illuminate\Foundation\Testing\RefreshDatabase;
  4. use Illuminate\Support\Str;
  5. uses(RefreshDatabase::class);
  6. /*
  7. * GET /v3/glossaries/{grammar|community}:系统 channel 里的术语,整表、按词排序。
  8. * 这里断言的是形状而非契约(spectator 未安装,见 v3-resource skill 第 3 步)。
  9. */
  10. function glossaryTerm(string $channel, string $word, array $attrs = []): DhammaTerm
  11. {
  12. $term = new DhammaTerm;
  13. $term->forceFill($attrs + [
  14. 'id' => random_int(1, PHP_INT_MAX),
  15. 'guid' => (string) Str::uuid(),
  16. 'word' => $word,
  17. 'word_en' => $word,
  18. 'meaning' => "{$word}-义",
  19. 'channal' => $channel,
  20. 'language' => 'zh-Hans',
  21. 'owner' => config('mint.admin.root_uuid'),
  22. 'editor_id' => 0,
  23. 'create_time' => time() * 1000,
  24. 'modify_time' => time() * 1000,
  25. ])->save();
  26. return $term;
  27. }
  28. it('lists the community glossary of a language, sorted by word', function () {
  29. $root = config('mint.admin.root_uuid');
  30. $zh = makeChannel($root, '_community_term_zh-hans_');
  31. $grammar = makeChannel($root, '_System_Grammar_Term_zh-hans_');
  32. glossaryTerm($zh, 'sīla', ['tag' => '', 'other_meaning' => '戒律']);
  33. glossaryTerm($zh, 'dhamma');
  34. glossaryTerm($grammar, 'vibhatti');
  35. glossaryTerm($zh, 'deleted')->delete();
  36. $this->getJson('/api/v3/glossaries/community?lang=zh-Hans')
  37. ->assertOk()
  38. ->assertJsonPath('meta', ['kind' => 'community', 'lang' => 'zh-Hans', 'total' => 2])
  39. ->assertJsonPath('data.*.word', ['dhamma', 'sīla'])
  40. ->assertJsonPath('data.1.tag', null)
  41. ->assertJsonPath('data.1.other_meaning', '戒律');
  42. $this->getJson('/api/v3/glossaries/grammar?lang=zh-Hans')->assertJsonPath('data.*.word', ['vibhatti']);
  43. });
  44. it('falls back to English, and defaults to the negotiated interface language', function () {
  45. $root = config('mint.admin.root_uuid');
  46. $en = makeChannel($root, '_community_term_en_');
  47. $zh = makeChannel($root, '_community_term_zh-hans_');
  48. glossaryTerm($en, 'en-word');
  49. glossaryTerm($zh, 'zh-word');
  50. $this->getJson('/api/v3/glossaries/community?lang=my')
  51. ->assertJsonPath('meta.lang', 'en')
  52. ->assertJsonPath('data.*.word', ['en-word']);
  53. $this->getJson('/api/v3/glossaries/community', ['Accept-Language' => 'zh-CN'])
  54. ->assertJsonPath('meta.lang', 'zh-Hans')
  55. ->assertJsonPath('data.*.word', ['zh-word']);
  56. });
  57. it('returns an empty list when neither the language nor English has the glossary', function () {
  58. $this->getJson('/api/v3/glossaries/grammar?lang=zh-Hans')
  59. ->assertOk()
  60. ->assertJsonPath('data', [])
  61. ->assertJsonPath('meta.total', 0);
  62. });
  63. it('rejects an unknown kind or a malformed language', function () {
  64. $this->getJson('/api/v3/glossaries/other')->assertNotFound();
  65. $this->getJson('/api/v3/glossaries/community?lang='.urlencode('../x'))
  66. ->assertUnprocessable()->assertJsonValidationErrors('lang');
  67. });
  68. it('never exposes the auto-increment id of the terms', function () {
  69. $zh = makeChannel(config('mint.admin.root_uuid'), '_community_term_zh-hans_');
  70. $term = glossaryTerm($zh, 'dhamma');
  71. $row = $this->getJson('/api/v3/glossaries/community?lang=zh-Hans')->json('data.0');
  72. expect(array_keys($row))->toBe(['id', 'word', 'tag', 'meaning', 'other_meaning'])
  73. ->and($row['id'])->toBe($term->guid)
  74. ->and(json_encode($row))->not->toContain((string) $term->id);
  75. });