TipitakaReadParaTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. use App\Models\PaliText;
  3. use App\Models\Sentence;
  4. use App\Services\PaliContentService;
  5. use Illuminate\Foundation\Testing\RefreshDatabase;
  6. use Illuminate\Support\Facades\Cache;
  7. use Illuminate\Support\Str;
  8. uses(RefreshDatabase::class);
  9. /**
  10. * 建测试用的 channel 和句子:book 9001 的第 1 段两句,第 2 段一句。返回 channel uid
  11. */
  12. function makeParagraphFixture(): string
  13. {
  14. $channel = makeChannel(makeStudio('para-owner'), 'para channel');
  15. makeSentence($channel, 9001, 1, 1, 'first sentence');
  16. makeSentence($channel, 9001, 1, 2, 'second sentence');
  17. makeSentence($channel, 9001, 2, 1, 'other paragraph');
  18. return $channel;
  19. }
  20. it('renders every sentence of a paragraph wrapped in divs', function () {
  21. $channel = makeParagraphFixture();
  22. $data = $this->getJson("/api/v3/tipitaka-read-para/9001-1?channel={$channel}")
  23. ->assertOk()
  24. ->json('data');
  25. expect($data['para'])->toBe(1);
  26. // 默认只输出 display
  27. expect($data)->not->toHaveKey('sentences');
  28. expect($data['display'])
  29. ->toContain("<div id='para-1' class='translation' data-para='1'>")
  30. ->toContain("<div class='sentence' data-sid='9001-1-1-1'>")
  31. ->toContain("<div class='sentence' data-sid='9001-1-2-2'>")
  32. ->toContain("<div class='para-block'>");
  33. });
  34. it('wraps a chapter title paragraph in a heading', function () {
  35. $channel = makeParagraphFixture();
  36. (new PaliText)->forceFill([
  37. 'book' => 9001,
  38. 'paragraph' => 1,
  39. 'level' => 2,
  40. 'class' => '',
  41. 'toc' => '',
  42. 'text' => '',
  43. 'html' => '',
  44. 'pcd_book_id' => 0,
  45. 'uid' => (string) Str::uuid(),
  46. ])->save();
  47. $display = $this->getJson("/api/v3/tipitaka-read-para/9001-1?channel={$channel}")
  48. ->assertOk()
  49. ->json('data.display');
  50. expect($display)->toContain('<h2>')->not->toContain('para-block');
  51. });
  52. it('can output only the sentences or both', function () {
  53. $channel = makeParagraphFixture();
  54. $sentences = $this->getJson("/api/v3/tipitaka-read-para/9001-1?channel={$channel}&view=sentences")
  55. ->assertOk()
  56. ->json('data');
  57. expect($sentences)->not->toHaveKey('display');
  58. expect($sentences['sentences'])->toHaveCount(2);
  59. expect($sentences['sentences'][0])->toHaveKeys(['sid', 'html']);
  60. $all = $this->getJson("/api/v3/tipitaka-read-para/9001-1?channel={$channel}&view=all")
  61. ->assertOk()
  62. ->json('data');
  63. expect($all)->toHaveKeys(['para', 'display', 'sentences']);
  64. $items = $this->getJson("/api/v3/tipitaka-read-para?book=9001&para=1&channel={$channel}&view=sentences")
  65. ->assertOk()
  66. ->json('data.items');
  67. expect($items[0])->not->toHaveKey('display');
  68. expect($items[0]['sentences'])->toHaveCount(2);
  69. });
  70. it('lists the paragraphs of a range and skips empty ones', function () {
  71. $channel = makeParagraphFixture();
  72. $items = $this->getJson("/api/v3/tipitaka-read-para?book=9001&para=1&to=3&channel={$channel}")
  73. ->assertOk()
  74. ->json('data.items');
  75. expect(array_column($items, 'para'))->toBe([1, 2]);
  76. });
  77. it('outputs one line per sentence for non html formats', function () {
  78. $channel = makeParagraphFixture();
  79. $display = $this->getJson("/api/v3/tipitaka-read-para/9001-1?channel={$channel}&format=text")
  80. ->assertOk()
  81. ->json('data.display');
  82. expect($display)->toBe("first sentence\nsecond sentence");
  83. });
  84. it('rejects an invalid id or channel', function () {
  85. $channel = makeParagraphFixture();
  86. $this->getJson("/api/v3/tipitaka-read-para/bad-id?channel={$channel}")
  87. ->assertJsonPath('ok', false);
  88. $this->getJson('/api/v3/tipitaka-read-para/9001-1?channel=not-a-uuid')
  89. ->assertJsonPath('ok', false);
  90. $this->getJson("/api/v3/tipitaka-read-para/9001-9?channel={$channel}")
  91. ->assertJsonPath('ok', false);
  92. });
  93. it('caches the paragraph and drops the cache when a sentence changes', function () {
  94. $channel = makeParagraphFixture();
  95. $url = "/api/v3/tipitaka-read-para/9001-1?channel={$channel}";
  96. $this->getJson($url)->assertOk();
  97. $tag = PaliContentService::paragraphCacheTag(9001, 1, $channel);
  98. $key = PaliContentService::paragraphCacheKey(9001, 1, $channel, 'html');
  99. expect(Cache::tags([$tag])->has($key))->toBeTrue();
  100. $sentence = Sentence::where('book_id', 9001)->where('paragraph', 1)->orderBy('word_start')->first();
  101. $sentence->content = 'changed sentence';
  102. $sentence->save();
  103. expect(Cache::tags([$tag])->has($key))->toBeFalse();
  104. expect($this->getJson($url)->json('data.display'))->toContain('changed sentence');
  105. });
  106. it('drops the cache when a sentence is added or deleted', function () {
  107. $channel = makeParagraphFixture();
  108. $url = "/api/v3/tipitaka-read-para/9001-1?channel={$channel}";
  109. $this->getJson($url)->assertOk();
  110. makeSentence($channel, 9001, 1, 3, 'third sentence');
  111. expect($this->getJson($url.'&view=sentences')->json('data.sentences'))->toHaveCount(3);
  112. Sentence::where('book_id', 9001)->where('paragraph', 1)->where('word_start', 3)->first()->delete();
  113. expect($this->getJson($url.'&view=sentences')->json('data.sentences'))->toHaveCount(2);
  114. });