Merge pull request #6697 from eileenmcnaughton/CRM-17116-custom-date-search-fields
[civicrm-core.git] / tests / phpunit / api / v3 / CustomValueTest.php
1 <?php
2 /**
3 * +--------------------------------------------------------------------+
4 * | CiviCRM version 4.7 |
5 * +--------------------------------------------------------------------+
6 * | Copyright CiviCRM LLC (c) 2004-2015 |
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 require_once 'CiviTest/CiviUnitTestCase.php';
29
30 /**
31 * Class api_v3_CustomValueTest
32 */
33 class api_v3_CustomValueTest extends CiviUnitTestCase {
34 protected $_apiversion = 3;
35 protected $ids;
36 protected $optionGroup;
37
38 public $DBResetRequired = FALSE;
39
40 public function setUp() {
41 parent::setUp();
42 }
43
44 public function _populateOptionAndCustomGroup($type = NULL) {
45 $dataValues = array(
46 'integer' => array(1, 2, 3),
47 'number' => array(10.11, 20.22, 30.33),
48 'string' => array(substr(sha1(rand()), 0, 4), substr(sha1(rand()), 0, 3), substr(sha1(rand()), 0, 2)),
49 'country' => array_rand(CRM_Core_PseudoConstant::country(FALSE, FALSE), 3),
50 'state_province' => array_rand(CRM_Core_PseudoConstant::stateProvince(FALSE, FALSE), 3),
51 'date' => NULL,
52 'contact' => NULL,
53 'boolean' => NULL,
54 );
55
56 $dataValues = !empty($type) ? array($type => $dataValues[$type]) : $dataValues;
57
58 foreach ($dataValues as $dataType => $values) {
59 $this->optionGroup[$dataType] = array('values' => $values);
60 if (!empty($values)) {
61 $result = $this->callAPISuccess('OptionGroup', 'create',
62 array(
63 'name' => "{$dataType}_group",
64 'api.option_value.create' => array('label' => "$dataType 1", 'value' => $values[0]),
65 'api.option_value.create.1' => array('label' => "$dataType 2", 'value' => $values[1]),
66 'api.option_value.create.2' => array('label' => "$dataType 3", 'value' => $values[2]),
67 )
68 );
69 $this->optionGroup[$dataType]['id'] = $result['id'];
70 }
71 elseif ($dataType == 'contact') {
72 for ($i = 0; $i < 3; $i++) {
73 $result = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'email' => substr(sha1(rand()), 0, 7) . '@yahoo.com'));
74 $this->optionGroup[$dataType]['values'][$i] = $result['id'];
75 }
76 }
77 $this->ids[$dataType] = $this->entityCustomGroupWithSingleFieldCreate("$dataType Custom Group", 'Contacts');
78 }
79
80 }
81
82 public function tearDown() {
83 $tablesToTruncate = array(
84 'civicrm_email',
85 'civicrm_custom_field',
86 'civicrm_custom_group',
87 'civicrm_contact',
88 );
89
90 // true tells quickCleanup to drop any tables that might have been created in the test
91 $this->quickCleanup($tablesToTruncate, TRUE);
92
93 // cleanup created option group for each custom-set before running next test
94 if (!empty($this->optionGroup)) {
95 foreach ($this->optionGroup as $type => $value) {
96 if (!empty($value['id'])) {
97 $this->callAPISuccess('OptionGroup', 'delete', array('id' => $value['id']));
98 }
99 }
100 }
101 }
102
103 public function testCreateCustomValue() {
104 $this->_populateOptionAndCustomGroup();
105
106 $customFieldDataType = CRM_Core_BAO_CustomField::dataType();
107 $dataToHtmlTypes = CRM_Core_BAO_CustomField::dataToHtml();
108 $count = 0;
109
110 foreach ($customFieldDataType as $dataType => $label) {
111 switch ($dataType) {
112 case 'String':
113 case 'Link':
114 case 'Int':
115 case 'Float':
116 case 'Money':
117 case 'Date':
118 case 'Country':
119 case 'StateProvince':
120 case 'Boolean':
121
122 //Based on the custom field data-type choose desired SQL operators(to test with) and basic $type
123 if (in_array($dataType, array('String', 'Link'))) {
124 $validSQLOperators = array('=', '!=', 'IN', 'NOT IN', 'LIKE', 'NOT LIKE', 'IS NOT NULL', 'IS NULL');
125 $type = 'string';
126 }
127 elseif ($dataType == 'Boolean') {
128 $validSQLOperators = array('=', '!=', 'IS NOT NULL', 'IS NULL');
129 $type = 'boolean';
130 }
131 else {
132 if ($dataType == 'Country') {
133 $type = 'country';
134 }
135 elseif ($dataType == 'StateProvince') {
136 $type = 'state_province';
137 }
138 elseif ($dataType == 'ContactReference') {
139 $type = 'contact';
140 }
141 elseif ($dataType == 'Date') {
142 $type = 'date';
143 }
144 else {
145 $type = $dataType == 'Int' ? 'integer' : 'number';
146 }
147 $validSQLOperators = array('=', '!=', 'IN', 'NOT IN', '<=', '>=', '>', '<', 'IS NOT NULL', 'IS NULL');
148 }
149
150 //Create custom field of $dataType and html-type $html
151 foreach ($dataToHtmlTypes[$count] as $html) {
152 $params = array(
153 'custom_group_id' => $this->ids[$type]['custom_group_id'],
154 'label' => "$dataType - $html",
155 'data_type' => $dataType,
156 'html_type' => $html,
157 'default_value' => NULL,
158 );
159 if (!in_array($html, array('Text', 'TextArea')) && !in_array($dataType, array('Link', 'Date', 'ContactReference', 'Boolean'))) {
160 $params += array('option_group_id' => $this->optionGroup[$type]['id']);
161 }
162 $customField = $this->customFieldCreate($params);
163 //Now test with $validSQLOperator SQL operators against its custom value(s)
164 $this->_testCustomValue($customField['values'][$customField['id']], $validSQLOperators, $type);
165 }
166 $count++;
167 break;
168
169 default:
170 // skipping File data-type
171 $count++;
172 break;
173 }
174 }
175 }
176
177 public function _testCustomValue($customField, $sqlOps, $type) {
178 $isSerialized = CRM_Core_BAO_CustomField::isSerialized($customField);
179 $customId = $customField['id'];
180 $params = array(
181 'contact_type' => 'Individual',
182 'email' => substr(sha1(rand()), 0, 7) . 'man1@yahoo.com',
183 );
184 $result = $this->callAPISuccess('Contact', 'create', $params);
185 $contactId = $result['id'];
186
187 $count = rand(1, 2);
188 $seperator = CRM_Core_DAO::VALUE_SEPARATOR;
189 if ($isSerialized) {
190 $selectedValue = $this->optionGroup[$type]['values'];
191 $notselectedValue = $selectedValue[$count];
192 unset($selectedValue[$count]);
193 }
194 elseif ($customField['html_type'] == 'Link') {
195 $selectedValue = "http://" . substr(sha1(rand()), 0, 7) . ".com";
196 $notselectedValue = "http://" . substr(sha1(rand()), 0, 7) . ".com";
197 }
198 elseif ($type == 'date') {
199 $selectedValue = date('Ymd');
200 $notselectedValue = $lesserSelectedValue = date('Ymd', strtotime('yesterday'));
201 $greaterSelectedValue = date('Ymd', strtotime('+ 1 day'));
202 }
203 elseif ($type == 'contact') {
204 $selectedValue = $this->optionGroup[$type]['values'][1];
205 $notselectedValue = $this->optionGroup[$type]['values'][0];
206 }
207 elseif ($type == 'boolean') {
208 $selectedValue = 1;
209 $notselectedValue = 0;
210 }
211 else {
212 $selectedValue = $this->optionGroup[$type]['values'][0];
213 $notselectedValue = $this->optionGroup[$type]['values'][$count];
214 if (in_array(">", $sqlOps)) {
215 $greaterSelectedValue = $selectedValue + 1;
216 $lesserSelectedValue = $selectedValue - 1;
217 }
218 }
219
220 $params = array('entity_id' => $contactId, 'custom_' . $customId => $selectedValue);
221 $this->callAPISuccess('CustomValue', 'create', $params);
222
223 foreach ($sqlOps as $op) {
224 $qillOp = CRM_Utils_Array::value($op, CRM_Core_SelectValues::getSearchBuilderOperators(), $op);
225 switch ($op) {
226 case '=':
227 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => (is_array($selectedValue) ? implode(CRM_Core_DAO::VALUE_SEPARATOR, $selectedValue) : $selectedValue)));
228 $this->assertEquals($contactId, $result['id']);
229 break;
230
231 case '!=':
232 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => $notselectedValue)));
233 $this->assertEquals(TRUE, array_key_exists($contactId, $result['values']));
234 break;
235
236 case '>':
237 case '<':
238 case '>=':
239 case '<=':
240 if ($isSerialized) {
241 continue;
242 }
243 // To be precise in for these operator we can't just rely on one contact,
244 // hence creating multiple contact with custom value less/more then $selectedValue respectively
245 $result = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'email' => substr(sha1(rand()), 0, 7) . 'man2@yahoo.com'));
246 $contactId2 = $result['id'];
247 $this->callAPISuccess('CustomValue', 'create', array('entity_id' => $contactId2, 'custom_' . $customId => $lesserSelectedValue));
248
249 if ($op == '>') {
250 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => $lesserSelectedValue)));
251 $this->assertEquals($contactId, $result['id']);
252 }
253 elseif ($op == '<') {
254 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => $selectedValue)));
255 $this->assertEquals($contactId2, $result['id']);
256 }
257 else {
258 $result = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'email' => substr(sha1(rand()), 0, 7) . 'man3@yahoo.com'));
259 $contactId3 = $result['id'];
260 $this->callAPISuccess('CustomValue', 'create', array('entity_id' => $contactId3, 'custom_' . $customId => $greaterSelectedValue));
261
262 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => $selectedValue)));
263
264 $this->assertEquals($contactId, $result['values'][$contactId]['id']);
265 if ($op == '>=') {
266 $this->assertEquals($contactId3, $result['values'][$contactId3]['id']);
267 }
268 else {
269 $this->assertEquals($contactId2, $result['values'][$contactId2]['id']);
270 }
271 $this->callAPISuccess('contact', 'delete', array('id' => $contactId3));
272 }
273
274 $this->callAPISuccess('contact', 'delete', array('id' => $contactId2));
275 break;
276
277 case 'IN':
278 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => (array) $selectedValue)));
279 $this->assertEquals($contactId, $result['id']);
280 break;
281
282 case 'NOT IN':
283 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => (array) $notselectedValue)));
284 $this->assertEquals($contactId, $result['id']);
285 break;
286
287 case 'LIKE':
288 $selectedValue = is_array($selectedValue) ? $selectedValue[0] : $selectedValue;
289 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => "%$selectedValue%")));
290 $this->assertEquals($contactId, $result['id']);
291 break;
292
293 case 'NOT LIKE':
294 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => $notselectedValue)));
295 $this->assertEquals($contactId, $result['id']);
296 break;
297
298 case 'IS NULL':
299 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => 1)));
300 $this->assertEquals(FALSE, array_key_exists($contactId, $result['values']));
301 break;
302
303 case 'IS NOT NULL':
304 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => 1)));
305 $this->assertEquals($contactId, $result['id']);
306 break;
307 }
308 }
309
310 $this->callAPISuccess('Contact', 'delete', array('id' => $contactId));
311 }
312
313 /**
314 * Ensure custom data is updated when option values are modified
315 *
316 * @link https://issues.civicrm.org/jira/browse/CRM-11856
317 *
318 * @throws \CiviCRM_API3_Exception
319 */
320 public function testAlterOptionValue() {
321 $this->_populateOptionAndCustomGroup('string');
322
323 $selectField = $this->customFieldCreate(array(
324 'custom_group_id' => $this->ids['string']['custom_group_id'],
325 'label' => 'Custom Select',
326 'html_type' => 'Select',
327 'option_group_id' => $this->optionGroup['string']['id'],
328 ));
329 $selectField = civicrm_api3('customField', 'getsingle', array('id' => $selectField['id']));
330 $radioField = $this->customFieldCreate(array(
331 'custom_group_id' => $this->ids['string']['custom_group_id'],
332 'label' => 'Custom Radio',
333 'html_type' => 'Radio',
334 'option_group_id' => $selectField['option_group_id'],
335 ));
336 $multiSelectField = $this->customFieldCreate(array(
337 'custom_group_id' => $this->ids['string']['custom_group_id'],
338 'label' => 'Custom Multi-Select',
339 'html_type' => 'Multi-Select',
340 'option_group_id' => $selectField['option_group_id'],
341 ));
342 $selectName = 'custom_' . $selectField['id'];
343 $radioName = 'custom_' . $radioField['id'];
344 $multiSelectName = 'custom_' . $multiSelectField['id'];
345 $controlFieldName = 'custom_' . $this->ids['string']['custom_field_id'];
346
347 $params = array(
348 'first_name' => 'abc4',
349 'last_name' => 'xyz4',
350 'contact_type' => 'Individual',
351 'email' => 'man4@yahoo.com',
352 $selectName => $this->optionGroup['string']['values'][0],
353 $multiSelectName => $this->optionGroup['string']['values'],
354 $radioName => $this->optionGroup['string']['values'][1],
355 // The control group in a science experiment should be unaffected
356 $controlFieldName => $this->optionGroup['string']['values'][2],
357 );
358
359 $contact = $this->callAPISuccess('Contact', 'create', $params);
360
361 $result = $this->callAPISuccess('Contact', 'getsingle', array(
362 'id' => $contact['id'],
363 'return' => array($selectName, $multiSelectName),
364 ));
365 $this->assertEquals($params[$selectName], $result[$selectName]);
366 $this->assertEquals($params[$multiSelectName], $result[$multiSelectName]);
367
368 $this->callAPISuccess('OptionValue', 'create', array(
369 'value' => 'one-modified',
370 'option_group_id' => $selectField['option_group_id'],
371 'name' => 'string 1',
372 'options' => array(
373 'match-mandatory' => array('option_group_id', 'name'),
374 ),
375 ));
376
377 $result = $this->callAPISuccess('Contact', 'getsingle', array(
378 'id' => $contact['id'],
379 'return' => array($selectName, $multiSelectName, $controlFieldName, $radioName),
380 ));
381 // Ensure the relevant fields have been updated
382 $this->assertEquals('one-modified', $result[$selectName]);
383 $this->assertEquals(array('one-modified', $params[$radioName], $params[$controlFieldName]), $result[$multiSelectName]);
384 // This field should not have changed because we didn't alter this option
385 $this->assertEquals($params[$radioName], $result[$radioName]);
386 // This should not have changed because this field doesn't use the affected option group
387 $this->assertEquals($params[$controlFieldName], $result[$controlFieldName]);
388 }
389
390 }