TermIndexServiceTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. use App\Models\DhammaTerm;
  3. use App\Models\TermCategory;
  4. use App\Services\TermIndexService;
  5. use Illuminate\Foundation\Testing\RefreshDatabase;
  6. use Illuminate\Support\Str;
  7. uses(RefreshDatabase::class);
  8. /*
  9. * 术语的 OpenSearch 文档用新渲染器构建:内容 = 摘要 + 正文;分类、质量优先取 v3 的列,没有时从正文模板抽。
  10. */
  11. function indexTerm(string $channel, array $attrs): DhammaTerm
  12. {
  13. $term = new DhammaTerm;
  14. $term->forceFill($attrs + [
  15. 'id' => random_int(1, PHP_INT_MAX),
  16. 'guid' => (string) Str::uuid(),
  17. 'word' => 'dhamma',
  18. 'word_en' => 'dhamma',
  19. 'meaning' => '法',
  20. 'channal' => $channel,
  21. 'language' => 'zh-Hans',
  22. 'owner' => 'owner',
  23. 'editor_id' => 0,
  24. 'create_time' => time() * 1000,
  25. 'modify_time' => time() * 1000,
  26. ])->save();
  27. return $term;
  28. }
  29. it('builds a v3 term document from summary + note with the new renderer and the v3 columns', function () {
  30. $owner = makeStudio('owner');
  31. $channel = makeChannel($owner, '译文');
  32. $term = indexTerm($channel, [
  33. 'owner' => $owner,
  34. 'summary' => '一句话摘要。',
  35. 'note' => "## 小节\n\n正文**段落**。",
  36. 'content_type' => 'markdown-e2',
  37. 'quality' => 'standard',
  38. ]);
  39. TermCategory::create(['term_guid' => $term->guid, 'category' => '法义', 'position' => 0]);
  40. $doc = app(TermIndexService::class)->buildDocument($term->guid);
  41. expect($doc['summary']['text'])->toBe('一句话摘要。');
  42. expect($doc['content']['display'])->toContain('一句话摘要。')->toContain('<h2')->toContain('<strong>段落</strong>');
  43. expect($doc['content']['text']['zh'])->toContain('正文段落。')->not->toContain('**');
  44. expect($doc['tags'])->toBe(['category:法义', 'quality:standard']);
  45. expect($doc['path'])->toBe('owner/译文');
  46. expect($doc['metadata']['channel'])->toBe($channel);
  47. });
  48. it('falls back to templates in the note for terms written through v2', function () {
  49. $owner = makeStudio('owner');
  50. $channel = makeChannel($owner);
  51. $term = indexTerm($channel, [
  52. 'owner' => $owner,
  53. 'note' => "{{quality|draft}}\n第一段。\n\n第二段。{{category|律学}} {{category|学处}}",
  54. ]);
  55. $doc = app(TermIndexService::class)->buildDocument($term->guid);
  56. expect($doc['tags'])->toBe(['category:律学', 'category:学处', 'quality:draft']);
  57. expect($doc['summary']['text'])->not->toBe('')->not->toContain('{{');
  58. });