Commit | Line | Data |
---|---|---|
e5ad0335 E |
1 | <?php |
2 | /* | |
7d61e75f TO |
3 | +--------------------------------------------------------------------+ |
4 | | Copyright CiviCRM LLC. All rights reserved. | | |
5 | | | | |
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 | +--------------------------------------------------------------------+ | |
e5ad0335 E |
10 | */ |
11 | ||
12 | /** | |
13 | * Test class for CRM_Contact_BAO_Group BAO | |
14 | * | |
15 | * @package CiviCRM | |
16 | * @group headless | |
17 | */ | |
18 | class CRM_Contact_BAO_SavedSearchTest extends CiviUnitTestCase { | |
19 | ||
c0b26fcd SL |
20 | use CRMTraits_Custom_CustomDataTrait; |
21 | ||
e5ad0335 E |
22 | /** |
23 | * Tears down the fixture, for example, closes a network connection. | |
24 | * | |
25 | * This method is called after a test is executed. | |
26 | */ | |
594a9328 | 27 | protected function tearDown(): void { |
c0b26fcd SL |
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']}"); | |
33 | } | |
34 | } | |
9099cab3 | 35 | $this->quickCleanup([ |
e5ad0335 E |
36 | 'civicrm_mapping_field', |
37 | 'civicrm_mapping', | |
38 | 'civicrm_group', | |
39 | 'civicrm_saved_search', | |
c0b26fcd SL |
40 | 'civicrm_custom_field', |
41 | 'civicrm_custom_group', | |
9099cab3 | 42 | ]); |
e5ad0335 E |
43 | } |
44 | ||
41e6c841 | 45 | /** |
46 | * Test setDefaults for privacy radio buttons. | |
e9f51713 | 47 | * |
48 | * @throws \Exception | |
41e6c841 | 49 | */ |
50 | public function testDefaultValues() { | |
c0b26fcd | 51 | $this->createCustomGroupWithFieldOfType([], 'int'); |
41e6c841 | 52 | $sg = new CRM_Contact_Form_Search_Advanced(); |
53 | $sg->controller = new CRM_Core_Controller(); | |
e9f51713 | 54 | $formValues = [ |
41e6c841 | 55 | 'group_search_selected' => 'group', |
9099cab3 | 56 | 'privacy_options' => ['do_not_email'], |
41e6c841 | 57 | 'privacy_operator' => 'OR', |
58 | 'privacy_toggle' => 2, | |
59 | 'operator' => 'AND', | |
60 | 'component_mode' => 1, | |
c0b26fcd SL |
61 | 'custom_' . $this->ids['CustomField']['int'] . '_from' => 0, |
62 | 'custom_' . $this->ids['CustomField']['int'] . '_to' => '', | |
9099cab3 | 63 | ]; |
41e6c841 | 64 | CRM_Core_DAO::executeQuery( |
e9f51713 | 65 | "INSERT INTO civicrm_saved_search (form_values) VALUES('" . serialize($formValues) . "')" |
41e6c841 | 66 | ); |
67 | $ssID = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()'); | |
68 | $sg->set('ssID', $ssID); | |
e9f51713 | 69 | $sg->set('formValues', $formValues); |
41e6c841 | 70 | |
71 | $defaults = $sg->setDefaultValues(); | |
72 | ||
e9f51713 | 73 | $this->checkArrayEquals($defaults, $formValues); |
241ee767 | 74 | $this->callAPISuccess('CustomField', 'delete', ['id' => $this->ids['CustomField']['int']]); |
6d8d4c91 | 75 | unset($this->ids['CustomField']['int']); |
241ee767 | 76 | $defaults = $sg->setDefaultValues(); |
6d8d4c91 | 77 | $this->checkArrayEquals($defaults, $formValues); |
41e6c841 | 78 | } |
79 | ||
c0b26fcd SL |
80 | /** |
81 | * Test setDefaults for privacy radio buttons. | |
82 | * | |
83 | * @throws \Exception | |
84 | */ | |
85 | public function testGetFormValuesWithCustomFields() { | |
86 | $this->createCustomGroupWithFieldsOfAllTypes(); | |
87 | $sg = new CRM_Contact_Form_Search_Advanced(); | |
88 | $sg->controller = new CRM_Core_Controller(); | |
89 | $formValues = [ | |
90 | 'group_search_selected' => 'group', | |
91 | 'privacy_options' => ['do_not_email'], | |
92 | 'privacy_operator' => 'OR', | |
93 | 'privacy_toggle' => 2, | |
94 | 'operator' => 'AND', | |
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', | |
100 | ]; | |
101 | CRM_Core_DAO::executeQuery( | |
102 | "INSERT INTO civicrm_saved_search (form_values) VALUES('" . serialize($formValues) . "')" | |
103 | ); | |
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); | |
107 | } | |
108 | ||
e5ad0335 E |
109 | /** |
110 | * Test fixValues function. | |
111 | * | |
112 | * @dataProvider getSavedSearches | |
113 | */ | |
114 | public function testGetFormValues($formValues, $expectedResult, $searchDescription) { | |
115 | CRM_Core_DAO::executeQuery( | |
116 | "INSERT INTO civicrm_saved_search (form_values) VALUES('" . serialize($formValues) . "')" | |
117 | ); | |
118 | $result = CRM_Contact_BAO_SavedSearch::getFormValues(CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()')); | |
9099cab3 | 119 | $this->assertEquals(['membership_type_id', 'membership_status_id'], array_keys($result)); |
e5ad0335 E |
120 | foreach ($result as $key => $value) { |
121 | $this->assertEquals($expectedResult, $value, 'failure on set ' . $searchDescription); | |
122 | } | |
123 | } | |
124 | ||
ce023e5d JP |
125 | /** |
126 | * Test if skipped elements are correctly | |
127 | * stored and retrieved as formvalues. | |
128 | */ | |
129 | public function testSkippedElements() { | |
130 | $relTypeID = $this->relationshipTypeCreate(); | |
131 | $savedSearch = new CRM_Contact_BAO_SavedSearch(); | |
9099cab3 | 132 | $formValues = [ |
ce023e5d JP |
133 | 'operator' => 'AND', |
134 | 'title' => 'testsmart', | |
135 | 'radio_ts' => 'ts_all', | |
136 | 'component_mode' => CRM_Contact_BAO_Query::MODE_CONTACTS, | |
137 | 'display_relationship_type' => "{$relTypeID}_a_b", | |
138 | 'uf_group_id' => 1, | |
9099cab3 CW |
139 | ]; |
140 | $queryParams = []; | |
ce023e5d JP |
141 | CRM_Contact_BAO_SavedSearch::saveSkippedElement($queryParams, $formValues); |
142 | $savedSearch->form_values = serialize($queryParams); | |
143 | $savedSearch->save(); | |
144 | ||
145 | $result = CRM_Contact_BAO_SavedSearch::getFormValues(CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()')); | |
9099cab3 | 146 | $expectedResult = [ |
ce023e5d JP |
147 | 'operator' => 'AND', |
148 | 'component_mode' => CRM_Contact_BAO_Query::MODE_CONTACTS, | |
149 | 'display_relationship_type' => "{$relTypeID}_a_b", | |
150 | 'uf_group_id' => 1, | |
9099cab3 | 151 | ]; |
ce023e5d JP |
152 | $this->checkArrayEquals($result, $expectedResult); |
153 | } | |
154 | ||
e5ad0335 E |
155 | /** |
156 | * Get variants of the fields we want to test. | |
157 | * | |
158 | * @return array | |
159 | */ | |
160 | public function getSavedSearches() { | |
9099cab3 | 161 | $return = []; |
e5ad0335 E |
162 | $searches = $this->getSearches(); |
163 | foreach ($searches as $key => $search) { | |
9099cab3 | 164 | $return[] = [$search['form_values'], $search['expected'], $key]; |
e5ad0335 E |
165 | } |
166 | return $return; | |
167 | } | |
168 | ||
169 | /** | |
170 | * Get variants of potential saved form values. | |
171 | * | |
172 | * Note that we include 1 in various ways to cover the possibility that 1 is treated as a boolean. | |
173 | * | |
174 | * @return array | |
175 | */ | |
176 | public function getSearches() { | |
9099cab3 CW |
177 | return [ |
178 | 'checkbox_format_1_first' => [ | |
179 | 'form_values' => [ | |
180 | 'member_membership_type_id' => [1 => 1, 2 => 1], | |
181 | 'member_status_id' => [1 => 1, 2 => 1], | |
182 | ], | |
183 | 'expected' => [1, 2], | |
184 | ], | |
185 | 'checkbox_format_1_later' => [ | |
186 | 'form_values' => [ | |
187 | 'member_membership_type_id' => [2 => 1, 1 => 1], | |
188 | 'member_status_id' => [2 => 1, 1 => 1], | |
189 | ], | |
190 | 'expected' => [2, 1], | |
191 | ], | |
192 | 'checkbox_format_single_use_1' => [ | |
193 | 'form_values' => [ | |
194 | 'member_membership_type_id' => [1 => 1], | |
195 | 'member_status_id' => [1 => 1], | |
196 | ], | |
197 | 'expected' => [1], | |
198 | ], | |
199 | 'checkbox_format_single_not_1' => [ | |
200 | 'form_values' => [ | |
201 | 'member_membership_type_id' => [2 => 1], | |
202 | 'member_status_id' => [2 => 1], | |
203 | ], | |
204 | 'expected' => [2], | |
205 | ], | |
206 | 'array_format' => [ | |
207 | 'form_values' => [ | |
208 | 'member_membership_type_id' => [1, 2], | |
209 | 'member_status_id' => [1, 2], | |
210 | ], | |
211 | 'expected' => [1, 2], | |
212 | ], | |
213 | 'array_format_1_later' => [ | |
214 | 'form_values' => [ | |
215 | 'member_membership_type_id' => [2, 1], | |
216 | 'member_status_id' => [2, 1], | |
217 | ], | |
218 | 'expected' => [2, 1], | |
219 | ], | |
220 | 'array_format_single_use_1' => [ | |
221 | 'form_values' => [ | |
222 | 'member_membership_type_id' => [1], | |
223 | 'member_status_id' => [1], | |
224 | ], | |
225 | 'expected' => [1], | |
226 | ], | |
227 | 'array_format_single_not_1' => [ | |
228 | 'form_values' => [ | |
229 | 'member_membership_type_id' => [2], | |
230 | 'member_status_id' => [2], | |
231 | ], | |
232 | 'expected' => [2], | |
233 | ], | |
234 | 'IN_format_single_not_1' => [ | |
235 | 'form_values' => [ | |
236 | 'membership_type_id' => ['IN' => [2]], | |
237 | 'membership_status_id' => ['IN' => [2]], | |
238 | ], | |
239 | 'expected' => [2], | |
240 | ], | |
241 | 'IN_format_1_later' => [ | |
242 | 'form_values' => [ | |
243 | 'membership_type_id' => ['IN' => [2, 1]], | |
244 | 'membership_status_id' => ['IN' => [2, 1]], | |
245 | ], | |
246 | 'expected' => [2, 1], | |
247 | ], | |
248 | ]; | |
e5ad0335 E |
249 | } |
250 | ||
251 | } |