From b7eaab715b74405f789d88c0b6462be32a732b95 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Sat, 22 Oct 2016 05:48:09 +0000 Subject: [PATCH] Make explicit calls to renderStringWithVariables --- .../CustomVariable/CustomVariableString.php | 6 +++- .../IcingaConfig/IcingaConfigHelper.php | 20 +++++++------ library/Director/Objects/IcingaObject.php | 7 ++++- .../IcingaConfig/IcingaConfigHelperTest.php | 29 ++++++++++--------- .../Director/Objects/IcingaServiceTest.php | 3 +- 5 files changed, 40 insertions(+), 25 deletions(-) diff --git a/library/Director/CustomVariable/CustomVariableString.php b/library/Director/CustomVariable/CustomVariableString.php index 6f0677b0..11942769 100644 --- a/library/Director/CustomVariable/CustomVariableString.php +++ b/library/Director/CustomVariable/CustomVariableString.php @@ -38,6 +38,10 @@ class CustomVariableString extends CustomVariable public function toConfigString($renderExpressions = false) { - return c::renderString($this->getValue(), $renderExpressions); + if ($renderExpressions) { + return c::renderStringWithVariables($this->getValue()); + } else { + return c::renderString($this->getValue()); + } } } diff --git a/library/Director/IcingaConfig/IcingaConfigHelper.php b/library/Director/IcingaConfig/IcingaConfigHelper.php index edc113b0..f76ad167 100644 --- a/library/Director/IcingaConfig/IcingaConfigHelper.php +++ b/library/Director/IcingaConfig/IcingaConfigHelper.php @@ -74,7 +74,7 @@ class IcingaConfigHelper // TODO: Find out how to allow multiline {{{...}}} strings. // Parameter? Dedicated method? Always if \n is found? - public static function renderString($string, $renderExpressions = false) + public static function renderString($string) { $special = array( '/\\\/', @@ -100,11 +100,7 @@ class IcingaConfigHelper $string = preg_replace($special, $replace, $string); - if ($renderExpressions) { - return self::renderStringWithVariables($string); - } else { - return '"' . $string . '"'; - } + return '"' . $string . '"'; } public static function renderDictionaryKey($key) @@ -262,15 +258,21 @@ class IcingaConfigHelper return $seconds . 's'; } - private static function renderStringWithVariables($string) { - $string = preg_replace('/(?renderRelationProperty($relKey, $value); } - return c::renderKeyValue($key, c::renderString($value, $this->isApplyRule())); + return c::renderKeyValue( + $key, + $this->isApplyRule() ? + c::renderStringWithVariables($value) : + c::renderString($value) + ); } protected function renderLegacyObjectProperty($key, $value) diff --git a/test/php/library/Director/IcingaConfig/IcingaConfigHelperTest.php b/test/php/library/Director/IcingaConfig/IcingaConfigHelperTest.php index ff2b8617..bb5955a0 100644 --- a/test/php/library/Director/IcingaConfig/IcingaConfigHelperTest.php +++ b/test/php/library/Director/IcingaConfig/IcingaConfigHelperTest.php @@ -83,21 +83,24 @@ class IcingaConfigHelperTest extends BaseTestCase public function testRenderStringWithVariables() { - $this->assertEquals(c::renderString('Before $var$', true), '"Before " + var'); - $this->assertEquals(c::renderString('$var$ After', true), 'var + " After"'); - $this->assertEquals(c::renderString('$var$', true), 'var'); - $this->assertEquals(c::renderString('$$var$$', true), '"$$var$$"'); - $this->assertEquals(c::renderString('Before $$var$$ After', true), '"Before $$var$$ After"'); + $this->assertEquals(c::renderStringWithVariables('Before $var$'), '"Before " + var'); + $this->assertEquals(c::renderStringWithVariables('$var$ After'), 'var + " After"'); + $this->assertEquals(c::renderStringWithVariables('$var$'), 'var'); + $this->assertEquals(c::renderStringWithVariables('$$var$$'), '"$$var$$"'); + $this->assertEquals(c::renderStringWithVariables('Before $$var$$ After'), '"Before $$var$$ After"'); $this->assertEquals( - c::renderString('Before $name$ $name$ After', true), - '"Before " + name + " " + name + " After"'); + c::renderStringWithVariables('Before $name$ $name$ After'), + '"Before " + name + " " + name + " After"' + ); $this->assertEquals( - c::renderString('Before $var1$ $var2$ After', true), - '"Before " + var1 + " " + var2 + " After"'); - $this->assertEquals(c::renderString('$host.vars.custom$', true), 'host.vars.custom'); - $this->assertEquals(c::renderString('$var"$', true), '"$var\"$"'); + c::renderStringWithVariables('Before $var1$ $var2$ After'), + '"Before " + var1 + " " + var2 + " After"' + ); + $this->assertEquals(c::renderStringWithVariables('$host.vars.custom$'), 'host.vars.custom'); + $this->assertEquals(c::renderStringWithVariables('$var"$'), '"$var\"$"'); $this->assertEquals( - c::renderString('\tI am\rrendering\nproperly\fand I $support$ "multiple" $variables$\$', true), - '"\\\\tI am\\\\rrendering\\\\nproperly\\\\fand I " + support + " \"multiple\" " + variables + "\\\\$"'); + c::renderStringWithVariables('\tI am\rrendering\nproperly\fand I $support$ "multiple" $variables$\$'), + '"\\\\tI am\\\\rrendering\\\\nproperly\\\\fand I " + support + " \"multiple\" " + variables + "\\\\$"' + ); } } diff --git a/test/php/library/Director/Objects/IcingaServiceTest.php b/test/php/library/Director/Objects/IcingaServiceTest.php index efbc18cf..07fea36c 100644 --- a/test/php/library/Director/Objects/IcingaServiceTest.php +++ b/test/php/library/Director/Objects/IcingaServiceTest.php @@ -12,7 +12,8 @@ class IcingaServiceTest extends BaseTestCase protected $testHostName = '___TEST___host'; protected $testServiceName = '___TEST___service'; - protected $createdServices = []; + + protected $createdServices = array(); public function testUnstoredHostCanBeLazySet() {