From 1b57171583b377393a5df6dce77fc51b5c78386b Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Thu, 9 Mar 2017 10:57:37 +0100 Subject: [PATCH] SyncUtils: allow special varnames, add tests fixes #839 --- library/Director/Import/SyncUtils.php | 6 +- .../library/Director/Import/SyncUtilsTest.php | 108 ++++++++++++++++++ 2 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 test/php/library/Director/Import/SyncUtilsTest.php diff --git a/library/Director/Import/SyncUtils.php b/library/Director/Import/SyncUtils.php index 55b048e8..8ec31da5 100644 --- a/library/Director/Import/SyncUtils.php +++ b/library/Director/Import/SyncUtils.php @@ -16,7 +16,7 @@ class SyncUtils */ public static function extractVariableNames($string) { - if (preg_match_all('/\${([A-Za-z0-9\._-]+)}/', $string, $m, PREG_PATTERN_ORDER)) { + if (preg_match_all('/\${([^}]+)}/', $string, $m, PREG_PATTERN_ORDER)) { return $m[1]; } else { return array(); @@ -103,7 +103,7 @@ class SyncUtils */ public static function fillVariables($string, $row) { - if (preg_match('/^\${([A-Za-z0-9\._-]+)}$/', $string, $m)) { + if (preg_match('/^\${([^}]+)}$/', $string, $m)) { return static::getSpecificValue($row, $m[1]); } @@ -111,7 +111,7 @@ class SyncUtils return SyncUtils::getSpecificValue($row, $match[1]); }; - return preg_replace_callback('/\${([A-Za-z0-9\._-]+)}/', $func, $string); + return preg_replace_callback('/\${([^}]+)}/', $func, $string); } public static function getRootVariables($vars) diff --git a/test/php/library/Director/Import/SyncUtilsTest.php b/test/php/library/Director/Import/SyncUtilsTest.php new file mode 100644 index 00000000..fda9c251 --- /dev/null +++ b/test/php/library/Director/Import/SyncUtilsTest.php @@ -0,0 +1,108 @@ +assertEquals( + array( + 'var.name', + '$Special Var' + ), + SyncUtils::extractVariableNames('This ${var.name} is ${$Special Var} are vars') + ); + + $this->assertEquals( + array(), + SyncUtils::extractVariableNames('No ${var.name vars ${$Special Var here') + ); + } + + public function testSpecificValuesCanBeRetrievedByName() + { + $row = (object)array( + 'host' => 'localhost', + 'ipaddress' => '127.0.0.1' + ); + + $this->assertEquals( + '127.0.0.1', + SyncUtils::getSpecificValue($row, 'ipaddress') + ); + } + + public function testMissingPropertiesMustBeNull() + { + $row = (object)array( + 'host' => 'localhost', + 'ipaddress' => '127.0.0.1' + ); + + $this->assertNull( + SyncUtils::getSpecificValue($row, 'address') + ); + } + + public function testNestedValuesCanBeRetrievedByPath() + { + $row = $this->getSampleRow(); + + $this->assertEquals( + '192.0.2.10', + SyncUtils::getSpecificValue($row, 'addresses.entries.eth0:1') + ); + + $this->assertEquals( + 2, + SyncUtils::getSpecificValue($row, 'addresses.count') + ); + } + + public function testRootVariablesCanBeExtracted() + { + $vars = array('test', 'nested.test', 'nested.dee.per'); + $this->assertEquals( + array( + 'test' => 'test', + 'nested' => 'nested' + ), + SyncUtils::getRootVariables($vars) + ); + } + + public function testVariables() + { + $string = '${addresses.entries.lo} and ${addresses.entries.eth0:1} are' + . ' ${This one?.$höüld be}${addressesmissing}'; + + $this->assertEquals( + '127.0.0.1 and 192.0.2.10 are fine', + SyncUtils::fillVariables( + $string, + $this->getSampleRow() + ) + ); + } + + protected function getSampleRow() + { + return (object) array( + 'host' => 'localhost', + 'addresses' => (object) array( + 'count' => 2, + 'entries' => (object) array( + 'lo' => '127.0.0.1', + 'eth0:1' => '192.0.2.10', + ) + ), + 'This one?' => (object) array( + '$höüld be' => 'fine' + ) + ); + } +}