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