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