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,105 @@
<?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\Select\Where;
use Espo\Core\Utils\Acl\UserAclManagerProvider;
use Espo\Core\Acl;
use Espo\Core\AclManager;
use Espo\Core\InjectableFactory;
use Espo\Core\Select\Where\Checker;
use Espo\Core\Select\Where\CheckerFactory;
use Espo\Entities\User;
use PHPUnit\Framework\TestCase;
class CheckerFactoryTest extends TestCase
{
private $injectableFactory;
private $user;
private $acl;
private $factory;
protected function setUp(): void
{
$this->injectableFactory = $this->createMock(InjectableFactory::class);
$aclManager = $this->createMock(AclManager::class);
$this->user = $this->createMock(User::class);
$this->acl = $this->createMock(Acl::class);
$userAclFilterResolver = $this->createMock(UserAclManagerProvider::class);
$aclManager
->expects($this->any())
->method('createUserAcl')
->with($this->user)
->willReturn($this->acl);
$userAclFilterResolver
->expects($this->any())
->method('get')
->with($this->user)
->willReturn($aclManager);
$this->factory = new CheckerFactory(
$this->injectableFactory,
$userAclFilterResolver
);
}
public function testCreate1()
{
$this->prepareFactoryTest();
}
protected function prepareFactoryTest()
{
$entityType = 'Test';
$object = $this->createMock(Checker::class);
$this->injectableFactory
->expects($this->once())
->method('createWith')
->with(
Checker::class,
[
'entityType' => $entityType,
'acl' => $this->acl,
]
)
->willReturn($object);
$resultObject = $this->factory->create(
$entityType,
$this->user
);
$this->assertEquals($object, $resultObject);
}
}

View File

