snapshot of https://github.com/espocrm/espocrm.git (master, 2026-09-18; blobs > 131072B omitted)
Some checks failed
Test / Test on PHP 8.3 (push) Failing after 5s
Test / Test on PHP 8.4 (push) Failing after 4s
Test / Test on PHP 8.5 (push) Failing after 3s
TypeScript static analysis / TypeScript check (push) Failing after 14s
Test Integration on MySQL / Test on PHP 8.3 (push) Failing after 2s
Test Integration on MySQL / Test on PHP 8.5 (push) Failing after 0s
Test Integration on PostgreSQL / Test on PHP 8.4 fix (push) Failing after 1s
Test Integration on PostgreSQL / Test on PHP 8.4 master (push) Failing after 1s

This commit is contained in:
upstream-mirror
2026-09-18 04:19:15 +05:00
commit 7cc7e6b4f3
10077 changed files with 900756 additions and 0 deletions

View File

@@ -0,0 +1,219 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace tests\unit\Espo\Core\Utils\File;
use PHPUnit\Framework\TestCase;
use tests\unit\ReflectionHelper;
use Espo\Core\Utils\File\ClassMap;
use Espo\Core\Utils\DataCache;
use Espo\Core\Utils\File\Manager as FileManager;
use Espo\Core\Utils\Config;
use Espo\Core\Utils\Module;
use Espo\Core\Utils\Module\PathProvider;
class ClassMapTest extends TestCase
{
/** @var ClassMap */
private $classMap;
private $dataCache;
protected $reflection;
private $systemConfig;
private $customPath = 'tests/unit/testData/EntryPoints/Espo/Custom/';
private $corePath = 'tests/unit/testData/EntryPoints/Espo/';
private $modulePath = 'tests/unit/testData/EntryPoints/Espo/Modules/{*}/';
/** @var Module */
private $module;
protected function setUp(): void
{
$fileManager = new FileManager();
$this->systemConfig = $this->createMock(Config\SystemConfig::class);
$this->module = $this->createMock(Module::class);
$this->dataCache = $this->createMock(DataCache::class);
$pathProvider = $this->createMock(PathProvider::class);
$pathProvider
->method('getCustom')
->willReturn($this->customPath);
$pathProvider
->method('getCore')
->willReturn($this->corePath);
$pathProvider
->method('getModule')
->willReturnCallback(
function (?string $moduleName): string {
if ($moduleName === null) {
return $this->modulePath;
}
return str_replace('{*}', $moduleName, $this->modulePath);
}
);
$this->module
->method('getOrderedList')
->willReturn(['Crm']);
$this->classMap = new ClassMap(
$fileManager,
$this->module,
$this->dataCache,
$pathProvider,
$this->systemConfig,
);
$this->reflection = new ReflectionHelper($this->classMap);
}
public function testGetDataWithNoCache1(): void
{
$expected = [
'Download' => 'tests\unit\testData\EntryPoints\Espo\EntryPoints\Download',
'TestEntry' => 'tests\unit\testData\EntryPoints\Espo\EntryPoints\TestEntry',
'InModule' => 'tests\unit\testData\EntryPoints\Espo\Modules\Crm\EntryPoints\InModule'
];
$this->assertEquals($expected, $this->classMap->getData('EntryPoints', null, ['run']));
}
public function testGetDataWithNoCache2(): void
{
$this->dataCache
->expects($this->once())
->method('has')
->with('entryPoints')
->willReturn(true);
$this->systemConfig
->expects($this->exactly(2))
->method('useCache')
->willReturn(false);
$this->module
->expects($this->once())
->method('getOrderedList')
->willReturn(
['Crm']
);
$cacheKey = 'entryPoints';
$result = [
'Download' => 'tests\unit\testData\EntryPoints\Espo\EntryPoints\Download',
'TestEntry' => 'tests\unit\testData\EntryPoints\Espo\EntryPoints\TestEntry',
'InModule' => 'tests\unit\testData\EntryPoints\Espo\Modules\Crm\EntryPoints\InModule',
];
$this->assertEquals(
$result,
$this->classMap->getData('EntryPoints', $cacheKey, ['run'])
);
}
public function testGetDataWithCache(): void
{
$result = [
'Download' => 'tests\\unit\\testData\\EntryPoints\\Espo\\EntryPoints\\Download',
];
$this->systemConfig
->expects($this->once())
->method('useCache')
->willReturn(true);
$cacheKey = 'entryPoints';
$this->dataCache
->expects($this->once())
->method('has')
->with('entryPoints')
->willReturn(true);
$this->dataCache
->expects($this->once())
->method('get')
->with('entryPoints')
->willReturn($result);
$this->module
->expects($this->never())
->method('getOrderedList');
$this->assertEquals($result, $this->classMap->getData('EntryPoints', $cacheKey, ['run']));
}
public function testGetDataWithNoCacheString(): void
{
$this->systemConfig
->expects($this->exactly(2))
->method('useCache')
->willReturn(true);
$this->module
->expects($this->once())
->method('getOrderedList')
->willReturn(
['Crm']
);
$cacheKey = 'entryPoints';
$result = [
'Download' => 'tests\unit\testData\EntryPoints\Espo\EntryPoints\Download',
'TestEntry' => 'tests\unit\testData\EntryPoints\Espo\EntryPoints\TestEntry',
'InModule' => 'tests\unit\testData\EntryPoints\Espo\Modules\Crm\EntryPoints\InModule',
];
$this->dataCache
->expects($this->once())
->method('has')
->with($cacheKey)
->willReturn(true);
$this->dataCache
->expects($this->once())
->method('get')
->with($cacheKey)
->willReturn(null);
$this->assertEquals(
$result,
$this->classMap->getData('EntryPoints', $cacheKey, ['run'])
);
}
}

