diff --git a/application/Espo/Core/Utils/FieldManager.php b/application/Espo/Core/Utils/FieldManager.php index d8f715ab81..ca288ca401 100644 --- a/application/Espo/Core/Utils/FieldManager.php +++ b/application/Espo/Core/Utils/FieldManager.php @@ -39,7 +39,6 @@ class FieldManager protected $customOptionName = 'isCustom'; - public function __construct(Metadata $metadata, Language $language) { $this->metadata = $metadata; @@ -133,7 +132,6 @@ class FieldManager { $fieldDef = $this->normalizeDefs($name, $fieldDef, $scope); - //$data = Json::encode($fieldDef); $this->getMetadata()->set($this->metadataType, $scope, $fieldDef); $res = $this->getMetadata()->save(); @@ -142,17 +140,17 @@ class FieldManager protected function setTranslatedOptions($name, $value, $scope) { - return $this->getLanguage()->set($name, $value, 'options', $scope); + return $this->getLanguage()->set($scope, 'options', $name, $value); } protected function setLabel($name, $value, $scope) { - return $this->getLanguage()->set($name, $value, 'fields', $scope); + return $this->getLanguage()->set($scope, 'fields', $name, $value); } protected function deleteLabel($name, $scope) { - $this->getLanguage()->delete($name, 'fields', $scope); + $this->getLanguage()->delete($scope, 'fields', $name); return $this->getLanguage()->save(); } diff --git a/application/Espo/Core/Utils/Language.php b/application/Espo/Core/Utils/Language.php index 1c5bd51cc1..19439929b8 100644 --- a/application/Espo/Core/Utils/Language.php +++ b/application/Espo/Core/Utils/Language.php @@ -220,7 +220,6 @@ class Language } $this->clearChanges(); - $this->init(true); return (bool) $result; } @@ -234,6 +233,7 @@ class Language { $this->changedData = array(); $this->deletedData = array(); + $this->init(true); } /** @@ -253,53 +253,76 @@ class Language /** * Set/change a label - * @param string | array $label - * @param mixed $value - * @param string $category + * * @param string $scope + * @param string $category + * @param string | array $name + * @param mixed $value + * + * @return void */ - public function set($label, $value, $category = 'labels', $scope = 'Global') + public function set($scope, $category, $name, $value) { - if (is_array($label)) { - foreach ($label as $rowLabel => $rowValue) { - $this->set($rowLabel, $rowValue, $category, $scope); + if (is_array($name)) { + foreach ($name as $rowLabel => $rowValue) { + $this->set($scope, $category, $rowLabel, $rowValue); } return; } - $this->changedData[$scope][$category][$label] = $value; + $this->changedData[$scope][$category][$name] = $value; $currentLanguage = $this->getLanguage(); - $this->data[$currentLanguage][$scope][$category][$label] = $value; + if (!isset($this->data[$currentLanguage])) { + $this->init(); + } + $this->data[$currentLanguage][$scope][$category][$name] = $value; + + $this->undelete($scope, $category, $name); } /** * Remove a label * - * @param string $label + * @param string $name * @param string $category * @param string $scope * * @return void */ - public function delete($label, $category = 'labels', $scope = 'Global') + public function delete($scope, $category, $name) { - if (is_array($label)) { - foreach ($label as $rowLabel) { - $this->delete($rowLabel, $category, $scope); + if (is_array($name)) { + foreach ($name as $rowLabel) { + $this->delete($scope, $category, $rowLabel); } return; } - $this->deletedData[$scope][$category][] = $label; + $this->deletedData[$scope][$category][] = $name; $currentLanguage = $this->getLanguage(); - if (isset($this->data[$currentLanguage][$scope][$category][$label])) { - unset($this->data[$currentLanguage][$scope][$category][$label]); + if (!isset($this->data[$currentLanguage])) { + $this->init(); } - if (isset($this->changedData[$scope][$category][$label])) { - unset($this->changedData[$scope][$category][$label]); + if (isset($this->data[$currentLanguage][$scope][$category][$name])) { + unset($this->data[$currentLanguage][$scope][$category][$name]); + } + + if (isset($this->changedData[$scope][$category][$name])) { + unset($this->changedData[$scope][$category][$name]); + } + } + + protected function undelete($scope, $category, $name) + { + if (isset($this->deletedData[$scope][$category])) { + foreach ($this->deletedData[$scope][$category] as $key => $labelName) { + if ($name === $labelName) { + unset($this->deletedData[$scope][$category][$key]); + } + } } } diff --git a/tests/Espo/Core/Utils/FieldManagerTest.php b/tests/Espo/Core/Utils/FieldManagerTest.php index fc045f10bd..6db875d491 100644 --- a/tests/Espo/Core/Utils/FieldManagerTest.php +++ b/tests/Espo/Core/Utils/FieldManagerTest.php @@ -1,4 +1,4 @@ -method('get') ->will($this->returnValue($data)); - $this->assertTrue($this->object->update('name', $data, 'Account')); + $this->object->update('name', $data, 'Account'); } public function testUpdateCustomFieldIsNotChanged() @@ -138,10 +138,9 @@ class FieldManagerTest extends \PHPUnit_Framework_TestCase "isCustom" => true, ); - $this->assertTrue($this->object->update('varName', $data, 'CustomEntity')); + $this->object->update('varName', $data, 'CustomEntity'); } - public function testRead() { $data = array( diff --git a/tests/Espo/Core/Utils/LanguageTest.php b/tests/Espo/Core/Utils/LanguageTest.php index 99b5b64df5..788cde284f 100644 --- a/tests/Espo/Core/Utils/LanguageTest.php +++ b/tests/Espo/Core/Utils/LanguageTest.php @@ -1,4 +1,4 @@ -object->setLanguage($originalLang); } - public function testGetLangCacheFile() { $cacheFile = $this->cacheFile; @@ -96,7 +95,6 @@ class LanguageTest extends \PHPUnit_Framework_TestCase $this->object->setLanguage($originalLang); } - public function testGetData() { $result = array ( @@ -142,7 +140,6 @@ class LanguageTest extends \PHPUnit_Framework_TestCase $this->assertEquals($result, $this->reflection->invokeMethod('getData', array())); } - public function testGet() { $result = array ( @@ -204,7 +201,6 @@ class LanguageTest extends \PHPUnit_Framework_TestCase $this->assertEquals($result, $this->object->translate('language', 'options', 'Global', $requiredOptions)); } - public function testTranslateArray() { $input = array( @@ -227,20 +223,124 @@ class LanguageTest extends \PHPUnit_Framework_TestCase public function testSet() { $label = 'TEST'; - $this->object->set('label', $label, 'fields', 'User'); + $this->object->set('User', 'fields', 'label', $label); $this->assertEquals($label, $this->object->translate('label', 'fields', 'User')); + $result = array( + 'User' => array( + 'fields' => array( + 'label' => 'TEST', + ), + ), + ); + $this->assertEquals($result, $this->reflection->getProperty('changedData')); + $label2 = 'TEST2'; - $this->object->set('name', $label2, 'fields', 'User'); + $this->object->set('User', 'fields', 'name', $label2); $this->assertEquals($label2, $this->object->translate('name', 'fields', 'User')); + $result = array( + 'User' => array( + 'fields' => array( + 'label' => 'TEST', + 'name' => 'TEST2', + ), + ), + ); + $this->assertEquals($result, $this->reflection->getProperty('changedData')); + $label3 = 'TEST3'; - $this->object->set('name', $label3, 'fields', 'Account'); + $this->object->set('Account', 'fields', 'name', $label3); $this->assertEquals($label3, $this->object->translate('name', 'fields', 'Account')); - $this->reflection->invokeMethod('init', array(true)); + $result = array( + 'User' => array( + 'fields' => array( + 'label' => 'TEST', + 'name' => 'TEST2', + ), + ), + 'Account' => array( + 'fields' => array( + 'name' => 'TEST3', + ), + ), + ); + $this->assertEquals($result, $this->reflection->getProperty('changedData')); + + $this->object->clearChanges(); + + $this->assertEquals(array(), $this->reflection->getProperty('changedData')); + $this->assertNotEquals('TEST', $this->object->get('User', 'fields', 'label')); + } + + public function testDelete() + { + $this->object->delete('User', 'fields', 'label'); + $this->assertNull($this->object->get('User.fields.label')); + + $result = array( + 'User' => array( + 'fields' => array( + 'label', + ), + ), + ); + $this->assertEquals($result, $this->reflection->getProperty('deletedData')); + + $this->object->delete('User', 'fields', 'name'); + $this->assertNull($this->object->get('User.fields.name')); + + $result = array( + 'User' => array( + 'fields' => array( + 'label', + 'name', + ), + ), + ); + $this->assertEquals($result, $this->reflection->getProperty('deletedData')); + + $this->object->clearChanges(); + + $this->assertNotNull($this->object->get('User.fields.label')); + $this->assertNotNull($this->object->get('User.fields.name')); + + $this->assertEquals(array(), $this->reflection->getProperty('deletedData')); + } + + public function testUndelete() + { + $this->object->delete('User', 'fields', 'label'); + $this->assertNull($this->object->get('User.fields.label')); + + $this->object->delete('User', 'fields', 'name'); + $this->assertNull($this->object->get('User.fields.name')); + + $label = 'TEST'; + $this->object->set('User', 'fields', 'label', $label); + $this->assertEquals($label, $this->object->translate('label', 'fields', 'User')); + + $result = array( + 'User' => array( + 'fields' => array( + 1 => 'name', + ), + ), + ); + $this->assertEquals($result, $this->reflection->getProperty('deletedData')); + + $label2 = 'TEST2'; + $this->object->set('User', 'fields', 'name', $label2); + $this->assertEquals($label2, $this->object->translate('name', 'fields', 'User')); + + $result = array( + 'User' => array( + 'fields' => array( + ), + ), + ); + $this->assertEquals($result, $this->reflection->getProperty('deletedData')); } } - -?>