xref: /webtrees/tests/app/Elements/XrefSharedNoteTest.php (revision 202c018b592d5a516e4a465dc6dc515f3be37399)
1701f5d18SGreg Roach<?php
2701f5d18SGreg Roach
3701f5d18SGreg Roach/**
4701f5d18SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6701f5d18SGreg Roach * This program is free software: you can redistribute it and/or modify
7701f5d18SGreg Roach * it under the terms of the GNU General Public License as published by
8701f5d18SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9701f5d18SGreg Roach * (at your option) any later version.
10701f5d18SGreg Roach * This program is distributed in the hope that it will be useful,
11701f5d18SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12701f5d18SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13701f5d18SGreg Roach * GNU General Public License for more details.
14701f5d18SGreg Roach * You should have received a copy of the GNU General Public License
15701f5d18SGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16701f5d18SGreg Roach */
17701f5d18SGreg Roach
18701f5d18SGreg Roachdeclare(strict_types=1);
19701f5d18SGreg Roach
20701f5d18SGreg Roachnamespace Fisharebest\Webtrees\Elements;
21701f5d18SGreg Roach
22701f5d18SGreg Roachuse DOMDocument;
23701f5d18SGreg Roachuse Fisharebest\Webtrees\Factories\SharedNoteFactory;
24701f5d18SGreg Roachuse Fisharebest\Webtrees\Registry;
25f0c88a96SGreg Roachuse Fisharebest\Webtrees\SharedNote;
26701f5d18SGreg Roachuse Fisharebest\Webtrees\TestCase;
27701f5d18SGreg Roachuse Fisharebest\Webtrees\Tree;
28*202c018bSGreg Roachuse PHPUnit\Framework\Attributes\CoversClass;
29701f5d18SGreg Roach
30*202c018bSGreg Roach
31*202c018bSGreg Roach#[CoversClass(AbstractElement::class)]
32*202c018bSGreg Roach#[CoversClass(AbstractXrefElement::class)]
33*202c018bSGreg Roach#[CoversClass(XrefSharedNote::class)]
34701f5d18SGreg Roachclass XrefSharedNoteTest extends TestCase
35701f5d18SGreg Roach{
36701f5d18SGreg Roach    public function testEdit(): void
37701f5d18SGreg Roach    {
38701f5d18SGreg Roach        $element = new XrefSharedNote('');
39701f5d18SGreg Roach
40701f5d18SGreg Roach        $tree = $this->createMock(Tree::class);
41701f5d18SGreg Roach
42701f5d18SGreg Roach        $factory = $this->createMock(SharedNoteFactory::class);
43701f5d18SGreg Roach
44701f5d18SGreg Roach        $factory->expects(self::once())
45701f5d18SGreg Roach            ->method('make')
46701f5d18SGreg Roach            ->willReturn(null);
47701f5d18SGreg Roach
48701f5d18SGreg Roach        Registry::sharedNoteFactory($factory);
49701f5d18SGreg Roach
50701f5d18SGreg Roach        $html = $element->edit('some-id', 'some-name', '@X123@', $tree);
51701f5d18SGreg Roach        $dom  = new DOMDocument();
52701f5d18SGreg Roach        $dom->loadHTML($html);
53701f5d18SGreg Roach
54701f5d18SGreg Roach        $select_nodes = $dom->getElementsByTagName('select');
55701f5d18SGreg Roach        self::assertEquals(1, $select_nodes->count());
56701f5d18SGreg Roach
57701f5d18SGreg Roach        $option_nodes = $select_nodes[0]->getElementsByTagName('option');
58701f5d18SGreg Roach        self::assertEquals(1, $option_nodes->count());
59701f5d18SGreg Roach    }
60701f5d18SGreg Roach    public function testEscape(): void
61701f5d18SGreg Roach    {
62701f5d18SGreg Roach        $element = new XrefSharedNote('');
63701f5d18SGreg Roach
64701f5d18SGreg Roach        self::assertSame('@X123@', $element->escape('@X123@'));
65701f5d18SGreg Roach    }
66701f5d18SGreg Roach
67701f5d18SGreg Roach    public function testValueXrefLink(): void
68701f5d18SGreg Roach    {
69701f5d18SGreg Roach        $element = new XrefSharedNote('');
70701f5d18SGreg Roach
71701f5d18SGreg Roach        $record = $this->createMock(SharedNote::class);
72701f5d18SGreg Roach
73701f5d18SGreg Roach        $record->expects(self::once())
74701f5d18SGreg Roach            ->method('fullName')
75701f5d18SGreg Roach            ->willReturn('Full Name');
76701f5d18SGreg Roach
77701f5d18SGreg Roach        $record->expects(self::once())
78701f5d18SGreg Roach            ->method('url')
79701f5d18SGreg Roach            ->willReturn('https://url');
80701f5d18SGreg Roach
81701f5d18SGreg Roach        $tree = $this->createMock(Tree::class);
82701f5d18SGreg Roach
83701f5d18SGreg Roach        $factory = $this->createMock(SharedNoteFactory::class);
84701f5d18SGreg Roach
85701f5d18SGreg Roach        $factory->expects(self::once())
86701f5d18SGreg Roach            ->method('make')
87701f5d18SGreg Roach            ->willReturn($record);
88701f5d18SGreg Roach
89701f5d18SGreg Roach
90701f5d18SGreg Roach        Registry::sharedNoteFactory($factory);
91701f5d18SGreg Roach
92701f5d18SGreg Roach        self::assertSame('<a href="https://url">Full Name</a>', $element->value('@X123@', $tree));
93701f5d18SGreg Roach    }
94701f5d18SGreg Roach
95701f5d18SGreg Roach    public function testValueXrefLinkWithInvalidXref(): void
96701f5d18SGreg Roach    {
97701f5d18SGreg Roach        $element = new XrefSharedNote('');
98701f5d18SGreg Roach
99701f5d18SGreg Roach        $tree = $this->createMock(Tree::class);
100701f5d18SGreg Roach
101701f5d18SGreg Roach        self::assertSame('<span class="error">invalid</span>', $element->value('invalid', $tree));
102701f5d18SGreg Roach    }
103701f5d18SGreg Roach
104701f5d18SGreg Roach    public function testValueXrefLinkWithMissingRecord(): void
105701f5d18SGreg Roach    {
106701f5d18SGreg Roach        $element = new XrefSharedNote('');
107701f5d18SGreg Roach
108701f5d18SGreg Roach        $tree = $this->createMock(Tree::class);
109701f5d18SGreg Roach
110701f5d18SGreg Roach        $factory = $this->createMock(SharedNoteFactory::class);
111701f5d18SGreg Roach
112701f5d18SGreg Roach        $factory->expects(self::once())
113701f5d18SGreg Roach            ->method('make')
114701f5d18SGreg Roach            ->willReturn(null);
115701f5d18SGreg Roach
116701f5d18SGreg Roach        Registry::sharedNoteFactory($factory);
117701f5d18SGreg Roach
118701f5d18SGreg Roach        self::assertSame('<span class="error">@X321@</span>', $element->value('@X321@', $tree));
119701f5d18SGreg Roach    }
120701f5d18SGreg Roach}
121