2021-11-30 17:39:33 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature\Services;
|
|
|
|
|
2022-11-22 15:15:52 +01:00
|
|
|
use App\Facades\Groups;
|
2021-12-02 13:15:53 +01:00
|
|
|
use App\Models\Group;
|
|
|
|
use App\Models\TwoFAccount;
|
2023-02-27 00:32:49 +01:00
|
|
|
use App\Models\User;
|
2023-08-01 11:28:27 +02:00
|
|
|
use App\Services\GroupService;
|
2023-02-27 00:32:49 +01:00
|
|
|
use Illuminate\Auth\Access\AuthorizationException;
|
2023-08-01 11:28:27 +02:00
|
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
2021-11-30 17:39:33 +01:00
|
|
|
use Tests\FeatureTestCase;
|
|
|
|
|
|
|
|
/**
|
2023-08-01 11:28:27 +02:00
|
|
|
* GroupServiceTest test class
|
2021-11-30 17:39:33 +01:00
|
|
|
*/
|
2023-08-01 11:28:27 +02:00
|
|
|
#[CoversClass(GroupService::class)]
|
|
|
|
#[CoversClass(Groups::class)]
|
2021-11-30 17:39:33 +01:00
|
|
|
class GroupServiceTest extends FeatureTestCase
|
|
|
|
{
|
2023-02-27 00:32:49 +01:00
|
|
|
/**
|
|
|
|
* @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
|
|
|
|
*/
|
|
|
|
protected $user;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
|
|
|
|
*/
|
2023-03-02 14:32:53 +01:00
|
|
|
protected $otherUser;
|
2023-02-27 00:32:49 +01:00
|
|
|
|
2021-11-30 17:39:33 +01:00
|
|
|
/**
|
2023-03-02 14:32:53 +01:00
|
|
|
* App\Models\Group $groupOne, $groupTwo, $groupThree
|
2021-11-30 17:39:33 +01:00
|
|
|
*/
|
2022-11-22 15:15:52 +01:00
|
|
|
protected $groupOne;
|
2021-11-30 17:39:33 +01:00
|
|
|
|
2022-11-22 15:15:52 +01:00
|
|
|
protected $groupTwo;
|
2021-11-30 17:39:33 +01:00
|
|
|
|
2023-03-02 14:32:53 +01:00
|
|
|
protected $groupThree;
|
|
|
|
|
2021-11-30 17:39:33 +01:00
|
|
|
/**
|
2023-03-02 14:32:53 +01:00
|
|
|
* App\Models\Group $twofaccountOne, $twofaccountTwo, $twofaccountThree
|
2021-11-30 17:39:33 +01:00
|
|
|
*/
|
2022-11-22 15:15:52 +01:00
|
|
|
protected $twofaccountOne;
|
|
|
|
|
|
|
|
protected $twofaccountTwo;
|
2021-11-30 17:39:33 +01:00
|
|
|
|
2023-03-02 14:32:53 +01:00
|
|
|
protected $twofaccountThree;
|
|
|
|
|
2021-11-30 17:39:33 +01:00
|
|
|
private const NEW_GROUP_NAME = 'MyNewGroup';
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function setUp() : void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2023-03-10 22:59:46 +01:00
|
|
|
$this->user = User::factory()->create();
|
2023-03-02 14:32:53 +01:00
|
|
|
$this->otherUser = User::factory()->create();
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2023-03-10 22:59:46 +01:00
|
|
|
$this->groupOne = Group::factory()->for($this->user)->create();
|
|
|
|
$this->groupTwo = Group::factory()->for($this->user)->create();
|
2023-03-02 14:32:53 +01:00
|
|
|
$this->groupThree = Group::factory()->for($this->otherUser)->create();
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2023-03-02 14:32:53 +01:00
|
|
|
Group::factory()->count(2)->for($this->otherUser)->create();
|
2021-11-30 17:39:33 +01:00
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
$this->twofaccountOne = TwoFAccount::factory()->for($this->user)->create([
|
|
|
|
'group_id' => $this->groupOne->id,
|
|
|
|
]);
|
|
|
|
$this->twofaccountTwo = TwoFAccount::factory()->for($this->user)->create([
|
|
|
|
'group_id' => $this->groupTwo->id,
|
|
|
|
]);
|
2021-11-30 17:39:33 +01:00
|
|
|
|
2023-03-02 14:32:53 +01:00
|
|
|
$this->twofaccountThree = TwoFAccount::factory()->for($this->otherUser)->create([
|
|
|
|
'group_id' => $this->groupThree->id,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function test_assign_a_twofaccount_to_a_group_persists_the_relation()
|
|
|
|
{
|
2023-03-07 15:17:07 +01:00
|
|
|
Groups::assign($this->twofaccountOne->id, $this->user, $this->groupTwo);
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2021-11-30 17:39:33 +01:00
|
|
|
$this->assertDatabaseHas('twofaccounts', [
|
2022-11-22 15:15:52 +01:00
|
|
|
'id' => $this->twofaccountOne->id,
|
2023-02-27 00:32:49 +01:00
|
|
|
'group_id' => $this->groupTwo->id,
|
2021-11-30 17:39:33 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2023-03-07 15:17:07 +01:00
|
|
|
public function test_assign_multiple_twofaccounts_to_group_persists_the_relation()
|
2021-11-30 17:39:33 +01:00
|
|
|
{
|
2023-03-07 15:17:07 +01:00
|
|
|
Groups::assign([$this->twofaccountOne->id, $this->twofaccountTwo->id], $this->user, $this->groupTwo);
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2021-11-30 17:39:33 +01:00
|
|
|
$this->assertDatabaseHas('twofaccounts', [
|
2022-11-22 15:15:52 +01:00
|
|
|
'id' => $this->twofaccountOne->id,
|
2023-02-27 00:32:49 +01:00
|
|
|
'group_id' => $this->groupTwo->id,
|
2021-11-30 17:39:33 +01:00
|
|
|
]);
|
|
|
|
$this->assertDatabaseHas('twofaccounts', [
|
2022-11-22 15:15:52 +01:00
|
|
|
'id' => $this->twofaccountTwo->id,
|
2023-02-27 00:32:49 +01:00
|
|
|
'group_id' => $this->groupTwo->id,
|
2021-11-30 17:39:33 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2023-03-07 15:17:07 +01:00
|
|
|
public function test_assign_a_twofaccount_to_no_group_assigns_to_user_default_group()
|
2021-11-30 17:39:33 +01:00
|
|
|
{
|
2023-02-27 00:32:49 +01:00
|
|
|
$this->user['preferences->defaultGroup'] = $this->groupTwo->id;
|
|
|
|
$this->user->save();
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2023-03-07 15:17:07 +01:00
|
|
|
Groups::assign($this->twofaccountOne->id, $this->user);
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2021-11-30 17:39:33 +01:00
|
|
|
$this->assertDatabaseHas('twofaccounts', [
|
2022-11-22 15:15:52 +01:00
|
|
|
'id' => $this->twofaccountOne->id,
|
2021-11-30 17:39:33 +01:00
|
|
|
'group_id' => $this->groupTwo->id,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2023-03-07 15:17:07 +01:00
|
|
|
public function test_assign_a_twofaccount_to_no_group_assigns_to_user_active_group()
|
2021-11-30 17:39:33 +01:00
|
|
|
{
|
2023-02-27 00:32:49 +01:00
|
|
|
$this->user['preferences->defaultGroup'] = -1;
|
|
|
|
$this->user['preferences->activeGroup'] = $this->groupTwo->id;
|
|
|
|
$this->user->save();
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2023-03-07 15:17:07 +01:00
|
|
|
Groups::assign($this->twofaccountOne->id, $this->user);
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2021-11-30 17:39:33 +01:00
|
|
|
$this->assertDatabaseHas('twofaccounts', [
|
2022-11-22 15:15:52 +01:00
|
|
|
'id' => $this->twofaccountOne->id,
|
2021-11-30 17:39:33 +01:00
|
|
|
'group_id' => $this->groupTwo->id,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2023-03-02 14:32:53 +01:00
|
|
|
public function test_assign_a_twofaccount_to_missing_active_group_returns_not_found()
|
2021-11-30 17:39:33 +01:00
|
|
|
{
|
2023-02-27 00:32:49 +01:00
|
|
|
$orginalGroup = $this->twofaccountOne->group_id;
|
|
|
|
|
|
|
|
$this->user['preferences->defaultGroup'] = -1;
|
|
|
|
$this->user['preferences->activeGroup'] = 1000;
|
|
|
|
$this->user->save();
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2023-03-07 15:17:07 +01:00
|
|
|
Groups::assign($this->twofaccountOne->id, $this->user);
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2021-11-30 17:39:33 +01:00
|
|
|
$this->assertDatabaseHas('twofaccounts', [
|
2022-11-22 15:15:52 +01:00
|
|
|
'id' => $this->twofaccountOne->id,
|
2023-02-27 00:32:49 +01:00
|
|
|
'group_id' => $orginalGroup,
|
2021-11-30 17:39:33 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2023-03-07 15:17:07 +01:00
|
|
|
public function test_user_can_assign_an_account()
|
2023-03-02 14:32:53 +01:00
|
|
|
{
|
|
|
|
$this->expectException(AuthorizationException::class);
|
|
|
|
|
2023-03-07 15:17:07 +01:00
|
|
|
Groups::assign($this->twofaccountThree->id, $this->user, $this->user->groups()->first());
|
2023-03-02 14:32:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2023-03-07 15:17:07 +01:00
|
|
|
public function test_user_can_assign_multiple_accounts()
|
2023-03-02 14:32:53 +01:00
|
|
|
{
|
|
|
|
$this->expectException(AuthorizationException::class);
|
|
|
|
|
2023-03-07 15:17:07 +01:00
|
|
|
Groups::assign([$this->twofaccountOne->id, $this->twofaccountThree->id], $this->user, $this->user->groups()->first());
|
2023-03-02 14:32:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2023-03-07 15:17:07 +01:00
|
|
|
public function test_prependTheAllGroup_add_the_group_on_top_of_groups()
|
2023-02-27 00:32:49 +01:00
|
|
|
{
|
2023-03-07 15:17:07 +01:00
|
|
|
$groups = Groups::prependTheAllGroup($this->user->groups, $this->user);
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2023-03-07 15:17:07 +01:00
|
|
|
$this->assertCount(3, $groups);
|
|
|
|
$this->assertEquals(0, $groups->first()->id);
|
|
|
|
$this->assertEquals(2, $groups->first()->twofaccounts_count);
|
2021-11-30 17:39:33 +01:00
|
|
|
}
|
2022-11-22 15:15:52 +01:00
|
|
|
}
|