Skip to content

Commit

Permalink
Merge pull request #16579 from seamuslee001/dev_core_1602_522
Browse files Browse the repository at this point in the history
dev/core#1602 Fix join date filtering in search builder by standardis…
  • Loading branch information
seamuslee001 authored Feb 18, 2020
2 parents 547c4b9 + 7dd656d commit 9fe495e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
28 changes: 26 additions & 2 deletions CRM/Member/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,32 @@ public static function whereClauseSingle(&$values, &$query) {
list($name, $op, $value, $grouping) = $values;
$fields = self::getFields();

$quoteValue = NULL;

if (!empty($value) && !is_array($value)) {
$quoteValue = "\"$value\"";
}

$fieldAliases = self::getLegacySupportedFields();

$fieldName = $name = self::getFieldName($values);
$qillName = $name;
if (in_array($name, $fieldAliases)) {
$qillName = array_search($name, $fieldAliases);
}
$pseudoExtraParam = [];
$fieldSpec = CRM_Utils_Array::value($fieldName, $fields, []);
$tableName = CRM_Utils_Array::value('table_name', $fieldSpec, 'civicrm_membership');
$dataType = CRM_Utils_Type::typeToString(CRM_Utils_Array::value('type', $fieldSpec));
if ($dataType === 'Timestamp' || $dataType === 'Date') {
$title = empty($fieldSpec['unique_title']) ? $fieldSpec['title'] : $fieldSpec['unique_title'];
$query->_tables['civicrm_membership'] = $query->_whereTables['civicrm_membership'] = 1;
$query->dateQueryBuilder($values,
$tableName, $fieldName, $fieldSpec['name'], $title
);
return;
}

switch ($name) {
case 'member_join_date_low':
case 'member_join_date_high':
Expand All @@ -167,7 +193,6 @@ public static function whereClauseSingle(&$values, &$query) {
);
return;

case 'membership_start_date':
case 'member_start_date_low':
case 'member_start_date_high':
Civi::log()->warning(
Expand All @@ -181,7 +206,6 @@ public static function whereClauseSingle(&$values, &$query) {
);
return;

case 'membership_end_date':
case 'member_end_date_low':
case 'member_end_date_high':
Civi::log()->warning(
Expand Down
43 changes: 43 additions & 0 deletions tests/phpunit/CRM/Member/BAO/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,47 @@ public function testConvertEntityFieldMultipleValueEntityRefDoubleRun() {
$this->assertEquals(['civicrm_membership.membership_type_id IN ("1", "2")'], $obj->_where[0]);
}

/**
* Membership Date fields
* @return array
*/
public function membershipDateFields() {
$fields = [];
$fields[] = ['membership_join_date'];
$fields[] = ['membership_start_date'];
$fields[] = ['membership_end_date'];
return $fields;
}

/**
* Test generating a correct where clause for date fields as generated by search builder
* @dataProvider membershipDateFields
*/
public function testMembershipDateWhereSearchBuilder($dateField) {
$dbDateField = str_replace('membership_', '', $dateField);
$formValues = [
'mapper' => [
1 => [['Membership', $dateField], [''], [''], [''], ['']],
2 => [[''], [''], [''], [''], ['']],
3 => [[''], [''], [''], [''], ['']],
],
'operator' => [
1 => ['<=', '', '', '', ''],
2 => ['', '', '', '', ''],
3 => ['', '', '', '', ''],
],
'value' => [
1 => ['20200201', '', '', '', ''],
2 => ['', '', '', '', '', ''],
3 => ['', '', '', '', '', ''],
],
'radio_ts' => '',
];
$searchBuilderForm = new CRM_Contact_Form_Search_Builder();
$params = [[$dateField, "<=", "20200201", 1, 0]];
$this->assertEquals($params, $searchBuilderForm->convertFormValues($formValues));
$obj = new CRM_Contact_BAO_Query($params);
$this->assertEquals(['civicrm_membership.' . $dbDateField . ' <= \'20200201000000\''], $obj->_where[1]);
}

}

0 comments on commit 9fe495e

Please sign in to comment.