|
12 | 12 | use App\Models\UserRole;
|
13 | 13 | use App\Notifications\AccountCreatedNotification;
|
14 | 14 | use App\Policies\UserPolicy;
|
| 15 | +use Closure; |
15 | 16 | use Illuminate\Foundation\Testing\RefreshDatabase;
|
16 | 17 | use Illuminate\Support\Facades\Notification;
|
17 | 18 | use PHPUnit\Framework\Attributes\CoversClass;
|
| 19 | +use PHPUnit\Framework\Attributes\DataProvider; |
18 | 20 | use Tests\TestCase;
|
19 | 21 |
|
20 | 22 | #[CoversClass(AccountCreatedNotification::class)]
|
@@ -121,6 +123,40 @@ public function testUserCanDeleteUsersOnlyWithCorrectAbility(): void
|
121 | 123 | $this->assertDatabaseMissing('users', ['id' => $user->id]);
|
122 | 124 | }
|
123 | 125 |
|
| 126 | + public function testUserCannotDeleteHimself(): void |
| 127 | + { |
| 128 | + $user = $this->actingAsUserWithAbility(Ability::DestroyUsers); |
| 129 | + |
| 130 | + $this->delete("/users/{$user->id}") |
| 131 | + ->assertForbidden() |
| 132 | + ->assertSee('kann nicht gelöscht werden, da es sich um das eigene Konto handelt.'); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * @param Closure(): User $userProvider |
| 137 | + */ |
| 138 | + #[DataProvider('usersWithReferences')] |
| 139 | + public function testUserCannotBeDeletedBecauseOfReferences(Closure $userProvider, string $message): void |
| 140 | + { |
| 141 | + $user = $userProvider(); |
| 142 | + |
| 143 | + $this->assertUserCannotDeleteDespiteAbility("/users/{$user->id}", [Ability::ViewUsers, Ability::DestroyUsers], $message); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * @return array<int, array{Closure(): User, string}> |
| 148 | + */ |
| 149 | + public static function usersWithReferences(): array |
| 150 | + { |
| 151 | + return [ |
| 152 | + [fn () => self::createBooking()->bookedByUser, 'kann nicht gelöscht werden, weil er/sie 1 Anmeldung hat.'], |
| 153 | + [fn () => self::createDocument(static fn () => self::createEvent())->uploadedByUser, 'kann nicht gelöscht werden, weil er/sie 1 Dokument hochgeladen hat.'], |
| 154 | + [fn () => self::createUserResponsibleFor(self::createEvent()), 'kann nicht gelöscht werden, weil er/sie für 1 Veranstaltung verantwortlich ist.'], |
| 155 | + [fn () => self::createUserResponsibleFor(self::createEventSeries()), 'kann nicht gelöscht werden, weil er/sie für 1 Veranstaltungsreihe verantwortlich ist.'], |
| 156 | + [fn () => self::createUserResponsibleFor(self::createOrganization()), 'kann nicht gelöscht werden, weil er/sie für 1 Organisation verantwortlich ist. '], |
| 157 | + ]; |
| 158 | + } |
| 159 | + |
124 | 160 | private function createRandomUser(): User
|
125 | 161 | {
|
126 | 162 | return User::factory()->create();
|
|
0 commit comments