1e5a6b4d4SGreg Roach<?php 23976b470SGreg Roach 3e5a6b4d4SGreg Roach/** 4e5a6b4d4SGreg Roach * webtrees: online genealogy 589f7189bSGreg Roach * Copyright (C) 2021 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; 2569675509SGreg Roachuse Symfony\Component\Cache\Adapter\NullAdapter; 26e5a6b4d4SGreg Roach 27e5a6b4d4SGreg Roach/** 28e5a6b4d4SGreg Roach * Test the UserService class 29e5a6b4d4SGreg Roach */ 30e5a6b4d4SGreg Roachclass UserServiceTest extends TestCase 31e5a6b4d4SGreg Roach{ 32cd94ca66SGreg Roach protected static bool $uses_database = true; 33e5a6b4d4SGreg Roach 34*57b69958SGreg Roach protected function setUp(): void 3569675509SGreg Roach { 3669675509SGreg Roach parent::setUp(); 3769675509SGreg Roach 38cd94ca66SGreg Roach $cache_factory = $this->createMock(CacheFactoryInterface::class); 3969675509SGreg Roach $cache_factory->method('array')->willReturn(new Cache(new NullAdapter())); 406b9cb339SGreg Roach Registry::cache($cache_factory); 4169675509SGreg Roach } 4269675509SGreg Roach 43e5a6b4d4SGreg Roach /** 44e5a6b4d4SGreg Roach * @covers \Fisharebest\Webtrees\Services\UserService::create 45e5a6b4d4SGreg Roach * @return void 46e5a6b4d4SGreg Roach */ 47e5a6b4d4SGreg Roach public function testCreate(): void 48e5a6b4d4SGreg Roach { 49e5a6b4d4SGreg Roach $user_service = new UserService(); 50e5a6b4d4SGreg Roach 51e5a6b4d4SGreg Roach $user = $user_service->create('user', 'User', 'user@example.com', 'secret'); 52e5a6b4d4SGreg Roach 535e933c21SGreg Roach self::assertSame(1, $user->id()); 54e5a6b4d4SGreg Roach } 55e5a6b4d4SGreg Roach 56e5a6b4d4SGreg Roach /** 5789db8edaSGreg Roach * @covers \Fisharebest\Webtrees\Services\UserService::delete 58e5a6b4d4SGreg Roach * @return void 59e5a6b4d4SGreg Roach */ 6089db8edaSGreg Roach public function testDelete(): void 61e5a6b4d4SGreg Roach { 62e5a6b4d4SGreg Roach $user_service = new UserService(); 63e5a6b4d4SGreg Roach $user = $user_service->create('user', 'User', 'user@example.com', 'secret'); 6489db8edaSGreg Roach $user_id = $user->id(); 6589db8edaSGreg Roach $user_service->delete($user); 66e5a6b4d4SGreg Roach 675e933c21SGreg Roach self::assertNull($user_service->find($user_id)); 68e5a6b4d4SGreg Roach } 69e5a6b4d4SGreg Roach 70e5a6b4d4SGreg Roach /** 71e5a6b4d4SGreg Roach * @covers \Fisharebest\Webtrees\Services\UserService::find 72e5a6b4d4SGreg Roach * @return void 73e5a6b4d4SGreg Roach */ 74e5a6b4d4SGreg Roach public function testFindNonExistingUser(): void 75e5a6b4d4SGreg Roach { 76e5a6b4d4SGreg Roach $user_service = new UserService(); 77e5a6b4d4SGreg Roach $user = $user_service->find(999); 78e5a6b4d4SGreg Roach 795e933c21SGreg Roach self::assertNull($user); 80e5a6b4d4SGreg Roach } 81e5a6b4d4SGreg Roach 82e5a6b4d4SGreg Roach /** 83e5a6b4d4SGreg Roach * @covers \Fisharebest\Webtrees\Services\UserService::find 84e5a6b4d4SGreg Roach * @return void 85e5a6b4d4SGreg Roach */ 86e5a6b4d4SGreg Roach public function testFindExistingUser(): void 87e5a6b4d4SGreg Roach { 88e5a6b4d4SGreg Roach $user_service = new UserService(); 89e5a6b4d4SGreg Roach $user1 = $user_service->create('user', 'User', 'user@example.com', 'secret'); 90e5a6b4d4SGreg Roach $user2 = $user_service->find($user1->id()); 91e5a6b4d4SGreg Roach 925e933c21SGreg Roach self::assertSame($user1->id(), $user2->id()); 93e5a6b4d4SGreg Roach } 94e5a6b4d4SGreg Roach 95e5a6b4d4SGreg Roach /** 96e5a6b4d4SGreg Roach * @covers \Fisharebest\Webtrees\Services\UserService::findByEmail 97e5a6b4d4SGreg Roach * @return void 98e5a6b4d4SGreg Roach */ 99e5a6b4d4SGreg Roach public function testFindUserByEmail(): 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->findByEmail($user1->email()); 104e5a6b4d4SGreg Roach 1055e933c21SGreg Roach self::assertSame($user1->id(), $user2->id()); 106e5a6b4d4SGreg Roach } 107e5a6b4d4SGreg Roach 108e5a6b4d4SGreg Roach /** 109e5a6b4d4SGreg Roach * @covers \Fisharebest\Webtrees\Services\UserService::findByUserName 110e5a6b4d4SGreg Roach * @return void 111e5a6b4d4SGreg Roach */ 112e5a6b4d4SGreg Roach public function testFindUserByUserName(): void 113e5a6b4d4SGreg Roach { 114e5a6b4d4SGreg Roach $user_service = new UserService(); 115e5a6b4d4SGreg Roach $user1 = $user_service->create('user', 'User', 'user@example.com', 'secret'); 116e5a6b4d4SGreg Roach $user2 = $user_service->findByUserName($user1->userName()); 117e5a6b4d4SGreg Roach 1185e933c21SGreg Roach self::assertSame($user1->id(), $user2->id()); 119e5a6b4d4SGreg Roach } 120e5a6b4d4SGreg Roach 121e5a6b4d4SGreg Roach /** 122e5a6b4d4SGreg Roach * @covers \Fisharebest\Webtrees\Services\UserService::findByIdentifier 123e5a6b4d4SGreg Roach * @return void 124e5a6b4d4SGreg Roach */ 125e5a6b4d4SGreg Roach public function testFindUserByIdentifier(): void 126e5a6b4d4SGreg Roach { 127e5a6b4d4SGreg Roach $user_service = new UserService(); 128e5a6b4d4SGreg Roach $user1 = $user_service->create('user', 'User', 'user@example.com', 'secret'); 129e5a6b4d4SGreg Roach $user2 = $user_service->findByIdentifier($user1->userName()); 130e5a6b4d4SGreg Roach $user3 = $user_service->findByIdentifier($user1->email()); 131e5a6b4d4SGreg Roach 1325e933c21SGreg Roach self::assertSame($user1->id(), $user2->id()); 1335e933c21SGreg Roach self::assertSame($user1->id(), $user3->id()); 134e5a6b4d4SGreg Roach } 135e5a6b4d4SGreg Roach 136e5a6b4d4SGreg Roach /** 137e5a6b4d4SGreg Roach * @covers \Fisharebest\Webtrees\Services\UserService::findByIndividual 138e5a6b4d4SGreg Roach * @return void 139e5a6b4d4SGreg Roach */ 140e5a6b4d4SGreg Roach public function testFindUsersByIndividual(): void 141e5a6b4d4SGreg Roach { 142e5a6b4d4SGreg Roach $user_service = new UserService(); 143e5a6b4d4SGreg Roach $user = $user_service->create('user', 'User', 'user@example.com', 'secret'); 144e5a6b4d4SGreg Roach Auth::login($user); 145e5a6b4d4SGreg Roach $tree = $this->importTree('demo.ged'); 146e5a6b4d4SGreg Roach $indi = $tree->createIndividual('0 @@ INDI'); 1471fe542e9SGreg Roach $tree->setUserPreference($user, UserInterface::PREF_TREE_ACCOUNT_XREF, $indi->xref()); 148e5a6b4d4SGreg Roach 149e5a6b4d4SGreg Roach $users = $user_service->findByIndividual($indi); 150e5a6b4d4SGreg Roach 1515e933c21SGreg Roach self::assertCount(1, $users); 1525e933c21SGreg Roach self::assertSame($user->id(), $users[0]->id()); 153e5a6b4d4SGreg Roach } 154e5a6b4d4SGreg Roach 155e5a6b4d4SGreg Roach /** 156e5a6b4d4SGreg Roach * @covers \Fisharebest\Webtrees\Services\UserService::all 157e5a6b4d4SGreg Roach * @return void 158e5a6b4d4SGreg Roach */ 159e5a6b4d4SGreg Roach public function testFindAllUsers(): void 160e5a6b4d4SGreg Roach { 161e5a6b4d4SGreg Roach $user_service = new UserService(); 162e5a6b4d4SGreg Roach $user1 = $user_service->create('bbbbb', 'BBBBB', 'bbbbb@example.com', 'secret'); 163e5a6b4d4SGreg Roach $user2 = $user_service->create('aaaaa', 'AAAAA', 'aaaaa@example.com', 'secret'); 164e5a6b4d4SGreg Roach 165e5a6b4d4SGreg Roach $users = $user_service->all(); 166e5a6b4d4SGreg Roach 1675e933c21SGreg Roach self::assertSame(2, $users->count()); 1685e933c21SGreg Roach self::assertSame($user2->id(), $users[0]->id()); 1695e933c21SGreg Roach self::assertSame($user1->id(), $users[1]->id()); 170e5a6b4d4SGreg Roach } 171e5a6b4d4SGreg Roach 172e5a6b4d4SGreg Roach /** 173e5a6b4d4SGreg Roach * @covers \Fisharebest\Webtrees\Services\UserService::administrators 174e5a6b4d4SGreg Roach * @return void 175e5a6b4d4SGreg Roach */ 176e5a6b4d4SGreg Roach public function testFindAdministrators(): void 177e5a6b4d4SGreg Roach { 178e5a6b4d4SGreg Roach $user_service = new UserService(); 179e5a6b4d4SGreg Roach $user_service->create('user', 'User', 'user@example.com', 'secret'); 180e5a6b4d4SGreg Roach 181e5a6b4d4SGreg Roach $admin = $user_service->create('admin', 'Admin', 'admin@example.com', 'secret'); 1821fe542e9SGreg Roach $admin->setPreference(UserInterface::PREF_IS_ADMINISTRATOR, '1'); 183e5a6b4d4SGreg Roach 184e5a6b4d4SGreg Roach $users = $user_service->administrators(); 185e5a6b4d4SGreg Roach 1865e933c21SGreg Roach self::assertCount(1, $users); 1875e933c21SGreg Roach self::assertSame($admin->id(), $users[0]->id()); 188e5a6b4d4SGreg Roach } 189e5a6b4d4SGreg Roach 190e5a6b4d4SGreg Roach /** 191e5a6b4d4SGreg Roach * @covers \Fisharebest\Webtrees\Services\UserService::managers 192e5a6b4d4SGreg Roach * @return void 193e5a6b4d4SGreg Roach */ 194e5a6b4d4SGreg Roach public function testFindManagers(): void 195e5a6b4d4SGreg Roach { 196e5a6b4d4SGreg Roach $user_service = new UserService(); 197e5a6b4d4SGreg Roach $user1 = $user_service->create('user1', 'User1', 'user1@example.com', 'secret'); 198e5a6b4d4SGreg Roach $user2 = $user_service->create('user2', 'User2', 'user2@example.com', 'secret'); 199e5a6b4d4SGreg Roach $user3 = $user_service->create('user3', 'User3', 'user3@example.com', 'secret'); 200e5a6b4d4SGreg Roach $user4 = $user_service->create('user4', 'User4', 'user4@example.com', 'secret'); 201e5a6b4d4SGreg Roach 202e5a6b4d4SGreg Roach $tree = $this->importTree('demo.ged'); 2031fe542e9SGreg Roach $tree->setUserPreference($user1, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_MANAGER); 2041fe542e9SGreg Roach $tree->setUserPreference($user2, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_MODERATOR); 2051fe542e9SGreg Roach $tree->setUserPreference($user3, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_EDITOR); 2061fe542e9SGreg Roach $tree->setUserPreference($user4, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_MEMBER); 207e5a6b4d4SGreg Roach 208e5a6b4d4SGreg Roach $users = $user_service->managers(); 209e5a6b4d4SGreg Roach 2105e933c21SGreg Roach self::assertCount(1, $users); 2115e933c21SGreg Roach self::assertSame($user1->id(), $users[0]->id()); 212e5a6b4d4SGreg Roach } 213e5a6b4d4SGreg Roach 214e5a6b4d4SGreg Roach /** 215e5a6b4d4SGreg Roach * @covers \Fisharebest\Webtrees\Services\UserService::moderators 216e5a6b4d4SGreg Roach * @return void 217e5a6b4d4SGreg Roach */ 218e5a6b4d4SGreg Roach public function testFindModerators(): void 219e5a6b4d4SGreg Roach { 220e5a6b4d4SGreg Roach $user_service = new UserService(); 221e5a6b4d4SGreg Roach $user1 = $user_service->create('user1', 'User1', 'user1@example.com', 'secret'); 222e5a6b4d4SGreg Roach $user2 = $user_service->create('user2', 'User2', 'user2@example.com', 'secret'); 223e5a6b4d4SGreg Roach $user3 = $user_service->create('user3', 'User3', 'user3@example.com', 'secret'); 224e5a6b4d4SGreg Roach $user4 = $user_service->create('user4', 'User4', 'user4@example.com', 'secret'); 225e5a6b4d4SGreg Roach 226e5a6b4d4SGreg Roach $tree = $this->importTree('demo.ged'); 2271fe542e9SGreg Roach $tree->setUserPreference($user1, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_MANAGER); 2281fe542e9SGreg Roach $tree->setUserPreference($user2, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_MODERATOR); 2291fe542e9SGreg Roach $tree->setUserPreference($user3, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_EDITOR); 2301fe542e9SGreg Roach $tree->setUserPreference($user4, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_MEMBER); 231e5a6b4d4SGreg Roach 232e5a6b4d4SGreg Roach $users = $user_service->moderators(); 233e5a6b4d4SGreg Roach 2345e933c21SGreg Roach self::assertCount(1, $users); 2355e933c21SGreg Roach self::assertSame($user2->id(), $users[0]->id()); 236e5a6b4d4SGreg Roach } 237e5a6b4d4SGreg Roach 238e5a6b4d4SGreg Roach /** 239e5a6b4d4SGreg Roach * @covers \Fisharebest\Webtrees\Services\UserService::unapproved 240e5a6b4d4SGreg Roach * @covers \Fisharebest\Webtrees\Services\UserService::unverified 241e5a6b4d4SGreg Roach * @return void 242e5a6b4d4SGreg Roach */ 243e5a6b4d4SGreg Roach public function testFindUnapprovedAndUnverified(): void 244e5a6b4d4SGreg Roach { 245e5a6b4d4SGreg Roach $user_service = new UserService(); 246e5a6b4d4SGreg Roach $user1 = $user_service->create('user1', 'User1', 'user1@example.com', 'secret'); 247e5a6b4d4SGreg Roach $user2 = $user_service->create('user2', 'User2', 'user2@example.com', 'secret'); 248e5a6b4d4SGreg Roach $user3 = $user_service->create('user3', 'User3', 'user3@example.com', 'secret'); 249e5a6b4d4SGreg Roach $user4 = $user_service->create('user4', 'User4', 'user4@example.com', 'secret'); 250e5a6b4d4SGreg Roach 2511fe542e9SGreg Roach $user1->setPreference(UserInterface::PREF_IS_EMAIL_VERIFIED, ''); 2521fe542e9SGreg Roach $user1->setPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED, ''); 2531fe542e9SGreg Roach $user2->setPreference(UserInterface::PREF_IS_EMAIL_VERIFIED, ''); 2541fe542e9SGreg Roach $user2->setPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED, '1'); 2551fe542e9SGreg Roach $user3->setPreference(UserInterface::PREF_IS_EMAIL_VERIFIED, '1'); 2561fe542e9SGreg Roach $user3->setPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED, ''); 2571fe542e9SGreg Roach $user4->setPreference(UserInterface::PREF_IS_EMAIL_VERIFIED, '1'); 2581fe542e9SGreg Roach $user4->setPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED, '1'); 259e5a6b4d4SGreg Roach 260e5a6b4d4SGreg Roach $users = $user_service->unapproved(); 261e5a6b4d4SGreg Roach 2625e933c21SGreg Roach self::assertSame(2, $users->count()); 2635e933c21SGreg Roach self::assertSame('user1', $users[0]->userName()); 2645e933c21SGreg Roach self::assertSame('user3', $users[1]->userName()); 265e5a6b4d4SGreg Roach 266e5a6b4d4SGreg Roach $users = $user_service->unverified(); 267e5a6b4d4SGreg Roach 2685e933c21SGreg Roach self::assertSame(2, $users->count()); 2695e933c21SGreg Roach self::assertSame('user1', $users[0]->userName()); 2705e933c21SGreg Roach self::assertSame('user2', $users[1]->userName()); 271e5a6b4d4SGreg Roach } 272e5a6b4d4SGreg Roach} 273