1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2018 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 AuthTest extends \Fisharebest\Webtrees\TestCase 24{ 25 protected static $uses_database = true; 26 27 /** 28 * @covers \Fisharebest\Webtrees\Auth::check 29 * @covers \Fisharebest\Webtrees\Auth::login 30 * @covers \Fisharebest\Webtrees\Auth::logout 31 * 32 * @return void 33 */ 34 public function testLoginAndLogout(): void 35 { 36 $user = User::create('user', 'User', 'user@example.com', 'secret'); 37 $this->assertFalse(Auth::check()); 38 39 Auth::login($user); 40 $this->assertTrue(Auth::check()); 41 42 Auth::logout(); 43 $this->assertFalse(Auth::check()); 44 } 45 46 /** 47 * @covers \Fisharebest\Webtrees\Auth::id 48 * 49 * @return void 50 */ 51 public function testIdNumbers(): void 52 { 53 // Initially, a visitor. 54 $this->assertNull(Auth::id()); 55 56 // ID numbers are issued sequentially, starting at 1. 57 $user1 = User::create('user1', 'User1', 'user1@example.com', 'secret'); 58 Auth::login($user1); 59 $this->assertSame(1, Auth::id()); 60 61 $user2 = User::create('user2', 'User2', 'user2@example.com', 'secret'); 62 Auth::login($user2); 63 $this->assertSame(2, Auth::id()); 64 } 65 66 /** 67 * @covers \Fisharebest\Webtrees\Auth::user 68 * 69 * @return void 70 */ 71 public function testVisitors(): void 72 { 73 // A visitor. 74 $user = Auth::user(); 75 76 $this->assertSame(0, $user->getUserId()); 77 $this->assertSame('', $user->getUserName()); 78 $this->assertSame('', $user->getRealName()); 79 $this->assertSame('', $user->getEmail()); 80 81 // Logged in. 82 $user = User::create('user', 'User', 'user@example.com', 'secret'); 83 Auth::login($user); 84 $user = Auth::user(); 85 $this->assertSame(1, $user->getUserId()); 86 $this->assertSame('user', $user->getUserName()); 87 $this->assertSame('User', $user->getRealName()); 88 $this->assertSame('user@example.com', $user->getEmail()); 89 } 90 91 /** 92 * @covers \Fisharebest\Webtrees\Auth::isAdmin 93 * 94 * @return void 95 */ 96 public function testAdministrator(): void 97 { 98 // By default, new users are not admins. 99 $user = User::create('admin', 'Administrator', 'admin@example.com', 'secret'); 100 $this->assertFalse(Auth::isAdmin($user)); 101 102 // Make the user a manager. 103 $user->setPreference('canadmin', '1'); 104 $this->assertTrue(Auth::isAdmin($user)); 105 106 // Test that the current user is an admin. 107 $this->assertFalse(Auth::isAdmin()); 108 Auth::login($user); 109 $this->assertTrue(Auth::isAdmin()); 110 } 111 112 /** 113 * @covers \Fisharebest\Webtrees\Auth::isManager 114 * @covers \Fisharebest\Webtrees\Auth::accessLevel 115 * 116 * @return void 117 */ 118 public function testManager(): void 119 { 120 $tree = Tree::create('test', 'Test'); 121 $tree->setPreference('imported', '1'); 122 123 // By default, new users are not managers. 124 $user = User::create('manager', 'Manager', 'manager@example.com', 'secret'); 125 $this->assertFalse(Auth::isManager($tree, $user)); 126 $this->assertSame(Auth::PRIV_PRIVATE, Auth::accessLevel($tree, $user)); 127 128 // Make the user a manager. 129 $tree->setUserPreference($user, 'canedit', 'admin'); 130 $this->assertTrue(Auth::isManager($tree, $user)); 131 $this->assertSame(Auth::PRIV_NONE, Auth::accessLevel($tree, $user)); 132 133 // Test that the current user is a manager. 134 $this->assertFalse(Auth::isManager($tree)); 135 $this->assertSame(Auth::PRIV_PRIVATE, Auth::accessLevel($tree)); 136 Auth::login($user); 137 $this->assertTrue(Auth::isManager($tree)); 138 $this->assertSame(Auth::PRIV_NONE, Auth::accessLevel($tree)); 139 } 140 141 /** 142 * @covers \Fisharebest\Webtrees\Auth::isModerator 143 * @covers \Fisharebest\Webtrees\Auth::accessLevel 144 * 145 * @return void 146 */ 147 public function testModerator(): void 148 { 149 // By default, new users are not moderators. 150 $user = User::create('moderator', 'Moderator', 'moderator@example.com', 'secret'); 151 $tree = Tree::create('test', 'Test'); 152 $this->assertFalse(Auth::isModerator($tree, $user)); 153 $this->assertSame(Auth::PRIV_PRIVATE, Auth::accessLevel($tree, $user)); 154 155 // Make the user a moderator. 156 $tree->setUserPreference($user, 'canedit', 'accept'); 157 $this->assertTrue(Auth::isModerator($tree, $user)); 158 $this->assertSame(Auth::PRIV_USER, Auth::accessLevel($tree, $user)); 159 160 // Test that the current user is a moderator. 161 $this->assertFalse(Auth::isModerator($tree)); 162 $this->assertSame(Auth::PRIV_PRIVATE, Auth::accessLevel($tree)); 163 Auth::login($user); 164 $this->assertTrue(Auth::isModerator($tree)); 165 $this->assertSame(Auth::PRIV_USER, Auth::accessLevel($tree)); 166 } 167 168 /** 169 * @covers \Fisharebest\Webtrees\Auth::isEditor 170 * @covers \Fisharebest\Webtrees\Auth::accessLevel 171 * 172 * @return void 173 */ 174 public function testEditor(): void 175 { 176 // By default, new users are not editors. 177 $user = User::create('editor', 'Editor', 'editor@example.com', 'secret'); 178 $tree = Tree::create('test', 'Test'); 179 $this->assertFalse(Auth::isEditor($tree, $user)); 180 $this->assertSame(Auth::PRIV_PRIVATE, Auth::accessLevel($tree, $user)); 181 182 // Make the user an editor. 183 $tree->setUserPreference($user, 'canedit', 'edit'); 184 $this->assertTrue(Auth::isEditor($tree, $user)); 185 $this->assertSame(Auth::PRIV_USER, Auth::accessLevel($tree, $user)); 186 187 // Test that the current user is an editor. 188 $this->assertFalse(Auth::isEditor($tree)); 189 $this->assertSame(Auth::PRIV_PRIVATE, Auth::accessLevel($tree)); 190 Auth::login($user); 191 $this->assertTrue(Auth::isEditor($tree)); 192 $this->assertSame(Auth::PRIV_USER, Auth::accessLevel($tree)); 193 } 194 195 /** 196 * @covers \Fisharebest\Webtrees\Auth::isMember 197 * @covers \Fisharebest\Webtrees\Auth::accessLevel 198 * 199 * @return void 200 */ 201 public function testMember(): void 202 { 203 // By default, new users are not members. 204 $user = User::create('member', 'Member', 'member@example.com', 'secret'); 205 $tree = Tree::create('test', 'Test'); 206 $this->assertFalse(Auth::isMember($tree, $user)); 207 $this->assertSame(Auth::PRIV_PRIVATE, Auth::accessLevel($tree, $user)); 208 209 // Make the user a members. 210 $tree->setUserPreference($user, 'canedit', 'access'); 211 $this->assertTrue(Auth::isMember($tree, $user)); 212 $this->assertSame(Auth::PRIV_USER, Auth::accessLevel($tree, $user)); 213 214 // Test that the current user is a member. 215 $this->assertFalse(Auth::isMember($tree)); 216 $this->assertSame(Auth::PRIV_PRIVATE, Auth::accessLevel($tree)); 217 Auth::login($user); 218 $this->assertTrue(Auth::isMember($tree)); 219 $this->assertSame(Auth::PRIV_USER, Auth::accessLevel($tree)); 220 } 221} 222