xref: /webtrees/tests/app/Elements/XrefIndividualTest.php (revision 9d74ed0ab3ae61aa8ae07578e026f14020844ce9)
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\Elements;
21
22use DOMDocument;
23use Fisharebest\Webtrees\Factories\IndividualFactory;
24use Fisharebest\Webtrees\Individual;
25use Fisharebest\Webtrees\Registry;
26use Fisharebest\Webtrees\TestCase;
27use Fisharebest\Webtrees\Tree;
28use PHPUnit\Framework\Attributes\CoversClass;
29use Psr\Http\Message\ServerRequestInterface;
30
31#[CoversClass(AbstractElement::class)]
32#[CoversClass(AbstractXrefElement::class)]
33#[CoversClass(XrefIndividual::class)]
34class XrefIndividualTest extends TestCase
35{
36    public function testEdit(): void
37    {
38        $element = new XrefIndividual('');
39
40        $tree = $this->createMock(Tree::class);
41
42        $factory = $this->createMock(IndividualFactory::class);
43
44        $factory->expects($this->once())
45            ->method('make')
46            ->willReturn(null);
47
48        Registry::individualFactory($factory);
49
50        $request = self::createRequest();
51
52        Registry::container()->set(ServerRequestInterface::class, $request);
53
54        $html = $element->edit('some-id', 'some-name', '@X123@', $tree);
55        $dom  = new DOMDocument();
56        $dom->loadHTML($html);
57
58        $select_nodes = $dom->getElementsByTagName('select');
59        self::assertEquals(1, $select_nodes->count());
60
61        $option_nodes = $select_nodes[0]->getElementsByTagName('option');
62        self::assertEquals(1, $option_nodes->count());
63    }
64
65    public function testEscape(): void
66    {
67        $element = new XrefIndividual('');
68
69        self::assertSame('@X123@', $element->escape('@X123@'));
70    }
71
72    public function testValueXrefLink(): void
73    {
74        $element = new XrefIndividual('');
75
76        $record = $this->createMock(Individual::class);
77
78        $record->expects($this->once())
79            ->method('fullName')
80            ->willReturn('Full Name');
81
82        $record->expects($this->once())
83            ->method('url')
84            ->willReturn('https://url');
85
86        $tree = $this->createMock(Tree::class);
87
88        $factory = $this->createMock(IndividualFactory::class);
89
90        $factory->expects($this->once())
91            ->method('make')
92            ->willReturn($record);
93
94        Registry::individualFactory($factory);
95
96        self::assertSame('<a href="https://url">Full Name</a>', $element->value('@X123@', $tree));
97    }
98
99    public function testValueXrefLinkWithInvalidXref(): void
100    {
101        $element = new XrefIndividual('');
102
103        $tree = $this->createMock(Tree::class);
104
105        self::assertSame('<span class="error">invalid</span>', $element->value('invalid', $tree));
106    }
107
108    public function testValueXrefLinkWithMissingRecord(): void
109    {
110        $element = new XrefIndividual('');
111
112        $tree = $this->createMock(Tree::class);
113
114        $factory = $this->createMock(IndividualFactory::class);
115
116        $factory->expects($this->once())
117            ->method('make')
118            ->willReturn(null);
119
120        Registry::individualFactory($factory);
121
122        self::assertSame('<span class="error">@X321@</span>', $element->value('@X321@', $tree));
123    }
124}
125