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; 28202c018bSGreg Roachuse PHPUnit\Framework\Attributes\CoversClass; 29701f5d18SGreg Roach 30202c018bSGreg Roach#[CoversClass(AbstractElement::class)] 31202c018bSGreg Roach#[CoversClass(AbstractXrefElement::class)] 32202c018bSGreg Roach#[CoversClass(XrefSharedNote::class)] 33701f5d18SGreg Roachclass XrefSharedNoteTest extends TestCase 34701f5d18SGreg Roach{ 35701f5d18SGreg Roach public function testEdit(): void 36701f5d18SGreg Roach { 37701f5d18SGreg Roach $element = new XrefSharedNote(''); 38701f5d18SGreg Roach 39701f5d18SGreg Roach $tree = $this->createMock(Tree::class); 40701f5d18SGreg Roach 41701f5d18SGreg Roach $factory = $this->createMock(SharedNoteFactory::class); 42701f5d18SGreg Roach 43*00ef1d3aSGreg Roach $factory->expects($this->once()) 44701f5d18SGreg Roach ->method('make') 45701f5d18SGreg Roach ->willReturn(null); 46701f5d18SGreg Roach 47701f5d18SGreg Roach Registry::sharedNoteFactory($factory); 48701f5d18SGreg Roach 49701f5d18SGreg Roach $html = $element->edit('some-id', 'some-name', '@X123@', $tree); 50701f5d18SGreg Roach $dom = new DOMDocument(); 51701f5d18SGreg Roach $dom->loadHTML($html); 52701f5d18SGreg Roach 53701f5d18SGreg Roach $select_nodes = $dom->getElementsByTagName('select'); 54701f5d18SGreg Roach self::assertEquals(1, $select_nodes->count()); 55701f5d18SGreg Roach 56701f5d18SGreg Roach $option_nodes = $select_nodes[0]->getElementsByTagName('option'); 57701f5d18SGreg Roach self::assertEquals(1, $option_nodes->count()); 58701f5d18SGreg Roach } 59701f5d18SGreg Roach public function testEscape(): void 60701f5d18SGreg Roach { 61701f5d18SGreg Roach $element = new XrefSharedNote(''); 62701f5d18SGreg Roach 63701f5d18SGreg Roach self::assertSame('@X123@', $element->escape('@X123@')); 64701f5d18SGreg Roach } 65701f5d18SGreg Roach 66701f5d18SGreg Roach public function testValueXrefLink(): void 67701f5d18SGreg Roach { 68701f5d18SGreg Roach $element = new XrefSharedNote(''); 69701f5d18SGreg Roach 70701f5d18SGreg Roach $record = $this->createMock(SharedNote::class); 71701f5d18SGreg Roach 72*00ef1d3aSGreg Roach $record->expects($this->once()) 73701f5d18SGreg Roach ->method('fullName') 74701f5d18SGreg Roach ->willReturn('Full Name'); 75701f5d18SGreg Roach 76*00ef1d3aSGreg Roach $record->expects($this->once()) 77701f5d18SGreg Roach ->method('url') 78701f5d18SGreg Roach ->willReturn('https://url'); 79701f5d18SGreg Roach 80701f5d18SGreg Roach $tree = $this->createMock(Tree::class); 81701f5d18SGreg Roach 82701f5d18SGreg Roach $factory = $this->createMock(SharedNoteFactory::class); 83701f5d18SGreg Roach 84*00ef1d3aSGreg Roach $factory->expects($this->once()) 85701f5d18SGreg Roach ->method('make') 86701f5d18SGreg Roach ->willReturn($record); 87701f5d18SGreg Roach 88701f5d18SGreg Roach Registry::sharedNoteFactory($factory); 89701f5d18SGreg Roach 90701f5d18SGreg Roach self::assertSame('<a href="https://url">Full Name</a>', $element->value('@X123@', $tree)); 91701f5d18SGreg Roach } 92701f5d18SGreg Roach 93701f5d18SGreg Roach public function testValueXrefLinkWithInvalidXref(): void 94701f5d18SGreg Roach { 95701f5d18SGreg Roach $element = new XrefSharedNote(''); 96701f5d18SGreg Roach 97701f5d18SGreg Roach $tree = $this->createMock(Tree::class); 98701f5d18SGreg Roach 99701f5d18SGreg Roach self::assertSame('<span class="error">invalid</span>', $element->value('invalid', $tree)); 100701f5d18SGreg Roach } 101701f5d18SGreg Roach 102701f5d18SGreg Roach public function testValueXrefLinkWithMissingRecord(): void 103701f5d18SGreg Roach { 104701f5d18SGreg Roach $element = new XrefSharedNote(''); 105701f5d18SGreg Roach 106701f5d18SGreg Roach $tree = $this->createMock(Tree::class); 107701f5d18SGreg Roach 108701f5d18SGreg Roach $factory = $this->createMock(SharedNoteFactory::class); 109701f5d18SGreg Roach 110*00ef1d3aSGreg Roach $factory->expects($this->once()) 111701f5d18SGreg Roach ->method('make') 112701f5d18SGreg Roach ->willReturn(null); 113701f5d18SGreg Roach 114701f5d18SGreg Roach Registry::sharedNoteFactory($factory); 115701f5d18SGreg Roach 116701f5d18SGreg Roach self::assertSame('<span class="error">@X321@</span>', $element->value('@X321@', $tree)); 117701f5d18SGreg Roach } 118701f5d18SGreg Roach} 119