}
}
}
- if (substr($element, 0, 7) == 'custom_' &&
- (substr($element, -5, 5) == '_from' || substr($element, -3, 3) == '_to')
- ) {
- // Ensure the _relative field is set if from or to are set to ensure custom date
- // fields with 'from' or 'to' values are displayed when the are set in the smart group
- // being loaded. (CRM-17116)
- if (!isset($result[CRM_Contact_BAO_Query::getCustomFieldName($element) . '_relative'])) {
- $result[CRM_Contact_BAO_Query::getCustomFieldName($element) . '_relative'] = 0;
+ // We should only set the relative key for custom date fields if it is not already set in the array.
+ $realField = str_replace(['_relative', '_low', '_high', '_to', '_high'], '', $element);
+ if (substr($element, 0, 7) == 'custom_' && CRM_Contact_BAO_Query::isCustomDateField($realField)) {
+ if (!isset($result[$realField . '_relative'])) {
+ $result[$realField . '_relative'] = 0;
}
}
// check to see if we need to convert the old privacy array
*/
class CRM_Contact_BAO_SavedSearchTest extends CiviUnitTestCase {
+ use CRMTraits_Custom_CustomDataTrait;
+
/**
* Sets up the fixture, for example, opens a network connection.
*
* This method is called after a test is executed.
*/
protected function tearDown() {
+ if (!empty($this->ids['CustomField'])) {
+ foreach ($this->ids['CustomField'] as $type => $id) {
+ $field = civicrm_api3('CustomField', 'getsingle', ['id' => $id]);
+ $group = civicrm_api3('CustomGroup', 'getsingle', ['id' => $field['custom_group_id']]);
+ CRM_Core_DAO::executeQuery("DROP TABLE IF Exists {$group['table_name']}");
+ }
+ }
$this->quickCleanup([
'civicrm_mapping_field',
'civicrm_mapping',
'civicrm_group',
'civicrm_saved_search',
+ 'civicrm_custom_field',
+ 'civicrm_custom_group',
]);
}
* @throws \Exception
*/
public function testDefaultValues() {
+ $this->createCustomGroupWithFieldOfType([], 'int');
$sg = new CRM_Contact_Form_Search_Advanced();
$sg->controller = new CRM_Core_Controller();
$formValues = [
'privacy_toggle' => 2,
'operator' => 'AND',
'component_mode' => 1,
+ 'custom_' . $this->ids['CustomField']['int'] . '_from' => 0,
+ 'custom_' . $this->ids['CustomField']['int'] . '_to' => '',
];
CRM_Core_DAO::executeQuery(
"INSERT INTO civicrm_saved_search (form_values) VALUES('" . serialize($formValues) . "')"
$this->checkArrayEquals($defaults, $formValues);
}
+ /**
+ * Test setDefaults for privacy radio buttons.
+ *
+ * @throws \Exception
+ */
+ public function testGetFormValuesWithCustomFields() {
+ $this->createCustomGroupWithFieldsOfAllTypes();
+ $sg = new CRM_Contact_Form_Search_Advanced();
+ $sg->controller = new CRM_Core_Controller();
+ $formValues = [
+ 'group_search_selected' => 'group',
+ 'privacy_options' => ['do_not_email'],
+ 'privacy_operator' => 'OR',
+ 'privacy_toggle' => 2,
+ 'operator' => 'AND',
+ 'component_mode' => 1,
+ 'custom_' . $this->ids['CustomField']['int'] . '_from' => 0,
+ 'custom_' . $this->ids['CustomField']['int'] . '_to' => '',
+ 'custom_' . $this->ids['CustomField']['select_date'] . '_high' => '2019-06-30',
+ 'custom_' . $this->ids['CustomField']['select_date'] . '_low' => '2019-06-30',
+ ];
+ CRM_Core_DAO::executeQuery(
+ "INSERT INTO civicrm_saved_search (form_values) VALUES('" . serialize($formValues) . "')"
+ );
+ $returnedFormValues = CRM_Contact_BAO_SavedSearch::getFormValues(CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()'));
+ $checkFormValues = $formValues + ['custom_' . $this->ids['CustomField']['select_date'] . '_relative' => 0];
+ $this->checkArrayEquals($returnedFormValues, $checkFormValues);
+ }
+
/**
* Test fixValues function.
*