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