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