Compare commits

...

2 Commits

Author SHA1 Message Date
jf-cbd
9befe56c83 Refactor test 2026-11-13 17:12:39 +01:00
jf-cbd
e705608274 initial commit 2026-11-13 16:58:43 +01:00
5 changed files with 115 additions and 33 deletions

View File

@@ -20,6 +20,8 @@
*
*/
use Symfony\Component\Yaml\Parser;
define('ITOP_APPLICATION', 'iTop');
define('ITOP_APPLICATION_SHORT', 'iTop');
@@ -2129,8 +2131,15 @@ class Config
* @throws \ConfigException
* @throws \CoreException
*/
protected function Load($sConfigFile)
protected function Load($sConfigFile, $sSysAdminConfFile = null)
{
$sSysAdminConfFile = $sSysAdminConfFile ?? APPCONF.'production/conf.yml';
$sSysAdminConfFileContent = trim(file_get_contents($sSysAdminConfFile));
$oYmlLoader = new Parser();
$aSysAdminConfValues = $oYmlLoader->parse($sSysAdminConfFileContent);
$aMySysAdminSettings = $aSysAdminConfValues['settings'] ?? [];
$aMySysAdminModuleSettings = $aSysAdminConfValues['module_settings'] ?? [];
$this->CheckFile('configuration', $sConfigFile);
$sConfigCode = trim(file_get_contents($sConfigFile));
@@ -2193,6 +2202,9 @@ class Config
$this->m_aAddons = $MyModules['addons'];
$MySettings = array_replace_recursive($MySettings, $aMySysAdminSettings);
$MyModuleSettings = array_replace_recursive($MyModuleSettings ?? [], $aMySysAdminModuleSettings);
foreach ($MySettings as $sPropCode => $rawvalue) {
if ($this->IsProperty($sPropCode)) {
if (is_string($rawvalue)) {

View File

@@ -1,24 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.5/phpunit.xsd"
bootstrap="unittestautoload.php"
backupGlobals="true"
colors="true"
columns="120"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnRisky="false"
stopOnSkipped="false"
verbose="true"
printerClass="\Sempro\PHPUnitPrettyPrinter\PrettyPrinterForPhpUnit9"
>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" bootstrap="unittestautoload.php" backupGlobals="true" colors="true" columns="120" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnError="false" stopOnFailure="false" stopOnIncomplete="false" stopOnRisky="false" stopOnSkipped="false" verbose="true" printerClass="\Sempro\PHPUnitPrettyPrinter\PrettyPrinterForPhpUnit9">
<coverage>
<include>
<file>../../core/apc-emulation.php</file>
<file>../../core/ormlinkset.class.inc.php</file>
<file>../../datamodels/2.x/itop-tickets/main.itop-tickets.php</file>
</include>
</coverage>
<php>
<ini name="memory_limit" value="512M"/>
<ini name="error_reporting" value="E_ALL"/>
@@ -27,7 +15,6 @@
<ini name="html_errors" value="Off"/>
<env name="PHPUNIT_PRETTY_PRINT_PROGRESS" value="true"/>
</php>
<testsuites>
<!-- Unitary tests -->
<testsuite name="Application">
@@ -51,25 +38,14 @@
<testsuite name="Webservices">
<directory>unitary-tests/webservices</directory>
</testsuite>
<testsuite name="Extensions">
<directory>../../env-production/*/test</directory>
<directory>../../env-production/*/tests/php-unit-tests</directory>
</testsuite>
<!-- Integration tests -->
<testsuite name="Integration">
<directory>integration-tests</directory>
</testsuite>
</testsuites>
<!-- Code coverage white list -->
<filter>
<whitelist>
<file>../../core/apc-emulation.php</file>
<file>../../core/ormlinkset.class.inc.php</file>
<file>../../datamodels/2.x/itop-tickets/main.itop-tickets.php</file>
</whitelist>
</filter>
</phpunit>

View File

@@ -0,0 +1,17 @@
# Fake system administrator configuration, used by iTopConfigParserTest::testSysAdminConfigComputation()
settings:
db_pwd: 'sys-admin-db-password'
email_transport_smtp.password: 'sys-admin-smtp-password'
encryption_key: 'sys-admin-encryption-key'
log_level_min:
DeadLock: 'Error'
Security: 'Warning'
module_settings:
authent-ldap:
default_pwd: 'sys-admin-ldap-password'
test-module:
options:
override: 'sys-admin-value'
added: 'sys-admin-value'
sys-admin-only-module:
api_key: 'sys-admin-api-key'

View File

@@ -0,0 +1,35 @@
<?php
/**
* Fake iTop administrator configuration, used by iTopConfigParserTest::testSysAdminConfigComputation()
*/
$MySettings = array(
'db_host' => 'itop-admin-db-host',
'db_pwd' => 'itop-admin-db-password',
'app_root_url' => 'http://itop-admin.example.org/',
'encryption_key' => 'itop-admin-encryption-key',
'log_level_min' => array(
'' => 'Error',
'DeadLock' => 'Debug',
),
);
$MyModuleSettings = array(
'authent-ldap' => array(
'host' => 'itop-admin-ldap-host',
'default_pwd' => 'itop-admin-ldap-password',
),
'test-module' => array(
'options' => array(
'keep' => 'itop-admin-value',
'override' => 'itop-admin-value',
),
),
);
$MyModules = array(
'addons' => array(
'user rights' => 'addons/userrights/userrightsprofile.class.inc.php',
),
);

View File

@@ -219,4 +219,46 @@ CONF;
$oTestEnv->WriteConfigFileSafe($oConfig);
$this->assertTrue(true, "Config file was written");
}
public function testSysAdminConfigComputation()
{
$sFixturesDir = __DIR__.'/Fixtures/SysAdminConfig';
$sTestConfDir = APPCONF.'php-unit-tests';
// conf/ is not versioned: build the configuration files from the fixtures, they will be removed by tearDown()
if (!is_dir($sTestConfDir)) {
mkdir($sTestConfDir, 0770, true);
}
$sItopConfFile = $sTestConfDir.'/config-itop.itopadmin-test.php';
$sSysAdminConfFile = $sTestConfDir.'/conf.sysadmin-test.yml';
$this->aFileToClean[] = $sItopConfFile;
$this->aFileToClean[] = $sSysAdminConfFile;
copy($sFixturesDir.'/config-itop.php', $sItopConfFile);
copy($sFixturesDir.'/conf.yml', $sSysAdminConfFile);
$oConfig = new Config($sItopConfFile, false);
$this->InvokeNonPublicMethod(Config::class, 'Load', $oConfig, [$sItopConfFile, $sSysAdminConfFile]);
// Settings
$this->assertSame('itop-admin-db-host', $oConfig->Get('db_host'), 'iTop admin only setting must be kept');
$this->assertSame('http://itop-admin.example.org/', $oConfig->Get('app_root_url'), 'iTop admin only setting must be kept');
$this->assertSame('sys-admin-db-password', $oConfig->Get('db_pwd'), 'System admin setting must override iTop admin setting');
$this->assertSame('sys-admin-smtp-password', $oConfig->Get('email_transport_smtp.password'), 'System admin only setting must be added');
$this->assertSame('sys-admin-encryption-key', $oConfig->GetEncryptionKey(), 'Settings stored in dedicated members must use the system admin value');
$this->assertEquals(
['' => 'Error', 'DeadLock' => 'Error', 'Security' => 'Warning'],
$oConfig->Get('log_level_min'),
'Array settings must be merged recursively, with system admin values taking precedence'
);
// Module settings
$this->assertSame('itop-admin-ldap-host', $oConfig->GetModuleSetting('authent-ldap', 'host'), 'iTop admin only module setting must be kept');
$this->assertSame('sys-admin-ldap-password', $oConfig->GetModuleSetting('authent-ldap', 'default_pwd'), 'System admin module setting must override iTop admin module setting');
$this->assertEquals(
['keep' => 'itop-admin-value', 'override' => 'sys-admin-value', 'added' => 'sys-admin-value'],
$oConfig->GetModuleSetting('test-module', 'options'),
'Module settings must be merged recursively, with system admin values taking precedence'
);
$this->assertSame('sys-admin-api-key', $oConfig->GetModuleSetting('sys-admin-only-module', 'api_key'), 'System admin only module must be added');
}
}