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