@@ -0,0 +1,525 @@
<?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\Select\Where;
use Espo\Core\Acl;
use Espo\Core\Acl\Table;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Select\Helpers\EntityHelper;
use Espo\Core\Select\Where\Checker;
use Espo\Core\Select\Where\Item;
use Espo\Core\Select\Where\Params;
use Espo\ORM\BaseEntity as Entity;
use Espo\ORM\EntityManager;
use PHPUnit\Framework\TestCase;
class CheckerTest extends TestCase
{
/** @var Checker|null */
protected $checker = null;
protected ?EntityManager $entityManager = null;
/** @var Acl|null */
protected $acl = null;
protected ?EntityHelper $entityHelper = null;
protected ?string $entityType = null;
protected ?string $foreignEntityType = null;
private $entity;
private $params;
protected function setUp() : void
{
$this->entityManager = $this->createMock(EntityManager::class);
$this->acl = $this->createMock(Acl::class);
$systemRestriction = $this->createMock(Acl\SystemRestriction::class);
$this->entityHelper = $this->createMock(EntityHelper::class);
$systemRestriction
->method('checkAttributeRead')
->willReturn(true);
$systemRestriction
->method('checkLinkRead')
->willReturn(true);
$systemRestriction
->method('checkFieldRead')
->willReturn(true);
$this->entityType = 'Test';
$this->foreignEntityType = 'TestForeign';
$this->checker = new Checker(
entityType: $this->entityType,
entityManager: $this->entityManager,
acl: $this->acl,
systemRestriction: $systemRestriction,
entityHelper: $this->entityHelper,
);
$this->params = $this->createMock(Params::class);
$this->entity = $this->createMock(Entity::class);
$foreignEntity = $this->createMock(Entity::class);
$this->entityManager
->expects($this->any())
->method('getNewEntity')
->willReturnMap([
[$this->entityType, $this->entity],
[$this->foreignEntityType, $foreignEntity],
]);
}
/** @noinspection PhpUnhandledExceptionInspection */
public function testAttributeExistence1()
{
$this->params
->expects($this->any())
->method('forbidComplexExpressions')
->willReturn(false);
$this->params
->expects($this->any())
->method('applyPermissionCheck')
->willReturn(false);
$item = Item::fromRaw([
'type' => 'and',
'value' => [
[
'type' => 'equals',
'attribute' => 'test1',
'value' => 'value1',
],
[
'type' => 'equals',
'attribute' => 'TEST:(test2)',
'value' => 'value2',
],
[
'type' => 'linkedWith',
'attribute' => 'test3',
'value' => 'value3',
],
],
]);
$this->entity
->expects(self::any())
->method('hasAttribute')
->willReturnMap([
['test1', true],
['test2', true],
]);
$this->entityHelper
->method('getRelationEntityType')
->willReturnMap([
[$this->entity, 'test3', 'AnotherEntity'],
]);
$another = $this->createMock(Entity::class);
$this->entityManager
->method('getEntityById')
->willReturnMap([
['AnotherEntity', 'value3', $another]
]);
$this->entity
->method('hasRelation')
->with('test3')
->willReturn(true);
$this->checker->check($item, $this->params);
}
/** @noinspection PhpUnhandledExceptionInspection */
public function testAttributeExistence2()
{
$this->params
->expects($this->any())
->method('forbidComplexExpressions')
->willReturn(false);
$this->params
->expects($this->any())
->method('applyPermissionCheck')
->willReturn(false);
$item = Item::fromRaw([
'type' => 'and',
'value' => [
[
'type' => 'equals',
'attribute' => 'test1',
'value' => 'value1',
],
],
]);
$this->entity
->expects(self::any())
->method('hasAttribute')
->willReturnMap([
['test1', false]
]);
$this->expectException(BadRequest::class);
$this->checker->check($item, $this->params);
}
/** @noinspection PhpUnhandledExceptionInspection */
public function testAttributeExistence3()
{
$this->params
->expects($this->any())
->method('forbidComplexExpressions')
->willReturn(false);
$this->params
->expects($this->any())
->method('applyPermissionCheck')
->willReturn(false);
$item = Item::fromRaw([
'type' => 'and',
'value' => [
[
'type' => 'linkedWith',
'attribute' => 'test3',
'value' => 'value3',
],
],
]);
$this->entity
->expects($this->once())
->method('hasRelation')
->with('test3')
->willReturn(false);
$this->expectException(BadRequest::class);
$this->checker->check($item, $this->params);
}
/** @noinspection PhpUnhandledExceptionInspection */
public function testPermissions1()
{
$this->params
->expects($this->any())
->method('forbidComplexExpressions')
->willReturn(false);
$this->params
->expects($this->any())
->method('applyPermissionCheck')
->willReturn(true);
$item = Item::fromRaw([
'type' => 'and',
'value' => [
[
'type' => 'equals',
'attribute' => 'test1',
'value' => 'value1',
],
[
'type' => 'equals',
'attribute' => 'test3.test2',
'value' => 'value2',
],
[
'type' => 'linkedWith',
'attribute' => 'test3',
'value' => 'value3',
],
],
]);
$this->entity
->expects($this->any())
->method('hasAttribute')
->willReturn(true);
$this->entity
->expects($this->any())
->method('hasRelation')
->with('test3')
->willReturn(true);
$this->entity
->expects($this->any())
->method('getRelationParam')
->with('test3', 'entity')
->willReturn($this->foreignEntityType);
$this->acl
->expects($this->any())
->method('getScopeForbiddenAttributeList')
->willReturnMap([
[$this->entityType, Table::ACTION_READ, Table::LEVEL_NO, []],
[$this->foreignEntityType, Table::ACTION_READ, Table::LEVEL_NO, []],
]);
$this->acl
->expects($this->any())
->method('getScopeForbiddenFieldList')
->with($this->entityType)
->willReturn([]);
$this->acl
->expects($this->any())
->method('getScopeForbiddenLinkList')
->with($this->entityType)
->willReturn([]);
$this->acl
->expects($this->any())
->method('checkScope')
->with($this->foreignEntityType)
->willReturn(true);
$foreign = $this->createMock(Entity::class);
$this->entityManager
->expects($this->any())
->method('getEntityById')
->willReturnMap([
[$this->foreignEntityType, 'value3', $foreign]
]);
$this->acl
->expects($this->any())
->method('checkEntityRead')
->willReturnMap([
[$foreign, true]
]);
$this->entity
->method('hasRelation')
->with('test3')
->willReturn(true);
$this->entityHelper
->method('getRelationEntityType')
->willReturnMap([
[$this->entity, 'test3', $this->foreignEntityType],
]);
$this->checker->check($item, $this->params);
$this->assertTrue(true);
}
public function testPermissions2()
{
$this->params
->expects($this->any())
->method('forbidComplexExpressions')
->willReturn(false);
$this->params
->expects($this->any())
->method('applyPermissionCheck')
->willReturn(true);
$item = Item::fromRaw([
'type' => 'and',
'value' => [
[
'type' => 'equals',
'attribute' => 'test1',
'value' => 'value1',
],
],
]);
$this->entity
->expects($this->any())
->method('hasAttribute')
->willReturn(true);
$this->entity
->expects($this->any())
->method('hasRelation')
->with('test3')
->willReturn(true);
$this->acl
->expects($this->any())
->method('getScopeForbiddenAttributeList')
->with($this->entityType)
->willReturn(['test1']);
$this->expectException(Forbidden::class);
/** @noinspection PhpUnhandledExceptionInspection */
$this->checker->check($item, $this->params);
}
/** @noinspection PhpUnhandledExceptionInspection */
public function testPermissions3()
{
$this->params
->expects($this->any())
->method('forbidComplexExpressions')
->willReturn(false);
$this->params
->expects($this->any())
->method('applyPermissionCheck')
->willReturn(true);
$item = Item::fromRaw([
'type' => 'and',
'value' => [
[
'type' => 'equals',
'attribute' => 'test3.test2',
'value' => 'value2',
],
],
]);
$this->entity
->expects($this->any())
->method('hasAttribute')
->willReturn(true);
$this->entity
->expects($this->any())
->method('hasRelation')
->with('test3')
->willReturn(true);
$this->entity
->expects($this->any())
->method('getRelationParam')
->with('test3', 'entity')
->willReturn($this->foreignEntityType);
$this->acl
->expects($this->any())
->method('checkScope')
->with($this->foreignEntityType)
->willReturn(false);
$this->expectException(Forbidden::class);
$this->checker->check($item, $this->params);
}
/** @noinspection PhpUnhandledExceptionInspection */
public function testPermissions4()
{
$this->params
->expects($this->any())
->method('forbidComplexExpressions')
->willReturn(false);
$this->params
->expects($this->any())
->method('applyPermissionCheck')
->willReturn(true);
$item = Item::fromRaw([
'type' => 'and',
'value' => [
[
'type' => 'linkedWith',
'attribute' => 'test3',
'value' => 'value3',
],
],
]);
$this->entity
->expects($this->any())
->method('hasAttribute')
->willReturn(true);
$this->entity
->expects($this->any())
->method('hasRelation')
->with('test3')
->willReturn(true);
$this->entity
->expects($this->any())
->method('getRelationParam')
->with('test3', 'entity')
->willReturn($this->foreignEntityType);
$this->acl
->expects($this->any())
->method('checkScope')
->with($this->foreignEntityType)
->willReturn(false);
$this->expectException(Forbidden::class);
$this->checker->check($item, $this->params);
}
/** @noinspection PhpUnhandledExceptionInspection */
public function testComplexExpressions1()
{
$this->params
->expects($this->any())
->method('forbidComplexExpressions')
->willReturn(true);
$this->params
->expects($this->any())
->method('applyPermissionCheck')
->willReturn(false);
$item = Item::fromRaw([
'type' => 'and',
'value' => [
[
'type' => 'equals',
'attribute' => 'TEST:(test2)',
'value' => 'value2',
]
],
]);
$this->expectException(Forbidden::class);
$this->checker->check($item, $this->params);
}
}

