From 776b1cdf404f3b8811b79556b28a76f0b9e75572 Mon Sep 17 00:00:00 2001 From: Jamie McClelland Date: Fri, 14 Sep 2018 11:32:15 -0400 Subject: [PATCH] Ensure relative dates are preserved for custom fields in smart group See https://lab.civicrm.org/dev/core/issues/389 --- CRM/Contact/BAO/SavedSearch.php | 9 ++++-- .../CRM/Contact/BAO/SavedSearchTest.php | 30 +++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/CRM/Contact/BAO/SavedSearch.php b/CRM/Contact/BAO/SavedSearch.php index da2b6fd946..142de53ad7 100644 --- a/CRM/Contact/BAO/SavedSearch.php +++ b/CRM/Contact/BAO/SavedSearch.php @@ -429,7 +429,7 @@ LEFT JOIN civicrm_email ON (contact_a.id = civicrm_email.contact_id AND civicrm_ $relativeDates = array('relative_dates' => array()); $specialDateFields = array('event_relative', 'case_from_relative', 'case_to_relative', 'participant_relative'); foreach ($formValues as $id => $value) { - if ((preg_match('/_date_relative$/', $id) || in_array($id, $specialDateFields)) && !empty($value)) { + if ((preg_match('/(_date|custom_[0-9]+)_relative$/', $id) || in_array($id, $specialDateFields)) && !empty($value)) { $entityName = strstr($id, '_date', TRUE); if (empty($entityName)) { $entityName = strstr($id, '_relative', TRUE); @@ -480,7 +480,12 @@ LEFT JOIN civicrm_email ON (contact_a.id = civicrm_email.contact_id AND civicrm_ // select date range as default if ($isCustomDateField) { - $formValues[$fieldName . '_relative'] = 0; + if (array_key_exists('relative_dates', $formValues) && array_key_exists($fieldName, $formValues['relative_dates'])) { + $formValues[$fieldName . '_relative'] = $formValues['relative_dates'][$fieldName]; + } + else { + $formValues[$fieldName . '_relative'] = 0; + } } switch ($op) { case 'BETWEEN': diff --git a/tests/phpunit/CRM/Contact/BAO/SavedSearchTest.php b/tests/phpunit/CRM/Contact/BAO/SavedSearchTest.php index 71180d5a6d..7a6cb0a772 100644 --- a/tests/phpunit/CRM/Contact/BAO/SavedSearchTest.php +++ b/tests/phpunit/CRM/Contact/BAO/SavedSearchTest.php @@ -127,6 +127,36 @@ class CRM_Contact_BAO_SavedSearchTest extends CiviUnitTestCase { $this->checkArrayEquals($result['relative_dates'], $expectedResult); } + /** + * Test relative dates + * + * The function saveRelativeDates should detect whether a field is using + * a relative date range and include in the fromValues a relative_date + * index so it is properly detects when executed. + */ + public function testCustomFieldRelativeDates() { + $queryParams = array( + 0 => array( + 0 => 'custom_13_low', + 1 => '=', + 2 => '20170425000000', + ), + 1 => array( + 0 => 'custom_13_high', + 1 => '=', + 2 => '20170501235959', + ), + ); + $formValues = array( + 'custom_13_relative' => 'ending.week', + ); + CRM_Contact_BAO_SavedSearch::saveRelativeDates($queryParams, $formValues); + // Since custom_13 doesn't have the word 'date' in it, the key is + // set to 0, rather than the field name. + $err = 'Relative date in custom field smart group creation failed.'; + $this->assertArrayHasKey('relative_dates', $queryParams, $err); + + } /** * Get variants of the fields we want to test. -- 2.25.1