1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2019 webtrees development team 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16declare(strict_types=1); 17 18namespace Fisharebest\Webtrees; 19 20use Fisharebest\Webtrees\Services\UserService; 21 22/** 23 * Test the UserService class 24 */ 25class UserServiceTest extends TestCase 26{ 27 protected static $uses_database = true; 28 29 /** 30 * @covers \Fisharebest\Webtrees\Services\UserService::create 31 * @return void 32 */ 33 public function testCreate(): void 34 { 35 $user_service = new UserService(); 36 37 $user = $user_service->create('user', 'User', 'user@example.com', 'secret'); 38 39 $this->assertSame(1, $user->id()); 40 } 41 42 /** 43 * @covers \Fisharebest\Webtrees\Services\UserService::setUserName 44 * @covers \Fisharebest\Webtrees\Services\UserService::userName 45 * @covers \Fisharebest\Webtrees\Services\UserService::setRealName 46 * @covers \Fisharebest\Webtrees\Services\UserService::realName 47 * @covers \Fisharebest\Webtrees\Services\UserService::setEmail 48 * @covers \Fisharebest\Webtrees\Services\UserService::email 49 * @covers \Fisharebest\Webtrees\Services\UserService::setPassword 50 * @covers \Fisharebest\Webtrees\Services\UserService::checkPassword 51 * @return void 52 */ 53 public function testGettersAndSetters(): void 54 { 55 $user_service = new UserService(); 56 $user = $user_service->create('user', 'User', 'user@example.com', 'secret'); 57 58 $this->assertSame(1, $user->id()); 59 60 $this->assertSame('user', $user->userName()); 61 $user->setUserName('foo'); 62 $this->assertSame('foo', $user->userName()); 63 64 $this->assertSame('User', $user->realName()); 65 $user->setRealName('Foo'); 66 $this->assertSame('Foo', $user->realName()); 67 68 $this->assertSame('user@example.com', $user->email()); 69 $user->setEmail('foo@example.com'); 70 $this->assertSame('foo@example.com', $user->email()); 71 72 $this->assertTrue($user->checkPassword('secret')); 73 $user->setPassword('letmein'); 74 $this->assertTrue($user->checkPassword('letmein')); 75 } 76 77 /** 78 * @covers \Fisharebest\Webtrees\Services\UserService::checkPassword 79 * @return void 80 */ 81 public function testCheckPasswordCaseSensitive(): void 82 { 83 $user_service = new UserService(); 84 $user = $user_service->create('user', 'User', 'user@example.com', 'secret'); 85 86 $this->assertTrue($user->checkPassword('secret')); 87 $this->assertFalse($user->checkPassword('SECRET')); 88 } 89 90 /** 91 * @covers \Fisharebest\Webtrees\Services\UserService::delete 92 * @return void 93 */ 94 public function testDelete(): void 95 { 96 $user_service = new UserService(); 97 $user = $user_service->create('user', 'User', 'user@example.com', 'secret'); 98 $user_id = $user->id(); 99 $user_service->delete($user); 100 101 $this->assertNull($user_service->find($user_id)); 102 } 103 104 /** 105 * @covers \Fisharebest\Webtrees\Services\UserService::setPreference 106 * @covers \Fisharebest\Webtrees\Services\UserService::getPreference 107 * @return void 108 */ 109 public function testPreferences(): void 110 { 111 $user_service = new UserService(); 112 $user = $user_service->create('user', 'User', 'user@example.com', 'secret'); 113 114 $this->assertSame('', $user->getPreference('foo')); 115 $user->setPreference('foo', 'bar'); 116 $this->assertSame('bar', $user->getPreference('foo')); 117 } 118 119 /** 120 * @covers \Fisharebest\Webtrees\Services\UserService::find 121 * @return void 122 */ 123 public function testFindNonExistingUser(): void 124 { 125 $user_service = new UserService(); 126 $user = $user_service->find(999); 127 128 $this->assertNull($user); 129 } 130 131 /** 132 * @covers \Fisharebest\Webtrees\Services\UserService::find 133 * @return void 134 */ 135 public function testFindExistingUser(): void 136 { 137 $user_service = new UserService(); 138 $user1 = $user_service->create('user', 'User', 'user@example.com', 'secret'); 139 $user2 = $user_service->find($user1->id()); 140 141 $this->assertSame($user1->id(), $user2->id()); 142 } 143 144 /** 145 * @covers \Fisharebest\Webtrees\Services\UserService::findByEmail 146 * @return void 147 */ 148 public function testFindUserByEmail(): void 149 { 150 $user_service = new UserService(); 151 $user1 = $user_service->create('user', 'User', 'user@example.com', 'secret'); 152 $user2 = $user_service->findByEmail($user1->email()); 153 154 $this->assertSame($user1->id(), $user2->id()); 155 } 156 157 /** 158 * @covers \Fisharebest\Webtrees\Services\UserService::findByUserName 159 * @return void 160 */ 161 public function testFindUserByUserName(): void 162 { 163 $user_service = new UserService(); 164 $user1 = $user_service->create('user', 'User', 'user@example.com', 'secret'); 165 $user2 = $user_service->findByUserName($user1->userName()); 166 167 $this->assertSame($user1->id(), $user2->id()); 168 } 169 170 /** 171 * @covers \Fisharebest\Webtrees\Services\UserService::findByIdentifier 172 * @return void 173 */ 174 public function testFindUserByIdentifier(): void 175 { 176 $user_service = new UserService(); 177 $user1 = $user_service->create('user', 'User', 'user@example.com', 'secret'); 178 $user2 = $user_service->findByIdentifier($user1->userName()); 179 $user3 = $user_service->findByIdentifier($user1->email()); 180 181 $this->assertSame($user1->id(), $user2->id()); 182 $this->assertSame($user1->id(), $user3->id()); 183 } 184 185 /** 186 * @covers \Fisharebest\Webtrees\Services\UserService::findByIndividual 187 * @return void 188 */ 189 public function testFindUsersByIndividual(): void 190 { 191 $user_service = new UserService(); 192 $user = $user_service->create('user', 'User', 'user@example.com', 'secret'); 193 Auth::login($user); 194 $tree = $this->importTree('demo.ged'); 195 $indi = $tree->createIndividual('0 @@ INDI'); 196 $tree->setUserPreference($user, 'gedcomid', $indi->xref()); 197 198 $users = $user_service->findByIndividual($indi); 199 200 $this->assertSame(1, count($users)); 201 $this->assertSame($user->id(), $users[0]->id()); 202 } 203 204 /** 205 * @covers \Fisharebest\Webtrees\Services\UserService::all 206 * @return void 207 */ 208 public function testFindAllUsers(): void 209 { 210 $user_service = new UserService(); 211 $user1 = $user_service->create('bbbbb', 'BBBBB', 'bbbbb@example.com', 'secret'); 212 $user2 = $user_service->create('aaaaa', 'AAAAA', 'aaaaa@example.com', 'secret'); 213 214 $users = $user_service->all(); 215 216 $this->assertSame(2, $users->count()); 217 $this->assertSame($user2->id(), $users[0]->id()); 218 $this->assertSame($user1->id(), $users[1]->id()); 219 } 220 221 /** 222 * @covers \Fisharebest\Webtrees\Services\UserService::administrators 223 * @return void 224 */ 225 public function testFindAdministrators(): void 226 { 227 $user_service = new UserService(); 228 $user_service->create('user', 'User', 'user@example.com', 'secret'); 229 230 $admin = $user_service->create('admin', 'Admin', 'admin@example.com', 'secret'); 231 $admin->setPreference('canadmin', '1'); 232 233 $users = $user_service->administrators(); 234 235 $this->assertSame(1, count($users)); 236 $this->assertSame($admin->id(), $users[0]->id()); 237 } 238 239 /** 240 * @covers \Fisharebest\Webtrees\Services\UserService::managers 241 * @return void 242 */ 243 public function testFindManagers(): void 244 { 245 $user_service = new UserService(); 246 $user1 = $user_service->create('user1', 'User1', 'user1@example.com', 'secret'); 247 $user2 = $user_service->create('user2', 'User2', 'user2@example.com', 'secret'); 248 $user3 = $user_service->create('user3', 'User3', 'user3@example.com', 'secret'); 249 $user4 = $user_service->create('user4', 'User4', 'user4@example.com', 'secret'); 250 251 $tree = $this->importTree('demo.ged'); 252 $tree->setUserPreference($user1, 'canedit', 'admin'); 253 $tree->setUserPreference($user2, 'canedit', 'accept'); 254 $tree->setUserPreference($user3, 'canedit', 'edit'); 255 $tree->setUserPreference($user4, 'canedit', 'access'); 256 257 $users = $user_service->managers(); 258 259 $this->assertSame(1, count($users)); 260 $this->assertSame($user1->id(), $users[0]->id()); 261 } 262 263 /** 264 * @covers \Fisharebest\Webtrees\Services\UserService::moderators 265 * @return void 266 */ 267 public function testFindModerators(): void 268 { 269 $user_service = new UserService(); 270 $user1 = $user_service->create('user1', 'User1', 'user1@example.com', 'secret'); 271 $user2 = $user_service->create('user2', 'User2', 'user2@example.com', 'secret'); 272 $user3 = $user_service->create('user3', 'User3', 'user3@example.com', 'secret'); 273 $user4 = $user_service->create('user4', 'User4', 'user4@example.com', 'secret'); 274 275 $tree = $this->importTree('demo.ged'); 276 $tree->setUserPreference($user1, 'canedit', 'admin'); 277 $tree->setUserPreference($user2, 'canedit', 'accept'); 278 $tree->setUserPreference($user3, 'canedit', 'edit'); 279 $tree->setUserPreference($user4, 'canedit', 'access'); 280 281 $users = $user_service->moderators(); 282 283 $this->assertSame(1, count($users)); 284 $this->assertSame($user2->id(), $users[0]->id()); 285 } 286 287 /** 288 * @covers \Fisharebest\Webtrees\Services\UserService::unapproved 289 * @covers \Fisharebest\Webtrees\Services\UserService::unverified 290 * @return void 291 */ 292 public function testFindUnapprovedAndUnverified(): void 293 { 294 $user_service = new UserService(); 295 $user1 = $user_service->create('user1', 'User1', 'user1@example.com', 'secret'); 296 $user2 = $user_service->create('user2', 'User2', 'user2@example.com', 'secret'); 297 $user3 = $user_service->create('user3', 'User3', 'user3@example.com', 'secret'); 298 $user4 = $user_service->create('user4', 'User4', 'user4@example.com', 'secret'); 299 300 $user1->setPreference('verified', '0'); 301 $user1->setPreference('verified_by_admin', '0'); 302 $user2->setPreference('verified', '0'); 303 $user2->setPreference('verified_by_admin', '1'); 304 $user3->setPreference('verified', '1'); 305 $user3->setPreference('verified_by_admin', '0'); 306 $user4->setPreference('verified', '1'); 307 $user4->setPreference('verified_by_admin', '1'); 308 309 $users = $user_service->unapproved(); 310 311 $this->assertSame(2, $users->count()); 312 $this->assertSame($user1->id(), $users[0]->id()); 313 $this->assertSame($user3->id(), $users[1]->id()); 314 315 $users = $user_service->unverified(); 316 317 $this->assertSame(2, $users->count()); 318 $this->assertSame($user1->id(), $users[0]->id()); 319 $this->assertSame($user2->id(), $users[1]->id()); 320 } 321} 322