From d12ae68bf48dc0e66c8d2ab31cb3f3dc573ff20e Mon Sep 17 00:00:00 2001 From: Taras Machyshyn Date: Fri, 10 Oct 2014 10:33:04 +0300 Subject: [PATCH] array merge fixes --- application/Espo/Core/Utils/Util.php | 14 ++- tests/Espo/Core/Utils/UtilTest.php | 157 ++++++++++++++++++++++----- 2 files changed, 141 insertions(+), 30 deletions(-) diff --git a/application/Espo/Core/Utils/Util.php b/application/Espo/Core/Utils/Util.php index aacf3cbac1..ecf05a7f57 100644 --- a/application/Espo/Core/Utils/Util.php +++ b/application/Espo/Core/Utils/Util.php @@ -142,7 +142,7 @@ class Util if ($appendKey !== false) { unset($newArray[$currentName][$appendKey]); $newArray[$currentName] = array_merge($currentValue, $newArray[$currentName]); - } else { + } else if (!static::isSingleArray($newArray[$currentName])) { $newArray[$currentName] = static::merge($currentValue, $newArray[$currentName]); } @@ -411,6 +411,18 @@ class Util return true; } + + public static function isSingleArray(array $array) + { + foreach ($array as $key => $value) { + if (!is_int($key)) { + return false; + } + } + + return true; + } + } diff --git a/tests/Espo/Core/Utils/UtilTest.php b/tests/Espo/Core/Utils/UtilTest.php index 885adfcb6b..9454b600ce 100644 --- a/tests/Espo/Core/Utils/UtilTest.php +++ b/tests/Espo/Core/Utils/UtilTest.php @@ -449,38 +449,137 @@ class UtilTest extends \PHPUnit_Framework_TestCase $result = array ( 'fields' => array ( - 'accountName' => - array ( - 'type' => 'foreign', - 'relation' => 'account', - 'foreign' => 'name', - ), - 'accountId' => - array ( - 'type' => 'foreignId', - 'index' => true, - 'where' => - array ( - '=' => 'contact.id IN ({value})', - ), - 'len' => 255, - ), - 'deleted' => - array ( - 'type' => 'bool', - 'default' => false, - 'trueValue' => true, - ), + 'accountName' => + array ( + 'type' => 'foreign', + 'relation' => 'account', + 'foreign' => 'name', + ), + 'accountId' => + array ( + 'type' => 'foreignId', + 'index' => true, + 'where' => + array ( + '=' => 'contact.id IN ({value})', + ), + 'len' => 255, + ), + 'deleted' => + array ( + 'type' => 'bool', + 'default' => false, + 'trueValue' => true, + ), ), 'relations' => array ( - 'createdBy' => - array ( - 'type' => 'belongsTo', - 'entity' => 'User', - 'key' => 'createdById', - 'foreignKey' => 'id', - ), + 'createdBy' => + array ( + 'type' => 'belongsTo', + 'entity' => 'User', + 'key' => 'createdById', + 'foreignKey' => 'id', + ), + ), + ); + + $this->assertEquals($result, Util::merge($currentArray, $newArray)); + } + + public function testMergeWithFieldsDefs() + { + $currentArray = array ( + 'fields' => + array ( + 'aaa1' => + array ( + 'type' => 'enum', + 'required' => false, + 'options' => + array ( + 0 => 'a1', + 1 => 'a3', + 2 => 'a3', + ), + 'isCustom' => true, + ), + 'hfghgfh' => + array ( + 'type' => 'varchar', + 'required' => false, + 'isCustom' => true, + 'default' => 'hfghfgh', + ), + 'jghjghj' => + array ( + 'type' => 'varchar', + 'required' => false, + 'isCustom' => true, + 'default' => 'jghjghjhg', + ), + 'gdfgdfg' => + array ( + 'type' => 'varchar', + 'required' => false, + 'isCustom' => true, + 'default' => 'gdfgdfg', + 'maxLength' => 70, + ), + ), + ); + + $newArray = array ( + 'fields' => + array ( + 'aaa1' => + array ( + 'type' => 'enum', + 'required' => false, + 'options' => + array ( + 0 => 'a1', + ), + 'isCustom' => true, + ), + ), + ); + + $result = array ( + 'fields' => + array ( + 'aaa1' => + array ( + 'type' => 'enum', + 'required' => false, + 'options' => + array ( + 0 => 'a1', + ), + 'isCustom' => true, + ), + 'hfghgfh' => + array ( + 'type' => 'varchar', + 'required' => false, + 'isCustom' => true, + 'default' => 'hfghfgh', + ), + 'jghjghj' => + array ( + 'type' => 'varchar', + 'required' => false, + 'isCustom' => true, + 'default' => 'jghjghjhg', + ), + 'gdfgdfg' => + array ( + 'type' => 'varchar', + 'required' => false, + 'isCustom' => true, + 'default' => 'gdfgdfg', + 'maxLength' => 70, + ), ), );