From bfe3bde75466d36708f289dcaad29c8f98560477 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 24 Jun 2021 16:24:34 -0400 Subject: [PATCH] APIv4 - add test for id_field --- Civi/Api4/Query/Api4SelectQuery.php | 14 +++----------- tests/phpunit/api/v4/Entity/ConformanceTest.php | 3 ++- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/Civi/Api4/Query/Api4SelectQuery.php b/Civi/Api4/Query/Api4SelectQuery.php index 99940efefd..1c174b3954 100644 --- a/Civi/Api4/Query/Api4SelectQuery.php +++ b/Civi/Api4/Query/Api4SelectQuery.php @@ -88,7 +88,7 @@ class Api4SelectQuery { $this->api = $apiGet; // Always select ID of main table unless grouping by something else - $id = $this->getIdField($this->getEntity()); + $id = CoreUtil::getInfoItem($this->getEntity(), 'id_field'); $this->forceSelectId = !$this->isAggregateQuery() || $this->getGroupBy() === [$id]; // Build field lists @@ -204,7 +204,7 @@ class Api4SelectQuery { } else { if ($this->forceSelectId) { - $id = $this->getIdField($this->getEntity()); + $id = CoreUtil::getInfoItem($this->getEntity(), 'id_field'); $select = array_merge([$id], $select); } @@ -232,7 +232,7 @@ class Api4SelectQuery { // If the joined_entity.id isn't in the fieldspec already, autoJoinFK will attempt to add the entity. $fkField = substr($wildField, 0, strrpos($wildField, '.')); $fkEntity = $this->getField($fkField)['fk_entity'] ?? NULL; - $id = $fkEntity ? $this->getIdField($fkEntity) : 'id'; + $id = $fkEntity ? CoreUtil::getInfoItem($fkEntity, 'id_field') : 'id'; $this->autoJoinFK($fkField . ".$id"); $matches = $this->selectMatchingFields($wildField); array_splice($select, $pos, 1, $matches); @@ -276,14 +276,6 @@ class Api4SelectQuery { return SelectUtil::getMatchingFields($pattern, array_keys($availableFields)); } - /** - * @param $entityName - * @return string - */ - private function getIdField($entityName) { - return CoreUtil::getApiClass($entityName)::getInfo()['id_field']; - } - /** * Add WHERE clause to query */ diff --git a/tests/phpunit/api/v4/Entity/ConformanceTest.php b/tests/phpunit/api/v4/Entity/ConformanceTest.php index 733cef9760..34d0b7cee6 100644 --- a/tests/phpunit/api/v4/Entity/ConformanceTest.php +++ b/tests/phpunit/api/v4/Entity/ConformanceTest.php @@ -164,7 +164,8 @@ class ConformanceTest extends UnitTestCase implements HookInterface { $this->assertNotEmpty($info['title_plural']); $this->assertNotEmpty($info['type']); $this->assertNotEmpty($info['description']); - $this->assertRegExp(';^\d\.\d\d$;', $info['since']); + $this->assertNotEmpty($info['id_field']); + $this->assertRegExp(';^\d\.\d+$;', $info['since']); $this->assertContains($info['searchable'], ['primary', 'secondary', 'bridge', 'none']); // Bridge must be between exactly 2 entities if (in_array('EntityBridge', $info['type'], TRUE)) { -- 2.25.1