4 * Include dataProvider for tests
7 class CRM_Core_BAO_CustomQueryTest
extends CiviUnitTestCase
{
8 use CRMTraits_Custom_CustomDataTrait
;
11 * Restore database to empty state.
13 * Note that rollback won't remove custom tables.
17 public function tearDown() {
21 $this->quickCleanup($tablesToTruncate, TRUE);
26 * Test filtering by relative custom data dates.
28 * @throws \CRM_Core_Exception
30 public function testSearchCustomDataDateRelative() {
31 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, 'ContactTestTest');
32 $dateCustomField = $this->customFieldCreate([
33 'custom_group_id' => $ids['custom_group_id'],
34 'label' => 'date field',
35 'data_type' => 'Date',
36 'html_type' => 'Select Date',
37 'default_value' => NULL,
39 $dateCustomFieldName = 'custom_' . $dateCustomField['id'];
41 $dateCustomFieldName . '_relative' => 'this.year',
42 $dateCustomFieldName . '_from' => '',
43 $dateCustomFieldName . '_to' => '',
45 // Assigning the relevant form value to be within a custom key is normally done in
46 // build field params. It would be better if it were all done in convertFormValues
47 // but for now we just imitate it.
49 $params = CRM_Contact_BAO_Query
::convertFormValues($formValues);
50 $queryObj = new CRM_Contact_BAO_Query($params);
52 "civicrm_value_testsearchcus_1.date_field_2 BETWEEN '" . date('Y') . "0101000000' AND '" . date('Y') . "1231235959'",
53 $queryObj->_where
[0][0]
55 $this->assertEquals("date field is This calendar year (between January 1st, " . date('Y') . " 12:00 AM and December 31st, " . date('Y') . " 11:59 PM)", $queryObj->_qill
[0][0]);
56 $queryObj = new CRM_Core_BAO_CustomQuery($params);
58 'id' => $dateCustomField['id'],
59 'label' => 'date field',
60 'extends' => 'Contact',
61 'data_type' => 'Date',
62 'html_type' => 'Select Date',
63 'is_search_range' => '0',
64 'column_name' => 'date_field_' . $dateCustomField['id'],
65 'table_name' => 'civicrm_value_testsearchcus_' . $ids['custom_group_id'],
66 'option_group_id' => NULL,
67 'groupTitle' => 'testSearchCustomDataDateRelative',
68 'default_value' => NULL,
69 'text_length' => NULL,
70 'options_per_line' => NULL,
71 'custom_group_id' => '1',
72 'extends_entity_column_value' => NULL,
73 'extends_entity_column_id' => NULL,
76 'date_format' => 'mm/dd/yy',
77 'time_format' => NULL,
79 'extends_table' => 'civicrm_contact',
80 'search_table' => 'contact_a',
81 'headerPattern' => '//',
82 'title' => 'date field',
83 'custom_field_id' => $dateCustomField['id'],
84 'name' => 'custom_' . $dateCustomField['id'],
86 'where' => 'civicrm_value_testsearchcus_' . $ids['custom_group_id'] . '.date_field_' . $dateCustomField['id'],
87 ], $queryObj->getFields()[$dateCustomField['id']]);
92 * Test filtering by relative custom data dates.
94 * @throws \CRM_Core_Exception
96 public function testSearchCustomDataDateFromTo() {
97 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, 'ContactTestTest');
98 $dateCustomField = $this->customFieldCreate([
99 'custom_group_id' => $ids['custom_group_id'],
100 'label' => 'date field',
101 'data_type' => 'Date',
102 'html_type' => 'Select Date',
103 'default_value' => NULL,
105 $dateCustomFieldName = 'custom_' . $dateCustomField['id'];
106 // Assigning the relevant form value to be within a custom key is normally done in
107 // build field params. It would be better if it were all done in convertFormValues
108 // but for now we just imitate it.
110 $dateCustomFieldName . '_from' => '2014-06-06',
111 $dateCustomFieldName . '_to' => '2015-06-06',
114 $params[$dateCustomField['id']] = CRM_Contact_BAO_Query
::convertFormValues($formValues);
115 $queryObj = new CRM_Core_BAO_CustomQuery($params);
118 'civicrm_value_testsearchcus_1.date_field_2 BETWEEN "20140606000000" AND "20150606235959"',
119 $queryObj->_where
[0][0]
121 $this->assertEquals($queryObj->_qill
[0][0], "date field BETWEEN 'June 6th, 2014 12:00 AM AND June 6th, 2015 11:59 PM'");
123 //CRM-17236 - Test custom date is correctly displayed without time.
124 $formattedValue = CRM_Core_BAO_CustomField
::displayValue(date('Ymdhms'), $dateCustomField['id']);
125 $this->assertEquals(date('m/d/Y'), $formattedValue);
129 * Test filtering by relative custom data.
131 public function testSearchCustomDataFromTo() {
132 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, 'ContactTestTest');
138 foreach ($datas as $type => $data) {
139 $customField = $this->customFieldCreate(
141 'custom_group_id' => $ids['custom_group_id'],
142 'label' => "$type field",
143 'data_type' => $type,
144 'html_type' => 'Text',
145 'default_value' => NULL,
148 $customFieldName = 'custom_' . $customField['id'];
149 // Assigning the relevant form value to be within a custom key is normally done in
150 // build field params. It would be better if it were all done in convertFormValues
151 // but for now we just imitate it.
155 $customFieldName . '_from' => $from,
156 $customFieldName . '_to' => $to,
159 $params = [$customField['id'] => CRM_Contact_BAO_Query
::convertFormValues($formValues)];
160 $queryObj = new CRM_Core_BAO_CustomQuery($params);
163 "civicrm_value_testsearchcus_1." . strtolower($type) . "_field_{$customField['id']} BETWEEN \"$from\" AND \"$to\"",
164 $queryObj->_where
[0][0]
166 $this->assertEquals($queryObj->_qill
[0][0], "$type field BETWEEN $from, $to");
171 * Test filtering by relative custom data.
173 public function testSearchCustomDataFromAndTo() {
174 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, 'ContactTestTest');
176 'Date' => '2015-06-06',
181 foreach ($datas as $type => $data) {
182 $isDate = ($type === 'Date');
183 $customField = $this->customFieldCreate(
185 'custom_group_id' => $ids['custom_group_id'],
186 'label' => "$type field",
187 'data_type' => $type,
188 'html_type' => ($isDate) ?
'Select Date' : 'Text',
189 'default_value' => NULL,
192 $customFieldName = 'custom_' . $customField['id'];
194 $expectedValue = ($isDate) ?
'"20150606235959"' : (($type == 'Money') ?
$data : "\"$data\"");
195 $expectedQillValue = ($isDate) ?
"'June 6th, 2015 11:59 PM'" : $data;
197 // Assigning the relevant form value to be within a custom key is normally done in
198 // build field params. It would be better if it were all done in convertFormValues
199 // but for now we just imitate it.
201 //Scenrio 2 : TO date filter
203 $customFieldName . '_to' => $data,
206 $params = [$customField['id'] => CRM_Contact_BAO_Query
::convertFormValues($formValues)];
207 $queryObj = new CRM_Core_BAO_CustomQuery($params);
209 $wierdStringThatMeansGreaterEquals = chr(226) . chr(137) . chr(164);
212 "civicrm_value_testsearchcus_1." . strtolower($type) . "_field_{$customField['id']} <= $expectedValue",
213 $queryObj->_where
[0][0]
215 $this->assertEquals($queryObj->_qill
[0][0],
216 "$type field " . $wierdStringThatMeansGreaterEquals . " $expectedQillValue"
219 //Scenrio 2 : FROM date filter
221 $customFieldName . '_from' => $data,
224 $params = [$customField['id'] => CRM_Contact_BAO_Query
::convertFormValues($formValues)];
225 $queryObj = new CRM_Core_BAO_CustomQuery($params);
227 $wierdStringThatMeansLessThanEquals = chr(226) . chr(137) . chr(165);
229 $expectedValue = ($isDate) ?
'"20150606000000"' : $expectedValue;
230 $expectedQillValue = ($isDate) ?
"'June 6th, 2015 12:00 AM'" : $expectedQillValue;
232 "civicrm_value_testsearchcus_1." . strtolower($type) . "_field_{$customField['id']} >= $expectedValue",
233 $queryObj->_where
[0][0]
235 $this->assertEquals($queryObj->_qill
[0][0],
236 "$type field " . $wierdStringThatMeansLessThanEquals . " $expectedQillValue"
242 * Test filtering by relative custom data dates.
244 * @throws \CRM_Core_Exception
246 public function testSearchCustomDataDateEquals() {
247 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, 'ContactTestTest');
248 $dateCustomField = $this->customFieldCreate([
249 'custom_group_id' => $ids['custom_group_id'],
250 'label' => 'date field',
251 'data_type' => 'Date',
252 'html_type' => 'Select Date',
253 'default_value' => NULL,
255 $dateCustomFieldName = 'custom_' . $dateCustomField['id'];
256 $this->individualCreate([$dateCustomFieldName => "2015-01-01"]);
257 // Assigning the relevant form value to be within a custom key is normally done in
258 // build field params. It would be better if it were all done in convertFormValues
259 // but for now we just imitate it.
260 $formValues = [$dateCustomFieldName => '2015-06-06'];
261 $params[$dateCustomField['id']] = CRM_Contact_BAO_Query
::convertFormValues($formValues);
262 $queryObj = new CRM_Core_BAO_CustomQuery($params);
266 "civicrm_value_testsearchcus_1.date_field_2 = '2015-06-06'",
267 $queryObj->_where
[0][0]
269 $this->assertEquals($queryObj->_qill
[0][0], "date field = 'June 6th, 2015'");
273 * Test search builder style query including custom address fields.
275 * @throws \CRM_Core_Exception
277 public function testAddressCustomFields() {
278 $this->createCustomGroupWithFieldOfType(['extends' => 'Address'], 'int');
279 $individualID = $this->individualCreate();
280 $this->callAPISuccess('Address', 'create', [
281 'contact_id' => $individualID,
282 'street_address' => '10 Downing Street',
283 'location_type_id' => 'Home',
284 $this->getCustomFieldName('int') => 5,
287 $queryObject = new CRM_Contact_BAO_Query(
288 [[$this->getCustomFieldName('int') . '-1', '=', 5, 1, 0]],
289 ['contact_type' => 1, 'location' => ['Home' => ['location_type' => 1, $this->getCustomFieldName('int') => 1]]]
291 $queryObject->query();
292 $tableName = $this->getCustomGroupTable();
293 $fieldName = $this->getCustomFieldColumnName('int');
295 $this->assertEquals([], $queryObject->_where
[0]);
296 $this->assertEquals($tableName . '.' . $fieldName . ' = 5', implode(', ', $queryObject->_where
[1]));
297 $this->assertEquals(1, $queryObject->_whereTables
['civicrm_contact']);
298 $this->assertEquals('LEFT JOIN civicrm_address `Home-address` ON (`Home-address`.contact_id = contact_a.id AND `Home-address`.location_type_id = 1)', trim($queryObject->_whereTables
['Home-address']));
299 $this->assertEquals("LEFT JOIN {$tableName} ON {$tableName}.entity_id = `Home-address`.id", trim($queryObject->_whereTables
[$tableName]));
300 $this->assertEquals([], $queryObject->_qill
[0]);
301 $this->assertEquals(['Enter integer here = 5'], $queryObject->_qill
[1]);