Skip to content

Commit 03ad288

Browse files
committed
Updated user avatar reset to clear relation id in database
Added test to cover. For #3977
1 parent 811be3a commit 03ad288

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

app/Http/Controllers/UserController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ public function update(Request $request, int $id)
164164
// Delete the profile image if reset option is in request
165165
if ($request->has('profile_image_reset')) {
166166
$this->imageRepo->destroyImage($user->avatar);
167+
$user->image_id = 0;
168+
$user->save();
167169
}
168170

169171
$redirectUrl = userCan('users-manage') ? '/settings/users' : "/settings/users/{$user->id}";

tests/Settings/SettingsTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Tests\Settings;
44

5-
use Illuminate\Support\Facades\Storage;
65
use Tests\TestCase;
76
use Tests\Uploads\UsesImages;
87

tests/User/UserManagementTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@
66
use BookStack\Auth\Access\UserInviteService;
77
use BookStack\Auth\Role;
88
use BookStack\Auth\User;
9+
use BookStack\Uploads\Image;
910
use Illuminate\Support\Facades\Hash;
1011
use Illuminate\Support\Str;
1112
use Mockery\MockInterface;
1213
use RuntimeException;
1314
use Tests\TestCase;
15+
use Tests\Uploads\UsesImages;
1416

1517
class UserManagementTest extends TestCase
1618
{
19+
use UsesImages;
20+
1721
public function test_user_creation()
1822
{
1923
/** @var User $user */
@@ -274,4 +278,33 @@ public function test_user_create_update_fails_if_locale_is_invalid()
274278
$resp->assertSessionHasErrors(['language' => 'The language may not be greater than 15 characters.']);
275279
$resp->assertSessionHasErrors(['language' => 'The language may only contain letters, numbers, dashes and underscores.']);
276280
}
281+
282+
public function test_user_avatar_update_and_reset()
283+
{
284+
$user = $this->users->viewer();
285+
$avatarFile = $this->getTestImage('avatar-icon.png');
286+
287+
$this->assertEquals(0, $user->image_id);
288+
289+
$upload = $this->asAdmin()->call('PUT', "/settings/users/{$user->id}", [
290+
'name' => 'Barry Scott',
291+
], [], ['profile_image' => $avatarFile], []);
292+
$upload->assertRedirect('/settings/users');
293+
294+
$user->refresh();
295+
$this->assertNotEquals(0, $user->image_id);
296+
/** @var Image $image */
297+
$image = Image::query()->findOrFail($user->image_id);
298+
$this->assertFileExists(public_path($image->path));
299+
300+
$reset = $this->put("/settings/users/{$user->id}", [
301+
'name' => 'Barry Scott',
302+
'profile_image_reset' => 'true',
303+
]);
304+
$upload->assertRedirect('/settings/users');
305+
306+
$user->refresh();
307+
$this->assertFileDoesNotExist(public_path($image->path));
308+
$this->assertEquals(0, $user->image_id);
309+
}
277310
}

0 commit comments

Comments
 (0)