forked from fa/breadcrumb-the-shire
109 lines
4.3 KiB
PHP
109 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace MintyPHP\Tests\Service\Auth;
|
|
|
|
use MintyPHP\Http\SessionStore;
|
|
use MintyPHP\Service\Auth\AuthSessionTenantContextService;
|
|
use MintyPHP\Service\Bookmark\BookmarkService;
|
|
use MintyPHP\Service\User\UserTenantContextService;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class AuthSessionTenantContextServiceTest extends TestCase
|
|
{
|
|
protected function setUp(): void
|
|
{
|
|
$_SESSION = [];
|
|
}
|
|
|
|
public function testHydrateMarksNoActiveTenantWhenUserHasNoTenants(): void
|
|
{
|
|
$_SESSION['current_tenant'] = ['id' => 9];
|
|
|
|
$userTenantContextService = $this->createMock(UserTenantContextService::class);
|
|
$userTenantContextService->method('getAvailableTenants')->willReturn([]);
|
|
$userTenantContextService->method('getAvailableDepartmentsByTenant')->willReturn([]);
|
|
$userTenantContextService->expects($this->never())->method('getCurrentTenant');
|
|
|
|
$bookmarkService = $this->createMock(BookmarkService::class);
|
|
$bookmarkService->method('listGroupedForUser')->willReturn(['groups' => [], 'ungrouped' => []]);
|
|
|
|
$service = new AuthSessionTenantContextService(
|
|
$userTenantContextService,
|
|
$bookmarkService,
|
|
new SessionStore()
|
|
);
|
|
|
|
$service->hydrateForUser(5);
|
|
|
|
$this->assertSame([], $_SESSION['available_tenants']);
|
|
$this->assertSame([], $_SESSION['available_departments_by_tenant']);
|
|
$this->assertSame(['groups' => [], 'ungrouped' => []], $_SESSION['user_bookmarks']);
|
|
$this->assertTrue((bool) $_SESSION['no_active_tenant']);
|
|
$this->assertArrayNotHasKey('current_tenant', $_SESSION);
|
|
}
|
|
|
|
public function testHydrateUsesFirstAvailableTenantAsFallback(): void
|
|
{
|
|
$availableTenants = [
|
|
['id' => 7, 'uuid' => 'tenant-7'],
|
|
['id' => 8, 'uuid' => 'tenant-8'],
|
|
];
|
|
$availableDepartments = ['7' => [['id' => 11]]];
|
|
$bookmarks = ['groups' => [['id' => 1, 'name' => 'Dev', 'bookmarks' => []]], 'ungrouped' => []];
|
|
|
|
$userTenantContextService = $this->createMock(UserTenantContextService::class);
|
|
$userTenantContextService->method('getAvailableTenants')->willReturn($availableTenants);
|
|
$userTenantContextService->method('getAvailableDepartmentsByTenant')->willReturn($availableDepartments);
|
|
$userTenantContextService->method('getCurrentTenant')->willReturn(null);
|
|
|
|
$bookmarkService = $this->createMock(BookmarkService::class);
|
|
$bookmarkService->method('listGroupedForUser')->willReturn($bookmarks);
|
|
|
|
$service = new AuthSessionTenantContextService(
|
|
$userTenantContextService,
|
|
$bookmarkService,
|
|
new SessionStore()
|
|
);
|
|
|
|
$service->hydrateForUser(5);
|
|
|
|
$this->assertSame($availableTenants, $_SESSION['available_tenants']);
|
|
$this->assertSame($availableDepartments, $_SESSION['available_departments_by_tenant']);
|
|
$this->assertSame($bookmarks, $_SESSION['user_bookmarks']);
|
|
$this->assertFalse((bool) $_SESSION['no_active_tenant']);
|
|
$this->assertSame(7, (int) ($_SESSION['current_tenant']['id'] ?? 0));
|
|
}
|
|
|
|
public function testHydratePreservesThemeAndPolicyFieldsInSession(): void
|
|
{
|
|
$availableTenants = [
|
|
[
|
|
'id' => 3,
|
|
'uuid' => 'tenant-3',
|
|
'default_theme' => 'dark',
|
|
'allow_user_theme' => '0',
|
|
],
|
|
];
|
|
|
|
$userTenantContextService = $this->createMock(UserTenantContextService::class);
|
|
$userTenantContextService->method('getAvailableTenants')->willReturn($availableTenants);
|
|
$userTenantContextService->method('getAvailableDepartmentsByTenant')->willReturn([]);
|
|
$userTenantContextService->method('getCurrentTenant')->willReturn($availableTenants[0]);
|
|
|
|
$bookmarkService = $this->createMock(BookmarkService::class);
|
|
$bookmarkService->method('listGroupedForUser')->willReturn(['groups' => [], 'ungrouped' => []]);
|
|
|
|
$service = new AuthSessionTenantContextService(
|
|
$userTenantContextService,
|
|
$bookmarkService,
|
|
new SessionStore()
|
|
);
|
|
|
|
$service->hydrateForUser(10);
|
|
|
|
$tenant = $_SESSION['current_tenant'] ?? [];
|
|
$this->assertSame('dark', $tenant['default_theme'] ?? null);
|
|
$this->assertSame('0', $tenant['allow_user_theme'] ?? null);
|
|
}
|
|
}
|