From 2ec7dd23eb2b67c65924211344fe8c4825f202a8 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Fri, 2 Aug 2024 15:39:09 +0200 Subject: [PATCH] Add: Update user --- app/Http/Resources/UserResource.php | 3 ++ app/User/Actions/UpdateAction.php | 30 +++++++++++ routes/web.php | 2 + tests/Feature/Permission/UserIndexTest.php | 1 + tests/Feature/Permission/UserUpdateTest.php | 56 +++++++++++++++++++++ 5 files changed, 92 insertions(+) create mode 100644 app/User/Actions/UpdateAction.php create mode 100644 tests/Feature/Permission/UserUpdateTest.php diff --git a/app/Http/Resources/UserResource.php b/app/Http/Resources/UserResource.php index 6908e10a..e1d6955b 100644 --- a/app/Http/Resources/UserResource.php +++ b/app/Http/Resources/UserResource.php @@ -32,6 +32,9 @@ class UserResource extends JsonResource 'avatar' => [ 'src' => Storage::url('avatar.png'), ], + 'links' => [ + 'update' => route('user.update', ['user' => $this->getModel()]), + ] ]; } diff --git a/app/User/Actions/UpdateAction.php b/app/User/Actions/UpdateAction.php new file mode 100644 index 00000000..36aee67a --- /dev/null +++ b/app/User/Actions/UpdateAction.php @@ -0,0 +1,30 @@ + 'required|string|max:255', + 'lastname' => 'required|string|max:255', + 'email' => ['required', 'string', 'email', 'max:255', Rule::unique('users', 'email')->ignore(request()->route('user')->id)], + ]; + } + + public function handle(User $user, ActionRequest $request): JsonResponse + { + $user->update($request->validated()); + + return response()->json([]); + } +} diff --git a/routes/web.php b/routes/web.php index 98c2afa8..6ae18aec 100644 --- a/routes/web.php +++ b/routes/web.php @@ -78,6 +78,7 @@ use App\Membership\Actions\MembershipStoreAction; use App\Membership\Actions\MembershipUpdateAction; use App\Payment\SubscriptionController; use App\User\Actions\StoreAction as UserStoreAction; +use App\User\Actions\UpdateAction as UserUpdateAction; Route::group(['namespace' => 'App\\Http\\Controllers'], function (): void { Auth::routes(['register' => false]); @@ -186,4 +187,5 @@ Route::group(['middleware' => 'auth:web'], function (): void { // -------------------------------------- user ------------------------------------- Route::post('/user', UserStoreAction::class)->name('user.store'); + Route::patch('/user/{user}', UserUpdateAction::class)->name('user.update'); }); diff --git a/tests/Feature/Permission/UserIndexTest.php b/tests/Feature/Permission/UserIndexTest.php index 4346c816..b3b336e5 100644 --- a/tests/Feature/Permission/UserIndexTest.php +++ b/tests/Feature/Permission/UserIndexTest.php @@ -23,6 +23,7 @@ class UserIndexTest extends TestCase ->assertInertiaPath('data.data.0.firstname', 'Anna') ->assertInertiaPath('data.data.0.lastname', 'Doe') ->assertInertiaPath('data.data.0.id', $anna->id) + ->assertInertiaPath('data.data.0.links.update', route('user.update', ['user' => $anna])) ->assertInertiaPath('data.data.1.firstname', 'Jane') ->assertInertiaPath('data.data.2.firstname', 'John') ->assertInertiaPath('data.meta.default.firstname', '') diff --git a/tests/Feature/Permission/UserUpdateTest.php b/tests/Feature/Permission/UserUpdateTest.php new file mode 100644 index 00000000..a6f848f9 --- /dev/null +++ b/tests/Feature/Permission/UserUpdateTest.php @@ -0,0 +1,56 @@ +login()->loginNami(); + + $user = User::factory()->create(); + $this->patchJson(route('user.update', ['user' => $user]), UserRequestFactory::new()->name('Max Muster')->email('max@muster.de')->create()) + ->assertOk(); + + $this->assertDatabaseHas('users', [ + 'email' => 'max@muster.de', + 'firstname' => 'Max', + 'lastname' => 'Muster', + ]); + $this->assertDatabaseCount('users', 2); + } + + public function testItCannotUseAGivenMailAddress(): void + { + $this->login()->loginNami(); + + $jane = User::factory()->email('jane@muster.de')->create(); + $user = User::factory()->create(); + $this->patchJson(route('user.update', ['user' => $user]), UserRequestFactory::new()->email('jane@muster.de')->create()) + ->assertJsonValidationErrors('email'); + $this->patchJson(route('user.update', ['user' => $jane]), UserRequestFactory::new()->email('jane@muster.de')->create()) + ->assertJsonMissingValidationErrors('email'); + } + + public function testItNeedsNameAndEmail(): void + { + Mail::fake(); + $this->login()->loginNami(); + $user = User::factory()->create(); + + User::factory()->email('jane@doe.de')->create(); + $this->patchJson(route('user.update', ['user' => $user]), UserRequestFactory::new()->name('')->email('max@muster.de')->create())->assertJsonValidationErrors('firstname'); + $this->patchJson(route('user.update', ['user' => $user]), UserRequestFactory::new()->name('Max Muster')->email('maxusterde')->create())->assertJsonValidationErrors('email'); + $this->patchJson(route('user.update', ['user' => $user]), UserRequestFactory::new()->name('Max Muster')->email('')->create())->assertJsonValidationErrors('email'); + $this->patchJson(route('user.update', ['user' => $user]), UserRequestFactory::new()->email('jane@doe.de')->create())->assertJsonValidationErrors('email'); + } +}