IcingaConfigHelper: fix numeric dictionary key...

...rendering and add related tests

fixes #12591
This commit is contained in:
Thomas Gelf 2016-08-30 12:15:48 +00:00
parent 11f41edd13
commit 6427b22621
3 changed files with 36 additions and 2 deletions

View File

@ -105,7 +105,7 @@ class IcingaConfigHelper
public static function renderDictionaryKey($key)
{
if (preg_match('/^[a-z0-9_]+\d*$/i', $key)) {
if (preg_match('/^[a-z_]+[a-z0-9_]*$/i', $key)) {
return static::escapeIfReserved($key);
} else {
return static::renderString($key);
@ -168,7 +168,7 @@ class IcingaConfigHelper
public static function isReserved($string)
{
return in_array($string, self::$reservedWords);
return in_array($string, self::$reservedWords, true);
}
public static function escapeIfReserved($string)

View File

@ -41,4 +41,32 @@ class IcingaConfigHelperTest extends BaseTestCase
$this->assertEquals(c::renderInterval(86400), '1d');
$this->assertEquals(c::renderInterval(86459), '86459s');
}
public function testCorrectlyIdentifiesReservedWords()
{
$this->assertTrue(c::isReserved('include'), 'include is a reserved word');
$this->assertFalse(c::isReserved(0), '(int) 0 is not a reserved word');
$this->assertFalse(c::isReserved(1), '(int) 1 is not a reserved word');
$this->assertFalse(c::isReserved(true), '(boolean) true is not a reserved word');
$this->assertTrue(c::isReserved('true'), '(string) true is a reserved word');
}
public function testWhetherDictionaryRendersCorrectly()
{
$dict = (object) array(
'key1' => 'bla',
'include' => 'reserved',
'spe cial' => 'value',
'0' => 'numeric',
);
$this->assertEquals(
c::renderDictionary($dict),
rtrim($this->loadRendered('dict1'))
);
}
protected function loadRendered($name)
{
return file_get_contents(__DIR__ . '/rendered/' . $name . '.out');
}
}

View File

@ -0,0 +1,6 @@
{
@include = reserved
key1 = bla
"spe cial" = value
"0" = numeric
}