Strengthen settings gateway tests
This commit is contained in:
@@ -24,6 +24,24 @@ class SettingsApiPolicyGatewayTest extends TestCase
|
||||
$this->assertFalse($gateway->setApiTokenDefaultTtlDays(31));
|
||||
}
|
||||
|
||||
public function testSetApiTokenMaxTtlDaysReturnsFalseWhenDefaultClampPersistFails(): void
|
||||
{
|
||||
$settings = $this->createMock(SettingRepositoryInterface::class);
|
||||
$settings->method('getValue')->willReturnCallback(static function (string $key): ?string {
|
||||
if ($key === 'api_token_default_ttl_days') {
|
||||
return '365';
|
||||
}
|
||||
return null;
|
||||
});
|
||||
$settings->expects($this->once())
|
||||
->method('set')
|
||||
->with('api_token_default_ttl_days', '120', 'setting.api_token_default_ttl_days')
|
||||
->willReturn(false);
|
||||
|
||||
$gateway = new SettingsApiPolicyGateway(new SettingsMetadataGateway($settings));
|
||||
$this->assertFalse($gateway->setApiTokenMaxTtlDays(120));
|
||||
}
|
||||
|
||||
public function testSetApiTokenMaxTtlDaysUpdatesDefaultWhenNeeded(): void
|
||||
{
|
||||
$settings = $this->createMock(SettingRepositoryInterface::class);
|
||||
@@ -86,4 +104,16 @@ class SettingsApiPolicyGatewayTest extends TestCase
|
||||
$this->assertSame(['https://a.example', 'https://b.example'], $gateway->getApiCorsAllowedOrigins());
|
||||
$this->assertSame("https://a.example\nhttps://b.example", $gateway->getApiCorsAllowedOriginsText());
|
||||
}
|
||||
|
||||
public function testSetApiCorsAllowedOriginsClearsSettingForEmptyInput(): void
|
||||
{
|
||||
$settings = $this->createMock(SettingRepositoryInterface::class);
|
||||
$settings->expects($this->once())
|
||||
->method('set')
|
||||
->with('api_cors_allowed_origins', null, 'setting.api_cors_allowed_origins')
|
||||
->willReturn(true);
|
||||
|
||||
$gateway = new SettingsApiPolicyGateway(new SettingsMetadataGateway($settings));
|
||||
$this->assertTrue($gateway->setApiCorsAllowedOrigins(" \n "));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,44 +8,29 @@ use MintyPHP\Repository\Settings\SettingRepositoryInterface;
|
||||
use MintyPHP\Repository\Tenant\TenantRepositoryInterface;
|
||||
use MintyPHP\Service\Settings\SettingsDefaultsGateway;
|
||||
use MintyPHP\Service\Settings\SettingsMetadataGateway;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class SettingsDefaultsGatewayTest extends TestCase
|
||||
{
|
||||
public function testSetDefaultTenantIdRejectsUnknownTenant(): void
|
||||
#[DataProvider('unknownReferenceProvider')]
|
||||
public function testSettersRejectUnknownReferences(string $setter, int $id): void
|
||||
{
|
||||
$settings = $this->createMock(SettingRepositoryInterface::class);
|
||||
$settings->expects($this->never())->method('set');
|
||||
|
||||
$tenantRepository = $this->createMock(TenantRepositoryInterface::class);
|
||||
$tenantRepository->expects($this->once())->method('find')->with(99)->willReturn(null);
|
||||
|
||||
$gateway = $this->newGateway($settings, $tenantRepository);
|
||||
$this->assertFalse($gateway->setDefaultTenantId(99));
|
||||
}
|
||||
|
||||
public function testSetDefaultRoleIdRejectsUnknownRole(): void
|
||||
{
|
||||
$settings = $this->createMock(SettingRepositoryInterface::class);
|
||||
$settings->expects($this->never())->method('set');
|
||||
|
||||
$roleRepository = $this->createMock(RoleRepositoryInterface::class);
|
||||
$roleRepository->expects($this->once())->method('find')->with(55)->willReturn(null);
|
||||
|
||||
$gateway = $this->newGateway($settings, null, $roleRepository);
|
||||
$this->assertFalse($gateway->setDefaultRoleId(55));
|
||||
}
|
||||
|
||||
public function testSetDefaultDepartmentIdRejectsUnknownDepartment(): void
|
||||
{
|
||||
$settings = $this->createMock(SettingRepositoryInterface::class);
|
||||
$settings->expects($this->never())->method('set');
|
||||
|
||||
$departmentRepository = $this->createMock(DepartmentRepositoryInterface::class);
|
||||
$departmentRepository->expects($this->once())->method('find')->with(77)->willReturn(null);
|
||||
$gateway = $this->newGateway($settings, $tenantRepository, $roleRepository, $departmentRepository);
|
||||
|
||||
$gateway = $this->newGateway($settings, null, null, $departmentRepository);
|
||||
$this->assertFalse($gateway->setDefaultDepartmentId(77));
|
||||
match ($setter) {
|
||||
'setDefaultTenantId' => $tenantRepository->expects($this->once())->method('find')->with($id)->willReturn(null),
|
||||
'setDefaultRoleId' => $roleRepository->expects($this->once())->method('find')->with($id)->willReturn(null),
|
||||
'setDefaultDepartmentId' => $departmentRepository->expects($this->once())->method('find')->with($id)->willReturn(null),
|
||||
};
|
||||
|
||||
$this->assertFalse($gateway->$setter($id));
|
||||
}
|
||||
|
||||
public function testSettersPersistValidIds(): void
|
||||
@@ -81,6 +66,23 @@ class SettingsDefaultsGatewayTest extends TestCase
|
||||
$this->assertTrue($gateway->setDefaultDepartmentId(null));
|
||||
}
|
||||
|
||||
#[DataProvider('getterNormalizationProvider')]
|
||||
public function testGettersNormalizeNonPositiveValuesToNull(
|
||||
string $storedKey,
|
||||
string $getter,
|
||||
?string $storedValue,
|
||||
?int $expected
|
||||
): void {
|
||||
$settings = $this->createMock(SettingRepositoryInterface::class);
|
||||
$settings->method('getValue')->willReturnCallback(
|
||||
static fn (string $key): ?string => $key === $storedKey ? $storedValue : null
|
||||
);
|
||||
|
||||
$gateway = $this->newGateway($settings);
|
||||
|
||||
$this->assertSame($expected, $gateway->$getter());
|
||||
}
|
||||
|
||||
private function newGateway(
|
||||
SettingRepositoryInterface $settingRepository,
|
||||
?TenantRepositoryInterface $tenantRepository = null,
|
||||
@@ -98,4 +100,23 @@ class SettingsDefaultsGatewayTest extends TestCase
|
||||
$departmentRepository
|
||||
);
|
||||
}
|
||||
|
||||
public static function unknownReferenceProvider(): array
|
||||
{
|
||||
return [
|
||||
'unknown tenant' => ['setDefaultTenantId', 99],
|
||||
'unknown role' => ['setDefaultRoleId', 55],
|
||||
'unknown department' => ['setDefaultDepartmentId', 77],
|
||||
];
|
||||
}
|
||||
|
||||
public static function getterNormalizationProvider(): array
|
||||
{
|
||||
return [
|
||||
'default tenant positive id' => ['default_tenant_id', 'getDefaultTenantId', '5', 5],
|
||||
'default tenant zero becomes null' => ['default_tenant_id', 'getDefaultTenantId', '0', null],
|
||||
'default role negative becomes null' => ['default_role_id', 'getDefaultRoleId', '-1', null],
|
||||
'default department missing stays null' => ['default_department_id', 'getDefaultDepartmentId', null, null],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,4 +50,46 @@ class SettingsMicrosoftGatewayTest extends TestCase
|
||||
$gateway = new SettingsMicrosoftGateway(new SettingsMetadataGateway($settings), $crypto);
|
||||
$this->assertTrue($gateway->setMicrosoftSharedClientSecret('secret-value'));
|
||||
}
|
||||
|
||||
public function testSetMicrosoftSharedClientSecretReturnsFalseWhenEncryptionFails(): void
|
||||
{
|
||||
$settings = $this->createMock(SettingRepositoryInterface::class);
|
||||
$settings->expects($this->never())->method('set');
|
||||
|
||||
$crypto = $this->createMock(SettingsCryptoGatewayInterface::class);
|
||||
$crypto->expects($this->once())
|
||||
->method('encryptString')
|
||||
->with('secret-value')
|
||||
->willThrowException(new \RuntimeException('encryption failed'));
|
||||
|
||||
$gateway = new SettingsMicrosoftGateway(new SettingsMetadataGateway($settings), $crypto);
|
||||
$this->assertFalse($gateway->setMicrosoftSharedClientSecret('secret-value'));
|
||||
}
|
||||
|
||||
public function testSetMicrosoftSharedClientSecretClearsSettingWhenEmpty(): void
|
||||
{
|
||||
$settings = $this->createMock(SettingRepositoryInterface::class);
|
||||
$settings->expects($this->once())
|
||||
->method('set')
|
||||
->with('microsoft_shared_client_secret_enc', null, 'setting.microsoft_shared_client_secret_enc')
|
||||
->willReturn(true);
|
||||
|
||||
$crypto = $this->createMock(SettingsCryptoGatewayInterface::class);
|
||||
$crypto->expects($this->never())->method('encryptString');
|
||||
|
||||
$gateway = new SettingsMicrosoftGateway(new SettingsMetadataGateway($settings), $crypto);
|
||||
$this->assertTrue($gateway->setMicrosoftSharedClientSecret(' '));
|
||||
}
|
||||
|
||||
public function testGetMicrosoftAuthorityFallsBackAndTrimsTrailingSlash(): void
|
||||
{
|
||||
$settings = $this->createMock(SettingRepositoryInterface::class);
|
||||
$settings->method('getValue')->willReturnOnConsecutiveCalls(null, 'https://login.microsoftonline.com/common/v2.0/');
|
||||
|
||||
$crypto = $this->createMock(SettingsCryptoGatewayInterface::class);
|
||||
$gateway = new SettingsMicrosoftGateway(new SettingsMetadataGateway($settings), $crypto);
|
||||
|
||||
$this->assertSame('https://login.microsoftonline.com/common/v2.0', $gateway->getMicrosoftAuthority());
|
||||
$this->assertSame('https://login.microsoftonline.com/common/v2.0', $gateway->getMicrosoftAuthority());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,18 @@ class SettingsUserLifecycleGatewayTest extends TestCase
|
||||
$this->assertTrue($gateway->setUserInactivityDeactivateDays(0));
|
||||
}
|
||||
|
||||
public function testSetUserInactivityDeactivateDaysReturnsFalseWhenDeleteResetFails(): void
|
||||
{
|
||||
$settings = $this->createMock(SettingRepositoryInterface::class);
|
||||
$settings->expects($this->once())
|
||||
->method('set')
|
||||
->with('user_inactivity_delete_days', '0', 'setting.user_inactivity_delete_days')
|
||||
->willReturn(false);
|
||||
|
||||
$gateway = new SettingsUserLifecycleGateway(new SettingsMetadataGateway($settings));
|
||||
$this->assertFalse($gateway->setUserInactivityDeactivateDays(0));
|
||||
}
|
||||
|
||||
public function testGetUserInactivityDeleteDaysReturnsZeroWhenDeactivateDisabled(): void
|
||||
{
|
||||
$settings = $this->createMock(SettingRepositoryInterface::class);
|
||||
@@ -49,4 +61,30 @@ class SettingsUserLifecycleGatewayTest extends TestCase
|
||||
$gateway = new SettingsUserLifecycleGateway(new SettingsMetadataGateway($settings));
|
||||
$this->assertSame(0, $gateway->getUserInactivityDeleteDays());
|
||||
}
|
||||
|
||||
public function testGetUserInactivityDeleteDaysFallsBackWhenStoredValueIsInvalid(): void
|
||||
{
|
||||
$settings = $this->createMock(SettingRepositoryInterface::class);
|
||||
$settings->method('getValue')->willReturnCallback(static function (string $key): ?string {
|
||||
if ($key === 'user_inactivity_deactivate_days') {
|
||||
return '180';
|
||||
}
|
||||
if ($key === 'user_inactivity_delete_days') {
|
||||
return '99999';
|
||||
}
|
||||
return null;
|
||||
});
|
||||
|
||||
$gateway = new SettingsUserLifecycleGateway(new SettingsMetadataGateway($settings));
|
||||
$this->assertSame(365, $gateway->getUserInactivityDeleteDays());
|
||||
}
|
||||
|
||||
public function testSetUserInactivityDeleteDaysRejectsOutOfRangeValue(): void
|
||||
{
|
||||
$settings = $this->createMock(SettingRepositoryInterface::class);
|
||||
$settings->expects($this->never())->method('set');
|
||||
|
||||
$gateway = new SettingsUserLifecycleGateway(new SettingsMetadataGateway($settings));
|
||||
$this->assertFalse($gateway->setUserInactivityDeleteDays(3651));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user