xref: /webtrees/tests/app/Elements/XrefFamilyTest.php (revision 1da5dadd02c9f21e0a345e21a19b2c63ae792ed3)
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\FamilyFactory;
24use Fisharebest\Webtrees\Family;
25use Fisharebest\Webtrees\Registry;
26use Fisharebest\Webtrees\TestCase;
27use Fisharebest\Webtrees\Tree;
28use Psr\Http\Message\ServerRequestInterface;
29
30/**
31 * Test harness for the class XrefFamily
32 *
33 * @covers \Fisharebest\Webtrees\Elements\AbstractElement
34 * @covers \Fisharebest\Webtrees\Elements\AbstractXrefElement
35 * @covers \Fisharebest\Webtrees\Elements\XrefFamily
36 */
37class XrefFamilyTest extends TestCase
38{
39    public function testEdit(): void
40    {
41        $element = new XrefFamily('');
42
43        $tree = $this->createMock(Tree::class);
44
45        $factory = $this->createMock(FamilyFactory::class);
46
47        $factory->expects(self::once())
48            ->method('make')
49            ->willReturn(null);
50
51        Registry::familyFactory($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
68    public function testEscape(): void
69    {
70        $element = new XrefFamily('');
71
72        self::assertSame('@X123@', $element->escape('@X123@'));
73    }
74
75    public function testValueXrefLink(): void
76    {
77        $element = new XrefFamily('');
78
79        $record = $this->createMock(Family::class);
80
81        $record->expects(self::once())
82            ->method('fullName')
83            ->willReturn('Full Name');
84
85        $record->expects(self::once())
86            ->method('url')
87            ->willReturn('https://url');
88
89        $tree = $this->createMock(Tree::class);
90
91        $factory = $this->createMock(FamilyFactory::class);
92
93        $factory->expects(self::once())
94            ->method('make')
95            ->willReturn($record);
96
97
98        Registry::familyFactory($factory);
99
100        self::assertSame('<a href="https://url">Full Name</a>', $element->value('@X123@', $tree));
101    }
102
103    public function testValueXrefLinkWithInvalidXref(): void
104    {
105        $element = new XrefFamily('');
106
107        $tree = $this->createMock(Tree::class);
108
109        self::assertSame('<span class="error">invalid</span>', $element->value('invalid', $tree));
110    }
111
112    public function testValueXrefLinkWithMissingRecord(): void
113    {
114        $element = new XrefFamily('');
115
116        $tree = $this->createMock(Tree::class);
117
118        $factory = $this->createMock(FamilyFactory::class);
119
120        $factory->expects(self::once())
121            ->method('make')
122            ->willReturn(null);
123
124        Registry::familyFactory($factory);
125
126        self::assertSame('<span class="error">@X321@</span>', $element->value('@X321@', $tree));
127    }
128}
129