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