Merge pull request #20381 from seamuslee001/api_v4_phpunit8_warnings
[civicrm-core.git] / tests / phpunit / CRM / Contact / BAO / SavedSearchTest.php
1 <?php
2 /*
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 +--------------------------------------------------------------------+
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
20 use CRMTraits_Custom_CustomDataTrait;
21
22 /**
23 * Tears down the fixture, for example, closes a network connection.
24 *
25 * This method is called after a test is executed.
26 */
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']}");
33 }
34 }
35 $this->quickCleanup([
36 'civicrm_mapping_field',
37 'civicrm_mapping',
38 'civicrm_group',
39 'civicrm_saved_search',
40 'civicrm_custom_field',
41 'civicrm_custom_group',
42 ]);
43 }
44
45 /**
46 * Test setDefaults for privacy radio buttons.
47 *
48 * @throws \Exception
49 */
50 public function testDefaultValues() {
51 $this->createCustomGroupWithFieldOfType([], 'int');
52 $sg = new CRM_Contact_Form_Search_Advanced();
53 $sg->controller = new CRM_Core_Controller();
54 $formValues = [
55 'group_search_selected' => 'group',
56 'privacy_options' => ['do_not_email'],
57 'privacy_operator' => 'OR',
58 'privacy_toggle' => 2,
59 'operator' => 'AND',
60 'component_mode' => 1,
61 'custom_' . $this->ids['CustomField']['int'] . '_from' => 0,
62 'custom_' . $this->ids['CustomField']['int'] . '_to' => '',
63 ];
64 CRM_Core_DAO::executeQuery(
65 "INSERT INTO civicrm_saved_search (form_values) VALUES('" . serialize($formValues) . "')"
66 );
67 $ssID = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()');
68 $sg->set('ssID', $ssID);
69 $sg->set('formValues', $formValues);
70
71 $defaults = $sg->setDefaultValues();
72
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);
78 }
79
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
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()'));
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);
122 }
123 }
124
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();
132 $formValues = [
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,
139 ];
140 $queryParams = [];
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()'));
146 $expectedResult = [
147 'operator' => 'AND',
148 'component_mode' => CRM_Contact_BAO_Query::MODE_CONTACTS,
149 'display_relationship_type' => "{$relTypeID}_a_b",
150 'uf_group_id' => 1,
151 ];
152 $this->checkArrayEquals($result, $expectedResult);
153 }
154
155 /**
156 * Get variants of the fields we want to test.
157 *
158 * @return array
159 */
160 public function getSavedSearches() {
161 $return = [];
162 $searches = $this->getSearches();
163 foreach ($searches as $key => $search) {
164 $return[] = [$search['form_values'], $search['expected'], $key];
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() {
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 ];
249 }
250
251 }