N°931: TagSet search (manage undefined values) unit tests

This commit is contained in:
Eric
2018-09-12 18:17:17 +02:00
parent 979095337b
commit d1ee7f4353
5 changed files with 73 additions and 15 deletions

View File

@@ -578,6 +578,7 @@ class BinaryExpression extends Expression
* @param null $oAttDef
*
* @return array
* @throws \MissingQueryArgument
*/
public function GetCriterion($oSearch, &$aArgs = null, $bRetrofitParams = false, $oAttDef = null)
{
@@ -676,6 +677,10 @@ class BinaryExpression extends Expression
}
}
}
if (isset($aCriteriaLeft['widget']) && isset($aCriteriaRight['widget']) && ($aCriteriaLeft['widget'] == AttributeDefinition::SEARCH_WIDGET_TYPE_TAG_SET) && ($aCriteriaRight['widget'] == AttributeDefinition::SEARCH_WIDGET_TYPE_TAG_SET))
{
$aCriteriaOverride['operator'] = 'MATCHES';
}
}
return array_merge($aCriteriaLeft, $aCriteriaRight, $aCriteriaOverride);
@@ -886,20 +891,20 @@ class ScalarExpression extends UnaryExpression
public function GetCriterion($oSearch, &$aArgs = null, $bRetrofitParams = false, $oAttDef = null)
{
$aCriteria = array();
$aCriterion = array();
switch ((string)($this->m_value))
{
case '%Y-%m-%d':
$aCriteria['unit'] = 'DAY';
$aCriterion['unit'] = 'DAY';
break;
case '%Y-%m':
$aCriteria['unit'] = 'MONTH';
$aCriterion['unit'] = 'MONTH';
break;
case '%w':
$aCriteria['unit'] = 'WEEKDAY';
$aCriterion['unit'] = 'WEEKDAY';
break;
case '%H':
$aCriteria['unit'] = 'HOUR';
$aCriterion['unit'] = 'HOUR';
break;
default:
$aValue = array();
@@ -938,7 +943,7 @@ class ScalarExpression extends UnaryExpression
}
else
{
$aValue['label'] = Dict::S('Enum:Undefined');
$aCriterion['has_undefined'] = true;
}
}
catch (Exception $e)
@@ -981,12 +986,12 @@ class ScalarExpression extends UnaryExpression
{
// only if a label is found
$aValue['value'] = $this->GetValue();
$aCriteria['values'] = array($aValue);
$aCriterion['values'] = array($aValue);
}
break;
}
$aCriteria['oql'] = $this->RenderExpression(false, $aArgs, $bRetrofitParams);
return $aCriteria;
$aCriterion['oql'] = $this->RenderExpression(false, $aArgs, $bRetrofitParams);
return $aCriterion;
}
}

View File

@@ -23,6 +23,11 @@ $(function()
'empty': null, // Remove as it will be handle by the "null" value in the "MATCHES" operator
'not_empty': null, // Remove as it will be handle by the "null" value in the "MATCHES" operator
},
// Null value
'null_value': {
'code': '',
'label': Dict.S('Enum:Undefined'),
},
},

View File

@@ -157,8 +157,7 @@ class CriterionToOQL extends CriterionConversionAbstract
{
$aValues = self::GetValues($aCriteria);
$sValue = self::GetValue($aValues, 0);
if (empty($sValue))
if (empty($sValue) && (!(isset($aCriteria['has_undefined'])) || !($aCriteria['has_undefined'])))
{
return "1";
}
@@ -183,17 +182,34 @@ class CriterionToOQL extends CriterionConversionAbstract
{
$aValues = self::GetValues($aCriteria);
$aRawValues = array();
$bHasUnDefined = isset($aCriteria['has_undefined']) ? $aCriteria['has_undefined'] : false;
for($i = 0; $i < count($aValues); $i++)
{
$aRawValues[] = self::GetValue($aValues, $i);
$sRawValue = self::GetValue($aValues, $i);
if (strlen($sRawValue) == 0)
{
$bHasUnDefined = true;
}
else
{
$aRawValues[] = $sRawValue;
}
}
$sValue = implode(' ', $aRawValues);
if (empty($sValue))
{
if ($bHasUnDefined)
{
return "({$sRef} = '')";
}
return "1";
}
if ($bHasUnDefined)
{
return "((({$sRef} MATCHES '{$sValue}') OR ({$sRef} = '')) AND 1)";
}
return "({$sRef} MATCHES '{$sValue}')";
}

View File

@@ -674,8 +674,18 @@ class CriterionToSearchForm extends CriterionConversionAbstract
{
case 'MATCHES':
// Nothing special to do
if (isset($aCriteria['has_undefined']) && $aCriteria['has_undefined'])
{
if (!isset($aCriteria['values']))
{
$aCriteria['values'] = array();
}
// Convention for 'undefined' tag set
$aCriteria['values'][] = array('value' => '', 'label' => Dict::S('Enum:Undefined'));
}
break;
case 'OR':
case 'ISNULL':
$aCriteria['operator'] = CriterionConversionAbstract::OP_EQUALS;
if (isset($aCriteria['has_undefined']) && $aCriteria['has_undefined'])
@@ -684,8 +694,21 @@ class CriterionToSearchForm extends CriterionConversionAbstract
{
$aCriteria['values'] = array();
}
// Convention for 'undefined' enums
$aCriteria['values'][] = array('value' => 'null', 'label' => Dict::S('Enum:Undefined'));
// Convention for 'undefined' tag set
$aCriteria['values'][] = array('value' => '', 'label' => Dict::S('Enum:Undefined'));
}
break;
case '=':
$aCriteria['operator'] = CriterionConversionAbstract::OP_EQUALS;
if (isset($aCriteria['has_undefined']) && $aCriteria['has_undefined'])
{
if (!isset($aCriteria['values']))
{
$aCriteria['values'] = array();
}
// Convention for 'undefined' tag set
$aCriteria['values'][] = array('value' => '', 'label' => Dict::S('Enum:Undefined'));
}
break;

View File

@@ -468,7 +468,16 @@ class CriterionConversionTest extends ItopDataTestCase
'ExpectedOQL' => "SELECT `UserRequest` FROM UserRequest AS `UserRequest` WHERE `UserRequest`.`tagfield` MATCHES 'tag1 tag2'",
'ExpectedCriterion' => array(array('widget' => 'tag_set')),
),
'TagSet Undefined' => array(
'OQL' => "SELECT UserRequest WHERE tagfield = ''",
'ExpectedOQL' => "SELECT `UserRequest` FROM UserRequest AS `UserRequest` WHERE (`UserRequest`.`tagfield` = '')",
'ExpectedCriterion' => array(array('widget' => 'tag_set')),
),
'TagSet Undefined and tag' => array(
'OQL' => "SELECT UserRequest WHERE (((tagfield MATCHES 'tag1 tag2') OR (tagfield = '')) AND 1)",
'ExpectedOQL' => "SELECT `UserRequest` FROM UserRequest AS `UserRequest` WHERE ((`UserRequest`.`tagfield` MATCHES 'tag1 tag2' OR (`UserRequest`.`tagfield` = '')) AND 1)",
'ExpectedCriterion' => array(array('widget' => 'tag_set')),
),
);
}