Merge pull request #15966 from seamuslee001/permission_access_metadata
[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 use CRMTraits_Custom_CustomDataTrait;
37
38 /**
39 * Sets up the fixture, for example, opens a network connection.
40 *
41 * This method is called before a test is executed.
42 */
43 protected function setUp() {
44 parent::setUp();
45 }
46
47 /**
48 * Tears down the fixture, for example, closes a network connection.
49 *
50 * This method is called after a test is executed.
51 */
52 protected function tearDown() {
53 if (!empty($this->ids['CustomField'])) {
54 foreach ($this->ids['CustomField'] as $type => $id) {
55 $field = civicrm_api3('CustomField', 'getsingle', ['id' => $id]);
56 $group = civicrm_api3('CustomGroup', 'getsingle', ['id' => $field['custom_group_id']]);
57 CRM_Core_DAO::executeQuery("DROP TABLE IF Exists {$group['table_name']}");
58 }
59 }
60 $this->quickCleanup([
61 'civicrm_mapping_field',
62 'civicrm_mapping',
63 'civicrm_group',
64 'civicrm_saved_search',
65 'civicrm_custom_field',
66 'civicrm_custom_group',
67 ]);
68 }
69
70 /**
71 * Test setDefaults for privacy radio buttons.
72 *
73 * @throws \Exception
74 */
75 public function testDefaultValues() {
76 $this->createCustomGroupWithFieldOfType([], 'int');
77 $sg = new CRM_Contact_Form_Search_Advanced();
78 $sg->controller = new CRM_Core_Controller();
79 $formValues = [
80 'group_search_selected' => 'group',
81 'privacy_options' => ['do_not_email'],
82 'privacy_operator' => 'OR',
83 'privacy_toggle' => 2,
84 'operator' => 'AND',
85 'component_mode' => 1,
86 'custom_' . $this->ids['CustomField']['int'] . '_from' => 0,
87 'custom_' . $this->ids['CustomField']['int'] . '_to' => '',
88 ];
89 CRM_Core_DAO::executeQuery(
90 "INSERT INTO civicrm_saved_search (form_values) VALUES('" . serialize($formValues) . "')"
91 );
92 $ssID = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()');
93 $sg->set('ssID', $ssID);
94 $sg->set('formValues', $formValues);
95
96 $defaults = $sg->setDefaultValues();
97
98 $this->checkArrayEquals($defaults, $formValues);
99 }
100
101 /**
102 * Test setDefaults for privacy radio buttons.
103 *
104 * @throws \Exception
105 */
106 public function testGetFormValuesWithCustomFields() {
107 $this->createCustomGroupWithFieldsOfAllTypes();
108 $sg = new CRM_Contact_Form_Search_Advanced();
109 $sg->controller = new CRM_Core_Controller();
110 $formValues = [
111 'group_search_selected' => 'group',
112 'privacy_options' => ['do_not_email'],
113 'privacy_operator' => 'OR',
114 'privacy_toggle' => 2,
115 'operator' => 'AND',
116 'component_mode' => 1,
117 'custom_' . $this->ids['CustomField']['int'] . '_from' => 0,
118 'custom_' . $this->ids['CustomField']['int'] . '_to' => '',
119 'custom_' . $this->ids['CustomField']['select_date'] . '_high' => '2019-06-30',
120 'custom_' . $this->ids['CustomField']['select_date'] . '_low' => '2019-06-30',
121 ];
122 CRM_Core_DAO::executeQuery(
123 "INSERT INTO civicrm_saved_search (form_values) VALUES('" . serialize($formValues) . "')"
124 );
125 $returnedFormValues = CRM_Contact_BAO_SavedSearch::getFormValues(CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()'));
126 $checkFormValues = $formValues + ['custom_' . $this->ids['CustomField']['select_date'] . '_relative' => 0];
127 $this->checkArrayEquals($returnedFormValues, $checkFormValues);
128 }
129
130 /**
131 * Test fixValues function.
132 *
133 * @dataProvider getSavedSearches
134 */
135 public function testGetFormValues($formValues, $expectedResult, $searchDescription) {
136 CRM_Core_DAO::executeQuery(
137 "INSERT INTO civicrm_saved_search (form_values) VALUES('" . serialize($formValues) . "')"
138 );
139 $result = CRM_Contact_BAO_SavedSearch::getFormValues(CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()'));
140 $this->assertEquals(['membership_type_id', 'membership_status_id'], array_keys($result));
141 foreach ($result as $key => $value) {
142 $this->assertEquals($expectedResult, $value, 'failure on set ' . $searchDescription);
143 }
144 }
145
146 /**
147 * Test if skipped elements are correctly
148 * stored and retrieved as formvalues.
149 */
150 public function testSkippedElements() {
151 $relTypeID = $this->relationshipTypeCreate();
152 $savedSearch = new CRM_Contact_BAO_SavedSearch();
153 $formValues = [
154 'operator' => 'AND',
155 'title' => 'testsmart',
156 'radio_ts' => 'ts_all',
157 'component_mode' => CRM_Contact_BAO_Query::MODE_CONTACTS,
158 'display_relationship_type' => "{$relTypeID}_a_b",
159 'uf_group_id' => 1,
160 ];
161 $queryParams = [];
162 CRM_Contact_BAO_SavedSearch::saveSkippedElement($queryParams, $formValues);
163 $savedSearch->form_values = serialize($queryParams);
164 $savedSearch->save();
165
166 $result = CRM_Contact_BAO_SavedSearch::getFormValues(CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()'));
167 $expectedResult = [
168 'operator' => 'AND',
169 'component_mode' => CRM_Contact_BAO_Query::MODE_CONTACTS,
170 'display_relationship_type' => "{$relTypeID}_a_b",
171 'uf_group_id' => 1,
172 ];
173 $this->checkArrayEquals($result, $expectedResult);
174 }
175
176 /**
177 * Get variants of the fields we want to test.
178 *
179 * @return array
180 */
181 public function getSavedSearches() {
182 $return = [];
183 $searches = $this->getSearches();
184 foreach ($searches as $key => $search) {
185 $return[] = [$search['form_values'], $search['expected'], $key];
186 }
187 return $return;
188 }
189
190 /**
191 * Get variants of potential saved form values.
192 *
193 * Note that we include 1 in various ways to cover the possibility that 1 is treated as a boolean.
194 *
195 * @return array
196 */
197 public function getSearches() {
198 return [
199 'checkbox_format_1_first' => [
200 'form_values' => [
201 'member_membership_type_id' => [1 => 1, 2 => 1],
202 'member_status_id' => [1 => 1, 2 => 1],
203 ],
204 'expected' => [1, 2],
205 ],
206 'checkbox_format_1_later' => [
207 'form_values' => [
208 'member_membership_type_id' => [2 => 1, 1 => 1],
209 'member_status_id' => [2 => 1, 1 => 1],
210 ],
211 'expected' => [2, 1],
212 ],
213 'checkbox_format_single_use_1' => [
214 'form_values' => [
215 'member_membership_type_id' => [1 => 1],
216 'member_status_id' => [1 => 1],
217 ],
218 'expected' => [1],
219 ],
220 'checkbox_format_single_not_1' => [
221 'form_values' => [
222 'member_membership_type_id' => [2 => 1],
223 'member_status_id' => [2 => 1],
224 ],
225 'expected' => [2],
226 ],
227 'array_format' => [
228 'form_values' => [
229 'member_membership_type_id' => [1, 2],
230 'member_status_id' => [1, 2],
231 ],
232 'expected' => [1, 2],
233 ],
234 'array_format_1_later' => [
235 'form_values' => [
236 'member_membership_type_id' => [2, 1],
237 'member_status_id' => [2, 1],
238 ],
239 'expected' => [2, 1],
240 ],
241 'array_format_single_use_1' => [
242 'form_values' => [
243 'member_membership_type_id' => [1],
244 'member_status_id' => [1],
245 ],
246 'expected' => [1],
247 ],
248 'array_format_single_not_1' => [
249 'form_values' => [
250 'member_membership_type_id' => [2],
251 'member_status_id' => [2],
252 ],
253 'expected' => [2],
254 ],
255 'IN_format_single_not_1' => [
256 'form_values' => [
257 'membership_type_id' => ['IN' => [2]],
258 'membership_status_id' => ['IN' => [2]],
259 ],
260 'expected' => [2],
261 ],
262 'IN_format_1_later' => [
263 'form_values' => [
264 'membership_type_id' => ['IN' => [2, 1]],
265 'membership_status_id' => ['IN' => [2, 1]],
266 ],
267 'expected' => [2, 1],
268 ],
269 ];
270 }
271
272 }