WikiPageTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. use App\Models\BookTitle;
  3. use App\Models\Channel;
  4. use App\Models\DhammaTerm;
  5. use App\Models\TermCategory;
  6. use App\Services\OpenSearchService;
  7. use Illuminate\Foundation\Testing\RefreshDatabase;
  8. use Illuminate\Support\Str;
  9. uses(RefreshDatabase::class);
  10. /*
  11. * library 百科页(/library/wiki/*)直接查库:分类取 term_categories,质量取 dhamma_terms.quality。
  12. * OpenSearch 换成一调用就失败的替身,确保这些页面不再依赖它。
  13. */
  14. beforeEach(function () {
  15. $this->app->instance(OpenSearchService::class, Mockery::mock(OpenSearchService::class, function ($mock) {
  16. $mock->shouldReceive('search', 'get')->never();
  17. }));
  18. });
  19. /** 直接写一条术语(带分类),返回 guid */
  20. function wikiTerm(string $channel, string $word, array $attrs = [], array $categories = []): string
  21. {
  22. $guid = (string) Str::uuid();
  23. (new DhammaTerm)->forceFill($attrs + [
  24. 'id' => random_int(1, PHP_INT_MAX),
  25. 'guid' => $guid,
  26. 'word' => $word,
  27. 'word_en' => $word,
  28. 'meaning' => "{$word}-义",
  29. 'note' => "{$word} 的正文。",
  30. 'channal' => $channel,
  31. 'language' => 'zh-Hans',
  32. 'owner' => Channel::where('uid', $channel)->value('owner_uid'),
  33. 'editor_id' => 0,
  34. 'content_type' => 'markdown-e2',
  35. 'create_time' => time() * 1000,
  36. 'modify_time' => time() * 1000,
  37. ])->save();
  38. foreach ($categories as $i => $category) {
  39. TermCategory::create(['term_guid' => $guid, 'category' => $category, 'position' => $i]);
  40. }
  41. return $guid;
  42. }
  43. it('lists the terms of a category and its tags, filtered by quality, public channels only', function () {
  44. $owner = makeStudio('owner');
  45. $public = makeChannel($owner, 'public');
  46. $private = makeChannel($owner, 'private');
  47. Channel::where('uid', $private)->update(['status' => Channel::STATUS_PRIVATE]);
  48. wikiTerm($public, 'by-sub-label', ['quality' => 'standard'], ['戒条罪类']);
  49. wikiTerm($public, 'by-tag', ['quality' => 'draft'], ['学处']);
  50. wikiTerm($public, 'pending-one', [], ['学处']);
  51. wikiTerm($private, 'zzprivateterm', ['quality' => 'featured'], ['学处']);
  52. wikiTerm($public, 'zzothercategory', ['quality' => 'featured'], ['禅修']);
  53. $html = $this->get('/library/wiki/zh-Hans?category=vinaya&quality=pending')->assertOk()->getContent();
  54. expect($html)->toContain('by-sub-label')->toContain('by-tag')->toContain('pending-one')
  55. ->not->toContain('zzprivateterm')->not->toContain('zzothercategory');
  56. $html = $this->get('/library/wiki/zh-Hans?category=vinaya&quality=draft')->assertOk()->getContent();
  57. expect($html)->toContain('by-sub-label')->toContain('by-tag')->not->toContain('pending-one');
  58. });
  59. it('shows a term by word or id with its categories and rendered note', function () {
  60. $owner = makeStudio('owner');
  61. $channel = makeChannel($owner);
  62. $guid = wikiTerm($channel, 'dhamma', ['quality' => 'featured', 'summary' => '一句话摘要。', 'note' => "## 小节\n\n正文段落。"], ['法义']);
  63. wikiTerm($channel, 'sīla', [], ['法义']);
  64. foreach (['dhamma', $guid] as $key) {
  65. $html = $this->get("/library/wiki/zh-Hans/{$key}")->assertOk()->getContent();
  66. expect($html)->toContain('一句话摘要。')->toContain('小节')->toContain('正文段落。')->toContain('法义')
  67. ->toContain('sīla'); // 相关条目:共享分类
  68. }
  69. });
  70. it('renders categories in the note as links to the category list, and lists terms by category name', function () {
  71. $owner = makeStudio('owner');
  72. $channel = makeChannel($owner);
  73. $guid = wikiTerm($channel, 'vinaya', ['note' => "正文。\n\n{{category|律学}} {{category|学处}}", 'quality' => 'standard'], ['律学', '学处']);
  74. wikiTerm($channel, 'zzothercat', ['quality' => 'standard'], ['禅修']);
  75. $html = $this->get("/library/wiki/zh-Hans/{$guid}")->assertOk()->getContent();
  76. $url = e(route('library.wiki.index', ['lang' => 'zh-Hans', 'category' => '学处']));
  77. expect($html)->toContain('<a class="tpl-category" href="'.$url.'">学处</a>')
  78. ->not->toContain('class="wiki-tags"');
  79. // 分类名(不是分类树里的 id)也能列出词条
  80. $list = $this->get('/library/wiki/zh-Hans?category='.urlencode('学处').'&quality=pending')->assertOk()->getContent();
  81. expect($list)->toContain('vinaya')->not->toContain('zzothercat');
  82. });
  83. it('puts each reference of {{ref}} on its own line', function () {
  84. $owner = makeStudio('owner');
  85. $channel = makeChannel($owner);
  86. // 文献名取自 book_titles:两处引用落在两本书里,各成一条
  87. BookTitle::query()->insert([
  88. ['book' => 207, 'paragraph' => 1, 'title' => 'kaṅkhāvitaraṇī', 'sn' => 0],
  89. ['book' => 200, 'paragraph' => 1, 'title' => 'vinayavinicchayo', 'sn' => 0],
  90. ]);
  91. $guid = wikiTerm($channel, 'refs', ['note' => "甲。{{para|id=207-1331|title=a 1331|style=reference}}乙。{{para|id=200-9955|title=b 9955|style=reference}}\n\n## 参考文献\n\n{{ref}}"]);
  92. $html = $this->get("/library/wiki/zh-Hans/{$guid}")->assertOk()->getContent();
  93. expect(substr_count($html, 'class="tpl-reference-item"'))->toBe(2)
  94. ->and($html)->toContain('</span><br /><span class="tpl-reference-item">');
  95. });
  96. it('follows a redirect to the target term in the same channel', function () {
  97. $owner = makeStudio('owner');
  98. $channel = makeChannel($owner);
  99. $target = wikiTerm($channel, 'nibbāna', ['tag' => '涅槃']);
  100. wikiTerm($channel, 'nirvāṇa', ['redirect' => 'nibbāna@涅槃']);
  101. $this->get('/library/wiki/zh-Hans/nirv%C4%81%E1%B9%87a')->assertRedirect(route('library.wiki.show', ['zh-Hans', $target]));
  102. });
  103. it('hides terms of private channels and unknown words', function () {
  104. $owner = makeStudio('owner');
  105. $private = makeChannel($owner);
  106. Channel::where('uid', $private)->update(['status' => Channel::STATUS_PRIVATE]);
  107. $guid = wikiTerm($private, 'secret');
  108. $this->get("/library/wiki/zh-Hans/{$guid}")->assertNotFound();
  109. $this->get('/library/wiki/zh-Hans/secret')->assertNotFound();
  110. $this->get('/library/wiki/zh-Hans/no-such-word')->assertNotFound();
  111. });