From 07d6d25b6186d848f6513f0bc3543809db23cf44 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Mon, 27 Jan 2020 19:47:21 -0500 Subject: [PATCH] APIv4 - Accept multiple fieldNames in Get helper fn --- Civi/Api4/Action/Entity/Get.php | 3 ++- Civi/Api4/Action/GetActions.php | 2 +- Civi/Api4/Generic/AbstractGetAction.php | 25 +++++++++++-------- .../api/v4/Action/BasicActionsTest.php | 10 +++----- 4 files changed, 22 insertions(+), 18 deletions(-) diff --git a/Civi/Api4/Action/Entity/Get.php b/Civi/Api4/Action/Entity/Get.php index 0e7d5e548f..15a53dab58 100644 --- a/Civi/Api4/Action/Entity/Get.php +++ b/Civi/Api4/Action/Entity/Get.php @@ -45,6 +45,7 @@ class Get extends \Civi\Api4\Generic\BasicGetAction { protected function getRecords() { $entities = []; $toGet = $this->_itemsToGet('name'); + $getDocs = $this->_isFieldSelected('description', 'comment', 'see'); $locations = array_merge([\Civi::paths()->getPath('[civicrm.root]/Civi.php')], array_column(\CRM_Extension_System::singleton()->getMapper()->getActiveModuleFiles(), 'filePath') ); @@ -59,7 +60,7 @@ class Get extends \Civi\Api4\Generic\BasicGetAction { && is_a('\Civi\Api4\\' . $matches[1], '\Civi\Api4\Generic\AbstractEntity', TRUE) ) { $entity = ['name' => $matches[1]]; - if ($this->_isFieldSelected('description') || $this->_isFieldSelected('comment') || $this->_isFieldSelected('see')) { + if ($getDocs) { $this->addDocs($entity); } $entities[$matches[1]] = $entity; diff --git a/Civi/Api4/Action/GetActions.php b/Civi/Api4/Action/GetActions.php index c960bca6a9..2a022206b1 100644 --- a/Civi/Api4/Action/GetActions.php +++ b/Civi/Api4/Action/GetActions.php @@ -84,7 +84,7 @@ class GetActions extends BasicGetAction { $action = ActionUtil::getAction($this->getEntityName(), $actionName); if (is_object($action)) { $this->_actions[$actionName] = ['name' => $actionName]; - if ($this->_isFieldSelected('description') || $this->_isFieldSelected('comment')) { + if ($this->_isFieldSelected('description', 'comment')) { $actionReflection = new \ReflectionClass($action); $actionInfo = ReflectionUtils::getCodeDocs($actionReflection); unset($actionInfo['method']); diff --git a/Civi/Api4/Generic/AbstractGetAction.php b/Civi/Api4/Generic/AbstractGetAction.php index 1e91ec928b..06d3f6c2f8 100644 --- a/Civi/Api4/Generic/AbstractGetAction.php +++ b/Civi/Api4/Generic/AbstractGetAction.php @@ -112,41 +112,46 @@ abstract class AbstractGetAction extends AbstractQueryAction { } /** - * Helper to see if a field should be selected by the getRecords function. + * Helper to see if field(s) should be selected by the getRecords function. * * Checks the SELECT, WHERE and ORDER BY params to see what fields are needed. * * Note that if no SELECT clause has been set then all fields should be selected * and this function will always return TRUE. * - * @param string $field + * @param string ...$fieldNames + * One or more field names to check (uses OR if multiple) * @return bool + * Returns true if any given fields are in use. */ - protected function _isFieldSelected($field) { - if (!$this->select || in_array($field, $this->select) || isset($this->orderBy[$field])) { + protected function _isFieldSelected(string ...$fieldNames) { + if (!$this->select || array_intersect($fieldNames, array_merge($this->select, array_keys($this->orderBy)))) { return TRUE; } - return $this->_whereContains($field); + return $this->_whereContains($fieldNames); } /** - * Walk through the where clause and check if a field is in use. + * Walk through the where clause and check if field(s) are in use. * - * @param string $field + * @param string|array $fieldName + * A single fieldName or an array of names (uses OR if multiple) * @param array $clauses * @return bool + * Returns true if any given fields are found in the where clause. */ - protected function _whereContains($field, $clauses = NULL) { + protected function _whereContains($fieldName, $clauses = NULL) { if ($clauses === NULL) { $clauses = $this->where; } + $fieldName = (array) $fieldName; foreach ($clauses as $clause) { if (is_array($clause) && is_string($clause[0])) { - if ($clause[0] == $field) { + if (in_array($clause[0], $fieldName)) { return TRUE; } elseif (is_array($clause[1])) { - return $this->_whereContains($field, $clause[1]); + return $this->_whereContains($fieldName, $clause[1]); } } } diff --git a/tests/phpunit/api/v4/Action/BasicActionsTest.php b/tests/phpunit/api/v4/Action/BasicActionsTest.php index c0c6852103..ace6f05bf8 100644 --- a/tests/phpunit/api/v4/Action/BasicActionsTest.php +++ b/tests/phpunit/api/v4/Action/BasicActionsTest.php @@ -176,15 +176,13 @@ class BasicActionsTest extends UnitTestCase { // If no "select" is set, should always return true $this->assertTrue($isFieldSelected->invoke($get, 'color')); $this->assertTrue($isFieldSelected->invoke($get, 'shape')); - $this->assertTrue($isFieldSelected->invoke($get, 'size')); + $this->assertTrue($isFieldSelected->invoke($get, 'size', 'color', 'shape')); // With a non-empty "select" fieldsToSelect() will return fields needed to evaluate each clause. $get->addSelect('id'); - $this->assertTrue($isFieldSelected->invoke($get, 'color')); + $this->assertTrue($isFieldSelected->invoke($get, 'color', 'shape', 'size')); $this->assertTrue($isFieldSelected->invoke($get, 'id')); - $this->assertFalse($isFieldSelected->invoke($get, 'shape')); - $this->assertFalse($isFieldSelected->invoke($get, 'size')); - $this->assertFalse($isFieldSelected->invoke($get, 'weight')); + $this->assertFalse($isFieldSelected->invoke($get, 'shape', 'size', 'weight')); $this->assertFalse($isFieldSelected->invoke($get, 'group')); $get->addClause('OR', ['shape', '=', 'round'], ['AND', [['size', '=', 'big'], ['weight', '!=', 'small']]]); @@ -192,7 +190,7 @@ class BasicActionsTest extends UnitTestCase { $this->assertTrue($isFieldSelected->invoke($get, 'id')); $this->assertTrue($isFieldSelected->invoke($get, 'shape')); $this->assertTrue($isFieldSelected->invoke($get, 'size')); - $this->assertTrue($isFieldSelected->invoke($get, 'weight')); + $this->assertTrue($isFieldSelected->invoke($get, 'group', 'weight')); $this->assertFalse($isFieldSelected->invoke($get, 'group')); $get->addOrderBy('group'); -- 2.25.1