TipitakaReadingRenderTest.php 7.1 KB

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