3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Test class for CRM_Contact_BAO_Group BAO
34 class CRM_Contact_BAO_SavedSearchTest
extends CiviUnitTestCase
{
37 * Sets up the fixture, for example, opens a network connection.
39 * This method is called before a test is executed.
41 protected function setUp() {
46 * Tears down the fixture, for example, closes a network connection.
48 * This method is called after a test is executed.
50 protected function tearDown() {
51 $this->quickCleanup(array(
52 'civicrm_mapping_field',
55 'civicrm_saved_search',
60 * Test setDefaults for privacy radio buttons.
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,
71 'component_mode' => 1,
73 CRM_Core_DAO
::executeQuery(
74 "INSERT INTO civicrm_saved_search (form_values) VALUES('" . serialize($sg->_formValues
) . "')"
76 $ssID = CRM_Core_DAO
::singleValueQuery('SELECT LAST_INSERT_ID()');
77 $sg->set('ssID', $ssID);
79 $defaults = $sg->setDefaultValues();
81 $this->checkArrayEquals($defaults, $sg->_formValues
);
85 * Test fixValues function.
87 * @dataProvider getSavedSearches
89 public function testGetFormValues($formValues, $expectedResult, $searchDescription) {
90 CRM_Core_DAO
::executeQuery(
91 "INSERT INTO civicrm_saved_search (form_values) VALUES('" . serialize($formValues) . "')"
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);
101 * Test if dates ranges are stored correctly
102 * in civicrm_saved_search table and are
103 * extracted properly.
105 public function testDateRange() {
106 $savedSearch = new CRM_Contact_BAO_SavedSearch();
109 'group_search_selected' => 'group',
110 'component_mode' => 1,
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',
120 $queryParams = array(
122 0 => 'participant_register_date_low',
129 0 => 'participant_register_date_high',
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();
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']);
148 * Test if skipped elements are correctly
149 * stored and retrieved as formvalues.
151 public function testSkippedElements() {
152 $relTypeID = $this->relationshipTypeCreate();
153 $savedSearch = new CRM_Contact_BAO_SavedSearch();
156 'title' => 'testsmart',
157 'radio_ts' => 'ts_all',
158 'component_mode' => CRM_Contact_BAO_Query
::MODE_CONTACTS
,
159 'display_relationship_type' => "{$relTypeID}_a_b",
162 $queryParams = array();
163 CRM_Contact_BAO_SavedSearch
::saveSkippedElement($queryParams, $formValues);
164 $savedSearch->form_values
= serialize($queryParams);
165 $savedSearch->save();
167 $result = CRM_Contact_BAO_SavedSearch
::getFormValues(CRM_Core_DAO
::singleValueQuery('SELECT LAST_INSERT_ID()'));
168 $expectedResult = array(
170 'component_mode' => CRM_Contact_BAO_Query
::MODE_CONTACTS
,
171 'display_relationship_type' => "{$relTypeID}_a_b",
174 $this->checkArrayEquals($result, $expectedResult);
178 * Test if relative dates are stored correctly
179 * in civicrm_saved_search table.
181 public function testRelativeDateValues() {
182 $savedSearch = new CRM_Contact_BAO_SavedSearch();
185 'event_relative' => 'this.month',
186 'participant_relative' => 'today',
187 'participant_test' => 0,
188 'title' => 'testsmart',
189 'radio_ts' => 'ts_all',
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();
197 $result = CRM_Contact_BAO_SavedSearch
::getFormValues(CRM_Core_DAO
::singleValueQuery('SELECT LAST_INSERT_ID()'));
199 'event' => 'this.month',
200 'participant' => 'today',
202 $this->checkArrayEquals($result['relative_dates'], $expectedResult);
206 * Test relative dates
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.
212 * It still presumably provides useful 'this does not fatal or give enotice' coverage.
214 public function testCustomFieldRelativeDates() {
215 // Create a custom field.
216 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'relative_date_test_group'));
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,
226 'is_searchable' => 1,
227 'date_format' => 'mm/dd/yy',
230 $customField = $this->callAPIAndDocument('custom_field', 'create', $params, __FUNCTION__
, __FILE__
);
231 $id = $customField['id'];
233 $queryParams = array(
235 0 => "custom_${id}_low",
237 2 => '20170425000000',
240 0 => "custom_${id}_high",
242 2 => '20170501235959',
246 "custom_${id}_relative" => 'ending.week',
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(array('civicrm_saved_search'), $dropCustomValueTables);
257 * Get variants of the fields we want to test.
261 public function getSavedSearches() {
263 $searches = $this->getSearches();
264 foreach ($searches as $key => $search) {
265 $return[] = array($search['form_values'], $search['expected'], $key);
271 * Get variants of potential saved form values.
273 * Note that we include 1 in various ways to cover the possibility that 1 is treated as a boolean.
277 public function getSearches() {
279 'checkbox_format_1_first' => array(
280 'form_values' => array(
281 'member_membership_type_id' => array(1 => 1, 2 => 1),
282 'member_status_id' => array(1 => 1, 2 => 1),
284 'expected' => array(1, 2),
286 'checkbox_format_1_later' => array(
287 'form_values' => array(
288 'member_membership_type_id' => array(2 => 1, 1 => 1),
289 'member_status_id' => array(2 => 1, 1 => 1),
291 'expected' => array(2, 1),
293 'checkbox_format_single_use_1' => array(
294 'form_values' => array(
295 'member_membership_type_id' => array(1 => 1),
296 'member_status_id' => array(1 => 1),
298 'expected' => array(1),
300 'checkbox_format_single_not_1' => array(
301 'form_values' => array(
302 'member_membership_type_id' => array(2 => 1),
303 'member_status_id' => array(2 => 1),
305 'expected' => array(2),
307 'array_format' => array(
308 'form_values' => array(
309 'member_membership_type_id' => array(1, 2),
310 'member_status_id' => array(1, 2),
312 'expected' => array(1, 2),
314 'array_format_1_later' => array(
315 'form_values' => array(
316 'member_membership_type_id' => array(2, 1),
317 'member_status_id' => array(2, 1),
319 'expected' => array(2, 1),
321 'array_format_single_use_1' => array(
322 'form_values' => array(
323 'member_membership_type_id' => array(1),
324 'member_status_id' => array(1),
326 'expected' => array(1),
328 'array_format_single_not_1' => array(
329 'form_values' => array(
330 'member_membership_type_id' => array(2),
331 'member_status_id' => array(2),
333 'expected' => array(2),
335 'IN_format_single_not_1' => array(
336 'form_values' => array(
337 'membership_type_id' => array('IN' => array(2)),
338 'membership_status_id' => array('IN' => array(2)),
340 'expected' => array(2),
342 'IN_format_1_later' => array(
343 'form_values' => array(
344 'membership_type_id' => array('IN' => array(2, 1)),
345 'membership_status_id' => array('IN' => array(2, 1)),
347 'expected' => array(2, 1),