View File

@@ -0,0 +1,180 @@
<?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\Select\Where;
use Espo\Core\Binding\Binder;
use Espo\Core\Binding\BindingContainer;
use Espo\Core\Binding\BindingData;
use Espo\Core\InjectableFactory;
use Espo\Core\Select\Where\Converter;
use Espo\Core\Select\Where\ConverterFactory;
use Espo\Core\Select\Where\DateTimeItemTransformer;
use Espo\Core\Select\Where\DefaultDateTimeItemTransformer;
use Espo\Core\Select\Where\ItemConverter;
use Espo\Core\Select\Where\ItemGeneralConverter;
use Espo\Core\Utils\Metadata;
use Espo\Entities\User;
use PHPUnit\Framework\TestCase;
use RuntimeException;
class ConverterFactoryTest extends TestCase
{
private $injectableFactory;
private $metadata;
private $user;
private $factory;
private $itemConverter;
private $dateTimeItemTransformer;
protected function setUp(): void
{
$this->injectableFactory = $this->createMock(InjectableFactory::class);
$this->metadata = $this->createMock(Metadata::class);
$this->user = $this->createMock(User::class);
$this->factory = new ConverterFactory(
$this->injectableFactory,
$this->metadata
);
$this->itemConverter = $this->createMock(ItemGeneralConverter::class);
$this->dateTimeItemTransformer = $this->createMock(DefaultDateTimeItemTransformer::class);
}
public function testCreate1()
{
$this->prepareFactoryTest(null, null, null);
}
public function testCreate2()
{
$this->prepareFactoryTest('SomeClass1', 'SomeClass2', 'SomeClass3');
}
protected function prepareFactoryTest(?string $className1, ?string $className2, ?string $className3)
{
$entityType = 'Test';
$this->metadata
->expects($this->any())
->method('get')
->willReturnMap([
[['selectDefs', $entityType, 'whereDateTimeItemTransformerClassName'], null, $className1],
[['selectDefs', $entityType, 'whereItemConverterClassName'], null, $className2],
[['selectDefs', $entityType, 'whereConverterClassName'], null, $className3],
]);
$className1 = $className1 ?? DefaultDateTimeItemTransformer::class;
$className2 = $className2 ?? ItemGeneralConverter::class;
$className3 = $className3 ?? Converter::class;
$object = $this->createMock(Converter::class);
$bindingData1 = new BindingData();
$binder1 = new Binder($bindingData1);
$binder1
->bindInstance(User::class, $this->user);
$binder1
->for($className1)
->bindValue('$entityType', $entityType);
$binder1
->for(DefaultDateTimeItemTransformer::class)
->bindValue('$entityType', $entityType);
$bc1 = new BindingContainer($bindingData1);
$bindingData2 = new BindingData();
$binder2 = new Binder($bindingData2);
$binder2
->bindInstance(User::class, $this->user);
$binder2
->for($className2)
->bindValue('$entityType', $entityType)
->bindInstance(DateTimeItemTransformer::class, $this->dateTimeItemTransformer);
$binder2
->for(ItemGeneralConverter::class)
->bindValue('$entityType', $entityType)
->bindInstance(DateTimeItemTransformer::class, $this->dateTimeItemTransformer);
$bc2 = new BindingContainer($bindingData2);
$bindingData3 = new BindingData();
$binder3 = new Binder($bindingData3);
$binder3
->bindInstance(User::class, $this->user)
->for($className3)
->bindValue('$entityType', $entityType)
->bindInstance(ItemConverter::class, $this->itemConverter);
$bc3 = new BindingContainer($bindingData3);
$c = $this->exactly(3);
$this->injectableFactory
->expects($c)
->method('createWithBinding')
->willReturnCallback(function ($className, $bc) use
($c, $object, $className1, $className2, $className3, $bc1, $bc2, $bc3) {
if ($c->numberOfInvocations() === 1) {
$this->assertEquals($className1, $className);
$this->assertEquals($bc1, $bc);
return $this->dateTimeItemTransformer;
}
if ($c->numberOfInvocations() === 2) {
$this->assertEquals($className2, $className);
$this->assertEquals($bc2, $bc);
return $this->itemConverter;
}
if ($c->numberOfInvocations() === 3) {
$this->assertEquals($className3, $className);
$this->assertEquals($bc3, $bc);
return $object;
}
throw new RuntimeException();
});
$resultObject = $this->factory->create($entityType, $this->user);
$this->assertEquals($object, $resultObject);
}
}

