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