xref: /webtrees/tests/app/Services/UserServiceTest.php (revision bac83be95cc96bdac6a0a180473e77cbbad83047)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees;
21
22use Fisharebest\Webtrees\Services\UserService;
23
24/**
25 * Test the UserService class
26 */
27class UserServiceTest extends TestCase
28{
29    protected static $uses_database = true;
30
31    /**
32     * @covers \Fisharebest\Webtrees\Services\UserService::create
33     * @return void
34     */
35    public function testCreate(): void
36    {
37        $user_service = new UserService();
38
39        $user = $user_service->create('user', 'User', 'user@example.com', 'secret');
40
41        $this->assertSame(1, $user->id());
42    }
43
44    /**
45     * @covers \Fisharebest\Webtrees\Services\UserService::delete
46     * @return void
47     */
48    public function testDelete(): void
49    {
50        $user_service = new UserService();
51        $user         = $user_service->create('user', 'User', 'user@example.com', 'secret');
52        $user_id      = $user->id();
53        $user_service->delete($user);
54
55        $this->assertNull($user_service->find($user_id));
56    }
57
58    /**
59     * @covers \Fisharebest\Webtrees\Services\UserService::find
60     * @return void
61     */
62    public function testFindNonExistingUser(): void
63    {
64        $user_service = new UserService();
65        $user         = $user_service->find(999);
66
67        $this->assertNull($user);
68    }
69
70    /**
71     * @covers \Fisharebest\Webtrees\Services\UserService::find
72     * @return void
73     */
74    public function testFindExistingUser(): void
75    {
76        $user_service = new UserService();
77        $user1        = $user_service->create('user', 'User', 'user@example.com', 'secret');
78        $user2        = $user_service->find($user1->id());
79
80        $this->assertSame($user1->id(), $user2->id());
81    }
82
83    /**
84     * @covers \Fisharebest\Webtrees\Services\UserService::findByEmail
85     * @return void
86     */
87    public function testFindUserByEmail(): void
88    {
89        $user_service = new UserService();
90        $user1        = $user_service->create('user', 'User', 'user@example.com', 'secret');
91        $user2        = $user_service->findByEmail($user1->email());
92
93        $this->assertSame($user1->id(), $user2->id());
94    }
95
96    /**
97     * @covers \Fisharebest\Webtrees\Services\UserService::findByUserName
98     * @return void
99     */
100    public function testFindUserByUserName(): void
101    {
102        $user_service = new UserService();
103        $user1        = $user_service->create('user', 'User', 'user@example.com', 'secret');
104        $user2        = $user_service->findByUserName($user1->userName());
105
106        $this->assertSame($user1->id(), $user2->id());
107    }
108
109    /**
110     * @covers \Fisharebest\Webtrees\Services\UserService::findByIdentifier
111     * @return void
112     */
113    public function testFindUserByIdentifier(): void
114    {
115        $user_service = new UserService();
116        $user1        = $user_service->create('user', 'User', 'user@example.com', 'secret');
117        $user2        = $user_service->findByIdentifier($user1->userName());
118        $user3        = $user_service->findByIdentifier($user1->email());
119
120        $this->assertSame($user1->id(), $user2->id());
121        $this->assertSame($user1->id(), $user3->id());
122    }
123
124    /**
125     * @covers \Fisharebest\Webtrees\Services\UserService::findByIndividual
126     * @return void
127     */
128    public function testFindUsersByIndividual(): void
129    {
130        $user_service = new UserService();
131        $user         = $user_service->create('user', 'User', 'user@example.com', 'secret');
132        Auth::login($user);
133        $tree = $this->importTree('demo.ged');
134        $indi = $tree->createIndividual('0 @@ INDI');
135        $tree->setUserPreference($user, User::PREF_TREE_ACCOUNT_XREF, $indi->xref());
136
137        $users = $user_service->findByIndividual($indi);
138
139        $this->assertCount(1, $users);
140        $this->assertSame($user->id(), $users[0]->id());
141    }
142
143    /**
144     * @covers \Fisharebest\Webtrees\Services\UserService::all
145     * @return void
146     */
147    public function testFindAllUsers(): void
148    {
149        $user_service = new UserService();
150        $user1        = $user_service->create('bbbbb', 'BBBBB', 'bbbbb@example.com', 'secret');
151        $user2        = $user_service->create('aaaaa', 'AAAAA', 'aaaaa@example.com', 'secret');
152
153        $users = $user_service->all();
154
155        $this->assertSame(2, $users->count());
156        $this->assertSame($user2->id(), $users[0]->id());
157        $this->assertSame($user1->id(), $users[1]->id());
158    }
159
160    /**
161     * @covers \Fisharebest\Webtrees\Services\UserService::administrators
162     * @return void
163     */
164    public function testFindAdministrators(): void
165    {
166        $user_service = new UserService();
167        $user_service->create('user', 'User', 'user@example.com', 'secret');
168
169        $admin = $user_service->create('admin', 'Admin', 'admin@example.com', 'secret');
170        $admin->setPreference(User::PREF_IS_ADMINISTRATOR, '1');
171
172        $users = $user_service->administrators();
173
174        $this->assertCount(1, $users);
175        $this->assertSame($admin->id(), $users[0]->id());
176    }
177
178    /**
179     * @covers \Fisharebest\Webtrees\Services\UserService::managers
180     * @return void
181     */
182    public function testFindManagers(): void
183    {
184        $user_service = new UserService();
185        $user1        = $user_service->create('user1', 'User1', 'user1@example.com', 'secret');
186        $user2        = $user_service->create('user2', 'User2', 'user2@example.com', 'secret');
187        $user3        = $user_service->create('user3', 'User3', 'user3@example.com', 'secret');
188        $user4        = $user_service->create('user4', 'User4', 'user4@example.com', 'secret');
189
190        $tree = $this->importTree('demo.ged');
191        $tree->setUserPreference($user1, User::PREF_TREE_ROLE, User::ROLE_MANAGER);
192        $tree->setUserPreference($user2, User::PREF_TREE_ROLE, User::ROLE_MODERATOR);
193        $tree->setUserPreference($user3, User::PREF_TREE_ROLE, User::ROLE_EDITOR);
194        $tree->setUserPreference($user4, User::PREF_TREE_ROLE, User::ROLE_MEMBER);
195
196        $users = $user_service->managers();
197
198        $this->assertCount(1, $users);
199        $this->assertSame($user1->id(), $users[0]->id());
200    }
201
202    /**
203     * @covers \Fisharebest\Webtrees\Services\UserService::moderators
204     * @return void
205     */
206    public function testFindModerators(): void
207    {
208        $user_service = new UserService();
209        $user1        = $user_service->create('user1', 'User1', 'user1@example.com', 'secret');
210        $user2        = $user_service->create('user2', 'User2', 'user2@example.com', 'secret');
211        $user3        = $user_service->create('user3', 'User3', 'user3@example.com', 'secret');
212        $user4        = $user_service->create('user4', 'User4', 'user4@example.com', 'secret');
213
214        $tree = $this->importTree('demo.ged');
215        $tree->setUserPreference($user1, User::PREF_TREE_ROLE, User::ROLE_MANAGER);
216        $tree->setUserPreference($user2, User::PREF_TREE_ROLE, User::ROLE_MODERATOR);
217        $tree->setUserPreference($user3, User::PREF_TREE_ROLE, User::ROLE_EDITOR);
218        $tree->setUserPreference($user4, User::PREF_TREE_ROLE, User::ROLE_MEMBER);
219
220        $users = $user_service->moderators();
221
222        $this->assertCount(1, $users);
223        $this->assertSame($user2->id(), $users[0]->id());
224    }
225
226    /**
227     * @covers \Fisharebest\Webtrees\Services\UserService::unapproved
228     * @covers \Fisharebest\Webtrees\Services\UserService::unverified
229     * @return void
230     */
231    public function testFindUnapprovedAndUnverified(): void
232    {
233        $user_service = new UserService();
234        $user1        = $user_service->create('user1', 'User1', 'user1@example.com', 'secret');
235        $user2        = $user_service->create('user2', 'User2', 'user2@example.com', 'secret');
236        $user3        = $user_service->create('user3', 'User3', 'user3@example.com', 'secret');
237        $user4        = $user_service->create('user4', 'User4', 'user4@example.com', 'secret');
238
239        $user1->setPreference(User::PREF_IS_EMAIL_VERIFIED, '');
240        $user1->setPreference(User::PREF_IS_ACCOUNT_APPROVED, '');
241        $user2->setPreference(User::PREF_IS_EMAIL_VERIFIED, '');
242        $user2->setPreference(User::PREF_IS_ACCOUNT_APPROVED, '1');
243        $user3->setPreference(User::PREF_IS_EMAIL_VERIFIED, '1');
244        $user3->setPreference(User::PREF_IS_ACCOUNT_APPROVED, '');
245        $user4->setPreference(User::PREF_IS_EMAIL_VERIFIED, '1');
246        $user4->setPreference(User::PREF_IS_ACCOUNT_APPROVED, '1');
247
248        $users = $user_service->unapproved();
249
250        $this->assertSame(2, $users->count());
251        $this->assertSame('user1', $users[0]->userName());
252        $this->assertSame('user3', $users[1]->userName());
253
254        $users = $user_service->unverified();
255
256        $this->assertSame(2, $users->count());
257        $this->assertSame('user1', $users[0]->userName());
258        $this->assertSame('user2', $users[1]->userName());
259    }
260}
261