SearchPaliWbwRefTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * search-pali-wbw 的资源里要带上 page_numbers 的页码引用。
  4. *
  5. * 每个 (book, paragraph) 在 page_numbers 里可能因为 wid 不同而有多行,
  6. * 输出时每个 type 只保留 wid 最小的那一行,并且只暴露 type / page 两个字段。
  7. */
  8. use Illuminate\Foundation\Testing\RefreshDatabase;
  9. use Illuminate\Support\Facades\DB;
  10. uses(RefreshDatabase::class);
  11. /**
  12. * 造一行能被检索命中的 wbw 词元。
  13. */
  14. function wbwRow(int $paragraph = 1): array
  15. {
  16. return [
  17. 'book' => 1,
  18. 'paragraph' => $paragraph,
  19. 'wid' => 1,
  20. 'word' => 'dhammo',
  21. 'real' => 'dhammo',
  22. 'type' => '',
  23. 'gramma' => '',
  24. 'part' => '',
  25. 'style' => '',
  26. 'pcd_book_id' => 1,
  27. 'weight' => 1,
  28. ];
  29. }
  30. /**
  31. * 造一段 pali_text,让资源能生成标题 / 链接 / 高亮。
  32. */
  33. function paliTextRow(int $paragraph = 1): array
  34. {
  35. return [
  36. 'book' => 1,
  37. 'paragraph' => $paragraph,
  38. 'level' => 1,
  39. 'class' => '',
  40. 'toc' => 'Dhammapada',
  41. 'text' => '',
  42. 'html' => '<p>dhammo</p>',
  43. ];
  44. }
  45. /**
  46. * 造一行 page_numbers。
  47. */
  48. function pageNumberRow(string $type, int $wid, int $page): array
  49. {
  50. return [
  51. 'type' => $type,
  52. 'volume' => 1,
  53. 'page' => $page,
  54. 'book' => 1,
  55. 'paragraph' => 1,
  56. 'wid' => $wid,
  57. 'pcd_book_id' => 1,
  58. ];
  59. }
  60. it('adds ref with the smallest wid page for each type', function () {
  61. DB::table('wbw_templates')->insert([wbwRow()]);
  62. DB::table('pali_texts')->insert([paliTextRow()]);
  63. DB::table('page_numbers')->insert([
  64. pageNumberRow('a', 5, 111),
  65. pageNumberRow('a', 2, 222),
  66. pageNumberRow('a', 9, 333),
  67. pageNumberRow('b', 4, 444),
  68. pageNumberRow('b', 1, 555),
  69. ]);
  70. $response = $this->getJson('/api/v2/search-pali-wbw?key=dhammo')
  71. ->assertOk();
  72. $ref = $response->json('data.rows.0.ref');
  73. expect($ref)->toHaveCount(2);
  74. $byType = collect($ref)->keyBy('type');
  75. expect($byType->get('a'))->toBe(['type' => 'a', 'page' => 222])
  76. ->and($byType->get('b'))->toBe(['type' => 'b', 'page' => 555]);
  77. });
  78. it('omits ref when there are no page_numbers rows', function () {
  79. DB::table('wbw_templates')->insert([wbwRow()]);
  80. DB::table('pali_texts')->insert([paliTextRow()]);
  81. $response = $this->getJson('/api/v2/search-pali-wbw?key=dhammo')
  82. ->assertOk();
  83. expect($response->json('data.rows.0'))->not->toHaveKey('ref');
  84. });