97 lines
3.3 KiB
PHP
97 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace Tests;
|
|
|
|
use App\Actions\Permissions\PermissionService;
|
|
use App\Enums\Permission;
|
|
use App\Models\Project;
|
|
use App\Models\Team;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
|
|
use Laravel\Fortify\Features;
|
|
|
|
abstract class TestCase extends BaseTestCase
|
|
{
|
|
protected function skipUnlessFortifyFeature(string $feature, ?string $message = null): void
|
|
{
|
|
if (! Features::enabled($feature)) {
|
|
$this->markTestSkipped($message ?? "Fortify feature [{$feature}] is not enabled.");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param array<int, Permission|string> $permissions
|
|
*/
|
|
protected function syncGlobalPermissions(User $user, array $permissions): void
|
|
{
|
|
app(PermissionService::class)->sync(
|
|
$user,
|
|
null,
|
|
$this->normalizePermissions($permissions, 'global'),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param array<int, Permission|string> $permissions
|
|
*/
|
|
protected function syncProjectPermissions(Project $project, User|Team $assignee, array $permissions): void
|
|
{
|
|
app(PermissionService::class)->sync(
|
|
$assignee,
|
|
$project,
|
|
$this->normalizePermissions($permissions, 'project'),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param array<int, Permission|string> $permissions
|
|
*/
|
|
protected function syncTeamPermissions(Team $team, User $user, array $permissions): void
|
|
{
|
|
app(PermissionService::class)->sync(
|
|
$user,
|
|
$team,
|
|
$this->normalizePermissions($permissions, 'team'),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param array<int, Permission|string> $permissions
|
|
* @return array<int, string>
|
|
*/
|
|
private function normalizePermissions(array $permissions, string $scope): array
|
|
{
|
|
return collect($permissions)
|
|
->map(function (Permission|string $permission) use ($scope): string {
|
|
if ($permission instanceof Permission) {
|
|
return $permission->value;
|
|
}
|
|
|
|
return match ($scope) {
|
|
'global' => match ($permission) {
|
|
'admin', 'global:admin' => Permission::GlobalAdmin->value,
|
|
'create', 'project:create' => Permission::ProjectCreate->value,
|
|
default => $permission,
|
|
},
|
|
'project' => match ($permission) {
|
|
'admin', 'delete', 'project:admin' => Permission::ProjectAdmin->value,
|
|
'edit' => Permission::ProjectAdmin->value,
|
|
'create', 'project:write', 'write' => Permission::ProjectWrite->value,
|
|
'project:read', 'read', 'view' => Permission::ProjectRead->value,
|
|
default => $permission,
|
|
},
|
|
'team' => match ($permission) {
|
|
'admin', 'team:admin' => Permission::TeamAdmin->value,
|
|
'invite', 'team:invite' => Permission::TeamInvite->value,
|
|
'manage', 'team:manage' => Permission::TeamManage->value,
|
|
default => $permission,
|
|
},
|
|
default => $permission,
|
|
};
|
|
})
|
|
->unique()
|
|
->values()
|
|
->all();
|
|
}
|
|
}
|