TipitakaContentParaTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. * 建一个句子,返回模型
  11. */
  12. function makeSentence(string $channelUid, int $book, int $para, int $wordStart, string $content): Sentence
  13. {
  14. $sentence = new Sentence;
  15. $sentence->forceFill([
  16. // sentences.id 不是自增列,必须显式给值
  17. 'id' => random_int(1, PHP_INT_MAX),
  18. 'uid' => (string) Str::uuid(),
  19. 'book_id' => $book,
  20. 'paragraph' => $para,
  21. 'word_start' => $wordStart,
  22. 'word_end' => $wordStart,
  23. 'channel_uid' => $channelUid,
  24. 'editor_uid' => (string) Str::uuid(),
  25. 'content' => $content,
  26. 'content_type' => 'markdown',
  27. 'strlen' => mb_strlen($content),
  28. 'status' => 30,
  29. 'create_time' => time() * 1000,
  30. 'modify_time' => time() * 1000,
  31. 'language' => 'zh-Hans',
  32. ])->save();
  33. return $sentence;
  34. }
  35. beforeEach(function () {
  36. $this->channel = makeChannel(makeStudio('para-owner'), 'para channel');
  37. makeSentence($this->channel, 9001, 1, 1, 'first sentence');
  38. makeSentence($this->channel, 9001, 1, 2, 'second sentence');
  39. makeSentence($this->channel, 9001, 2, 1, 'other paragraph');
  40. });
  41. it('renders every sentence of a paragraph wrapped in divs', function () {
  42. $data = $this->getJson("/api/v2/tipitaka-content-para/9001-1?channel={$this->channel}")
  43. ->assertOk()
  44. ->json('data');
  45. expect($data['para'])->toBe(1);
  46. expect($data['sentences'])->toHaveCount(2);
  47. expect($data['display'])
  48. ->toContain("<div class='translation' data-para='1'>")
  49. ->toContain("<div class='sentence' data-sid='9001-1-1-1'>")
  50. ->toContain("<div class='sentence' data-sid='9001-1-2-2'>")
  51. ->toContain("<div class='para-block'>");
  52. });
  53. it('wraps a chapter title paragraph in a heading', function () {
  54. (new PaliText)->forceFill([
  55. 'book' => 9001,
  56. 'paragraph' => 1,
  57. 'level' => 2,
  58. 'class' => '',
  59. 'toc' => '',
  60. 'text' => '',
  61. 'html' => '',
  62. 'pcd_book_id' => 0,
  63. 'uid' => (string) Str::uuid(),
  64. ])->save();
  65. $display = $this->getJson("/api/v2/tipitaka-content-para/9001-1?channel={$this->channel}")
  66. ->assertOk()
  67. ->json('data.display');
  68. expect($display)->toContain('<h2>')->not->toContain('para-block');
  69. });
  70. it('lists the paragraphs of a range and skips empty ones', function () {
  71. $items = $this->getJson("/api/v2/tipitaka-content-para?book=9001&para=1&to=3&channel={$this->channel}")
  72. ->assertOk()
  73. ->json('data.items');
  74. expect(array_column($items, 'para'))->toBe([1, 2]);
  75. });
  76. it('outputs one line per sentence for non html formats', function () {
  77. $display = $this->getJson("/api/v2/tipitaka-content-para/9001-1?channel={$this->channel}&format=text")
  78. ->assertOk()
  79. ->json('data.display');
  80. expect($display)->toBe("first sentence\nsecond sentence");
  81. });
  82. it('rejects an invalid id or channel', function () {
  83. $this->getJson("/api/v2/tipitaka-content-para/bad-id?channel={$this->channel}")
  84. ->assertJsonPath('ok', false);
  85. $this->getJson('/api/v2/tipitaka-content-para/9001-1?channel=not-a-uuid')
  86. ->assertJsonPath('ok', false);
  87. $this->getJson("/api/v2/tipitaka-content-para/9001-9?channel={$this->channel}")
  88. ->assertJsonPath('ok', false);
  89. });
  90. it('caches the paragraph and drops the cache when a sentence changes', function () {
  91. $url = "/api/v2/tipitaka-content-para/9001-1?channel={$this->channel}";
  92. $this->getJson($url)->assertOk();
  93. $tag = PaliContentService::paragraphCacheTag(9001, 1, $this->channel);
  94. $key = PaliContentService::paragraphCacheKey(9001, 1, $this->channel, 'html');
  95. expect(Cache::tags([$tag])->has($key))->toBeTrue();
  96. $sentence = Sentence::where('book_id', 9001)->where('paragraph', 1)->orderBy('word_start')->first();
  97. $sentence->content = 'changed sentence';
  98. $sentence->save();
  99. expect(Cache::tags([$tag])->has($key))->toBeFalse();
  100. expect($this->getJson($url)->json('data.display'))->toContain('changed sentence');
  101. });
  102. it('drops the cache when a sentence is added or deleted', function () {
  103. $url = "/api/v2/tipitaka-content-para/9001-1?channel={$this->channel}";
  104. $this->getJson($url)->assertOk();
  105. makeSentence($this->channel, 9001, 1, 3, 'third sentence');
  106. expect($this->getJson($url)->json('data.sentences'))->toHaveCount(3);
  107. Sentence::where('book_id', 9001)->where('paragraph', 1)->where('word_start', 3)->first()->delete();
  108. expect($this->getJson($url)->json('data.sentences'))->toHaveCount(2);
  109. });