View File

@@ -0,0 +1,447 @@
<?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\Select\Where;
use Espo\Core\Select\Helpers\RandomStringGenerator;
use Espo\Core\Select\Where\Converter;
use Espo\Core\Select\Where\DefaultDateTimeItemTransformer;
use Espo\Core\Select\Where\Item;
use Espo\Core\Select\Where\ItemConverter;
use Espo\Core\Select\Where\ItemConverterFactory;
use Espo\Core\Select\Where\ItemGeneralConverter;
use Espo\Core\Select\Where\Scanner;
use Espo\Core\Utils\Config;
use Espo\Core\Utils\Metadata;
use Espo\Entities\User;
use Espo\ORM\Defs as ORMDefs;
use Espo\ORM\Defs\EntityDefs;
use Espo\ORM\Entity;
use Espo\ORM\EntityManager;
use Espo\ORM\Metadata as ormMetadata;
use Espo\ORM\Query\Part\Join\JoinType;
use Espo\ORM\Query\Part\WhereClause;
use Espo\ORM\Query\Select;
use Espo\ORM\Query\SelectBuilder as QueryBuilder;
use Espo\ORM\QueryBuilder as BaseQueryBuilder;
use PHPUnit\Framework\TestCase;
class ConverterTest extends TestCase
{
private $entityType;
private $user;
private $scanner;
private $itemConverterFactory;
private $ormDefs;
private $queryBuilder;
private $converter;
protected function setUp() : void
{
$this->entityType = 'Test';
$this->user = $this->createMock(User::class);
$config = $this->createMock(Config::class);
$applicationConfig = $this->createMock(Config\ApplicationConfig::class);
$metadata = $this->createMock(Metadata::class);
$this->scanner = $this->createMock(Scanner::class);
$randomStringGenerator = $this->createMock(RandomStringGenerator::class);
$this->itemConverterFactory = $this->createMock(ItemConverterFactory::class);
$entityManager = $this->createMock(EntityManager::class);
$ormMetadata = $this->createMock(ormMetadata::class);
$baseQueryBuilder = $this->createMock(BaseQueryBuilder::class);
$entityManager
->expects($this->any())
->method('getMetadata')
->willReturn($ormMetadata);
$entityManager
->expects($this->any())
->method('getQueryBuilder')
->willReturn($baseQueryBuilder);
$this->ormDefs = $this->createMock(ORMDefs::class);
$this->queryBuilder = $this->createMock(QueryBuilder::class);
$randomStringGenerator
->expects($this->any())
->method('generate')
->willReturn('Random');
$applicationConfig
->expects($this->any())
->method('getTimeZone')
->willReturn('UTC');
$dateTimeItemTransformer = new DefaultDateTimeItemTransformer($config, $applicationConfig);
$itemConverter = new ItemGeneralConverter(
$this->entityType,
$this->user,
$dateTimeItemTransformer,
$this->scanner,
$this->itemConverterFactory,
$randomStringGenerator,
$this->ormDefs,
$config,
$metadata,
$applicationConfig,
);
$this->converter = new Converter(
$itemConverter,
$this->scanner
);
}
public function testConvertApplyLeftJoins()
{
$item = Item::fromRaw([
'type' => 'and',
'value' => [
],
]);
$this->scanner
->expects($this->once())
->method('apply')
->with($this->queryBuilder, $item);
$this->converter->convert($this->queryBuilder, $item);
}
public function testConvertEquals1()
{
$item = Item::fromRaw([
'type' => 'and',
'value' => [
[
'type' => 'equals',
'attribute' => 'test',
'value' => 'test-value',
],
],
]);
$whereClause = $this->converter->convert($this->queryBuilder, $item);
$expected = [
'test=' => 'test-value',
];
$this->assertEquals($expected, $whereClause->getRaw());
}
public function testConvertEquals2()
{
$item = Item::fromRaw([
'type' => 'and',
'value' => [
[
'type' => 'equals',
'attribute' => 'test1',
'value' => 'value1',
],
[
'type' => 'equals',
'attribute' => 'test2',
'value' => 'value2',
],
],
]);
$whereClause = $this->converter->convert($this->queryBuilder, $item);
$expected = [
['test1=' => 'value1'],
['test2=' => 'value2'],
];
$this->assertEquals($expected, $whereClause->getRaw());
}
public function testConvertOr()
{
$item = Item::fromRaw([
'type' => 'and',
'value' => [
[
'type' => 'or',
'value' => [
[
'type' => 'equals',
'attribute' => 'test1',
'value' => 'value1',
],
[
'type' => 'notEquals',
'attribute' => 'test2',
'value' => 'value2',
],
],
],
],
]);
$whereClause = $this->converter->convert($this->queryBuilder, $item);
$expected = [
'OR' => [
['test1=' => 'value1'],
['test2!=' => 'value2'],
],
];
$this->assertEquals($expected, $whereClause->getRaw());
}
public function testConvertDateTimeOn1()
{
$item = Item::fromRaw([
'type' => 'and',
'value' => [
[
'type' => 'on',
'attribute' => 'test',
'value' => '2020-12-20',
'dateTime' => true,
'timeZone' => 'Europe/Kyiv',
],
],
]);
$whereClause = $this->converter->convert($this->queryBuilder, $item);
$expected = [
'AND' => [
'test>=' => '2020-12-19 22:00:00',
'test<=' => '2020-12-20 21:59:59',
],
];
$this->assertEquals($expected, $whereClause->getRaw());
}
public function testConvertSubQueryIn()
{
$item = Item::fromRaw([
'type' => 'and',
'value' => [
[
'type' => 'subQueryIn',
'value' => [
[
'type' => 'equals',
'attribute' => 'test1',
'value' => 'value1',
],
],
],
],
]);
$sqItem = Item::fromRaw([
'type' => 'and',
'value' => [
[
'type' => 'equals',
'attribute' => 'test1',
'value' => 'value1',
],
],
]);
$c = $this->exactly(2);
$this->scanner
->expects($c)
->method('apply')
->willReturnCallback(function ($qb, $it) use ($c, $sqItem, $item) {
if ($c->numberOfInvocations() === 1) {
$this->assertEquals($sqItem, $it);
}
if ($c->numberOfInvocations() === 2) {
$this->assertEquals($item, $it);
}
});
$whereClause = $this->converter->convert($this->queryBuilder, $item);
$expected = [
'id=s' => Select::fromRaw([
'select' => ['id'],
'from' => $this->entityType,
'whereClause' => [
'AND' => [
['test1=' => 'value1'],
],
],
'leftJoins' => [],
'joins' => [],
]),
];
$this->assertEquals($expected, $whereClause->getRaw());
}
public function testConvertLinkedWith1()
{
$link = 'test';
$value = ['value-id'];
$item = Item::fromRaw([
'type' => 'and',
'value' => [
[
'type' => 'linkedWith',
'attribute' => $link,
'value' => $value,
],
],
]);
$this->ormDefs
->expects($this->any())
->method('getEntity')
->with($this->entityType)
->willReturn(
EntityDefs::fromRaw(
[
'relations' => [
$link => [
'type' => Entity::MANY_MANY,
'entity' => 'Foreign',
'midKeys' => ['localId', 'foreignId'],
'relationName' => 'entityEntity'
],
],
],
$this->entityType
)
);
$alias = $link . 'LinkedWithFilterRandom';
$this->queryBuilder
->expects($this->never())
->method('distinct');
$expected = [
'id=s' => Select::fromRaw([
'select' => ['id'],
'from' => 'Test',
'joins' => [
[
'test',
$alias,
null,
[
'onlyMiddle' => true,
'type' => JoinType::left,
],
],
],
'whereClause' => [
$alias . '.foreignId' => $value,
],
]),
];
$whereClause = $this->converter->convert($this->queryBuilder, $item);
$this->assertEquals($expected, $whereClause->getRaw());
}
public function testConvertCustomConverter()
{
$type = 'testType';
$attribute = 'testAttribute';
$item = Item::fromRaw([
'type' => 'and',
'value' => [
[
'type' => $type,
'attribute' => $attribute,
'value' => 'test-value',
],
],
]);
$subItem = Item::fromRaw([
'type' => $type,
'attribute' => $attribute,
'value' => 'test-value',
]);
$converter = $this->createMock(ItemConverter::class);
$converter
->expects($this->once())
->method('convert')
->with($this->queryBuilder, $subItem)
->willReturn(
WhereClause::fromRaw([
'testAnother=' => 'test-value',
])
);
$this->itemConverterFactory
->expects($this->once())
->method('has')
->with($this->entityType, $attribute, $type)
->willReturn(true);
$this->itemConverterFactory
->expects($this->once())
->method('create')
->with($this->entityType, $attribute, $type, $this->user)
->willReturn($converter);
$whereClause = $this->converter->convert($this->queryBuilder, $item);
$expected = [
'testAnother=' => 'test-value',
];
$this->assertEquals($expected, $whereClause->getRaw());
}
}

