|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature; |
| 4 | + |
| 5 | +use App\Services\PostingAutofillService; |
| 6 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 7 | +use Illuminate\Support\Facades\DB; |
| 8 | +use ReflectionClass; |
| 9 | +use Tests\TestCase; |
| 10 | + |
| 11 | +/** |
| 12 | + * 回歸測試:fuzzyMatchOffice 對泛稱官名「同知」的消歧順序。 |
| 13 | + * |
| 14 | + * 修正前的 bug:「同知」會被 Step 2 的 c_office_chn_alt 匹配誤命中「同知樞密院事」。 |
| 15 | + * 修正方式:在 Step 1(朝代專屬精確匹配)後、Step 2(alt 匹配)前加入 |
| 16 | + * admin_type 消歧,只在朝代專屬精確匹配落空時才將泛稱導向抽象官名。 |
| 17 | + * |
| 18 | + * 這組測試同時保護兩個不變量: |
| 19 | + * 1. 宋代等找不到朝代專屬「同知」記錄時,會根據地址 admin_type 導向 |
| 20 | + * 「同知某府軍府事」/「同知某州軍州事」。 |
| 21 | + * 2. 明清等已有朝代專屬「同知」記錄時,消歧不會蓋掉朝代專屬匹配 |
| 22 | + * (避免 Step 1.5 被人提前到 Step 1 之前)。 |
| 23 | + */ |
| 24 | +class PostingAutofillOfficeMatchTest extends TestCase { |
| 25 | + use RefreshDatabase; |
| 26 | + |
| 27 | + protected function setUp(): void { |
| 28 | + parent::setUp(); |
| 29 | + |
| 30 | + DB::table('DYNASTIES')->insert([ |
| 31 | + ['c_dy' => 15, 'c_dynasty_chn' => '宋', 'c_dynasty' => 'Song', 'c_start' => 960, 'c_end' => 1279, 'c_sort' => 15], |
| 32 | + ['c_dy' => 19, 'c_dynasty_chn' => '明', 'c_dynasty' => 'Ming', 'c_start' => 1368, 'c_end' => 1644, 'c_sort' => 19], |
| 33 | + ['c_dy' => 20, 'c_dynasty_chn' => '清', 'c_dynasty' => 'Qing', 'c_start' => 1644, 'c_end' => 1911, 'c_sort' => 20], |
| 34 | + ]); |
| 35 | + |
| 36 | + DB::table('OFFICE_CODES')->insert([ |
| 37 | + // 宋代:泛稱「同知」會出現在 107 的 alt 清單,用以重現舊版誤匹配。 |
| 38 | + ['c_office_id' => 107, 'c_dy' => 15, 'c_office_chn' => '同知樞密院事', 'c_office_chn_alt' => '同知院事;同知;同知樞'], |
| 39 | + ['c_office_id' => 3301, 'c_dy' => 15, 'c_office_chn' => '同知某州軍州事', 'c_office_chn_alt' => '同知;郡副'], |
| 40 | + ['c_office_id' => 6974, 'c_dy' => 15, 'c_office_chn' => '同知某府軍府事', 'c_office_chn_alt' => null], |
| 41 | + // 明清:朝代專屬的「同知」精確記錄。 |
| 42 | + ['c_office_id' => 70974, 'c_dy' => 19, 'c_office_chn' => '同知', 'c_office_chn_alt' => null], |
| 43 | + ['c_office_id' => 85485, 'c_dy' => 20, 'c_office_chn' => '同知', 'c_office_chn_alt' => '候補同知;候選同知'], |
| 44 | + ]); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * 呼叫 protected fuzzyMatchOffice,避免走完整 HTTP 流程。 |
| 49 | + */ |
| 50 | + protected function matchOffice(string $officeName, ?int $dynastyCode, ?string $adminType): ?array { |
| 51 | + $service = app(PostingAutofillService::class); |
| 52 | + $ref = new ReflectionClass($service); |
| 53 | + $method = $ref->getMethod('fuzzyMatchOffice'); |
| 54 | + $method->setAccessible(true); |
| 55 | + |
| 56 | + return $method->invoke($service, $officeName, $dynastyCode, $adminType); |
| 57 | + } |
| 58 | + |
| 59 | + public function test_song_tongzhi_with_fu_resolves_to_abstract_fu_office() { |
| 60 | + $result = $this->matchOffice('同知', 15, '府'); |
| 61 | + |
| 62 | + $this->assertNotNull($result); |
| 63 | + $this->assertSame(6974, $result['id']); |
| 64 | + $this->assertSame('同知某府軍府事', $result['text']); |
| 65 | + $this->assertSame('exact', $result['match_type']); |
| 66 | + } |
| 67 | + |
| 68 | + public function test_song_tongzhi_with_zhou_resolves_to_abstract_zhou_office() { |
| 69 | + $result = $this->matchOffice('同知', 15, '州'); |
| 70 | + |
| 71 | + $this->assertNotNull($result); |
| 72 | + $this->assertSame(3301, $result['id']); |
| 73 | + $this->assertSame('同知某州軍州事', $result['text']); |
| 74 | + } |
| 75 | + |
| 76 | + public function test_song_tongzhi_with_xian_resolves_to_abstract_zhou_office() { |
| 77 | + $result = $this->matchOffice('同知', 15, '縣'); |
| 78 | + |
| 79 | + $this->assertNotNull($result); |
| 80 | + $this->assertSame(3301, $result['id']); |
| 81 | + } |
| 82 | + |
| 83 | + public function test_ming_tongzhi_with_fu_prefers_dynasty_specific_record() { |
| 84 | + // 明代有朝代專屬「同知」(70974),Step 1 應優先命中, |
| 85 | + // 不得因 admin_type=府 而退化到宋代 6974。 |
| 86 | + $result = $this->matchOffice('同知', 19, '府'); |
| 87 | + |
| 88 | + $this->assertNotNull($result); |
| 89 | + $this->assertSame(70974, $result['id']); |
| 90 | + $this->assertSame('同知', $result['text']); |
| 91 | + } |
| 92 | + |
| 93 | + public function test_qing_tongzhi_with_fu_prefers_dynasty_specific_record() { |
| 94 | + $result = $this->matchOffice('同知', 20, '府'); |
| 95 | + |
| 96 | + $this->assertNotNull($result); |
| 97 | + $this->assertSame(85485, $result['id']); |
| 98 | + } |
| 99 | + |
| 100 | + public function test_song_tongzhi_without_admin_type_does_not_hit_disambiguation() { |
| 101 | + // 沒有 admin_type 時,消歧步驟不應觸發;維持既有(非此次修正範圍的)行為。 |
| 102 | + // 此處僅驗證不會回傳 6974/3301 抽象官名,不對具體命中做斷言。 |
| 103 | + $result = $this->matchOffice('同知', 15, null); |
| 104 | + |
| 105 | + if ($result !== null) { |
| 106 | + $this->assertNotContains($result['id'], [6974, 3301]); |
| 107 | + } |
| 108 | + $this->assertTrue(true); |
| 109 | + } |
| 110 | +} |
0 commit comments