From 3f0340c8e9fe79987f76ff4e67a78a83db2d34b4 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 3 Jun 2021 20:13:48 -0700 Subject: [PATCH] FinancialItem - Provide defaults so that stricter ConformanceTest will pas Context: There were three separate, concurrent PRs - two added more tests and events to APIv4, and the third added a new entity (FinancialItem). FinancialItem got merged first. I'm working reconciling the other two... and discovered that `FinancialItem` isn't passing. Problem: When the `ConformanceTest` creates a `FinancialItem`, it doesn't fill in valid values for `entity_table,entity_id`. These values are important to the access-control criteria used in reading-back data. --- .../FinancialItemCreationSpecProvider.php | 52 +++++++++++++++++++ .../Service/TestCreationParameterProvider.php | 13 ++++- 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 Civi/Api4/Service/Spec/Provider/FinancialItemCreationSpecProvider.php diff --git a/Civi/Api4/Service/Spec/Provider/FinancialItemCreationSpecProvider.php b/Civi/Api4/Service/Spec/Provider/FinancialItemCreationSpecProvider.php new file mode 100644 index 0000000000..4567d2bed1 --- /dev/null +++ b/Civi/Api4/Service/Spec/Provider/FinancialItemCreationSpecProvider.php @@ -0,0 +1,52 @@ +getFieldByName('entity_table')->setRequired(TRUE); + $spec->getFieldByName('entity_id')->setRequired(TRUE); + $spec->getFieldByName('entity_table')->setDefaultValue(self::DEFAULT_TABLE); + } + + /** + * @param string $entity + * @param string $action + * + * @return bool + */ + public function applies($entity, $action) { + return $entity === 'FinancialItem' && $action === 'create'; + } + +} diff --git a/tests/phpunit/api/v4/Service/TestCreationParameterProvider.php b/tests/phpunit/api/v4/Service/TestCreationParameterProvider.php index 632945ed49..757258b48e 100644 --- a/tests/phpunit/api/v4/Service/TestCreationParameterProvider.php +++ b/tests/phpunit/api/v4/Service/TestCreationParameterProvider.php @@ -20,6 +20,7 @@ namespace api\v4\Service; use Civi\Api4\Service\Spec\FieldSpec; +use Civi\Api4\Service\Spec\Provider\FinancialItemCreationSpecProvider; use Civi\Api4\Service\Spec\SpecGatherer; class TestCreationParameterProvider { @@ -82,9 +83,19 @@ class TestCreationParameterProvider { elseif ($field->getDefaultValue()) { return $field->getDefaultValue(); } - elseif (in_array($field->getName(), ['entity_id', 'contact_id'])) { + elseif ($field->getName() === 'contact_id') { return $this->getFkID($field, 'Contact'); } + elseif ($field->getName() === 'entity_id') { + // What could possibly go wrong with this? + switch ($field->getTableName()) { + case 'civicrm_financial_item': + return $this->getFkID($field, FinancialItemCreationSpecProvider::DEFAULT_ENTITY); + + default: + return $this->getFkID($field, 'Contact'); + } + } $randomValue = $this->getRandomValue($field->getDataType()); -- 2.25.1