]> BookStack Code Mirror - bookstack/blob - tests/Commands/DeleteUsersCommandTest.php
PHP: Addressed 8.4 deprecations within app itself
[bookstack] / tests / Commands / DeleteUsersCommandTest.php
1 <?php
2
3 namespace Tests\Commands;
4
5 use BookStack\Users\Models\User;
6 use Illuminate\Database\Eloquent\Collection;
7 use Tests\TestCase;
8
9 class DeleteUsersCommandTest extends TestCase
10 {
11     public function test_command_deletes_users()
12     {
13         $userCount = User::query()->count();
14         $normalUsers = $this->getNormalUsers();
15
16         $normalUserCount = $userCount - count($normalUsers);
17         $this->artisan('bookstack:delete-users')
18             ->expectsConfirmation('Are you sure you want to continue?', 'yes')
19             ->expectsOutputToContain("Deleted $normalUserCount of $userCount total users.")
20             ->assertExitCode(0);
21
22         $this->assertDatabaseMissing('users', ['id' => $normalUsers->first()->id]);
23     }
24
25     public function test_command_requires_confirmation()
26     {
27         $normalUsers = $this->getNormalUsers();
28
29         $this->artisan('bookstack:delete-users')
30             ->expectsConfirmation('Are you sure you want to continue?', 'no')
31             ->assertExitCode(0);
32
33         $this->assertDatabaseHas('users', ['id' => $normalUsers->first()->id]);
34     }
35
36     protected function getNormalUsers(): Collection
37     {
38         return User::query()->whereNull('system_name')
39             ->get()
40             ->filter(function (User $user) {
41                 return !$user->hasSystemRole('admin');
42             });
43     }
44 }