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