View File

@@ -0,0 +1,204 @@
<?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\Select\Where;
use Espo\Core\Binding\Binder;
use Espo\Core\Binding\BindingContainer;
use Espo\Core\Binding\BindingData;
use Espo\Core\InjectableFactory;
use Espo\Core\Select\Where\ItemConverterFactory;
use Espo\Core\Utils\Metadata;
use Espo\Entities\User;
use PHPUnit\Framework\TestCase;
use tests\unit\testClasses\Core\Select\Where\ItemConverters\TestConverter;
class ItemConverterFactoryTest extends TestCase
{
private $metadata;
private $injectableFactory;
private $user;
private $factory;
protected function setUp(): void
{
$this->injectableFactory = $this->createMock(InjectableFactory::class);
$this->metadata = $this->createMock(Metadata::class);
$this->user = $this->createMock(User::class);
$this->factory = new ItemConverterFactory(
$this->injectableFactory,
$this->metadata
);
}
public function testCreateForType()
{
$this->prepareFactoryTest(TestConverter::class);
$this->prepareFactoryTest(TestConverter::class, true);
}
public function testHasFalseForType()
{
$this->metadata
->expects($this->once())
->method('get')
->with([
'app', 'select', 'whereItemConverterClassNameMap', 'someType'
])
->willReturn(null);
$this->assertFalse(
$this->factory->hasForType('someType')
);
}
public function testCreateEntityType()
{
$this->prepareFactoryTestEntityType(TestConverter::class);
$this->prepareFactoryTestEntityType(TestConverter::class, true);
}
protected function prepareFactoryTest(?string $className, bool $testHas = false)
{
$entityType = 'Test';
$type = 'someType';
$this->metadata
->expects($this->any())
->method('get')
->with([
'app', 'select', 'whereItemConverterClassNameMap', $type
])
->willReturn($className);
if ($testHas) {
$this->assertTrue(
$this->factory->hasForType($type)
);
return;
}
$object = $this->createMock($className);
$bindingData = new BindingData();
$binder = new Binder($bindingData);
$binder
->bindInstance(User::class, $this->user);
$binder
->for($className)
->bindValue('$entityType', $entityType);
$bindingContainer = new BindingContainer($bindingData);
$this->injectableFactory
->expects($this->once())
->method('createWithBinding')
->with(
$className,
$bindingContainer
)
->willReturn($object);
$resultObject = $this->factory->createForType(
$type,
$entityType,
$this->user
);
$this->assertEquals($object, $resultObject);
}
protected function prepareFactoryTestEntityType(?string $className, bool $testHas = false)
{
$entityType = 'Test';
$type = 'someType';
$attribute = 'test';
$this->metadata
->expects($this->any())
->method('get')
->with([
'selectDefs', $entityType, 'whereItemConverterClassNameMap', $attribute . '_' . $type
])
->willReturn($className);
if ($testHas) {
$this->assertTrue(
$this->factory->has(
$entityType,
$attribute,
$type
)
);
return;
}
$object = $this->createMock($className);
$bindingData = new BindingData();
$binder = new Binder($bindingData);
$binder
->bindInstance(User::class, $this->user);
$binder
->for($className)
->bindValue('$entityType', $entityType);
$bindingContainer = new BindingContainer($bindingData);
$this->injectableFactory
->expects($this->once())
->method('createWithBinding')
->with(
$className,
$bindingContainer
)
->willReturn($object);
$resultObject = $this->factory->create(
$entityType,
$attribute,
$type,
$this->user
);
$this->assertEquals($object, $resultObject);
}
}

