xref: /webtrees/tests/app/Elements/XrefSubmissionTest.php (revision 455ea186c69f201c925b6cf60187a65446830fc5)
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\SubmissionFactory;
24use Fisharebest\Webtrees\Registry;
25use Fisharebest\Webtrees\Submission;
26use Fisharebest\Webtrees\TestCase;
27use Fisharebest\Webtrees\Tree;
28use PHPUnit\Framework\Attributes\CoversClass;
29use Psr\Http\Message\ServerRequestInterface;
30
31#[CoversClass(AbstractElement::class)]
32#[CoversClass(AbstractXrefElement::class)]
33#[CoversClass(XrefSubmission::class)]
34class XrefSubmissionTest extends TestCase
35{
36    public function testEdit(): void
37    {
38        $element = new XrefSubmission('');
39
40        $tree = $this->createMock(Tree::class);
41
42        $factory = $this->createMock(SubmissionFactory::class);
43
44        $factory->expects($this->once())
45            ->method('make')
46            ->willReturn(null);
47
48        Registry::submissionFactory($factory);
49
50        $request = self::createRequest();
51
52        Registry::container()->set(ServerRequestInterface::class, $request);
53
54        $html = $element->edit('some-id', 'some-name', '@X123@', $tree);
55        $dom  = new DOMDocument();
56        $dom->loadHTML($html);
57
58        $select_nodes = $dom->getElementsByTagName('select');
59        self::assertEquals(1, $select_nodes->count());
60
61        $option_nodes = $select_nodes[0]->getElementsByTagName('option');
62        self::assertEquals(1, $option_nodes->count());
63    }
64    public function testEscape(): void
65    {
66        $element = new XrefSubmission('');
67
68        self::assertSame('@X123@', $element->escape('@X123@'));
69    }
70
71    public function testValueXrefLink(): void
72    {
73        $element = new XrefSubmission('');
74
75        $record = $this->createMock(Submission::class);
76
77        $record->expects($this->once())
78            ->method('fullName')
79            ->willReturn('Full Name');
80
81        $record->expects($this->once())
82            ->method('url')
83            ->willReturn('https://url');
84
85        $tree = $this->createMock(Tree::class);
86
87        $factory = $this->createMock(SubmissionFactory::class);
88
89        $factory->expects($this->once())
90            ->method('make')
91            ->willReturn($record);
92
93        Registry::submissionFactory($factory);
94
95        self::assertSame('<a href="https://url">Full Name</a>', $element->value('@X123@', $tree));
96    }
97
98    public function testValueXrefLinkWithInvalidXref(): void
99    {
100        $element = new XrefSubmission('');
101
102        $tree = $this->createMock(Tree::class);
103
104        self::assertSame('<span class="error">invalid</span>', $element->value('invalid', $tree));
105    }
106
107    public function testValueXrefLinkWithMissingRecord(): void
108    {
109        $element = new XrefSubmission('');
110
111        $tree = $this->createMock(Tree::class);
112
113        $factory = $this->createMock(SubmissionFactory::class);
114
115        $factory->expects($this->once())
116            ->method('make')
117            ->willReturn(null);
118
119        Registry::submissionFactory($factory);
120
121        self::assertSame('<span class="error">@X321@</span>', $element->value('@X321@', $tree));
122    }
123}
124