xref: /webtrees/tests/app/Elements/XrefLocationTest.php (revision e5a5851c1ee2fef05c5230fce3ffdc366591b06f)
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\LocationFactory;
24use Fisharebest\Webtrees\Location;
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(XrefLocation::class)]
34class XrefLocationTest extends TestCase
35{
36    public function testEdit(): void
37    {
38        $element = new XrefLocation('');
39
40        $tree = $this->createMock(Tree::class);
41
42        $factory = $this->createMock(LocationFactory::class);
43
44        $factory->expects($this->once())
45            ->method('make')
46            ->willReturn(null);
47
48        Registry::locationFactory($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 XrefLocation('');
68
69        self::assertSame('@X123@', $element->escape('@X123@'));
70    }
71
72    public function testValueXrefLink(): void
73    {
74        $element = new XrefLocation('');
75
76        $record = $this->createMock(Location::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(LocationFactory::class);
89
90        $factory->expects($this->once())
91            ->method('make')
92            ->willReturn($record);
93
94        Registry::locationFactory($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 XrefLocation('');
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 XrefLocation('');
111
112        $tree = $this->createMock(Tree::class);
113
114        $factory = $this->createMock(LocationFactory::class);
115
116        $factory->expects($this->once())
117            ->method('make')
118            ->willReturn(null);
119
120        Registry::locationFactory($factory);
121
122        self::assertSame('<span class="error">@X321@</span>', $element->value('@X321@', $tree));
123    }
124}
125