diff --git a/core/metamodel.class.php b/core/metamodel.class.php index 386c3a12e..ab032712b 100644 --- a/core/metamodel.class.php +++ b/core/metamodel.class.php @@ -3074,8 +3074,8 @@ abstract class MetaModel $MANDATORY_ATTRIBUTES = array('attributes'); $UNIQUENESS_MANDATORY_KEYS_NB = count($MANDATORY_ATTRIBUTES); $bHasDisabledKey = false; - $bHasMissingMandatoryKey = false; - $iMissingMandatoryKeysNb = 0; + $bHasMissingMandatoryKey = true; + $iMissingMandatoryKeysNb = $UNIQUENESS_MANDATORY_KEYS_NB; $bHasAllMandatoryKeysMissing = false; $bHasNonDisabledKeys = false; @@ -3086,6 +3086,12 @@ abstract class MetaModel $bHasDisabledKey = true; continue; } + $bHasNonDisabledKeys = true; + + if (in_array($sUniquenessRuleKey, $MANDATORY_ATTRIBUTES, true)) { + $bHasMissingMandatoryKey = false; + $iMissingMandatoryKeysNb--; + } if (($sUniquenessRuleKey === 'attributes') && (!empty($aExistingClassFields))) { @@ -3097,21 +3103,6 @@ abstract class MetaModel } } } - - if (!$aUniquenessRuleProperty) - { - if (!in_array($sUniquenessRuleKey, $MANDATORY_ATTRIBUTES, true)) - { - continue; - } - - $bHasMissingMandatoryKey = true; - $iMissingMandatoryKeysNb++; - - continue; - } - - $bHasNonDisabledKeys = true; } if ($iMissingMandatoryKeysNb == $UNIQUENESS_MANDATORY_KEYS_NB) diff --git a/test/core/UniquenessConstraintTest.php b/test/core/UniquenessConstraintTest.php new file mode 100644 index 000000000..7e20e1d6e --- /dev/null +++ b/test/core/UniquenessConstraintTest.php @@ -0,0 +1,77 @@ +assertEquals($bIsRuleShouldBeValid, $bRuleValidResult, "Validity test returned $bRuleValidResult"); + } + + public function testUniquenessRuleValidityCheckProvider() + { + return array( + 'simplest rule' => array(true, false, array('attributes' => array('name'))), + 'with all properties' => array( + true, + false, + array( + 'attributes' => array('name'), + 'filter' => 'name != \'\'', + 'disabled' => false, + 'is_blocking' => true, + ), + ), + 'only disabled key without ancestor' => array( + false, + false, + array( + 'disabled' => true, + ), + ), + 'only disabled key with ancestor' => array( + true, + true, + array( + 'disabled' => true, + ), + ), + ); + } +} \ No newline at end of file