Merge pull request #15061 from eileenmcnaughton/agile
[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_relative' => 'today',
187 'participant_test' => 0,
188 'title' => 'testsmart',
189 'radio_ts' => 'ts_all',
190 ];
191 $queryParams = [];
192 CRM_Contact_BAO_SavedSearch::saveRelativeDates($queryParams, $formValues);
193 CRM_Contact_BAO_SavedSearch::saveSkippedElement($queryParams, $formValues);
194 $savedSearch->form_values = serialize($queryParams);
195 $savedSearch->save();
196
197 $result = CRM_Contact_BAO_SavedSearch::getFormValues(CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()'));
198 $expectedResult = [
199 'event' => 'this.month',
200 'participant' => 'today',
201 ];
202 $this->checkArrayEquals($result['relative_dates'], $expectedResult);
203 }
204
205 /**
206 * Test relative dates
207 *
208 * This is a slightly odd test because it was originally created to test that we DO create a
209 * special 'relative_dates' key but the new favoured format is not to do that and to
210 * save (eg) custom_1_relative = this.day.
211 *
212 * It still presumably provides useful 'this does not fatal or give enotice' coverage.
213 */
214 public function testCustomFieldRelativeDates() {
215 // Create a custom field.
216 $customGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'relative_date_test_group']);
217 $params = [
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/yy',
228 'is_active' => 1,
229 ];
230 $customField = $this->callAPIAndDocument('custom_field', 'create', $params, __FUNCTION__, __FILE__);
231 $id = $customField['id'];
232
233 $queryParams = [
234 0 => [
235 0 => "custom_${id}_low",
236 1 => '=',
237 2 => '20170425000000',
238 ],
239 1 => [
240 0 => "custom_${id}_high",
241 1 => '=',
242 2 => '20170501235959',
243 ],
244 ];
245 $formValues = [
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 $this->assertArrayNotHasKey('relative_dates', $queryParams, 'Relative date in custom field smart group creation failed.');
252 $dropCustomValueTables = TRUE;
253 $this->quickCleanup(['civicrm_saved_search'], $dropCustomValueTables);
254 }
255
256 /**
257 * Get variants of the fields we want to test.
258 *
259 * @return array
260 */
261 public function getSavedSearches() {
262 $return = [];
263 $searches = $this->getSearches();
264 foreach ($searches as $key => $search) {
265 $return[] = [$search['form_values'], $search['expected'], $key];
266 }
267 return $return;
268 }
269
270 /**
271 * Get variants of potential saved form values.
272 *
273 * Note that we include 1 in various ways to cover the possibility that 1 is treated as a boolean.
274 *
275 * @return array
276 */
277 public function getSearches() {
278 return [
279 'checkbox_format_1_first' => [
280 'form_values' => [
281 'member_membership_type_id' => [1 => 1, 2 => 1],
282 'member_status_id' => [1 => 1, 2 => 1],
283 ],
284 'expected' => [1, 2],
285 ],
286 'checkbox_format_1_later' => [
287 'form_values' => [
288 'member_membership_type_id' => [2 => 1, 1 => 1],
289 'member_status_id' => [2 => 1, 1 => 1],
290 ],
291 'expected' => [2, 1],
292 ],
293 'checkbox_format_single_use_1' => [
294 'form_values' => [
295 'member_membership_type_id' => [1 => 1],
296 'member_status_id' => [1 => 1],
297 ],
298 'expected' => [1],
299 ],
300 'checkbox_format_single_not_1' => [
301 'form_values' => [
302 'member_membership_type_id' => [2 => 1],
303 'member_status_id' => [2 => 1],
304 ],
305 'expected' => [2],
306 ],
307 'array_format' => [
308 'form_values' => [
309 'member_membership_type_id' => [1, 2],
310 'member_status_id' => [1, 2],
311 ],
312 'expected' => [1, 2],
313 ],
314 'array_format_1_later' => [
315 'form_values' => [
316 'member_membership_type_id' => [2, 1],
317 'member_status_id' => [2, 1],
318 ],
319 'expected' => [2, 1],
320 ],
321 'array_format_single_use_1' => [
322 'form_values' => [
323 'member_membership_type_id' => [1],
324 'member_status_id' => [1],
325 ],
326 'expected' => [1],
327 ],
328 'array_format_single_not_1' => [
329 'form_values' => [
330 'member_membership_type_id' => [2],
331 'member_status_id' => [2],
332 ],
333 'expected' => [2],
334 ],
335 'IN_format_single_not_1' => [
336 'form_values' => [
337 'membership_type_id' => ['IN' => [2]],
338 'membership_status_id' => ['IN' => [2]],
339 ],
340 'expected' => [2],
341 ],
342 'IN_format_1_later' => [
343 'form_values' => [
344 'membership_type_id' => ['IN' => [2, 1]],
345 'membership_status_id' => ['IN' => [2, 1]],
346 ],
347 'expected' => [2, 1],
348 ],
349 ];
350 }
351
352 }