1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2023 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Services; 21 22use Fisharebest\Webtrees\TestCase; 23use Fisharebest\Webtrees\Tree; 24use PHPUnit\Framework\Attributes\CoversClass; 25use PHPUnit\Framework\Attributes\DataProvider; 26 27 28#[CoversClass(GedcomEditService::class)] 29class GedcomEditServiceTest extends TestCase 30{ 31 protected static bool $uses_database = true; 32 33 public function testEditLinesToGedcom(): void 34 { 35 $gedcom_edit_service = new GedcomEditService(); 36 37 static::assertSame( 38 '1 BIRT Y', 39 $gedcom_edit_service->editLinesToGedcom( 40 'INDI', 41 ['1'], 42 ['BIRT'], 43 ['Y'], 44 false 45 ) 46 ); 47 48 static::assertSame( 49 "\n1 BIRT Y\n2 ADDR England", 50 $gedcom_edit_service->editLinesToGedcom( 51 'INDI', 52 ['1', '2'], 53 ['BIRT', 'ADDR'], 54 ['Y', 'England'] 55 ) 56 ); 57 58 static::assertSame( 59 "\n1 BIRT\n2 PLAC England", 60 $gedcom_edit_service->editLinesToGedcom( 61 'INDI', 62 ['1', '2'], 63 ['BIRT', 'PLAC'], 64 ['Y', 'England'] 65 ) 66 ); 67 68 static::assertSame( 69 "\n1 BIRT\n2 PLAC England\n2 SOUR @S1@\n3 PAGE 123", 70 $gedcom_edit_service->editLinesToGedcom( 71 'INDI', 72 ['1', '2', '2', '3'], 73 ['BIRT', 'PLAC', 'SOUR', 'PAGE'], 74 ['Y', 'England', '@S1@', '123'] 75 ) 76 ); 77 78 // Missing SOUR, so ignore PAGE 79 static::assertSame( 80 "\n1 BIRT\n2 PLAC England", 81 $gedcom_edit_service->editLinesToGedcom( 82 'INDI', 83 ['1', '2', '2', '3'], 84 ['BIRT', 'PLAC', 'SOUR', 'PAGE'], 85 ['Y', 'England', '', '123'] 86 ) 87 ); 88 89 static::assertSame( 90 "\n1 BIRT\n2 PLAC England", 91 $gedcom_edit_service->editLinesToGedcom( 92 'INDI', 93 ['1', '2', '2', '3'], 94 ['BIRT', 'PLAC', 'SOUR', 'PAGE'], 95 ['Y', 'England', '', '123'] 96 ) 97 ); 98 99 static::assertSame( 100 "\n1 BIRT\n2 PLAC England\n1 DEAT\n2 PLAC Scotland", 101 $gedcom_edit_service->editLinesToGedcom( 102 'INDI', 103 ['1', '2', '2', '3', '1', '2', '2', '3'], 104 ['BIRT', 'PLAC', 'SOUR', 'PAGE', 'DEAT', 'PLAC', 'SOUR', 'PAGE'], 105 ['Y', 'England', '', '123', 'Y', 'Scotland', '', '123'] 106 ) 107 ); 108 } 109 110 /** 111 * @param string $required_famfacts 112 * @param array<string> $expected_new_facts 113 */ 114 #[DataProvider('newFamilyFactsData')] 115 public function testNewFamilyFacts(string $required_famfacts, array $expected_new_facts): void 116 { 117 $gedcom_edit_service = new GedcomEditService(); 118 119 $tree = $this->createMock(Tree::class); 120 $tree->method('getPreference')->with('QUICK_REQUIRED_FAMFACTS')->willReturn($required_famfacts); 121 122 $new_facts = $gedcom_edit_service->newFamilyFacts($tree); 123 self::assertSameSize($expected_new_facts, $new_facts); 124 for ($i = 0; $i < count($expected_new_facts); $i++) { 125 $new_fact = $new_facts->get($i); 126 self::assertSame($expected_new_facts[$i], $new_fact->tag()); 127 } 128 } 129 130 /** 131 * @param array<string> $names 132 * @param array<string> $expected_new_facts 133 */ 134 #[DataProvider('newIndividualFactsData')] 135 public function testNewIndividualFactsWithNoFacts( 136 string $required_facts, 137 string $sex, 138 array $names, 139 array $expected_new_facts 140 ): void { 141 $gedcom_edit_service = new GedcomEditService(); 142 143 $tree = $this->createMock(Tree::class); 144 $tree->method('getPreference')->with('QUICK_REQUIRED_FACTS')->willReturn($required_facts); 145 146 $new_facts = $gedcom_edit_service->newIndividualFacts($tree, $sex, $names); 147 self::assertSameSize($expected_new_facts, $new_facts); 148 for ($i = 0; $i < count($expected_new_facts); $i++) { 149 $new_fact = $new_facts->get($i); 150 self::assertSame($expected_new_facts[$i], $new_fact->tag()); 151 } 152 } 153 154 /** 155 * Data provider for new family facts tests 156 * @return array<array<string|array<string>>> 157 */ 158 public static function newFamilyFactsData(): array 159 { 160 return [ 161 ['', []], 162 ['MARR', ['FAM:MARR']], 163 ['FOOTAG', ['FAM:FOOTAG']], 164 ['MARR,DIV', ['FAM:MARR', 'FAM:DIV']], 165 ]; 166 } 167 168 /** 169 * Data provider for new individual facts tests 170 * @return array<array<string|array<string>>> 171 */ 172 public static function newIndividualFactsData(): array 173 { 174 return [ 175 ['', 'F', ['1 NAME FOONAME'], ['INDI:SEX', 'INDI:NAME']], 176 ['BIRT', 'F', ['1 NAME FOONAME'], ['INDI:SEX', 'INDI:NAME', 'INDI:BIRT']], 177 ['FOOTAG', 'F', ['1 NAME FOONAME'], ['INDI:SEX', 'INDI:NAME', 'INDI:FOOTAG']], 178 ['BIRT,DEAT', 'F', ['1 NAME FOONAME'], ['INDI:SEX', 'INDI:NAME', 'INDI:BIRT', 'INDI:DEAT']], 179 ]; 180 } 181} 182