Skip to content

EspoCRM – Open Source CRM Application

condition checker fix and has support

Yuri Kuznetsovf4c6c16df76f
diff --git a/application/Espo/Tools/DynamicLogic/ConditionChecker.php b/application/Espo/Tools/DynamicLogic/ConditionChecker.php
index eac84d4c17..aec1f7cdb7 100644
--- a/application/Espo/Tools/DynamicLogic/ConditionChecker.php
+++ b/application/Espo/Tools/DynamicLogic/ConditionChecker.php
@@ -173,6 +173,22 @@ class ConditionChecker
return true;
}
+ if ($type === Type::Has) {
+ if (is_array($setValue)) {
+ return in_array($value, $setValue);
+ }
+
+ return false;
+ }
+
+ if ($type === Type::NotHas) {
+ if (is_array($setValue)) {
+ return !in_array($value, $setValue);
+ }
+
+ return true;
+ }
+
if ($type === Type::StartsWith) {
if (is_string($setValue) && is_string($value)) {
return str_starts_with($setValue, $value);
@@ -214,16 +230,16 @@ class ConditionChecker
}
if ($type === Type::In) {
- if (is_array($setValue)) {
- return in_array($value, $setValue);
+ if (is_array($value)) {
+ return in_array($setValue, $value);
}
return false;
}
if ($type === Type::NotIn) {
- if (is_array($setValue)) {
- return !in_array($value, $setValue);
+ if (is_array($value)) {
+ return !in_array($setValue, $value);
}
return true;
@@ -278,6 +294,7 @@ class ConditionChecker
return $setDate->isLessThan($today);
}
+ /** @phpstan-ignore-next-line deadCode.unreachable */
throw new BadCondition("Unimplemented type '$type->value'.");
}
diff --git a/application/Espo/Tools/DynamicLogic/Item.php b/application/Espo/Tools/DynamicLogic/Item.php
index 71e49f197d..36ed50cd88 100644
--- a/application/Espo/Tools/DynamicLogic/Item.php
+++ b/application/Espo/Tools/DynamicLogic/Item.php
@@ -65,6 +65,10 @@ readonly class Item
throw new BadCondition("No type.");
}
+ if ($type === 'has') {
+ $type = 'contains';
+ }
+
if ($type === Type::And->value || $type === Type::Or->value) {
if (!is_array($value)) {
throw new BadCondition("Non-array value.");
diff --git a/tests/unit/Espo/Tools/DynamicLogic/CheckerTest.php b/tests/unit/Espo/Tools/DynamicLogic/CheckerTest.php
index cd9e5ff95e..fe3ccc3f6e 100644
--- a/tests/unit/Espo/Tools/DynamicLogic/CheckerTest.php
+++ b/tests/unit/Espo/Tools/DynamicLogic/CheckerTest.php
@@ -613,7 +613,7 @@ class CheckerTest extends TestCase
/**
* @noinspection PhpUnhandledExceptionInspection
*/
- public function testIn(): void
+ public function testHas(): void
{
$map = [
'array' => ['test'],
@@ -625,7 +625,7 @@ class CheckerTest extends TestCase
$checker->check(
Item::fromGroupDefinition([
(object) [
- 'type' => 'in',
+ 'type' => 'has',
'attribute' => 'array',
'value' => 'test',
]
@@ -637,7 +637,7 @@ class CheckerTest extends TestCase
$checker->check(
Item::fromGroupDefinition([
(object) [
- 'type' => 'in',
+ 'type' => 'has',
'attribute' => 'array',
'value' => 'hello',
]
@@ -649,7 +649,7 @@ class CheckerTest extends TestCase
/**
* @noinspection PhpUnhandledExceptionInspection
*/
- public function testNotIn(): void
+ public function testNotHas(): void
{
$map = [
'array' => ['test'],
@@ -661,7 +661,7 @@ class CheckerTest extends TestCase
$checker->check(
Item::fromGroupDefinition([
(object) [
- 'type' => 'notIn',
+ 'type' => 'notHas',
'attribute' => 'array',
'value' => 'test',
]
@@ -673,7 +673,7 @@ class CheckerTest extends TestCase
$checker->check(
Item::fromGroupDefinition([
(object) [
- 'type' => 'notIn',
+ 'type' => 'notHas',
'attribute' => 'array',
'value' => 'hello',
]
@@ -682,6 +682,78 @@ class CheckerTest extends TestCase
);
}
+ /**
+ * @noinspection PhpUnhandledExceptionInspection
+ */
+ public function testIn(): void
+ {
+ $map = [
+ 'value' => 'test',
+ ];
+
+ $checker = new ConditionChecker($this->initEntity($map));
+
+ $this->assertTrue(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'in',
+ 'attribute' => 'value',
+ 'value' => ['test'],
+ ]
+ ])
+ )
+ );
+
+ $this->assertFalse(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'in',
+ 'attribute' => 'value',
+ 'value' => ['hello'],
+ ]
+ ])
+ )
+ );
+ }
+
+ /**
+ * @noinspection PhpUnhandledExceptionInspection
+ */
+ public function testNotIn(): void
+ {
+ $map = [
+ 'value' => 'test',
+ ];
+
+ $checker = new ConditionChecker($this->initEntity($map));
+
+ $this->assertFalse(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'notIn',
+ 'attribute' => 'value',
+ 'value' => ['test'],
+ ]
+ ])
+ )
+ );
+
+ $this->assertTrue(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'notIn',
+ 'attribute' => 'value',
+ 'value' => ['hello'],
+ ]
+ ])
+ )
+ );
+ }
+
/**
* @noinspection PhpUnhandledExceptionInspection
*/