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