mirror of
https://github.com/Combodo/iTop.git
synced 2026-11-30 10:38:55 +01:00
Compare commits
2 Commits
feature/10
...
support/3.
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d28ac820f1 | ||
|
|
f0206cf924 |
@@ -35,8 +35,6 @@ class LoginDefaultBefore extends AbstractLoginFSMExtension
|
||||
if ($index !== false) {
|
||||
// Force login mode
|
||||
Session::Set('login_mode', $sProposedLoginMode);
|
||||
} else {
|
||||
Session::Unset('login_mode');
|
||||
}
|
||||
return LoginWebPage::LOGIN_FSM_CONTINUE;
|
||||
}
|
||||
|
||||
@@ -2615,9 +2615,11 @@ class Config
|
||||
* Write the configuration to a file (php format) that can be reloaded later
|
||||
* By default write to the same file that was specified when constructing the object
|
||||
*
|
||||
* @since 3.2.3-2 3.2.4 3.3.0 N°9728
|
||||
*
|
||||
* @param string $sFileName string Name of the file to write to (emtpy to write to the same file)
|
||||
*
|
||||
* @return boolean True otherwise throws an Exception
|
||||
* @return bool True otherwise throws an Exception
|
||||
*
|
||||
* @throws \ConfigException
|
||||
*/
|
||||
@@ -2733,7 +2735,7 @@ class Config
|
||||
$aParserValue = null;
|
||||
}
|
||||
$sSeenAs = self::PrettyVarExport($aParserValue, $aSettingInfo['value'], "\t");
|
||||
fwrite($hFile, "\t'$sPropCode' => $sSeenAs,\n");
|
||||
fwrite($hFile, "\t".var_export((string) $sPropCode, true)." => $sSeenAs,\n");
|
||||
}
|
||||
}
|
||||
fwrite($hFile, ");\n");
|
||||
@@ -2742,10 +2744,10 @@ class Config
|
||||
fwrite($hFile, "/**\n *\n * Modules specific settings\n *\n */\n");
|
||||
fwrite($hFile, "\$MyModuleSettings = array(\n");
|
||||
foreach ($this->m_aModuleSettings as $sModule => $aProperties) {
|
||||
fwrite($hFile, "\t'$sModule' => array (\n");
|
||||
fwrite($hFile, "\t".var_export((string) $sModule, true)." => array (\n");
|
||||
foreach ($aProperties as $sProperty => $value) {
|
||||
$sNiceExport = self::PrettyVarExport($this->oItopConfigParser->GetVarValue('MyModuleSettings', $sProperty), $value, "\t\t");
|
||||
fwrite($hFile, "\t\t'$sProperty' => $sNiceExport,\n");
|
||||
fwrite($hFile, "\t\t".var_export((string) $sProperty, true)." => $sNiceExport,\n");
|
||||
}
|
||||
fwrite($hFile, "\t),\n");
|
||||
}
|
||||
@@ -2763,7 +2765,7 @@ class Config
|
||||
} else {
|
||||
fwrite($hFile, "\t'addons' => array (\n");
|
||||
foreach ($this->m_aAddons as $sKey => $sFile) {
|
||||
fwrite($hFile, "\t\t'$sKey' => '$sFile',\n");
|
||||
fwrite($hFile, "\t\t".var_export((string) $sKey, true).' => '.var_export($sFile, true).",\n");
|
||||
}
|
||||
fwrite($hFile, "\t),\n");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
use Combodo\iTop\Application\Helper\Session;
|
||||
use Combodo\iTop\Test\UnitTest\ItopDataTestCase;
|
||||
|
||||
/**
|
||||
* @runClassInSeparateProcess Required because PHPUnit outputs something earlier, thus causing the headers to be sent
|
||||
*/
|
||||
class LoginDefaultBeforeTest extends ItopDataTestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
Session::$bAllowCLI = true;
|
||||
Session::Start();
|
||||
$_REQUEST = [];
|
||||
$this->RequireOnceItopFile('application/logindefault.class.inc.php');
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
parent::tearDown();
|
||||
Session::$bAllowCLI = false;
|
||||
}
|
||||
|
||||
public function testOnStart_NoLoginModeRequiredNorStoredInSession()
|
||||
{
|
||||
$this->CallStartAndCheckLoginModeStored(null, null);
|
||||
}
|
||||
|
||||
public function testOnStart_StoreLoginModeRequiredWhenConfigured()
|
||||
{
|
||||
MetaModel::GetConfig()->SetAllowedLoginTypes(["SAML"]);
|
||||
$_REQUEST['login_mode'] = "SAML";
|
||||
|
||||
$this->CallStartAndCheckLoginModeStored("SAML", null);
|
||||
}
|
||||
|
||||
public function testOnStart_SwitchLoginModeWhenNewOneRequiredIsConfigured()
|
||||
{
|
||||
MetaModel::GetConfig()->SetAllowedLoginTypes(["SAML"]);
|
||||
$_REQUEST['login_mode'] = "SAML";
|
||||
|
||||
$this->CallStartAndCheckLoginModeStored("SAML");
|
||||
}
|
||||
|
||||
public function testOnStart_PreserveCurrentLoginModeInSessionWhenNewOneRequiredIsNotConfigured()
|
||||
{
|
||||
$_REQUEST['login_mode'] = "SAML";
|
||||
|
||||
$this->CallStartAndCheckLoginModeStored("previous_mode");
|
||||
}
|
||||
|
||||
public function testOnStart_PreserveCurrentLoginModeInSessionWhenNoOtherRequired()
|
||||
{
|
||||
$this->CallStartAndCheckLoginModeStored('previous_mode');
|
||||
}
|
||||
|
||||
private function CallStartAndCheckLoginModeStored($expected, ?string $sPreviousLoginMode = 'previous_mode')
|
||||
{
|
||||
Session::Set('login_mode', $sPreviousLoginMode);
|
||||
|
||||
$iErrorCode = 666;
|
||||
$res = $this->InvokeNonPublicMethod(LoginDefaultBefore::class, 'OnStart', new LoginDefaultBefore(), [&$iErrorCode]);
|
||||
$this->assertEquals(LoginWebPage::EXIT_CODE_OK, $iErrorCode);
|
||||
$this->assertEquals(LoginWebPage::LOGIN_FSM_CONTINUE, $res);
|
||||
$this->assertEquals($expected, Session::Get('login_mode'));
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,10 @@ namespace Combodo\iTop\Test\UnitTest\Module\iTopConfig;
|
||||
|
||||
use Combodo\iTop\Test\UnitTest\ItopTestCase;
|
||||
use Config;
|
||||
use PhpParser\ConstExprEvaluator;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\ParserFactory;
|
||||
use utils;
|
||||
|
||||
class ConfigTest extends ItopTestCase
|
||||
{
|
||||
@@ -91,4 +95,140 @@ class ConfigTest extends ItopTestCase
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 3.2.3
|
||||
* @dataProvider ProviderArrayKeys
|
||||
*/
|
||||
public function testWriteToFilePreservesModuleKeys(int|string $key): void
|
||||
{
|
||||
$oConfig = new Config();
|
||||
$oConfig->SetModuleSetting('existing-module', 'existing.property', true);
|
||||
$oConfig->SetModuleSetting($key, 'sample.property', 'value');
|
||||
|
||||
$this->AssertWriteToFilePreservesSettings($oConfig, [
|
||||
'existing-module' => ['existing.property' => true],
|
||||
$key => ['sample.property' => 'value'],
|
||||
], $oConfig->GetAddons());
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 3.2.3
|
||||
* @dataProvider ProviderArrayKeys
|
||||
*/
|
||||
public function testWriteToFilePreservesPropertyKeys(int|string $key): void
|
||||
{
|
||||
$oConfig = new Config();
|
||||
$oConfig->SetModuleSetting('sample-module', 'existing.property', true);
|
||||
$oConfig->SetModuleSetting('sample-module', $key, [$key => 'value']);
|
||||
|
||||
$this->AssertWriteToFilePreservesSettings($oConfig, [
|
||||
'sample-module' => [
|
||||
'existing.property' => true,
|
||||
$key => [$key => 'value'],
|
||||
],
|
||||
], $oConfig->GetAddons());
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 3.2.3
|
||||
* @dataProvider ProviderArrayKeys
|
||||
*/
|
||||
public function testWriteToFilePreservesAddons(int|string $key): void
|
||||
{
|
||||
$oConfig = new Config();
|
||||
$aAddons = $oConfig->GetAddons();
|
||||
$aAddons[$key] = 'addons/'.$key.'/module.php';
|
||||
$oConfig->SetAddons($aAddons);
|
||||
|
||||
$this->AssertWriteToFilePreservesSettings($oConfig, [], $aAddons);
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 3.2.3
|
||||
* @dataProvider ProviderArrayKeys
|
||||
*/
|
||||
public function testWriteToFilePreservesIncrementalAddons(int|string $key): void
|
||||
{
|
||||
$aAddons = (new Config())->GetAddons();
|
||||
$aAddons[$key] = 'addons/'.$key.'/module.php';
|
||||
$sSourceFile = tempnam(sys_get_temp_dir(), 'config-source');
|
||||
$this->aFileToClean[] = $sSourceFile;
|
||||
$this->assertNotFalse(file_put_contents($sSourceFile, "<?php\n"
|
||||
."\$MySettings = [];\n"
|
||||
."\$MyModuleSettings = [];\n"
|
||||
."\$MyModules = [];\n"
|
||||
."\$MyModules['addons'] = ".var_export($aAddons, true).";\n"));
|
||||
|
||||
$this->AssertWriteToFilePreservesSettings(new Config($sSourceFile), [], $aAddons);
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 3.2.3
|
||||
* @return array<string, array{int|string}>
|
||||
*/
|
||||
public static function ProviderArrayKeys(): array
|
||||
{
|
||||
return [
|
||||
'module and property separators' => ['sample-module.setting_1'],
|
||||
'apostrophe' => ["O'Reilly"],
|
||||
'double quote' => ['a"b'],
|
||||
'backslash' => ['a\\b'],
|
||||
'repeated backslashes' => ['a\\\\b'],
|
||||
'trailing backslash' => ['trailing\\'],
|
||||
'backslash before apostrophe' => ["a\\'b"],
|
||||
'unicode' => ["caf\u{00E9}-\u{65E5}\u{672C}"],
|
||||
'line breaks' => ["first\nsecond\rthird"],
|
||||
'dollar sign' => ['$value'],
|
||||
'empty string' => [''],
|
||||
'zero' => [0],
|
||||
'integer' => [7],
|
||||
'negative integer' => [-7],
|
||||
'numeric string' => ['7'],
|
||||
'leading zero' => ['07'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 3.2.3
|
||||
* @param array<int|string, array<int|string, mixed>> $aModuleSettings
|
||||
* @param array<int|string, string> $aAddons
|
||||
*/
|
||||
private function AssertWriteToFilePreservesSettings(Config $oConfig, array $aModuleSettings, array $aAddons): void
|
||||
{
|
||||
$oOriginalConfig = utils::GetConfig();
|
||||
$oParser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
|
||||
$oEvaluator = new ConstExprEvaluator();
|
||||
|
||||
try {
|
||||
for ($i = 0; $i < 2; $i++) {
|
||||
$sTargetFile = tempnam(sys_get_temp_dir(), 'config-target');
|
||||
$this->aFileToClean[] = $sTargetFile;
|
||||
$this->assertTrue($oConfig->WriteToFile($sTargetFile));
|
||||
|
||||
$aNodes = $oParser->parse(file_get_contents($sTargetFile));
|
||||
$this->assertCount(3, $aNodes);
|
||||
$aValues = [];
|
||||
foreach ($aNodes as $oNode) {
|
||||
$this->assertInstanceOf(Node\Stmt\Expression::class, $oNode);
|
||||
$this->assertInstanceOf(Node\Expr\Assign::class, $oNode->expr);
|
||||
$this->assertInstanceOf(Node\Expr\Variable::class, $oNode->expr->var);
|
||||
$aValues[$oNode->expr->var->name] = $oEvaluator->evaluateDirectly($oNode->expr->expr);
|
||||
}
|
||||
$this->assertSame(['MySettings', 'MyModuleSettings', 'MyModules'], array_keys($aValues));
|
||||
$this->assertSame($aModuleSettings, $aValues['MyModuleSettings']);
|
||||
$this->assertSame(['addons' => $aAddons], $aValues['MyModules']);
|
||||
|
||||
$oConfig = new Config($sTargetFile);
|
||||
$this->assertSame($aAddons, $oConfig->GetAddons());
|
||||
foreach ($aModuleSettings as $sModule => $aProperties) {
|
||||
foreach ($aProperties as $sProperty => $value) {
|
||||
$this->assertSame($value, $oConfig->GetModuleSetting($sModule, $sProperty));
|
||||
}
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
utils::SetConfig($oOriginalConfig);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user