Merge pull request #15989 from seamuslee001/dev_core_523
[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 * Sets up the fixture, for example, opens a network connection.
24 *
25 * This method is called before a test is executed.
26 */
27 protected function setUp() {
28 parent::setUp();
29 }
30
31 /**
32 * Tears down the fixture, for example, closes a network connection.
33 *
34 * This method is called after a test is executed.
35 */
36 protected function tearDown() {
37 if (!empty($this->ids['CustomField'])) {
38 foreach ($this->ids['CustomField'] as $type => $id) {
39 $field = civicrm_api3('CustomField', 'getsingle', ['id' => $id]);
40 $group = civicrm_api3('CustomGroup', 'getsingle', ['id' => $field['custom_group_id']]);
41 CRM_Core_DAO::executeQuery("DROP TABLE IF Exists {$group['table_name']}");
42 }
43 }
44 $this->quickCleanup([
45 'civicrm_mapping_field',
46 'civicrm_mapping',
47 'civicrm_group',
48 'civicrm_saved_search',
49 'civicrm_custom_field',
50 'civicrm_custom_group',
51 ]);
52 }
53
54 /**
55 * Test setDefaults for privacy radio buttons.
56 *
57 * @throws \Exception
58 */
59 public function testDefaultValues() {
60 $this->createCustomGroupWithFieldOfType([], 'int');
61 $sg = new CRM_Contact_Form_Search_Advanced();
62 $sg->controller = new CRM_Core_Controller();
63 $formValues = [
64 'group_search_selected' => 'group',
65 'privacy_options' => ['do_not_email'],
66 'privacy_operator' => 'OR',
67 'privacy_toggle' => 2,
68 'operator' => 'AND',
69 'component_mode' => 1,
70 'custom_' . $this->ids['CustomField']['int'] . '_from' => 0,
71 'custom_' . $this->ids['CustomField']['int'] . '_to' => '',
72 ];
73 CRM_Core_DAO::executeQuery(
74 "INSERT INTO civicrm_saved_search (form_values) VALUES('" . serialize($formValues) . "')"
75 );
76 $ssID = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()');
77 $sg->set('ssID', $ssID);
78 $sg->set('formValues', $formValues);
79
80 $defaults = $sg->setDefaultValues();
81
82 $this->checkArrayEquals($defaults, $formValues);
83 $this->callAPISuccess('CustomField', 'delete', ['id' => $this->ids['CustomField']['int']]);
84 unset($this->ids['CustomField']['int']);
85 $defaults = $sg->setDefaultValues();
86 $this->checkArrayEquals($defaults, $formValues);
87 }
88
89 /**
90 * Test setDefaults for privacy radio buttons.
91 *
92 * @throws \Exception
93 */
94 public function testGetFormValuesWithCustomFields() {
95 $this->createCustomGroupWithFieldsOfAllTypes();
96 $sg = new CRM_Contact_Form_Search_Advanced();
97 $sg->controller = new CRM_Core_Controller();
98 $formValues = [
99 'group_search_selected' => 'group',
100 'privacy_options' => ['do_not_email'],
101 'privacy_operator' => 'OR',
102 'privacy_toggle' => 2,
103 'operator' => 'AND',
104 'component_mode' => 1,
105 'custom_' . $this->ids['CustomField']['int'] . '_from' => 0,
106 'custom_' . $this->ids['CustomField']['int'] . '_to' => '',
107 'custom_' . $this->ids['CustomField']['select_date'] . '_high' => '2019-06-30',
108 'custom_' . $this->ids['CustomField']['select_date'] . '_low' => '2019-06-30',
109 ];
110 CRM_Core_DAO::executeQuery(
111 "INSERT INTO civicrm_saved_search (form_values) VALUES('" . serialize($formValues) . "')"
112 );
113 $returnedFormValues = CRM_Contact_BAO_SavedSearch::getFormValues(CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()'));
114 $checkFormValues = $formValues + ['custom_' . $this->ids['CustomField']['select_date'] . '_relative' => 0];
115 $this->checkArrayEquals($returnedFormValues, $checkFormValues);
116 }
117
118 /**
119 * Test fixValues function.
120 *
121 * @dataProvider getSavedSearches
122 */
123 public function testGetFormValues($formValues, $expectedResult, $searchDescription) {
124 CRM_Core_DAO::executeQuery(
125 "INSERT INTO civicrm_saved_search (form_values) VALUES('" . serialize($formValues) . "')"
126 );
127 $result = CRM_Contact_BAO_SavedSearch::getFormValues(CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()'));
128 $this->assertEquals(['membership_type_id', 'membership_status_id'], array_keys($result));
129 foreach ($result as $key => $value) {
130 $this->assertEquals($expectedResult, $value, 'failure on set ' . $searchDescription);
131 }
132 }
133
134 /**
135 * Test if skipped elements are correctly
136 * stored and retrieved as formvalues.
137 */
138 public function testSkippedElements() {
139 $relTypeID = $this->relationshipTypeCreate();
140 $savedSearch = new CRM_Contact_BAO_SavedSearch();
141 $formValues = [
142 'operator' => 'AND',
143 'title' => 'testsmart',
144 'radio_ts' => 'ts_all',
145 'component_mode' => CRM_Contact_BAO_Query::MODE_CONTACTS,
146 'display_relationship_type' => "{$relTypeID}_a_b",
147 'uf_group_id' => 1,
148 ];
149 $queryParams = [];
150 CRM_Contact_BAO_SavedSearch::saveSkippedElement($queryParams, $formValues);
151 $savedSearch->form_values = serialize($queryParams);
152 $savedSearch->save();
153
154 $result = CRM_Contact_BAO_SavedSearch::getFormValues(CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()'));
155 $expectedResult = [
156 'operator' => 'AND',
157 'component_mode' => CRM_Contact_BAO_Query::MODE_CONTACTS,
158 'display_relationship_type' => "{$relTypeID}_a_b",
159 'uf_group_id' => 1,
160 ];
161 $this->checkArrayEquals($result, $expectedResult);
162 }
163
164 /**
165 * Get variants of the fields we want to test.
166 *
167 * @return array
168 */
169 public function getSavedSearches() {
170 $return = [];
171 $searches = $this->getSearches();
172 foreach ($searches as $key => $search) {
173 $return[] = [$search['form_values'], $search['expected'], $key];
174 }
175 return $return;
176 }
177
178 /**
179 * Get variants of potential saved form values.
180 *
181 * Note that we include 1 in various ways to cover the possibility that 1 is treated as a boolean.
182 *
183 * @return array
184 */
185 public function getSearches() {
186 return [
187 'checkbox_format_1_first' => [
188 'form_values' => [
189 'member_membership_type_id' => [1 => 1, 2 => 1],
190 'member_status_id' => [1 => 1, 2 => 1],
191 ],
192 'expected' => [1, 2],
193 ],
194 'checkbox_format_1_later' => [
195 'form_values' => [
196 'member_membership_type_id' => [2 => 1, 1 => 1],
197 'member_status_id' => [2 => 1, 1 => 1],
198 ],
199 'expected' => [2, 1],
200 ],
201 'checkbox_format_single_use_1' => [
202 'form_values' => [
203 'member_membership_type_id' => [1 => 1],
204 'member_status_id' => [1 => 1],
205 ],
206 'expected' => [1],
207 ],
208 'checkbox_format_single_not_1' => [
209 'form_values' => [
210 'member_membership_type_id' => [2 => 1],
211 'member_status_id' => [2 => 1],
212 ],
213 'expected' => [2],
214 ],
215 'array_format' => [
216 'form_values' => [
217 'member_membership_type_id' => [1, 2],
218 'member_status_id' => [1, 2],
219 ],
220 'expected' => [1, 2],
221 ],
222 'array_format_1_later' => [
223 'form_values' => [
224 'member_membership_type_id' => [2, 1],
225 'member_status_id' => [2, 1],
226 ],
227 'expected' => [2, 1],
228 ],
229 'array_format_single_use_1' => [
230 'form_values' => [
231 'member_membership_type_id' => [1],
232 'member_status_id' => [1],
233 ],
234 'expected' => [1],
235 ],
236 'array_format_single_not_1' => [
237 'form_values' => [
238 'member_membership_type_id' => [2],
239 'member_status_id' => [2],
240 ],
241 'expected' => [2],
242 ],
243 'IN_format_single_not_1' => [
244 'form_values' => [
245 'membership_type_id' => ['IN' => [2]],
246 'membership_status_id' => ['IN' => [2]],
247 ],
248 'expected' => [2],
249 ],
250 'IN_format_1_later' => [
251 'form_values' => [
252 'membership_type_id' => ['IN' => [2, 1]],
253 'membership_status_id' => ['IN' => [2, 1]],
254 ],
255 'expected' => [2, 1],
256 ],
257 ];
258 }
259
260 }