xref: /webtrees/tests/app/TreeTest.php (revision 8f9b0fb289eccd0c5057286c6cfe4391622acc59)
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::createIndividual
84     *
85     * @return void
86     */
87    public function testCreateIndividual(): void
88    {
89        $tree = Tree::create('tree-name', 'Tree title');
90        $user = User::create('user', 'User', 'user@example.com', 'secret');
91        $user->setPreference('canadmin', '1');
92        Auth::login($user);
93
94        $record = $tree->createIndividual("0 @@ INDI\n1 SEX F\n1 NAME Foo /Bar/");
95        $this->assertTrue($record->isPendingAddition());
96
97        $user->setPreference('auto_accept', '1');
98        $record = $tree->createIndividual("0 @@ INDI\n1 SEX F\n1 NAME Foo /Bar/");
99        $this->assertFalse($record->isPendingAddition());
100    }
101
102    /**
103     * @covers \Fisharebest\Webtrees\Tree::getNewXref
104     *
105     * @return void
106     */
107    public function testGetNewXref(): void
108    {
109        $tree = Tree::create('tree-name', 'Tree title');
110
111        $this->assertSame('X1', $tree->getNewXref());
112        $this->assertSame('X2', $tree->getNewXref());
113        $this->assertSame('X3', $tree->getNewXref());
114        $this->assertSame('X4', $tree->getNewXref());
115        $this->assertSame('X5', $tree->getNewXref());
116    }
117
118    /**
119     * @covers \Fisharebest\Webtrees\Tree::createFamily
120     *
121     * @return void
122     */
123    public function testCreateFamily(): void
124    {
125        $tree = Tree::create('tree-name', 'Tree title');
126        $user = User::create('user', 'User', 'user@example.com', 'secret');
127        $user->setPreference('canadmin', '1');
128        Auth::login($user);
129
130        $record = $tree->createFamily("0 @@ FAM\n1 MARR Y");
131        $this->assertTrue($record->isPendingAddition());
132
133        $user->setPreference('auto_accept', '1');
134        $record = $tree->createFamily("0 @@ FAM\n1 MARR Y");
135        $this->assertFalse($record->isPendingAddition());
136    }
137
138    /**
139     * @covers \Fisharebest\Webtrees\Tree::significantIndividual
140     *
141     * @return void
142     */
143    public function testSignificantIndividual(): void
144    {
145        $tree = Tree::create('tree-name', 'Tree title');
146        $user = User::create('user', 'User', 'user@example.com', 'secret');
147        $user->setPreference('auto_accept', '1');
148        Auth::login($user);
149        $individual = $tree->significantIndividual($user);
150
151        // No individuals in tree?  Dummy individual
152        $this->assertSame('I', $tree->significantIndividual($user)->xref());
153
154        $record1 = $tree->createIndividual("0 @@ INDI\n1 SEX F\n1 NAME Foo /Bar/");
155        $record2 = $tree->createIndividual("0 @@ INDI\n1 SEX F\n1 NAME Foo /Bar/");
156        $record3 = $tree->createIndividual("0 @@ INDI\n1 SEX F\n1 NAME Foo /Bar/");
157        $record4 = $tree->createIndividual("0 @@ INDI\n1 SEX F\n1 NAME Foo /Bar/");
158
159        // Individuals exist?  First one (lowest XREF).
160        $this->assertSame($record1->xref(), $tree->significantIndividual($user)->xref());
161
162        // Preference for tree?
163        $tree->setPreference('PEDIGREE_ROOT_ID', $record2->xref());
164        $this->assertSame($record2->xref(), $tree->significantIndividual($user)->xref());
165
166        // User preference
167        $tree->setUserPreference($user, 'gedcomid', $record3->xref());
168        $this->assertSame($record3->xref(), $tree->significantIndividual($user)->xref());
169
170        // User record
171        $tree->setUserPreference($user, 'rootid', $record4->xref());
172        $this->assertSame($record4->xref(), $tree->significantIndividual($user)->xref());
173    }
174}
175