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