Merge pull request #16469 from civicrm/5.22
[civicrm-core.git] / tests / phpunit / api / v3 / CustomValueTest.php
CommitLineData
6a488035 1<?php
7d61e75f
TO
2
3/*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
6c6e6187 11 */
6a488035 12
e9479dcf
EM
13/**
14 * Class api_v3_CustomValueTest
acb109b7 15 * @group headless
e9479dcf 16 */
6a488035 17class api_v3_CustomValueTest extends CiviUnitTestCase {
6c6e6187 18 protected $_apiversion = 3;
f9a5aadd 19
ec28b24d 20 protected $optionGroup;
b7c9bc4c 21
6a488035
TO
22 public $DBResetRequired = FALSE;
23
00be9182 24 public function setUp() {
6a488035 25 parent::setUp();
ec28b24d 26 }
27
6fca1a34 28 public function _populateOptionAndCustomGroup($type = NULL) {
9099cab3
CW
29 $dataValues = [
30 'integer' => [1, 2, 3],
31 'number' => [10.11, 20.22, 30.33],
32 'string' => [substr(sha1(rand()), 0, 4) . '(', substr(sha1(rand()), 0, 3) . '|', substr(sha1(rand()), 0, 2) . ','],
f9887f64 33 // 'country' => array_rand(CRM_Core_PseudoConstant::country(FALSE, FALSE), 3),
34 // This does not work in the test at the moment due to caching issues.
35 //'state_province' => array_rand(CRM_Core_PseudoConstant::stateProvince(FALSE, FALSE), 3),
ec28b24d 36 'date' => NULL,
a6d226f4 37 'contact' => NULL,
872a35c1 38 'boolean' => NULL,
9099cab3 39 ];
ec28b24d 40
9099cab3 41 $dataValues = !empty($type) ? [$type => $dataValues[$type]] : $dataValues;
6fca1a34 42
ec28b24d 43 foreach ($dataValues as $dataType => $values) {
9099cab3 44 $this->optionGroup[$dataType] = ['values' => $values];
ec28b24d 45 if (!empty($values)) {
ec28b24d 46 $result = $this->callAPISuccess('OptionGroup', 'create',
9099cab3 47 [
ec28b24d 48 'name' => "{$dataType}_group",
9099cab3
CW
49 'api.option_value.create' => ['label' => "$dataType 1", 'value' => $values[0]],
50 'api.option_value.create.1' => ['label' => "$dataType 2", 'value' => $values[1]],
51 'api.option_value.create.2' => ['label' => "$dataType 3", 'value' => $values[2]],
52 ]
ec28b24d 53 );
54 $this->optionGroup[$dataType]['id'] = $result['id'];
55 }
a6d226f4 56 elseif ($dataType == 'contact') {
f819588f 57 for ($i = 0; $i < 3; $i++) {
9099cab3 58 $result = $this->callAPISuccess('Contact', 'create', ['contact_type' => 'Individual', 'email' => substr(sha1(rand()), 0, 7) . '@yahoo.com']);
a6d226f4 59 $this->optionGroup[$dataType]['values'][$i] = $result['id'];
60 }
61 }
ec28b24d 62 $this->ids[$dataType] = $this->entityCustomGroupWithSingleFieldCreate("$dataType Custom Group", 'Contacts');
63 }
64
6a488035
TO
65 }
66
00be9182 67 public function tearDown() {
9099cab3 68 $tablesToTruncate = [
6a488035
TO
69 'civicrm_email',
70 'civicrm_custom_field',
71 'civicrm_custom_group',
72 'civicrm_contact',
9099cab3 73 ];
6a488035
TO
74
75 // true tells quickCleanup to drop any tables that might have been created in the test
76 $this->quickCleanup($tablesToTruncate, TRUE);
6fca1a34 77
78 // cleanup created option group for each custom-set before running next test
79 if (!empty($this->optionGroup)) {
80 foreach ($this->optionGroup as $type => $value) {
81 if (!empty($value['id'])) {
9099cab3 82 $count = $this->callAPISuccess('OptionGroup', 'get', ['id' => $value['id']]);
a60c0bc8 83 if ((bool) $count['count']) {
9099cab3 84 $this->callAPISuccess('OptionGroup', 'delete', ['id' => $value['id']]);
a60c0bc8 85 }
6fca1a34 86 }
87 }
88 }
6a488035
TO
89 }
90
91 public function testCreateCustomValue() {
6fca1a34 92 $this->_populateOptionAndCustomGroup();
9099cab3 93 $this->_customField = $this->customFieldCreate(['custom_group_id' => $this->ids['string']['custom_group_id']]);
0ec36ef1 94 $this->_customFieldID = $this->_customField['id'];
6fca1a34 95
ec28b24d 96 $customFieldDataType = CRM_Core_BAO_CustomField::dataType();
97 $dataToHtmlTypes = CRM_Core_BAO_CustomField::dataToHtml();
98 $count = 0;
9099cab3 99 $optionSupportingHTMLTypes = ['Select', 'Radio', 'CheckBox', 'Autocomplete-Select', 'Multi-Select'];
6a488035 100
ec28b24d 101 foreach ($customFieldDataType as $dataType => $label) {
ec28b24d 102 switch ($dataType) {
f9887f64 103 // case 'Country':
104 // case 'StateProvince':
ec28b24d 105 case 'String':
106 case 'Link':
107 case 'Int':
108 case 'Float':
109 case 'Money':
872a35c1 110 case 'Date':
872a35c1 111 case 'Boolean':
112
113 //Based on the custom field data-type choose desired SQL operators(to test with) and basic $type
9099cab3
CW
114 if (in_array($dataType, ['String', 'Link'])) {
115 $validSQLOperators = ['=', '!=', 'IN', 'NOT IN', 'LIKE', 'NOT LIKE', 'IS NOT NULL', 'IS NULL'];
ec28b24d 116 $type = 'string';
117 }
872a35c1 118 elseif ($dataType == 'Boolean') {
9099cab3 119 $validSQLOperators = ['=', '!=', 'IS NOT NULL', 'IS NULL'];
872a35c1 120 $type = 'boolean';
121 }
ec28b24d 122 else {
872a35c1 123 if ($dataType == 'Country') {
124 $type = 'country';
125 }
126 elseif ($dataType == 'StateProvince') {
ec28b24d 127 $type = 'state_province';
128 }
a6d226f4 129 elseif ($dataType == 'ContactReference') {
130 $type = 'contact';
131 }
132 elseif ($dataType == 'Date') {
133 $type = 'date';
134 }
ec28b24d 135 else {
a6d226f4 136 $type = $dataType == 'Int' ? 'integer' : 'number';
ec28b24d 137 }
9099cab3 138 $validSQLOperators = ['=', '!=', 'IN', 'NOT IN', '<=', '>=', '>', '<', 'IS NOT NULL', 'IS NULL'];
ec28b24d 139 }
6a488035 140
872a35c1 141 //Create custom field of $dataType and html-type $html
ec28b24d 142 foreach ($dataToHtmlTypes[$count] as $html) {
f9887f64 143 // per CRM-18568 the like operator does not currently work for fields with options.
144 // the LIKE operator could potentially bypass ACLs (as could IS NOT NULL) and some thought needs to be given
145 // to it.
146 if (in_array($html, $optionSupportingHTMLTypes)) {
9099cab3 147 $validSQLOperators = array_diff($validSQLOperators, ['LIKE', 'NOT LIKE']);
f9887f64 148 }
9099cab3 149 $params = [
ec28b24d 150 'custom_group_id' => $this->ids[$type]['custom_group_id'],
151 'label' => "$dataType - $html",
152 'data_type' => $dataType,
153 'html_type' => $html,
154 'default_value' => NULL,
9099cab3
CW
155 ];
156 if (!in_array($html, ['Text', 'TextArea']) && !in_array($dataType, ['Link', 'Date', 'ContactReference', 'Boolean'])) {
157 $params += ['option_group_id' => $this->optionGroup[$type]['id']];
ec28b24d 158 }
159 $customField = $this->customFieldCreate($params);
872a35c1 160 //Now test with $validSQLOperator SQL operators against its custom value(s)
161 $this->_testCustomValue($customField['values'][$customField['id']], $validSQLOperators, $type);
ec28b24d 162 }
163 $count++;
164 break;
6a488035 165
ec28b24d 166 default:
f9887f64 167 // skipping File data-type & state province due to caching issues
ec28b24d 168 $count++;
169 break;
170 }
171 }
172 }
6a488035 173
ec28b24d 174 public function _testCustomValue($customField, $sqlOps, $type) {
175 $isSerialized = CRM_Core_BAO_CustomField::isSerialized($customField);
176 $customId = $customField['id'];
9099cab3 177 $params = [
6a488035 178 'contact_type' => 'Individual',
ec28b24d 179 'email' => substr(sha1(rand()), 0, 7) . 'man1@yahoo.com',
9099cab3 180 ];
fc928539 181 $result = $this->callAPISuccess('Contact', 'create', $params);
ec28b24d 182 $contactId = $result['id'];
6a488035 183
8007b543 184 $count = rand(1, 2);
f9887f64 185
ec28b24d 186 if ($isSerialized) {
187 $selectedValue = $this->optionGroup[$type]['values'];
188 $notselectedValue = $selectedValue[$count];
189 unset($selectedValue[$count]);
190 }
191 elseif ($customField['html_type'] == 'Link') {
192 $selectedValue = "http://" . substr(sha1(rand()), 0, 7) . ".com";
193 $notselectedValue = "http://" . substr(sha1(rand()), 0, 7) . ".com";
194 }
195 elseif ($type == 'date') {
196 $selectedValue = date('Ymd');
a6d226f4 197 $notselectedValue = $lesserSelectedValue = date('Ymd', strtotime('yesterday'));
198 $greaterSelectedValue = date('Ymd', strtotime('+ 1 day'));
199 }
200 elseif ($type == 'contact') {
201 $selectedValue = $this->optionGroup[$type]['values'][1];
202 $notselectedValue = $this->optionGroup[$type]['values'][0];
ec28b24d 203 }
872a35c1 204 elseif ($type == 'boolean') {
205 $selectedValue = 1;
206 $notselectedValue = 0;
207 }
ec28b24d 208 else {
209 $selectedValue = $this->optionGroup[$type]['values'][0];
210 $notselectedValue = $this->optionGroup[$type]['values'][$count];
a6d226f4 211 if (in_array(">", $sqlOps)) {
212 $greaterSelectedValue = $selectedValue + 1;
213 $lesserSelectedValue = $selectedValue - 1;
214 }
ec28b24d 215 }
6a488035 216
0ec36ef1
JP
217 $params = [
218 'entity_id' => $contactId,
219 'custom_' . $customId => $selectedValue,
220 "custom_{$this->_customFieldID}" => "Test String Value for {$this->_customFieldID}",
221 ];
ec28b24d 222 $this->callAPISuccess('CustomValue', 'create', $params);
6c6e6187 223
bc3c9f57
JP
224 //Test for different return value syntax.
225 $returnValues = [
226 ['return' => "custom_{$customId}"],
227 ['return' => ["custom_{$customId}"]],
228 ["return.custom_{$customId}" => 1],
0ec36ef1
JP
229 ['return' => ["custom_{$customId}", "custom_{$this->_customFieldID}"]],
230 ["return.custom_{$customId}" => 1, "return.custom_{$this->_customFieldID}" => 1],
bc3c9f57 231 ];
0ec36ef1 232 foreach ($returnValues as $key => $val) {
bc3c9f57 233 $params = array_merge($val, [
bc3c9f57
JP
234 'entity_id' => $contactId,
235 ]);
236 $customValue = $this->callAPISuccess('CustomValue', 'get', $params);
237 if (is_array($selectedValue)) {
238 $expected = array_values($selectedValue);
0ec36ef1 239 $this->checkArrayEquals($expected, $customValue['values'][$customId]['latest']);
bc3c9f57
JP
240 }
241 elseif ($type == 'date') {
0ec36ef1 242 $this->assertEquals($selectedValue, date('Ymd', strtotime(str_replace('.', '/', $customValue['values'][$customId]['latest']))));
bc3c9f57
JP
243 }
244 else {
0ec36ef1
JP
245 $this->assertEquals($selectedValue, $customValue['values'][$customId]['latest']);
246 }
247 if ($key > 2) {
248 $this->assertEquals("Test String Value for {$this->_customFieldID}", $customValue['values'][$this->_customFieldID]['latest']);
bc3c9f57
JP
249 }
250 }
251
ec28b24d 252 foreach ($sqlOps as $op) {
a6d226f4 253 $qillOp = CRM_Utils_Array::value($op, CRM_Core_SelectValues::getSearchBuilderOperators(), $op);
ec28b24d 254 switch ($op) {
a6d226f4 255 case '=':
9099cab3 256 $result = $this->callAPISuccess('Contact', 'Get', ['custom_' . $customId => (is_array($selectedValue) ? implode(CRM_Core_DAO::VALUE_SEPARATOR, $selectedValue) : $selectedValue)]);
a6d226f4 257 $this->assertEquals($contactId, $result['id']);
a6d226f4 258 break;
842ee194 259
ec28b24d 260 case '!=':
9099cab3 261 $result = $this->callAPISuccess('Contact', 'Get', ['custom_' . $customId => [$op => $notselectedValue]]);
a4a68fa4 262 $this->assertEquals(TRUE, array_key_exists($contactId, $result['values']));
ec28b24d 263 break;
6c6e6187 264
ec28b24d 265 case '>':
266 case '<':
267 case '>=':
268 case '<=':
a6d226f4 269 if ($isSerialized) {
e255b57a 270 break;
a6d226f4 271 }
ec28b24d 272 // To be precise in for these operator we can't just rely on one contact,
273 // hence creating multiple contact with custom value less/more then $selectedValue respectively
9099cab3 274 $result = $this->callAPISuccess('Contact', 'create', ['contact_type' => 'Individual', 'email' => substr(sha1(rand()), 0, 7) . 'man2@yahoo.com']);
ec28b24d 275 $contactId2 = $result['id'];
9099cab3 276 $this->callAPISuccess('CustomValue', 'create', ['entity_id' => $contactId2, 'custom_' . $customId => $lesserSelectedValue]);
842ee194 277
ec28b24d 278 if ($op == '>') {
9099cab3 279 $result = $this->callAPISuccess('Contact', 'Get', ['custom_' . $customId => [$op => $lesserSelectedValue]]);
ec28b24d 280 $this->assertEquals($contactId, $result['id']);
281 }
282 elseif ($op == '<') {
9099cab3 283 $result = $this->callAPISuccess('Contact', 'Get', ['custom_' . $customId => [$op => $selectedValue]]);
ec28b24d 284 $this->assertEquals($contactId2, $result['id']);
285 }
286 else {
9099cab3 287 $result = $this->callAPISuccess('Contact', 'create', ['contact_type' => 'Individual', 'email' => substr(sha1(rand()), 0, 7) . 'man3@yahoo.com']);
ec28b24d 288 $contactId3 = $result['id'];
9099cab3 289 $this->callAPISuccess('CustomValue', 'create', ['entity_id' => $contactId3, 'custom_' . $customId => $greaterSelectedValue]);
842ee194 290
9099cab3 291 $result = $this->callAPISuccess('Contact', 'Get', ['custom_' . $customId => [$op => $selectedValue]]);
ec28b24d 292
293 $this->assertEquals($contactId, $result['values'][$contactId]['id']);
294 if ($op == '>=') {
295 $this->assertEquals($contactId3, $result['values'][$contactId3]['id']);
296 }
297 else {
298 $this->assertEquals($contactId2, $result['values'][$contactId2]['id']);
299 }
9099cab3 300 $this->callAPISuccess('contact', 'delete', ['id' => $contactId3]);
ec28b24d 301 }
302
9099cab3 303 $this->callAPISuccess('contact', 'delete', ['id' => $contactId2]);
ec28b24d 304 break;
305
306 case 'IN':
9099cab3 307 $result = $this->callAPISuccess('Contact', 'Get', ['custom_' . $customId => [$op => (array) $selectedValue]]);
ec28b24d 308 $this->assertEquals($contactId, $result['id']);
309 break;
310
311 case 'NOT IN':
9099cab3 312 $result = $this->callAPISuccess('Contact', 'Get', ['custom_' . $customId => [$op => (array) $notselectedValue]]);
ec28b24d 313 $this->assertEquals($contactId, $result['id']);
314 break;
315
316 case 'LIKE':
a6d226f4 317 $selectedValue = is_array($selectedValue) ? $selectedValue[0] : $selectedValue;
9099cab3 318 $result = $this->callAPISuccess('Contact', 'Get', ['custom_' . $customId => [$op => "%$selectedValue%"]]);
ec28b24d 319 $this->assertEquals($contactId, $result['id']);
320 break;
6a488035 321
ec28b24d 322 case 'NOT LIKE':
9099cab3 323 $result = $this->callAPISuccess('Contact', 'Get', ['custom_' . $customId => [$op => $notselectedValue]]);
ec28b24d 324 $this->assertEquals($contactId, $result['id']);
325 break;
6c6e6187 326
ec28b24d 327 case 'IS NULL':
9099cab3 328 $result = $this->callAPISuccess('Contact', 'Get', ['custom_' . $customId => [$op => 1]]);
ec28b24d 329 $this->assertEquals(FALSE, array_key_exists($contactId, $result['values']));
330 break;
842ee194 331
ec28b24d 332 case 'IS NOT NULL':
9099cab3 333 $result = $this->callAPISuccess('Contact', 'Get', ['custom_' . $customId => [$op => 1]]);
ec28b24d 334 $this->assertEquals($contactId, $result['id']);
335 break;
336 }
337 }
6c6e6187 338
9099cab3 339 $this->callAPISuccess('Contact', 'delete', ['id' => $contactId]);
842ee194 340 }
96025800 341
58eaa092
CW
342 /**
343 * Ensure custom data is updated when option values are modified
344 *
345 * @link https://issues.civicrm.org/jira/browse/CRM-11856
346 *
347 * @throws \CiviCRM_API3_Exception
348 */
349 public function testAlterOptionValue() {
6fca1a34 350 $this->_populateOptionAndCustomGroup('string');
351
9099cab3 352 $selectField = $this->customFieldCreate([
6fca1a34 353 'custom_group_id' => $this->ids['string']['custom_group_id'],
58eaa092
CW
354 'label' => 'Custom Select',
355 'html_type' => 'Select',
6fca1a34 356 'option_group_id' => $this->optionGroup['string']['id'],
9099cab3
CW
357 ]);
358 $selectField = civicrm_api3('customField', 'getsingle', ['id' => $selectField['id']]);
359 $radioField = $this->customFieldCreate([
6fca1a34 360 'custom_group_id' => $this->ids['string']['custom_group_id'],
58eaa092
CW
361 'label' => 'Custom Radio',
362 'html_type' => 'Radio',
363 'option_group_id' => $selectField['option_group_id'],
9099cab3
CW
364 ]);
365 $multiSelectField = $this->customFieldCreate([
6fca1a34 366 'custom_group_id' => $this->ids['string']['custom_group_id'],
58eaa092
CW
367 'label' => 'Custom Multi-Select',
368 'html_type' => 'Multi-Select',
369 'option_group_id' => $selectField['option_group_id'],
9099cab3 370 ]);
58eaa092
CW
371 $selectName = 'custom_' . $selectField['id'];
372 $radioName = 'custom_' . $radioField['id'];
373 $multiSelectName = 'custom_' . $multiSelectField['id'];
6fca1a34 374 $controlFieldName = 'custom_' . $this->ids['string']['custom_field_id'];
58eaa092 375
9099cab3 376 $params = [
58eaa092
CW
377 'first_name' => 'abc4',
378 'last_name' => 'xyz4',
379 'contact_type' => 'Individual',
380 'email' => 'man4@yahoo.com',
6fca1a34 381 $selectName => $this->optionGroup['string']['values'][0],
382 $multiSelectName => $this->optionGroup['string']['values'],
383 $radioName => $this->optionGroup['string']['values'][1],
58eaa092 384 // The control group in a science experiment should be unaffected
6fca1a34 385 $controlFieldName => $this->optionGroup['string']['values'][2],
9099cab3 386 ];
58eaa092
CW
387
388 $contact = $this->callAPISuccess('Contact', 'create', $params);
389
9099cab3 390 $result = $this->callAPISuccess('Contact', 'getsingle', [
58eaa092 391 'id' => $contact['id'],
9099cab3
CW
392 'return' => [$selectName, $multiSelectName],
393 ]);
6fca1a34 394 $this->assertEquals($params[$selectName], $result[$selectName]);
395 $this->assertEquals($params[$multiSelectName], $result[$multiSelectName]);
58eaa092 396
9099cab3 397 $this->callAPISuccess('OptionValue', 'create', [
58eaa092
CW
398 'value' => 'one-modified',
399 'option_group_id' => $selectField['option_group_id'],
6fca1a34 400 'name' => 'string 1',
9099cab3
CW
401 'options' => [
402 'match-mandatory' => ['option_group_id', 'name'],
403 ],
404 ]);
58eaa092 405
9099cab3 406 $result = $this->callAPISuccess('Contact', 'getsingle', [
58eaa092 407 'id' => $contact['id'],
9099cab3
CW
408 'return' => [$selectName, $multiSelectName, $controlFieldName, $radioName],
409 ]);
58eaa092
CW
410 // Ensure the relevant fields have been updated
411 $this->assertEquals('one-modified', $result[$selectName]);
9099cab3 412 $this->assertEquals(['one-modified', $params[$radioName], $params[$controlFieldName]], $result[$multiSelectName]);
58eaa092 413 // This field should not have changed because we didn't alter this option
6fca1a34 414 $this->assertEquals($params[$radioName], $result[$radioName]);
58eaa092 415 // This should not have changed because this field doesn't use the affected option group
6fca1a34 416 $this->assertEquals($params[$controlFieldName], $result[$controlFieldName]);
f36d17d2 417 // Add test of proof that multivalue fields.
9099cab3 418 $this->callAPISuccess('CustomValue', 'create', [
f36d17d2 419 'entity_id' => $contact['id'],
9099cab3
CW
420 $multiSelectName => [$params[$radioName], $params[$controlFieldName]],
421 ]);
422 $result = $this->callAPISuccess('Contact', 'getsingle', [
f36d17d2 423 'id' => $contact['id'],
9099cab3
CW
424 'return' => [$selectName, $multiSelectName, $controlFieldName, $radioName],
425 ]);
f36d17d2 426
9099cab3 427 $this->assertEquals([$params[$radioName], $params[$controlFieldName]], $result[$multiSelectName]);
58eaa092
CW
428 }
429
24871985 430 public function testGettree() {
9099cab3 431 $cg = $this->callAPISuccess('CustomGroup', 'create', [
24871985
CW
432 'title' => 'TestGettree',
433 'extends' => 'Individual',
9099cab3
CW
434 ]);
435 $cf = $this->callAPISuccess('CustomField', 'create', [
24871985
CW
436 'custom_group_id' => $cg['id'],
437 'label' => 'Got Options',
438 'name' => 'got_options',
439 "data_type" => "String",
440 "html_type" => "Multi-Select",
9099cab3
CW
441 'option_values' => ['1' => 'One', '2' => 'Two', '3' => 'Three'],
442 ]);
24871985 443 $fieldName = 'custom_' . $cf['id'];
9099cab3 444 $contact = $this->individualCreate([$fieldName => ['2', '3']]);
0b330e6d
CW
445
446 // Verify values are formatted correctly
9099cab3
CW
447 $tree = $this->callAPISuccess('CustomValue', 'gettree', ['entity_type' => 'Contact', 'entity_id' => $contact]);
448 $this->assertEquals(['2', '3'], $tree['values']['TestGettree']['fields']['got_options']['value']['data']);
24871985 449 $this->assertEquals('Two, Three', $tree['values']['TestGettree']['fields']['got_options']['value']['display']);
0b330e6d
CW
450
451 // Try limiting the return params
9099cab3 452 $tree = $this->callAPISuccess('CustomValue', 'gettree', [
0b330e6d
CW
453 'entity_type' => 'Contact',
454 'entity_id' => $contact,
9099cab3 455 'return' => [
0b330e6d
CW
456 'custom_group.id',
457 'custom_field.id',
9099cab3
CW
458 ],
459 ]);
460 $this->assertEquals(['2', '3'], $tree['values']['TestGettree']['fields']['got_options']['value']['data']);
e6446db2 461 $this->assertEquals('Two, Three', $tree['values']['TestGettree']['fields']['got_options']['value']['display']);
9099cab3 462 $this->assertEquals(['id', 'fields'], array_keys($tree['values']['TestGettree']));
0b330e6d 463
e6446db2 464 // Ensure display values are returned even if data is not
9099cab3 465 $tree = $this->callAPISuccess('CustomValue', 'gettree', [
e6446db2
CW
466 'entity_type' => 'Contact',
467 'entity_id' => $contact,
9099cab3 468 'return' => [
e6446db2 469 'custom_value.display',
9099cab3
CW
470 ],
471 ]);
e6446db2
CW
472 $this->assertEquals('Two, Three', $tree['values']['TestGettree']['fields']['got_options']['value']['display']);
473 $this->assertFalse(isset($tree['values']['TestGettree']['fields']['got_options']['value']['data']));
474
0b330e6d
CW
475 // Verify that custom set appears for individuals even who don't have any custom data
476 $contact2 = $this->individualCreate();
9099cab3 477 $tree = $this->callAPISuccess('CustomValue', 'gettree', ['entity_type' => 'Contact', 'entity_id' => $contact2]);
0b330e6d
CW
478 $this->assertArrayHasKey('TestGettree', $tree['values']);
479
480 // Verify that custom set doesn't appear for other contact types
481 $org = $this->organizationCreate();
9099cab3 482 $tree = $this->callAPISuccess('CustomValue', 'gettree', ['entity_type' => 'Contact', 'entity_id' => $org]);
0b330e6d
CW
483 $this->assertArrayNotHasKey('TestGettree', $tree['values']);
484
485 }
486
487 public function testGettree_getfields() {
9099cab3 488 $fields = $this->callAPISuccess('CustomValue', 'getfields', ['api_action' => 'gettree']);
0b330e6d
CW
489 $fields = $fields['values'];
490 $this->assertTrue((bool) $fields['entity_id']['api.required']);
491 $this->assertTrue((bool) $fields['entity_type']['api.required']);
492 $this->assertEquals('custom_group.id', $fields['custom_group.id']['name']);
493 $this->assertEquals('custom_field.id', $fields['custom_field.id']['name']);
494 $this->assertEquals('custom_value.id', $fields['custom_value.id']['name']);
24871985
CW
495 }
496
63b7d442
AS
497 /**
498 * Test that custom fields in greeting strings are updated.
499 */
500 public function testUpdateCustomGreetings() {
501 // Create a custom group with one field.
9099cab3 502 $customGroupResult = $this->callAPISuccess('CustomGroup', 'create', [
63b7d442
AS
503 'sequential' => 1,
504 'title' => "test custom group",
505 'extends' => "Individual",
9099cab3
CW
506 ]);
507 $customFieldResult = $this->callAPISuccess('CustomField', 'create', [
63b7d442
AS
508 'custom_group_id' => $customGroupResult['id'],
509 'label' => "greeting test",
510 'data_type' => "String",
511 'html_type' => "Text",
9099cab3 512 ]);
63b7d442
AS
513 $customFieldId = $customFieldResult['id'];
514
515 // Create a contact with an email greeting format that includes the new custom field.
9099cab3 516 $contactResult = $this->callAPISuccess('Contact', 'create', [
63b7d442
AS
517 'contact_type' => 'Individual',
518 'email' => substr(sha1(rand()), 0, 7) . '@yahoo.com',
519 'email_greeting_id' => "Customized",
520 'email_greeting_custom' => "Dear {contact.custom_{$customFieldId}}",
9099cab3 521 ]);
63b7d442
AS
522 $cid = $contactResult['id'];
523
524 // Define testing values.
525 $uniq = uniqid();
526 $testGreetingValue = "Dear $uniq";
527
528 // Update contact's custom field with CustomValue.create
9099cab3 529 $customValueResult = $this->callAPISuccess('CustomValue', 'create', [
63b7d442
AS
530 'entity_id' => $cid,
531 "custom_{$customFieldId}" => $uniq,
532 'entity_table' => "civicrm_contact",
9099cab3 533 ]);
63b7d442 534
9099cab3 535 $contact = $this->callAPISuccessGetSingle('Contact', ['id' => $cid, 'return' => 'email_greeting']);
63b7d442
AS
536 $this->assertEquals($testGreetingValue, $contact['email_greeting_display']);
537
538 }
539
5498def2
T
540 /**
541 * Creates a multi-valued custom field set and creates a contact with mutliple values for it.
542 *
543 * @return array
544 */
545 private function _testGetCustomValueMultiple() {
546 $fieldIDs = $this->CustomGroupMultipleCreateWithFields();
547 $customFieldValues = [];
548 foreach ($fieldIDs['custom_field_id'] as $id) {
549 $customFieldValues["custom_{$id}"] = "field_{$id}_value_1";
550 }
551 $this->assertNotEmpty($customFieldValues);
552 $contactParams = [
553 'first_name' => 'Jane',
554 'last_name' => 'Doe',
555 'contact_type' => 'Individual',
556 ];
557 $contact = $this->callAPISuccess('Contact', 'create', array_merge($contactParams, $customFieldValues));
558 foreach ($fieldIDs['custom_field_id'] as $id) {
559 $customFieldValues["custom_{$id}"] = "field_{$id}_value_2";
560 }
561 $result = $this->callAPISuccess('Contact', 'create', array_merge(['id' => $contact['id']], $customFieldValues));
562 return [
563 $contact['id'],
564 $customFieldValues,
565 ];
566 }
567
568 /**
569 * Test that specific custom values can be retrieved while using return with comma separated values as genererated by the api explorer.
570 * ['return' => 'custom_1,custom_2']
571 */
572 public function testGetCustomValueReturnMultipleApiExplorer() {
573 list($cid, $customFieldValues) = $this->_testGetCustomValueMultiple();
574 $result = $this->callAPISuccess('CustomValue', 'get', [
575 'return' => implode(',', array_keys($customFieldValues)),
576 'entity_id' => $cid,
577 ]);
578 $this->assertEquals(count($customFieldValues), $result['count']);
579 }
580
581 /**
582 * Test that specific custom values can be retrieved while using return with array style syntax.
583 * ['return => ['custom_1', 'custom_2']]
584 */
585 public function testGetCustomValueReturnMultipleArray() {
586 list($cid, $customFieldValues) = $this->_testGetCustomValueMultiple();
587 $result = $this->callAPISuccess('CustomValue', 'get', [
588 'return' => array_keys($customFieldValues),
589 'entity_id' => $cid,
590 ]);
591 $this->assertEquals(count($customFieldValues), $result['count']);
592 }
593
594 /**
595 * Test that specific custom values can be retrieved while using a list of return parameters.
596 * [['return.custom_1' => '1'], ['return.custom_2' => '1']]
597 */
598 public function testGetCustomValueReturnMultipleList() {
599 list($cid, $customFieldValues) = $this->_testGetCustomValueMultiple();
600 $returnArray = [];
601 foreach ($customFieldValues as $field => $value) {
602 $returnArray["return.{$field}"] = 1;
603 }
604 $result = $this->callAPISuccess('CustomValue', 'get', array_merge($returnArray, ['entity_id' => $cid]));
605 $this->assertEquals(count($customFieldValues), $result['count']);
606 }
607
afbd0c22
JP
608 /**
609 * Test getdisplayvalue api and verify if it returns
610 * the custom text for display.
611 */
612 public function testGetDisplayValue() {
613 list($cid, $customFieldValues) = $this->_testGetCustomValueMultiple();
614 foreach ($customFieldValues as $field => $value) {
615 list(, $customFieldID) = explode("_", $field);
616 $result = $this->callAPISuccess('CustomValue', 'getdisplayvalue', [
617 'entity_id' => $cid,
618 'custom_field_id' => $customFieldID,
619 ]);
620 $expectedValue = [
621 'display' => $value,
622 'raw' => $value,
623 ];
624 $this->checkArrayEquals($result['values'][$customFieldID], $expectedValue);
625
626 $customDisplayValue = $this->callAPISuccess('CustomValue', 'getdisplayvalue', [
627 'entity_id' => $cid,
628 'custom_field_id' => $customFieldID,
629 'custom_field_value' => "Test Custom Display - {$value}",
630 ]);
631 $expectedValue = [
632 'display' => "Test Custom Display - {$value}",
633 'raw' => "Test Custom Display - {$value}",
634 ];
635 $this->checkArrayEquals($customDisplayValue['values'][$customFieldID], $expectedValue);
636 }
637 }
638
ef10e0b5 639}