xref: /webtrees/tests/app/Elements/XrefFamilyTest.php (revision b55cbc6b43247e8b2ad14af6f6d24dc6747195ff)
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\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    /**
40     * @return void
41     */
42    public function testEdit(): void
43    {
44        $element = new XrefFamily('');
45
46        $tree = $this->createMock(Tree::class);
47
48        $factory = $this->createMock(FamilyFactory::class);
49
50        $factory->expects(self::once())
51            ->method('make')
52            ->willReturn(null);
53
54        Registry::familyFactory($factory);
55
56        $request = self::createRequest();
57
58        app()->instance(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    /**
72     * @return void
73     */
74    public function testEscape(): void
75    {
76        $element = new XrefFamily('');
77
78        self::assertSame('@X123@', $element->escape('@X123@'));
79    }
80
81    /**
82     * @return void
83     */
84    public function testValueXrefLink(): void
85    {
86        $element = new XrefFamily('');
87
88        $record = $this->createMock(Family::class);
89
90        $record->expects(self::once())
91            ->method('fullName')
92            ->willReturn('Full Name');
93
94        $record->expects(self::once())
95            ->method('url')
96            ->willReturn('https://url');
97
98        $tree = $this->createMock(Tree::class);
99
100        $factory = $this->createMock(FamilyFactory::class);
101
102        $factory->expects(self::once())
103            ->method('make')
104            ->willReturn($record);
105
106
107        Registry::familyFactory($factory);
108
109        self::assertSame('<a href="https://url">Full Name</a>', $element->value('@X123@', $tree));
110    }
111
112    /**
113     * @return void
114     */
115    public function testValueXrefLinkWithInvalidXref(): void
116    {
117        $element = new XrefFamily('');
118
119        $tree = $this->createMock(Tree::class);
120
121        self::assertSame('<span class="error">invalid</span>', $element->value('invalid', $tree));
122    }
123
124    /**
125     * @return void
126     */
127    public function testValueXrefLinkWithMissingRecord(): void
128    {
129        $element = new XrefFamily('');
130
131        $tree = $this->createMock(Tree::class);
132
133        $factory = $this->createMock(FamilyFactory::class);
134
135        $factory->expects(self::once())
136            ->method('make')
137            ->willReturn(null);
138
139        Registry::familyFactory($factory);
140
141        self::assertSame('<span class="error">@X321@</span>', $element->value('@X321@', $tree));
142    }
143}
144