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