MarkupTemplateDataTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. use App\Models\Article;
  3. use App\Models\BookTitle;
  4. use App\Models\Channel;
  5. use App\Models\DhammaTerm;
  6. use App\Models\Discussion;
  7. use App\Models\PaliText;
  8. use App\Services\Markup\Format;
  9. use App\Services\Markup\MarkupService;
  10. use App\Services\Markup\RenderOptions;
  11. use Illuminate\Foundation\Testing\RefreshDatabase;
  12. use Illuminate\Support\Facades\DB;
  13. use Illuminate\Support\Str;
  14. uses(RefreshDatabase::class);
  15. /**
  16. * App\Services\Markup 里要查库的模板:术语、句子、quote、ql、qa、Tpl、g、ref,以及批量取数、逐段对读、嵌套层数。
  17. */
  18. function markupDataTree(string $source, array $options = []): array
  19. {
  20. return json_decode(json_encode(app(MarkupService::class)->render($source, new RenderOptions(...$options))), true);
  21. }
  22. function markupDataRender(string $source, Format $format, array $options = []): string
  23. {
  24. return app(MarkupService::class)->render($source, new RenderOptions(...array_merge($options, ['format' => $format])));
  25. }
  26. function makeMarkupTerm(array $attrs): DhammaTerm
  27. {
  28. $term = new DhammaTerm;
  29. $term->forceFill(array_merge([
  30. 'id' => random_int(1, PHP_INT_MAX),
  31. 'guid' => (string) Str::uuid(),
  32. 'word_en' => $attrs['word'],
  33. 'meaning' => '',
  34. 'language' => 'zh-Hans',
  35. 'editor_id' => 0,
  36. 'create_time' => time() * 1000,
  37. 'modify_time' => time() * 1000,
  38. ], $attrs))->save();
  39. return $term;
  40. }
  41. /**
  42. * root 名下的系统 channel。
  43. */
  44. function makeSystemChannel(string $name, string $type = 'translation', string $lang = 'en'): string
  45. {
  46. $uid = makeChannel(config('mint.admin.root_uuid'), $name);
  47. Channel::where('uid', $uid)->update(['type' => $type, 'lang' => $lang]);
  48. return $uid;
  49. }
  50. beforeEach(function () {
  51. $this->owner = makeStudio('markup-owner');
  52. $this->channel = makeChannel($this->owner, 'markup channel');
  53. });
  54. describe('term', function () {
  55. it('finds the channel own term', function () {
  56. makeMarkupTerm(['word' => 'dhamma', 'meaning' => '法', 'other_meaning' => '正法', 'channal' => $this->channel, 'owner' => $this->owner]);
  57. $tree = markupDataTree('[[dhamma]]', ['channels' => [$this->channel]]);
  58. expect($tree[0]['props'])->toMatchArray([
  59. 'word' => 'dhamma',
  60. 'meaning' => '法',
  61. 'meaning2' => '正法',
  62. 'channel' => $this->channel,
  63. 'parentChannelId' => $this->channel,
  64. 'parentStudioId' => $this->owner,
  65. ]);
  66. expect($tree[0]['children'])->toBe([['type' => 'text', 'value' => '法(dhamma)']]);
  67. });
  68. it('falls back to the studio term in the same language before the language family', function () {
  69. makeMarkupTerm(['word' => 'sati', 'meaning' => '念-繁', 'language' => 'zh-Hant', 'owner' => $this->owner]);
  70. makeMarkupTerm(['word' => 'sati', 'meaning' => '念', 'language' => 'zh-Hans', 'owner' => $this->owner]);
  71. makeMarkupTerm(['word' => 'sati', 'meaning' => 'mindfulness', 'language' => 'en', 'owner' => $this->owner]);
  72. expect(markupDataTree('[[sati]]', ['channels' => [$this->channel]])[0]['props']['meaning'])->toBe('念');
  73. });
  74. it('falls back to the community term channel', function () {
  75. $community = makeSystemChannel('_community_term_zh-hans_');
  76. makeMarkupTerm(['word' => 'kusala', 'meaning' => '善', 'channal' => $community, 'owner' => config('mint.admin.root_uuid')]);
  77. $props = markupDataTree('{{term|kusala}}', ['channels' => [$this->channel]])[0]['props'];
  78. expect($props)->toMatchArray(['meaning' => '善', 'isCommunity' => true]);
  79. });
  80. it('filters by tag with word@tag and overrides the label with word#text', function () {
  81. makeMarkupTerm(['word' => 'sutta', 'meaning' => '经', 'tag' => 'a', 'channal' => $this->channel, 'owner' => $this->owner]);
  82. makeMarkupTerm(['word' => 'sutta', 'meaning' => '契经', 'tag' => 'b', 'channal' => $this->channel, 'owner' => $this->owner]);
  83. expect(markupDataTree('[[sutta@b]]', ['channels' => [$this->channel]])[0]['props']['meaning'])->toBe('契经');
  84. expect(markupDataTree('[[sutta@a#经文]]', ['channels' => [$this->channel]])[0]['props']['meaning'])->toBe('经文');
  85. });
  86. it('leaves the term note out of the tree but renders it, with its own templates, in markdown', function () {
  87. makeMarkupTerm(['word' => 'magga', 'meaning' => '道', 'note' => '见 [[nibbāna]]', 'channal' => $this->channel, 'owner' => $this->owner]);
  88. makeMarkupTerm(['word' => 'nibbāna', 'meaning' => '涅槃', 'channal' => $this->channel, 'owner' => $this->owner]);
  89. // 前端术语组件自己取词条详情,note 不随正文下发
  90. expect(markupDataTree('[[magga]]', ['channels' => [$this->channel]])[0]['props'])->not->toHaveKey('note');
  91. expect(markupDataRender('[[magga]]', Format::Markdown, ['channels' => [$this->channel], 'footnote' => true]))
  92. ->toBe("道(magga)[^1]\n\n[^1]: 见 涅槃(nibbāna)");
  93. });
  94. it('expands term notes only one level deep', function () {
  95. makeMarkupTerm(['word' => 'a', 'meaning' => '甲', 'note' => '甲注 [[b]]', 'channal' => $this->channel, 'owner' => $this->owner]);
  96. makeMarkupTerm(['word' => 'b', 'meaning' => '乙', 'note' => '乙注 [[c]]', 'channal' => $this->channel, 'owner' => $this->owner]);
  97. makeMarkupTerm(['word' => 'c', 'meaning' => '丙', 'channal' => $this->channel, 'owner' => $this->owner]);
  98. // 甲的 note 里的乙只出译名,不再展开乙注(否则词条互相引用会成倍膨胀)
  99. expect(markupDataRender('[[a]]', Format::Markdown, ['channels' => [$this->channel], 'footnote' => true]))
  100. ->toBe("甲(a)[^1]\n\n[^1]: 甲注 乙(b)");
  101. });
  102. it('renders terms per format', function () {
  103. makeMarkupTerm(['word' => 'dhamma', 'meaning' => '法', 'note' => '注', 'channal' => $this->channel, 'owner' => $this->owner]);
  104. $options = ['channels' => [$this->channel]];
  105. expect(markupDataRender('[[dhamma]]', Format::Text, $options))->toBe('法');
  106. expect(markupDataRender('[[dhamma]]', Format::Html, $options))
  107. ->toMatch('/^<span><span class="term-ref" data-id="[0-9a-f-]{36}" data-term="dhamma">法<\/span><\/span>$/');
  108. expect(markupDataRender('[[dhamma]]与[[dhamma]]', Format::Markdown, $options + ['footnote' => true]))
  109. ->toBe("法(dhamma)[^1]与法\n\n[^1]: 注");
  110. });
  111. it('looks up all terms of a document with a constant number of queries', function () {
  112. $words = [];
  113. foreach (range(1, 30) as $i) {
  114. makeMarkupTerm(['word' => "w{$i}", 'meaning' => "义{$i}", 'channal' => $this->channel, 'owner' => $this->owner]);
  115. $words[] = "[[w{$i}]]";
  116. }
  117. makeSystemChannel('_community_term_zh-hans_');
  118. DB::enableQueryLog();
  119. $tree = markupDataTree(implode(' ', $words), ['channels' => [$this->channel]]);
  120. $queries = count(DB::getQueryLog());
  121. DB::disableQueryLog();
  122. expect(collect($tree)->where('type', 'tpl')->pluck('props.meaning')->all())->toHaveCount(30)->toContain('义30');
  123. // channel、本 channel 术语、studio 术语、社区 channel、社区术语
  124. expect($queries)->toBeLessThanOrEqual(6);
  125. });
  126. });
  127. describe('sent', function () {
  128. beforeEach(function () {
  129. $this->pali = makeSystemChannel('_System_Pali_VRI_', 'original', 'pi');
  130. makeSentence($this->pali, 999, 1, 1, '<span>Evaṃ me sutaṃ</span>');
  131. makeSentence($this->channel, 999, 1, 1, '如是我闻[[dhamma]]');
  132. makeMarkupTerm(['word' => 'dhamma', 'meaning' => '法', 'channal' => $this->channel, 'owner' => $this->owner]);
  133. });
  134. it('renders origin and translation bodies with the row channel', function () {
  135. $props = markupDataTree('{{999-1-1-1}}', ['channels' => [$this->channel]])[0]['props'];
  136. // 只给渲染要用的:坐标在 id 里,原文 / 译文已分组(不给 channel.type),没有义注就不给 commentaries
  137. expect(array_keys($props))->toBe(['id', 'show', 'origin', 'translation']);
  138. expect($props)->toMatchArray(['id' => '999-1-1-1', 'show' => 'both']);
  139. expect($props['origin'][0]['channel'])->toBe(['id' => $this->pali, 'name' => '_System_Pali_VRI_', 'lang' => 'pi']);
  140. expect(array_keys($props['origin'][0]))->toBe(['id', 'channel', 'body']);
  141. expect($props['origin'][0]['body'][0])->toMatchArray(['type' => 'el', 'tag' => 'span']);
  142. expect($props['translation'][0]['body'][1]['name'])->toBe('term');
  143. expect($props['translation'][0]['body'][1]['props']['meaning'])->toBe('法');
  144. expect($props['translation'][0])->not->toHaveKey('content');
  145. });
  146. it('includes the source content in edit mode', function () {
  147. $wbw = makeSystemChannel('_System_Wbw_VRI_', 'wbw', 'pi');
  148. makeSentence($wbw, 999, 1, 1, 'wbw text');
  149. $tree = markupDataTree('{{999-1-1-1}}', ['channels' => [$this->channel], 'mode' => 'edit']);
  150. expect($tree[0]['name'])->toBe('sentedit');
  151. expect($tree[0]['props']['translation'][0]['content'])->toBe('如是我闻[[dhamma]]');
  152. expect($tree[0]['props']['translation'][0]['contentType'])->toBe('markdown');
  153. expect($tree[0]['props']['origin'][0]['channel']['id'])->toBe($wbw);
  154. });
  155. it('reads a single channel with id@channel', function () {
  156. $other = makeChannel($this->owner, 'other');
  157. makeSentence($other, 999, 1, 1, '另一译');
  158. $props = markupDataTree("{{999-1-1-1@{$other}}}", ['channels' => [$this->channel]])[0]['props'];
  159. expect(array_column(array_column($props['translation'], 'channel'), 'id'))->toBe([$other]);
  160. });
  161. it('renders sentences per format and honours text=', function () {
  162. $options = ['channels' => [$this->channel]];
  163. expect(markupDataRender('{{999-1-1-1}}', Format::Html, $options))
  164. ->toBe('<span><span class="sentence"><span class="origin"><span><span>Evaṃ me sutaṃ</span></span></span>'
  165. .'<span class="translation"><span>如是我闻<span class="term-ref" data-id="'
  166. .DhammaTerm::where('word', 'dhamma')->value('guid').'" data-term="dhamma">法</span></span></span></span></span>');
  167. expect(markupDataRender('{{sent|id=999-1-1-1|text=translation}}', Format::Text, $options))->toBe('如是我闻法');
  168. expect(markupDataRender('{{999-1-1-1}}', Format::Simple, $options))->toBe('Evaṃ me sutaṃ如是我闻法');
  169. expect(markupDataRender('{{999-1-1-1}}', Format::Markdown, $options + ['origin' => false]))->toBe('如是我闻法(dhamma)');
  170. });
  171. it('renders a markdown-e2 sentence with code instead of notes', function () {
  172. DB::table('sentences')->where('channel_uid', $this->channel)->update(['content' => '用 `code`', 'content_type' => 'markdown-e2']);
  173. $body = markupDataTree('{{999-1-1-1}}', ['channels' => [$this->channel]])[0]['props']['translation'][0]['body'];
  174. expect($body[1])->toMatchArray(['type' => 'el', 'tag' => 'code']);
  175. });
  176. it('renders sentences inside raw html blocks', function () {
  177. $tree = markupDataTree("<nt>\n{{999-1-1-1}}\n</nt>", ['channels' => [$this->channel]]);
  178. expect($tree[0]['attrs']['data-tag'])->toBe('nt');
  179. $sent = collect($tree[0]['children'])->firstWhere('type', 'tpl');
  180. expect($sent['props']['translation'][0]['body'][0]['value'])->toBe('如是我闻');
  181. });
  182. it('duplicates sentence-only paragraphs in paragraph mode', function () {
  183. $tree = markupDataTree("{{999-1-1-1}}\n\n正文", ['channels' => [$this->channel], 'paragraph' => true]);
  184. expect(array_column($tree, 'tag'))->toBe(['p', 'p', 'p']);
  185. expect($tree[0]['children'][0]['props']['show'])->toBe('origin');
  186. expect($tree[1]['children'][0]['props']['show'])->toBe('translation');
  187. });
  188. it('loads all sentences of a document in one query', function () {
  189. foreach (range(2, 20) as $start) {
  190. makeSentence($this->channel, 999, 1, $start, "句{$start}");
  191. }
  192. $source = implode('', array_map(fn ($start) => "{{999-1-{$start}-{$start}}}", range(1, 20)));
  193. DB::enableQueryLog();
  194. markupDataTree($source, ['channels' => [$this->channel]]);
  195. $sentenceQueries = collect(DB::getQueryLog())->filter(fn ($q) => str_contains($q['query'], 'from "sentences"'))->count();
  196. DB::disableQueryLog();
  197. expect($sentenceQueries)->toBe(1);
  198. });
  199. it('stops nesting content that refers to itself', function () {
  200. DB::table('sentences')->where('channel_uid', $this->channel)->update(['content' => '自引{{999-1-1-1}}']);
  201. $tree = markupDataTree('{{999-1-1-1}}', ['channels' => [$this->channel]]);
  202. $json = json_encode($tree, JSON_UNESCAPED_UNICODE);
  203. // 文章 → 句子正文 → 句子正文,第 3 层不再展开
  204. expect(substr_count($json, '自引'))->toBe(2);
  205. expect($json)->toContain('"body":null');
  206. });
  207. it('lists referenced books with ref', function () {
  208. BookTitle::forceCreate(['book' => 999, 'paragraph' => 0, 'title' => 'dīghanikāya', 'sn' => 1]);
  209. $props = markupDataTree("{{999-1-1-1}}\n\n{{ref}}", ['channels' => [$this->channel]])[1]['children'][0]['props'];
  210. expect($props['pali'])->toBe([['sn' => 1, 'title' => 'dīghanikāya', 'copyright' => 'CSCD V4 VRI 2008']]);
  211. expect(markupDataRender("{{999-1-1-1}}\n\n{{ref}}", Format::Markdown, ['channels' => [$this->channel]]))
  212. ->toEndWith("[1] **Dīghanikāya** CSCD V4 VRI 2008\n\n");
  213. });
  214. });
  215. describe('templates reading other tables', function () {
  216. it('renders quote with the paragraph toc', function () {
  217. (new PaliText)->forceFill([
  218. 'book' => 9001, 'paragraph' => 3, 'level' => 2, 'class' => '', 'toc' => 'Brahmajālasutta',
  219. 'text' => '', 'html' => '', 'path' => json_encode([['title' => 'dīgha']]), 'uid' => (string) Str::uuid(),
  220. ])->save();
  221. expect(markupDataTree('{{quote|9001-3}}', ['channels' => [$this->channel]])[0]['props'])
  222. ->toBe(['paraId' => '9001-3', 'pali' => 'Brahmajālasutta']);
  223. expect(markupDataRender('{{quote|9001-3}}', Format::Text))->toBe('Brahmajālasutta');
  224. expect(markupDataRender('{{quote|9001-404}}', Format::Text))->toBe('9001-404');
  225. });
  226. it('renders qa with the discussion title', function () {
  227. $qa = Discussion::forceCreate([
  228. 'res_type' => 'sentence', 'res_id' => (string) Str::uuid(), 'type' => 'qa',
  229. 'title' => '为什么?', 'content' => 'body', 'editor_uid' => $this->owner,
  230. ]);
  231. $props = markupDataTree("{{qa|{$qa->id}}}")[0]['props'];
  232. expect($props)->toBe(['id' => $qa->id, 'title' => '为什么?', 'resType' => 'sentence']);
  233. });
  234. it('renders Tpl articles with mustache params', function () {
  235. (new Article)->forceFill([
  236. 'id' => random_int(1, PHP_INT_MAX), 'uid' => (string) Str::uuid(), 'title' => 'Tpl:greet',
  237. 'content' => 'Hello **{{1}}** from {{place}}', 'owner' => $this->owner, 'owner_id' => 0, 'editor_id' => 0,
  238. 'create_time' => time() * 1000, 'modify_time' => time() * 1000,
  239. ])->save();
  240. $tree = markupDataTree('{{Tpl:greet|Ānanda|place=Sāvatthī}}');
  241. expect($tree[0])->toMatchArray(['name' => 'tpl', 'props' => ['name' => 'Tpl:greet']]);
  242. expect($tree[0]['children'][1])->toMatchArray(['tag' => 'strong']);
  243. expect(markupDataRender('{{Tpl:greet|Ānanda|place=Sāvatthī}}', Format::Text))->toBe('Hello Ānanda from Sāvatthī');
  244. expect(markupDataTree('{{Tpl:missing}}')[0]['children'])->toBe([]);
  245. });
  246. it('renders grammar lookups from the system grammar channel', function () {
  247. $grammar = makeSystemChannel('_System_Grammar_Term_en_');
  248. makeMarkupTerm(['word' => 'nom', 'meaning' => 'nominative', 'channal' => $grammar, 'owner' => config('mint.admin.root_uuid')]);
  249. $props = markupDataTree('{{g|nom}}', ['channels' => [$this->channel]])[0]['props'];
  250. expect($props['word'])->toBe('nom');
  251. expect($props['term'])->toMatchArray(['meaning' => 'nominative', 'channel' => $grammar]);
  252. });
  253. it('renders quote-link with a localized book name', function () {
  254. makeMarkupTerm(['word' => 'dīghanikāya', 'meaning' => '长部', 'tag' => ':quote:', 'channal' => $this->channel, 'owner' => $this->owner]);
  255. $tree = markupDataTree('{{ql|type=m|bookname=dīghanikāya|volume=1|page=23}}', ['channels' => [$this->channel]]);
  256. expect($tree[0]['name'])->toBe('quote-link');
  257. expect($tree[0]['props'])->toMatchArray(['type' => 'm', 'bookName' => 'dīghanikāya', 'volume' => 1, 'page' => '23']);
  258. expect($tree[0]['props'])->not->toHaveKey('bookNameLocal');
  259. expect($tree[0]['props']['term']['meaning'])->toBe('长部');
  260. expect(markupDataRender('{{ql|type=m|bookname=dīghanikāya|volume=1|page=23}}', Format::Text, ['channels' => [$this->channel]]))
  261. ->toBe('长部 1.23');
  262. });
  263. it('renders nissaya with the channel language', function () {
  264. Channel::where('uid', $this->channel)->update(['lang' => 'my']);
  265. expect(markupDataTree('{{nissaya|evaṃ|ဤသို့}}', ['channels' => [$this->channel]])[0]['props'])->toBe(['pali' => 'evaṃ', 'meaning' => ['ဤသို့']]);
  266. expect(markupDataRender('{{nissaya|evaṃ|ဤသို့}}', Format::Html, ['channels' => [$this->channel]]))
  267. ->toBe('<span><span lang="pi-Mymr">evaṃ</span>၊ <span lang="my">ဤသို့</span></span>');
  268. });
  269. });