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