dev/core#1374 Fix search formValue handling on contribution search
[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 * @throws \Exception
63 */
64 public function testDefaultValues() {
65 $sg = new CRM_Contact_Form_Search_Advanced();
66 $sg->controller = new CRM_Core_Controller();
67 $formValues = [
68 'group_search_selected' => 'group',
69 'privacy_options' => ['do_not_email'],
70 'privacy_operator' => 'OR',
71 'privacy_toggle' => 2,
72 'operator' => 'AND',
73 'component_mode' => 1,
74 ];
75 CRM_Core_DAO::executeQuery(
76 "INSERT INTO civicrm_saved_search (form_values) VALUES('" . serialize($formValues) . "')"
77 );
78 $ssID = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()');
79 $sg->set('ssID', $ssID);
80 $sg->set('formValues', $formValues);
81
82 $defaults = $sg->setDefaultValues();
83
84 $this->checkArrayEquals($defaults, $formValues);
85 }
86
87 /**
88 * Test fixValues function.
89 *
90 * @dataProvider getSavedSearches
91 */
92 public function testGetFormValues($formValues, $expectedResult, $searchDescription) {
93 CRM_Core_DAO::executeQuery(
94 "INSERT INTO civicrm_saved_search (form_values) VALUES('" . serialize($formValues) . "')"
95 );
96 $result = CRM_Contact_BAO_SavedSearch::getFormValues(CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()'));
97 $this->assertEquals(['membership_type_id', 'membership_status_id'], array_keys($result));
98 foreach ($result as $key => $value) {
99 $this->assertEquals($expectedResult, $value, 'failure on set ' . $searchDescription);
100 }
101 }
102
103 /**
104 * Test if skipped elements are correctly
105 * stored and retrieved as formvalues.
106 */
107 public function testSkippedElements() {
108 $relTypeID = $this->relationshipTypeCreate();
109 $savedSearch = new CRM_Contact_BAO_SavedSearch();
110 $formValues = [
111 'operator' => 'AND',
112 'title' => 'testsmart',
113 'radio_ts' => 'ts_all',
114 'component_mode' => CRM_Contact_BAO_Query::MODE_CONTACTS,
115 'display_relationship_type' => "{$relTypeID}_a_b",
116 'uf_group_id' => 1,
117 ];
118 $queryParams = [];
119 CRM_Contact_BAO_SavedSearch::saveSkippedElement($queryParams, $formValues);
120 $savedSearch->form_values = serialize($queryParams);
121 $savedSearch->save();
122
123 $result = CRM_Contact_BAO_SavedSearch::getFormValues(CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()'));
124 $expectedResult = [
125 'operator' => 'AND',
126 'component_mode' => CRM_Contact_BAO_Query::MODE_CONTACTS,
127 'display_relationship_type' => "{$relTypeID}_a_b",
128 'uf_group_id' => 1,
129 ];
130 $this->checkArrayEquals($result, $expectedResult);
131 }
132
133 /**
134 * Get variants of the fields we want to test.
135 *
136 * @return array
137 */
138 public function getSavedSearches() {
139 $return = [];
140 $searches = $this->getSearches();
141 foreach ($searches as $key => $search) {
142 $return[] = [$search['form_values'], $search['expected'], $key];
143 }
144 return $return;
145 }
146
147 /**
148 * Get variants of potential saved form values.
149 *
150 * Note that we include 1 in various ways to cover the possibility that 1 is treated as a boolean.
151 *
152 * @return array
153 */
154 public function getSearches() {
155 return [
156 'checkbox_format_1_first' => [
157 'form_values' => [
158 'member_membership_type_id' => [1 => 1, 2 => 1],
159 'member_status_id' => [1 => 1, 2 => 1],
160 ],
161 'expected' => [1, 2],
162 ],
163 'checkbox_format_1_later' => [
164 'form_values' => [
165 'member_membership_type_id' => [2 => 1, 1 => 1],
166 'member_status_id' => [2 => 1, 1 => 1],
167 ],
168 'expected' => [2, 1],
169 ],
170 'checkbox_format_single_use_1' => [
171 'form_values' => [
172 'member_membership_type_id' => [1 => 1],
173 'member_status_id' => [1 => 1],
174 ],
175 'expected' => [1],
176 ],
177 'checkbox_format_single_not_1' => [
178 'form_values' => [
179 'member_membership_type_id' => [2 => 1],
180 'member_status_id' => [2 => 1],
181 ],
182 'expected' => [2],
183 ],
184 'array_format' => [
185 'form_values' => [
186 'member_membership_type_id' => [1, 2],
187 'member_status_id' => [1, 2],
188 ],
189 'expected' => [1, 2],
190 ],
191 'array_format_1_later' => [
192 'form_values' => [
193 'member_membership_type_id' => [2, 1],
194 'member_status_id' => [2, 1],
195 ],
196 'expected' => [2, 1],
197 ],
198 'array_format_single_use_1' => [
199 'form_values' => [
200 'member_membership_type_id' => [1],
201 'member_status_id' => [1],
202 ],
203 'expected' => [1],
204 ],
205 'array_format_single_not_1' => [
206 'form_values' => [
207 'member_membership_type_id' => [2],
208 'member_status_id' => [2],
209 ],
210 'expected' => [2],
211 ],
212 'IN_format_single_not_1' => [
213 'form_values' => [
214 'membership_type_id' => ['IN' => [2]],
215 'membership_status_id' => ['IN' => [2]],
216 ],
217 'expected' => [2],
218 ],
219 'IN_format_1_later' => [
220 'form_values' => [
221 'membership_type_id' => ['IN' => [2, 1]],
222 'membership_status_id' => ['IN' => [2, 1]],
223 ],
224 'expected' => [2, 1],
225 ],
226 ];
227 }
228
229 }