Cleanup following smart group conversions and fix the old name of the relationship...
[civicrm-core.git] / tests / phpunit / CRM / Contact / BAO / SavedSearchTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Test class for CRM_Contact_BAO_Group BAO
30 *
31 * @package CiviCRM
32 * @group headless
33 */
34 class CRM_Contact_BAO_SavedSearchTest extends CiviUnitTestCase {
35
36 /**
37 * Sets up the fixture, for example, opens a network connection.
38 *
39 * This method is called before a test is executed.
40 */
41 protected function setUp() {
42 parent::setUp();
43 }
44
45 /**
46 * Tears down the fixture, for example, closes a network connection.
47 *
48 * This method is called after a test is executed.
49 */
50 protected function tearDown() {
51 $this->quickCleanup([
52 'civicrm_mapping_field',
53 'civicrm_mapping',
54 'civicrm_group',
55 'civicrm_saved_search',
56 ]);
57 }
58
59 /**
60 * Test setDefaults for privacy radio buttons.
61 */
62 public function testDefaultValues() {
63 $sg = new CRM_Contact_Form_Search_Advanced();
64 $sg->controller = new CRM_Core_Controller();
65 $sg->_formValues = [
66 'group_search_selected' => 'group',
67 'privacy_options' => ['do_not_email'],
68 'privacy_operator' => 'OR',
69 'privacy_toggle' => 2,
70 'operator' => 'AND',
71 'component_mode' => 1,
72 ];
73 CRM_Core_DAO::executeQuery(
74 "INSERT INTO civicrm_saved_search (form_values) VALUES('" . serialize($sg->_formValues) . "')"
75 );
76 $ssID = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()');
77 $sg->set('ssID', $ssID);
78
79 $defaults = $sg->setDefaultValues();
80
81 $this->checkArrayEquals($defaults, $sg->_formValues);
82 }
83
84 /**
85 * Test fixValues function.
86 *
87 * @dataProvider getSavedSearches
88 */
89 public function testGetFormValues($formValues, $expectedResult, $searchDescription) {
90 CRM_Core_DAO::executeQuery(
91 "INSERT INTO civicrm_saved_search (form_values) VALUES('" . serialize($formValues) . "')"
92 );
93 $result = CRM_Contact_BAO_SavedSearch::getFormValues(CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()'));
94 $this->assertEquals(['membership_type_id', 'membership_status_id'], array_keys($result));
95 foreach ($result as $key => $value) {
96 $this->assertEquals($expectedResult, $value, 'failure on set ' . $searchDescription);
97 }
98 }
99
100 /**
101 * Test if dates ranges are stored correctly
102 * in civicrm_saved_search table and are
103 * extracted properly.
104 */
105 public function testDateRange() {
106 $savedSearch = new CRM_Contact_BAO_SavedSearch();
107 $formValues = [
108 'hidden_basic' => 1,
109 'group_search_selected' => 'group',
110 'component_mode' => 1,
111 'operator' => 'AND',
112 'privacy_operator' => 'OR',
113 'privacy_toggle' => 1,
114 'participant_register_date_low' => '01/01/2009',
115 'participant_register_date_high' => '01/01/2018',
116 'radio_ts' => 'ts_all',
117 'title' => 'bah bah bah',
118 ];
119
120 $queryParams = [
121 0 => [
122 0 => 'participant_register_date_low',
123 1 => '=',
124 2 => '01/01/2009',
125 3 => 0,
126 4 => 0,
127 ],
128 1 => [
129 0 => 'participant_register_date_high',
130 1 => '=',
131 2 => '01/01/2018',
132 3 => 0,
133 4 => 0,
134 ],
135 ];
136
137 CRM_Contact_BAO_SavedSearch::saveRelativeDates($queryParams, $formValues);
138 CRM_Contact_BAO_SavedSearch::saveSkippedElement($queryParams, $formValues);
139 $savedSearch->form_values = serialize($queryParams);
140 $savedSearch->save();
141
142 $result = CRM_Contact_BAO_SavedSearch::getFormValues(CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()'));
143 $this->assertEquals('01/01/2009', $result['participant_register_date_low']);
144 $this->assertEquals('01/01/2018', $result['participant_register_date_high']);
145 }
146
147 /**
148 * Test if skipped elements are correctly
149 * stored and retrieved as formvalues.
150 */
151 public function testSkippedElements() {
152 $relTypeID = $this->relationshipTypeCreate();
153 $savedSearch = new CRM_Contact_BAO_SavedSearch();
154 $formValues = [
155 'operator' => 'AND',
156 'title' => 'testsmart',
157 'radio_ts' => 'ts_all',
158 'component_mode' => CRM_Contact_BAO_Query::MODE_CONTACTS,
159 'display_relationship_type' => "{$relTypeID}_a_b",
160 'uf_group_id' => 1,
161 ];
162 $queryParams = [];
163 CRM_Contact_BAO_SavedSearch::saveSkippedElement($queryParams, $formValues);
164 $savedSearch->form_values = serialize($queryParams);
165 $savedSearch->save();
166
167 $result = CRM_Contact_BAO_SavedSearch::getFormValues(CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()'));
168 $expectedResult = [
169 'operator' => 'AND',
170 'component_mode' => CRM_Contact_BAO_Query::MODE_CONTACTS,
171 'display_relationship_type' => "{$relTypeID}_a_b",
172 'uf_group_id' => 1,
173 ];
174 $this->checkArrayEquals($result, $expectedResult);
175 }
176
177 /**
178 * Test if relative dates are stored correctly
179 * in civicrm_saved_search table.
180 */
181 public function testRelativeDateValues() {
182 $savedSearch = new CRM_Contact_BAO_SavedSearch();
183 $formValues = [
184 'operator' => 'AND',
185 'event_relative' => 'this.month',
186 'participant_test' => 0,
187 'title' => 'testsmart',
188 'radio_ts' => 'ts_all',
189 ];
190 $queryParams = [];
191 CRM_Contact_BAO_SavedSearch::saveRelativeDates($queryParams, $formValues);
192 CRM_Contact_BAO_SavedSearch::saveSkippedElement($queryParams, $formValues);
193 $savedSearch->form_values = serialize($queryParams);
194 $savedSearch->save();
195
196 $result = CRM_Contact_BAO_SavedSearch::getFormValues(CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()'));
197 $expectedResult = [
198 'event' => 'this.month',
199 ];
200 $this->checkArrayEquals($result['relative_dates'], $expectedResult);
201 }
202
203 /**
204 * Test if change log relative dates are stored correctly
205 * in civicrm_saved_search table.
206 */
207 public function testRelativeDateChangeLog() {
208 $savedSearch = new CRM_Contact_BAO_SavedSearch();
209 $formValues = [
210 'operator' => 'AND',
211 'log_date_relative' => 'this.month',
212 'radio_ts' => 'ts_all',
213 ];
214 $queryParams = [];
215 CRM_Contact_BAO_SavedSearch::saveRelativeDates($queryParams, $formValues);
216 CRM_Contact_BAO_SavedSearch::saveSkippedElement($queryParams, $formValues);
217 $savedSearch->form_values = serialize($queryParams);
218 $savedSearch->save();
219
220 $result = CRM_Contact_BAO_SavedSearch::getFormValues(CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()'));
221 $expectedResult = [
222 'log' => 'this.month',
223 ];
224 $this->checkArrayEquals($result['relative_dates'], $expectedResult);
225 }
226
227 /**
228 * Test relative dates
229 *
230 * This is a slightly odd test because it was originally created to test that we DO create a
231 * special 'relative_dates' key but the new favoured format is not to do that and to
232 * save (eg) custom_1_relative = this.day.
233 *
234 * It still presumably provides useful 'this does not fatal or give enotice' coverage.
235 */
236 public function testCustomFieldRelativeDates() {
237 // Create a custom field.
238 $customGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'relative_date_test_group']);
239 $params = [
240 'custom_group_id' => $customGroup['id'],
241 'name' => 'test_datefield',
242 'label' => 'Date Field for Testing',
243 'html_type' => 'Select Date',
244 'data_type' => 'Date',
245 'default_value' => NULL,
246 'weight' => 4,
247 'is_required' => 1,
248 'is_searchable' => 1,
249 'date_format' => 'mm/dd/yy',
250 'is_active' => 1,
251 ];
252 $customField = $this->callAPIAndDocument('custom_field', 'create', $params, __FUNCTION__, __FILE__);
253 $id = $customField['id'];
254
255 $queryParams = [
256 0 => [
257 0 => "custom_${id}_low",
258 1 => '=',
259 2 => '20170425000000',
260 ],
261 1 => [
262 0 => "custom_${id}_high",
263 1 => '=',
264 2 => '20170501235959',
265 ],
266 ];
267 $formValues = [
268 "custom_${id}_relative" => 'ending.week',
269 ];
270 CRM_Contact_BAO_SavedSearch::saveRelativeDates($queryParams, $formValues);
271 // Since custom_13 doesn't have the word 'date' in it, the key is
272 // set to 0, rather than the field name.
273 $this->assertArrayNotHasKey('relative_dates', $queryParams, 'Relative date in custom field smart group creation failed.');
274 $dropCustomValueTables = TRUE;
275 $this->quickCleanup(['civicrm_saved_search'], $dropCustomValueTables);
276 }
277
278 /**
279 * Get variants of the fields we want to test.
280 *
281 * @return array
282 */
283 public function getSavedSearches() {
284 $return = [];
285 $searches = $this->getSearches();
286 foreach ($searches as $key => $search) {
287 $return[] = [$search['form_values'], $search['expected'], $key];
288 }
289 return $return;
290 }
291
292 /**
293 * Get variants of potential saved form values.
294 *
295 * Note that we include 1 in various ways to cover the possibility that 1 is treated as a boolean.
296 *
297 * @return array
298 */
299 public function getSearches() {
300 return [
301 'checkbox_format_1_first' => [
302 'form_values' => [
303 'member_membership_type_id' => [1 => 1, 2 => 1],
304 'member_status_id' => [1 => 1, 2 => 1],
305 ],
306 'expected' => [1, 2],
307 ],
308 'checkbox_format_1_later' => [
309 'form_values' => [
310 'member_membership_type_id' => [2 => 1, 1 => 1],
311 'member_status_id' => [2 => 1, 1 => 1],
312 ],
313 'expected' => [2, 1],
314 ],
315 'checkbox_format_single_use_1' => [
316 'form_values' => [
317 'member_membership_type_id' => [1 => 1],
318 'member_status_id' => [1 => 1],
319 ],
320 'expected' => [1],
321 ],
322 'checkbox_format_single_not_1' => [
323 'form_values' => [
324 'member_membership_type_id' => [2 => 1],
325 'member_status_id' => [2 => 1],
326 ],
327 'expected' => [2],
328 ],
329 'array_format' => [
330 'form_values' => [
331 'member_membership_type_id' => [1, 2],
332 'member_status_id' => [1, 2],
333 ],
334 'expected' => [1, 2],
335 ],
336 'array_format_1_later' => [
337 'form_values' => [
338 'member_membership_type_id' => [2, 1],
339 'member_status_id' => [2, 1],
340 ],
341 'expected' => [2, 1],
342 ],
343 'array_format_single_use_1' => [
344 'form_values' => [
345 'member_membership_type_id' => [1],
346 'member_status_id' => [1],
347 ],
348 'expected' => [1],
349 ],
350 'array_format_single_not_1' => [
351 'form_values' => [
352 'member_membership_type_id' => [2],
353 'member_status_id' => [2],
354 ],
355 'expected' => [2],
356 ],
357 'IN_format_single_not_1' => [
358 'form_values' => [
359 'membership_type_id' => ['IN' => [2]],
360 'membership_status_id' => ['IN' => [2]],
361 ],
362 'expected' => [2],
363 ],
364 'IN_format_1_later' => [
365 'form_values' => [
366 'membership_type_id' => ['IN' => [2, 1]],
367 'membership_status_id' => ['IN' => [2, 1]],
368 ],
369 'expected' => [2, 1],
370 ],
371 ];
372 }
373
374 }