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