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