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