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