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