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() {
52 'civicrm_mapping_field',
55 'civicrm_saved_search',
60 * Test setDefaults for privacy radio buttons.
64 public function testDefaultValues() {
65 $sg = new CRM_Contact_Form_Search_Advanced();
66 $sg->controller
= new CRM_Core_Controller();
68 'group_search_selected' => 'group',
69 'privacy_options' => ['do_not_email'],
70 'privacy_operator' => 'OR',
71 'privacy_toggle' => 2,
73 'component_mode' => 1,
75 CRM_Core_DAO
::executeQuery(
76 "INSERT INTO civicrm_saved_search (form_values) VALUES('" . serialize($formValues) . "')"
78 $ssID = CRM_Core_DAO
::singleValueQuery('SELECT LAST_INSERT_ID()');
79 $sg->set('ssID', $ssID);
80 $sg->set('formValues', $formValues);
82 $defaults = $sg->setDefaultValues();
84 $this->checkArrayEquals($defaults, $formValues);
88 * Test fixValues function.
90 * @dataProvider getSavedSearches
92 public function testGetFormValues($formValues, $expectedResult, $searchDescription) {
93 CRM_Core_DAO
::executeQuery(
94 "INSERT INTO civicrm_saved_search (form_values) VALUES('" . serialize($formValues) . "')"
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);
104 * Test if skipped elements are correctly
105 * stored and retrieved as formvalues.
107 public function testSkippedElements() {
108 $relTypeID = $this->relationshipTypeCreate();
109 $savedSearch = new CRM_Contact_BAO_SavedSearch();
112 'title' => 'testsmart',
113 'radio_ts' => 'ts_all',
114 'component_mode' => CRM_Contact_BAO_Query
::MODE_CONTACTS
,
115 'display_relationship_type' => "{$relTypeID}_a_b",
119 CRM_Contact_BAO_SavedSearch
::saveSkippedElement($queryParams, $formValues);
120 $savedSearch->form_values
= serialize($queryParams);
121 $savedSearch->save();
123 $result = CRM_Contact_BAO_SavedSearch
::getFormValues(CRM_Core_DAO
::singleValueQuery('SELECT LAST_INSERT_ID()'));
126 'component_mode' => CRM_Contact_BAO_Query
::MODE_CONTACTS
,
127 'display_relationship_type' => "{$relTypeID}_a_b",
130 $this->checkArrayEquals($result, $expectedResult);
134 * Get variants of the fields we want to test.
138 public function getSavedSearches() {
140 $searches = $this->getSearches();
141 foreach ($searches as $key => $search) {
142 $return[] = [$search['form_values'], $search['expected'], $key];
148 * Get variants of potential saved form values.
150 * Note that we include 1 in various ways to cover the possibility that 1 is treated as a boolean.
154 public function getSearches() {
156 'checkbox_format_1_first' => [
158 'member_membership_type_id' => [1 => 1, 2 => 1],
159 'member_status_id' => [1 => 1, 2 => 1],
161 'expected' => [1, 2],
163 'checkbox_format_1_later' => [
165 'member_membership_type_id' => [2 => 1, 1 => 1],
166 'member_status_id' => [2 => 1, 1 => 1],
168 'expected' => [2, 1],
170 'checkbox_format_single_use_1' => [
172 'member_membership_type_id' => [1 => 1],
173 'member_status_id' => [1 => 1],
177 'checkbox_format_single_not_1' => [
179 'member_membership_type_id' => [2 => 1],
180 'member_status_id' => [2 => 1],
186 'member_membership_type_id' => [1, 2],
187 'member_status_id' => [1, 2],
189 'expected' => [1, 2],
191 'array_format_1_later' => [
193 'member_membership_type_id' => [2, 1],
194 'member_status_id' => [2, 1],
196 'expected' => [2, 1],
198 'array_format_single_use_1' => [
200 'member_membership_type_id' => [1],
201 'member_status_id' => [1],
205 'array_format_single_not_1' => [
207 'member_membership_type_id' => [2],
208 'member_status_id' => [2],
212 'IN_format_single_not_1' => [
214 'membership_type_id' => ['IN' => [2]],
215 'membership_status_id' => ['IN' => [2]],
219 'IN_format_1_later' => [
221 'membership_type_id' => ['IN' => [2, 1]],
222 'membership_status_id' => ['IN' => [2, 1]],
224 'expected' => [2, 1],