Merge branch 'master' of ssh://172.20.0.1/var/git/espo/backend
This commit is contained in:
@@ -28,8 +28,9 @@
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Utils\File;
|
||||
use Espo\Core\Utils,
|
||||
Espo\Core\Exceptions\Error;
|
||||
|
||||
use Espo\Core\Utils;
|
||||
use Espo\Core\Exceptions\Error;
|
||||
|
||||
class Manager
|
||||
{
|
||||
@@ -110,7 +111,7 @@ class Manager
|
||||
}
|
||||
|
||||
if ($isReturnSingleArray) {
|
||||
return $this->getSingeFileList($result, $onlyFileType);
|
||||
return $this->getSingeFileList($result, $onlyFileType, $path);
|
||||
}
|
||||
|
||||
return $result;
|
||||
@@ -125,7 +126,7 @@ class Manager
|
||||
*
|
||||
* @return aray
|
||||
*/
|
||||
protected function getSingeFileList(array $fileList, $onlyFileType = null, $parentDirName = '')
|
||||
protected function getSingeFileList(array $fileList, $onlyFileType = null, $basePath = null, $parentDirName = '')
|
||||
{
|
||||
$singleFileList = array();
|
||||
foreach($fileList as $dirName => $fileName) {
|
||||
@@ -133,16 +134,16 @@ class Manager
|
||||
if (is_array($fileName)) {
|
||||
$currentDir = Utils\Util::concatPath($parentDirName, $dirName);
|
||||
|
||||
if (!isset($onlyFileType) || $onlyFileType == $this->isFile($currentDir)) {
|
||||
if (!isset($onlyFileType) || $onlyFileType == $this->isFile($currentDir, $basePath)) {
|
||||
$singleFileList[] = $currentDir;
|
||||
}
|
||||
|
||||
$singleFileList = array_merge($singleFileList, $this->getSingeFileList($fileName, $onlyFileType, $currentDir));
|
||||
$singleFileList = array_merge($singleFileList, $this->getSingeFileList($fileName, $onlyFileType, $basePath, $currentDir));
|
||||
|
||||
} else {
|
||||
$currentFileName = Utils\Util::concatPath($parentDirName, $fileName);
|
||||
|
||||
if (!isset($onlyFileType) || $onlyFileType == $this->isFile($currentFileName)) {
|
||||
if (!isset($onlyFileType) || $onlyFileType == $this->isFile($currentFileName, $basePath)) {
|
||||
$singleFileList[] = $currentFileName;
|
||||
}
|
||||
}
|
||||
@@ -221,7 +222,6 @@ class Manager
|
||||
*/
|
||||
public function putPhpContents($path, $data, $withObjects = false)
|
||||
{
|
||||
|
||||
return $this->putContents($path, $this->wrapForDataExport($data, $withObjects), LOCK_EX);
|
||||
}
|
||||
|
||||
@@ -425,13 +425,7 @@ class Manager
|
||||
$sourcePath = $this->concatPaths($sourcePath);
|
||||
$destPath = $this->concatPaths($destPath);
|
||||
|
||||
if (isset($fileList)) {
|
||||
if (!empty($sourcePath)) {
|
||||
foreach ($fileList as &$fileName) {
|
||||
$fileName = $this->concatPaths(array($sourcePath, $fileName));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (!isset($fileList)) {
|
||||
$fileList = is_file($sourcePath) ? (array) $sourcePath : $this->getFileList($sourcePath, $recursively, '', true, true);
|
||||
}
|
||||
|
||||
@@ -679,10 +673,16 @@ class Manager
|
||||
* Check if $dirname is directory.
|
||||
*
|
||||
* @param string $dirname
|
||||
* @param string $basePath
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isDir($dirname)
|
||||
public function isDir($dirname, $basePath = null)
|
||||
{
|
||||
if (!empty($basePath)) {
|
||||
$dirname = $this->concatPaths([$basePath, $dirname]);
|
||||
}
|
||||
|
||||
return is_dir($dirname);
|
||||
}
|
||||
|
||||
@@ -690,10 +690,16 @@ class Manager
|
||||
* Check if $filename is file. If $filename doesn'ot exist, check by pathinfo
|
||||
*
|
||||
* @param string $filename
|
||||
* @param string $basePath
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isFile($filename)
|
||||
public function isFile($filename, $basePath = null)
|
||||
{
|
||||
if (!empty($basePath)) {
|
||||
$filename = $this->concatPaths([$basePath, $filename]);
|
||||
}
|
||||
|
||||
if (file_exists($filename)) {
|
||||
return is_file($filename);
|
||||
}
|
||||
|
||||
@@ -236,17 +236,39 @@ class Metadata
|
||||
}
|
||||
}
|
||||
|
||||
public function getAllObjects($isJSON = false, $reload = false)
|
||||
protected function getObjData($reload = false)
|
||||
{
|
||||
if (!isset($this->objData) || $reload) {
|
||||
$this->objInit($reload);
|
||||
}
|
||||
|
||||
return $this->objData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Object Metadata
|
||||
*
|
||||
* @param mixed string|array $key
|
||||
* @param mixed $default
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
public function getObjects($key = null, $default = null)
|
||||
{
|
||||
$objData = $this->getObjData();
|
||||
|
||||
return Util::getValueByKey($objData, $key, $default);
|
||||
}
|
||||
|
||||
public function getAllObjects($isJSON = false, $reload = false)
|
||||
{
|
||||
$objData = $this->getObjData($reload);
|
||||
|
||||
if ($isJSON) {
|
||||
return Json::encode($this->objData);
|
||||
return Json::encode($objData);
|
||||
}
|
||||
|
||||
return $this->objData;
|
||||
return $objData;
|
||||
}
|
||||
|
||||
public function getAllForFrontend()
|
||||
@@ -311,25 +333,15 @@ class Metadata
|
||||
* @param string|array $key
|
||||
* @param mixed $default
|
||||
*
|
||||
* @return array|null
|
||||
* @return object|mixed
|
||||
*/
|
||||
public function getCustom($key = null, $default = null)
|
||||
public function getCustom($key1, $key2, $default = null)
|
||||
{
|
||||
$keyList = is_array($key) ? $key : explode('.', $key);
|
||||
|
||||
if (!isset($keyList[0]) || !isset($keyList[1])) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
list($key1, $key2) = $keyList;
|
||||
unset($keyList[0], $keyList[1]);
|
||||
|
||||
$filePath = array($this->paths['customPath'], $key1, $key2.'.json');
|
||||
$fileContent = $this->getFileManager()->getContents($filePath);
|
||||
|
||||
if ($fileContent) {
|
||||
$data = Json::getArrayData($fileContent);
|
||||
return Util::getValueByKey($data, $keyList, $default);
|
||||
return Json::decode($fileContent);
|
||||
}
|
||||
|
||||
return $default;
|
||||
@@ -346,8 +358,8 @@ class Metadata
|
||||
*/
|
||||
public function saveCustom($key1, $key2, $data)
|
||||
{
|
||||
$changedData = Json::encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
$filePath = array($this->paths['customPath'], $key1, $key2.'.json');
|
||||
$changedData = Json::encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
|
||||
$result = $this->getFileManager()->putContents($filePath, $changedData);
|
||||
|
||||
|
||||
@@ -432,4 +432,78 @@ class ManagerTest extends \PHPUnit\Framework\TestCase
|
||||
$this->object->removeInDir($cachePath);
|
||||
}
|
||||
}
|
||||
|
||||
public function testCopyTestCase2()
|
||||
{
|
||||
$path = 'tests/unit/testData/FileManager/copy/testCase2';
|
||||
$cachePath = $this->cachePath . '/copy/testCase2';
|
||||
|
||||
$expectedResult = [
|
||||
'custom/Espo/Custom/test1.php',
|
||||
'data/test2.php',
|
||||
'data/upload/5a86d9bf1154968dc',
|
||||
'test0.php'
|
||||
];
|
||||
|
||||
$result = $this->object->copy($path, $cachePath, true);
|
||||
|
||||
if ($result) {
|
||||
$this->assertEquals($expectedResult, $this->object->getFileList($cachePath, true, '', true, true));
|
||||
$this->object->removeInDir($cachePath);
|
||||
}
|
||||
}
|
||||
|
||||
public function testCopyTestCase3()
|
||||
{
|
||||
$path = 'tests/unit/testData/FileManager/copy/testCase3';
|
||||
$cachePath = $this->cachePath . '/copy/testCase3';
|
||||
|
||||
$expectedResult = [
|
||||
'custom/Espo/Custom/test1.php',
|
||||
'data/test2.php',
|
||||
'data/upload/5a86d9bf1154968dc',
|
||||
'test0.php'
|
||||
];
|
||||
|
||||
$fileList = $this->object->getFileList($path, true, '', true, true);
|
||||
|
||||
$this->assertEquals($expectedResult, $fileList, "Expected Result and File List");
|
||||
|
||||
$result = $this->object->copy($path, $cachePath, true, $fileList);
|
||||
|
||||
if ($result) {
|
||||
$this->assertEquals($expectedResult, $this->object->getFileList($cachePath, true, '', true, true), "Expected Result and List of copied files");
|
||||
$this->object->removeInDir($cachePath);
|
||||
}
|
||||
}
|
||||
|
||||
public function testCopyTestCase4()
|
||||
{
|
||||
$path = 'tests/unit/testData/FileManager/copy/testCase4';
|
||||
$cachePath = $this->cachePath . '/copy/testCase4';
|
||||
|
||||
$expectedResult = [
|
||||
'custom',
|
||||
'custom/Espo',
|
||||
'custom/Espo/Custom',
|
||||
'custom/Espo/Custom/test1.php',
|
||||
'data',
|
||||
'data/test2.php',
|
||||
'data/upload',
|
||||
'data/upload/5a86d9bf1154968dc',
|
||||
'test0.php'
|
||||
];
|
||||
|
||||
$fileList = $this->object->getFileList($path, true, '', null, true);
|
||||
|
||||
$this->assertEquals($expectedResult, $fileList, "Expected Result and File List");
|
||||
|
||||
$result = $this->object->copy($path, $cachePath, true, $fileList);
|
||||
|
||||
if ($result) {
|
||||
$this->assertEquals($expectedResult, $this->object->getFileList($cachePath, true, '', null, true), "Expected Result and List of copied files");
|
||||
$this->object->removeInDir($cachePath);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -40,9 +40,10 @@ class MetadataTest extends \PHPUnit\Framework\TestCase
|
||||
protected $reflection;
|
||||
|
||||
protected $defaultCacheFile = 'tests/unit/testData/Utils/Metadata/metadata.php';
|
||||
protected $defaultObjCacheFile = 'tests/unit/testData/Utils/Metadata/metadata.php';
|
||||
|
||||
protected $cacheFile = 'tests/unit/testData/cache/metadata.php';
|
||||
protected $ormCacheFile = 'tests/unit/testData/Utils/Metadata/ormMetadata.php';
|
||||
protected $objCacheFile = 'tests/unit/testData/cache/objMetadata.php';
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
@@ -51,6 +52,10 @@ class MetadataTest extends \PHPUnit\Framework\TestCase
|
||||
copy($this->defaultCacheFile, $this->cacheFile);
|
||||
}
|
||||
|
||||
if (!file_exists($this->objCacheFile)) {
|
||||
copy($this->defaultObjCacheFile, $this->objCacheFile);
|
||||
}
|
||||
|
||||
$this->objects['fileManager'] = new \Espo\Core\Utils\File\Manager();
|
||||
|
||||
$this->objects['log'] = $this->getMockBuilder('\\Espo\\Core\\Utils\\Log')->disableOriginalConstructor()->getMock();
|
||||
@@ -60,6 +65,7 @@ class MetadataTest extends \PHPUnit\Framework\TestCase
|
||||
|
||||
$this->reflection = new ReflectionHelper($this->object);
|
||||
$this->reflection->setProperty('cacheFile', $this->cacheFile);
|
||||
$this->reflection->setProperty('objCacheFile', $this->objCacheFile);
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
@@ -238,28 +244,22 @@ class MetadataTest extends \PHPUnit\Framework\TestCase
|
||||
$paths['customPath'] = $customPath;
|
||||
$this->reflection->setProperty('paths', $paths);
|
||||
|
||||
$this->assertNull($this->object->getCustom(['entityDefs', 'Lead']));
|
||||
$this->assertNull($this->object->getCustom('entityDefs.Lead'));
|
||||
$this->assertNull($this->object->getCustom('entityDefs.Lead.fields'));
|
||||
$this->assertNull($this->object->getCustom('entityDefs', 'Lead'));
|
||||
|
||||
$customData = $this->object->getCustom('entityDefs.Lead', []);
|
||||
$this->assertTrue(is_array($customData));
|
||||
$customData = $this->object->getCustom('entityDefs', 'Lead', (object) []);
|
||||
$this->assertTrue(is_object($customData));
|
||||
|
||||
$data = array (
|
||||
'fields' =>
|
||||
array (
|
||||
'status' =>
|
||||
array (
|
||||
$data = (object) [
|
||||
'fields' => (object) [
|
||||
'status' => (object) [
|
||||
"type" => "enum",
|
||||
"options" => ["__APPEND__", "Test1", "Test2"],
|
||||
),
|
||||
),
|
||||
);
|
||||
],
|
||||
],
|
||||
];
|
||||
$this->object->saveCustom('entityDefs', 'Lead', $data);
|
||||
|
||||
$this->assertEquals($data, $this->object->getCustom('entityDefs.Lead'));
|
||||
$this->assertEquals($data['fields'], $this->object->getCustom('entityDefs.Lead.fields'));
|
||||
$this->assertEquals($data['fields'], $this->object->getCustom(['entityDefs', 'Lead', 'fields']));
|
||||
$this->assertEquals($data, $this->object->getCustom('entityDefs', 'Lead'));
|
||||
|
||||
unlink($customPath . '/entityDefs/Lead.json');
|
||||
}
|
||||
@@ -274,22 +274,20 @@ class MetadataTest extends \PHPUnit\Framework\TestCase
|
||||
$paths['customPath'] = $customPath;
|
||||
$this->reflection->setProperty('paths', $paths);
|
||||
|
||||
$data = array (
|
||||
'fields' =>
|
||||
array (
|
||||
'status' =>
|
||||
array (
|
||||
$data = (object) [
|
||||
'fields' => (object) [
|
||||
'status' => (object) [
|
||||
"type" => "enum",
|
||||
"options" => ["__APPEND__", "Test1", "Test2"],
|
||||
),
|
||||
),
|
||||
);
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$this->object->saveCustom('entityDefs', 'Lead', $data);
|
||||
|
||||
$savedFile = $customPath . '/entityDefs/Lead.json';
|
||||
$fileContent = $this->objects['fileManager']->getContents($savedFile);
|
||||
$savedData = \Espo\Core\Utils\Json::getArrayData($fileContent);
|
||||
$savedData = \Espo\Core\Utils\Json::decode($fileContent);
|
||||
|
||||
$this->assertEquals($data, $savedData);
|
||||
|
||||
@@ -308,40 +306,52 @@ class MetadataTest extends \PHPUnit\Framework\TestCase
|
||||
$paths['customPath'] = $customPath;
|
||||
$this->reflection->setProperty('paths', $paths);
|
||||
|
||||
$initData = array (
|
||||
'fields' =>
|
||||
array (
|
||||
'status' =>
|
||||
array (
|
||||
$initData = (object) [
|
||||
'fields' => (object) [
|
||||
'status' => (object) [
|
||||
"type" => "enum",
|
||||
"options" => ["__APPEND__", "Test1", "Test2"],
|
||||
),
|
||||
),
|
||||
);
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$this->object->saveCustom('entityDefs', 'Lead', $initData);
|
||||
|
||||
$customData = $this->object->getCustom(['entityDefs', 'Lead']);
|
||||
unset($customData['fields']['status']['type']);
|
||||
$customData['fields']['status']['options'] = ["__APPEND__", "Test1"];
|
||||
$customData = $this->object->getCustom('entityDefs', 'Lead');
|
||||
|
||||
unset($customData->fields->status->type);
|
||||
$customData->fields->status->options = ["__APPEND__", "Test1"];
|
||||
$this->object->saveCustom('entityDefs', 'Lead', $customData);
|
||||
|
||||
$savedFile = $customPath . '/entityDefs/Lead.json';
|
||||
$fileContent = $this->objects['fileManager']->getContents($savedFile);
|
||||
$savedData = \Espo\Core\Utils\Json::getArrayData($fileContent);
|
||||
$savedData = \Espo\Core\Utils\Json::decode($fileContent);
|
||||
|
||||
$expectedData = array (
|
||||
'fields' =>
|
||||
array (
|
||||
'status' =>
|
||||
array (
|
||||
$expectedData = (object) [
|
||||
'fields' => (object) [
|
||||
'status' => (object) [
|
||||
"options" => ["__APPEND__", "Test1"],
|
||||
),
|
||||
),
|
||||
);
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$this->assertEquals($expectedData, $savedData);
|
||||
|
||||
unlink($savedFile);
|
||||
}
|
||||
|
||||
public function testGetObjects()
|
||||
{
|
||||
$result = 'System';
|
||||
$this->assertEquals($result, $this->object->getObjects('app.adminPanel.system.label'));
|
||||
|
||||
$result = 'fields';
|
||||
$this->assertObjectHasAttribute($result, $this->object->getObjects('entityDefs.User'));
|
||||
|
||||
$result = (object) [
|
||||
'type' => 'bool',
|
||||
'tooltip' => true
|
||||
];
|
||||
$this->assertEquals($result, $this->object->getObjects('entityDefs.User.fields.isAdmin'));
|
||||
}
|
||||
}
|
||||
@@ -1437,6 +1437,38 @@ class UtilTest extends \PHPUnit\Framework\TestCase
|
||||
$this->assertEquals('customReturns', Util::getValueByKey($inputArray, 'Contact.notExists', 'customReturns'));
|
||||
}
|
||||
|
||||
public function testGetValueByKeyWithObjects()
|
||||
{
|
||||
$inputObject = (object) [
|
||||
'Account' => (object) [
|
||||
'useCache' => true,
|
||||
'sub' => (object) [
|
||||
'subV' => '125',
|
||||
'subO' => (object) [
|
||||
'subOV' => '125',
|
||||
'subOV2' => '125',
|
||||
],
|
||||
],
|
||||
],
|
||||
'Contact' => (object) [
|
||||
'useCache' => true,
|
||||
],
|
||||
];
|
||||
|
||||
$this->assertEquals($inputObject, Util::getValueByKey($inputObject));
|
||||
$this->assertEquals($inputObject, Util::getValueByKey($inputObject, ''));
|
||||
|
||||
$this->assertEquals('125', Util::getValueByKey($inputObject, 'Account.sub.subV'));
|
||||
|
||||
$result = (object) ['useCache' => true];
|
||||
$this->assertEquals($result, Util::getValueByKey($inputObject, 'Contact'));
|
||||
|
||||
$this->assertNull(Util::getValueByKey($inputObject, 'Contact.notExists'));
|
||||
|
||||
$this->assertEquals('customReturns', Util::getValueByKey($inputObject, 'Contact.notExists', 'customReturns'));
|
||||
$this->assertNotEquals('customReturns', Util::getValueByKey($inputObject, 'Contact.useCache', 'customReturns'));
|
||||
}
|
||||
|
||||
public function testUnsetInArrayByValue()
|
||||
{
|
||||
$newArray = json_decode('[
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user