xref: /webtrees/tests/app/Services/UserServiceTest.php (revision a6d4916949516e3eeaad5077f18f158b867e0927)
1e5a6b4d4SGreg Roach<?php
23976b470SGreg Roach
3e5a6b4d4SGreg Roach/**
4e5a6b4d4SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6e5a6b4d4SGreg Roach * This program is free software: you can redistribute it and/or modify
7e5a6b4d4SGreg Roach * it under the terms of the GNU General Public License as published by
8e5a6b4d4SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9e5a6b4d4SGreg Roach * (at your option) any later version.
10e5a6b4d4SGreg Roach * This program is distributed in the hope that it will be useful,
11e5a6b4d4SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12e5a6b4d4SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13e5a6b4d4SGreg Roach * GNU General Public License for more details.
14e5a6b4d4SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16e5a6b4d4SGreg Roach */
17fcfa147eSGreg Roach
18e5a6b4d4SGreg Roachdeclare(strict_types=1);
19e5a6b4d4SGreg Roach
20e5a6b4d4SGreg Roachnamespace Fisharebest\Webtrees;
21e5a6b4d4SGreg Roach
2269675509SGreg Roachuse Fisharebest\Webtrees\Contracts\CacheFactoryInterface;
231fe542e9SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
24e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService;
25202c018bSGreg Roachuse PHPUnit\Framework\Attributes\CoversClass;
2669675509SGreg Roachuse Symfony\Component\Cache\Adapter\NullAdapter;
27e5a6b4d4SGreg Roach
28202c018bSGreg Roach#[CoversClass(UserService::class)]
29e5a6b4d4SGreg Roachclass UserServiceTest extends TestCase
30e5a6b4d4SGreg Roach{
31cd94ca66SGreg Roach    protected static bool $uses_database = true;
32e5a6b4d4SGreg Roach
3392a78a2fSGreg Roach    /**
3492a78a2fSGreg Roach     * Things to run before every test.
3592a78a2fSGreg Roach     */
3657b69958SGreg Roach    protected function setUp(): void
3769675509SGreg Roach    {
3869675509SGreg Roach        parent::setUp();
3969675509SGreg Roach
40cd94ca66SGreg Roach        $cache_factory = $this->createMock(CacheFactoryInterface::class);
4169675509SGreg Roach        $cache_factory->method('array')->willReturn(new Cache(new NullAdapter()));
426b9cb339SGreg Roach        Registry::cache($cache_factory);
4369675509SGreg Roach    }
4469675509SGreg Roach
45e5a6b4d4SGreg Roach    public function testCreate(): void
46e5a6b4d4SGreg Roach    {
47e5a6b4d4SGreg Roach        $user_service = new UserService();
48e5a6b4d4SGreg Roach
49e5a6b4d4SGreg Roach        $user = $user_service->create('user', 'User', 'user@example.com', 'secret');
50e5a6b4d4SGreg Roach
515e933c21SGreg Roach        self::assertSame(1, $user->id());
52e5a6b4d4SGreg Roach    }
53e5a6b4d4SGreg Roach
5489db8edaSGreg Roach    public function testDelete(): void
55e5a6b4d4SGreg Roach    {
56e5a6b4d4SGreg Roach        $user_service = new UserService();
57e5a6b4d4SGreg Roach        $user         = $user_service->create('user', 'User', 'user@example.com', 'secret');
5889db8edaSGreg Roach        $user_id      = $user->id();
5989db8edaSGreg Roach        $user_service->delete($user);
60e5a6b4d4SGreg Roach
615e933c21SGreg Roach        self::assertNull($user_service->find($user_id));
62e5a6b4d4SGreg Roach    }
63e5a6b4d4SGreg Roach
64e5a6b4d4SGreg Roach    public function testFindNonExistingUser(): void
65e5a6b4d4SGreg Roach    {
66e5a6b4d4SGreg Roach        $user_service = new UserService();
67e5a6b4d4SGreg Roach        $user         = $user_service->find(999);
68e5a6b4d4SGreg Roach
695e933c21SGreg Roach        self::assertNull($user);
70e5a6b4d4SGreg Roach    }
71e5a6b4d4SGreg Roach
72e5a6b4d4SGreg Roach    public function testFindExistingUser(): void
73e5a6b4d4SGreg Roach    {
74e5a6b4d4SGreg Roach        $user_service = new UserService();
75e5a6b4d4SGreg Roach        $user1        = $user_service->create('user', 'User', 'user@example.com', 'secret');
76e5a6b4d4SGreg Roach        $user2        = $user_service->find($user1->id());
77e5a6b4d4SGreg Roach
785e933c21SGreg Roach        self::assertSame($user1->id(), $user2->id());
79e5a6b4d4SGreg Roach    }
80e5a6b4d4SGreg Roach
81e5a6b4d4SGreg Roach    public function testFindUserByEmail(): void
82e5a6b4d4SGreg Roach    {
83e5a6b4d4SGreg Roach        $user_service = new UserService();
84e5a6b4d4SGreg Roach        $user1        = $user_service->create('user', 'User', 'user@example.com', 'secret');
85e5a6b4d4SGreg Roach        $user2        = $user_service->findByEmail($user1->email());
86e5a6b4d4SGreg Roach
875e933c21SGreg Roach        self::assertSame($user1->id(), $user2->id());
88e5a6b4d4SGreg Roach    }
89e5a6b4d4SGreg Roach
90e5a6b4d4SGreg Roach    public function testFindUserByUserName(): void
91e5a6b4d4SGreg Roach    {
92e5a6b4d4SGreg Roach        $user_service = new UserService();
93e5a6b4d4SGreg Roach        $user1        = $user_service->create('user', 'User', 'user@example.com', 'secret');
94e5a6b4d4SGreg Roach        $user2        = $user_service->findByUserName($user1->userName());
95e5a6b4d4SGreg Roach
965e933c21SGreg Roach        self::assertSame($user1->id(), $user2->id());
97e5a6b4d4SGreg Roach    }
98e5a6b4d4SGreg Roach
99e5a6b4d4SGreg Roach    public function testFindUserByIdentifier(): void
100e5a6b4d4SGreg Roach    {
101e5a6b4d4SGreg Roach        $user_service = new UserService();
102e5a6b4d4SGreg Roach        $user1        = $user_service->create('user', 'User', 'user@example.com', 'secret');
103e5a6b4d4SGreg Roach        $user2        = $user_service->findByIdentifier($user1->userName());
104e5a6b4d4SGreg Roach        $user3        = $user_service->findByIdentifier($user1->email());
105e5a6b4d4SGreg Roach
1065e933c21SGreg Roach        self::assertSame($user1->id(), $user2->id());
1075e933c21SGreg Roach        self::assertSame($user1->id(), $user3->id());
108e5a6b4d4SGreg Roach    }
109e5a6b4d4SGreg Roach
110e5a6b4d4SGreg Roach    public function testFindUsersByIndividual(): void
111e5a6b4d4SGreg Roach    {
112e5a6b4d4SGreg Roach        $user_service = new UserService();
113e5a6b4d4SGreg Roach        $user         = $user_service->create('user', 'User', 'user@example.com', 'secret');
114e5a6b4d4SGreg Roach        Auth::login($user);
115e5a6b4d4SGreg Roach        $tree = $this->importTree('demo.ged');
116e5a6b4d4SGreg Roach        $indi = $tree->createIndividual('0 @@ INDI');
1171fe542e9SGreg Roach        $tree->setUserPreference($user, UserInterface::PREF_TREE_ACCOUNT_XREF, $indi->xref());
118e5a6b4d4SGreg Roach
119e5a6b4d4SGreg Roach        $users = $user_service->findByIndividual($indi);
120e5a6b4d4SGreg Roach
1215e933c21SGreg Roach        self::assertCount(1, $users);
1225e933c21SGreg Roach        self::assertSame($user->id(), $users[0]->id());
123e5a6b4d4SGreg Roach    }
124e5a6b4d4SGreg Roach
125e5a6b4d4SGreg Roach    public function testFindAllUsers(): void
126e5a6b4d4SGreg Roach    {
127e5a6b4d4SGreg Roach        $user_service = new UserService();
128e5a6b4d4SGreg Roach        $user1        = $user_service->create('bbbbb', 'BBBBB', 'bbbbb@example.com', 'secret');
129e5a6b4d4SGreg Roach        $user2        = $user_service->create('aaaaa', 'AAAAA', 'aaaaa@example.com', 'secret');
130e5a6b4d4SGreg Roach
131e5a6b4d4SGreg Roach        $users = $user_service->all();
132e5a6b4d4SGreg Roach
133*a6d49169SGreg Roach        self::assertCount(2, $users);
1345e933c21SGreg Roach        self::assertSame($user2->id(), $users[0]->id());
1355e933c21SGreg Roach        self::assertSame($user1->id(), $users[1]->id());
136e5a6b4d4SGreg Roach    }
137e5a6b4d4SGreg Roach
138e5a6b4d4SGreg Roach    public function testFindAdministrators(): void
139e5a6b4d4SGreg Roach    {
140e5a6b4d4SGreg Roach        $user_service = new UserService();
141e5a6b4d4SGreg Roach        $user_service->create('user', 'User', 'user@example.com', 'secret');
142e5a6b4d4SGreg Roach
143e5a6b4d4SGreg Roach        $admin = $user_service->create('admin', 'Admin', 'admin@example.com', 'secret');
1441fe542e9SGreg Roach        $admin->setPreference(UserInterface::PREF_IS_ADMINISTRATOR, '1');
145e5a6b4d4SGreg Roach
146e5a6b4d4SGreg Roach        $users = $user_service->administrators();
147e5a6b4d4SGreg Roach
1485e933c21SGreg Roach        self::assertCount(1, $users);
1495e933c21SGreg Roach        self::assertSame($admin->id(), $users[0]->id());
150e5a6b4d4SGreg Roach    }
151e5a6b4d4SGreg Roach
152e5a6b4d4SGreg Roach    public function testFindManagers(): void
153e5a6b4d4SGreg Roach    {
154e5a6b4d4SGreg Roach        $user_service = new UserService();
155e5a6b4d4SGreg Roach        $user1        = $user_service->create('user1', 'User1', 'user1@example.com', 'secret');
156e5a6b4d4SGreg Roach        $user2        = $user_service->create('user2', 'User2', 'user2@example.com', 'secret');
157e5a6b4d4SGreg Roach        $user3        = $user_service->create('user3', 'User3', 'user3@example.com', 'secret');
158e5a6b4d4SGreg Roach        $user4        = $user_service->create('user4', 'User4', 'user4@example.com', 'secret');
159e5a6b4d4SGreg Roach
160e5a6b4d4SGreg Roach        $tree = $this->importTree('demo.ged');
1611fe542e9SGreg Roach        $tree->setUserPreference($user1, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_MANAGER);
1621fe542e9SGreg Roach        $tree->setUserPreference($user2, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_MODERATOR);
1631fe542e9SGreg Roach        $tree->setUserPreference($user3, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_EDITOR);
1641fe542e9SGreg Roach        $tree->setUserPreference($user4, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_MEMBER);
165e5a6b4d4SGreg Roach
166e5a6b4d4SGreg Roach        $users = $user_service->managers();
167e5a6b4d4SGreg Roach
1685e933c21SGreg Roach        self::assertCount(1, $users);
1695e933c21SGreg Roach        self::assertSame($user1->id(), $users[0]->id());
170e5a6b4d4SGreg Roach    }
171e5a6b4d4SGreg Roach
172e5a6b4d4SGreg Roach    public function testFindModerators(): void
173e5a6b4d4SGreg Roach    {
174e5a6b4d4SGreg Roach        $user_service = new UserService();
175e5a6b4d4SGreg Roach        $user1        = $user_service->create('user1', 'User1', 'user1@example.com', 'secret');
176e5a6b4d4SGreg Roach        $user2        = $user_service->create('user2', 'User2', 'user2@example.com', 'secret');
177e5a6b4d4SGreg Roach        $user3        = $user_service->create('user3', 'User3', 'user3@example.com', 'secret');
178e5a6b4d4SGreg Roach        $user4        = $user_service->create('user4', 'User4', 'user4@example.com', 'secret');
179e5a6b4d4SGreg Roach
180e5a6b4d4SGreg Roach        $tree = $this->importTree('demo.ged');
1811fe542e9SGreg Roach        $tree->setUserPreference($user1, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_MANAGER);
1821fe542e9SGreg Roach        $tree->setUserPreference($user2, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_MODERATOR);
1831fe542e9SGreg Roach        $tree->setUserPreference($user3, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_EDITOR);
1841fe542e9SGreg Roach        $tree->setUserPreference($user4, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_MEMBER);
185e5a6b4d4SGreg Roach
186e5a6b4d4SGreg Roach        $users = $user_service->moderators();
187e5a6b4d4SGreg Roach
1885e933c21SGreg Roach        self::assertCount(1, $users);
1895e933c21SGreg Roach        self::assertSame($user2->id(), $users[0]->id());
190e5a6b4d4SGreg Roach    }
191e5a6b4d4SGreg Roach
192e5a6b4d4SGreg Roach    public function testFindUnapprovedAndUnverified(): void
193e5a6b4d4SGreg Roach    {
194e5a6b4d4SGreg Roach        $user_service = new UserService();
195e5a6b4d4SGreg Roach        $user1        = $user_service->create('user1', 'User1', 'user1@example.com', 'secret');
196e5a6b4d4SGreg Roach        $user2        = $user_service->create('user2', 'User2', 'user2@example.com', 'secret');
197e5a6b4d4SGreg Roach        $user3        = $user_service->create('user3', 'User3', 'user3@example.com', 'secret');
198e5a6b4d4SGreg Roach        $user4        = $user_service->create('user4', 'User4', 'user4@example.com', 'secret');
199e5a6b4d4SGreg Roach
2001fe542e9SGreg Roach        $user1->setPreference(UserInterface::PREF_IS_EMAIL_VERIFIED, '');
2011fe542e9SGreg Roach        $user1->setPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED, '');
2021fe542e9SGreg Roach        $user2->setPreference(UserInterface::PREF_IS_EMAIL_VERIFIED, '');
2031fe542e9SGreg Roach        $user2->setPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED, '1');
2041fe542e9SGreg Roach        $user3->setPreference(UserInterface::PREF_IS_EMAIL_VERIFIED, '1');
2051fe542e9SGreg Roach        $user3->setPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED, '');
2061fe542e9SGreg Roach        $user4->setPreference(UserInterface::PREF_IS_EMAIL_VERIFIED, '1');
2071fe542e9SGreg Roach        $user4->setPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED, '1');
208e5a6b4d4SGreg Roach
209e5a6b4d4SGreg Roach        $users = $user_service->unapproved();
210e5a6b4d4SGreg Roach
211*a6d49169SGreg Roach        self::assertCount(2, $users);
2125e933c21SGreg Roach        self::assertSame('user1', $users[0]->userName());
2135e933c21SGreg Roach        self::assertSame('user3', $users[1]->userName());
214e5a6b4d4SGreg Roach
215e5a6b4d4SGreg Roach        $users = $user_service->unverified();
216e5a6b4d4SGreg Roach
217*a6d49169SGreg Roach        self::assertCount(2, $users);
2185e933c21SGreg Roach        self::assertSame('user1', $users[0]->userName());
2195e933c21SGreg Roach        self::assertSame('user2', $users[1]->userName());
220e5a6b4d4SGreg Roach    }
221e5a6b4d4SGreg Roach}
222