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