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 * Test administrators. 29 * 30 * @covers \Fisharebest\Webtrees\Auth 31 * 32 * @return void 33 */ 34 public function testAdministrator(): void 35 { 36 // By default, new users are not admins. 37 $user = User::create('admin', 'Administrator', 'admin@example.com', 'secret'); 38 $this->assertFalse(Auth::isAdmin($user)); 39 40 // Make the user a manager. 41 $user->setPreference('canadmin', '1'); 42 $this->assertTrue(Auth::isAdmin($user)); 43 44 // Test that the current user is an admin. 45 $this->assertFalse(Auth::isAdmin()); 46 Auth::login($user); 47 $this->assertTrue(Auth::isAdmin()); 48 } 49 50 /** 51 * Test managers. 52 * 53 * @covers \Fisharebest\Webtrees\Auth 54 * 55 * @return void 56 */ 57 public function testManager(): void 58 { 59 $tree = Tree::create('test', 'Test'); 60 $tree->setPreference('imported', '1'); 61 62 // By default, new users are not managers. 63 $user = User::create('manager', 'Manager', 'manager@example.com', 'secret'); 64 $this->assertFalse(Auth::isManager($tree, $user)); 65 $this->assertSame(Auth::PRIV_PRIVATE, Auth::accessLevel($tree, $user)); 66 67 // Make the user a manager. 68 $tree->setUserPreference($user, 'canedit', 'admin'); 69 $this->assertTrue(Auth::isManager($tree, $user)); 70 $this->assertSame(Auth::PRIV_NONE, Auth::accessLevel($tree, $user)); 71 72 // Test that the current user is a manager. 73 $this->assertFalse(Auth::isManager($tree)); 74 $this->assertSame(Auth::PRIV_PRIVATE, Auth::accessLevel($tree)); 75 Auth::login($user); 76 $this->assertTrue(Auth::isManager($tree)); 77 $this->assertSame(Auth::PRIV_NONE, Auth::accessLevel($tree)); 78 } 79 80 /** 81 * Test moderators. 82 * 83 * @covers \Fisharebest\Webtrees\Auth 84 * 85 * @return void 86 */ 87 public function testModerator(): void 88 { 89 // By default, new users are not moderators. 90 $user = User::create('moderator', 'Moderator', 'moderator@example.com', 'secret'); 91 $tree = Tree::create('test', 'Test'); 92 $this->assertFalse(Auth::isModerator($tree, $user)); 93 $this->assertSame(Auth::PRIV_PRIVATE, Auth::accessLevel($tree, $user)); 94 95 // Make the user a moderator. 96 $tree->setUserPreference($user, 'canedit', 'accept'); 97 $this->assertTrue(Auth::isModerator($tree, $user)); 98 $this->assertSame(Auth::PRIV_USER, Auth::accessLevel($tree, $user)); 99 100 // Test that the current user is a moderator. 101 $this->assertFalse(Auth::isModerator($tree)); 102 $this->assertSame(Auth::PRIV_PRIVATE, Auth::accessLevel($tree)); 103 Auth::login($user); 104 $this->assertTrue(Auth::isModerator($tree)); 105 $this->assertSame(Auth::PRIV_USER, Auth::accessLevel($tree)); 106 } 107 108 /** 109 * Test editors. 110 * 111 * @covers \Fisharebest\Webtrees\Auth 112 * 113 * @return void 114 */ 115 public function testEditor(): void 116 { 117 // By default, new users are not editors. 118 $user = User::create('editor', 'Editor', 'editor@example.com', 'secret'); 119 $tree = Tree::create('test', 'Test'); 120 $this->assertFalse(Auth::isEditor($tree, $user)); 121 $this->assertSame(Auth::PRIV_PRIVATE, Auth::accessLevel($tree, $user)); 122 123 // Make the user an editor. 124 $tree->setUserPreference($user, 'canedit', 'edit'); 125 $this->assertTrue(Auth::isEditor($tree, $user)); 126 $this->assertSame(Auth::PRIV_USER, Auth::accessLevel($tree, $user)); 127 128 // Test that the current user is an editor. 129 $this->assertFalse(Auth::isEditor($tree)); 130 $this->assertSame(Auth::PRIV_PRIVATE, Auth::accessLevel($tree)); 131 Auth::login($user); 132 $this->assertTrue(Auth::isEditor($tree)); 133 $this->assertSame(Auth::PRIV_USER, Auth::accessLevel($tree)); 134 } 135 136 /** 137 * Test members. 138 * 139 * @covers \Fisharebest\Webtrees\Auth 140 * 141 * @return void 142 */ 143 public function testMember(): void 144 { 145 // By default, new users are not members. 146 $user = User::create('member', 'Member', 'member@example.com', 'secret'); 147 $tree = Tree::create('test', 'Test'); 148 $this->assertFalse(Auth::isMember($tree, $user)); 149 $this->assertSame(Auth::PRIV_PRIVATE, Auth::accessLevel($tree, $user)); 150 151 // Make the user a members. 152 $tree->setUserPreference($user, 'canedit', 'access'); 153 $this->assertTrue(Auth::isMember($tree, $user)); 154 $this->assertSame(Auth::PRIV_USER, Auth::accessLevel($tree, $user)); 155 156 // Test that the current user is a member. 157 $this->assertFalse(Auth::isMember($tree)); 158 $this->assertSame(Auth::PRIV_PRIVATE, Auth::accessLevel($tree)); 159 Auth::login($user); 160 $this->assertTrue(Auth::isMember($tree)); 161 $this->assertSame(Auth::PRIV_USER, Auth::accessLevel($tree)); 162 } 163} 164