xref: /webtrees/tests/app/Elements/XrefMediaTest.php (revision 202c018b592d5a516e4a465dc6dc515f3be37399)
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\MediaFactory;
24use Fisharebest\Webtrees\Media;
25use Fisharebest\Webtrees\Registry;
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(XrefMedia::class)]
35class XrefMediaTest extends TestCase
36{
37    public function testEdit(): void
38    {
39        $element = new XrefMedia('');
40
41        $tree = $this->createMock(Tree::class);
42
43        $factory = $this->createMock(MediaFactory::class);
44
45        $factory->expects(self::once())
46            ->method('make')
47            ->willReturn(null);
48
49        Registry::mediaFactory($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 XrefMedia('');
68
69        self::assertSame('@X123@', $element->escape('@X123@'));
70    }
71
72    public function testValueXrefLink(): void
73    {
74        $element = new XrefMedia('');
75
76        $record = $this->createMock(Media::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(MediaFactory::class);
89
90        $factory->expects(self::once())
91            ->method('make')
92            ->willReturn($record);
93
94
95        Registry::mediaFactory($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 XrefMedia('');
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 XrefMedia('');
112
113        $tree = $this->createMock(Tree::class);
114
115        $factory = $this->createMock(MediaFactory::class);
116
117        $factory->expects(self::once())
118            ->method('make')
119            ->willReturn(null);
120
121        Registry::mediaFactory($factory);
122
123        self::assertSame('<span class="error">@X321@</span>', $element->value('@X321@', $tree));
124    }
125}
126