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#[CoversClass(GedcomEditService::class)] 28class GedcomEditServiceTest extends TestCase 29{ 30 protected static bool $uses_database = true; 31 32 public function testEditLinesToGedcom(): void 33 { 34 $gedcom_edit_service = new GedcomEditService(); 35 36 static::assertSame( 37 '1 BIRT Y', 38 $gedcom_edit_service->editLinesToGedcom( 39 'INDI', 40 ['1'], 41 ['BIRT'], 42 ['Y'], 43 false 44 ) 45 ); 46 47 static::assertSame( 48 "\n1 BIRT Y\n2 ADDR England", 49 $gedcom_edit_service->editLinesToGedcom( 50 'INDI', 51 ['1', '2'], 52 ['BIRT', 'ADDR'], 53 ['Y', 'England'] 54 ) 55 ); 56 57 static::assertSame( 58 "\n1 BIRT\n2 PLAC England", 59 $gedcom_edit_service->editLinesToGedcom( 60 'INDI', 61 ['1', '2'], 62 ['BIRT', 'PLAC'], 63 ['Y', 'England'] 64 ) 65 ); 66 67 static::assertSame( 68 "\n1 BIRT\n2 PLAC England\n2 SOUR @S1@\n3 PAGE 123", 69 $gedcom_edit_service->editLinesToGedcom( 70 'INDI', 71 ['1', '2', '2', '3'], 72 ['BIRT', 'PLAC', 'SOUR', 'PAGE'], 73 ['Y', 'England', '@S1@', '123'] 74 ) 75 ); 76 77 // Missing SOUR, so ignore PAGE 78 static::assertSame( 79 "\n1 BIRT\n2 PLAC England", 80 $gedcom_edit_service->editLinesToGedcom( 81 'INDI', 82 ['1', '2', '2', '3'], 83 ['BIRT', 'PLAC', 'SOUR', 'PAGE'], 84 ['Y', 'England', '', '123'] 85 ) 86 ); 87 88 static::assertSame( 89 "\n1 BIRT\n2 PLAC England", 90 $gedcom_edit_service->editLinesToGedcom( 91 'INDI', 92 ['1', '2', '2', '3'], 93 ['BIRT', 'PLAC', 'SOUR', 'PAGE'], 94 ['Y', 'England', '', '123'] 95 ) 96 ); 97 98 static::assertSame( 99 "\n1 BIRT\n2 PLAC England\n1 DEAT\n2 PLAC Scotland", 100 $gedcom_edit_service->editLinesToGedcom( 101 'INDI', 102 ['1', '2', '2', '3', '1', '2', '2', '3'], 103 ['BIRT', 'PLAC', 'SOUR', 'PAGE', 'DEAT', 'PLAC', 'SOUR', 'PAGE'], 104 ['Y', 'England', '', '123', 'Y', 'Scotland', '', '123'] 105 ) 106 ); 107 } 108 109 /** 110 * @param string $required_famfacts 111 * @param array<string> $expected_new_facts 112 */ 113 #[DataProvider('newFamilyFactsData')] 114 public function testNewFamilyFacts(string $required_famfacts, array $expected_new_facts): void 115 { 116 $gedcom_edit_service = new GedcomEditService(); 117 118 $tree = $this->createMock(Tree::class); 119 $tree->method('getPreference')->with('QUICK_REQUIRED_FAMFACTS')->willReturn($required_famfacts); 120 121 $new_facts = $gedcom_edit_service->newFamilyFacts($tree); 122 self::assertSameSize($expected_new_facts, $new_facts); 123 for ($i = 0; $i < count($expected_new_facts); $i++) { 124 $new_fact = $new_facts->get($i); 125 self::assertSame($expected_new_facts[$i], $new_fact->tag()); 126 } 127 } 128 129 /** 130 * @param array<string> $names 131 * @param array<string> $expected_new_facts 132 */ 133 #[DataProvider('newIndividualFactsData')] 134 public function testNewIndividualFactsWithNoFacts( 135 string $required_facts, 136 string $sex, 137 array $names, 138 array $expected_new_facts 139 ): void { 140 $gedcom_edit_service = new GedcomEditService(); 141 142 $tree = $this->createMock(Tree::class); 143 $tree->method('getPreference')->with('QUICK_REQUIRED_FACTS')->willReturn($required_facts); 144 145 $new_facts = $gedcom_edit_service->newIndividualFacts($tree, $sex, $names); 146 self::assertSameSize($expected_new_facts, $new_facts); 147 for ($i = 0; $i < count($expected_new_facts); $i++) { 148 $new_fact = $new_facts->get($i); 149 self::assertSame($expected_new_facts[$i], $new_fact->tag()); 150 } 151 } 152 153 /** 154 * Data provider for new family facts tests 155 * @return array<array<string|array<string>>> 156 */ 157 public static function newFamilyFactsData(): array 158 { 159 return [ 160 ['', []], 161 ['MARR', ['FAM:MARR']], 162 ['FOOTAG', ['FAM:FOOTAG']], 163 ['MARR,DIV', ['FAM:MARR', 'FAM:DIV']], 164 ]; 165 } 166 167 /** 168 * Data provider for new individual facts tests 169 * @return array<array<string|array<string>>> 170 */ 171 public static function newIndividualFactsData(): array 172 { 173 return [ 174 ['', 'F', ['1 NAME FOONAME'], ['INDI:SEX', 'INDI:NAME']], 175 ['BIRT', 'F', ['1 NAME FOONAME'], ['INDI:SEX', 'INDI:NAME', 'INDI:BIRT']], 176 ['FOOTAG', 'F', ['1 NAME FOONAME'], ['INDI:SEX', 'INDI:NAME', 'INDI:FOOTAG']], 177 ['BIRT,DEAT', 'F', ['1 NAME FOONAME'], ['INDI:SEX', 'INDI:NAME', 'INDI:BIRT', 'INDI:DEAT']], 178 ]; 179 } 180} 181