Skip to content
Permalink
Browse files Browse the repository at this point in the history
Cleanup code, add unit tests.
Signed-off-by: Volker Theile <votdev@gmx.de>
  • Loading branch information
votdev committed Sep 26, 2020
1 parent dac11fb commit ebb51bb
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 3 deletions.
@@ -0,0 +1,58 @@
#!/usr/bin/phpunit -c/etc/openmediavault
<?php
/**
* This file is part of OpenMediaVault.
*
* @license http://www.gnu.org/licenses/gpl.html GPL Version 3
* @author Volker Theile <volker.theile@openmediavault.org>
* @copyright Copyright (c) 2009-2020 Volker Theile
*
* OpenMediaVault is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* OpenMediaVault is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenMediaVault. If not, see <http://www.gnu.org/licenses/>.
*/
require_once("openmediavault/autoloader.inc");

class test_openmediavault_config_databasebackend extends \PHPUnit\Framework\TestCase {
private $databaseBackend;

public function setUp() {
$this->databaseBackend = new \OMV\Config\DatabaseBackend(
sprintf("%s/../data/config.xml", getcwd()),
FALSE);
$this->databaseBackend->load();
}

public function test_compare_1() {
$this->assertEquals($this->databaseBackend->compare(
"//system/time",
[
"timezone" => "Europe/Berlin",
"ntp" => [
"enable" => 0,
"timeservers" => "pool.ntp.org",
"clients" => ""
]
]
), 0);
}

public function test_compare_2() {
$this->assertEquals($this->databaseBackend->compare(
"//system/powermanagement",
[
"cpufreq" => 0,
"powerbtn" => "nothing"
]
), -1);
}
}
Expand Up @@ -335,4 +335,32 @@ public function test_array_boolval_12() {
public function test_array_boolval_13() {
$this->assertTrue(array_boolval("a", "c", TRUE));
}

public function test_array_sort_key_1() {
$d = [
['id' => 3, 'text' => 'b'],
['id' => 1, 'text' => 'a'],
['id' => 2, 'text' => 'c']
];
$this->assertTrue(array_sort_key($d, "id"));
$this->assertEquals($d, [
['id' => 1, 'text' => 'a'],
['id' => 2, 'text' => 'c'],
['id' => 3, 'text' => 'b']
]);
}

public function test_array_sort_key_2() {
$d = [
['id' => 3, 'text' => 'b'],
['id' => 1, 'text' => 'a'],
['id' => 2, 'text' => 'c']
];
$this->assertTrue(array_sort_key($d, "text"));
$this->assertEquals($d, [
['id' => 1, 'text' => 'a'],
['id' => 3, 'text' => 'b'],
['id' => 2, 'text' => 'c']
]);
}
}
Expand Up @@ -331,15 +331,30 @@ class DatabaseBackend {
// Convert all values to strings before comparison. This is necessary
// because the values returned from the configuration database are
// strings.
if (FALSE === array_walk_recursive($data, function(&$item, $key) {
if (is_string($item)) {
if (!mb_check_encoding($item, "UTF-8")) {
$item = utf8_encode($item);
}
} else {
$item = utf8_encode(strval($item));
}
})) {
return FALSE;
}
if (FALSE === array_walk_recursive($data,
create_function('&$item, $key', 'if (is_string($item)) { '.
'if (!mb_check_encoding($item, "UTF-8")) { '.
'$item = utf8_encode($item); } } else { '.
'$item = utf8_encode(strval($item)); }')))
return FALSE;
// Compare the arrays.
if (0 == count(array_diff($result, $data)))
// Note, we can not use `array_diff` here because this function
// does not support multidimensional arrays.
if (0 === strcmp(json_encode_safe($result),
json_encode_safe($data))) {
return 0;
}
return -1;
}

Expand Down
6 changes: 4 additions & 2 deletions deb/openmediavault/usr/share/php/openmediavault/functions.inc
Expand Up @@ -84,9 +84,11 @@ function array_sort_key(array &$array, $key) {
if (!is_multi_array($array))
return FALSE;
// Sort the array.
if (FALSE === uasort($array, create_function('$a, $b',
"return strnatcmp(strval(\$a['$key']), strval(\$b['$key']));")))
if (FALSE === uasort($array, function($a, $b) use($key) {
return strnatcmp(strval($a[$key]), strval($b[$key]));
})) {
return FALSE;
}
// Re-index the array.
$array = array_values($array);
return TRUE;
Expand Down

0 comments on commit ebb51bb

Please sign in to comment.