5 use BookStack\Actions\ActivityType;
6 use BookStack\Auth\Access\UserInviteService;
7 use BookStack\Auth\Role;
8 use BookStack\Auth\User;
9 use Illuminate\Support\Facades\Hash;
10 use Illuminate\Support\Str;
11 use Mockery\MockInterface;
15 class UserManagementTest extends TestCase
17 public function test_user_creation()
19 /** @var User $user */
20 $user = User::factory()->make();
21 $adminRole = Role::getRole('admin');
23 $resp = $this->asAdmin()->get('/settings/users');
24 $this->withHtml($resp)->assertElementContains('a[href="' . url('/settings/users/create') . '"]', 'Add New User');
26 $resp = $this->get('/settings/users/create');
27 $this->withHtml($resp)->assertElementContains('form[action="' . url('/settings/users/create') . '"]', 'Save');
29 $resp = $this->post('/settings/users/create', [
30 'name' => $user->name,
31 'email' => $user->email,
32 'password' => $user->password,
33 'password-confirm' => $user->password,
34 'roles[' . $adminRole->id . ']' => 'true',
36 $resp->assertRedirect('/settings/users');
38 $resp = $this->get('/settings/users');
39 $resp->assertSee($user->name);
41 $this->assertDatabaseHas('users', $user->only('name', 'email'));
44 $this->assertStringStartsWith(Str::slug($user->name), $user->slug);
47 public function test_user_updating()
49 $user = $this->getNormalUser();
50 $password = $user->password;
52 $resp = $this->asAdmin()->get('/settings/users/' . $user->id);
53 $resp->assertSee($user->email);
55 $this->put($user->getEditUrl(), [
56 'name' => 'Barry Scott',
57 ])->assertRedirect('/settings/users');
59 $this->assertDatabaseHas('users', ['id' => $user->id, 'name' => 'Barry Scott', 'password' => $password]);
60 $this->assertDatabaseMissing('users', ['name' => $user->name]);
63 $this->assertStringStartsWith(Str::slug($user->name), $user->slug);
66 public function test_user_password_update()
68 $user = $this->getNormalUser();
69 $userProfilePage = '/settings/users/' . $user->id;
71 $this->asAdmin()->get($userProfilePage);
72 $this->put($userProfilePage, [
73 'password' => 'newpassword',
74 ])->assertRedirect($userProfilePage);
76 $this->get($userProfilePage)->assertSee('Password confirmation required');
78 $this->put($userProfilePage, [
79 'password' => 'newpassword',
80 'password-confirm' => 'newpassword',
81 ])->assertRedirect('/settings/users');
83 $userPassword = User::query()->find($user->id)->password;
84 $this->assertTrue(Hash::check('newpassword', $userPassword));
87 public function test_user_cannot_be_deleted_if_last_admin()
89 $adminRole = Role::getRole('admin');
91 // Delete all but one admin user if there are more than one
92 $adminUsers = $adminRole->users;
93 if (count($adminUsers) > 1) {
94 /** @var User $user */
95 foreach ($adminUsers->splice(1) as $user) {
100 // Ensure we currently only have 1 admin user
101 $this->assertEquals(1, $adminRole->users()->count());
102 /** @var User $user */
103 $user = $adminRole->users->first();
105 $resp = $this->asAdmin()->delete('/settings/users/' . $user->id);
106 $resp->assertRedirect('/settings/users/' . $user->id);
108 $resp = $this->get('/settings/users/' . $user->id);
109 $resp->assertSee('You cannot delete the only admin');
111 $this->assertDatabaseHas('users', ['id' => $user->id]);
114 public function test_delete()
116 $editor = $this->getEditor();
117 $resp = $this->asAdmin()->delete("settings/users/{$editor->id}");
118 $resp->assertRedirect('/settings/users');
119 $resp = $this->followRedirects($resp);
121 $resp->assertSee('User successfully removed');
122 $this->assertActivityExists(ActivityType::USER_DELETE);
124 $this->assertDatabaseMissing('users', ['id' => $editor->id]);
127 public function test_delete_offers_migrate_option()
129 $editor = $this->getEditor();
130 $resp = $this->asAdmin()->get("settings/users/{$editor->id}/delete");
131 $resp->assertSee('Migrate Ownership');
132 $resp->assertSee('new_owner_id');
135 public function test_migrate_option_hidden_if_user_cannot_manage_users()
137 $editor = $this->getEditor();
139 $resp = $this->asEditor()->get("settings/users/{$editor->id}/delete");
140 $resp->assertDontSee('Migrate Ownership');
141 $resp->assertDontSee('new_owner_id');
143 $this->giveUserPermissions($editor, ['users-manage']);
145 $resp = $this->asEditor()->get("settings/users/{$editor->id}/delete");
146 $resp->assertSee('Migrate Ownership');
147 $resp->assertSee('new_owner_id');
150 public function test_delete_with_new_owner_id_changes_ownership()
152 $page = $this->entities->page();
153 $owner = $page->ownedBy;
154 $newOwner = User::query()->where('id', '!=', $owner->id)->first();
156 $this->asAdmin()->delete("settings/users/{$owner->id}", ['new_owner_id' => $newOwner->id]);
157 $this->assertDatabaseHas('pages', [
159 'owned_by' => $newOwner->id,
163 public function test_guest_profile_shows_limited_form()
165 $guest = User::getDefault();
166 $resp = $this->asAdmin()->get('/settings/users/' . $guest->id);
167 $resp->assertSee('Guest');
168 $this->withHtml($resp)->assertElementNotExists('#password');
171 public function test_guest_profile_cannot_be_deleted()
173 $guestUser = User::getDefault();
174 $resp = $this->asAdmin()->get('/settings/users/' . $guestUser->id . '/delete');
175 $resp->assertSee('Delete User');
176 $resp->assertSee('Guest');
177 $this->withHtml($resp)->assertElementContains('form[action$="/settings/users/' . $guestUser->id . '"] button', 'Confirm');
179 $resp = $this->delete('/settings/users/' . $guestUser->id);
180 $resp->assertRedirect('/settings/users/' . $guestUser->id);
181 $resp = $this->followRedirects($resp);
182 $resp->assertSee('cannot delete the guest user');
185 public function test_user_create_language_reflects_default_system_locale()
187 $langs = ['en', 'fr', 'hr'];
188 foreach ($langs as $lang) {
189 config()->set('app.locale', $lang);
190 $resp = $this->asAdmin()->get('/settings/users/create');
191 $this->withHtml($resp)->assertElementExists('select[name="language"] option[value="' . $lang . '"][selected]');
195 public function test_user_creation_is_not_performed_if_the_invitation_sending_fails()
197 /** @var User $user */
198 $user = User::factory()->make();
199 $adminRole = Role::getRole('admin');
201 // Simulate an invitation sending failure
202 $this->mock(UserInviteService::class, function (MockInterface $mock) {
203 $mock->shouldReceive('sendInvitation')->once()->andThrow(RuntimeException::class);
206 $this->asAdmin()->post('/settings/users/create', [
207 'name' => $user->name,
208 'email' => $user->email,
209 'send_invite' => 'true',
210 'roles[' . $adminRole->id . ']' => 'true',
213 // Since the invitation failed, the user should not exist in the database
214 $this->assertDatabaseMissing('users', $user->only('name', 'email'));
217 public function test_user_create_activity_is_not_persisted_if_the_invitation_sending_fails()
219 /** @var User $user */
220 $user = User::factory()->make();
221 $adminRole = Role::getRole('admin');
223 $this->mock(UserInviteService::class, function (MockInterface $mock) {
224 $mock->shouldReceive('sendInvitation')->once()->andThrow(RuntimeException::class);
227 $this->asAdmin()->post('/settings/users/create', [
228 'name' => $user->name,
229 'email' => $user->email,
230 'send_invite' => 'true',
231 'roles[' . $adminRole->id . ']' => 'true',
234 $this->assertDatabaseMissing('activities', ['type' => 'USER_CREATE']);
237 public function test_user_create_update_fails_if_locale_is_invalid()
239 $user = $this->getEditor();
242 $resp = $this->asAdmin()->put($user->getEditUrl(), ['language' => 'this_is_too_long']);
243 $resp->assertSessionHasErrors(['language' => 'The language may not be greater than 15 characters.']);
246 // Invalid characters
247 $resp = $this->put($user->getEditUrl(), ['language' => 'en<GB']);
248 $resp->assertSessionHasErrors(['language' => 'The language may only contain letters, numbers, dashes and underscores.']);
252 $resp = $this->post('/settings/users/create', [
253 'language' => 'en<GB_and_this_is_longer',
257 $resp->assertSessionHasErrors(['language' => 'The language may not be greater than 15 characters.']);
258 $resp->assertSessionHasErrors(['language' => 'The language may only contain letters, numbers, dashes and underscores.']);