1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2019 webtrees development team 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16declare(strict_types=1); 17 18namespace Fisharebest\Webtrees; 19 20/** 21 * Test harness for the class Tree 22 */ 23class TreeTest extends \Fisharebest\Webtrees\TestCase 24{ 25 protected static $uses_database = true; 26 27 /** 28 * @covers \Fisharebest\Webtrees\Tree::__construct 29 * @covers \Fisharebest\Webtrees\Tree::create 30 * @covers \Fisharebest\Webtrees\Tree::id 31 * @covers \Fisharebest\Webtrees\Tree::name 32 * @covers \Fisharebest\Webtrees\Tree::title 33 * 34 * @return void 35 */ 36 public function testConstructor(): void 37 { 38 $tree = Tree::create('tree-name', 'Tree title'); 39 40 $this->assertSame(1, $tree->id()); 41 $this->assertSame('tree-name', $tree->name()); 42 $this->assertSame('Tree title', $tree->title()); 43 } 44 45 /** 46 * @covers \Fisharebest\Webtrees\Tree::getPreference 47 * @covers \Fisharebest\Webtrees\Tree::setPreference 48 * 49 * @return void 50 */ 51 public function testTreePreferences(): void 52 { 53 $tree = Tree::create('tree-name', 'Tree title'); 54 55 $pref = $tree->getPreference('foo', 'default'); 56 $this->assertSame('default', $pref); 57 58 $tree->setPreference('foo', 'bar'); 59 $pref = $tree->getPreference('foo', 'default'); 60 $this->assertSame('bar', $pref); 61 } 62 63 /** 64 * @covers \Fisharebest\Webtrees\Tree::getUserPreference 65 * @covers \Fisharebest\Webtrees\Tree::setUserPreference 66 * 67 * @return void 68 */ 69 public function testUserTreePreferences(): void 70 { 71 $tree = Tree::create('tree-name', 'Tree title'); 72 $user = User::create('user', 'User', 'user@example.com', 'secret'); 73 74 $pref = $tree->getUserPreference($user, 'foo', 'default'); 75 $this->assertSame('default', $pref); 76 77 $tree->setUserPreference($user, 'foo', 'bar'); 78 $pref = $tree->getUserPreference($user, 'foo', 'default'); 79 $this->assertSame('bar', $pref); 80 } 81 82 /** 83 * @covers \Fisharebest\Webtrees\Tree::getNewXref 84 * 85 * @return void 86 */ 87 public function testGetNewXref(): void 88 { 89 $tree = Tree::create('tree-name', 'Tree title'); 90 91 $this->assertSame('X1', $tree->getNewXref()); 92 $this->assertSame('X2', $tree->getNewXref()); 93 $this->assertSame('X3', $tree->getNewXref()); 94 $this->assertSame('X4', $tree->getNewXref()); 95 $this->assertSame('X5', $tree->getNewXref()); 96 } 97 98 /** 99 * @covers \Fisharebest\Webtrees\Tree::createIndividual 100 * @expectedException \InvalidArgumentException 101 * 102 * @return void 103 */ 104 public function testCreateInvalidIndividual(): void 105 { 106 $tree = Tree::create('tree-name', 'Tree title'); 107 $user = User::create('user', 'User', 'user@example.com', 'secret'); 108 $user->setPreference('canadmin', '1'); 109 Auth::login($user); 110 111 $tree->createIndividual("0 @@ FOO\n1 SEX U"); 112 } 113 114 /** 115 * @covers \Fisharebest\Webtrees\Tree::createIndividual 116 * 117 * @return void 118 */ 119 public function testCreateIndividual(): void 120 { 121 $tree = Tree::create('tree-name', 'Tree title'); 122 $user = User::create('user', 'User', 'user@example.com', 'secret'); 123 $user->setPreference('canadmin', '1'); 124 Auth::login($user); 125 126 $record = $tree->createIndividual("0 @@ INDI\n1 SEX F\n1 NAME Foo /Bar/"); 127 $this->assertTrue($record->isPendingAddition()); 128 129 $user->setPreference('auto_accept', '1'); 130 $record = $tree->createIndividual("0 @@ INDI\n1 SEX F\n1 NAME Foo /Bar/"); 131 $this->assertFalse($record->isPendingAddition()); 132 } 133 134 /** 135 * @covers \Fisharebest\Webtrees\Tree::createFamily 136 * @expectedException \InvalidArgumentException 137 * 138 * @return void 139 */ 140 public function testCreateInvalidFamily(): void 141 { 142 $tree = Tree::create('tree-name', 'Tree title'); 143 $user = User::create('user', 'User', 'user@example.com', 'secret'); 144 $user->setPreference('canadmin', '1'); 145 Auth::login($user); 146 147 $tree->createFamily("0 @@ FOO\n1 MARR Y"); 148 } 149 150 /** 151 * @covers \Fisharebest\Webtrees\Tree::createFamily 152 * 153 * @return void 154 */ 155 public function testCreateFamily(): void 156 { 157 $tree = Tree::create('tree-name', 'Tree title'); 158 $user = User::create('user', 'User', 'user@example.com', 'secret'); 159 $user->setPreference('canadmin', '1'); 160 Auth::login($user); 161 162 $record = $tree->createFamily("0 @@ FAM\n1 MARR Y"); 163 $this->assertTrue($record->isPendingAddition()); 164 165 $user->setPreference('auto_accept', '1'); 166 $record = $tree->createFamily("0 @@ FAM\n1 MARR Y"); 167 $this->assertFalse($record->isPendingAddition()); 168 } 169 170 /** 171 * @covers \Fisharebest\Webtrees\Tree::createMediaObject 172 * @expectedException \InvalidArgumentException 173 * 174 * @return void 175 */ 176 public function testCreateInvalidMediaObject(): void 177 { 178 $tree = Tree::create('tree-name', 'Tree title'); 179 $user = User::create('user', 'User', 'user@example.com', 'secret'); 180 $user->setPreference('canadmin', '1'); 181 Auth::login($user); 182 183 $tree->createMediaObject("0 @@ FOO\n1 MARR Y"); 184 } 185 186 /** 187 * @covers \Fisharebest\Webtrees\Tree::createMediaObject 188 * 189 * @return void 190 */ 191 public function testCreateMediaObject(): void 192 { 193 $tree = Tree::create('tree-name', 'Tree title'); 194 $user = User::create('user', 'User', 'user@example.com', 'secret'); 195 $user->setPreference('canadmin', '1'); 196 Auth::login($user); 197 198 $record = $tree->createMediaObject("0 @@ OBJE\n1 FILE foo.jpeg"); 199 $this->assertTrue($record->isPendingAddition()); 200 201 $user->setPreference('auto_accept', '1'); 202 $record = $tree->createMediaObject("0 @@ OBJE\n1 FILE foo.jpeg"); 203 $this->assertFalse($record->isPendingAddition()); 204 } 205 206 /** 207 * @covers \Fisharebest\Webtrees\Tree::createRecord 208 * @expectedException \InvalidArgumentException 209 * 210 * @return void 211 */ 212 public function testCreateInvalidRecord(): void 213 { 214 $tree = Tree::create('tree-name', 'Tree title'); 215 $user = User::create('user', 'User', 'user@example.com', 'secret'); 216 $user->setPreference('canadmin', '1'); 217 Auth::login($user); 218 219 $tree->createRecord("0 @@FOO\n1 NOTE noted"); 220 } 221 222 /** 223 * @covers \Fisharebest\Webtrees\Tree::createRecord 224 * 225 * @return void 226 */ 227 public function testCreateRecord(): void 228 { 229 $tree = Tree::create('tree-name', 'Tree title'); 230 $user = User::create('user', 'User', 'user@example.com', 'secret'); 231 $user->setPreference('canadmin', '1'); 232 Auth::login($user); 233 234 $record = $tree->createRecord("0 @@ FOO\n1 NOTE noted"); 235 $this->assertTrue($record->isPendingAddition()); 236 237 $user->setPreference('auto_accept', '1'); 238 $record = $tree->createRecord("0 @@ FOO\n1 NOTE noted"); 239 $this->assertFalse($record->isPendingAddition()); 240 } 241 242 /** 243 * @covers \Fisharebest\Webtrees\Tree::significantIndividual 244 * 245 * @return void 246 */ 247 public function testSignificantIndividual(): void 248 { 249 $tree = Tree::create('tree-name', 'Tree title'); 250 $user = User::create('user', 'User', 'user@example.com', 'secret'); 251 $user->setPreference('auto_accept', '1'); 252 Auth::login($user); 253 254 // No individuals in tree? Dummy individual 255 $this->assertSame('I', $tree->significantIndividual($user)->xref()); 256 257 $record1 = $tree->createIndividual("0 @@ INDI\n1 SEX F\n1 NAME Foo /Bar/"); 258 $record2 = $tree->createIndividual("0 @@ INDI\n1 SEX F\n1 NAME Foo /Bar/"); 259 $record3 = $tree->createIndividual("0 @@ INDI\n1 SEX F\n1 NAME Foo /Bar/"); 260 $record4 = $tree->createIndividual("0 @@ INDI\n1 SEX F\n1 NAME Foo /Bar/"); 261 262 // Individuals exist? First one (lowest XREF). 263 $this->assertSame($record1->xref(), $tree->significantIndividual($user)->xref()); 264 265 // Preference for tree? 266 $tree->setPreference('PEDIGREE_ROOT_ID', $record2->xref()); 267 $this->assertSame($record2->xref(), $tree->significantIndividual($user)->xref()); 268 269 // User preference 270 $tree->setUserPreference($user, 'gedcomid', $record3->xref()); 271 $this->assertSame($record3->xref(), $tree->significantIndividual($user)->xref()); 272 273 // User record 274 $tree->setUserPreference($user, 'rootid', $record4->xref()); 275 $this->assertSame($record4->xref(), $tree->significantIndividual($user)->xref()); 276 } 277 278 /** 279 * @covers \Fisharebest\Webtrees\Tree::importGedcomFile 280 * @covers \Fisharebest\Webtrees\Tree::deleteGenealogyData 281 * @covers \Fisharebest\Webtrees\Tree::delete 282 * 283 * @return void 284 */ 285 public function testImportAndDeleteGedcomFile(): void 286 { 287 $tree = $this->importTree('demo.ged'); 288 $this->assertNotNull(Tree::findByName('demo.ged')); 289 290 $tree->delete(); 291 $this->assertNull(Tree::findByName('demo.ged')); 292 } 293} 294