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