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