Skip to content

Commit 5acc43e

Browse files
added test suite
1 parent 202f67d commit 5acc43e

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

tests/Feature/AdminTest.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,24 @@
4141
assertCanBanUsers();
4242
});
4343

44+
test('admins can ban a user and delete their threads', function () {
45+
$this->loginAsAdmin();
46+
47+
assertCanBanUsersAndDeleteThreads();
48+
});
49+
4450
test('moderators can ban a user', function () {
4551
$this->loginAsModerator();
4652

4753
assertCanBanUsers();
4854
});
4955

56+
test('moderators can ban a user and delete their threads', function () {
57+
$this->loginAsModerator();
58+
59+
assertCanBanUsersAndDeleteThreads();
60+
});
61+
5062
test('admins can unban a user', function () {
5163
$this->loginAsAdmin();
5264

@@ -366,11 +378,23 @@ function assertCanBanUsers()
366378
{
367379
$user = User::factory()->create(['name' => 'Freek Murze']);
368380

369-
test()->put('/admin/users/'.$user->username().'/ban', ['reason' => 'A good reason'])
381+
test()->put('/admin/users/'.$user->username().'/ban', ['reason' => 'A good reason', 'delete_threads' => false])
382+
->assertRedirect('/user/'.$user->username());
383+
384+
test()->assertDatabaseMissing('users', ['id' => $user->id(), 'banned_at' => null]);
385+
test()->assertDatabaseHas('users', ['id' => $user->id(), 'banned_reason' => 'A good reason']);
386+
}
387+
388+
function assertCanBanUsersAndDeleteThreads()
389+
{
390+
$user = User::factory()->create(['name' => 'Freek Murze']);
391+
392+
test()->put('/admin/users/'.$user->username().'/ban', ['reason' => 'A good reason', 'delete_threads' => true])
370393
->assertRedirect('/user/'.$user->username());
371394

372395
test()->assertDatabaseMissing('users', ['id' => $user->id(), 'banned_at' => null]);
373396
test()->assertDatabaseHas('users', ['id' => $user->id(), 'banned_reason' => 'A good reason']);
397+
test()->assertDatabaseMissing('threads', ['author_id' => $user->id()]);
374398
}
375399

376400
function assertCanUnbanUsers()
@@ -397,6 +421,6 @@ function assertCannotBanUsersByType(int $type)
397421
{
398422
$user = User::factory()->create(['type' => $type]);
399423

400-
test()->put('/admin/users/'.$user->username().'/ban', ['reason' => 'A good reason'])
424+
test()->put('/admin/users/'.$user->username().'/ban', ['reason' => 'A good reason', 'delete_threads' => fake()->boolean()])
401425
->assertForbidden();
402426
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
use App\Jobs\DeleteUserThreads;
4+
use App\Models\Thread;
5+
use App\Models\User;
6+
use Illuminate\Foundation\Testing\DatabaseMigrations;
7+
use Tests\TestCase;
8+
9+
uses(TestCase::class);
10+
uses(DatabaseMigrations::class);
11+
12+
test('we can delete an user threads', function () {
13+
$user = User::factory()->create();
14+
15+
Thread::factory()->for($user, 'authorRelation')->count(5)->create();
16+
17+
$this->loginAsAdmin();
18+
$this->dispatch(new DeleteUserThreads($user));
19+
20+
$this->assertDatabaseMissing('threads', ['author_id' => $user->id()]);
21+
});

0 commit comments

Comments
 (0)