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