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