xref: /webtrees/tests/app/Elements/XrefRepositoryTest.php (revision d43b95f50fc59d15ce34c15c3637ecb5a4e71ea8)
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    /**
40     * @return void
41     */
42    public function testEdit(): void
43    {
44        $element = new XrefRepository('');
45
46        $tree = $this->createMock(Tree::class);
47
48        $factory = $this->createMock(RepositoryFactory::class);
49
50        $factory->expects(self::once())
51            ->method('make')
52            ->willReturn(null);
53
54        Registry::repositoryFactory($factory);
55
56        $request = self::createRequest();
57
58        Registry::container()->set(ServerRequestInterface::class, $request);
59
60        $html = $element->edit('some-id', 'some-name', '@X123@', $tree);
61        $dom  = new DOMDocument();
62        $dom->loadHTML($html);
63
64        $select_nodes = $dom->getElementsByTagName('select');
65        self::assertEquals(1, $select_nodes->count());
66
67        $option_nodes = $select_nodes[0]->getElementsByTagName('option');
68        self::assertEquals(1, $option_nodes->count());
69    }
70    /**
71     * @return void
72     */
73    public function testEscape(): void
74    {
75        $element = new XrefRepository('');
76
77        self::assertSame('@X123@', $element->escape('@X123@'));
78    }
79
80    /**
81     * @return void
82     */
83    public function testValueXrefLink(): void
84    {
85        $element = new XrefRepository('');
86
87        $record = $this->createMock(Repository::class);
88
89        $record->expects(self::once())
90            ->method('fullName')
91            ->willReturn('Full Name');
92
93        $record->expects(self::once())
94            ->method('url')
95            ->willReturn('https://url');
96
97        $tree = $this->createMock(Tree::class);
98
99        $factory = $this->createMock(RepositoryFactory::class);
100
101        $factory->expects(self::once())
102            ->method('make')
103            ->willReturn($record);
104
105
106        Registry::repositoryFactory($factory);
107
108        self::assertSame('<a href="https://url">Full Name</a>', $element->value('@X123@', $tree));
109    }
110
111    /**
112     * @return void
113     */
114    public function testValueXrefLinkWithInvalidXref(): void
115    {
116        $element = new XrefRepository('');
117
118        $tree = $this->createMock(Tree::class);
119
120        self::assertSame('<span class="error">invalid</span>', $element->value('invalid', $tree));
121    }
122
123    /**
124     * @return void
125     */
126    public function testValueXrefLinkWithMissingRecord(): void
127    {
128        $element = new XrefRepository('');
129
130        $tree = $this->createMock(Tree::class);
131
132        $factory = $this->createMock(RepositoryFactory::class);
133
134        $factory->expects(self::once())
135            ->method('make')
136            ->willReturn(null);
137
138        Registry::repositoryFactory($factory);
139
140        self::assertSame('<span class="error">@X321@</span>', $element->value('@X321@', $tree));
141    }
142}
143