profile test passes now
This commit is contained in:
@@ -51,6 +51,14 @@ class ProfileController extends Controller
|
|||||||
|
|
||||||
$user = $request->user();
|
$user = $request->user();
|
||||||
|
|
||||||
|
if ($user->ownedOrganisations()->count()) {
|
||||||
|
return back()->withErrors(['password' => __('You must delete or transfer your organisations before you can delete your account.')]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($user->organisations()->count()) {
|
||||||
|
return back()->withErrors(['password' => __('You must leave your organisations before you can delete your account.')]);
|
||||||
|
}
|
||||||
|
|
||||||
Auth::logout();
|
Auth::logout();
|
||||||
|
|
||||||
$user->delete();
|
$user->delete();
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ namespace App\Models;
|
|||||||
|
|
||||||
use App\Enums\OrganisationRole;
|
use App\Enums\OrganisationRole;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||||
use Illuminate\Notifications\Notifiable;
|
use Illuminate\Notifications\Notifiable;
|
||||||
|
|
||||||
@@ -47,7 +49,12 @@ class User extends Authenticatable
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function organisations()
|
public function ownedOrganisations(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(Organisation::class, 'owner_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function organisations(): BelongsToMany
|
||||||
{
|
{
|
||||||
return $this->belongsToMany(Organisation::class)
|
return $this->belongsToMany(Organisation::class)
|
||||||
->withPivot('role')
|
->withPivot('role')
|
||||||
|
|||||||
@@ -50,9 +50,27 @@ test('email verification status is unchanged when the email address is unchanged
|
|||||||
expect($user->refresh()->email_verified_at)->not->toBeNull();
|
expect($user->refresh()->email_verified_at)->not->toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('user is prevented from deleting their account if they own organisations', function () {
|
||||||
|
$user = User::factory()->create();
|
||||||
|
|
||||||
|
$response = $this
|
||||||
|
->actingAs($user)
|
||||||
|
->delete('/settings/profile', [
|
||||||
|
'password' => 'password',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$response
|
||||||
|
->assertSessionHasErrors('password');
|
||||||
|
|
||||||
|
expect($user->fresh())->not->toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
test('user can delete their account', function () {
|
test('user can delete their account', function () {
|
||||||
$user = User::factory()->create();
|
$user = User::factory()->create();
|
||||||
|
|
||||||
|
$user->ownedOrganisations()->delete();
|
||||||
|
$user->organisations()->delete();
|
||||||
|
|
||||||
$response = $this
|
$response = $this
|
||||||
->actingAs($user)
|
->actingAs($user)
|
||||||
->delete('/settings/profile', [
|
->delete('/settings/profile', [
|
||||||
|
|||||||
Reference in New Issue
Block a user