Add test for DB error on search add to group CRM-21159
[civicrm-core.git] / tests / phpunit / CRM / Contact / SelectorTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 * Include parent class definition
30 */
31
32 /**
33 * Test contact custom search functions
34 *
35 * @package CiviCRM
36 * @group headless
37 */
38 class CRM_Contact_Form_SelectorTest extends CiviUnitTestCase {
39
40 public function tearDown() {
41
42 }
43 /**
44 * Test the query from the selector class is consistent with the dataset expectation.
45 *
46 * @param array $dataSet
47 * The data set to be tested. Note that when adding new datasets often only form_values and expected where
48 * clause will need changing.
49 *
50 * @dataProvider querySets
51 */
52 public function testSelectorQuery($dataSet) {
53 $params = CRM_Contact_BAO_Query::convertFormValues($dataSet['form_values'], 0, FALSE, NULL, array());
54 foreach ($dataSet['settings'] as $setting) {
55 $this->callAPISuccess('Setting', 'create', array($setting['name'] => $setting['value']));
56 }
57 $selector = new CRM_Contact_Selector(
58 $dataSet['class'],
59 $dataSet['form_values'],
60 $params,
61 $dataSet['return_properties'],
62 $dataSet['action'],
63 $dataSet['includeContactIds'],
64 $dataSet['searchDescendentGroups'],
65 $dataSet['context']
66 );
67 $queryObject = $selector->getQueryObject();
68 $sql = $queryObject->query();
69 $this->wrangleDefaultClauses($dataSet['expected_query']);
70 foreach ($dataSet['expected_query'] as $index => $queryString) {
71 $this->assertEquals($this->strWrangle($queryString), $this->strWrangle($sql[$index]));
72 }
73 }
74
75 /**
76 * Data sets for testing.
77 */
78 public function querySets() {
79 return array(
80 array(
81 array(
82 'description' => 'Normal default behaviour',
83 'class' => 'CRM_Contact_Selector',
84 'settings' => array(),
85 'form_values' => array('email' => 'mickey@mouseville.com'),
86 'params' => array(),
87 'return_properties' => NULL,
88 'context' => 'advanced',
89 'action' => CRM_Core_Action::ADVANCED,
90 'includeContactIds' => NULL,
91 'searchDescendentGroups' => FALSE,
92 'expected_query' => array(
93 0 => 'default',
94 1 => 'default',
95 2 => "WHERE ( civicrm_email.email LIKE '%mickey@mouseville.com%' ) AND (contact_a.is_deleted = 0)",
96 ),
97 ),
98 ),
99 array(
100 array(
101 'description' => 'Normal default + user added wildcard',
102 'class' => 'CRM_Contact_Selector',
103 'settings' => array(),
104 'form_values' => array('email' => '%mickey@mouseville.com', 'sort_name' => 'Mouse'),
105 'params' => array(),
106 'return_properties' => NULL,
107 'context' => 'advanced',
108 'action' => CRM_Core_Action::ADVANCED,
109 'includeContactIds' => NULL,
110 'searchDescendentGroups' => FALSE,
111 'expected_query' => array(
112 0 => 'default',
113 1 => 'default',
114 2 => "WHERE ( civicrm_email.email LIKE '%mickey@mouseville.com%' AND ( ( ( contact_a.sort_name LIKE '%mouse%' ) OR ( civicrm_email.email LIKE '%mouse%' ) ) ) ) AND (contact_a.is_deleted = 0)",
115 ),
116 ),
117 ),
118 array(
119 array(
120 'description' => 'Site set to not pre-pend wildcard',
121 'class' => 'CRM_Contact_Selector',
122 'settings' => array(array('name' => 'includeWildCardInName', 'value' => FALSE)),
123 'form_values' => array('email' => 'mickey@mouseville.com', 'sort_name' => 'Mouse'),
124 'params' => array(),
125 'return_properties' => NULL,
126 'context' => 'advanced',
127 'action' => CRM_Core_Action::ADVANCED,
128 'includeContactIds' => NULL,
129 'searchDescendentGroups' => FALSE,
130 'expected_query' => array(
131 0 => 'default',
132 1 => 'default',
133 2 => "WHERE ( civicrm_email.email LIKE 'mickey@mouseville.com%' AND ( ( ( contact_a.sort_name LIKE 'mouse%' ) OR ( civicrm_email.email LIKE 'mouse%' ) ) ) ) AND (contact_a.is_deleted = 0)",
134 ),
135 ),
136 ),
137 array(
138 array(
139 'description' => 'Use of quotes for exact string',
140 'use_case_comments' => 'This is something that was in the code but seemingly not working. No UI info on it though!',
141 'class' => 'CRM_Contact_Selector',
142 'settings' => array(array('name' => 'includeWildCardInName', 'value' => FALSE)),
143 'form_values' => array('email' => '"mickey@mouseville.com"', 'sort_name' => 'Mouse'),
144 'params' => array(),
145 'return_properties' => NULL,
146 'context' => 'advanced',
147 'action' => CRM_Core_Action::ADVANCED,
148 'includeContactIds' => NULL,
149 'searchDescendentGroups' => FALSE,
150 'expected_query' => array(
151 0 => 'default',
152 1 => 'default',
153 2 => "WHERE ( civicrm_email.email = 'mickey@mouseville.com' AND ( ( ( contact_a.sort_name LIKE 'mouse%' ) OR ( civicrm_email.email LIKE 'mouse%' ) ) ) ) AND (contact_a.is_deleted = 0)",
154 ),
155 ),
156 ),
157 );
158 }
159
160 /**
161 * Test the contact ID query does not fail on country search.
162 */
163 public function testContactIDQuery() {
164 $params = [[
165 0 => 'country-1',
166 1 => '=',
167 2 => '1228',
168 3 => 1,
169 4 => 0,
170 ]];
171
172 $searchOBJ = new CRM_Contact_Selector(NULL);
173 $searchOBJ->contactIDQuery($params, '1_u');
174 }
175
176 /**
177 * Get the default select string since this is generally consistent.
178 */
179 public function getDefaultSelectString() {
180 return 'SELECT contact_a.id as contact_id, contact_a.contact_type as `contact_type`, contact_a.contact_sub_type as `contact_sub_type`, contact_a.sort_name as `sort_name`,'
181 . ' contact_a.display_name as `display_name`, contact_a.do_not_email as `do_not_email`, contact_a.do_not_phone as `do_not_phone`, contact_a.do_not_mail as `do_not_mail`,'
182 . ' contact_a.do_not_sms as `do_not_sms`, contact_a.do_not_trade as `do_not_trade`, contact_a.is_opt_out as `is_opt_out`, contact_a.legal_identifier as `legal_identifier`,'
183 . ' contact_a.external_identifier as `external_identifier`, contact_a.nick_name as `nick_name`, contact_a.legal_name as `legal_name`, contact_a.image_URL as `image_URL`,'
184 . ' contact_a.preferred_communication_method as `preferred_communication_method`, contact_a.preferred_language as `preferred_language`,'
185 . ' contact_a.preferred_mail_format as `preferred_mail_format`, contact_a.first_name as `first_name`, contact_a.middle_name as `middle_name`, contact_a.last_name as `last_name`,'
186 . ' contact_a.prefix_id as `prefix_id`, contact_a.suffix_id as `suffix_id`, contact_a.formal_title as `formal_title`, contact_a.communication_style_id as `communication_style_id`,'
187 . ' contact_a.job_title as `job_title`, contact_a.gender_id as `gender_id`, contact_a.birth_date as `birth_date`, contact_a.is_deceased as `is_deceased`,'
188 . ' contact_a.deceased_date as `deceased_date`, contact_a.household_name as `household_name`,'
189 . ' IF ( contact_a.contact_type = \'Individual\', NULL, contact_a.organization_name ) as organization_name, contact_a.sic_code as `sic_code`, contact_a.is_deleted as `contact_is_deleted`,'
190 . ' IF ( contact_a.contact_type = \'Individual\', contact_a.organization_name, NULL ) as current_employer, civicrm_address.id as address_id,'
191 . ' civicrm_address.street_address as `street_address`, civicrm_address.supplemental_address_1 as `supplemental_address_1`, '
192 . 'civicrm_address.supplemental_address_2 as `supplemental_address_2`, civicrm_address.supplemental_address_3 as `supplemental_address_3`, civicrm_address.city as `city`, civicrm_address.postal_code_suffix as `postal_code_suffix`, '
193 . 'civicrm_address.postal_code as `postal_code`, civicrm_address.geo_code_1 as `geo_code_1`, civicrm_address.geo_code_2 as `geo_code_2`, '
194 . 'civicrm_address.state_province_id as state_province_id, civicrm_address.country_id as country_id, civicrm_phone.id as phone_id, civicrm_phone.phone_type_id as phone_type_id, '
195 . 'civicrm_phone.phone as `phone`, civicrm_email.id as email_id, civicrm_email.email as `email`, civicrm_email.on_hold as `on_hold`, civicrm_im.id as im_id, '
196 . 'civicrm_im.provider_id as provider_id, civicrm_im.name as `im`, civicrm_worldregion.id as worldregion_id, civicrm_worldregion.name as `world_region`';
197 }
198
199 /**
200 * Get the default from string since this is generally consistent.
201 */
202 public function getDefaultFromString() {
203 return ' FROM civicrm_contact contact_a LEFT JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id AND civicrm_address.is_primary = 1 )'
204 . ' LEFT JOIN civicrm_email ON (contact_a.id = civicrm_email.contact_id AND civicrm_email.is_primary = 1)'
205 . ' LEFT JOIN civicrm_phone ON (contact_a.id = civicrm_phone.contact_id AND civicrm_phone.is_primary = 1)'
206 . ' LEFT JOIN civicrm_im ON (contact_a.id = civicrm_im.contact_id AND civicrm_im.is_primary = 1) '
207 . 'LEFT JOIN civicrm_country ON civicrm_address.country_id = civicrm_country.id LEFT JOIN civicrm_worldregion ON civicrm_country.region_id = civicrm_worldregion.id ';
208 }
209
210 /**
211 * Strangle strings into a more matchable format.
212 *
213 * @param string $string
214 * @return string
215 */
216 public function strWrangle($string) {
217 return str_replace(' ', ' ', $string);
218 }
219
220 /**
221 * Swap out default parts of the query for the actual string.
222 *
223 * Note that it seems to make more sense to resolve this earlier & pass it in from a clean code point of
224 * view, but the output on fail includes long sql statements that are of low relevance then.
225 *
226 * @param array $expectedQuery
227 */
228 public function wrangleDefaultClauses(&$expectedQuery) {
229 if ($expectedQuery[0] == 'default') {
230 $expectedQuery[0] = $this->getDefaultSelectString();
231 }
232 if ($expectedQuery[1] == 'default') {
233 $expectedQuery[1] = $this->getDefaultFromString();
234 }
235 }
236
237 }