SearchPaliWbwRefTest.php 3.7 KB

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