xref: /webtrees/tests/app/Services/UserServiceTest.php (revision da2d94b3c5b527bc60619c89c58b126cd0a846fa)
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
20use Fisharebest\Webtrees\Services\UserService;
21
22/**
23 * Test the UserService class
24 */
25class UserServiceTest extends TestCase
26{
27    protected static $uses_database = true;
28
29    /**
30     * @covers \Fisharebest\Webtrees\Services\UserService::create
31     * @return void
32     */
33    public function testCreate(): void
34    {
35        $user_service = new UserService();
36
37        $user = $user_service->create('user', 'User', 'user@example.com', 'secret');
38
39        $this->assertSame(1, $user->id());
40    }
41
42    /**
43     * @covers \Fisharebest\Webtrees\Services\UserService::delete
44     * @return void
45     */
46    public function testDelete(): void
47    {
48        $user_service = new UserService();
49        $user         = $user_service->create('user', 'User', 'user@example.com', 'secret');
50        $user_id      = $user->id();
51        $user_service->delete($user);
52
53        $this->assertNull($user_service->find($user_id));
54    }
55
56    /**
57     * @covers \Fisharebest\Webtrees\Services\UserService::find
58     * @return void
59     */
60    public function testFindNonExistingUser(): void
61    {
62        $user_service = new UserService();
63        $user         = $user_service->find(999);
64
65        $this->assertNull($user);
66    }
67
68    /**
69     * @covers \Fisharebest\Webtrees\Services\UserService::find
70     * @return void
71     */
72    public function testFindExistingUser(): void
73    {
74        $user_service = new UserService();
75        $user1        = $user_service->create('user', 'User', 'user@example.com', 'secret');
76        $user2        = $user_service->find($user1->id());
77
78        $this->assertSame($user1->id(), $user2->id());
79    }
80
81    /**
82     * @covers \Fisharebest\Webtrees\Services\UserService::findByEmail
83     * @return void
84     */
85    public function testFindUserByEmail(): void
86    {
87        $user_service = new UserService();
88        $user1        = $user_service->create('user', 'User', 'user@example.com', 'secret');
89        $user2        = $user_service->findByEmail($user1->email());
90
91        $this->assertSame($user1->id(), $user2->id());
92    }
93
94    /**
95     * @covers \Fisharebest\Webtrees\Services\UserService::findByUserName
96     * @return void
97     */
98    public function testFindUserByUserName(): void
99    {
100        $user_service = new UserService();
101        $user1        = $user_service->create('user', 'User', 'user@example.com', 'secret');
102        $user2        = $user_service->findByUserName($user1->userName());
103
104        $this->assertSame($user1->id(), $user2->id());
105    }
106
107    /**
108     * @covers \Fisharebest\Webtrees\Services\UserService::findByIdentifier
109     * @return void
110     */
111    public function testFindUserByIdentifier(): void
112    {
113        $user_service = new UserService();
114        $user1        = $user_service->create('user', 'User', 'user@example.com', 'secret');
115        $user2        = $user_service->findByIdentifier($user1->userName());
116        $user3        = $user_service->findByIdentifier($user1->email());
117
118        $this->assertSame($user1->id(), $user2->id());
119        $this->assertSame($user1->id(), $user3->id());
120    }
121
122    /**
123     * @covers \Fisharebest\Webtrees\Services\UserService::findByIndividual
124     * @return void
125     */
126    public function testFindUsersByIndividual(): void
127    {
128        $user_service = new UserService();
129        $user         = $user_service->create('user', 'User', 'user@example.com', 'secret');
130        Auth::login($user);
131        $tree = $this->importTree('demo.ged');
132        $indi = $tree->createIndividual('0 @@ INDI');
133        $tree->setUserPreference($user, 'gedcomid', $indi->xref());
134
135        $users = $user_service->findByIndividual($indi);
136
137        $this->assertCount(1, $users);
138        $this->assertSame($user->id(), $users[0]->id());
139    }
140
141    /**
142     * @covers \Fisharebest\Webtrees\Services\UserService::all
143     * @return void
144     */
145    public function testFindAllUsers(): void
146    {
147        $user_service = new UserService();
148        $user1        = $user_service->create('bbbbb', 'BBBBB', 'bbbbb@example.com', 'secret');
149        $user2        = $user_service->create('aaaaa', 'AAAAA', 'aaaaa@example.com', 'secret');
150
151        $users = $user_service->all();
152
153        $this->assertSame(2, $users->count());
154        $this->assertSame($user2->id(), $users[0]->id());
155        $this->assertSame($user1->id(), $users[1]->id());
156    }
157
158    /**
159     * @covers \Fisharebest\Webtrees\Services\UserService::administrators
160     * @return void
161     */
162    public function testFindAdministrators(): void
163    {
164        $user_service = new UserService();
165        $user_service->create('user', 'User', 'user@example.com', 'secret');
166
167        $admin = $user_service->create('admin', 'Admin', 'admin@example.com', 'secret');
168        $admin->setPreference('canadmin', '1');
169
170        $users = $user_service->administrators();
171
172        $this->assertCount(1, $users);
173        $this->assertSame($admin->id(), $users[0]->id());
174    }
175
176    /**
177     * @covers \Fisharebest\Webtrees\Services\UserService::managers
178     * @return void
179     */
180    public function testFindManagers(): void
181    {
182        $user_service = new UserService();
183        $user1        = $user_service->create('user1', 'User1', 'user1@example.com', 'secret');
184        $user2        = $user_service->create('user2', 'User2', 'user2@example.com', 'secret');
185        $user3        = $user_service->create('user3', 'User3', 'user3@example.com', 'secret');
186        $user4        = $user_service->create('user4', 'User4', 'user4@example.com', 'secret');
187
188        $tree = $this->importTree('demo.ged');
189        $tree->setUserPreference($user1, 'canedit', 'admin');
190        $tree->setUserPreference($user2, 'canedit', 'accept');
191        $tree->setUserPreference($user3, 'canedit', 'edit');
192        $tree->setUserPreference($user4, 'canedit', 'access');
193
194        $users = $user_service->managers();
195
196        $this->assertCount(1, $users);
197        $this->assertSame($user1->id(), $users[0]->id());
198    }
199
200    /**
201     * @covers \Fisharebest\Webtrees\Services\UserService::moderators
202     * @return void
203     */
204    public function testFindModerators(): void
205    {
206        $user_service = new UserService();
207        $user1        = $user_service->create('user1', 'User1', 'user1@example.com', 'secret');
208        $user2        = $user_service->create('user2', 'User2', 'user2@example.com', 'secret');
209        $user3        = $user_service->create('user3', 'User3', 'user3@example.com', 'secret');
210        $user4        = $user_service->create('user4', 'User4', 'user4@example.com', 'secret');
211
212        $tree = $this->importTree('demo.ged');
213        $tree->setUserPreference($user1, 'canedit', 'admin');
214        $tree->setUserPreference($user2, 'canedit', 'accept');
215        $tree->setUserPreference($user3, 'canedit', 'edit');
216        $tree->setUserPreference($user4, 'canedit', 'access');
217
218        $users = $user_service->moderators();
219
220        $this->assertCount(1, $users);
221        $this->assertSame($user2->id(), $users[0]->id());
222    }
223
224    /**
225     * @covers \Fisharebest\Webtrees\Services\UserService::unapproved
226     * @covers \Fisharebest\Webtrees\Services\UserService::unverified
227     * @return void
228     */
229    public function testFindUnapprovedAndUnverified(): void
230    {
231        $user_service = new UserService();
232        $user1        = $user_service->create('user1', 'User1', 'user1@example.com', 'secret');
233        $user2        = $user_service->create('user2', 'User2', 'user2@example.com', 'secret');
234        $user3        = $user_service->create('user3', 'User3', 'user3@example.com', 'secret');
235        $user4        = $user_service->create('user4', 'User4', 'user4@example.com', 'secret');
236
237        $user1->setPreference('verified', '0');
238        $user1->setPreference('verified_by_admin', '0');
239        $user2->setPreference('verified', '0');
240        $user2->setPreference('verified_by_admin', '1');
241        $user3->setPreference('verified', '1');
242        $user3->setPreference('verified_by_admin', '0');
243        $user4->setPreference('verified', '1');
244        $user4->setPreference('verified_by_admin', '1');
245
246        $users = $user_service->unapproved();
247
248        $this->assertSame(2, $users->count());
249        $this->assertSame($user1->id(), $users[0]->id());
250        $this->assertSame($user3->id(), $users[1]->id());
251
252        $users = $user_service->unverified();
253
254        $this->assertSame(2, $users->count());
255        $this->assertSame($user1->id(), $users[0]->id());
256        $this->assertSame($user2->id(), $users[1]->id());
257    }
258}
259