From 8af2132926909fb22828c1be0979046052b14776 Mon Sep 17 00:00:00 2001 From: Roland Gruber Date: Sun, 12 Apr 2020 12:39:52 +0200 Subject: [PATCH] import/export config --- lam/lib/config.inc | 24 ++++++++++ lam/lib/persistence.inc | 58 +++++++++++++++++++++++ lam/tests/lib/LAMCfgMainTest.php | 35 ++++++++++++++ lam/tests/lib/modules/persistenceTest.php | 53 +++++++++++++++++++++ 4 files changed, 170 insertions(+) create mode 100644 lam/lib/persistence.inc create mode 100644 lam/tests/lib/modules/persistenceTest.php diff --git a/lam/lib/config.inc b/lam/lib/config.inc index 87616d83..6b8d24ea 100644 --- a/lam/lib/config.inc +++ b/lam/lib/config.inc @@ -2681,6 +2681,30 @@ class LAMCfgMain { $this->reload(); } + /** + * Exports the configuration data. + * + * @return array config data + */ + public function exportData() { + $data = array(); + foreach ($this->settings as $setting) { + $data[$setting] = $this->$setting; + } + return $data; + } + + /** + * Imports configuration data. + * + * @param array $data config data + */ + public function importData($data) { + foreach ($data as $dataKey => $dataValue) { + $this->$dataKey = $dataValue; + } + } + /** * Reloads preferences from config file config.cfg * diff --git a/lam/lib/persistence.inc b/lam/lib/persistence.inc new file mode 100644 index 00000000..60f54783 --- /dev/null +++ b/lam/lib/persistence.inc @@ -0,0 +1,58 @@ +_getMainConfigData(); + return json_encode($jsonData); + } + + /** + * Internal function to read master configuration. + * + * @return array data + */ + public function _getMainConfigData() { + $mainCfg = new \LAMCfgMain(); + return $mainCfg->exportData(); + } + +} + diff --git a/lam/tests/lib/LAMCfgMainTest.php b/lam/tests/lib/LAMCfgMainTest.php index aa4b2d64..1b5bad13 100644 --- a/lam/tests/lib/LAMCfgMainTest.php +++ b/lam/tests/lib/LAMCfgMainTest.php @@ -120,4 +120,39 @@ class LAMCfgMainTest extends TestCase { $this->assertFalse($this->conf->showLicenseWarningOnScreen()); } + /** + * Tests the export. + */ + public function testExportData() { + $this->conf->passwordMinLower = 3; + $this->conf->sessionTimeout = 240; + $this->conf->logLevel = LOG_ERR; + $this->conf->mailServer = 'mailserver'; + + $data = $this->conf->exportData(); + + $this->assertEquals(3, $data['passwordMinLower']); + $this->assertEquals(240, $data['sessionTimeout']); + $this->assertEquals(LOG_ERR, $data['logLevel']); + $this->assertEquals('mailserver', $data['mailServer']); + } + + /** + * Tests the import. + */ + public function testImportData() { + $importData = array(); + $importData['passwordMinLower'] = 3; + $importData['sessionTimeout'] = 240; + $importData['logLevel'] = LOG_ERR; + $importData['mailServer'] = 'mailserver'; + + $this->conf->importData($importData); + + $this->assertEquals(3, $this->conf->passwordMinLower); + $this->assertEquals(240, $this->conf->sessionTimeout); + $this->assertEquals(LOG_ERR, $this->conf->logLevel); + $this->assertEquals('mailserver', $this->conf->mailServer); + } + } \ No newline at end of file diff --git a/lam/tests/lib/modules/persistenceTest.php b/lam/tests/lib/modules/persistenceTest.php new file mode 100644 index 00000000..09eb98b0 --- /dev/null +++ b/lam/tests/lib/modules/persistenceTest.php @@ -0,0 +1,53 @@ + 'val', + 'confMainKey2' => 4, + 'confMainKey3' => '', + ); + $expectedJson = json_encode(array('mainconfig' => $mainData)); + + $exporter = $this->getMockBuilder('\LAM\PERSISTENCE\ConfigDataExporter') + ->setMethods(array('_getMainConfigData')) + ->getMock(); + $exporter->method('_getMainConfigData')->willReturn($mainData); + + $json = $exporter->exportAsJson(); + + $this->assertEquals($expectedJson, $json); + } + +} +