xref: /webtrees/tests/app/Module/FixDuplicateLinksTest.php (revision 213b90b0705463be9294b5a4cb346e84f8da1e13)
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\Module;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\Services\DataFixService;
24use Fisharebest\Webtrees\Services\GedcomImportService;
25use Fisharebest\Webtrees\Services\TreeService;
26use Fisharebest\Webtrees\Session;
27use Fisharebest\Webtrees\TestCase;
28use Fisharebest\Webtrees\Tree;
29use Illuminate\Support\Collection;
30
31/**
32 * Class FixDuplicateLinksTest.
33 *
34 * @covers \Fisharebest\Webtrees\Module\FixDuplicateLinks
35 * @covers \Fisharebest\Webtrees\Module\ModuleDataFixTrait
36 */
37class FixDuplicateLinksTest extends TestCase
38{
39    protected static bool $uses_database = true;
40
41    protected FixDuplicateLinks $fixDuplicateLinks;
42
43    protected Tree $tree;
44
45    protected bool $restore_session_user = false;
46
47    /**
48     * {@inheritdoc}
49     */
50    protected function setUp(): void
51    {
52        parent::setUp();
53
54        $tree_service = new TreeService(new GedcomImportService());
55        $this->tree = $tree_service->create('name', 'title');
56
57        $this->fixDuplicateLinks = new FixDuplicateLinks(new DataFixService());
58
59        if (Auth::id() === null) {
60            Session::put('wt_user', 0);
61            $this->restore_session_user = true;
62        }
63    }
64
65    /**
66     * {@inheritdoc}
67     */
68    protected function tearDown(): void
69    {
70        parent::tearDown();
71
72        if ($this->restore_session_user) {
73            Session::forget('wt_user');
74        }
75
76        unset($this->fixDuplicateLinks, $this->tree);
77    }
78
79    /**
80     * Test the module returns a title and a description
81     */
82    public function testModuleMetadata(): void
83    {
84        self::assertNotEmpty($this->fixDuplicateLinks->title());
85        self::assertNotEmpty($this->fixDuplicateLinks->description());
86    }
87
88    /**
89     * Test the trait's recordsToFix method
90     */
91    public function testRecordsToFix(): void
92    {
93        $records = $this->fixDuplicateLinks->recordsToFix($this->tree, []);
94        self::assertInstanceOf(Collection::class, $records);
95        self::assertCount(1, $records);
96
97        $records = $this->fixDuplicateLinks->recordsToFix($this->tree, ['start' => 'X1', 'end' => 'X9']);
98        self::assertCount(1, $records);
99
100        $records = $this->fixDuplicateLinks->recordsToFix($this->tree, ['start' => 'X2', 'end' => 'X9']);
101        self::assertCount(0, $records);
102    }
103
104    /**
105     * Test the doesRecordNeedUpdate method on a negative and positive test
106     */
107    public function testDoesRecordNeedUpdate(): void
108    {
109        $family = $this->tree->createFamily("0 @@ FAM\n1 HUSB @X1@\n1 CHIL @X2@");
110        self::assertFalse($this->fixDuplicateLinks->doesRecordNeedUpdate($family, []));
111
112        $family->createFact('1 CHIL @X2@', true);
113        self::assertTrue($this->fixDuplicateLinks->doesRecordNeedUpdate($family, []));
114    }
115
116    /**
117     * Test the preview of the update
118     */
119    public function testPreviewUpdate(): void
120    {
121        $family = $this->tree->createFamily("0 @@ FAM\n1 HUSB @X1@\n1 CHIL @X2@\n1 CHIL @X2@");
122
123        self::assertStringContainsString(
124            '<del>1 CHIL @X2@</del>',
125            $this->fixDuplicateLinks->previewUpdate($family, [])
126        );
127    }
128
129    /**
130     * Test the update of the record
131     */
132    public function testUpdateRecord(): void
133    {
134        $family = $this->tree->createFamily("0 @@ FAM\n1 HUSB @X1@\n1 CHIL @X2@\n1 CHIL @X2@");
135        $this->fixDuplicateLinks->updateRecord($family, []);
136
137        self::assertCount(1, $family->facts(['CHIL']));
138    }
139}
140