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