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 20/** 21 * Test the user functions 22 */ 23class UserTest extends \Fisharebest\Webtrees\TestCase 24{ 25 protected static $uses_database = true; 26 27 /** 28 * @covers \Fisharebest\Webtrees\User::__construct 29 * @covers \Fisharebest\Webtrees\User::create 30 * @return void 31 */ 32 public function testCreate(): void 33 { 34 $user = User::create('user', 'User', 'user@example.com', 'secret'); 35 36 $this->assertSame(1, $user->getUserId()); 37 } 38 39 /** 40 * @covers \Fisharebest\Webtrees\User::setUserId 41 * @covers \Fisharebest\Webtrees\User::getUserId 42 * @covers \Fisharebest\Webtrees\User::setUserName 43 * @covers \Fisharebest\Webtrees\User::getUserName 44 * @covers \Fisharebest\Webtrees\User::setRealName 45 * @covers \Fisharebest\Webtrees\User::getRealName 46 * @covers \Fisharebest\Webtrees\User::setEmail 47 * @covers \Fisharebest\Webtrees\User::getEmail 48 * @covers \Fisharebest\Webtrees\User::setPassword 49 * @covers \Fisharebest\Webtrees\User::checkPassword 50 * @return void 51 */ 52 public function testGettersAndSetters(): void 53 { 54 $user = User::create('user', 'User', 'user@example.com', 'secret'); 55 56 $this->assertSame(1, $user->getUserId()); 57 58 $this->assertSame('user', $user->getUserName()); 59 $user->setUserName('foo'); 60 $this->assertSame('foo', $user->getUserName()); 61 62 $this->assertSame('User', $user->getRealName()); 63 $user->setRealName('Foo'); 64 $this->assertSame('Foo', $user->getRealName()); 65 66 $this->assertSame('user@example.com', $user->getEmail()); 67 $user->setEmail('foo@example.com'); 68 $this->assertSame('foo@example.com', $user->getEmail()); 69 70 $this->assertTrue($user->checkPassword('secret')); 71 $user->setPassword('letmein'); 72 $this->assertTrue($user->checkPassword('letmein')); 73 } 74 75 /** 76 * @covers \Fisharebest\Webtrees\User::checkPassword 77 * @return void 78 */ 79 public function testCheckPasswordCaseSensitive(): void 80 { 81 $user = User::create('user', 'User', 'user@example.com', 'secret'); 82 83 $this->assertTrue($user->checkPassword('secret')); 84 $this->assertFalse($user->checkPassword('SECRET')); 85 } 86 87 /** 88 * @covers \Fisharebest\Webtrees\User::delete 89 * @return void 90 */ 91 public function testDelete(): void 92 { 93 $user = User::create('user', 'User', 'user@example.com', 'secret'); 94 $user_id = $user->getUserId(); 95 $user->delete(); 96 97 $this->assertNull(User::find($user_id)); 98 } 99 100 /** 101 * @covers \Fisharebest\Webtrees\User::setPreference 102 * @covers \Fisharebest\Webtrees\User::getPreference 103 * @return void 104 */ 105 public function testPreferences(): void 106 { 107 $user = User::create('user', 'User', 'user@example.com', 'secret'); 108 109 $this->assertSame('', $user->getPreference('foo')); 110 $user->setPreference('foo', 'bar'); 111 $this->assertSame('bar', $user->getPreference('foo')); 112 } 113 114 /** 115 * @covers \Fisharebest\Webtrees\User::find 116 * @return void 117 */ 118 public function testFindNonExistingUser(): void 119 { 120 $user = User::find(999); 121 122 $this->assertNull($user); 123 } 124 125 /** 126 * @covers \Fisharebest\Webtrees\User::find 127 * @return void 128 */ 129 public function testFindExistingUser(): void 130 { 131 $user1 = User::create('user', 'User', 'user@example.com', 'secret'); 132 $user2 = User::find($user1->getUserId()); 133 134 $this->assertSame($user1->getuserId(), $user2->getuserId()); 135 } 136 137 /** 138 * @covers \Fisharebest\Webtrees\User::findByEmail 139 * @return void 140 */ 141 public function testFindUserByEmail(): void 142 { 143 $user1 = User::create('user', 'User', 'user@example.com', 'secret'); 144 $user2 = User::findByEmail($user1->getEmail()); 145 146 $this->assertSame($user1->getuserId(), $user2->getuserId()); 147 } 148 149 /** 150 * @covers \Fisharebest\Webtrees\User::findByUserName 151 * @return void 152 */ 153 public function testFindUserByUserName(): void 154 { 155 $user1 = User::create('user', 'User', 'user@example.com', 'secret'); 156 $user2 = User::findByUserName($user1->getUserName()); 157 158 $this->assertSame($user1->getuserId(), $user2->getuserId()); 159 } 160 161 /** 162 * @covers \Fisharebest\Webtrees\User::findByIdentifier 163 * @return void 164 */ 165 public function testFindUserByIdentifier(): void 166 { 167 $user1 = User::create('user', 'User', 'user@example.com', 'secret'); 168 $user2 = User::findByIdentifier($user1->getUsername()); 169 $user3 = User::findByIdentifier($user1->getEmail()); 170 171 $this->assertSame($user1->getuserId(), $user2->getuserId()); 172 $this->assertSame($user1->getuserId(), $user3->getuserId()); 173 } 174 175 /** 176 * @covers \Fisharebest\Webtrees\User::findByIndividual 177 * @return void 178 */ 179 public function testFindUsersByIndividual(): void 180 { 181 $user = User::create('user', 'User', 'user@example.com', 'secret'); 182 Auth::login($user); 183 $tree = $this->importTree('demo.ged'); 184 $indi = $tree->createIndividual('0 @@ INDI'); 185 $tree->setUserPreference($user, 'gedcomid', $indi->xref()); 186 187 $users = User::findByIndividual($indi); 188 189 $this->assertSame(1, count($users)); 190 $this->assertSame($user->getuserId(), $users[0]->getuserId()); 191 } 192 193 /** 194 * @covers \Fisharebest\Webtrees\User::all 195 * @return void 196 */ 197 public function testFindAllUsers(): void 198 { 199 $user1 = User::create('bbbbb', 'BBBBB', 'bbbbb@example.com', 'secret'); 200 $user2 = User::create('aaaaa', 'AAAAA', 'aaaaa@example.com', 'secret'); 201 202 $users = User::all(); 203 204 $this->assertSame(2, count($users)); 205 $this->assertSame($user2->getUserId(), $users[0]->getUserId()); 206 $this->assertSame($user1->getUserId(), $users[1]->getUserId()); 207 } 208 209 /** 210 * @covers \Fisharebest\Webtrees\User::administrators 211 * @return void 212 */ 213 public function testFindAdministrators(): void 214 { 215 User::create('user', 'User', 'user@example.com', 'secret'); 216 217 $admin = User::create('admin', 'Admin', 'admin@example.com', 'secret'); 218 $admin->setPreference('canadmin', '1'); 219 220 $users = User::administrators(); 221 222 $this->assertSame(1, count($users)); 223 $this->assertSame($admin->getUserId(), $users[0]->getUserId()); 224 } 225 226 /** 227 * @covers \Fisharebest\Webtrees\User::managers 228 * @return void 229 */ 230 public function testFindManagers(): void 231 { 232 $user1 = User::create('user1', 'User1', 'user1@example.com', 'secret'); 233 $user2 = User::create('user2', 'User2', 'user2@example.com', 'secret'); 234 $user3 = User::create('user3', 'User3', 'user3@example.com', 'secret'); 235 $user4 = User::create('user4', 'User4', 'user4@example.com', 'secret'); 236 237 $tree = $this->importTree('demo.ged'); 238 $tree->setUserPreference($user1, 'canedit', 'admin'); 239 $tree->setUserPreference($user2, 'canedit', 'accept'); 240 $tree->setUserPreference($user3, 'canedit', 'edit'); 241 $tree->setUserPreference($user4, 'canedit', 'access'); 242 243 $users = User::managers(); 244 245 $this->assertSame(1, count($users)); 246 $this->assertSame($user1->getuserId(), $users[0]->getuserId()); 247 } 248 249 /** 250 * @covers \Fisharebest\Webtrees\User::moderators 251 * @return void 252 */ 253 public function testFindModerators(): void 254 { 255 $user1 = User::create('user1', 'User1', 'user1@example.com', 'secret'); 256 $user2 = User::create('user2', 'User2', 'user2@example.com', 'secret'); 257 $user3 = User::create('user3', 'User3', 'user3@example.com', 'secret'); 258 $user4 = User::create('user4', 'User4', 'user4@example.com', 'secret'); 259 260 $tree = $this->importTree('demo.ged'); 261 $tree->setUserPreference($user1, 'canedit', 'admin'); 262 $tree->setUserPreference($user2, 'canedit', 'accept'); 263 $tree->setUserPreference($user3, 'canedit', 'edit'); 264 $tree->setUserPreference($user4, 'canedit', 'access'); 265 266 $users = User::moderators(); 267 268 $this->assertSame(1, count($users)); 269 $this->assertSame($user2->getuserId(), $users[0]->getuserId()); 270 } 271 272 /** 273 * @covers \Fisharebest\Webtrees\User::unapproved 274 * @covers \Fisharebest\Webtrees\User::unverified 275 * @return void 276 */ 277 public function testFindUnapprovedAndUnverified(): void 278 { 279 $user1 = User::create('user1', 'User1', 'user1@example.com', 'secret'); 280 $user2 = User::create('user2', 'User2', 'user2@example.com', 'secret'); 281 $user3 = User::create('user3', 'User3', 'user3@example.com', 'secret'); 282 $user4 = User::create('user4', 'User4', 'user4@example.com', 'secret'); 283 284 $user1->setPreference('verified', '0'); 285 $user1->setPreference('verified_by_admin', '0'); 286 $user2->setPreference('verified', '0'); 287 $user2->setPreference('verified_by_admin', '1'); 288 $user3->setPreference('verified', '1'); 289 $user3->setPreference('verified_by_admin', '0'); 290 $user4->setPreference('verified', '1'); 291 $user4->setPreference('verified_by_admin', '1'); 292 293 $users = User::unapproved(); 294 295 $this->assertSame(2, count($users)); 296 $this->assertSame($user1->getuserId(), $users[0]->getuserId()); 297 $this->assertSame($user3->getuserId(), $users[1]->getuserId()); 298 299 $users = User::unverified(); 300 301 $this->assertSame(2, count($users)); 302 $this->assertSame($user1->getuserId(), $users[0]->getuserId()); 303 $this->assertSame($user2->getuserId(), $users[1]->getuserId()); 304 } 305} 306