View File

@@ -0,0 +1,558 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace tests\unit\Espo\Core\Utils\File;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use tests\unit\ReflectionHelper;
use Espo\Core\Utils\Util;
use Espo\Core\Utils\File\Manager as FileManager;
class ManagerTest extends TestCase
{
/**
* @var FileManager
*/
private $fileManager;
protected $filesPath = 'tests/unit/testData/FileManager';
protected const CACHE_PATH = 'tests/unit/testData/cache/FileManager';
protected $reflection;
protected function setUp(): void
{
$this->fileManager = new FileManager();
$this->reflection = new ReflectionHelper($this->fileManager);
}
public function testGetFileName()
{
$this->assertEquals('Donwload', $this->fileManager->getFileName('Donwload.php'));
$this->assertEquals('Donwload', $this->fileManager->getFileName('/Donwload.php'));
$this->assertEquals('Donwload', $this->fileManager->getFileName('\Donwload.php'));
$this->assertEquals('Donwload', $this->fileManager->getFileName('application/Espo/EntryPoints/Donwload.php'));
}
public function testGetContents()
{
$result = file_get_contents($this->filesPath . '/getContent/test.json');
$this->assertEquals(
$result,
$this->fileManager->getContents($this->filesPath . '/getContent/test.json')
);
}
public function testPutContents()
{
$testPath = self::CACHE_PATH;
$result= 'next value';
$this->assertTrue(
$this->fileManager->putContents($testPath . '/setContent.json', $result)
);
$this->assertEquals(
$result,
$this->fileManager->getContents($testPath . '/setContent.json')
);
@unlink($testPath . '/setContent.json');
}
public function testGetDirName()
{
$input = 'data/logs/espo.log';
$result = 'logs';
$this->assertEquals($result, $this->fileManager->getDirName($input, false));
$input = 'data/logs/espo.log/';
$result = 'logs';
$this->assertEquals($result, $this->fileManager->getDirName($input, false));
$input = 'application/Espo/Resources/metadata/entityDefs';
$result = 'entityDefs';
$this->assertEquals($result, $this->fileManager->getDirName($input, false));
$input = 'application/Espo/Resources/metadata/entityDefs/';
$result = 'entityDefs';
$this->assertEquals($result, $this->fileManager->getDirName($input, false));
//path doesn't exists. Be careful to use "/" at the beginning
$input = '/application/Espo/Resources/metadata/entityDefs';
$result = 'metadata';
$this->assertEquals($result, $this->fileManager->getDirName($input, false));
$input = 'notRealPath/logs/espo.log';
$result = 'logs';
$this->assertEquals($result, $this->fileManager->getDirName($input, false));
$input = $this->filesPath . '/getContent';
$result = 'getContent';
$this->assertEquals($result, $this->fileManager->getDirName($input, false));
}
public function testGetDirNameFullPath()
{
$input = 'data/logs/espo.log';
$result = 'data/logs';
$this->assertEquals($result, $this->fileManager->getDirName($input));
$input = 'data/logs/espo.log/';
$result = 'data/logs';
$this->assertEquals($result, $this->fileManager->getDirName($input));
$input = 'application/Espo/Resources/metadata/entityDefs';
$result = 'application/Espo/Resources/metadata/entityDefs';
$this->assertEquals($result, $this->fileManager->getDirName($input));
$input = 'application/Espo/Resources/metadata/entityDefs/';
$result = 'application/Espo/Resources/metadata/entityDefs';
$this->assertEquals($result, $this->fileManager->getDirName($input));
//path doesn't exists. Be careful to use "/" at the beginning
$input = '/application/Espo/Resources/metadata/entityDefs';
$result = '/application/Espo/Resources/metadata';
$this->assertEquals($result, $this->fileManager->getDirName($input));
$input = 'notRealPath/logs/espo.log';
$result = 'notRealPath/logs';
$this->assertEquals($result, $this->fileManager->getDirName($input));
$input = $this->filesPath . '/getContent';
$result = $this->filesPath . '/getContent';
$this->assertEquals($result, $this->fileManager->getDirName($input, true));
}
public function testUnsetContents()
{
$testPath = self::CACHE_PATH.'/unsets.json';
$initData = '{"fields":{"someName":{"type":"varchar","maxLength":40},"someName2":{"type":"varchar","maxLength":36}}}';
$this->fileManager->putContents($testPath, $initData);
$unsets = ['fields.someName2'];
$this->assertTrue($this->fileManager->unsetJsonContents($testPath, $unsets));
$result = '{"fields":{"someName":{"type":"varchar","maxLength":40}}}';
$this->assertJsonStringEqualsJsonFile($testPath, $result);
}
public function testIsDirEmpty()
{
$this->assertFalse($this->fileManager->isDirEmpty('application'));
$this->assertFalse($this->fileManager->isDirEmpty('tests/unit/Espo'));
$this->assertFalse($this->fileManager->isDirEmpty('tests/unit/Espo/Core/Utils/File'));
$dirPath = 'tests/unit/testData/cache/EmptyDir';
if (file_exists($dirPath) || mkdir($dirPath, 0755)) {
$this->assertTrue($this->fileManager->isDirEmpty($dirPath));
}
}
public function testGetParentDirName()
{
$input = 'application/Espo/Resources/metadata/entityDefs';
$result = 'metadata';
$this->assertEquals($result, $this->fileManager->getParentDirName($input, false));
$input = 'application/Espo/Resources/metadata/entityDefs/';
$result = 'metadata';
$this->assertEquals($result, $this->fileManager->getParentDirName($input, false));
//path doesn't exists. Be careful to use "/" at the beginning
$input = '/application/Espo/Resources/metadata/entityDefs';
$result = 'metadata';
$this->assertEquals($result, $this->fileManager->getParentDirName($input, false));
//path doesn't exists. Be careful to use "/" at the beginning
$input = '/application/Espo/Resources/metadata/entityDefs';
$result = '/application/Espo/Resources/metadata';
$this->assertEquals($result, $this->fileManager->getParentDirName($input));
$input = 'notRealPath/logs/espo.log';
$result = 'notRealPath/logs';
$this->assertEquals($result, $this->fileManager->getParentDirName($input));
$input = $this->filesPath . '/getContent';
$result = $this->filesPath;
$this->assertEquals($result, $this->fileManager->getParentDirName($input, true));
}
public function testGetSingleFileListAll()
{
$input = array (
'custom' =>
array (
'Espo' =>
array (
'Custom' =>
array (
'Modules' =>
array (
'ExtensionTest' =>
array (
0 => 'File.json',
1 => 'File.php',
),
),
),
),
),
);
$result = array (
'custom',
'custom/Espo',
'custom/Espo/Custom',
'custom/Espo/Custom/Modules',
'custom/Espo/Custom/Modules/ExtensionTest',
'custom/Espo/Custom/Modules/ExtensionTest/File.json',
'custom/Espo/Custom/Modules/ExtensionTest/File.php',
);
$result = array_map('\Espo\Core\Utils\Util::fixPath', $result);
$this->assertEquals($result, $this->reflection->invokeMethod('getSingleFileList', array($input)));
}
public function testGetSingeFileListOnlyFiles()
{
$input = array (
'custom' =>
array (
'Espo' =>
array (
'Custom' =>
array (
'Modules' =>
array (
'ExtensionTest' =>
array (
0 => 'File.json',
1 => 'File.php',
),
),
),
),
),
);
$result = array (
'custom/Espo/Custom/Modules/ExtensionTest/File.json',
'custom/Espo/Custom/Modules/ExtensionTest/File.php',
);
$result = array_map('\Espo\Core\Utils\Util::fixPath', $result);
$this->assertEquals($result, $this->reflection->invokeMethod('getSingleFileList', array($input, true)));
}
public function testGetSingeFileListOnlyDirs()
{
$input = array (
'custom' =>
array (
'Espo' =>
array (
'Custom' =>
array (
'Modules' =>
array (
'ExtensionTest' =>
array (
0 => 'File.json',
1 => 'File.php',
),
),
),
),
),
);
$result = array (
'custom',
'custom/Espo',
'custom/Espo/Custom',
'custom/Espo/Custom/Modules',
'custom/Espo/Custom/Modules/ExtensionTest',
);
$result = array_map('\Espo\Core\Utils\Util::fixPath', $result);
$this->assertEquals($result, $this->reflection->invokeMethod('getSingleFileList', array($input, false)));
}
static public function fileListSets()
{
return [
['Set1', [
'custom',
'custom/Espo',
'custom/Espo/Custom',
'custom/Espo/Custom/Modules',
'custom/Espo/Custom/Modules/TestModule',
'custom/Espo/Custom/Modules/TestModule/SubFolder',
'custom/Espo/Custom/Modules/TestModule/SubFolder/Tester.txt',
]
],
['Set2', [
'custom',
'custom/Espo',
'custom/Espo/Custom',
'custom/Espo/Custom/Resources',
'custom/Espo/Custom/Resources/metadata',
'custom/Espo/Custom/Resources/metadata/entityDefs',
'custom/Espo/Custom/Resources/metadata/entityDefs/Account.json',
]
],
['Set3', [
'custom',
'custom/test.file',
]
],
];
}
#[DataProvider('fileListSets')]
public function testRemoveWithEmptyDirs($name, $result)
{
$path = Util::fixPath($this->filesPath . '/Remove/' . $name);
$cachePath = Util::fixPath(self::CACHE_PATH . '/' . $name);
$result = array_map('\Espo\Core\Utils\Util::fixPath', $result);
$fileList = array (
$cachePath . '/custom/Espo/Custom/Modules/ExtensionTest/File.json',
$cachePath . '/custom/Espo/Custom/Modules/ExtensionTest/File.php',
);
$fileList = array_map('\Espo\Core\Utils\Util::fixPath', $fileList);
$res = $this->fileManager->copy($path, $cachePath, true);
if ($res) {
$this->assertTrue($this->fileManager->remove($fileList, null, true));
$this->assertEquals($result, $this->fileManager->getFileList($cachePath, true, '', null, true));
}
}
static public function existsPathSet()
{
return [
['application/Espo/Core/Application.php', 'application/Espo/Core/Application.php',],
['application/Espo/Core/NotRealApplication.php', 'application/Espo/Core'],
['application/Espo/Core/NotRealApplication.php', 'application/Espo/Core'],
['application/NoEspo/Core/Application.php', 'application'],
['notRealPath/Espo/Core/Application.php', '.'],
];
}
#[DataProvider('existsPathSet')]
public function testGetExistsPath($input, $result)
{
$this->assertEquals(
$result,
$this->reflection->invokeMethod('getExistsPath', [$input])
);
}
public function testCopyTestCase1()
{
$path = Util::fixPath($this->filesPath . '/copy/testCase1');
$cachePath = Util::fixPath(self::CACHE_PATH . '/copy/testCase1');
$expectedResult = [
'custom/Espo/Custom/Modules/ExtensionTest/File.json',
'custom/Espo/Custom/Modules/ExtensionTest/File.php',
'custom/Espo/Custom/Modules/TestModule/SubFolder/Tester.txt',
];
$expectedResult = array_map('\Espo\Core\Utils\Util::fixPath', $expectedResult);
$result = $this->fileManager->copy($path, $cachePath, true);
if ($result) {
$this->assertEquals($expectedResult, $this->fileManager->getFileList($cachePath, true, '', true, true));
$this->fileManager->removeInDir($cachePath);
}
}
public function testCopyTestCase2()
{
$path = Util::fixPath($this->filesPath . '/copy/testCase2');
$cachePath = Util::fixPath(self::CACHE_PATH . '/copy/testCase2');
$expectedResult = [
'custom/Espo/Custom/test1.php',
'data/test2.php',
'data/upload/5a86d9bf1154968dc',
'test0.php'
];
$expectedResult = array_map('\Espo\Core\Utils\Util::fixPath', $expectedResult);
$result = $this->fileManager->copy($path, $cachePath, true);
if ($result) {
$this->assertEquals($expectedResult, $this->fileManager->getFileList($cachePath, true, '', true, true));
$this->fileManager->removeInDir($cachePath);
}
}
public function testCopyTestCase3()
{
$path = Util::fixPath($this->filesPath . '/copy/testCase3');
$cachePath = Util::fixPath(self::CACHE_PATH . '/copy/testCase3');
$expectedResult = [
'custom/Espo/Custom/test1.php',
'data/test2.php',
'data/upload/5a86d9bf1154968dc',
'test0.php'
];
$expectedResult = array_map('\Espo\Core\Utils\Util::fixPath', $expectedResult);
$fileList = $this->fileManager->getFileList($path, true, '', true, true);
$this->assertEquals($expectedResult, $fileList, "Expected Result and File List");
$result = $this->fileManager->copy($path, $cachePath, true, $fileList);
if ($result) {
$this->assertEquals(
$expectedResult,
$this->fileManager->getFileList($cachePath, true, '', true, true),
"Expected Result and List of copied files"
);
$this->fileManager->removeInDir($cachePath);
}
}
public function testCopyTestCase4()
{
$path = Util::fixPath($this->filesPath . '/copy/testCase4');
$cachePath = Util::fixPath(self::CACHE_PATH . '/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'
];
$expectedResult = array_map('\Espo\Core\Utils\Util::fixPath', $expectedResult);
$fileList = $this->fileManager->getFileList($path, true, '', null, true);
$this->assertEquals($expectedResult, $fileList, "Expected Result and File List");
$result = $this->fileManager->copy($path, $cachePath, true, $fileList);
if ($result) {
$this->assertEquals(
$expectedResult,
$this->fileManager->getFileList($cachePath, true, '', null, true),
"Expected Result and List of copied files"
);
$this->fileManager->removeInDir($cachePath);
}
}
static public function relativePathData()
{
$tmpPath = self::CACHE_PATH;
if (!file_exists($tmpPath)) {
mkdir($tmpPath, 0775, true);
}
$tmpFile = tempnam($tmpPath, 'tmp');
$data = [
['data/config.php', 'data/config.php'],
[realpath('vendor/autoload.php'), 'vendor/autoload.php'],
[$tmpFile, $tmpPath . '/' . basename($tmpFile)],
[realpath('application/Espo/Core'), 'application/Espo/Core'],
[realpath('application/Espo/Core') . '/', 'application/Espo/Core/'],
[realpath('application/Espo/Core/Application.php'), 'application/Espo/Core/Application.php'],
['C:\\espocrm\\data\\config.php', 'data\\config.php', 'C:\\espocrm', '\\'],
['C:espocrm\\data\\config.php', 'data\\config.php', 'C:espocrm', '\\'],
['C:\\espocrm\\data\\tmp\\' . basename($tmpFile), 'data\\tmp\\' . basename($tmpFile), 'C:\\espocrm', '\\'],
];
@unlink($tmpFile);
return $data;
}
#[DataProvider('relativePathData')]
public function testGetRelativePath($path, $expectedResult, $basePath = null, $dirSeparator = null)
{
$this->assertEquals(
Util::fixPath($expectedResult),
$this->fileManager->getRelativePath($path, $basePath, $dirSeparator)
);
}
public static function reservedCustomPathData(): array
{
return [
['custom/Espo/Custom', true],
['custom/Espo/Modules', true],
['client/custom/modules', true],
['client/custom/modules/', true],
['custom/Espo/Custom/Resources', false],
['custom/Espo/Modules/Test', false],
['client/custom/modules/test', false],
['application/Espo/Core', false],
];
}
#[DataProvider('reservedCustomPathData')]
public function testIsReservedCustomPath(string $path, bool $expectedResult): void
{
$this->assertEquals(
$expectedResult,
$this->reflection->invokeMethod('isReservedCustomPath', [$path])
);
}
}

