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\SourceFactory; 24use Fisharebest\Webtrees\Registry; 25use Fisharebest\Webtrees\Source; 26use Fisharebest\Webtrees\TestCase; 27use Fisharebest\Webtrees\Tree; 28use PHPUnit\Framework\Attributes\CoversClass; 29use Psr\Http\Message\ServerRequestInterface; 30 31 32#[CoversClass(AbstractElement::class)] 33#[CoversClass(AbstractXrefElement::class)] 34#[CoversClass(XrefSource::class)] 35class XrefSourceTest extends TestCase 36{ 37 protected static bool $uses_database = true; 38 39 public function testEdit(): void 40 { 41 $element = new XrefSource(''); 42 43 $tree = $this->createMock(Tree::class); 44 45 $factory = $this->createMock(SourceFactory::class); 46 47 $factory->expects(self::once()) 48 ->method('make') 49 ->willReturn(null); 50 51 Registry::sourceFactory($factory); 52 53 $request = self::createRequest(); 54 55 Registry::container()->set(ServerRequestInterface::class, $request); 56 57 $html = $element->edit('some-id', 'some-name', '@X123@', $tree); 58 $dom = new DOMDocument(); 59 $dom->loadHTML($html); 60 61 $select_nodes = $dom->getElementsByTagName('select'); 62 self::assertEquals(1, $select_nodes->count()); 63 64 $option_nodes = $select_nodes[0]->getElementsByTagName('option'); 65 self::assertEquals(1, $option_nodes->count()); 66 } 67 68 public function testEditInlineSource(): void 69 { 70 $element = new XrefSource(''); 71 72 $tree = $this->createMock(Tree::class); 73 74 $request = self::createRequest(); 75 76 Registry::container()->set(ServerRequestInterface::class, $request); 77 78 $html = $element->edit('some-id', 'some-name', 'An inline source', $tree); 79 $dom = new DOMDocument(); 80 $dom->loadHTML($html); 81 82 $textarea_nodes = $dom->getElementsByTagName('textarea'); 83 self::assertEquals(1, $textarea_nodes->count()); 84 } 85 86 public function testEscape(): void 87 { 88 $element = new XrefSource(''); 89 90 self::assertSame('@X123@', $element->escape('@X123@')); 91 } 92 93 public function testValueXrefLink(): void 94 { 95 $element = new XrefSource(''); 96 97 $record = $this->createMock(Source::class); 98 99 $record->expects(self::once()) 100 ->method('fullName') 101 ->willReturn('Full Name'); 102 103 $record->expects(self::once()) 104 ->method('url') 105 ->willReturn('https://url'); 106 107 $tree = $this->createMock(Tree::class); 108 109 $factory = $this->createMock(SourceFactory::class); 110 111 $factory->expects(self::once()) 112 ->method('make') 113 ->willReturn($record); 114 115 116 Registry::sourceFactory($factory); 117 118 self::assertSame('<a href="https://url">Full Name</a>', $element->value('@X123@', $tree)); 119 } 120 121 public function testValueXrefLinkWithInvalidXref(): void 122 { 123 $element = new XrefSource(''); 124 125 $tree = $this->createMock(Tree::class); 126 127 self::assertSame('<span class="error">@invalid@</span>', $element->value('@invalid@', $tree)); 128 } 129 130 public function testValueXrefLinkWithInlineData(): void 131 { 132 $element = new XrefSource(''); 133 134 $tree = $this->createMock(Tree::class); 135 136 self::assertSame('<p>invalid</p>', $element->value('invalid', $tree)); 137 } 138 139 public function testValueXrefLinkWithMissingRecord(): void 140 { 141 $element = new XrefSource(''); 142 143 $tree = $this->createMock(Tree::class); 144 145 $factory = $this->createMock(SourceFactory::class); 146 147 $factory->expects(self::once()) 148 ->method('make') 149 ->willReturn(null); 150 151 Registry::sourceFactory($factory); 152 153 self::assertSame('<span class="error">@X321@</span>', $element->value('@X321@', $tree)); 154 } 155} 156