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