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