]> BookStack Code Mirror - bookstack/blobdiff - tests/User/UserManagementTest.php
Testing: Added entity decode flag and phpunit env option
[bookstack] / tests / User / UserManagementTest.php
index c09ce8cb3517494ca6d8da7658e0fe890ef27d15..df60bede6e1de83d83720ad5ed2608f7a524a9ce 100644 (file)
@@ -2,11 +2,11 @@
 
 namespace Tests\User;
 
-use BookStack\Actions\ActivityType;
-use BookStack\Auth\Access\UserInviteService;
-use BookStack\Auth\Role;
-use BookStack\Auth\User;
-use BookStack\Entities\Models\Page;
+use BookStack\Access\UserInviteService;
+use BookStack\Activity\ActivityType;
+use BookStack\Uploads\Image;
+use BookStack\Users\Models\Role;
+use BookStack\Users\Models\User;
 use Illuminate\Support\Facades\Hash;
 use Illuminate\Support\Str;
 use Mockery\MockInterface;
@@ -47,7 +47,7 @@ class UserManagementTest extends TestCase
 
     public function test_user_updating()
     {
-        $user = $this->getNormalUser();
+        $user = $this->users->viewer();
         $password = $user->password;
 
         $resp = $this->asAdmin()->get('/settings/users/' . $user->id);
@@ -66,7 +66,7 @@ class UserManagementTest extends TestCase
 
     public function test_user_password_update()
     {
-        $user = $this->getNormalUser();
+        $user = $this->users->viewer();
         $userProfilePage = '/settings/users/' . $user->id;
 
         $this->asAdmin()->get($userProfilePage);
@@ -114,7 +114,7 @@ class UserManagementTest extends TestCase
 
     public function test_delete()
     {
-        $editor = $this->getEditor();
+        $editor = $this->users->editor();
         $resp = $this->asAdmin()->delete("settings/users/{$editor->id}");
         $resp->assertRedirect('/settings/users');
         $resp = $this->followRedirects($resp);
@@ -127,7 +127,7 @@ class UserManagementTest extends TestCase
 
     public function test_delete_offers_migrate_option()
     {
-        $editor = $this->getEditor();
+        $editor = $this->users->editor();
         $resp = $this->asAdmin()->get("settings/users/{$editor->id}/delete");
         $resp->assertSee('Migrate Ownership');
         $resp->assertSee('new_owner_id');
@@ -135,22 +135,23 @@ class UserManagementTest extends TestCase
 
     public function test_migrate_option_hidden_if_user_cannot_manage_users()
     {
-        $editor = $this->getEditor();
+        $editor = $this->users->editor();
 
         $resp = $this->asEditor()->get("settings/users/{$editor->id}/delete");
         $resp->assertDontSee('Migrate Ownership');
         $resp->assertDontSee('new_owner_id');
 
-        $this->giveUserPermissions($editor, ['users-manage']);
+        $this->permissions->grantUserRolePermissions($editor, ['users-manage']);
 
         $resp = $this->asEditor()->get("settings/users/{$editor->id}/delete");
         $resp->assertSee('Migrate Ownership');
+        $this->withHtml($resp)->assertElementExists('form input[name="new_owner_id"]');
         $resp->assertSee('new_owner_id');
     }
 
     public function test_delete_with_new_owner_id_changes_ownership()
     {
-        $page = Page::query()->first();
+        $page = $this->entities->page();
         $owner = $page->ownedBy;
         $newOwner = User::query()->where('id', '!=', $owner->id)->first();
 
@@ -161,6 +162,33 @@ class UserManagementTest extends TestCase
         ]);
     }
 
+    public function test_delete_with_empty_owner_migration_id_works()
+    {
+        $user = $this->users->editor();
+
+        $resp = $this->asAdmin()->delete("settings/users/{$user->id}", ['new_owner_id' => '']);
+        $resp->assertRedirect('/settings/users');
+        $this->assertActivityExists(ActivityType::USER_DELETE);
+        $this->assertSessionHas('success');
+    }
+
+    public function test_delete_removes_user_preferences()
+    {
+        $editor = $this->users->editor();
+        setting()->putUser($editor, 'dark-mode-enabled', 'true');
+
+        $this->assertDatabaseHas('settings', [
+            'setting_key' => 'user:' . $editor->id . ':dark-mode-enabled',
+            'value' => 'true',
+        ]);
+
+        $this->asAdmin()->delete("settings/users/{$editor->id}");
+
+        $this->assertDatabaseMissing('settings', [
+            'setting_key' => 'user:' . $editor->id . ':dark-mode-enabled',
+        ]);
+    }
+
     public function test_guest_profile_shows_limited_form()
     {
         $guest = User::getDefault();
@@ -234,4 +262,57 @@ class UserManagementTest extends TestCase
 
         $this->assertDatabaseMissing('activities', ['type' => 'USER_CREATE']);
     }
+
+    public function test_user_create_update_fails_if_locale_is_invalid()
+    {
+        $user = $this->users->editor();
+
+        // Too long
+        $resp = $this->asAdmin()->put($user->getEditUrl(), ['language' => 'this_is_too_long']);
+        $resp->assertSessionHasErrors(['language' => 'The language may not be greater than 15 characters.']);
+        session()->flush();
+
+        // Invalid characters
+        $resp = $this->put($user->getEditUrl(), ['language' => 'en<GB']);
+        $resp->assertSessionHasErrors(['language' => 'The language may only contain letters, numbers, dashes and underscores.']);
+        session()->flush();
+
+        // Both on create
+        $resp = $this->post('/settings/users/create', [
+            'language' => 'en<GB_and_this_is_longer',
+            'name'     => 'My name',
+            'email'    => '[email protected]',
+        ]);
+        $resp->assertSessionHasErrors(['language' => 'The language may not be greater than 15 characters.']);
+        $resp->assertSessionHasErrors(['language' => 'The language may only contain letters, numbers, dashes and underscores.']);
+    }
+
+    public function test_user_avatar_update_and_reset()
+    {
+        $user = $this->users->viewer();
+        $avatarFile = $this->files->uploadedImage('avatar-icon.png');
+
+        $this->assertEquals(0, $user->image_id);
+
+        $upload = $this->asAdmin()->call('PUT', "/settings/users/{$user->id}", [
+            'name' => 'Barry Scott',
+        ], [], ['profile_image' => $avatarFile], []);
+        $upload->assertRedirect('/settings/users');
+
+        $user->refresh();
+        $this->assertNotEquals(0, $user->image_id);
+        /** @var Image $image */
+        $image = Image::query()->findOrFail($user->image_id);
+        $this->assertFileExists(public_path($image->path));
+
+        $reset = $this->put("/settings/users/{$user->id}", [
+            'name' => 'Barry Scott',
+            'profile_image_reset' => 'true',
+        ]);
+        $upload->assertRedirect('/settings/users');
+
+        $user->refresh();
+        $this->assertFileDoesNotExist(public_path($image->path));
+        $this->assertEquals(0, $user->image_id);
+    }
 }