DiscussionBodyV3Test.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. use App\Models\DhammaTerm;
  3. use App\Models\Discussion;
  4. use Illuminate\Foundation\Testing\RefreshDatabase;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Str;
  7. uses(RefreshDatabase::class);
  8. /**
  9. * discussions 的正文渲染:`body` 是 App\Services\Markup 的节点树,`summary` 是纯文本。
  10. * 只有挂在句子上的才渲染(模板要用句子所在 channel 解析)。
  11. */
  12. function makeBodyTopic(string $owner, string $channel, string $content, array $attrs = []): Discussion
  13. {
  14. $sentence = makeSentence($channel, 1, 1, random_int(1, 100000), 'sentence text');
  15. return Discussion::forceCreate(array_merge([
  16. 'res_type' => 'sentence',
  17. 'res_id' => $sentence->uid,
  18. 'type' => 'discussion',
  19. 'title' => 'topic',
  20. 'content' => $content,
  21. 'editor_uid' => $owner,
  22. ], $attrs));
  23. }
  24. function makeBodyTerm(string $channel, string $owner, string $word, string $meaning): void
  25. {
  26. (new DhammaTerm)->forceFill([
  27. 'id' => random_int(1, PHP_INT_MAX), 'guid' => (string) Str::uuid(), 'word' => $word, 'word_en' => $word,
  28. 'meaning' => $meaning, 'channal' => $channel, 'owner' => $owner, 'language' => 'zh-Hans',
  29. 'editor_id' => 0, 'create_time' => time() * 1000, 'modify_time' => time() * 1000,
  30. ])->save();
  31. }
  32. it('renders the body as a node tree and the summary as text', function () {
  33. $owner = makeStudio('body-owner');
  34. $channel = makeChannel($owner);
  35. makeBodyTerm($channel, $owner, 'dhamma', '法');
  36. $topic = makeBodyTopic($owner, $channel, '关于[[dhamma]]{{n|脚注}}的讨论');
  37. $data = $this->getJson("/api/v3/discussions/{$topic->id}", authHeader($owner))->assertOk()->json('data');
  38. expect($data)->not->toHaveKey('html');
  39. expect($data['body'][0])->toBe(['type' => 'text', 'value' => '关于']);
  40. expect($data['body'][1])->toMatchArray(['type' => 'tpl', 'name' => 'term']);
  41. expect($data['body'][1]['props'])->toMatchArray(['word' => 'dhamma', 'meaning' => '法', 'parentChannelId' => $channel]);
  42. expect($data['body'][2])->toMatchArray(['type' => 'tpl', 'name' => 'note', 'children' => [['type' => 'text', 'value' => '脚注']]]);
  43. expect($data['summary'])->toBe('关于法的讨论');
  44. });
  45. it('does not render discussions that are not on a sentence', function () {
  46. $owner = makeStudio('body-owner2');
  47. $topic = Discussion::forceCreate([
  48. 'res_type' => 'article', 'res_id' => (string) Str::uuid(), 'type' => 'discussion',
  49. 'title' => 't', 'content' => '[[dhamma]]', 'editor_uid' => $owner,
  50. ]);
  51. $data = $this->getJson("/api/v3/discussions/{$topic->id}", authHeader($owner))->assertOk()->json('data');
  52. expect($data)->not->toHaveKey('body')->not->toHaveKey('summary');
  53. });
  54. it('writes v3 content as markdown-e2 and renders backticks as code', function () {
  55. $owner = makeStudio('body-owner-e2');
  56. $channel = makeChannel($owner);
  57. $sentence = makeSentence($channel, 1, 1, 7, 'sentence text');
  58. $data = $this->postJson('/api/v3/discussions', [
  59. 'target_type' => 'sentence', 'target_id' => $sentence->uid, 'type' => 'discussion',
  60. 'title' => 't', 'content' => '用 `{{n|x}}` 写脚注', 'notify' => false,
  61. ], authHeader($owner))->assertCreated()->json('data');
  62. expect($data['content_type'])->toBe('markdown-e2');
  63. expect($data['body'][1])->toMatchArray(['type' => 'el', 'tag' => 'code', 'children' => [['type' => 'text', 'value' => '{{n|x}}']]]);
  64. // v2 / v4 照旧读 content_type
  65. $row = Discussion::find($data['id']);
  66. expect($row->content_type)->toBe('markdown')->and($row->content_type_v2)->toBe('markdown-e2');
  67. $reply = $this->postJson("/api/v3/discussions/{$data['id']}/replies", ['content' => '`code`', 'notify' => false], authHeader($owner))
  68. ->assertCreated()->json('data');
  69. expect($reply['content_type'])->toBe('markdown-e2');
  70. expect($reply['body'][0]['tag'])->toBe('code');
  71. });
  72. it('keeps v2 rows in the legacy dialect until their content is edited through v3', function () {
  73. $owner = makeStudio('body-owner-legacy');
  74. $channel = makeChannel($owner);
  75. $topic = makeBodyTopic($owner, $channel, '旧`脚注`写法');
  76. $data = $this->getJson("/api/v3/discussions/{$topic->id}", authHeader($owner))->assertOk()->json('data');
  77. expect($data['content_type'])->toBe('markdown');
  78. expect($data['body'][1])->toMatchArray(['type' => 'tpl', 'name' => 'note']);
  79. // 只改状态不动正文:方言不变
  80. $this->patchJson("/api/v3/discussions/{$topic->id}", ['status' => 'close'], authHeader($owner))->assertOk()
  81. ->assertJsonPath('data.content_type', 'markdown');
  82. // 经 v3 改了正文:按 E2 存
  83. $patched = $this->patchJson("/api/v3/discussions/{$topic->id}", ['content' => '新`代码`'], authHeader($owner))
  84. ->assertOk()->json('data');
  85. expect($patched['content_type'])->toBe('markdown-e2');
  86. expect($patched['body'][1]['tag'])->toBe('code');
  87. expect($topic->fresh()->content_type)->toBe('markdown');
  88. });
  89. it('looks up the terms of a whole page of discussions together', function () {
  90. $owner = makeStudio('body-owner4');
  91. $channel = makeChannel($owner);
  92. $sentence = makeSentence($channel, 1, 1, 1, 'sentence text');
  93. foreach (range(1, 10) as $i) {
  94. makeBodyTerm($channel, $owner, "w{$i}", "义{$i}");
  95. Discussion::forceCreate([
  96. 'res_type' => 'sentence', 'res_id' => $sentence->uid, 'type' => 'discussion',
  97. 'title' => "t{$i}", 'content' => "[[w{$i}]]", 'editor_uid' => $owner,
  98. ]);
  99. }
  100. DB::enableQueryLog();
  101. $data = $this->getJson("/api/v3/discussions?target_type=sentence&target_id={$sentence->uid}", authHeader($owner))
  102. ->assertOk()->json('data');
  103. $termQueries = collect(DB::getQueryLog())->filter(fn ($q) => str_contains($q['query'], 'from "dhamma_terms"'))->count();
  104. DB::disableQueryLog();
  105. expect(collect($data)->pluck('summary')->sort()->values()->all())->toContain('义1', '义10');
  106. // 本 channel 的术语、studio 术语各一次(没有社区术语 channel 时不查社区)
  107. expect($termQueries)->toBeLessThanOrEqual(3);
  108. });