xref: /webtrees/tests/app/Elements/XrefSourceTest.php (revision 81b514b4672980e5db010e9d89b55eaf131e798f)
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\SourceFactory;
24use Fisharebest\Webtrees\Registry;
25use Fisharebest\Webtrees\Source;
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 XrefSource
34 *
35 * @covers \Fisharebest\Webtrees\Elements\AbstractElement
36 * @covers \Fisharebest\Webtrees\Elements\AbstractXrefElement
37 * @covers \Fisharebest\Webtrees\Elements\XrefSource
38 */
39class XrefSourceTest extends TestCase
40{
41    /**
42     * @return void
43     */
44    public function testEdit(): void
45    {
46        $element = new XrefSource('');
47
48        $tree = $this->createMock(Tree::class);
49
50        $factory = $this->createMock(SourceFactory::class);
51
52        $factory->expects(self::once())
53            ->method('make')
54            ->willReturn(null);
55
56        Registry::sourceFactory($factory);
57
58        $request = self::createRequest();
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 testEditInlineSource(): void
77    {
78        $element = new XrefSource('');
79
80        $tree = $this->createMock(Tree::class);
81
82        $request = self::createRequest();
83
84        app()->instance(ServerRequestInterface::class, $request);
85
86        $html = $element->edit('some-id', 'some-name', 'An inline source', $tree);
87        $dom  = new DOMDocument();
88        $dom->loadHTML($html);
89
90        $textarea_nodes = $dom->getElementsByTagName('textarea');
91        self::assertEquals(1, $textarea_nodes->count());
92    }
93
94    /**
95     * @return void
96     */
97    public function testEscape(): void
98    {
99        $element = new XrefSource('');
100
101        self::assertSame('@X123@', $element->escape('@X123@'));
102    }
103
104    /**
105     * @return void
106     */
107    public function testValueXrefLink(): void
108    {
109        $element = new XrefSource('');
110
111        $record = $this->createMock(Source::class);
112
113        $record->expects(self::once())
114            ->method('fullName')
115            ->willReturn('Full Name');
116
117        $record->expects(self::once())
118            ->method('url')
119            ->willReturn('https://url');
120
121        $tree = $this->createMock(Tree::class);
122
123        $factory = $this->createMock(SourceFactory::class);
124
125        $factory->expects(self::once())
126            ->method('make')
127            ->willReturn($record);
128
129
130        Registry::sourceFactory($factory);
131
132        self::assertSame('<a href="https://url">Full Name</a>', $element->value('@X123@', $tree));
133    }
134
135    /**
136     * @return void
137     */
138    public function testValueXrefLinkWithInvalidXref(): void
139    {
140        $element = new XrefSource('');
141
142        $tree = $this->createMock(Tree::class);
143
144        self::assertSame('<span class="error">@invalid@</span>', $element->value('@invalid@', $tree));
145    }
146
147    /**
148     * @return void
149     */
150    public function testValueXrefLinkWithInlineData(): void
151    {
152        $element = new XrefSource('');
153
154        $tree = $this->createMock(Tree::class);
155
156        self::assertSame('<p>invalid</p>', $element->value('invalid', $tree));
157    }
158
159    /**
160     * @return void
161     */
162    public function testValueXrefLinkWithMissingRecord(): void
163    {
164        $element = new XrefSource('');
165
166        $tree = $this->createMock(Tree::class);
167
168        $factory = $this->createMock(SourceFactory::class);
169
170        $factory->expects(self::once())
171            ->method('make')
172            ->willReturn(null);
173
174        Registry::sourceFactory($factory);
175
176        self::assertSame('<span class="error">@X321@</span>', $element->value('@X321@', $tree));
177    }
178}
179