View File

@@ -0,0 +1,65 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace tests\unit\Espo\Core\Utils\File;
use Espo\Core\Utils\File\MimeType;
use Espo\Core\Utils\Metadata;
class MimeTypeTest extends \PHPUnit\Framework\TestCase
{
private Metadata $metadata;
protected function setUp(): void
{
$this->metadata = $this->createMock(Metadata::class);
}
public function testGetMimeTypeByExtension(): void
{
$this->metadata
->expects($this->any())
->method('get')
->with(['app', 'file', 'extensionMimeTypeMap', 'csv'])
->willReturn(['text/csv']);
$util = new MimeType($this->metadata);
$this->assertEquals('text/csv', $util->getMimeTypeByExtension('csv'));
$this->assertEquals('text/csv', $util->getMimeTypeByExtension('CSV'));
}
public function testMatchMimeTypeToAcceptToken(): void
{
$this->assertTrue(MimeType::matchMimeTypeToAcceptToken('text/csv', 'text/csv'));
$this->assertFalse(MimeType::matchMimeTypeToAcceptToken('text/csv', 'text/plain'));
$this->assertTrue(MimeType::matchMimeTypeToAcceptToken('video/mpeg', 'video/*'));
$this->assertFalse(MimeType::matchMimeTypeToAcceptToken('video/mpeg', 'image/*'));
}
}

