formula where tests

This commit is contained in:
Yuri Kuznetsov
2025-06-25 15:06:15 +03:00
parent 41a0fd48bb
commit b587ef50dd
5 changed files with 178 additions and 17 deletions
@@ -34,7 +34,6 @@ use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Formula\ArgumentList;
use Espo\Core\Formula\Exceptions\Error;
use Espo\Core\Formula\Functions\BaseFunction;
use Espo\Core\Di;
use Espo\Core\Formula\Functions\RecordGroup\Util\FindQueryUtil;
@@ -32,17 +32,12 @@ namespace Espo\Core\Formula\Functions\RecordGroup;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Formula\ArgumentList;
use Espo\Core\Formula\Exceptions\Error;
use Espo\Core\Formula\Exceptions\Error as FormulaError;
use Espo\Core\Formula\Functions\BaseFunction;
use Espo\Core\Di;
use Espo\Core\Formula\Functions\RecordGroup\Util\FindQueryUtil;
use Espo\Core\Select\Where\Item;
use Espo\Core\Utils\Json;
use Espo\ORM\Name\Attribute;
use Espo\ORM\Query\Part\Order;
use InvalidArgumentException;
use stdClass;
/**
* @noinspection PhpUnused
@@ -136,7 +136,13 @@ class FindRelatedManyType extends BaseFunction implements
$relationType = $entity->getRelationParam($link, 'type');
if (in_array($relationType, ['belongsTo', 'hasOne', 'belongsToParent'])) {
if (
in_array($relationType, [
RelationType::BELONGS_TO,
RelationType::HAS_ONE,
RelationType::BELONGS_TO_PARENT,
])
) {
$this->throwError("Not supported link type '$relationType'.");
}
@@ -32,16 +32,15 @@ namespace Espo\Core\Formula\Functions\RecordGroup;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\ORM\Entity as CoreEntity;
use Espo\ORM\Defs\Params\RelationParam;
use Espo\ORM\Name\Attribute;
use Espo\Core\Formula\{
Exceptions\Error,
Functions\BaseFunction,
ArgumentList,
Functions\RecordGroup\Util\FindQueryUtil};
use Espo\Core\Formula\ArgumentList;
use Espo\Core\Formula\Exceptions\Error;
use Espo\Core\Formula\Functions\BaseFunction;
use Espo\Core\Formula\Functions\RecordGroup\Util\FindQueryUtil;
use Espo\Core\Di;
use Espo\ORM\Query\Part\Order;
use Espo\ORM\Type\RelationType;
class FindRelatedOneType extends BaseFunction implements
Di\EntityManagerAware,
@@ -101,7 +100,13 @@ class FindRelatedOneType extends BaseFunction implements
$relationType = $entity->getRelationParam($link, 'type');
if (in_array($relationType, ['belongsTo', 'hasOne', 'belongsToParent'])) {
if (
in_array($relationType, [
RelationType::BELONGS_TO,
RelationType::HAS_ONE,
RelationType::BELONGS_TO_PARENT,
])
) {
$relatedEntity = $entityManager
->getRDBRepository($entityType)
->getRelation($entity, $link)
@@ -122,7 +127,7 @@ class FindRelatedOneType extends BaseFunction implements
$order = $metadata->get(['entityDefs', $entityType, 'collection', 'order']) ?? 'ASC';
}
} else {
$order = $order ?? 'ASC';
$order = $order ?? Order::ASC;
}
$foreignEntityType = $entity->getRelationParam($link, RelationParam::ENTITY);
@@ -174,7 +179,7 @@ class FindRelatedOneType extends BaseFunction implements
$queryBuilder->where($whereClause);
}
if ($relationType === 'hasChildren') {
if ($relationType === RelationType::HAS_CHILDREN) {
$queryBuilder->where([
$foreignLink . 'Id' => $entity->getId(),
$foreignLink . 'Type' => $entity->getEntityType(),
@@ -36,6 +36,7 @@ use Espo\Core\Formula\Exceptions\UnsafeFunction;
use Espo\Core\Formula\Manager;
use Espo\Entities\User;
use Espo\Modules\Crm\Entities\Account;
use Espo\Modules\Crm\Entities\Contact;
use Espo\Modules\Crm\Entities\Meeting;
use Espo\Modules\Crm\Entities\Opportunity;
use Espo\ORM\EntityManager;
@@ -1016,4 +1017,159 @@ class FormulaTest extends BaseTestCase
/** @noinspection PhpUnhandledExceptionInspection */
$fm->runSafe($script);
}
/**
* @noinspection PhpUnhandledExceptionInspection
*/
public function testFilterWhere(): void
{
$em = $this->getEntityManager();
$fm = $this->getContainer()->getByClass(Manager::class);
$account1 = $em->createEntity(Account::ENTITY_TYPE, ['name' => 'a1']);
$em->createEntity(Account::ENTITY_TYPE, ['name' => 'a2']);
$contact1 = $em->createEntity(Contact::ENTITY_TYPE, [
'lastName' => 'c1_1',
'accountId' => $account1->getId(),
]);
$em->createEntity(Contact::ENTITY_TYPE, [
'lastName' => 'c1_2',
'accountId' => $account1->getId(),
]);
$em->createEntity(Opportunity::ENTITY_TYPE, [
'accountId' => $account1->getId(),
'amount' => 100.0,
'name' => 'o1_1',
]);
$em->createEntity(Opportunity::ENTITY_TYPE, [
'accountId' => $account1->getId(),
'amount' => 150.0,
'name' => 'o1_2',
]);
$script = "
\$where = object\create(\$w);
\$where['type'] = 'and';
\$where['value'] = list(
(
\$item1 = object\create();
\$item1['type'] = 'equals';
\$item1['attribute'] = 'name';
\$item1['value'] = 'a1';
\$item1;
)
);
record\\findMany('Account', 2, null, null, \$where);
";
$result = $fm->run($script);
$this->assertEquals([$account1->getId()], $result);
$script = "
\$where = object\create(\$w);
\$where['type'] = 'and';
\$where['value'] = list(
(
\$item1 = object\create();
\$item1['type'] = 'equals';
\$item1['attribute'] = 'name';
\$item1['value'] = 'a1';
\$item1;
)
);
record\\findOne('Account', null, null, \$where);
";
$result = $fm->run($script);
$this->assertEquals($account1->getId(), $result);
$script = "
\$where = object\create(\$w);
\$where['type'] = 'and';
\$where['value'] = list(
(
\$item1 = object\create();
\$item1['type'] = 'equals';
\$item1['attribute'] = 'name';
\$item1['value'] = 'a1';
\$item1;
)
);
record\\exists('Account', \$where);
";
$result = $fm->run($script);
$this->assertTrue($result);
$script = "
\$where = object\create(\$w);
\$where['type'] = 'and';
\$where['value'] = list(
(
\$item1 = object\create();
\$item1['type'] = 'equals';
\$item1['attribute'] = 'name';
\$item1['value'] = 'a1';
\$item1;
)
);
record\\count('Account', \$where);
";
$result = $fm->run($script);
$this->assertEquals(1, $result);
$script = "
\$item = object\create();
\$item['type'] = 'equals';
\$item['attribute'] = 'lastName';
\$item['value'] = 'c1_1';
\$item;
record\\findRelatedOne('Account', '{$account1->getId()}', 'contacts', null, null, \$item);
";
$result = $fm->run($script);
$this->assertEquals($contact1->getId(), $result);
$script = "
\$item = object\create();
\$item['type'] = 'equals';
\$item['attribute'] = 'lastName';
\$item['value'] = 'c1_1';
\$item;
record\\findRelatedMany('Account', '{$account1->getId()}', 'contacts', 2, null, null, \$item);
";
$result = $fm->run($script);
$this->assertEquals([$contact1->getId()], $result);
$script = "
\$item = object\create();
\$item['type'] = 'equals';
\$item['attribute'] = 'lastName';
\$item['value'] = 'c1_1';
\$item;
entity\\countRelated('contacts', \$item);
";
$result = $fm->run($script, $account1);
$this->assertEquals(1, $result);
$script = "
\$item = object\create();
\$item['type'] = 'equals';
\$item['attribute'] = 'name';
\$item['value'] = 'o1_1';
\$item;
entity\\sumRelated('opportunities', 'amount', \$item);
";
$result = $fm->run($script, $account1);
$this->assertEquals(100.0, $result);
}
}