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
{
36 use CRMTraits_Custom_CustomDataTrait
;
39 * Sets up the fixture, for example, opens a network connection.
41 * This method is called before a test is executed.
43 protected function setUp() {
48 * Tears down the fixture, for example, closes a network connection.
50 * This method is called after a test is executed.
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']}");
61 'civicrm_mapping_field',
64 'civicrm_saved_search',
65 'civicrm_custom_field',
66 'civicrm_custom_group',
71 * Test setDefaults for privacy radio buttons.
75 public function testDefaultValues() {
76 $this->createCustomGroupWithFieldOfType([], 'int');
77 $sg = new CRM_Contact_Form_Search_Advanced();
78 $sg->controller
= new CRM_Core_Controller();
80 'group_search_selected' => 'group',
81 'privacy_options' => ['do_not_email'],
82 'privacy_operator' => 'OR',
83 'privacy_toggle' => 2,
85 'component_mode' => 1,
86 'custom_' . $this->ids
['CustomField']['int'] . '_from' => 0,
87 'custom_' . $this->ids
['CustomField']['int'] . '_to' => '',
89 CRM_Core_DAO
::executeQuery(
90 "INSERT INTO civicrm_saved_search (form_values) VALUES('" . serialize($formValues) . "')"
92 $ssID = CRM_Core_DAO
::singleValueQuery('SELECT LAST_INSERT_ID()');
93 $sg->set('ssID', $ssID);
94 $sg->set('formValues', $formValues);
96 $defaults = $sg->setDefaultValues();
98 $this->checkArrayEquals($defaults, $formValues);
102 * Test setDefaults for privacy radio buttons.
106 public function testGetFormValuesWithCustomFields() {
107 $this->createCustomGroupWithFieldsOfAllTypes();
108 $sg = new CRM_Contact_Form_Search_Advanced();
109 $sg->controller
= new CRM_Core_Controller();
111 'group_search_selected' => 'group',
112 'privacy_options' => ['do_not_email'],
113 'privacy_operator' => 'OR',
114 'privacy_toggle' => 2,
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',
122 CRM_Core_DAO
::executeQuery(
123 "INSERT INTO civicrm_saved_search (form_values) VALUES('" . serialize($formValues) . "')"
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);
131 * Test fixValues function.
133 * @dataProvider getSavedSearches
135 public function testGetFormValues($formValues, $expectedResult, $searchDescription) {
136 CRM_Core_DAO
::executeQuery(
137 "INSERT INTO civicrm_saved_search (form_values) VALUES('" . serialize($formValues) . "')"
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);
147 * Test if skipped elements are correctly
148 * stored and retrieved as formvalues.
150 public function testSkippedElements() {
151 $relTypeID = $this->relationshipTypeCreate();
152 $savedSearch = new CRM_Contact_BAO_SavedSearch();
155 'title' => 'testsmart',
156 'radio_ts' => 'ts_all',
157 'component_mode' => CRM_Contact_BAO_Query
::MODE_CONTACTS
,
158 'display_relationship_type' => "{$relTypeID}_a_b",
162 CRM_Contact_BAO_SavedSearch
::saveSkippedElement($queryParams, $formValues);
163 $savedSearch->form_values
= serialize($queryParams);
164 $savedSearch->save();
166 $result = CRM_Contact_BAO_SavedSearch
::getFormValues(CRM_Core_DAO
::singleValueQuery('SELECT LAST_INSERT_ID()'));
169 'component_mode' => CRM_Contact_BAO_Query
::MODE_CONTACTS
,
170 'display_relationship_type' => "{$relTypeID}_a_b",
173 $this->checkArrayEquals($result, $expectedResult);
177 * Get variants of the fields we want to test.
181 public function getSavedSearches() {
183 $searches = $this->getSearches();
184 foreach ($searches as $key => $search) {
185 $return[] = [$search['form_values'], $search['expected'], $key];
191 * Get variants of potential saved form values.
193 * Note that we include 1 in various ways to cover the possibility that 1 is treated as a boolean.
197 public function getSearches() {
199 'checkbox_format_1_first' => [
201 'member_membership_type_id' => [1 => 1, 2 => 1],
202 'member_status_id' => [1 => 1, 2 => 1],
204 'expected' => [1, 2],
206 'checkbox_format_1_later' => [
208 'member_membership_type_id' => [2 => 1, 1 => 1],
209 'member_status_id' => [2 => 1, 1 => 1],
211 'expected' => [2, 1],
213 'checkbox_format_single_use_1' => [
215 'member_membership_type_id' => [1 => 1],
216 'member_status_id' => [1 => 1],
220 'checkbox_format_single_not_1' => [
222 'member_membership_type_id' => [2 => 1],
223 'member_status_id' => [2 => 1],
229 'member_membership_type_id' => [1, 2],
230 'member_status_id' => [1, 2],
232 'expected' => [1, 2],
234 'array_format_1_later' => [
236 'member_membership_type_id' => [2, 1],
237 'member_status_id' => [2, 1],
239 'expected' => [2, 1],
241 'array_format_single_use_1' => [
243 'member_membership_type_id' => [1],
244 'member_status_id' => [1],
248 'array_format_single_not_1' => [
250 'member_membership_type_id' => [2],
251 'member_status_id' => [2],
255 'IN_format_single_not_1' => [
257 'membership_type_id' => ['IN' => [2]],
258 'membership_status_id' => ['IN' => [2]],
262 'IN_format_1_later' => [
264 'membership_type_id' => ['IN' => [2, 1]],
265 'membership_status_id' => ['IN' => [2, 1]],
267 'expected' => [2, 1],