diff --git a/application/Espo/ORM/Util.php b/application/Espo/ORM/Util.php index 283d7354c5..a2eccd3bf8 100644 --- a/application/Espo/ORM/Util.php +++ b/application/Espo/ORM/Util.php @@ -53,15 +53,25 @@ class Util } foreach ($v1 as $i => $itemValue) { - if (is_object($itemValue) && is_object($v2[$i])) { - if (!self::areValuesEqual(AttributeType::JSON_OBJECT, $itemValue, $v2[$i])) { + $otherValue = $v2[$i]; + + if (is_object($itemValue) && is_object($otherValue)) { + if (!self::areValuesEqual(AttributeType::JSON_OBJECT, $itemValue, $otherValue)) { return false; } continue; } - if ($itemValue !== $v2[$i]) { + if (is_array($itemValue) && is_array($otherValue)) { + if (!self::areValuesEqual(AttributeType::JSON_ARRAY, $itemValue, $otherValue)) { + return false; + } + + continue; + } + + if ($itemValue !== $otherValue) { return false; } } diff --git a/tests/unit/Espo/ORM/UtilTest.php b/tests/unit/Espo/ORM/UtilTest.php new file mode 100644 index 0000000000..b5f4107794 --- /dev/null +++ b/tests/unit/Espo/ORM/UtilTest.php @@ -0,0 +1,201 @@ +. + * + * 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\ORM; + +use Espo\ORM\Type\AttributeType; +use Espo\ORM\Util; +use PHPUnit\Framework\TestCase; + +class UtilTest extends TestCase +{ + public function testAreValuesEqualScalar(): void + { + $this->assertTrue( + Util::areValuesEqual( + AttributeType::VARCHAR, + 'a1', + 'a1' + ) + ); + + $this->assertTrue( + Util::areValuesEqual( + AttributeType::VARCHAR, + 1, + 1 + ) + ); + + $this->assertTrue( + Util::areValuesEqual( + AttributeType::VARCHAR, + 1.1, + 1.1 + ) + ); + + $this->assertFalse( + Util::areValuesEqual( + AttributeType::VARCHAR, + 'a1', + 'a2' + ) + ); + } + + public function testAreValuesEqualArray(): void + { + $this->assertTrue( + Util::areValuesEqual( + AttributeType::JSON_ARRAY, + ['a1', 'a2'], + ['a1', 'a2'], + ) + ); + + $this->assertFalse( + Util::areValuesEqual( + AttributeType::JSON_ARRAY, + ['a1', 'a2'], + ['a2', 'a1'], + ) + ); + + $this->assertTrue( + Util::areValuesEqual( + AttributeType::JSON_ARRAY, + ['a1', 'a2'], + ['a1', 'a2'], + isUnordered: true, + ) + ); + + $this->assertTrue( + Util::areValuesEqual( + AttributeType::JSON_ARRAY, + ['a1', 'a2'], + ['a2', 'a1'], + isUnordered: true, + ) + ); + + $this->assertTrue( + Util::areValuesEqual( + AttributeType::JSON_ARRAY, + ['a1', 'a2'], + ['a2', 'a1'], + isUnordered: true, + ) + ); + + $this->assertTrue( + Util::areValuesEqual( + AttributeType::JSON_ARRAY, + [['a1'], ['a2']], + [['a1'], ['a2']], + ) + ); + + $this->assertTrue( + Util::areValuesEqual( + AttributeType::JSON_ARRAY, + [(object) ['a1' => 1], (object) ['a2' => 1]], + [(object) ['a1' => 1], (object) ['a2' => 1]], + ) + ); + + $this->assertFalse( + Util::areValuesEqual( + AttributeType::JSON_ARRAY, + [(object) ['a1' => 1], (object) ['a2' => 1]], + [(object) ['a1' => 2], (object) ['a2' => 1]], + ) + ); + + $this->assertTrue( + Util::areValuesEqual( + AttributeType::JSON_ARRAY, + [[(object) ['a1' => 1], (object) ['a2' => 1]]], + [[(object) ['a1' => 1], (object) ['a2' => 1]]], + ) + ); + + $this->assertFalse( + Util::areValuesEqual( + AttributeType::JSON_ARRAY, + [[(object) ['a1' => 1], (object) ['a2' => 1]]], + [[(object) ['a1' => 2], (object) ['a2' => 1]]], + ) + ); + } + + public function testAreValuesEqualObject(): void + { + $this->assertTrue( + Util::areValuesEqual( + AttributeType::JSON_OBJECT, + (object) ['a1' => 1], + (object) ['a1' => 1], + ) + ); + + $this->assertFalse( + Util::areValuesEqual( + AttributeType::JSON_OBJECT, + (object) ['a1' => 1], + (object) ['a1' => 2], + ) + ); + + $this->assertFalse( + Util::areValuesEqual( + AttributeType::JSON_OBJECT, + (object) ['a1' => 1], + (object) ['a2' => 2], + ) + ); + + $this->assertTrue( + Util::areValuesEqual( + AttributeType::JSON_OBJECT, + (object) ['a1' => 1, 'a2' => 2], + (object) ['a2' => 2, 'a1' => 1], + ) + ); + + $this->assertTrue( + Util::areValuesEqual( + AttributeType::JSON_OBJECT, + (object) ['a1' => [1, 2], 'a2' => [3, 4]], + (object) ['a1' => [1, 2], 'a2' => [3, 4]], + ) + ); + } +}