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