xref: /webtrees/tests/app/TreeTest.php (revision 7d76853dd5c018b2e8d9e1bfee6ef4e96d8698b3)
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::significantIndividual
208     *
209     * @return void
210     */
211    public function testSignificantIndividual(): void
212    {
213        $tree = Tree::create('tree-name', 'Tree title');
214        $user = User::create('user', 'User', 'user@example.com', 'secret');
215        $user->setPreference('auto_accept', '1');
216        Auth::login($user);
217        $individual = $tree->significantIndividual($user);
218
219        // No individuals in tree?  Dummy individual
220        $this->assertSame('I', $tree->significantIndividual($user)->xref());
221
222        $record1 = $tree->createIndividual("0 @@ INDI\n1 SEX F\n1 NAME Foo /Bar/");
223        $record2 = $tree->createIndividual("0 @@ INDI\n1 SEX F\n1 NAME Foo /Bar/");
224        $record3 = $tree->createIndividual("0 @@ INDI\n1 SEX F\n1 NAME Foo /Bar/");
225        $record4 = $tree->createIndividual("0 @@ INDI\n1 SEX F\n1 NAME Foo /Bar/");
226
227        // Individuals exist?  First one (lowest XREF).
228        $this->assertSame($record1->xref(), $tree->significantIndividual($user)->xref());
229
230        // Preference for tree?
231        $tree->setPreference('PEDIGREE_ROOT_ID', $record2->xref());
232        $this->assertSame($record2->xref(), $tree->significantIndividual($user)->xref());
233
234        // User preference
235        $tree->setUserPreference($user, 'gedcomid', $record3->xref());
236        $this->assertSame($record3->xref(), $tree->significantIndividual($user)->xref());
237
238        // User record
239        $tree->setUserPreference($user, 'rootid', $record4->xref());
240        $this->assertSame($record4->xref(), $tree->significantIndividual($user)->xref());
241    }
242}
243