quickCleanup([ 'civicrm_mapping_field', 'civicrm_mapping', 'civicrm_group', 'civicrm_saved_search', ]); } /** * Test setDefaults for privacy radio buttons. */ public function testDefaultValues() { $sg = new CRM_Contact_Form_Search_Advanced(); $sg->controller = new CRM_Core_Controller(); $sg->_formValues = [ 'group_search_selected' => 'group', 'privacy_options' => ['do_not_email'], 'privacy_operator' => 'OR', 'privacy_toggle' => 2, 'operator' => 'AND', 'component_mode' => 1, ]; CRM_Core_DAO::executeQuery( "INSERT INTO civicrm_saved_search (form_values) VALUES('" . serialize($sg->_formValues) . "')" ); $ssID = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()'); $sg->set('ssID', $ssID); $defaults = $sg->setDefaultValues(); $this->checkArrayEquals($defaults, $sg->_formValues); } /** * Test fixValues function. * * @dataProvider getSavedSearches */ public function testGetFormValues($formValues, $expectedResult, $searchDescription) { CRM_Core_DAO::executeQuery( "INSERT INTO civicrm_saved_search (form_values) VALUES('" . serialize($formValues) . "')" ); $result = CRM_Contact_BAO_SavedSearch::getFormValues(CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()')); $this->assertEquals(['membership_type_id', 'membership_status_id'], array_keys($result)); foreach ($result as $key => $value) { $this->assertEquals($expectedResult, $value, 'failure on set ' . $searchDescription); } } /** * 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 = [ '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 = [ 0 => [ 0 => 'participant_register_date_low', 1 => '=', 2 => '01/01/2009', 3 => 0, 4 => 0, ], 1 => [ 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 skipped elements are correctly * stored and retrieved as formvalues. */ public function testSkippedElements() { $relTypeID = $this->relationshipTypeCreate(); $savedSearch = new CRM_Contact_BAO_SavedSearch(); $formValues = [ 'operator' => 'AND', 'title' => 'testsmart', 'radio_ts' => 'ts_all', 'component_mode' => CRM_Contact_BAO_Query::MODE_CONTACTS, 'display_relationship_type' => "{$relTypeID}_a_b", 'uf_group_id' => 1, ]; $queryParams = []; 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()')); $expectedResult = [ 'operator' => 'AND', 'component_mode' => CRM_Contact_BAO_Query::MODE_CONTACTS, 'display_relationship_type' => "{$relTypeID}_a_b", 'uf_group_id' => 1, ]; $this->checkArrayEquals($result, $expectedResult); } /** * Test if relative dates are stored correctly * in civicrm_saved_search table. */ public function testRelativeDateValues() { $savedSearch = new CRM_Contact_BAO_SavedSearch(); $formValues = [ 'operator' => 'AND', 'event_relative' => 'this.month', 'participant_relative' => 'today', 'participant_test' => 0, 'title' => 'testsmart', 'radio_ts' => 'ts_all', ]; $queryParams = []; 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()')); $expectedResult = [ 'event' => 'this.month', 'participant' => 'today', ]; $this->checkArrayEquals($result['relative_dates'], $expectedResult); } /** * Test relative dates * * This is a slightly odd test because it was originally created to test that we DO create a * special 'relative_dates' key but the new favoured format is not to do that and to * save (eg) custom_1_relative = this.day. * * It still presumably provides useful 'this does not fatal or give enotice' coverage. */ public function testCustomFieldRelativeDates() { // Create a custom field. $customGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'relative_date_test_group']); $params = [ 'custom_group_id' => $customGroup['id'], 'name' => 'test_datefield', 'label' => 'Date Field for Testing', 'html_type' => 'Select Date', 'data_type' => 'Date', 'default_value' => NULL, 'weight' => 4, 'is_required' => 1, 'is_searchable' => 1, 'date_format' => 'mm/dd/yy', 'is_active' => 1, ]; $customField = $this->callAPIAndDocument('custom_field', 'create', $params, __FUNCTION__, __FILE__); $id = $customField['id']; $queryParams = [ 0 => [ 0 => "custom_${id}_low", 1 => '=', 2 => '20170425000000', ], 1 => [ 0 => "custom_${id}_high", 1 => '=', 2 => '20170501235959', ], ]; $formValues = [ "custom_${id}_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. $this->assertArrayNotHasKey('relative_dates', $queryParams, 'Relative date in custom field smart group creation failed.'); $dropCustomValueTables = TRUE; $this->quickCleanup(['civicrm_saved_search'], $dropCustomValueTables); } /** * Get variants of the fields we want to test. * * @return array */ public function getSavedSearches() { $return = []; $searches = $this->getSearches(); foreach ($searches as $key => $search) { $return[] = [$search['form_values'], $search['expected'], $key]; } return $return; } /** * Get variants of potential saved form values. * * Note that we include 1 in various ways to cover the possibility that 1 is treated as a boolean. * * @return array */ public function getSearches() { return [ 'checkbox_format_1_first' => [ 'form_values' => [ 'member_membership_type_id' => [1 => 1, 2 => 1], 'member_status_id' => [1 => 1, 2 => 1], ], 'expected' => [1, 2], ], 'checkbox_format_1_later' => [ 'form_values' => [ 'member_membership_type_id' => [2 => 1, 1 => 1], 'member_status_id' => [2 => 1, 1 => 1], ], 'expected' => [2, 1], ], 'checkbox_format_single_use_1' => [ 'form_values' => [ 'member_membership_type_id' => [1 => 1], 'member_status_id' => [1 => 1], ], 'expected' => [1], ], 'checkbox_format_single_not_1' => [ 'form_values' => [ 'member_membership_type_id' => [2 => 1], 'member_status_id' => [2 => 1], ], 'expected' => [2], ], 'array_format' => [ 'form_values' => [ 'member_membership_type_id' => [1, 2], 'member_status_id' => [1, 2], ], 'expected' => [1, 2], ], 'array_format_1_later' => [ 'form_values' => [ 'member_membership_type_id' => [2, 1], 'member_status_id' => [2, 1], ], 'expected' => [2, 1], ], 'array_format_single_use_1' => [ 'form_values' => [ 'member_membership_type_id' => [1], 'member_status_id' => [1], ], 'expected' => [1], ], 'array_format_single_not_1' => [ 'form_values' => [ 'member_membership_type_id' => [2], 'member_status_id' => [2], ], 'expected' => [2], ], 'IN_format_single_not_1' => [ 'form_values' => [ 'membership_type_id' => ['IN' => [2]], 'membership_status_id' => ['IN' => [2]], ], 'expected' => [2], ], 'IN_format_1_later' => [ 'form_values' => [ 'membership_type_id' => ['IN' => [2, 1]], 'membership_status_id' => ['IN' => [2, 1]], ], 'expected' => [2, 1], ], ]; } }