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