Merge pull request #13837 from eileenmcnaughton/event_conf
[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(array(
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 = array(
66 'group_search_selected' => 'group',
67 'privacy_options' => array('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(array('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 = array(
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 = array(
121 0 => array(
122 0 => 'participant_register_date_low',
123 1 => '=',
124 2 => '01/01/2009',
125 3 => 0,
126 4 => 0,
127 ),
128 1 => array(
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 = array(
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 = array();
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 = array(
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 = array(
184 'operator' => 'AND',
185 'event_relative' => 'this.month',
186 'participant_relative' => 'today',
187 'contribution_date_relative' => 'this.week',
188 'participant_test' => 0,
189 'title' => 'testsmart',
190 'radio_ts' => 'ts_all',
191 );
192 $queryParams = array();
193 CRM_Contact_BAO_SavedSearch::saveRelativeDates($queryParams, $formValues);
194 CRM_Contact_BAO_SavedSearch::saveSkippedElement($queryParams, $formValues);
195 $savedSearch->form_values = serialize($queryParams);
196 $savedSearch->save();
197
198 $result = CRM_Contact_BAO_SavedSearch::getFormValues(CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()'));
199 $expectedResult = array(
200 'event' => 'this.month',
201 'participant' => 'today',
202 'contribution' => 'this.week',
203 );
204 $this->checkArrayEquals($result['relative_dates'], $expectedResult);
205 }
206
207 /**
208 * Test relative dates
209 *
210 * The function saveRelativeDates should detect whether a field is using
211 * a relative date range and include in the fromValues a relative_date
212 * index so it is properly detects when executed.
213 */
214 public function testCustomFieldRelativeDates() {
215 // Create a custom field.
216 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'relative_date_test_group'));
217 $params = array(
218 'custom_group_id' => $customGroup['id'],
219 'name' => 'test_datefield',
220 'label' => 'Date Field for Testing',
221 'html_type' => 'Select Date',
222 'data_type' => 'Date',
223 'default_value' => NULL,
224 'weight' => 4,
225 'is_required' => 1,
226 'is_searchable' => 1,
227 'date_format' => 'mm/dd/yyyy',
228 'is_active' => 1,
229 );
230 $customField = $this->callAPIAndDocument('custom_field', 'create', $params, __FUNCTION__, __FILE__);
231 $id = $customField['id'];
232
233 $queryParams = array(
234 0 => array(
235 0 => "custom_${id}_low",
236 1 => '=',
237 2 => '20170425000000',
238 ),
239 1 => array(
240 0 => "custom_${id}_high",
241 1 => '=',
242 2 => '20170501235959',
243 ),
244 );
245 $formValues = array(
246 "custom_${id}_relative" => 'ending.week',
247 );
248 CRM_Contact_BAO_SavedSearch::saveRelativeDates($queryParams, $formValues);
249 // Since custom_13 doesn't have the word 'date' in it, the key is
250 // set to 0, rather than the field name.
251 $err = 'Relative date in custom field smart group creation failed.';
252 $this->assertArrayHasKey('relative_dates', $queryParams, $err);
253 $dropCustomValueTables = TRUE;
254 $this->quickCleanup(array('civicrm_saved_search'), $dropCustomValueTables);
255 }
256
257 /**
258 * Get variants of the fields we want to test.
259 *
260 * @return array
261 */
262 public function getSavedSearches() {
263 $return = array();
264 $searches = $this->getSearches();
265 foreach ($searches as $key => $search) {
266 $return[] = array($search['form_values'], $search['expected'], $key);
267 }
268 return $return;
269 }
270
271 /**
272 * Get variants of potential saved form values.
273 *
274 * Note that we include 1 in various ways to cover the possibility that 1 is treated as a boolean.
275 *
276 * @return array
277 */
278 public function getSearches() {
279 return array(
280 'checkbox_format_1_first' => array(
281 'form_values' => array(
282 'member_membership_type_id' => array(1 => 1, 2 => 1),
283 'member_status_id' => array(1 => 1, 2 => 1),
284 ),
285 'expected' => array(1, 2),
286 ),
287 'checkbox_format_1_later' => array(
288 'form_values' => array(
289 'member_membership_type_id' => array(2 => 1, 1 => 1),
290 'member_status_id' => array(2 => 1, 1 => 1),
291 ),
292 'expected' => array(2, 1),
293 ),
294 'checkbox_format_single_use_1' => array(
295 'form_values' => array(
296 'member_membership_type_id' => array(1 => 1),
297 'member_status_id' => array(1 => 1),
298 ),
299 'expected' => array(1),
300 ),
301 'checkbox_format_single_not_1' => array(
302 'form_values' => array(
303 'member_membership_type_id' => array(2 => 1),
304 'member_status_id' => array(2 => 1),
305 ),
306 'expected' => array(2),
307 ),
308 'array_format' => array(
309 'form_values' => array(
310 'member_membership_type_id' => array(1, 2),
311 'member_status_id' => array(1, 2),
312 ),
313 'expected' => array(1, 2),
314 ),
315 'array_format_1_later' => array(
316 'form_values' => array(
317 'member_membership_type_id' => array(2, 1),
318 'member_status_id' => array(2, 1),
319 ),
320 'expected' => array(2, 1),
321 ),
322 'array_format_single_use_1' => array(
323 'form_values' => array(
324 'member_membership_type_id' => array(1),
325 'member_status_id' => array(1),
326 ),
327 'expected' => array(1),
328 ),
329 'array_format_single_not_1' => array(
330 'form_values' => array(
331 'member_membership_type_id' => array(2),
332 'member_status_id' => array(2),
333 ),
334 'expected' => array(2),
335 ),
336 'IN_format_single_not_1' => array(
337 'form_values' => array(
338 'membership_type_id' => array('IN' => array(2)),
339 'membership_status_id' => array('IN' => array(2)),
340 ),
341 'expected' => array(2),
342 ),
343 'IN_format_1_later' => array(
344 'form_values' => array(
345 'membership_type_id' => array('IN' => array(2, 1)),
346 'membership_status_id' => array('IN' => array(2, 1)),
347 ),
348 'expected' => array(2, 1),
349 ),
350 );
351 }
352
353 }