View File

@@ -0,0 +1,193 @@
<?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\Select\Where;
use Espo\Core\Select\Where\Item;
use Espo\Core\Select\Where\Item\Data\DateTime;
use InvalidArgumentException;
class ItemTest extends \PHPUnit\Framework\TestCase
{
protected function setUp(): void
{
}
public function testFromArray()
{
$item = Item::createBuilder()
->setType('equals')
->setAttribute('test')
->setValue('testValue')
->build();
$this->assertEquals('equals', $item->getType());
$this->assertEquals('test', $item->getAttribute());
$this->assertEquals('testValue', $item->getValue());
$this->assertNull($item->getData());
$item1 = Item::createBuilder()
->setType('equals')
->setAttribute('test')
->setValue(1)
->build();
$this->assertEquals('equals', $item1->getType());
$this->assertEquals('test', $item1->getAttribute());
$this->assertEquals(1, $item1->getValue());
$item2 = Item::createBuilder()
->setType('equals')
->setAttribute('test')
->setValue('testValue')
->setData(
DateTime::create()->withTimeZone('Europe/London')
)
->build();
$this->assertNotNull($item2->getData());
$this->assertEquals('Europe/London', $item2->getData()->getTimeZone());
}
public function testEmpty()
{
$this->expectException(InvalidArgumentException::class);
$item = Item::fromRaw([]);
}
public function testEmptyAttribute1()
{
$this->expectException(InvalidArgumentException::class);
$item = Item::fromRaw([
'type' => 'equals',
]);
}
public function testEmptyAttribute2()
{
$item = Item::fromRaw([
'type' => 'and',
'value' => [],
]);
$this->assertNotNull($item);
}
public function testEmptyType()
{
$this->expectException(InvalidArgumentException::class);
$item = Item::fromRaw([
'attribute' => 'test',
]);
}
public function testNonExistingParam()
{
$this->expectException(InvalidArgumentException::class);
$params = Item::fromRaw([
'bad' => 'd',
]);
}
public function testGetRaw1()
{
$raw = [
'type' => 'and',
'value' => [],
];
$item = Item::fromRaw($raw);
$result = $item->getRaw();
$this->assertEquals($raw, $result);
}
public function testGetRaw2()
{
$raw = [
'type' => 'euqls',
'attribute' => 'test',
'value' => '2020-12-12',
'dateTime' => true,
'timeZone' => 'UTC',
];
$item = Item::fromRaw($raw);
$result = $item->getRaw();
$this->assertEquals($raw, $result);
}
public function testGetItemList1()
{
$raw = [
'type' => 'and',
'value' => [
[
'type' => 'or',
'value' => [],
],
[
'type' => 'or',
'value' => [],
],
],
];
$item = Item::fromRaw($raw);
$this->assertEquals('or', $item->getItemList()[0]->getType());
}
public function testGetItemList2()
{
$item = Item::createBuilder()
->setType('and')
->setItemList([
Item::createBuilder()
->setType('or')
->setItemList([])
->build(),
Item::createBuilder()
->setType('or')
->setItemList([])
->build(),
])
->build();
$this->assertEquals('or', $item->getItemList()[0]->getType());
}
}

