Skip to content

Commit

Permalink
Merge pull request #12909 from eileenmcnaughton/57reg
Browse files Browse the repository at this point in the history
Ensure hard-coded date ranges are preserved in smart groups
  • Loading branch information
eileenmcnaughton committed Oct 9, 2018
2 parents 585d646 + 55ea4c3 commit d037704
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 12 deletions.
34 changes: 22 additions & 12 deletions CRM/Contact/BAO/SavedSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,20 +124,30 @@ public static function getFormValues($id) {
continue;
}
}
// Check for a date range field, which might be a standard date
// range or a relative date.
if (strpos($id, '_date_low') !== FALSE || strpos($id, '_date_high') !== FALSE) {
$entityName = strstr($id, '_date', TRUE);
if (!empty($result['relative_dates']) && array_key_exists($entityName, $result['relative_dates'])) {
$result[$id] = NULL;
$result["{$entityName}_date_relative"] = $result['relative_dates'][$entityName];
}
elseif (!empty($specialDateFields[$id])) {
$entityName = strstr($specialDateFields[$id], '_date', TRUE);
$result[$id] = NULL;
$result["{$entityName}_relative"] = $result['relative_dates'][$entityName];
}
else {
$result[$id] = $value;
$result["{$entityName}_date_relative"] = 0;

// This is the default, for non relative dates. We will overwrite
// it if we determine this is a relative date.
$result[$id] = $value;
$result["{$entityName}_date_relative"] = 0;

if (!empty($result['relative_dates'])) {
if (array_key_exists($entityName, $result['relative_dates'])) {
// We have a match from a regular field.
$result[$id] = NULL;
$result["{$entityName}_date_relative"] = $result['relative_dates'][$entityName];
}
elseif (!empty($specialDateFields[$id])) {
// We may have a match on a special date field.
$entityName = strstr($specialDateFields[$id], '_date', TRUE);
if (array_key_exists($entityName, $result['relative_dates'])) {
$result[$id] = NULL;
$result["{$entityName}_relative"] = $result['relative_dates'][$entityName];
}
}
}
}
else {
Expand Down
46 changes: 46 additions & 0 deletions tests/phpunit/CRM/Contact/BAO/SavedSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,52 @@ public function testGetFormValues($formValues, $expectedResult, $searchDescripti
}
}

/**
* Test if dates ranges are stored correctly
* in civicrm_saved_search table and are
* extracted properly.
*/
public function testDateRange() {
$savedSearch = new CRM_Contact_BAO_SavedSearch();
$formValues = array(
'hidden_basic' => 1,
'group_search_selected' => 'group',
'component_mode' => 1,
'operator' => 'AND',
'privacy_operator' => 'OR',
'privacy_toggle' => 1,
'participant_register_date_low' => '01/01/2009',
'participant_register_date_high' => '01/01/2018',
'radio_ts' => 'ts_all',
'title' => 'bah bah bah',
);

$queryParams = array(
0 => array(
0 => 'participant_register_date_low',
1 => '=',
2 => '01/01/2009',
3 => 0,
4 => 0,
),
1 => array(
0 => 'participant_register_date_high',
1 => '=',
2 => '01/01/2018',
3 => 0,
4 => 0,
),
);

CRM_Contact_BAO_SavedSearch::saveRelativeDates($queryParams, $formValues);
CRM_Contact_BAO_SavedSearch::saveSkippedElement($queryParams, $formValues);
$savedSearch->form_values = serialize($queryParams);
$savedSearch->save();

$result = CRM_Contact_BAO_SavedSearch::getFormValues(CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()'));
$this->assertEquals('01/01/2009', $result['participant_register_date_low']);
$this->assertEquals('01/01/2018', $result['participant_register_date_high']);
}
/**
* Test if relative dates are stored correctly
* in civicrm_saved_search table.
Expand Down

0 comments on commit d037704

Please sign in to comment.