xref: /webtrees/tests/app/Elements/XrefSharedNoteTest.php (revision f0c88a961929e1d14256ee4286994384bb29fbc1)
1701f5d18SGreg Roach<?php
2701f5d18SGreg Roach
3701f5d18SGreg Roach/**
4701f5d18SGreg Roach * webtrees: online genealogy
5701f5d18SGreg Roach * Copyright (C) 2022 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;
25*f0c88a96SGreg Roachuse Fisharebest\Webtrees\SharedNote;
26701f5d18SGreg Roachuse Fisharebest\Webtrees\TestCase;
27701f5d18SGreg Roachuse Fisharebest\Webtrees\Tree;
28701f5d18SGreg Roach
29701f5d18SGreg Roach/**
30701f5d18SGreg Roach * Test harness for the class XrefSharedNote
31701f5d18SGreg Roach *
32701f5d18SGreg Roach * @covers \Fisharebest\Webtrees\Elements\AbstractElement
33701f5d18SGreg Roach * @covers \Fisharebest\Webtrees\Elements\AbstractXrefElement
34701f5d18SGreg Roach * @covers \Fisharebest\Webtrees\Elements\XrefSharedNote
35701f5d18SGreg Roach */
36701f5d18SGreg Roachclass XrefSharedNoteTest extends TestCase
37701f5d18SGreg Roach{
38701f5d18SGreg Roach    /**
39701f5d18SGreg Roach     * @return void
40701f5d18SGreg Roach     */
41701f5d18SGreg Roach    public function testEdit(): void
42701f5d18SGreg Roach    {
43701f5d18SGreg Roach        $element = new XrefSharedNote('');
44701f5d18SGreg Roach
45701f5d18SGreg Roach        $tree = $this->createMock(Tree::class);
46701f5d18SGreg Roach
47701f5d18SGreg Roach        $factory = $this->createMock(SharedNoteFactory::class);
48701f5d18SGreg Roach
49701f5d18SGreg Roach        $factory->expects(self::once())
50701f5d18SGreg Roach            ->method('make')
51701f5d18SGreg Roach            ->willReturn(null);
52701f5d18SGreg Roach
53701f5d18SGreg Roach        Registry::sharedNoteFactory($factory);
54701f5d18SGreg Roach
55701f5d18SGreg Roach        $html = $element->edit('some-id', 'some-name', '@X123@', $tree);
56701f5d18SGreg Roach        $dom  = new DOMDocument();
57701f5d18SGreg Roach        $dom->loadHTML($html);
58701f5d18SGreg Roach
59701f5d18SGreg Roach        $select_nodes = $dom->getElementsByTagName('select');
60701f5d18SGreg Roach        self::assertEquals(1, $select_nodes->count());
61701f5d18SGreg Roach
62701f5d18SGreg Roach        $option_nodes = $select_nodes[0]->getElementsByTagName('option');
63701f5d18SGreg Roach        self::assertEquals(1, $option_nodes->count());
64701f5d18SGreg Roach    }
65701f5d18SGreg Roach    /**
66701f5d18SGreg Roach     * @return void
67701f5d18SGreg Roach     */
68701f5d18SGreg Roach    public function testEscape(): void
69701f5d18SGreg Roach    {
70701f5d18SGreg Roach        $element = new XrefSharedNote('');
71701f5d18SGreg Roach
72701f5d18SGreg Roach        self::assertSame('@X123@', $element->escape('@X123@'));
73701f5d18SGreg Roach    }
74701f5d18SGreg Roach
75701f5d18SGreg Roach    /**
76701f5d18SGreg Roach     * @return void
77701f5d18SGreg Roach     */
78701f5d18SGreg Roach    public function testValueXrefLink(): void
79701f5d18SGreg Roach    {
80701f5d18SGreg Roach        $element = new XrefSharedNote('');
81701f5d18SGreg Roach
82701f5d18SGreg Roach        $record = $this->createMock(SharedNote::class);
83701f5d18SGreg Roach
84701f5d18SGreg Roach        $record->expects(self::once())
85701f5d18SGreg Roach            ->method('fullName')
86701f5d18SGreg Roach            ->willReturn('Full Name');
87701f5d18SGreg Roach
88701f5d18SGreg Roach        $record->expects(self::once())
89701f5d18SGreg Roach            ->method('url')
90701f5d18SGreg Roach            ->willReturn('https://url');
91701f5d18SGreg Roach
92701f5d18SGreg Roach        $tree = $this->createMock(Tree::class);
93701f5d18SGreg Roach
94701f5d18SGreg Roach        $factory = $this->createMock(SharedNoteFactory::class);
95701f5d18SGreg Roach
96701f5d18SGreg Roach        $factory->expects(self::once())
97701f5d18SGreg Roach            ->method('make')
98701f5d18SGreg Roach            ->willReturn($record);
99701f5d18SGreg Roach
100701f5d18SGreg Roach
101701f5d18SGreg Roach        Registry::sharedNoteFactory($factory);
102701f5d18SGreg Roach
103701f5d18SGreg Roach        self::assertSame('<a href="https://url">Full Name</a>', $element->value('@X123@', $tree));
104701f5d18SGreg Roach    }
105701f5d18SGreg Roach
106701f5d18SGreg Roach    /**
107701f5d18SGreg Roach     * @return void
108701f5d18SGreg Roach     */
109701f5d18SGreg Roach    public function testValueXrefLinkWithInvalidXref(): void
110701f5d18SGreg Roach    {
111701f5d18SGreg Roach        $element = new XrefSharedNote('');
112701f5d18SGreg Roach
113701f5d18SGreg Roach        $tree = $this->createMock(Tree::class);
114701f5d18SGreg Roach
115701f5d18SGreg Roach        self::assertSame('<span class="error">invalid</span>', $element->value('invalid', $tree));
116701f5d18SGreg Roach    }
117701f5d18SGreg Roach
118701f5d18SGreg Roach    /**
119701f5d18SGreg Roach     * @return void
120701f5d18SGreg Roach     */
121701f5d18SGreg Roach    public function testValueXrefLinkWithMissingRecord(): void
122701f5d18SGreg Roach    {
123701f5d18SGreg Roach        $element = new XrefSharedNote('');
124701f5d18SGreg Roach
125701f5d18SGreg Roach        $tree = $this->createMock(Tree::class);
126701f5d18SGreg Roach
127701f5d18SGreg Roach        $factory = $this->createMock(SharedNoteFactory::class);
128701f5d18SGreg Roach
129701f5d18SGreg Roach        $factory->expects(self::once())
130701f5d18SGreg Roach            ->method('make')
131701f5d18SGreg Roach            ->willReturn(null);
132701f5d18SGreg Roach
133701f5d18SGreg Roach        Registry::sharedNoteFactory($factory);
134701f5d18SGreg Roach
135701f5d18SGreg Roach        self::assertSame('<span class="error">@X321@</span>', $element->value('@X321@', $tree));
136701f5d18SGreg Roach    }
137701f5d18SGreg Roach}
138