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