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