Add: Update user
continuous-integration/drone/push Build is failing Details

This commit is contained in:
philipp lang 2024-08-02 15:39:09 +02:00
parent 4548407d55
commit 2ec7dd23eb
5 changed files with 92 additions and 0 deletions

View File

@ -32,6 +32,9 @@ class UserResource extends JsonResource
'avatar' => [ 'avatar' => [
'src' => Storage::url('avatar.png'), 'src' => Storage::url('avatar.png'),
], ],
'links' => [
'update' => route('user.update', ['user' => $this->getModel()]),
]
]; ];
} }

View File

@ -0,0 +1,30 @@
<?php
namespace App\User\Actions;
use App\User;
use Illuminate\Validation\Rule;
use Illuminate\Http\JsonResponse;
use Lorisleiva\Actions\ActionRequest;
use Lorisleiva\Actions\Concerns\AsAction;
class UpdateAction
{
use AsAction;
public function rules(): array
{
return [
'firstname' => '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([]);
}
}

View File

@ -78,6 +78,7 @@ use App\Membership\Actions\MembershipStoreAction;
use App\Membership\Actions\MembershipUpdateAction; use App\Membership\Actions\MembershipUpdateAction;
use App\Payment\SubscriptionController; use App\Payment\SubscriptionController;
use App\User\Actions\StoreAction as UserStoreAction; use App\User\Actions\StoreAction as UserStoreAction;
use App\User\Actions\UpdateAction as UserUpdateAction;
Route::group(['namespace' => 'App\\Http\\Controllers'], function (): void { Route::group(['namespace' => 'App\\Http\\Controllers'], function (): void {
Auth::routes(['register' => false]); Auth::routes(['register' => false]);
@ -186,4 +187,5 @@ Route::group(['middleware' => 'auth:web'], function (): void {
// -------------------------------------- user ------------------------------------- // -------------------------------------- user -------------------------------------
Route::post('/user', UserStoreAction::class)->name('user.store'); Route::post('/user', UserStoreAction::class)->name('user.store');
Route::patch('/user/{user}', UserUpdateAction::class)->name('user.update');
}); });

View File

@ -23,6 +23,7 @@ class UserIndexTest extends TestCase
->assertInertiaPath('data.data.0.firstname', 'Anna') ->assertInertiaPath('data.data.0.firstname', 'Anna')
->assertInertiaPath('data.data.0.lastname', 'Doe') ->assertInertiaPath('data.data.0.lastname', 'Doe')
->assertInertiaPath('data.data.0.id', $anna->id) ->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.1.firstname', 'Jane')
->assertInertiaPath('data.data.2.firstname', 'John') ->assertInertiaPath('data.data.2.firstname', 'John')
->assertInertiaPath('data.meta.default.firstname', '') ->assertInertiaPath('data.meta.default.firstname', '')

View File

@ -0,0 +1,56 @@
<?php
namespace Tests\Feature\Permission;
use App\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Mail;
use Tests\RequestFactories\UserRequestFactory;
use Tests\TestCase;
class UserUpdateTest extends TestCase
{
use DatabaseTransactions;
public function testItUpdatesAUser(): void
{
$this->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');
}
}