5 use Civi\API\Exception\NotImplementedException
;
7 require_once 'api/v3/utils.php';
12 * This trait defines a number of helper functions for testing APIv3. Commonly
13 * used helpers include `callAPISuccess()`, `callAPIFailure()`,
14 * `assertAPISuccess()`, and `assertAPIFailure()`.
16 * This trait is intended for use with PHPUnit-based test cases.
21 * Get the api versions to test.
25 public function versionThreeAndFour() {
30 * Api version - easier to override than just a define
33 protected $_apiversion = 3;
36 * Check that api returned 'is_error' => 1
37 * else provide full message
38 * @param array $result
40 * @param array $valuesToExclude
41 * @param string $prefix
42 * Extra test to add to message.
44 public function assertAPIArrayComparison($result, $expected, $valuesToExclude = [], $prefix = '') {
45 $valuesToExclude = array_merge($valuesToExclude, ['debug', 'xdebug', 'sequential']);
46 foreach ($valuesToExclude as $value) {
47 if (isset($result[$value])) {
48 unset($result[$value]);
50 if (isset($expected[$value])) {
51 unset($expected[$value]);
54 $this->assertEquals($result, $expected, "api result array comparison failed " . $prefix . print_r($result, TRUE) . ' was compared to ' . print_r($expected, TRUE));
58 * Check that a deleted item has been deleted.
63 public function assertAPIDeleted($entity, $id) {
64 $this->callAPISuccess($entity, 'getcount', ['id' => $id], 0);
68 * Check that api returned 'is_error' => 1.
70 * @param array $apiResult
72 * @param string $prefix
73 * Extra test to add to message.
74 * @param null $expectedError
76 public function assertAPIFailure($apiResult, $prefix = '', $expectedError = NULL) {
77 if (!empty($prefix)) {
80 if ($expectedError && !empty($apiResult['is_error'])) {
81 $this->assertContains($expectedError, $apiResult['error_message'], 'api error message not as expected' . $prefix);
83 $this->assertEquals(1, $apiResult['is_error'], "api call should have failed but it succeeded " . $prefix . (print_r($apiResult, TRUE)));
84 $this->assertNotEmpty($apiResult['error_message']);
88 * Check that api returned 'is_error' => 0.
90 * @param array $apiResult
92 * @param string $prefix
93 * Extra test to add to message.
95 public function assertAPISuccess($apiResult, $prefix = '') {
96 if (!empty($prefix)) {
99 $errorMessage = empty($apiResult['error_message']) ?
'' : " " . $apiResult['error_message'];
101 if (!empty($apiResult['debug_information'])) {
102 $errorMessage .= "\n " . print_r($apiResult['debug_information'], TRUE);
104 if (!empty($apiResult['trace'])) {
105 $errorMessage .= "\n" . print_r($apiResult['trace'], TRUE);
107 $this->assertEmpty($apiResult['is_error'] ??
NULL, $prefix . $errorMessage);
111 * This function exists to wrap api functions.
112 * so we can ensure they fail where expected & throw exceptions without litterering the test with checks
113 * @param string $entity
114 * @param string $action
115 * @param array $params
116 * @param string $expectedErrorMessage
118 * @param null $extraOutput
121 public function callAPIFailure($entity, $action, $params, $expectedErrorMessage = NULL, $extraOutput = NULL) {
122 if (is_array($params)) {
124 'version' => $this->_apiversion
,
127 $result = $this->civicrm_api($entity, $action, $params);
128 $this->assertAPIFailure($result, "We expected a failure for $entity $action but got a success", $expectedErrorMessage);
133 * wrap api functions.
134 * so we can ensure they succeed & throw exceptions without litterering the test with checks
136 * @param string $entity
137 * @param string $action
138 * @param array $params
139 * @param mixed $checkAgainst
140 * Optional value to check result against, implemented for getvalue,.
141 * getcount, getsingle. Note that for getvalue the type is checked rather than the value
142 * for getsingle the array is compared against an array passed in - the id is not compared (for
147 * @throws \CRM_Core_Exception
149 public function callAPISuccess($entity, $action, $params = [], $checkAgainst = NULL) {
150 $params = array_merge([
151 'version' => $this->_apiversion
,
156 switch (strtolower($action)) {
158 return $this->callAPISuccessGetValue($entity, $params, $checkAgainst);
161 return $this->callAPISuccessGetSingle($entity, $params, $checkAgainst);
164 return $this->callAPISuccessGetCount($entity, $params, $checkAgainst);
166 $result = $this->civicrm_api($entity, $action, $params);
167 $this->assertAPISuccess($result, "Failure in api call for $entity $action");
172 * This function exists to wrap api getValue function & check the result
173 * so we can ensure they succeed & throw exceptions without litterering the test with checks
174 * There is a type check in this
176 * @param string $entity
177 * @param array $params
180 * @throws \CRM_Core_Exception
184 public function callAPISuccessGetCount($entity, $params, $count = NULL) {
186 'version' => $this->_apiversion
,
189 $result = $this->civicrm_api($entity, 'getcount', $params);
190 if (!is_int($result) ||
!empty($result['is_error']) ||
isset($result['values'])) {
191 throw new \
CRM_Core_Exception('Invalid getcount result : ' . print_r($result, TRUE) . " type :" . gettype($result));
193 if (is_int($count)) {
194 $this->assertEquals($count, $result, "incorrect count returned from $entity getcount");
200 * This function exists to wrap api getsingle function & check the result
201 * so we can ensure they succeed & throw exceptions without litterering the test with checks
203 * @param string $entity
204 * @param array $params
205 * @param array $checkAgainst
206 * Array to compare result against.
214 * @throws \CRM_Core_Exception
218 public function callAPISuccessGetSingle($entity, $params, $checkAgainst = NULL) {
220 'version' => $this->_apiversion
,
222 $result = $this->civicrm_api($entity, 'getsingle', $params);
223 if (!is_array($result) ||
!empty($result['is_error']) ||
isset($result['values'])) {
224 $unfilteredResult = $this->civicrm_api($entity, 'get', ['version' => $this->_apiversion
]);
225 throw new \
CRM_Core_Exception(
226 'Invalid getsingle result' . print_r($result, TRUE)
227 . "\n entity: $entity . \n params \n " . print_r($params, TRUE)
228 . "\n entities retrieved with blank params \n" . print_r($unfilteredResult, TRUE)
232 // @todo - have gone with the fn that unsets id? should we check id?
233 $this->checkArrayEquals($result, $checkAgainst);
239 * This function exists to wrap api getValue function & check the result
240 * so we can ensure they succeed & throw exceptions without litterering the test with checks
241 * There is a type check in this
243 * @param string $entity
244 * @param array $params
245 * @param string $type
246 * Per http://php.net/manual/en/function.gettype.php possible types.
255 * @throws \CRM_Core_Exception
257 public function callAPISuccessGetValue($entity, $params, $type = NULL) {
259 'version' => $this->_apiversion
,
262 $result = $this->civicrm_api($entity, 'getvalue', $params);
263 if (is_array($result) && (!empty($result['is_error']) ||
isset($result['values']))) {
264 throw new \
CRM_Core_Exception('Invalid getvalue result' . print_r($result, TRUE));
267 if ($type === 'integer') {
268 // api seems to return integers as strings
269 $this->assertTrue(is_numeric($result), "expected a numeric value but got " . print_r($result, 1));
272 $this->assertType($type, $result, "returned result should have been of type $type but was ");
279 * A stub for the API interface. This can be overriden by subclasses to change how the API is called.
283 * @param array $params
286 public function civicrm_api($entity, $action, $params = []) {
287 if (($params['version'] ??
0) == 4) {
288 return $this->runApi4Legacy($entity, $action, $params);
290 return civicrm_api($entity, $action, $params);
294 * Emulate v3 syntax so we can run api3 tests on v4
298 * @param array $v3Params
300 * @throws \API_Exception
301 * @throws \CiviCRM_API3_Exception
304 public function runApi4Legacy($v3Entity, $v3Action, $v3Params = []) {
305 $v4Entity = self
::convertEntityNameToApi4($v3Entity);
306 $v4Action = $v3Action = strtolower($v3Action);
307 $v4Params = ['checkPermissions' => isset($v3Params['check_permissions']) ?
(bool) $v3Params['check_permissions'] : FALSE];
308 $sequential = !empty($v3Params['sequential']);
309 $options = \
_civicrm_api3_get_options_from_params($v3Params, in_array($v4Entity, ['Contact', 'Participant', 'Event', 'Group', 'Contribution', 'Membership']));
310 $indexBy = in_array($v3Action, ['get', 'create', 'replace']) && !$sequential ?
'id' : NULL;
311 $onlyId = !empty($v3Params['format.only_id']);
312 $onlySuccess = !empty($v3Params['format.is_success']);
313 if (!empty($v3Params['filters']['is_current']) ||
!empty($v3Params['isCurrent'])) {
314 $v4Params['current'] = TRUE;
316 $language = $v3Params['options']['language'] ??
$v3Params['option.language'] ??
NULL;
318 $v4Params['language'] = $language;
320 $toRemove = ['option.', 'return', 'api.', 'format.'];
321 $chains = $joins = $custom = [];
322 foreach ($v3Params as $key => $val) {
323 foreach ($toRemove as $remove) {
324 if (strpos($key, $remove) === 0) {
325 if ($remove == 'api.') {
326 $chains[$key] = $val;
328 unset($v3Params[$key]);
333 $v3Fields = civicrm_api3($v3Entity, 'getfields', ['action' => $v3Action])['values'];
336 foreach ($v3Params as $key => $val) {
337 if ($val === 'null') {
338 $v3Params[$key] = NULL;
342 if ($v4Entity == 'Setting') {
344 $v4Params['domainId'] = $v3Params['domain_id'] ??
NULL;
345 if ($v3Action == 'getfields') {
346 if (!empty($v3Params['name'])) {
347 $v3Params['filters']['name'] = $v3Params['name'];
349 foreach ($v3Params['filters'] ??
[] as $filter => $val) {
350 $v4Params['where'][] = [$filter, '=', $val];
353 if ($v3Action == 'create') {
356 if ($v3Action == 'revert') {
357 $v4Params['select'] = (array) $v3Params['name'];
359 if ($v3Action == 'getvalue') {
360 $options['return'] = [$v3Params['name'] => 1];
363 \CRM_Utils_Array
::remove($v3Params, 'domain_id', 'name');
366 \CRM_Utils_Array
::remove($v3Params, 'options', 'debug', 'version', 'sort', 'offset', 'rowCount', 'check_permissions', 'sequential', 'filters', 'isCurrent');
368 // Work around ugly hack in v3 Domain api
369 if ($v4Entity == 'Domain') {
370 $v3Fields['version'] = ['name' => 'version', 'api.aliases' => ['domain_version']];
371 unset($v3Fields['domain_version']);
374 foreach ($v3Fields as $name => $field) {
375 // Resolve v3 aliases
376 foreach ($field['api.aliases'] ??
[] as $alias) {
377 if (isset($v3Params[$alias])) {
378 $v3Params[$field['name']] = $v3Params[$alias];
379 unset($v3Params[$alias]);
382 // Convert custom field names
383 if (strpos($name, 'custom_') === 0 && is_numeric($name[7])) {
384 // Strictly speaking, using titles instead of names is incorrect, but it works for
385 // unit tests where names and titles are identical and saves an extra db lookup.
386 $custom[$field['groupTitle']][$field['title']] = $name;
387 $v4FieldName = $field['groupTitle'] . '.' . $field['title'];
388 if (isset($v3Params[$name])) {
389 $v3Params[$v4FieldName] = $v3Params[$name];
390 unset($v3Params[$name]);
392 if (isset($options['return'][$name])) {
393 $options['return'][$v4FieldName] = 1;
394 unset($options['return'][$name]);
398 if ($name === 'option_group_id' && isset($v3Params[$name]) && !is_numeric($v3Params[$name])) {
399 // This is a per field hack (bad) but we can't solve everything at once
400 // & a cleverer way turned out to be too much for this round.
401 // Being in the test class it's tested....
402 $v3Params['option_group.name'] = $v3Params['option_group_id'];
403 unset($v3Params['option_group_id']);
409 $v4Params['select'] = ['row_count'];
410 // No break - keep processing as get
414 // No break - keep processing as get
416 if ($options['return'] && $v3Action !== 'getcount') {
417 $v4Params['select'] = array_keys($options['return']);
418 // Ensure id field is returned as v3 always expects it
419 if ($v4Entity != 'Setting' && !in_array('id', $v4Params['select'])) {
420 $v4Params['select'][] = 'id';
422 // Convert join syntax
423 foreach ($v4Params['select'] as &$select) {
424 if (strstr($select, '_id.')) {
425 $joins[$select] = explode('.', str_replace('_id.', '.', $select));
426 $select = str_replace('_id.', '.', $select);
430 if ($options['limit'] && $v4Entity != 'Setting') {
431 $v4Params['limit'] = $options['limit'];
433 if ($options['offset']) {
434 $v4Params['offset'] = $options['offset'];
436 if ($options['sort']) {
437 foreach (explode(',', $options['sort']) as $sort) {
438 list($sortField, $sortDir) = array_pad(explode(' ', trim($sort)), 2, 'ASC');
439 $v4Params['orderBy'][$sortField] = $sortDir;
445 if (empty($v3Params['values'])) {
446 $v4Action = 'delete';
449 $v4Params['records'] = $v3Params['values'];
451 unset($v3Params['values']);
456 if (!empty($v3Params['id'])) {
457 $v4Action = 'update';
458 $v4Params['where'][] = ['id', '=', $v3Params['id']];
461 $v4Params['values'] = $v3Params;
462 unset($v4Params['values']['id']);
466 if (isset($v3Params['id'])) {
467 $v4Params['where'][] = ['id', '=', $v3Params['id']];
473 $v4Action = 'getFields';
475 'where' => [['name', '=', $v3Params['field']]],
476 'loadOptions' => TRUE,
481 $v4Action = 'getFields';
482 if (!empty($v3Params['action']) ||
!empty($v3Params['api_action'])) {
483 $v4Params['action'] = !empty($v3Params['action']) ?
$v3Params['action'] : $v3Params['api_action'];
485 $indexBy = !$sequential ?
'name' : NULL;
489 // Ensure this api4 entity/action exists
491 $actionInfo = \
civicrm_api4($v4Entity, 'getActions', ['checkPermissions' => FALSE, 'where' => [['name', '=', $v4Action]]]);
493 catch (NotImplementedException
$e) {
494 // For now we'll mark the test incomplete if a v4 entity doesn't exit yet
495 $this->markTestIncomplete($e->getMessage());
497 if (!isset($actionInfo[0])) {
498 throw new \
Exception("Api4 $v4Entity $v4Action does not exist.");
501 // Migrate special params like fix_address
502 foreach ($actionInfo[0]['params'] as $v4ParamName => $paramInfo) {
503 // camelCase in api4, lower_case in api3
504 $v3ParamName = strtolower(preg_replace('/(?=[A-Z])/', '_$0', $v4ParamName));
505 if (isset($v3Params[$v3ParamName])) {
506 $v4Params[$v4ParamName] = $v3Params[$v3ParamName];
507 unset($v3Params[$v3ParamName]);
508 if ($paramInfo['type'][0] == 'bool') {
509 $v4Params[$v4ParamName] = (bool) $v4Params[$v4ParamName];
514 // Build where clause for 'getcount', 'getsingle', 'getvalue', 'get' & 'replace'
515 if ($v4Action == 'get' ||
$v3Action == 'replace') {
516 foreach ($v3Params as $key => $val) {
518 if (is_array($val) && count($val) == 1 && array_intersect_key($val, array_flip(\CRM_Core_DAO
::acceptedSQLOperators()))) {
519 foreach ($val as $op => $newVal) {
523 $v4Params['where'][] = [$key, $op, $val];
528 $result = \
civicrm_api4($v4Entity, $v4Action, $v4Params, $indexBy);
530 catch (\Exception
$e) {
531 return $onlySuccess ?
0 : [
533 'error_message' => $e->getMessage(),
538 if (($v3Action == 'getsingle' ||
$v3Action == 'getvalue' ||
$v3Action == 'delete') && count($result) != 1) {
539 return $onlySuccess ?
0 : [
541 'error_message' => "Expected one $v4Entity but found " . count($result),
542 'count' => count($result),
550 if ($v3Action == 'getcount') {
551 return $result->count();
555 return $result->first()['id'];
558 if ($v3Action == 'getvalue' && $v4Entity == 'Setting') {
559 return $result->first()['value'] ??
NULL;
562 if ($v3Action == 'getvalue') {
563 $returnKey = array_keys($options['return'])[0];
564 return $result->first()[$returnKey] ??
NULL;
567 // Mimic api3 behavior when using 'replace' action to delete all
568 if ($v3Action == 'replace' && $v4Action == 'delete') {
569 $result->exchangeArray([]);
572 if ($v3Action == 'getoptions') {
575 'count' => $result['options'] ?
count($result['options']) : 0,
576 'values' => $result['options'] ?
: [],
581 // Emulate the weird return format of api3 settings
582 if (($v3Action == 'get' ||
$v3Action == 'create') && $v4Entity == 'Setting') {
584 foreach ($result as $item) {
585 $settings[$item['domain_id']][$item['name']] = $item['value'];
587 $result->exchangeArray($sequential ?
array_values($settings) : $settings);
590 foreach ($result as $index => $row) {
592 foreach ($chains as $key => $params) {
593 $result[$index][$key] = $this->runApi4LegacyChain($key, $params, $v4Entity, $row, $sequential);
595 // Convert join format
596 foreach ($joins as $api3Key => $api4Path) {
597 $result[$index][$api3Key] = \CRM_Utils_Array
::pathGet($result[$index], $api4Path);
599 // Resolve custom field names
600 foreach ($custom as $group => $fields) {
601 foreach ($fields as $field => $v3FieldName) {
602 if (isset($row["$group.$field"])) {
603 $result[$index][$v3FieldName] = $row["$group.$field"];
604 unset($result[$index]["$group.$field"]);
610 if ($v3Action == 'getsingle') {
611 return $result->first();
617 'count' => count($result),
618 'values' => (array) $result,
619 'id' => is_object($result) && count($result) == 1 ?
($result->first()['id'] ??
NULL) : NULL,
625 * @param mixed $params
626 * @param string $mainEntity
627 * @param array $result
628 * @param bool $sequential
630 * @throws \API_Exception
632 protected function runApi4LegacyChain($key, $params, $mainEntity, $result, $sequential) {
633 // Handle an array of multiple calls using recursion
634 if (is_array($params) && isset($params[0]) && is_array($params[0])) {
636 foreach ($params as $chain) {
637 $results[] = $this->runApi4LegacyChain($key, $chain, $mainEntity, $result, $sequential);
642 // Handle single api call
643 list(, $chainEntity, $chainAction) = explode('.', $key);
644 $lcChainEntity = \
_civicrm_api_get_entity_name_from_camel($chainEntity);
645 $chainEntity = self
::convertEntityNameToApi4($chainEntity);
646 $lcMainEntity = \
_civicrm_api_get_entity_name_from_camel($mainEntity);
647 $params = is_array($params) ?
$params : [];
649 // Api3 expects this to be inherited
650 $params +
= ['sequential' => $sequential];
652 // Replace $value.field_name
653 foreach ($params as $name => $param) {
654 if (is_string($param) && strpos($param, '$value.') === 0) {
655 $param = substr($param, 7);
656 $params[$name] = $result[$param] ??
NULL;
661 $getFields = civicrm_api4($chainEntity, 'getFields', ['select' => ['name']], 'name');
663 catch (NotImplementedException
$e) {
664 $this->markTestIncomplete($e->getMessage());
667 // Emulate the string-fu guesswork that api3 does
668 if ($chainEntity == $mainEntity && empty($params['id']) && !empty($result['id'])) {
669 $params['id'] = $result['id'];
671 elseif (empty($params['id']) && !empty($result[$lcChainEntity . '_id'])) {
672 $params['id'] = $result[$lcChainEntity . '_id'];
674 elseif (!empty($result['id']) && isset($getFields[$lcMainEntity . '_id']) && empty($params[$lcMainEntity . '_id'])) {
675 $params[$lcMainEntity . '_id'] = $result['id'];
677 return $this->runApi4Legacy($chainEntity, $chainAction, $params);
681 * Fix the naming differences between api3 & api4 entities.
683 * @param string $legacyName
686 public static function convertEntityNameToApi4($legacyName) {
687 $api4Name = \CRM_Utils_String
::convertStringToCamel($legacyName);
692 return $map[$api4Name] ??
$api4Name;