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