1a25f0a04SGreg Roach<?php 2a25f0a04SGreg Roach/** 3a25f0a04SGreg Roach * webtrees: online genealogy 48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 5a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify 6a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by 7a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or 8a25f0a04SGreg Roach * (at your option) any later version. 9a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful, 10a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 11a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12a25f0a04SGreg Roach * GNU General Public License for more details. 13a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License 14a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 15a25f0a04SGreg Roach */ 16e7f56f2aSGreg Roachdeclare(strict_types=1); 17e7f56f2aSGreg Roach 1884e2cf4eSGreg Roachnamespace Fisharebest\Webtrees; 19a25f0a04SGreg Roach 20a25f0a04SGreg Roach/** 21a25f0a04SGreg Roach * Test harness for the class Tree 22a25f0a04SGreg Roach */ 2384e2cf4eSGreg Roachclass TreeTest extends \Fisharebest\Webtrees\TestCase 24c1010edaSGreg Roach{ 25126654d7SGreg Roach protected static $uses_database = true; 26126654d7SGreg Roach 27a25f0a04SGreg Roach /** 28126654d7SGreg Roach * @covers \Fisharebest\Webtrees\Tree::__construct 29126654d7SGreg Roach * @covers \Fisharebest\Webtrees\Tree::create 30126654d7SGreg Roach * @covers \Fisharebest\Webtrees\Tree::id 31126654d7SGreg Roach * @covers \Fisharebest\Webtrees\Tree::name 32126654d7SGreg Roach * @covers \Fisharebest\Webtrees\Tree::title 3352348eb8SGreg Roach * 3452348eb8SGreg Roach * @return void 35a25f0a04SGreg Roach */ 36126654d7SGreg Roach public function testConstructor(): void 37c1010edaSGreg Roach { 38126654d7SGreg Roach $tree = Tree::create('tree-name', 'Tree title'); 39126654d7SGreg Roach 40126654d7SGreg Roach $this->assertSame(1, $tree->id()); 41126654d7SGreg Roach $this->assertSame('tree-name', $tree->name()); 42126654d7SGreg Roach $this->assertSame('Tree title', $tree->title()); 43126654d7SGreg Roach } 44126654d7SGreg Roach 45126654d7SGreg Roach /** 46126654d7SGreg Roach * @covers \Fisharebest\Webtrees\Tree::getPreference 47126654d7SGreg Roach * @covers \Fisharebest\Webtrees\Tree::setPreference 48126654d7SGreg Roach * 49126654d7SGreg Roach * @return void 50126654d7SGreg Roach */ 51af343084SGreg Roach public function testTreePreferences(): void 52126654d7SGreg Roach { 53126654d7SGreg Roach $tree = Tree::create('tree-name', 'Tree title'); 54126654d7SGreg Roach 55126654d7SGreg Roach $pref = $tree->getPreference('foo', 'default'); 56126654d7SGreg Roach $this->assertSame('default', $pref); 57126654d7SGreg Roach 58126654d7SGreg Roach $tree->setPreference('foo', 'bar'); 59126654d7SGreg Roach $pref = $tree->getPreference('foo', 'default'); 60126654d7SGreg Roach $this->assertSame('bar', $pref); 61a25f0a04SGreg Roach } 62af343084SGreg Roach 63af343084SGreg Roach /** 64af343084SGreg Roach * @covers \Fisharebest\Webtrees\Tree::getUserPreference 65000dd58cSGreg Roach * @covers \Fisharebest\Webtrees\Tree::setUserPreference 66af343084SGreg Roach * 67af343084SGreg Roach * @return void 68af343084SGreg Roach */ 69af343084SGreg Roach public function testUserTreePreferences(): void 70af343084SGreg Roach { 71af343084SGreg Roach $tree = Tree::create('tree-name', 'Tree title'); 72af343084SGreg Roach $user = User::create('user', 'User', 'user@example.com', 'secret'); 73af343084SGreg Roach 74af343084SGreg Roach $pref = $tree->getUserPreference($user, 'foo', 'default'); 75af343084SGreg Roach $this->assertSame('default', $pref); 76af343084SGreg Roach 77af343084SGreg Roach $tree->setUserPreference($user, 'foo', 'bar'); 78af343084SGreg Roach $pref = $tree->getUserPreference($user, 'foo', 'default'); 79af343084SGreg Roach $this->assertSame('bar', $pref); 80af343084SGreg Roach } 81963fbaeeSGreg Roach 82963fbaeeSGreg Roach /** 83963fbaeeSGreg Roach * @covers \Fisharebest\Webtrees\Tree::createIndividual 84963fbaeeSGreg Roach * 85963fbaeeSGreg Roach * @return void 86963fbaeeSGreg Roach */ 87963fbaeeSGreg Roach public function testCreateIndividual(): void 88963fbaeeSGreg Roach { 89963fbaeeSGreg Roach $tree = Tree::create('tree-name', 'Tree title'); 90963fbaeeSGreg Roach $user = User::create('user', 'User', 'user@example.com', 'secret'); 91963fbaeeSGreg Roach $user->setPreference('canadmin', '1'); 92963fbaeeSGreg Roach Auth::login($user); 93963fbaeeSGreg Roach 94*8f9b0fb2SGreg Roach $record = $tree->createIndividual("0 @@ INDI\n1 SEX F\n1 NAME Foo /Bar/"); 95*8f9b0fb2SGreg Roach $this->assertTrue($record->isPendingAddition()); 96963fbaeeSGreg Roach 97963fbaeeSGreg Roach $user->setPreference('auto_accept', '1'); 98*8f9b0fb2SGreg Roach $record = $tree->createIndividual("0 @@ INDI\n1 SEX F\n1 NAME Foo /Bar/"); 99*8f9b0fb2SGreg Roach $this->assertFalse($record->isPendingAddition()); 100*8f9b0fb2SGreg Roach } 101963fbaeeSGreg Roach 102*8f9b0fb2SGreg Roach /** 103*8f9b0fb2SGreg Roach * @covers \Fisharebest\Webtrees\Tree::getNewXref 104*8f9b0fb2SGreg Roach * 105*8f9b0fb2SGreg Roach * @return void 106*8f9b0fb2SGreg Roach */ 107*8f9b0fb2SGreg Roach public function testGetNewXref(): void 108*8f9b0fb2SGreg Roach { 109*8f9b0fb2SGreg Roach $tree = Tree::create('tree-name', 'Tree title'); 110963fbaeeSGreg Roach 111*8f9b0fb2SGreg Roach $this->assertSame('X1', $tree->getNewXref()); 112*8f9b0fb2SGreg Roach $this->assertSame('X2', $tree->getNewXref()); 113*8f9b0fb2SGreg Roach $this->assertSame('X3', $tree->getNewXref()); 114*8f9b0fb2SGreg Roach $this->assertSame('X4', $tree->getNewXref()); 115*8f9b0fb2SGreg Roach $this->assertSame('X5', $tree->getNewXref()); 116*8f9b0fb2SGreg Roach } 117963fbaeeSGreg Roach 118*8f9b0fb2SGreg Roach /** 119*8f9b0fb2SGreg Roach * @covers \Fisharebest\Webtrees\Tree::createFamily 120*8f9b0fb2SGreg Roach * 121*8f9b0fb2SGreg Roach * @return void 122*8f9b0fb2SGreg Roach */ 123*8f9b0fb2SGreg Roach public function testCreateFamily(): void 124*8f9b0fb2SGreg Roach { 125*8f9b0fb2SGreg Roach $tree = Tree::create('tree-name', 'Tree title'); 126*8f9b0fb2SGreg Roach $user = User::create('user', 'User', 'user@example.com', 'secret'); 127*8f9b0fb2SGreg Roach $user->setPreference('canadmin', '1'); 128*8f9b0fb2SGreg Roach Auth::login($user); 129*8f9b0fb2SGreg Roach 130*8f9b0fb2SGreg Roach $record = $tree->createFamily("0 @@ FAM\n1 MARR Y"); 131*8f9b0fb2SGreg Roach $this->assertTrue($record->isPendingAddition()); 132*8f9b0fb2SGreg Roach 133*8f9b0fb2SGreg Roach $user->setPreference('auto_accept', '1'); 134*8f9b0fb2SGreg Roach $record = $tree->createFamily("0 @@ FAM\n1 MARR Y"); 135*8f9b0fb2SGreg Roach $this->assertFalse($record->isPendingAddition()); 136*8f9b0fb2SGreg Roach } 137*8f9b0fb2SGreg Roach 138*8f9b0fb2SGreg Roach /** 139*8f9b0fb2SGreg Roach * @covers \Fisharebest\Webtrees\Tree::significantIndividual 140*8f9b0fb2SGreg Roach * 141*8f9b0fb2SGreg Roach * @return void 142*8f9b0fb2SGreg Roach */ 143*8f9b0fb2SGreg Roach public function testSignificantIndividual(): void 144*8f9b0fb2SGreg Roach { 145*8f9b0fb2SGreg Roach $tree = Tree::create('tree-name', 'Tree title'); 146*8f9b0fb2SGreg Roach $user = User::create('user', 'User', 'user@example.com', 'secret'); 147*8f9b0fb2SGreg Roach $user->setPreference('auto_accept', '1'); 148*8f9b0fb2SGreg Roach Auth::login($user); 149*8f9b0fb2SGreg Roach $individual = $tree->significantIndividual($user); 150*8f9b0fb2SGreg Roach 151*8f9b0fb2SGreg Roach // No individuals in tree? Dummy individual 152*8f9b0fb2SGreg Roach $this->assertSame('I', $tree->significantIndividual($user)->xref()); 153*8f9b0fb2SGreg Roach 154*8f9b0fb2SGreg Roach $record1 = $tree->createIndividual("0 @@ INDI\n1 SEX F\n1 NAME Foo /Bar/"); 155*8f9b0fb2SGreg Roach $record2 = $tree->createIndividual("0 @@ INDI\n1 SEX F\n1 NAME Foo /Bar/"); 156*8f9b0fb2SGreg Roach $record3 = $tree->createIndividual("0 @@ INDI\n1 SEX F\n1 NAME Foo /Bar/"); 157*8f9b0fb2SGreg Roach $record4 = $tree->createIndividual("0 @@ INDI\n1 SEX F\n1 NAME Foo /Bar/"); 158*8f9b0fb2SGreg Roach 159*8f9b0fb2SGreg Roach // Individuals exist? First one (lowest XREF). 160*8f9b0fb2SGreg Roach $this->assertSame($record1->xref(), $tree->significantIndividual($user)->xref()); 161*8f9b0fb2SGreg Roach 162*8f9b0fb2SGreg Roach // Preference for tree? 163*8f9b0fb2SGreg Roach $tree->setPreference('PEDIGREE_ROOT_ID', $record2->xref()); 164*8f9b0fb2SGreg Roach $this->assertSame($record2->xref(), $tree->significantIndividual($user)->xref()); 165*8f9b0fb2SGreg Roach 166*8f9b0fb2SGreg Roach // User preference 167*8f9b0fb2SGreg Roach $tree->setUserPreference($user, 'gedcomid', $record3->xref()); 168*8f9b0fb2SGreg Roach $this->assertSame($record3->xref(), $tree->significantIndividual($user)->xref()); 169*8f9b0fb2SGreg Roach 170*8f9b0fb2SGreg Roach // User record 171*8f9b0fb2SGreg Roach $tree->setUserPreference($user, 'rootid', $record4->xref()); 172*8f9b0fb2SGreg Roach $this->assertSame($record4->xref(), $tree->significantIndividual($user)->xref()); 173963fbaeeSGreg Roach } 174a25f0a04SGreg Roach} 175