xref: /webtrees/tests/app/Services/GedcomEditServiceTest.php (revision 18e9ae7ea5dfd5b02910bfdedf8a87a54c432a88)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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            /** @var \Fisharebest\Webtrees\Fact $new_fact */
131            $new_fact = $new_facts->get($i);
132            self::assertSame($expected_new_facts[$i], $new_fact->tag());
133        }
134    }
135
136    /**
137     * @dataProvider newIndividualFactsData
138     *
139     * @param string $required_facts
140     * @param string $sex
141     * @param array<string> $names
142     * @param array<string> $expected_new_facts
143     */
144    public function testNewIndividualFactsWithNoFacts(
145        string $required_facts,
146        string $sex,
147        array $names,
148        array $expected_new_facts
149    ): void {
150        $gedcom_edit_service = new GedcomEditService();
151
152        $tree = $this->createMock(Tree::class);
153        $tree->method('getPreference')->with('QUICK_REQUIRED_FACTS')->willReturn($required_facts);
154
155        $new_facts = $gedcom_edit_service->newIndividualFacts($tree, $sex, $names);
156        self::assertSameSize($expected_new_facts, $new_facts);
157        for ($i = 0; $i < count($expected_new_facts); $i++) {
158            /** @var \Fisharebest\Webtrees\Fact $new_fact */
159            $new_fact = $new_facts->get($i);
160            self::assertSame($expected_new_facts[$i], $new_fact->tag());
161        }
162    }
163
164    /**
165     * Data provider for new family facts tests
166     * @return array<array<string|array<string>>>
167     */
168    public function newFamilyFactsData(): array
169    {
170        return [
171            ['', []],
172            ['MARR', ['FAM:MARR']],
173            ['FOOTAG', ['FAM:FOOTAG']],
174            ['MARR,DIV', ['FAM:MARR', 'FAM:DIV']],
175        ];
176    }
177
178    /**
179     * Data provider for new inidvidual facts tests
180     * @return array<array<string|array<string>>>
181     */
182    public function newIndividualFactsData(): array
183    {
184        return [
185            ['', 'F', ['1 NAME FOONAME'], ['INDI:SEX', 'INDI:NAME']],
186            ['BIRT', 'F', ['1 NAME FOONAME'], ['INDI:SEX', 'INDI:NAME', 'INDI:BIRT']],
187            ['FOOTAG', 'F', ['1 NAME FOONAME'], ['INDI:SEX', 'INDI:NAME', 'INDI:FOOTAG']],
188            ['BIRT,DEAT', 'F', ['1 NAME FOONAME'], ['INDI:SEX', 'INDI:NAME', 'INDI:BIRT', 'INDI:DEAT']],
189        ];
190    }
191}
192