1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2019 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\Services\UserService; 23use Illuminate\Support\Facades\DB; 24 25/** 26 * Test the UserService class 27 */ 28class UserServiceTest extends TestCase 29{ 30 protected static $uses_database = true; 31 32 /** 33 * @covers \Fisharebest\Webtrees\Services\UserService::create 34 * @return void 35 */ 36 public function testCreate(): void 37 { 38 $user_service = new UserService(); 39 40 $user = $user_service->create('user', 'User', 'user@example.com', 'secret'); 41 42 $this->assertSame(1, $user->id()); 43 } 44 45 /** 46 * @covers \Fisharebest\Webtrees\Services\UserService::delete 47 * @return void 48 */ 49 public function testDelete(): void 50 { 51 $user_service = new UserService(); 52 $user = $user_service->create('user', 'User', 'user@example.com', 'secret'); 53 $user_id = $user->id(); 54 $user_service->delete($user); 55 56 $this->assertNull($user_service->find($user_id)); 57 } 58 59 /** 60 * @covers \Fisharebest\Webtrees\Services\UserService::find 61 * @return void 62 */ 63 public function testFindNonExistingUser(): void 64 { 65 $user_service = new UserService(); 66 $user = $user_service->find(999); 67 68 $this->assertNull($user); 69 } 70 71 /** 72 * @covers \Fisharebest\Webtrees\Services\UserService::find 73 * @return void 74 */ 75 public function testFindExistingUser(): void 76 { 77 $user_service = new UserService(); 78 $user1 = $user_service->create('user', 'User', 'user@example.com', 'secret'); 79 $user2 = $user_service->find($user1->id()); 80 81 $this->assertSame($user1->id(), $user2->id()); 82 } 83 84 /** 85 * @covers \Fisharebest\Webtrees\Services\UserService::findByEmail 86 * @return void 87 */ 88 public function testFindUserByEmail(): void 89 { 90 $user_service = new UserService(); 91 $user1 = $user_service->create('user', 'User', 'user@example.com', 'secret'); 92 $user2 = $user_service->findByEmail($user1->email()); 93 94 $this->assertSame($user1->id(), $user2->id()); 95 } 96 97 /** 98 * @covers \Fisharebest\Webtrees\Services\UserService::findByUserName 99 * @return void 100 */ 101 public function testFindUserByUserName(): void 102 { 103 $user_service = new UserService(); 104 $user1 = $user_service->create('user', 'User', 'user@example.com', 'secret'); 105 $user2 = $user_service->findByUserName($user1->userName()); 106 107 $this->assertSame($user1->id(), $user2->id()); 108 } 109 110 /** 111 * @covers \Fisharebest\Webtrees\Services\UserService::findByIdentifier 112 * @return void 113 */ 114 public function testFindUserByIdentifier(): void 115 { 116 $user_service = new UserService(); 117 $user1 = $user_service->create('user', 'User', 'user@example.com', 'secret'); 118 $user2 = $user_service->findByIdentifier($user1->userName()); 119 $user3 = $user_service->findByIdentifier($user1->email()); 120 121 $this->assertSame($user1->id(), $user2->id()); 122 $this->assertSame($user1->id(), $user3->id()); 123 } 124 125 /** 126 * @covers \Fisharebest\Webtrees\Services\UserService::findByIndividual 127 * @return void 128 */ 129 public function testFindUsersByIndividual(): void 130 { 131 $user_service = new UserService(); 132 $user = $user_service->create('user', 'User', 'user@example.com', 'secret'); 133 Auth::login($user); 134 $tree = $this->importTree('demo.ged'); 135 $indi = $tree->createIndividual('0 @@ INDI'); 136 $tree->setUserPreference($user, User::PREF_TREE_ACCOUNT_XREF, $indi->xref()); 137 138 $users = $user_service->findByIndividual($indi); 139 140 $this->assertCount(1, $users); 141 $this->assertSame($user->id(), $users[0]->id()); 142 } 143 144 /** 145 * @covers \Fisharebest\Webtrees\Services\UserService::all 146 * @return void 147 */ 148 public function testFindAllUsers(): void 149 { 150 $user_service = new UserService(); 151 $user1 = $user_service->create('bbbbb', 'BBBBB', 'bbbbb@example.com', 'secret'); 152 $user2 = $user_service->create('aaaaa', 'AAAAA', 'aaaaa@example.com', 'secret'); 153 154 $users = $user_service->all(); 155 156 $this->assertSame(2, $users->count()); 157 $this->assertSame($user2->id(), $users[0]->id()); 158 $this->assertSame($user1->id(), $users[1]->id()); 159 } 160 161 /** 162 * @covers \Fisharebest\Webtrees\Services\UserService::administrators 163 * @return void 164 */ 165 public function testFindAdministrators(): void 166 { 167 $user_service = new UserService(); 168 $user_service->create('user', 'User', 'user@example.com', 'secret'); 169 170 $admin = $user_service->create('admin', 'Admin', 'admin@example.com', 'secret'); 171 $admin->setPreference(User::PREF_IS_ADMINISTRATOR, '1'); 172 173 $users = $user_service->administrators(); 174 175 $this->assertCount(1, $users); 176 $this->assertSame($admin->id(), $users[0]->id()); 177 } 178 179 /** 180 * @covers \Fisharebest\Webtrees\Services\UserService::managers 181 * @return void 182 */ 183 public function testFindManagers(): void 184 { 185 $user_service = new UserService(); 186 $user1 = $user_service->create('user1', 'User1', 'user1@example.com', 'secret'); 187 $user2 = $user_service->create('user2', 'User2', 'user2@example.com', 'secret'); 188 $user3 = $user_service->create('user3', 'User3', 'user3@example.com', 'secret'); 189 $user4 = $user_service->create('user4', 'User4', 'user4@example.com', 'secret'); 190 191 $tree = $this->importTree('demo.ged'); 192 $tree->setUserPreference($user1, User::PREF_TREE_ROLE, User::ROLE_MANAGER); 193 $tree->setUserPreference($user2, User::PREF_TREE_ROLE, User::ROLE_MODERATOR); 194 $tree->setUserPreference($user3, User::PREF_TREE_ROLE, User::ROLE_EDITOR); 195 $tree->setUserPreference($user4, User::PREF_TREE_ROLE, User::ROLE_MEMBER); 196 197 $users = $user_service->managers(); 198 199 $this->assertCount(1, $users); 200 $this->assertSame($user1->id(), $users[0]->id()); 201 } 202 203 /** 204 * @covers \Fisharebest\Webtrees\Services\UserService::moderators 205 * @return void 206 */ 207 public function testFindModerators(): void 208 { 209 $user_service = new UserService(); 210 $user1 = $user_service->create('user1', 'User1', 'user1@example.com', 'secret'); 211 $user2 = $user_service->create('user2', 'User2', 'user2@example.com', 'secret'); 212 $user3 = $user_service->create('user3', 'User3', 'user3@example.com', 'secret'); 213 $user4 = $user_service->create('user4', 'User4', 'user4@example.com', 'secret'); 214 215 $tree = $this->importTree('demo.ged'); 216 $tree->setUserPreference($user1, User::PREF_TREE_ROLE, User::ROLE_MANAGER); 217 $tree->setUserPreference($user2, User::PREF_TREE_ROLE, User::ROLE_MODERATOR); 218 $tree->setUserPreference($user3, User::PREF_TREE_ROLE, User::ROLE_EDITOR); 219 $tree->setUserPreference($user4, User::PREF_TREE_ROLE, User::ROLE_MEMBER); 220 221 $users = $user_service->moderators(); 222 223 $this->assertCount(1, $users); 224 $this->assertSame($user2->id(), $users[0]->id()); 225 } 226 227 /** 228 * @covers \Fisharebest\Webtrees\Services\UserService::unapproved 229 * @covers \Fisharebest\Webtrees\Services\UserService::unverified 230 * @return void 231 */ 232 public function testFindUnapprovedAndUnverified(): void 233 { 234 $user_service = new UserService(); 235 $user1 = $user_service->create('user1', 'User1', 'user1@example.com', 'secret'); 236 $user2 = $user_service->create('user2', 'User2', 'user2@example.com', 'secret'); 237 $user3 = $user_service->create('user3', 'User3', 'user3@example.com', 'secret'); 238 $user4 = $user_service->create('user4', 'User4', 'user4@example.com', 'secret'); 239 240 $user1->setPreference(User::PREF_IS_EMAIL_VERIFIED, ''); 241 $user1->setPreference(User::PREF_IS_ACCOUNT_APPROVED, ''); 242 $user2->setPreference(User::PREF_IS_EMAIL_VERIFIED, ''); 243 $user2->setPreference(User::PREF_IS_ACCOUNT_APPROVED, '1'); 244 $user3->setPreference(User::PREF_IS_EMAIL_VERIFIED, '1'); 245 $user3->setPreference(User::PREF_IS_ACCOUNT_APPROVED, ''); 246 $user4->setPreference(User::PREF_IS_EMAIL_VERIFIED, '1'); 247 $user4->setPreference(User::PREF_IS_ACCOUNT_APPROVED, '1'); 248 249 $users = $user_service->unapproved(); 250 251 $this->assertSame(2, $users->count()); 252 $this->assertSame('user1', $users[0]->userName()); 253 $this->assertSame('user3', $users[1]->userName()); 254 255 $users = $user_service->unverified(); 256 257 $this->assertSame(2, $users->count()); 258 $this->assertSame('user1', $users[0]->userName()); 259 $this->assertSame('user2', $users[1]->userName()); 260 } 261} 262