From ebd98361a6d96d85932787f2666616660498f83f Mon Sep 17 00:00:00 2001 From: colemanw Date: Tue, 19 Sep 2023 14:56:16 -0400 Subject: [PATCH] APIv4 - Hack around bad dfk metadata in unit tests --- CRM/Financial/BAO/EntityFinancialAccount.php | 2 +- Civi/Test/Api4TestTrait.php | 24 +++++++++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/CRM/Financial/BAO/EntityFinancialAccount.php b/CRM/Financial/BAO/EntityFinancialAccount.php index 80363d1844..41f4f5b069 100644 --- a/CRM/Financial/BAO/EntityFinancialAccount.php +++ b/CRM/Financial/BAO/EntityFinancialAccount.php @@ -278,8 +278,8 @@ class CRM_Financial_BAO_EntityFinancialAccount extends CRM_Financial_DAO_EntityF */ public static function entityTables(): array { return [ - 'civicrm_financial_type' => ts('Financial Type'), 'civicrm_option_value' => ts('Payment Instrument'), + 'civicrm_financial_type' => ts('Financial Type'), 'civicrm_payment_processor' => ts('Payment Processor'), ]; } diff --git a/Civi/Test/Api4TestTrait.php b/Civi/Test/Api4TestTrait.php index 1165723a4c..b78e81c7b2 100644 --- a/Civi/Test/Api4TestTrait.php +++ b/Civi/Test/Api4TestTrait.php @@ -4,7 +4,6 @@ namespace Civi\Test; use Civi\Api4\Generic\AbstractAction; use Civi\Api4\Generic\Result; -use Civi\Api4\Service\Spec\Provider\FinancialItemCreationSpecProvider; use Civi\Api4\Utils\CoreUtil; /** @@ -227,6 +226,19 @@ trait Api4TestTrait { * @throws \CRM_Core_Exception */ private function getRequiredValue(array $field) { + // Hack that shouldn't exist except that these entities were added with bad fk metadata + // and existing tests at the time didn't catch it. + // TODO: Fix these entities and delete this list + // NOT TODO: Add to this list! + $fixmeDfkMissing = [ + 'EntityBatch' => 'Contact', + 'FinancialTrxn' => 'Contact', + 'Log' => 'Contact', + 'Managed' => 'Contact', + 'PCPBlock' => 'Contact', + 'PriceSetEntity' => 'Contact', + 'RecentItem' => 'Contact', + ]; if (!empty($field['options'])) { return key($field['options']); } @@ -239,12 +251,12 @@ trait Api4TestTrait { if (isset($field['default_value'])) { return $field['default_value']; } + // Obviously an FK field, but if we get here it's missing FK metadata :( if ($field['name'] === 'contact_id' || $field['name'] === 'entity_id') { - // Obviously an FK field, but if we get here it's missing FK metadata :( - // FIXME: This is what we SHOULD do here... - // throw new \CRM_Core_Exception($field['name'] . ' should have foreign key information defined.'); - // ... instead this is how it was done, so we're stuck with it until FK metadata for every field gets fixed - return $this->getFkID('Contact'); + if (isset($fixmeDfkMissing[$field['entity']])) { + return $this->getFkID($fixmeDfkMissing[$field['entity']]); + } + throw new \CRM_Core_Exception($field['name'] . ' should have foreign key information defined.'); } // If there are no options but the field is supposed to have them, we may need to // create a new option -- 2.25.1