From 1af5e93bd379c1068592270c9a5c5dc74a97b42c Mon Sep 17 00:00:00 2001 From: Taras Machyshyn Date: Tue, 12 Aug 2014 12:56:17 +0300 Subject: [PATCH 1/3] added test file to ignore list --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 538699f79f..b085703006 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ /client /test.php /main.html +/tests/testData/Utils/Config/config.php From 07e7374a18f188517163e4dcafb206f54abbaa59 Mon Sep 17 00:00:00 2001 From: Taras Machyshyn Date: Tue, 12 Aug 2014 13:04:08 +0300 Subject: [PATCH 2/3] added 'remove' possibility to config --- application/Espo/Core/Utils/Config.php | 23 +++++- application/Espo/Core/Utils/File/Manager.php | 39 +++++----- application/Espo/Core/Utils/Util.php | 8 ++- tests/Espo/Core/Utils/ConfigTest.php | 75 ++++++++++++-------- 4 files changed, 92 insertions(+), 53 deletions(-) diff --git a/application/Espo/Core/Utils/Config.php b/application/Espo/Core/Utils/Config.php index 25337a9d25..19270a24a0 100644 --- a/application/Espo/Core/Utils/Config.php +++ b/application/Espo/Core/Utils/Config.php @@ -56,6 +56,7 @@ class Config private $data; private $changedData = array(); + private $removeData = array(); private $fileManager; @@ -122,6 +123,23 @@ class Config } } + /** + * Remove an option in config + * + * @param string $name + * @return bool | null - null if an option doesn't exist + */ + public function remove($name) + { + if (array_key_exists($name, $this->data)) { + unset($this->data[$name]); + $this->removeData[] = $name; + return true; + } + + return null; + } + public function save() { $values = $this->changedData; @@ -130,9 +148,12 @@ class Config $values = array_merge($this->updateCacheTimestamp(true), $values); } - $result = $this->getFileManager()->mergeContentsPHP($this->configPath, $values, true); + $removeData = empty($this->removeData) ? null : $this->removeData; + + $result = $this->getFileManager()->mergeContentsPHP($this->configPath, $values, 1, $removeData); if ($result) { $this->changedData = array(); + $this->removeData = array(); $this->loadConfig(true); } diff --git a/application/Espo/Core/Utils/File/Manager.php b/application/Espo/Core/Utils/File/Manager.php index c4031f88de..7534e9e45b 100644 --- a/application/Espo/Core/Utils/File/Manager.php +++ b/application/Espo/Core/Utils/File/Manager.php @@ -225,22 +225,33 @@ class Manager * @param string | array $path * @param string $content JSON string * @param bool $isJSON - * @param array $mergeOptions + * @param string | array $mergeOptions + * @param string | array $removeOptions - List of unset keys from content + * @param bool $isReturn - Is result to be returned or stored * - * @return bool + * @return bool | array */ - public function mergeContents($path, $content, $isJSON = false, $mergeOptions = null) + public function mergeContents($path, $content, $isJSON = false, $mergeOptions = null, $removeOptions = null, $isReturn = false) { $fileContent = $this->getContents($path); $savedDataArray = Utils\Json::getArrayData($fileContent); $newDataArray = Utils\Json::getArrayData($content); + if (isset($removeOptions)) { + $savedDataArray = Utils\Util::unsetInArray($savedDataArray, $removeOptions); + $newDataArray = Utils\Util::unsetInArray($newDataArray, $removeOptions); + } + $data = Utils\Util::merge($savedDataArray, $newDataArray, $mergeOptions); if ($isJSON) { $data = Utils\Json::encode($data, JSON_PRETTY_PRINT); } + if ($isReturn) { + return $data; + } + return $this->putContents($path, $data); } @@ -248,26 +259,14 @@ class Manager * Merge PHP content and save it to a file * * @param string | array $path - * @param string $content - * @param bool $onlyFirstLevel - Merge only first level. Ex. current: array('test'=>array('item1', 'item2')). $content= array('test'=>array('item1'),). Result will be array('test'=>array('item1')). - * + * @param string $content JSON string + * @param string | array $mergeOptions + * @param string | array $removeOptions - List of unset keys from content * @return bool */ - public function mergeContentsPHP($path, $content, $onlyFirstLevel = false, $mergeOptions = null) + public function mergeContentsPHP($path, $content, $mergeOptions = null, $removeOptions = null) { - $fileContent = $this->getContents($path); - - $savedDataArray = Utils\Json::getArrayData($fileContent); - $newDataArray = Utils\Json::getArrayData($content); - - if ($onlyFirstLevel) { - foreach($newDataArray as $key => $val) { - $setVal = is_array($val) ? array() : ''; - $savedDataArray[$key] = $setVal; - } - } - - $data = Utils\Util::merge($savedDataArray, $newDataArray, $mergeOptions); + $data = $this->mergeContents($path, $content, false, $mergeOptions, $removeOptions, true); return $this->putContentsPHP($path, $data); } diff --git a/application/Espo/Core/Utils/Util.php b/application/Espo/Core/Utils/Util.php index 22359906dc..60fe48e235 100644 --- a/application/Espo/Core/Utils/Util.php +++ b/application/Espo/Core/Utils/Util.php @@ -359,11 +359,17 @@ class Util if (!empty($unsetSett)){ $keyItems = explode('.', $unsetSett); $currVal = isset($content[$rootKey]) ? "\$content['{$rootKey}']" : "\$content"; + + $lastKey = array_pop($keyItems); foreach($keyItems as $keyItem){ $currVal .= "['{$keyItem}']"; } - $currVal = "if (isset({$currVal})) unset({$currVal});"; + $unsetElem = $currVal . "['{$lastKey}']"; + $currVal = " + if (isset({$unsetElem}) || array_key_exists({$lastKey}, {$currVal})) { + unset({$unsetElem}); + } "; eval($currVal); } } diff --git a/tests/Espo/Core/Utils/ConfigTest.php b/tests/Espo/Core/Utils/ConfigTest.php index 14874c6e63..96dd5ee181 100644 --- a/tests/Espo/Core/Utils/ConfigTest.php +++ b/tests/Espo/Core/Utils/ConfigTest.php @@ -15,39 +15,39 @@ class ConfigTest extends \PHPUnit_Framework_TestCase protected $systemConfigPath = 'tests/testData/Utils/Config/systemConfig.php'; - protected function setUp() - { + protected function setUp() + { $this->objects['fileManager'] = new \Espo\Core\Utils\File\Manager(); - $this->object = new \Espo\Core\Utils\Config($this->objects['fileManager']); + $this->object = new \Espo\Core\Utils\Config($this->objects['fileManager']); - $this->reflection = new ReflectionHelper($this->object); + $this->reflection = new ReflectionHelper($this->object); - $this->reflection->setProperty('configPath', $this->configPath); - $this->reflection->setProperty('systemConfigPath', $this->systemConfigPath); - } + $this->reflection->setProperty('configPath', $this->configPath); + $this->reflection->setProperty('systemConfigPath', $this->systemConfigPath); + } - protected function tearDown() - { - $this->object = NULL; - } + protected function tearDown() + { + $this->object = NULL; + } - public function testLoadConfig() + public function testLoadConfig() { $this->assertArrayHasKey('database', $this->reflection->invokeMethod('loadConfig', array())); $this->assertArrayHasKey('dateFormat', $this->reflection->invokeMethod('loadConfig', array())); } - public function testGet() + public function testGet() { $result = array( 'driver' => 'pdo_mysql', - 'host' => 'localhost', - 'dbname' => 'espocrm', - 'user' => 'root', - 'password' => '', + 'host' => 'localhost', + 'dbname' => 'espocrm', + 'user' => 'root', + 'password' => '', ); $this->assertEquals($result, $this->object->get('database')); @@ -62,17 +62,17 @@ class ConfigTest extends \PHPUnit_Framework_TestCase } - public function testSet() + public function testSet() { - $setKey= 'testOption'; + $setKey= 'testOption'; $setValue= 'Test'; $this->object->set($setKey, $setValue); - $this->assertTrue($this->object->save()); - $this->assertEquals($setValue, $this->object->get($setKey)); + $this->assertTrue($this->object->save()); + $this->assertEquals($setValue, $this->object->get($setKey)); - $this->object->set($setKey, 'Another Wrong Value'); - $this->assertTrue($this->object->save()); + $this->object->set($setKey, 'Another Wrong Value'); + $this->assertTrue($this->object->save()); } public function testSetArray() @@ -83,15 +83,28 @@ class ConfigTest extends \PHPUnit_Framework_TestCase ); $this->object->set($values); - $this->assertTrue($this->object->save()); - $this->assertEquals('Test', $this->object->get('testOption')); - $this->assertEquals('Test2', $this->object->get('testOption2')); + $this->assertTrue($this->object->save()); + $this->assertEquals('Test', $this->object->get('testOption')); + $this->assertEquals('Test2', $this->object->get('testOption2')); - $wrongArray = array( - 'testOption' => 'Another Wrong Value', - ); - $this->object->set($wrongArray); - $this->assertTrue($this->object->save()); + $wrongArray = array( + 'testOption' => 'Another Wrong Value', + ); + $this->object->set($wrongArray); + $this->assertTrue($this->object->save()); + } + + public function testRemove() + { + $optKey = 'removeOption'; + $optValue = 'Test'; + + $this->object->set($optKey, $optValue); + $this->assertTrue($this->object->save()); + + $this->assertTrue($this->object->remove($optKey)); + + $this->assertNull($this->object->get($optKey)); } public function testSystemConfigMerge() From 8d4219802bc3f02a0e3c4e58c5025bec5d925d35 Mon Sep 17 00:00:00 2001 From: Taras Machyshyn Date: Tue, 12 Aug 2014 13:44:23 +0300 Subject: [PATCH 3/3] added rebuild for Currency Settings --- application/Espo/Controllers/Settings.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/application/Espo/Controllers/Settings.php b/application/Espo/Controllers/Settings.php index 44d39a3133..5593787faa 100644 --- a/application/Espo/Controllers/Settings.php +++ b/application/Espo/Controllers/Settings.php @@ -63,6 +63,12 @@ class Settings extends \Espo\Core\Controllers\Base throw new Error('Cannot save settings'); } + /** Rebuild for Currency Settings */ + if (isset($data['baseCurrency']) || isset($data['currencyRates'])) { + $this->getContainer()->get('dataManager')->rebuildDatabase(array()); + } + /** END Rebuild for Currency Settings */ + return $this->getConfigData(); } }