5 use BookStack\Actions\ActivityType;
6 use BookStack\Auth\Access\UserInviteService;
7 use BookStack\Auth\Role;
8 use BookStack\Auth\User;
9 use BookStack\Uploads\Image;
10 use Illuminate\Support\Facades\Hash;
11 use Illuminate\Support\Str;
12 use Mockery\MockInterface;
16 class UserManagementTest extends TestCase
18 public function test_user_creation()
20 /** @var User $user */
21 $user = User::factory()->make();
22 $adminRole = Role::getRole('admin');
24 $resp = $this->asAdmin()->get('/settings/users');
25 $this->withHtml($resp)->assertElementContains('a[href="' . url('/settings/users/create') . '"]', 'Add New User');
27 $resp = $this->get('/settings/users/create');
28 $this->withHtml($resp)->assertElementContains('form[action="' . url('/settings/users/create') . '"]', 'Save');
30 $resp = $this->post('/settings/users/create', [
31 'name' => $user->name,
32 'email' => $user->email,
33 'password' => $user->password,
34 'password-confirm' => $user->password,
35 'roles[' . $adminRole->id . ']' => 'true',
37 $resp->assertRedirect('/settings/users');
39 $resp = $this->get('/settings/users');
40 $resp->assertSee($user->name);
42 $this->assertDatabaseHas('users', $user->only('name', 'email'));
45 $this->assertStringStartsWith(Str::slug($user->name), $user->slug);
48 public function test_user_updating()
50 $user = $this->users->viewer();
51 $password = $user->password;
53 $resp = $this->asAdmin()->get('/settings/users/' . $user->id);
54 $resp->assertSee($user->email);
56 $this->put($user->getEditUrl(), [
57 'name' => 'Barry Scott',
58 ])->assertRedirect('/settings/users');
60 $this->assertDatabaseHas('users', ['id' => $user->id, 'name' => 'Barry Scott', 'password' => $password]);
61 $this->assertDatabaseMissing('users', ['name' => $user->name]);
64 $this->assertStringStartsWith(Str::slug($user->name), $user->slug);
67 public function test_user_password_update()
69 $user = $this->users->viewer();
70 $userProfilePage = '/settings/users/' . $user->id;
72 $this->asAdmin()->get($userProfilePage);
73 $this->put($userProfilePage, [
74 'password' => 'newpassword',
75 ])->assertRedirect($userProfilePage);
77 $this->get($userProfilePage)->assertSee('Password confirmation required');
79 $this->put($userProfilePage, [
80 'password' => 'newpassword',
81 'password-confirm' => 'newpassword',
82 ])->assertRedirect('/settings/users');
84 $userPassword = User::query()->find($user->id)->password;
85 $this->assertTrue(Hash::check('newpassword', $userPassword));
88 public function test_user_cannot_be_deleted_if_last_admin()
90 $adminRole = Role::getRole('admin');
92 // Delete all but one admin user if there are more than one
93 $adminUsers = $adminRole->users;
94 if (count($adminUsers) > 1) {
95 /** @var User $user */
96 foreach ($adminUsers->splice(1) as $user) {
101 // Ensure we currently only have 1 admin user
102 $this->assertEquals(1, $adminRole->users()->count());
103 /** @var User $user */
104 $user = $adminRole->users->first();
106 $resp = $this->asAdmin()->delete('/settings/users/' . $user->id);
107 $resp->assertRedirect('/settings/users/' . $user->id);
109 $resp = $this->get('/settings/users/' . $user->id);
110 $resp->assertSee('You cannot delete the only admin');
112 $this->assertDatabaseHas('users', ['id' => $user->id]);
115 public function test_delete()
117 $editor = $this->users->editor();
118 $resp = $this->asAdmin()->delete("settings/users/{$editor->id}");
119 $resp->assertRedirect('/settings/users');
120 $resp = $this->followRedirects($resp);
122 $resp->assertSee('User successfully removed');
123 $this->assertActivityExists(ActivityType::USER_DELETE);
125 $this->assertDatabaseMissing('users', ['id' => $editor->id]);
128 public function test_delete_offers_migrate_option()
130 $editor = $this->users->editor();
131 $resp = $this->asAdmin()->get("settings/users/{$editor->id}/delete");
132 $resp->assertSee('Migrate Ownership');
133 $resp->assertSee('new_owner_id');
136 public function test_migrate_option_hidden_if_user_cannot_manage_users()
138 $editor = $this->users->editor();
140 $resp = $this->asEditor()->get("settings/users/{$editor->id}/delete");
141 $resp->assertDontSee('Migrate Ownership');
142 $resp->assertDontSee('new_owner_id');
144 $this->permissions->grantUserRolePermissions($editor, ['users-manage']);
146 $resp = $this->asEditor()->get("settings/users/{$editor->id}/delete");
147 $resp->assertSee('Migrate Ownership');
148 $this->withHtml($resp)->assertElementExists('form input[name="new_owner_id"]');
149 $resp->assertSee('new_owner_id');
152 public function test_delete_with_new_owner_id_changes_ownership()
154 $page = $this->entities->page();
155 $owner = $page->ownedBy;
156 $newOwner = User::query()->where('id', '!=', $owner->id)->first();
158 $this->asAdmin()->delete("settings/users/{$owner->id}", ['new_owner_id' => $newOwner->id]);
159 $this->assertDatabaseHas('pages', [
161 'owned_by' => $newOwner->id,
165 public function test_delete_removes_user_preferences()
167 $editor = $this->users->editor();
168 setting()->putUser($editor, 'dark-mode-enabled', 'true');
170 $this->assertDatabaseHas('settings', [
171 'setting_key' => 'user:' . $editor->id . ':dark-mode-enabled',
175 $this->asAdmin()->delete("settings/users/{$editor->id}");
177 $this->assertDatabaseMissing('settings', [
178 'setting_key' => 'user:' . $editor->id . ':dark-mode-enabled',
182 public function test_guest_profile_shows_limited_form()
184 $guest = User::getDefault();
185 $resp = $this->asAdmin()->get('/settings/users/' . $guest->id);
186 $resp->assertSee('Guest');
187 $this->withHtml($resp)->assertElementNotExists('#password');
190 public function test_guest_profile_cannot_be_deleted()
192 $guestUser = User::getDefault();
193 $resp = $this->asAdmin()->get('/settings/users/' . $guestUser->id . '/delete');
194 $resp->assertSee('Delete User');
195 $resp->assertSee('Guest');
196 $this->withHtml($resp)->assertElementContains('form[action$="/settings/users/' . $guestUser->id . '"] button', 'Confirm');
198 $resp = $this->delete('/settings/users/' . $guestUser->id);
199 $resp->assertRedirect('/settings/users/' . $guestUser->id);
200 $resp = $this->followRedirects($resp);
201 $resp->assertSee('cannot delete the guest user');
204 public function test_user_create_language_reflects_default_system_locale()
206 $langs = ['en', 'fr', 'hr'];
207 foreach ($langs as $lang) {
208 config()->set('app.locale', $lang);
209 $resp = $this->asAdmin()->get('/settings/users/create');
210 $this->withHtml($resp)->assertElementExists('select[name="language"] option[value="' . $lang . '"][selected]');
214 public function test_user_creation_is_not_performed_if_the_invitation_sending_fails()
216 /** @var User $user */
217 $user = User::factory()->make();
218 $adminRole = Role::getRole('admin');
220 // Simulate an invitation sending failure
221 $this->mock(UserInviteService::class, function (MockInterface $mock) {
222 $mock->shouldReceive('sendInvitation')->once()->andThrow(RuntimeException::class);
225 $this->asAdmin()->post('/settings/users/create', [
226 'name' => $user->name,
227 'email' => $user->email,
228 'send_invite' => 'true',
229 'roles[' . $adminRole->id . ']' => 'true',
232 // Since the invitation failed, the user should not exist in the database
233 $this->assertDatabaseMissing('users', $user->only('name', 'email'));
236 public function test_user_create_activity_is_not_persisted_if_the_invitation_sending_fails()
238 /** @var User $user */
239 $user = User::factory()->make();
240 $adminRole = Role::getRole('admin');
242 $this->mock(UserInviteService::class, function (MockInterface $mock) {
243 $mock->shouldReceive('sendInvitation')->once()->andThrow(RuntimeException::class);
246 $this->asAdmin()->post('/settings/users/create', [
247 'name' => $user->name,
248 'email' => $user->email,
249 'send_invite' => 'true',
250 'roles[' . $adminRole->id . ']' => 'true',
253 $this->assertDatabaseMissing('activities', ['type' => 'USER_CREATE']);
256 public function test_user_create_update_fails_if_locale_is_invalid()
258 $user = $this->users->editor();
261 $resp = $this->asAdmin()->put($user->getEditUrl(), ['language' => 'this_is_too_long']);
262 $resp->assertSessionHasErrors(['language' => 'The language may not be greater than 15 characters.']);
265 // Invalid characters
266 $resp = $this->put($user->getEditUrl(), ['language' => 'en<GB']);
267 $resp->assertSessionHasErrors(['language' => 'The language may only contain letters, numbers, dashes and underscores.']);
271 $resp = $this->post('/settings/users/create', [
272 'language' => 'en<GB_and_this_is_longer',
276 $resp->assertSessionHasErrors(['language' => 'The language may not be greater than 15 characters.']);
277 $resp->assertSessionHasErrors(['language' => 'The language may only contain letters, numbers, dashes and underscores.']);
280 public function test_user_avatar_update_and_reset()
282 $user = $this->users->viewer();
283 $avatarFile = $this->files->uploadedImage('avatar-icon.png');
285 $this->assertEquals(0, $user->image_id);
287 $upload = $this->asAdmin()->call('PUT', "/settings/users/{$user->id}", [
288 'name' => 'Barry Scott',
289 ], [], ['profile_image' => $avatarFile], []);
290 $upload->assertRedirect('/settings/users');
293 $this->assertNotEquals(0, $user->image_id);
294 /** @var Image $image */
295 $image = Image::query()->findOrFail($user->image_id);
296 $this->assertFileExists(public_path($image->path));
298 $reset = $this->put("/settings/users/{$user->id}", [
299 'name' => 'Barry Scott',
300 'profile_image_reset' => 'true',
302 $upload->assertRedirect('/settings/users');
305 $this->assertFileDoesNotExist(public_path($image->path));
306 $this->assertEquals(0, $user->image_id);