From dff2e344a43185d75fb086f9a071e305202012e5 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Sat, 6 Mar 2021 16:25:04 +0200 Subject: [PATCH] fix formula modulo --- .../Functions/NumericGroup/ModuloType.php | 21 ++++++++++--------- .../unit/Espo/Core/Formula/EvaluatorTest.php | 16 ++++++++++++++ tests/unit/Espo/Core/Formula/FormulaTest.php | 6 +++--- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/application/Espo/Core/Formula/Functions/NumericGroup/ModuloType.php b/application/Espo/Core/Formula/Functions/NumericGroup/ModuloType.php index 8bdc9f0d89..edb4549fad 100644 --- a/application/Espo/Core/Formula/Functions/NumericGroup/ModuloType.php +++ b/application/Espo/Core/Formula/Functions/NumericGroup/ModuloType.php @@ -38,18 +38,19 @@ class ModuloType extends BaseFunction { public function process(ArgumentList $args) { - $result = 1; - - foreach ($args as $subItem) { - $part = $this->evaluate($subItem); - - if (!is_float($part) && !is_int($part)) { - $part = floatval($part); - } - - $result %= $part; + if (count($args) < 2) { + $this->throwTooFewArguments(); } + $result = $this->evaluate($args[0]); + $part = $this->evaluate($args[1]); + + if (!is_float($part) && !is_int($part)) { + $part = floatval($part); + } + + $result %= $part; + return $result; } } diff --git a/tests/unit/Espo/Core/Formula/EvaluatorTest.php b/tests/unit/Espo/Core/Formula/EvaluatorTest.php index 938fa70da0..5cf20af2b4 100644 --- a/tests/unit/Espo/Core/Formula/EvaluatorTest.php +++ b/tests/unit/Espo/Core/Formula/EvaluatorTest.php @@ -472,4 +472,20 @@ class EvaluatorTest extends \PHPUnit\Framework\TestCase $this->assertNotEmpty($id); } + + public function testModulo1() : void + { + $expression = "123 % 5"; + $actual = $this->evaluator->process($expression); + + $this->assertEquals(123 % 5, $actual); + } + + public function testModulo2() : void + { + $expression = "124 % 5"; + $actual = $this->evaluator->process($expression); + + $this->assertEquals(124 % 5, $actual); + } } diff --git a/tests/unit/Espo/Core/Formula/FormulaTest.php b/tests/unit/Espo/Core/Formula/FormulaTest.php index 7f9ec58bda..edc3aafb75 100644 --- a/tests/unit/Espo/Core/Formula/FormulaTest.php +++ b/tests/unit/Espo/Core/Formula/FormulaTest.php @@ -787,11 +787,11 @@ class FormulaTest extends \PHPUnit\Framework\TestCase "value": [ { "type": "value", - "value": 3 + "value": 124 }, { "type": "value", - "value": 2 + "value": 5 } ] } @@ -799,7 +799,7 @@ class FormulaTest extends \PHPUnit\Framework\TestCase $result = $this->createProcessor()->process($item); - $this->assertEquals(3 % 2, $result); + $this->assertEquals(124 % 5, $result); } function testIfThenElse1()