4 * Include dataProvider for tests
7 class CRM_Core_BAO_CustomQueryTest
extends CiviUnitTestCase
{
10 * Restore database to empty state.
12 * Note that rollback won't remove custom tables.
16 public function tearDown() {
17 $tablesToTruncate = array(
20 $this->quickCleanup($tablesToTruncate, TRUE);
25 * Test filtering by relative custom data dates.
27 public function testSearchCustomDataDateRelative() {
28 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, 'ContactTestTest');
29 $dateCustomField = $this->customFieldCreate(array(
30 'custom_group_id' => $ids['custom_group_id'],
31 'label' => 'date field',
32 'data_type' => 'Date',
33 'html_type' => 'Select Date',
34 'default_value' => NULL,
36 $dateCustomFieldName = 'custom_' . $dateCustomField['id'];
38 $dateCustomFieldName . '_relative' => 'this.year',
39 $dateCustomFieldName . '_from' => '',
40 $dateCustomFieldName . '_to' => '',
42 // Assigning the relevant form value to be within a custom key is normally done in
43 // build field params. It would be better if it were all done in convertFormValues
44 // but for now we just imitate it.
45 $params[$dateCustomField['id']] = CRM_Contact_BAO_Query
::convertFormValues($formValues);
46 $queryObj = new CRM_Core_BAO_CustomQuery($params);
49 'civicrm_value_testsearchcus_1.date_field_2 BETWEEN "' . date('Y') . '0101000000" AND "' . date('Y') . '1231235959"',
50 $queryObj->_where
[0][0]
52 $this->assertEquals($queryObj->_qill
[0][0], "date field BETWEEN 'January 1st, " . date('Y') . " 12:00 AM AND December 31st, " . date('Y') . " 11:59 PM'");
56 * Test filtering by relative custom data dates.
58 public function testSearchCustomDataDateFromTo() {
59 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, 'ContactTestTest');
60 $dateCustomField = $this->customFieldCreate(array(
61 'custom_group_id' => $ids['custom_group_id'],
62 'label' => 'date field',
63 'data_type' => 'Date',
64 'html_type' => 'Select Date',
65 'default_value' => NULL,
67 $dateCustomFieldName = 'custom_' . $dateCustomField['id'];
68 // Assigning the relevant form value to be within a custom key is normally done in
69 // build field params. It would be better if it were all done in convertFormValues
70 // but for now we just imitate it.
72 $dateCustomFieldName . '_from' => '2014-06-06',
73 $dateCustomFieldName . '_to' => '2015-06-06',
76 $params[$dateCustomField['id']] = CRM_Contact_BAO_Query
::convertFormValues($formValues);
77 $queryObj = new CRM_Core_BAO_CustomQuery($params);
80 'civicrm_value_testsearchcus_1.date_field_2 BETWEEN "20140606000000" AND "20150606235959"',
81 $queryObj->_where
[0][0]
83 $this->assertEquals($queryObj->_qill
[0][0], "date field BETWEEN 'June 6th, 2014 12:00 AM AND June 6th, 2015 11:59 PM'");
87 * Test filtering by relative custom data.
89 public function testSearchCustomDataFromTo() {
90 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, 'ContactTestTest');
96 foreach ($datas as $type => $data) {
97 $customField = $this->customFieldCreate(
99 'custom_group_id' => $ids['custom_group_id'],
100 'label' => "$type field",
101 'data_type' => $type,
102 'html_type' => 'Text',
103 'default_value' => NULL,
106 $customFieldName = 'custom_' . $customField['id'];
107 // Assigning the relevant form value to be within a custom key is normally done in
108 // build field params. It would be better if it were all done in convertFormValues
109 // but for now we just imitate it.
113 $customFieldName . '_from' => $from,
114 $customFieldName . '_to' => $to,
117 $params = array($customField['id'] => CRM_Contact_BAO_Query
::convertFormValues($formValues));
118 $queryObj = new CRM_Core_BAO_CustomQuery($params);
121 "civicrm_value_testsearchcus_1." . strtolower($type) . "_field_{$customField['id']} BETWEEN \"$from\" AND \"$to\"",
122 $queryObj->_where
[0][0]
124 $this->assertEquals($queryObj->_qill
[0][0], "$type field BETWEEN $from, $to");
129 * Test filtering by relative custom data.
131 public function testSearchCustomDataFromAndTo() {
132 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, 'ContactTestTest');
134 'Date' => '2015-06-06',
139 foreach ($datas as $type => $data) {
140 $isDate = ($type === 'Date');
141 $customField = $this->customFieldCreate(
143 'custom_group_id' => $ids['custom_group_id'],
144 'label' => "$type field",
145 'data_type' => $type,
146 'html_type' => ($isDate) ?
'Select Date' : 'Text',
147 'default_value' => NULL,
150 $customFieldName = 'custom_' . $customField['id'];
152 $expectedValue = ($isDate) ?
'"20150606235959"' : (($type == 'Money') ?
$data : "\"$data\"");
153 $expectedQillValue = ($isDate) ?
"'June 6th, 2015 11:59 PM'" : $data;
155 // Assigning the relevant form value to be within a custom key is normally done in
156 // build field params. It would be better if it were all done in convertFormValues
157 // but for now we just imitate it.
159 //Scenrio 2 : TO date filter
161 $customFieldName . '_to' => $data,
164 $params = array($customField['id'] => CRM_Contact_BAO_Query
::convertFormValues($formValues));
165 $queryObj = new CRM_Core_BAO_CustomQuery($params);
167 $wierdStringThatMeansGreaterEquals = chr(226) . chr(137) . chr(164);
170 "civicrm_value_testsearchcus_1." . strtolower($type) . "_field_{$customField['id']} <= $expectedValue",
171 $queryObj->_where
[0][0]
173 $this->assertEquals($queryObj->_qill
[0][0],
174 "$type field " . $wierdStringThatMeansGreaterEquals . " $expectedQillValue"
177 //Scenrio 2 : FROM date filter
179 $customFieldName . '_from' => $data,
182 $params = array($customField['id'] => CRM_Contact_BAO_Query
::convertFormValues($formValues));
183 $queryObj = new CRM_Core_BAO_CustomQuery($params);
185 $wierdStringThatMeansLessThanEquals = chr(226) . chr(137) . chr(165);
187 $expectedValue = ($isDate) ?
'"20150606000000"' : $expectedValue;
188 $expectedQillValue = ($isDate) ?
"'June 6th, 2015 12:00 AM'" : $expectedQillValue;
190 "civicrm_value_testsearchcus_1." . strtolower($type) . "_field_{$customField['id']} >= $expectedValue",
191 $queryObj->_where
[0][0]
193 $this->assertEquals($queryObj->_qill
[0][0],
194 "$type field " . $wierdStringThatMeansLessThanEquals . " $expectedQillValue"
200 * Test filtering by relative custom data dates.
202 public function testSearchCustomDataDateEquals() {
203 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, 'ContactTestTest');
204 $dateCustomField = $this->customFieldCreate(array(
205 'custom_group_id' => $ids['custom_group_id'],
206 'label' => 'date field',
207 'data_type' => 'Date',
208 'html_type' => 'Select Date',
209 'default_value' => NULL,
211 $dateCustomFieldName = 'custom_' . $dateCustomField['id'];
212 $this->individualCreate(array($dateCustomFieldName => "2015-01-01"));
213 // Assigning the relevant form value to be within a custom key is normally done in
214 // build field params. It would be better if it were all done in convertFormValues
215 // but for now we just imitate it.
216 $formValues = array($dateCustomFieldName => '2015-06-06');
217 $params[$dateCustomField['id']] = CRM_Contact_BAO_Query
::convertFormValues($formValues);
218 $queryObj = new CRM_Core_BAO_CustomQuery($params);
222 "civicrm_value_testsearchcus_1.date_field_2 = '2015-06-06'",
223 $queryObj->_where
[0][0]
225 $this->assertEquals($queryObj->_qill
[0][0], "date field = 'June 6th, 2015'");