SearchPaliWbwRefTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * search-pali-wbw 的资源里要带上页码引用 ref。
  4. *
  5. * 每个 (book, paragraph) 在 page_numbers 里可能因为 wid 不同而有多行,
  6. * 输出时每个 type 只保留 wid 最小的那一行,并且暴露 type / page / title 三个字段;
  7. * type 把单字母代号映射成缩写(M→My、P→PTS、V→VRI、T→Thai、O→Other),
  8. * title 是书缩写:type 为 M 时取 abbr_my、为 P 时取 abbr_pts。
  9. * 另外恒有一条 type='WP' 的条目,page 为段落号、title 为 abbr_wp。
  10. */
  11. use Illuminate\Foundation\Testing\RefreshDatabase;
  12. use Illuminate\Support\Facades\DB;
  13. uses(RefreshDatabase::class);
  14. /**
  15. * 造一行能被检索命中的 wbw 词元。
  16. */
  17. function wbwRow(int $paragraph = 1): array
  18. {
  19. return [
  20. 'book' => 1,
  21. 'paragraph' => $paragraph,
  22. 'wid' => 1,
  23. 'word' => 'dhammo',
  24. 'real' => 'dhammo',
  25. 'type' => '',
  26. 'gramma' => '',
  27. 'part' => '',
  28. 'style' => '',
  29. 'pcd_book_id' => 1,
  30. 'weight' => 1,
  31. ];
  32. }
  33. /**
  34. * 造一段 pali_text,让资源能生成标题 / 链接 / 高亮。
  35. */
  36. function paliTextRow(int $paragraph = 1): array
  37. {
  38. return [
  39. 'book' => 1,
  40. 'paragraph' => $paragraph,
  41. 'level' => 1,
  42. 'class' => '',
  43. 'toc' => 'Dhammapada',
  44. 'text' => '',
  45. 'html' => '<p>dhammo</p>',
  46. ];
  47. }
  48. /**
  49. * 造一行 page_numbers。
  50. */
  51. function pageNumberRow(string $type, int $wid, int $page, int $paragraph = 1): array
  52. {
  53. return [
  54. 'type' => $type,
  55. 'volume' => 1,
  56. 'page' => $page,
  57. 'book' => 1,
  58. 'paragraph' => $paragraph,
  59. 'wid' => $wid,
  60. 'pcd_book_id' => 1,
  61. ];
  62. }
  63. it('adds ref with the smallest wid page for each type', function () {
  64. DB::table('wbw_templates')->insert([wbwRow(10)]);
  65. DB::table('pali_texts')->insert([paliTextRow(10)]);
  66. DB::table('page_numbers')->insert([
  67. pageNumberRow('M', 5, 111, 10),
  68. pageNumberRow('M', 2, 222, 10),
  69. pageNumberRow('M', 9, 333, 10),
  70. pageNumberRow('P', 4, 444, 10),
  71. pageNumberRow('P', 1, 555, 10),
  72. ]);
  73. $response = $this->getJson('/api/v2/search-pali-wbw?key=dhammo')
  74. ->assertOk();
  75. $byType = collect($response->json('data.rows.0.ref'))->keyBy('type');
  76. expect($byType->get('My'))->toBe(['type' => 'My', 'page' => 222, 'title' => 'namakkāra'])
  77. ->and($byType->get('PTS'))->toBe(['type' => 'PTS', 'page' => 555, 'title' => 'Nam'])
  78. ->and($byType->get('WP'))->toBe(['type' => 'WP', 'page' => 10, 'title' => 'namakkāra.']);
  79. });
  80. it('maps all page number types to their abbreviations', function () {
  81. DB::table('wbw_templates')->insert([wbwRow(10)]);
  82. DB::table('pali_texts')->insert([paliTextRow(10)]);
  83. DB::table('page_numbers')->insert([
  84. pageNumberRow('P', 1, 101, 10),
  85. pageNumberRow('M', 2, 102, 10),
  86. pageNumberRow('V', 3, 103, 10),
  87. pageNumberRow('T', 4, 104, 10),
  88. pageNumberRow('O', 5, 105, 10),
  89. ]);
  90. $response = $this->getJson('/api/v2/search-pali-wbw?key=dhammo')
  91. ->assertOk();
  92. $byType = collect($response->json('data.rows.0.ref'))->keyBy('type');
  93. expect($byType->get('PTS'))->toBe(['type' => 'PTS', 'page' => 101, 'title' => 'Nam'])
  94. ->and($byType->get('My'))->toBe(['type' => 'My', 'page' => 102, 'title' => 'namakkāra'])
  95. ->and($byType->get('VRI'))->toBe(['type' => 'VRI', 'page' => 103, 'title' => null])
  96. ->and($byType->get('Thai'))->toBe(['type' => 'Thai', 'page' => 104, 'title' => null])
  97. ->and($byType->get('Other'))->toBe(['type' => 'Other', 'page' => 105, 'title' => null]);
  98. });
  99. it('adds the WP entry even when there are no page_numbers rows', function () {
  100. DB::table('wbw_templates')->insert([wbwRow(10)]);
  101. DB::table('pali_texts')->insert([paliTextRow(10)]);
  102. $response = $this->getJson('/api/v2/search-pali-wbw?key=dhammo')
  103. ->assertOk();
  104. expect($response->json('data.rows.0.ref'))->toBe([
  105. ['type' => 'WP', 'page' => 10, 'title' => 'namakkāra.'],
  106. ]);
  107. });