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