xref: /webtrees/tests/app/Elements/XrefNoteTest.php (revision 58feaed63d47b8abced82391ad3679c63abf54d7)
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\NoteFactory;
24use Fisharebest\Webtrees\Note;
25use Fisharebest\Webtrees\Registry;
26use Fisharebest\Webtrees\TestCase;
27use Fisharebest\Webtrees\Tree;
28use PHPUnit\Framework\Attributes\CoversClass;
29
30#[CoversClass(AbstractElement::class)]
31#[CoversClass(AbstractXrefElement::class)]
32#[CoversClass(XrefNote::class)]
33class XrefNoteTest extends TestCase
34{
35    public function testEdit(): void
36    {
37        $element = new XrefNote('');
38
39        $tree = $this->createMock(Tree::class);
40
41        $factory = $this->createMock(NoteFactory::class);
42
43        $factory->expects($this->once())
44            ->method('make')
45            ->willReturn(null);
46
47        Registry::noteFactory($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 XrefNote('');
62
63        self::assertSame('@X123@', $element->escape('@X123@'));
64    }
65
66    public function testValueXrefLink(): void
67    {
68        $element = new XrefNote('');
69
70        $record = $this->createMock(Note::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(NoteFactory::class);
83
84        $factory->expects($this->once())
85            ->method('make')
86            ->willReturn($record);
87
88        Registry::noteFactory($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 XrefNote('');
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 XrefNote('');
105
106        $tree = $this->createMock(Tree::class);
107
108        $factory = $this->createMock(NoteFactory::class);
109
110        $factory->expects($this->once())
111            ->method('make')
112            ->willReturn(null);
113
114        Registry::noteFactory($factory);
115
116        self::assertSame('<span class="error">@X321@</span>', $element->value('@X321@', $tree));
117    }
118}
119