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