View File

@@ -0,0 +1,127 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace tests\unit\Espo\Core\Utils\File;
use Espo\Core\Utils\File\Permission;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use tests\unit\ReflectionHelper;
class PermissionTest extends TestCase
{
protected $object;
protected $objects;
protected $reflection;
protected $fileList;
protected function setUp() : void
{
$this->objects['fileManager'] = $this->getMockBuilder('\Espo\Core\Utils\File\Manager')->disableOriginalConstructor()->getMock();
$this->object = new Permission($this->objects['fileManager']);
$this->reflection = new ReflectionHelper($this->object);
$this->fileList = [
'application/Espo/Controllers/Email.php',
'application/Espo/Controllers/EmailAccount.php',
'application/Espo/Controllers/EmailAddress.php',
'application/Espo/Controllers/ExternalAccount.php',
'application/Espo/Controllers/Import.php',
'application/Espo/Controllers/Integration.php',
'application/Espo/Modules/Crm/Resources/i18n/pl_PL/Calendar.json',
'application/Espo/Modules/Crm/Resources/i18n/pl_PL/Call.json',
'application/Espo/Modules/Crm/Resources/i18n/pl_PL/Case.json',
'application/Espo/Modules/Crm/Resources/i18n/pl_PL/Contact.json',
'application/Espo/Modules/Crm/Resources/i18n/pl_PL/Global.json',
'application/Espo/Resources/layouts/User/filters.json',
'application/Espo/Resources/metadata/app/acl.json',
'application/Espo/Resources/metadata/app/defaultDashboardLayout.json'
];
}
protected function tearDown() : void
{
$this->object = NULL;
}
public function testGetSearchCount()
{
$search = 'application/Espo/Controllers/';
$methodResult = $this->reflection->invokeMethod('getSearchCount', array($search, $this->fileList));
$result = 6;
$this->assertEquals($result, $methodResult);
$search = 'application/Espo/Controllers/Email.php';
$methodResult = $this->reflection->invokeMethod('getSearchCount', array($search, $this->fileList));
$result = 1;
$this->assertEquals($result, $methodResult);
$search = 'application/Espo/Controllers/NotReal';
$methodResult = $this->reflection->invokeMethod('getSearchCount', array($search, $this->fileList));
$result = 0;
$this->assertEquals($result, $methodResult);
}
public function testArrangePermissionList()
{
$result = array(
'application/Espo/Controllers',
'application/Espo/Modules/Crm/Resources/i18n/pl_PL',
'application/Espo/Resources/layouts/User/filters.json',
'application/Espo/Resources/metadata/app',
);
$this->assertEquals( $result, $this->object->arrangePermissionList($this->fileList) );
}
static public function requiredPermissionsData()
{
return [
['data/config.php', '0775', '0664'],
['data/tmp/tmpivqW1X', '0775', '0664'],
['application/Espo/Core/Application.php', '0755', '0644'],
['custom/Espo/Custom/Resources/metadata/entityDefs', '0775', '0664'],
['custom/Espo/Custom/Resources/metadata/entityDefs/Account.json', '0775', '0664'],
['custom/Espo/Modules', '0775', '0664'],
['client/modules/crm/src', '0755', '0644'],
['client/modules/crm/src/views/account/detail.js', '0755', '0644'],
];
}
#[DataProvider('requiredPermissionsData')]
public function testGetRequiredPermissions($path, $dirPermission, $filePermission)
{
$result = $this->object->getRequiredPermissions($path);
$this->assertEquals($dirPermission, $result['dir']);
$this->assertEquals($filePermission, $result['file']);
}
}