Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,11 @@ public function hasBlocked(User $user): bool
return $this->blockedUsers()->where('blocked_user_id', $user->getKey())->exists();
}

public function hasUnblocked(User $user): bool
{
return ! $this->hasBlocked($user);
}

public function scopeWithUsersWhoDoesntBlock(Builder $query, User $user)
{
return $query->whereDoesntHave('blockedUsers', function ($query) use ($user) {
Expand Down
65 changes: 65 additions & 0 deletions tests/Feature/BlockUsersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\Feature\BrowserKitTestCase;

uses(BrowserKitTestCase::class);
uses(RefreshDatabase::class);

test('cannot block user when not logged in', function () {
$user = $this->createUser();

$this->put("/users/{$user->username}/block")->assertRedirectedTo('login');
});

test('cannot unblock user when not logged in', function () {
$user = $this->createUser();

$this->put("/users/{$user->username}/unblock")->assertRedirectedTo('login');
});

test('cannot block self', function () {
$user = $this->createUser();

$this->loginAs($user);

$this->put("/users/{$user->username}/block")->assertForbidden();
});

test('cannot block moderator', function () {
$user = $this->createUser();
$moderator = $this->createUser([
'username' => 'moderator',
'email' => 'moderator@example.com',
'type' => User::MODERATOR,
]);

$this->loginAs($user);

$this->put("/users/{$moderator->username}/block")->assertForbidden();
});

test('can block other user', function () {
$blocker = $this->createUser();
$blocked = $this->createUser([
'username' => 'blocked',
'email' => 'blocked@example.com',
]);

$this->loginAs($blocker);

$this->put("/users/{$blocked->username}/block")->assertSessionHas('success', trans('settings.user.blocked'));
});

test('can unblock other user', function () {
$unblocker = $this->createUser();
$unblocked = $this->createUser([
'username' => 'unblocked',
'email' => 'unblocked@example.com',
]);

$this->loginAs($unblocker);

$this->put("/users/{$unblocked->username}/unblock")->assertSessionHas('success', trans('settings.user.unblocked'));
});
22 changes: 22 additions & 0 deletions tests/Integration/Jobs/BlockUserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use App\Jobs\BlockUser;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\TestCase;

uses(TestCase::class);
uses(DatabaseMigrations::class);

it('can block a user', function () {
$blocker = $this->createUser();
$blocked = $this->createUser([
'username' => 'blocked',
'email' => 'blocked@example.com',
]);

$this->loginAs($blocker);

$this->dispatch(new BlockUser($blocker, $blocked));

expect($blocker->hasBlocked($blocked))->toBeTrue();
});
22 changes: 22 additions & 0 deletions tests/Integration/Jobs/UnblockUserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use App\Jobs\UnblockUser;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\TestCase;

uses(TestCase::class);
uses(DatabaseMigrations::class);

it('can unblock a user', function () {
$unblocker = $this->createUser();
$unblocked = $this->createUser([
'username' => 'unblocked',
'email' => 'unblocked@example.com',
]);

$this->loginAs($unblocker);

$this->dispatch(new UnblockUser($unblocker, $unblocked));

expect($unblocker->hasUnblocked($unblocked))->toBeTrue();
});