TipitakaReadingRenderTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. use App\Facades\RedisCache;
  3. use App\Models\PaliText;
  4. use App\Models\Sentence;
  5. use App\Services\PaliContentService;
  6. use Illuminate\Foundation\Testing\RefreshDatabase;
  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-reading/{$channel}?book=9001&para=1")
  23. ->assertOk()
  24. ->json('data.0');
  25. expect($data['para'])->toBe(1);
  26. // 默认只输出 display
  27. expect($data)->not->toHaveKey('sentences');
  28. expect($data['display'])
  29. // 段落 class 后面拼 pali_texts.class,这里为空,所以允许末尾空白
  30. ->toMatch("/<div id='para-1' class='translation\\s*' data-para='1'>/")
  31. ->toContain("<div class='sentence' lang='zh-Hans' data-sid='9001-1-1-1'>")
  32. ->toContain("<div class='sentence' lang='zh-Hans' data-sid='9001-1-2-2'>")
  33. ->toContain("<div class='para-block'>");
  34. });
  35. it('wraps a chapter title paragraph in a heading', function () {
  36. $channel = makeParagraphFixture();
  37. (new PaliText)->forceFill([
  38. 'book' => 9001,
  39. 'paragraph' => 1,
  40. 'level' => 2,
  41. 'class' => '',
  42. 'toc' => '',
  43. 'text' => '',
  44. 'html' => '',
  45. 'pcd_book_id' => 0,
  46. 'uid' => (string) Str::uuid(),
  47. ])->save();
  48. $display = $this->getJson("/api/v3/tipitaka-reading/{$channel}?book=9001&para=1")
  49. ->assertOk()
  50. ->json('data.0.display');
  51. expect($display)->toContain('<h2>')->not->toContain('para-block');
  52. });
  53. it('can output only the sentences or both', function () {
  54. $channel = makeParagraphFixture();
  55. $sentences = $this->getJson("/api/v3/tipitaka-reading/{$channel}?book=9001&para=1&include=sentences")
  56. ->assertOk()
  57. ->json('data.0');
  58. expect($sentences)->not->toHaveKey('display');
  59. expect($sentences['sentences'])->toHaveCount(2);
  60. expect($sentences['sentences'][0])->toHaveKeys(['sid', 'html']);
  61. $all = $this->getJson("/api/v3/tipitaka-reading/{$channel}?book=9001&para=1&include=all")
  62. ->assertOk()
  63. ->json('data.0');
  64. expect($all)->toHaveKeys(['para', 'display', 'sentences']);
  65. });
  66. it('lists the paragraphs of a range and skips empty ones', function () {
  67. $channel = makeParagraphFixture();
  68. $items = $this->getJson("/api/v3/tipitaka-reading/{$channel}?book=9001&para=1&to=3")
  69. ->assertOk()
  70. ->json('data');
  71. expect(array_column($items, 'para'))->toBe([1, 2]);
  72. });
  73. it('outputs one line per sentence for non html formats', function () {
  74. $channel = makeParagraphFixture();
  75. $display = $this->getJson("/api/v3/tipitaka-reading/{$channel}?book=9001&para=1&format=text")
  76. ->assertOk()
  77. ->json('data.0.display');
  78. expect($display)->toBe("first sentence\nsecond sentence");
  79. });
  80. it('rejects a malformed channel in the path but validates filters as 422', function () {
  81. $channel = makeParagraphFixture();
  82. // 路径参数由路由约束把关:不是 uuid 就匹配不上任何路由 → 404
  83. $this->getJson('/api/v3/tipitaka-reading/not-a-uuid')->assertStatus(404);
  84. // filter 是查询参数,走 FormRequest → 422
  85. $this->getJson("/api/v3/tipitaka-reading/{$channel}?book=9001&para=not-a-number")
  86. ->assertStatus(422)
  87. ->assertJsonStructure(['errors' => ['para']]);
  88. $this->getJson("/api/v3/tipitaka-reading/{$channel}?book=not-a-number")
  89. ->assertStatus(422)
  90. ->assertJsonStructure(['errors' => ['book']]);
  91. // book 只在跟 chapter / para / to 一起用时必填
  92. $this->getJson("/api/v3/tipitaka-reading/{$channel}?para=1")
  93. ->assertStatus(422)
  94. ->assertJsonStructure(['errors' => ['book']]);
  95. // chapter 与 para 互斥
  96. $this->getJson("/api/v3/tipitaka-reading/{$channel}?book=9001&chapter=1&para=1")
  97. ->assertStatus(422);
  98. });
  99. it('fetches the whole channel when no filter is given', function () {
  100. $channel = makeParagraphFixture();
  101. // 不带任何查询串 = 取这个 channel 的全部译文,下载场景要的就是这个
  102. $json = $this->getJson("/api/v3/tipitaka-reading/{$channel}")->assertOk()->json();
  103. expect(array_column($json['data'], 'para'))->toBe([1, 2])
  104. ->and($json['data'][0]['book'])->toBe(9001)
  105. // 无 book 过滤时不给 total / remaining,靠 next_cursor 判断结束
  106. ->and($json['meta'])->not->toHaveKey('total')
  107. ->and($json['meta']['next_cursor'])->toBeNull();
  108. });
  109. it('returns an empty collection for a paragraph with no content', function () {
  110. $channel = makeParagraphFixture();
  111. // 这里是集合端点,没内容就是空集合而不是 404——区间口径下空段落本来就被跳过,
  112. // 单段只是区间长度为 1 的特例,两者口径要一致
  113. $json = $this->getJson("/api/v3/tipitaka-reading/{$channel}?book=9001&para=9")
  114. ->assertOk()
  115. ->json();
  116. expect($json['data'])->toBe([])
  117. ->and($json['meta'])->toMatchArray(['total' => 0, 'remaining' => 0])
  118. ->and($json['meta']['next_cursor'])->toBeNull();
  119. });
  120. it('rejects a range that ends before it starts', function () {
  121. $channel = makeParagraphFixture();
  122. $this->getJson("/api/v3/tipitaka-reading/{$channel}?book=9001&para=3&to=1")
  123. ->assertStatus(422)
  124. ->assertJsonPath('errors.to.0', __('site.invalid_parameter'));
  125. });
  126. it('caches the paragraph and drops the cache when a sentence changes', function () {
  127. $channel = makeParagraphFixture();
  128. $url = "/api/v3/tipitaka-reading/{$channel}?book=9001&para=1";
  129. $this->getJson($url)->assertOk();
  130. $key = PaliContentService::paragraphCacheKey(9001, 1, $channel, 'html');
  131. expect(RedisCache::has($key))->toBeTrue();
  132. $sentence = Sentence::where('book_id', 9001)->where('paragraph', 1)->orderBy('word_start')->first();
  133. $sentence->content = 'changed sentence';
  134. $sentence->save();
  135. expect(RedisCache::has($key))->toBeFalse();
  136. expect($this->getJson($url)->json('data.0.display'))->toContain('changed sentence');
  137. });
  138. it('drops the cache when a sentence is added or deleted', function () {
  139. $channel = makeParagraphFixture();
  140. $url = "/api/v3/tipitaka-reading/{$channel}?book=9001&para=1";
  141. $this->getJson($url)->assertOk();
  142. makeSentence($channel, 9001, 1, 3, 'third sentence');
  143. expect($this->getJson($url.'&include=sentences')->json('data.0.sentences'))->toHaveCount(3);
  144. Sentence::where('book_id', 9001)->where('paragraph', 1)->where('word_start', 3)->first()->delete();
  145. expect($this->getJson($url.'&include=sentences')->json('data.0.sentences'))->toHaveCount(2);
  146. });