3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
13 * Test class for CRM_Contact_BAO_Group BAO
18 class CRM_Contact_BAO_SavedSearchTest
extends CiviUnitTestCase
{
20 use CRMTraits_Custom_CustomDataTrait
;
23 * Sets up the fixture, for example, opens a network connection.
25 * This method is called before a test is executed.
27 protected function setUp() {
32 * Tears down the fixture, for example, closes a network connection.
34 * This method is called after a test is executed.
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']}");
45 'civicrm_mapping_field',
48 'civicrm_saved_search',
49 'civicrm_custom_field',
50 'civicrm_custom_group',
55 * Test setDefaults for privacy radio buttons.
59 public function testDefaultValues() {
60 $this->createCustomGroupWithFieldOfType([], 'int');
61 $sg = new CRM_Contact_Form_Search_Advanced();
62 $sg->controller
= new CRM_Core_Controller();
64 'group_search_selected' => 'group',
65 'privacy_options' => ['do_not_email'],
66 'privacy_operator' => 'OR',
67 'privacy_toggle' => 2,
69 'component_mode' => 1,
70 'custom_' . $this->ids
['CustomField']['int'] . '_from' => 0,
71 'custom_' . $this->ids
['CustomField']['int'] . '_to' => '',
73 CRM_Core_DAO
::executeQuery(
74 "INSERT INTO civicrm_saved_search (form_values) VALUES('" . serialize($formValues) . "')"
76 $ssID = CRM_Core_DAO
::singleValueQuery('SELECT LAST_INSERT_ID()');
77 $sg->set('ssID', $ssID);
78 $sg->set('formValues', $formValues);
80 $defaults = $sg->setDefaultValues();
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);
90 * Test setDefaults for privacy radio buttons.
94 public function testGetFormValuesWithCustomFields() {
95 $this->createCustomGroupWithFieldsOfAllTypes();
96 $sg = new CRM_Contact_Form_Search_Advanced();
97 $sg->controller
= new CRM_Core_Controller();
99 'group_search_selected' => 'group',
100 'privacy_options' => ['do_not_email'],
101 'privacy_operator' => 'OR',
102 'privacy_toggle' => 2,
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',
110 CRM_Core_DAO
::executeQuery(
111 "INSERT INTO civicrm_saved_search (form_values) VALUES('" . serialize($formValues) . "')"
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);
119 * Test fixValues function.
121 * @dataProvider getSavedSearches
123 public function testGetFormValues($formValues, $expectedResult, $searchDescription) {
124 CRM_Core_DAO
::executeQuery(
125 "INSERT INTO civicrm_saved_search (form_values) VALUES('" . serialize($formValues) . "')"
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);
135 * Test if skipped elements are correctly
136 * stored and retrieved as formvalues.
138 public function testSkippedElements() {
139 $relTypeID = $this->relationshipTypeCreate();
140 $savedSearch = new CRM_Contact_BAO_SavedSearch();
143 'title' => 'testsmart',
144 'radio_ts' => 'ts_all',
145 'component_mode' => CRM_Contact_BAO_Query
::MODE_CONTACTS
,
146 'display_relationship_type' => "{$relTypeID}_a_b",
150 CRM_Contact_BAO_SavedSearch
::saveSkippedElement($queryParams, $formValues);
151 $savedSearch->form_values
= serialize($queryParams);
152 $savedSearch->save();
154 $result = CRM_Contact_BAO_SavedSearch
::getFormValues(CRM_Core_DAO
::singleValueQuery('SELECT LAST_INSERT_ID()'));
157 'component_mode' => CRM_Contact_BAO_Query
::MODE_CONTACTS
,
158 'display_relationship_type' => "{$relTypeID}_a_b",
161 $this->checkArrayEquals($result, $expectedResult);
165 * Get variants of the fields we want to test.
169 public function getSavedSearches() {
171 $searches = $this->getSearches();
172 foreach ($searches as $key => $search) {
173 $return[] = [$search['form_values'], $search['expected'], $key];
179 * Get variants of potential saved form values.
181 * Note that we include 1 in various ways to cover the possibility that 1 is treated as a boolean.
185 public function getSearches() {
187 'checkbox_format_1_first' => [
189 'member_membership_type_id' => [1 => 1, 2 => 1],
190 'member_status_id' => [1 => 1, 2 => 1],
192 'expected' => [1, 2],
194 'checkbox_format_1_later' => [
196 'member_membership_type_id' => [2 => 1, 1 => 1],
197 'member_status_id' => [2 => 1, 1 => 1],
199 'expected' => [2, 1],
201 'checkbox_format_single_use_1' => [
203 'member_membership_type_id' => [1 => 1],
204 'member_status_id' => [1 => 1],
208 'checkbox_format_single_not_1' => [
210 'member_membership_type_id' => [2 => 1],
211 'member_status_id' => [2 => 1],
217 'member_membership_type_id' => [1, 2],
218 'member_status_id' => [1, 2],
220 'expected' => [1, 2],
222 'array_format_1_later' => [
224 'member_membership_type_id' => [2, 1],
225 'member_status_id' => [2, 1],
227 'expected' => [2, 1],
229 'array_format_single_use_1' => [
231 'member_membership_type_id' => [1],
232 'member_status_id' => [1],
236 'array_format_single_not_1' => [
238 'member_membership_type_id' => [2],
239 'member_status_id' => [2],
243 'IN_format_single_not_1' => [
245 'membership_type_id' => ['IN' => [2]],
246 'membership_status_id' => ['IN' => [2]],
250 'IN_format_1_later' => [
252 'membership_type_id' => ['IN' => [2, 1]],
253 'membership_status_id' => ['IN' => [2, 1]],
255 'expected' => [2, 1],