v1.0.0
This commit is contained in:
113
src/tests/Feature/AdminTeamManagementTest.php
Normal file
113
src/tests/Feature/AdminTeamManagementTest.php
Normal file
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
use App\Livewire\Admin\Panel;
|
||||
use App\Models\Category;
|
||||
use App\Models\Subcategory;
|
||||
use App\Models\Team;
|
||||
use App\Models\User;
|
||||
use Livewire\Livewire;
|
||||
|
||||
function adminUser(): User
|
||||
{
|
||||
return User::query()->create(['name' => 'Admin', 'email' => 'admin@example.com', 'roles' => ['admin']]);
|
||||
}
|
||||
|
||||
function operatorUser(string $email = 'op1@example.com'): User
|
||||
{
|
||||
return User::query()->create(['name' => 'Operator '.$email, 'email' => $email, 'roles' => ['operator']]);
|
||||
}
|
||||
|
||||
function subcategoryFixture(): Subcategory
|
||||
{
|
||||
$category = Category::query()->create(['name' => 'IT']);
|
||||
|
||||
return $category->subcategories()->create(['name' => 'VPN']);
|
||||
}
|
||||
|
||||
test('admin can create a team with members and subcategories via the multiselect', function () {
|
||||
$admin = adminUser();
|
||||
$op1 = operatorUser('op1@example.com');
|
||||
$op2 = operatorUser('op2@example.com');
|
||||
$sub = subcategoryFixture();
|
||||
|
||||
Livewire::actingAs($admin)->test(Panel::class)
|
||||
->call('setTab', 'teams')
|
||||
->call('openTeamForm')
|
||||
->set('teamForm.name', 'Infrastruktura')
|
||||
->call('toggleTeamMember', $op1->id)
|
||||
->call('toggleTeamMember', $op2->id)
|
||||
->call('toggleTeamSubcategory', $sub->id)
|
||||
->call('submitTeam')
|
||||
->assertOk();
|
||||
|
||||
$team = Team::query()->where('name', 'Infrastruktura')->firstOrFail();
|
||||
|
||||
expect($team->members->pluck('id')->sort()->values()->all())->toBe([$op1->id, $op2->id])
|
||||
->and($team->subcategories()->pluck('subcategories.id')->all())->toBe([$sub->id]);
|
||||
});
|
||||
|
||||
test('admin can edit a team, swap out a member and change its subcategories', function () {
|
||||
$admin = adminUser();
|
||||
$op1 = operatorUser('op1@example.com');
|
||||
$op2 = operatorUser('op2@example.com');
|
||||
$subA = subcategoryFixture();
|
||||
$subB = Category::query()->create(['name' => 'Admin'])->subcategories()->create(['name' => 'Wnioski']);
|
||||
|
||||
$team = Team::query()->create(['name' => 'Zespół A']);
|
||||
$team->subcategories()->attach($subA->id);
|
||||
$op1->teams()->attach($team->id);
|
||||
|
||||
Livewire::actingAs($admin)->test(Panel::class)
|
||||
->call('editTeam', $team->id)
|
||||
->assertSet('teamForm.name', 'Zespół A')
|
||||
->assertSet('teamForm.memberIds', [$op1->id])
|
||||
->set('teamForm.name', 'Zespół A zmieniony')
|
||||
->call('toggleTeamMember', $op1->id) // remove op1
|
||||
->call('toggleTeamMember', $op2->id) // add op2
|
||||
->call('toggleTeamSubcategory', $subA->id) // remove subA
|
||||
->call('toggleTeamSubcategory', $subB->id) // add subB
|
||||
->call('submitTeam')
|
||||
->assertOk();
|
||||
|
||||
$team->refresh();
|
||||
|
||||
expect($team->name)->toBe('Zespół A zmieniony')
|
||||
->and($op1->fresh()->teams->pluck('id')->all())->toBe([])
|
||||
->and($op2->fresh()->teams->pluck('id')->all())->toBe([$team->id])
|
||||
->and($team->subcategories()->pluck('subcategories.id')->all())->toBe([$subB->id]);
|
||||
});
|
||||
|
||||
test('admin can delete a team, which unassigns its members without touching their roles', function () {
|
||||
$admin = adminUser();
|
||||
$op1 = operatorUser('op1@example.com');
|
||||
|
||||
$team = Team::query()->create(['name' => 'Do usunięcia']);
|
||||
$op1->teams()->attach($team->id);
|
||||
|
||||
Livewire::actingAs($admin)->test(Panel::class)
|
||||
->call('removeTeam', $team->id)
|
||||
->assertSet('pendingDeleteType', 'team')
|
||||
->call('confirmPendingDelete')
|
||||
->assertOk();
|
||||
|
||||
expect(Team::query()->find($team->id))->toBeNull()
|
||||
->and($op1->fresh()->teams)->toBeEmpty()
|
||||
->and($op1->fresh()->roles)->toBe(['operator']);
|
||||
});
|
||||
|
||||
test('a user can belong to more than one team at once, and the users list shows all of them', function () {
|
||||
$admin = adminUser();
|
||||
$op1 = operatorUser('op1@example.com');
|
||||
|
||||
$teamA = Team::query()->create(['name' => 'Infrastruktura']);
|
||||
$teamB = Team::query()->create(['name' => 'Aplikacje']);
|
||||
|
||||
$op1->teams()->attach([$teamA->id, $teamB->id]);
|
||||
|
||||
expect($op1->fresh()->teams->pluck('name')->sort()->values()->all())->toBe(['Aplikacje', 'Infrastruktura']);
|
||||
|
||||
Livewire::actingAs($admin)->test(Panel::class)
|
||||
->call('setTab', 'users')
|
||||
->assertSee('Infrastruktura')
|
||||
->assertSee('Aplikacje');
|
||||
});
|
||||
Reference in New Issue
Block a user