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