3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 * Include parent class definition
33 * Test contact custom search functions
38 class CRM_Contact_Form_SelectorTest
extends CiviUnitTestCase
{
40 public function tearDown() {
44 * Test the query from the selector class is consistent with the dataset expectation.
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.
50 * @dataProvider querySets
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']));
57 $selector = new CRM_Contact_Selector(
59 $dataSet['form_values'],
61 $dataSet['return_properties'],
63 $dataSet['includeContactIds'],
64 $dataSet['searchDescendentGroups'],
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]));
76 * Data sets for testing.
78 public function querySets() {
82 'description' => 'Normal default behaviour',
83 'class' => 'CRM_Contact_Selector',
84 'settings' => array(),
85 'form_values' => array('email' => 'mickey@mouseville.com'),
87 'return_properties' => NULL,
88 'context' => 'advanced',
89 'action' => CRM_Core_Action
::ADVANCED
,
90 'includeContactIds' => NULL,
91 'searchDescendentGroups' => FALSE,
92 'expected_query' => array(
95 2 => "WHERE ( civicrm_email.email LIKE '%mickey@mouseville.com%' ) AND (contact_a.is_deleted = 0)",
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'),
106 'return_properties' => NULL,
107 'context' => 'advanced',
108 'action' => CRM_Core_Action
::ADVANCED
,
109 'includeContactIds' => NULL,
110 'searchDescendentGroups' => FALSE,
111 'expected_query' => array(
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)",
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'),
125 'return_properties' => NULL,
126 'context' => 'advanced',
127 'action' => CRM_Core_Action
::ADVANCED
,
128 'includeContactIds' => NULL,
129 'searchDescendentGroups' => FALSE,
130 'expected_query' => array(
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)",
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'),
145 'return_properties' => NULL,
146 'context' => 'advanced',
147 'action' => CRM_Core_Action
::ADVANCED
,
148 'includeContactIds' => NULL,
149 'searchDescendentGroups' => FALSE,
150 'expected_query' => array(
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)",
161 * Test the contact ID query does not fail on country search.
163 public function testContactIDQuery() {
172 $searchOBJ = new CRM_Contact_Selector(NULL);
173 $searchOBJ->contactIDQuery($params, '1_u');
177 * Get the default select string since this is generally consistent.
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`';
200 * Get the default from string since this is generally consistent.
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 ';
211 * Strangle strings into a more matchable format.
213 * @param string $string
216 public function strWrangle($string) {
217 return str_replace(' ', ' ', $string);
221 * Swap out default parts of the query for the actual string.
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.
226 * @param array $expectedQuery
228 public function wrangleDefaultClauses(&$expectedQuery) {
229 if ($expectedQuery[0] == 'default') {
230 $expectedQuery[0] = $this->getDefaultSelectString();
232 if ($expectedQuery[1] == 'default') {
233 $expectedQuery[1] = $this->getDefaultFromString();