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