From 03ad288aaa55a0bd0aa143711aa07d0f1801d604 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Thu, 26 Jan 2023 17:15:09 +0000 Subject: [PATCH] Updated user avatar reset to clear relation id in database Added test to cover. For #3977 --- app/Http/Controllers/UserController.php | 2 ++ tests/Settings/SettingsTest.php | 1 - tests/User/UserManagementTest.php | 33 +++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index f69f00cf7..2fcfa4289 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -164,6 +164,8 @@ class UserController extends Controller // Delete the profile image if reset option is in request if ($request->has('profile_image_reset')) { $this->imageRepo->destroyImage($user->avatar); + $user->image_id = 0; + $user->save(); } $redirectUrl = userCan('users-manage') ? '/settings/users' : "/settings/users/{$user->id}"; diff --git a/tests/Settings/SettingsTest.php b/tests/Settings/SettingsTest.php index 1161a466e..f4db6f1c8 100644 --- a/tests/Settings/SettingsTest.php +++ b/tests/Settings/SettingsTest.php @@ -2,7 +2,6 @@ namespace Tests\Settings; -use Illuminate\Support\Facades\Storage; use Tests\TestCase; use Tests\Uploads\UsesImages; diff --git a/tests/User/UserManagementTest.php b/tests/User/UserManagementTest.php index 1c5c040da..38447d293 100644 --- a/tests/User/UserManagementTest.php +++ b/tests/User/UserManagementTest.php @@ -6,14 +6,18 @@ use BookStack\Actions\ActivityType; use BookStack\Auth\Access\UserInviteService; use BookStack\Auth\Role; use BookStack\Auth\User; +use BookStack\Uploads\Image; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Str; use Mockery\MockInterface; use RuntimeException; use Tests\TestCase; +use Tests\Uploads\UsesImages; class UserManagementTest extends TestCase { + use UsesImages; + public function test_user_creation() { /** @var User $user */ @@ -274,4 +278,33 @@ class UserManagementTest extends TestCase $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->getTestImage('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); + } }