View File

@@ -0,0 +1,88 @@
<?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\Select\Where;
use Espo\Core\{
Select\Where\Params,
};
use InvalidArgumentException;
class ParamsTest extends \PHPUnit\Framework\TestCase
{
protected function setUp() : void
{
}
public function testFromArray()
{
$item = Params::fromAssoc([
'applyPermissionCheck' => true,
'forbidComplexExpressions' => true,
]);
$this->assertTrue($item->applyPermissionCheck());
$this->assertTrue($item->forbidComplexExpressions());
$item = Params::fromAssoc([
'applyPermissionCheck' => false,
'forbidComplexExpressions' => false,
]);
$this->assertFalse($item->applyPermissionCheck());
$this->assertFalse($item->forbidComplexExpressions());
$item = Params::fromAssoc([
'applyPermissionCheck' => false,
'forbidComplexExpressions' => true,
]);
$this->assertFalse($item->applyPermissionCheck());
$this->assertTrue($item->forbidComplexExpressions());
}
public function testEmpty()
{
$item = Params::fromAssoc([
]);
$this->assertFalse($item->applyPermissionCheck());
$this->assertFalse($item->forbidComplexExpressions());
}
public function testNonExistingParam()
{
$this->expectException(InvalidArgumentException::class);
$params = Params::fromAssoc([
'bad' => 'd',
]);
}
}

