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