xref: /webtrees/tests/app/Services/UserServiceTest.php (revision 202c018b592d5a516e4a465dc6dc515f3be37399)
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 PHPUnit\Framework\Attributes\CoversClass;
26use Symfony\Component\Cache\Adapter\NullAdapter;
27
28
29#[CoversClass(UserService::class)]
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    public function testCreate(): void
47    {
48        $user_service = new UserService();
49
50        $user = $user_service->create('user', 'User', 'user@example.com', 'secret');
51
52        self::assertSame(1, $user->id());
53    }
54
55    public function testDelete(): void
56    {
57        $user_service = new UserService();
58        $user         = $user_service->create('user', 'User', 'user@example.com', 'secret');
59        $user_id      = $user->id();
60        $user_service->delete($user);
61
62        self::assertNull($user_service->find($user_id));
63    }
64
65    public function testFindNonExistingUser(): void
66    {
67        $user_service = new UserService();
68        $user         = $user_service->find(999);
69
70        self::assertNull($user);
71    }
72
73    public function testFindExistingUser(): void
74    {
75        $user_service = new UserService();
76        $user1        = $user_service->create('user', 'User', 'user@example.com', 'secret');
77        $user2        = $user_service->find($user1->id());
78
79        self::assertSame($user1->id(), $user2->id());
80    }
81
82    public function testFindUserByEmail(): void
83    {
84        $user_service = new UserService();
85        $user1        = $user_service->create('user', 'User', 'user@example.com', 'secret');
86        $user2        = $user_service->findByEmail($user1->email());
87
88        self::assertSame($user1->id(), $user2->id());
89    }
90
91    public function testFindUserByUserName(): void
92    {
93        $user_service = new UserService();
94        $user1        = $user_service->create('user', 'User', 'user@example.com', 'secret');
95        $user2        = $user_service->findByUserName($user1->userName());
96
97        self::assertSame($user1->id(), $user2->id());
98    }
99
100    public function testFindUserByIdentifier(): void
101    {
102        $user_service = new UserService();
103        $user1        = $user_service->create('user', 'User', 'user@example.com', 'secret');
104        $user2        = $user_service->findByIdentifier($user1->userName());
105        $user3        = $user_service->findByIdentifier($user1->email());
106
107        self::assertSame($user1->id(), $user2->id());
108        self::assertSame($user1->id(), $user3->id());
109    }
110
111    public function testFindUsersByIndividual(): void
112    {
113        $user_service = new UserService();
114        $user         = $user_service->create('user', 'User', 'user@example.com', 'secret');
115        Auth::login($user);
116        $tree = $this->importTree('demo.ged');
117        $indi = $tree->createIndividual('0 @@ INDI');
118        $tree->setUserPreference($user, UserInterface::PREF_TREE_ACCOUNT_XREF, $indi->xref());
119
120        $users = $user_service->findByIndividual($indi);
121
122        self::assertCount(1, $users);
123        self::assertSame($user->id(), $users[0]->id());
124    }
125
126    public function testFindAllUsers(): void
127    {
128        $user_service = new UserService();
129        $user1        = $user_service->create('bbbbb', 'BBBBB', 'bbbbb@example.com', 'secret');
130        $user2        = $user_service->create('aaaaa', 'AAAAA', 'aaaaa@example.com', 'secret');
131
132        $users = $user_service->all();
133
134        self::assertSame(2, $users->count());
135        self::assertSame($user2->id(), $users[0]->id());
136        self::assertSame($user1->id(), $users[1]->id());
137    }
138
139    public function testFindAdministrators(): void
140    {
141        $user_service = new UserService();
142        $user_service->create('user', 'User', 'user@example.com', 'secret');
143
144        $admin = $user_service->create('admin', 'Admin', 'admin@example.com', 'secret');
145        $admin->setPreference(UserInterface::PREF_IS_ADMINISTRATOR, '1');
146
147        $users = $user_service->administrators();
148
149        self::assertCount(1, $users);
150        self::assertSame($admin->id(), $users[0]->id());
151    }
152
153    public function testFindManagers(): void
154    {
155        $user_service = new UserService();
156        $user1        = $user_service->create('user1', 'User1', 'user1@example.com', 'secret');
157        $user2        = $user_service->create('user2', 'User2', 'user2@example.com', 'secret');
158        $user3        = $user_service->create('user3', 'User3', 'user3@example.com', 'secret');
159        $user4        = $user_service->create('user4', 'User4', 'user4@example.com', 'secret');
160
161        $tree = $this->importTree('demo.ged');
162        $tree->setUserPreference($user1, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_MANAGER);
163        $tree->setUserPreference($user2, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_MODERATOR);
164        $tree->setUserPreference($user3, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_EDITOR);
165        $tree->setUserPreference($user4, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_MEMBER);
166
167        $users = $user_service->managers();
168
169        self::assertCount(1, $users);
170        self::assertSame($user1->id(), $users[0]->id());
171    }
172
173    public function testFindModerators(): void
174    {
175        $user_service = new UserService();
176        $user1        = $user_service->create('user1', 'User1', 'user1@example.com', 'secret');
177        $user2        = $user_service->create('user2', 'User2', 'user2@example.com', 'secret');
178        $user3        = $user_service->create('user3', 'User3', 'user3@example.com', 'secret');
179        $user4        = $user_service->create('user4', 'User4', 'user4@example.com', 'secret');
180
181        $tree = $this->importTree('demo.ged');
182        $tree->setUserPreference($user1, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_MANAGER);
183        $tree->setUserPreference($user2, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_MODERATOR);
184        $tree->setUserPreference($user3, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_EDITOR);
185        $tree->setUserPreference($user4, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_MEMBER);
186
187        $users = $user_service->moderators();
188
189        self::assertCount(1, $users);
190        self::assertSame($user2->id(), $users[0]->id());
191    }
192
193    public function testFindUnapprovedAndUnverified(): void
194    {
195        $user_service = new UserService();
196        $user1        = $user_service->create('user1', 'User1', 'user1@example.com', 'secret');
197        $user2        = $user_service->create('user2', 'User2', 'user2@example.com', 'secret');
198        $user3        = $user_service->create('user3', 'User3', 'user3@example.com', 'secret');
199        $user4        = $user_service->create('user4', 'User4', 'user4@example.com', 'secret');
200
201        $user1->setPreference(UserInterface::PREF_IS_EMAIL_VERIFIED, '');
202        $user1->setPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED, '');
203        $user2->setPreference(UserInterface::PREF_IS_EMAIL_VERIFIED, '');
204        $user2->setPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED, '1');
205        $user3->setPreference(UserInterface::PREF_IS_EMAIL_VERIFIED, '1');
206        $user3->setPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED, '');
207        $user4->setPreference(UserInterface::PREF_IS_EMAIL_VERIFIED, '1');
208        $user4->setPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED, '1');
209
210        $users = $user_service->unapproved();
211
212        self::assertSame(2, $users->count());
213        self::assertSame('user1', $users[0]->userName());
214        self::assertSame('user3', $users[1]->userName());
215
216        $users = $user_service->unverified();
217
218        self::assertSame(2, $users->count());
219        self::assertSame('user1', $users[0]->userName());
220        self::assertSame('user2', $users[1]->userName());
221    }
222}
223