3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Test class for CRM_Contact_BAO_Group BAO
18 class CRM_Contact_BAO_SavedSearchTest
extends CiviUnitTestCase
{
20 use CRMTraits_Custom_CustomDataTrait
;
23 * Tears down the fixture, for example, closes a network connection.
25 * This method is called after a test is executed.
27 protected function tearDown(): void
{
28 if (!empty($this->ids
['CustomField'])) {
29 foreach ($this->ids
['CustomField'] as $type => $id) {
30 $field = civicrm_api3('CustomField', 'getsingle', ['id' => $id]);
31 $group = civicrm_api3('CustomGroup', 'getsingle', ['id' => $field['custom_group_id']]);
32 CRM_Core_DAO
::executeQuery("DROP TABLE IF Exists {$group['table_name']}");
36 'civicrm_mapping_field',
39 'civicrm_saved_search',
40 'civicrm_custom_field',
41 'civicrm_custom_group',
46 * Test setDefaults for privacy radio buttons.
50 public function testDefaultValues() {
51 $this->createCustomGroupWithFieldOfType([], 'int');
52 $sg = new CRM_Contact_Form_Search_Advanced();
53 $sg->controller
= new CRM_Core_Controller();
55 'group_search_selected' => 'group',
56 'privacy_options' => ['do_not_email'],
57 'privacy_operator' => 'OR',
58 'privacy_toggle' => 2,
60 'component_mode' => 1,
61 'custom_' . $this->ids
['CustomField']['int'] . '_from' => 0,
62 'custom_' . $this->ids
['CustomField']['int'] . '_to' => '',
64 CRM_Core_DAO
::executeQuery(
65 "INSERT INTO civicrm_saved_search (form_values) VALUES('" . serialize($formValues) . "')"
67 $ssID = CRM_Core_DAO
::singleValueQuery('SELECT LAST_INSERT_ID()');
68 $sg->set('ssID', $ssID);
69 $sg->set('formValues', $formValues);
71 $defaults = $sg->setDefaultValues();
73 $this->checkArrayEquals($defaults, $formValues);
74 $this->callAPISuccess('CustomField', 'delete', ['id' => $this->ids
['CustomField']['int']]);
75 unset($this->ids
['CustomField']['int']);
76 $defaults = $sg->setDefaultValues();
77 $this->checkArrayEquals($defaults, $formValues);
81 * Test setDefaults for privacy radio buttons.
85 public function testGetFormValuesWithCustomFields() {
86 $this->createCustomGroupWithFieldsOfAllTypes();
87 $sg = new CRM_Contact_Form_Search_Advanced();
88 $sg->controller
= new CRM_Core_Controller();
90 'group_search_selected' => 'group',
91 'privacy_options' => ['do_not_email'],
92 'privacy_operator' => 'OR',
93 'privacy_toggle' => 2,
95 'component_mode' => 1,
96 'custom_' . $this->ids
['CustomField']['int'] . '_from' => 0,
97 'custom_' . $this->ids
['CustomField']['int'] . '_to' => '',
98 'custom_' . $this->ids
['CustomField']['select_date'] . '_high' => '2019-06-30',
99 'custom_' . $this->ids
['CustomField']['select_date'] . '_low' => '2019-06-30',
101 CRM_Core_DAO
::executeQuery(
102 "INSERT INTO civicrm_saved_search (form_values) VALUES('" . serialize($formValues) . "')"
104 $returnedFormValues = CRM_Contact_BAO_SavedSearch
::getFormValues(CRM_Core_DAO
::singleValueQuery('SELECT LAST_INSERT_ID()'));
105 $checkFormValues = $formValues +
['custom_' . $this->ids
['CustomField']['select_date'] . '_relative' => 0];
106 $this->checkArrayEquals($returnedFormValues, $checkFormValues);
110 * Test fixValues function.
112 * @dataProvider getSavedSearches
114 public function testGetFormValues($formValues, $expectedResult, $searchDescription) {
115 CRM_Core_DAO
::executeQuery(
116 "INSERT INTO civicrm_saved_search (form_values) VALUES('" . serialize($formValues) . "')"
118 $result = CRM_Contact_BAO_SavedSearch
::getFormValues(CRM_Core_DAO
::singleValueQuery('SELECT LAST_INSERT_ID()'));
119 $this->assertEquals(['membership_type_id', 'membership_status_id'], array_keys($result));
120 foreach ($result as $key => $value) {
121 $this->assertEquals($expectedResult, $value, 'failure on set ' . $searchDescription);
126 * Test if skipped elements are correctly
127 * stored and retrieved as formvalues.
129 public function testSkippedElements() {
130 $relTypeID = $this->relationshipTypeCreate();
131 $savedSearch = new CRM_Contact_BAO_SavedSearch();
134 'title' => 'testsmart',
135 'radio_ts' => 'ts_all',
136 'component_mode' => CRM_Contact_BAO_Query
::MODE_CONTACTS
,
137 'display_relationship_type' => "{$relTypeID}_a_b",
141 CRM_Contact_BAO_SavedSearch
::saveSkippedElement($queryParams, $formValues);
142 $savedSearch->form_values
= serialize($queryParams);
143 $savedSearch->save();
145 $result = CRM_Contact_BAO_SavedSearch
::getFormValues(CRM_Core_DAO
::singleValueQuery('SELECT LAST_INSERT_ID()'));
148 'component_mode' => CRM_Contact_BAO_Query
::MODE_CONTACTS
,
149 'display_relationship_type' => "{$relTypeID}_a_b",
152 $this->checkArrayEquals($result, $expectedResult);
156 * Get variants of the fields we want to test.
160 public function getSavedSearches() {
162 $searches = $this->getSearches();
163 foreach ($searches as $key => $search) {
164 $return[] = [$search['form_values'], $search['expected'], $key];
170 * Get variants of potential saved form values.
172 * Note that we include 1 in various ways to cover the possibility that 1 is treated as a boolean.
176 public function getSearches() {
178 'checkbox_format_1_first' => [
180 'member_membership_type_id' => [1 => 1, 2 => 1],
181 'member_status_id' => [1 => 1, 2 => 1],
183 'expected' => [1, 2],
185 'checkbox_format_1_later' => [
187 'member_membership_type_id' => [2 => 1, 1 => 1],
188 'member_status_id' => [2 => 1, 1 => 1],
190 'expected' => [2, 1],
192 'checkbox_format_single_use_1' => [
194 'member_membership_type_id' => [1 => 1],
195 'member_status_id' => [1 => 1],
199 'checkbox_format_single_not_1' => [
201 'member_membership_type_id' => [2 => 1],
202 'member_status_id' => [2 => 1],
208 'member_membership_type_id' => [1, 2],
209 'member_status_id' => [1, 2],
211 'expected' => [1, 2],
213 'array_format_1_later' => [
215 'member_membership_type_id' => [2, 1],
216 'member_status_id' => [2, 1],
218 'expected' => [2, 1],
220 'array_format_single_use_1' => [
222 'member_membership_type_id' => [1],
223 'member_status_id' => [1],
227 'array_format_single_not_1' => [
229 'member_membership_type_id' => [2],
230 'member_status_id' => [2],
234 'IN_format_single_not_1' => [
236 'membership_type_id' => ['IN' => [2]],
237 'membership_status_id' => ['IN' => [2]],
241 'IN_format_1_later' => [
243 'membership_type_id' => ['IN' => [2, 1]],
244 'membership_status_id' => ['IN' => [2, 1]],
246 'expected' => [2, 1],