View File

@@ -0,0 +1,241 @@
<?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\Select\Where;
use Espo\Core\Select\Where\Item;
use Espo\Core\Select\Where\Scanner;
use Espo\ORM\BaseEntity as Entity;
use Espo\ORM\Entity as OrmEntity;
use Espo\ORM\EntityManager;
use Espo\ORM\Query\Select as Query;
use Espo\ORM\Query\SelectBuilder as QueryBuilder;
use Espo\ORM\Type\RelationType;
use PHPUnit\Framework\TestCase;
class ScannerTest extends TestCase
{
private $scanner;
private $queryBuilder;
private $entity;
protected function setUp() : void
{
$entityManager = $this->createMock(EntityManager::class);
$this->scanner = new Scanner($entityManager);
$entityType = 'Test';
$this->queryBuilder = $this->createMock(QueryBuilder::class);
$query = $this->createMock(Query::class);
$query
->expects($this->any())
->method('getFrom')
->willReturn($entityType);
$this->queryBuilder
->expects($this->any())
->method('build')
->willReturn($query);
$this->entity = $this->createMock(Entity::class);
$entityManager
->expects($this->any())
->method('getNewEntity')
->with($entityType)
->willReturn($this->entity);
}
public function testApplyLeftJoins1(): void
{
$item = Item::fromRaw([
'type' => 'and',
'value' => [
[
'type' => 'equals',
'attribute' => 'test1',
'value' => 'value',
],
[
'type' => 'equals',
'attribute' => 'TEST:(link2.test2)',
'value' => 'value',
],
[
'type' => 'equals',
'attribute' => 'link3.test3',
'value' => 'value',
],
[
'type' => 'equals',
'attribute' => 'test4',
'value' => 'value',
],
],
]);
$this->entity
->expects($this->any())
->method('hasRelation')
->willReturnMap(
[
['link1', true],
['link2', true],
['link3', true],
]
);
$c = $this->exactly(3);
$this->queryBuilder
->expects($c)
->method('leftJoin')
->willReturnCallback(function ($link) use ($c) {
if ($c->numberOfInvocations() === 1) {
$this->assertEquals('link2', $link);
}
if ($c->numberOfInvocations() === 2) {
$this->assertEquals('link3', $link);
}
if ($c->numberOfInvocations() === 3) {
$this->assertEquals('link4', $link);
}
return $this->queryBuilder;
});
/*$this->queryBuilder
->expects($this->once())
->method('distinct');*/
$this->entity
->expects($this->any())
->method('getAttributeType')
->willReturnMap(
[
['test1', OrmEntity::VARCHAR],
['test4', OrmEntity::FOREIGN],
]
);
$this->entity
->expects($this->any())
->method('getRelationType')
->willReturnMap(
[
['link2', OrmEntity::HAS_MANY],
['link3', OrmEntity::BELONGS_TO],
]
);
$this->entity
->expects($this->any())
->method('getAttributeParam')
->willReturnMap(
[
['test4', 'relation', 'link4'],
]
);
$this->scanner->apply($this->queryBuilder, $item);
}
public function testHasRelatedMany(): void
{
$em = $this->createMock(EntityManager::class);
$entity = $this->createMock(Entity::class);
$em->expects($this->once())
->method('getNewEntity')
->willReturn($entity);
$entity
->expects($this->any())
->method('hasRelation')
->willReturnMap(
[
['link2', true],
['link3', true],
]
);
$entity
->expects($this->any())
->method('getRelationType')
->willReturnMap(
[
['link2', RelationType::HAS_MANY],
['link3', RelationType::BELONGS_TO],
]
);
$scanner = new Scanner($em);
$item = Item::fromRaw([
'type' => 'and',
'value' => [
[
'type' => 'equals',
'attribute' => 'test1',
'value' => 'value',
],
[
'type' => 'equals',
'attribute' => 'TEST:(link2.test2)',
'value' => 'value',
],
[
'type' => 'equals',
'attribute' => 'link3.test3',
'value' => 'value',
],
],
]);
$this->assertTrue($scanner->hasRelatedMany('Test', $item));
$item = Item::fromRaw([
'type' => 'and',
'value' => [
[
'type' => 'equals',
'attribute' => 'link3.test3',
'value' => 'value',
],
],
]);
$this->assertFalse($scanner->hasRelatedMany('Test', $item));
}
}