From 14c690fb1e2917a5504b82a7e55f399ac79a9ed6 Mon Sep 17 00:00:00 2001 From: colemanw Date: Sat, 21 Oct 2023 14:15:00 -0400 Subject: [PATCH] ManagedEntity - Simplify ExportAction to omit domain_id --- Civi/Api4/Generic/ExportAction.php | 29 ++----------------- .../api/v4/Entity/ManagedEntityTest.php | 26 +++++------------ 2 files changed, 9 insertions(+), 46 deletions(-) diff --git a/Civi/Api4/Generic/ExportAction.php b/Civi/Api4/Generic/ExportAction.php index 1644b7fae2..158ad8515e 100644 --- a/Civi/Api4/Generic/ExportAction.php +++ b/Civi/Api4/Generic/ExportAction.php @@ -121,24 +121,6 @@ class ExportAction extends AbstractAction { } // The get api always returns ID, but it should not be included in an export unset($record['id']); - // Should references be limited to the current domain? - $limitRefsByDomain = $entityType === 'OptionGroup' && \CRM_Core_OptionGroup::isDomainOptionGroup($record['name']) ? \CRM_Core_BAO_Domain::getDomain()->id : FALSE; - foreach ($allFields as $fieldName => $field) { - if (($field['fk_entity'] ?? NULL) === 'Domain') { - $alias = $fieldName . '.name'; - if (isset($record[$alias])) { - // If this entity is for a specific domain, limit references to that same domain - if ($fieldName === 'domain_id') { - $limitRefsByDomain = \CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', $record[$alias], 'id', 'name'); - } - // Swap current domain for special API keyword - if ($record[$alias] === \CRM_Core_BAO_Domain::getDomain()->name) { - unset($record[$alias]); - $record[$fieldName] = 'current_domain'; - } - } - } - } $name = ($parentName ?? '') . $entityType . '_' . ($record['name'] ?? count($this->exportedEntities[$entityType])); // Ensure safe characters, max length. // This is used for the value of `civicrm_managed.name` which has a maxlength of 255, but is also used @@ -197,15 +179,6 @@ class ExportAction extends AbstractAction { if ($refEntity === 'CustomField' && $entityType === 'OptionGroup') { continue; } - // Limit references by domain - if (property_exists($reference, 'domain_id')) { - if (!isset($reference->domain_id)) { - $reference->find(TRUE); - } - if (isset($reference->domain_id) && $reference->domain_id != $limitRefsByDomain) { - continue; - } - } $references[$refEntity][] = $reference; } foreach ($references as $refEntity => $records) { @@ -284,6 +257,8 @@ class ExportAction extends AbstractAction { ['type', 'IN', ['Field', 'Custom']], ['readonly', '!=', TRUE], ]; + // Domains are handled automatically + $excludeFields[] = 'domain_id'; if ($excludeFields) { $conditions[] = ['name', 'NOT IN', $excludeFields]; } diff --git a/tests/phpunit/api/v4/Entity/ManagedEntityTest.php b/tests/phpunit/api/v4/Entity/ManagedEntityTest.php index f9829c9a7e..0ad05516ba 100644 --- a/tests/phpunit/api/v4/Entity/ManagedEntityTest.php +++ b/tests/phpunit/api/v4/Entity/ManagedEntityTest.php @@ -448,23 +448,6 @@ class ManagedEntityTest extends TestCase implements HeadlessInterface, Transacti $this->assertCount(0, OptionGroup::get(FALSE)->addWhere('name', '=', 'testManagedOptionGroup')->execute()); } - /** - * @throws \CRM_Core_Exception - */ - public function testExportOptionGroupWithDomain(): void { - $result = OptionGroup::get(FALSE) - ->addWhere('name', '=', 'from_email_address') - ->addChain('export', OptionGroup::export()->setId('$id')) - ->execute()->first(); - $this->assertEquals('from_email_address', $result['export'][1]['params']['values']['option_group_id.name']); - $this->assertArrayNotHasKey('visibility_id', $result['export'][1]['params']['values']); - $this->assertStringStartsWith('OptionGroup_from_email_address_OptionValue_', $result['export'][1]['name']); - // All references should be from the current domain - foreach (array_slice($result['export'], 1) as $reference) { - $this->assertEquals('current_domain', $reference['params']['values']['domain_id']); - } - } - /** * @throws \CRM_Core_Exception */ @@ -599,6 +582,10 @@ class ManagedEntityTest extends TestCase implements HeadlessInterface, Transacti $this->assertEquals(2, $nav['export'][2]['params']['values']['has_separator']); // Weight should not be included in export of children, leaving it to be auto-managed $this->assertArrayNotHasKey('weight', $nav['export'][1]['params']['values']); + // Domain is auto-managed & should not be included in export + $this->assertArrayNotHasKey('domain_id', $nav['export'][1]['params']['values']); + $this->assertArrayNotHasKey('domain_id.name', $nav['export'][1]['params']['values']); + $this->assertArrayNotHasKey('domain_id:name', $nav['export'][1]['params']['values']); // Children should have been assigned correct auto-weights $children = Navigation::get(FALSE) @@ -615,9 +602,10 @@ class ManagedEntityTest extends TestCase implements HeadlessInterface, Transacti ->setId($decoys[0]['id']) ->execute(); $this->assertCount(4, $decoyExport); - $this->assertEquals('Decoy domain', $decoyExport[0]['params']['values']['domain_id.name']); - $this->assertEquals('Decoy domain', $decoyExport[1]['params']['values']['domain_id.name']); $this->assertArrayNotHasKey('weight', $decoyExport[1]['params']['values']); + $this->assertArrayNotHasKey('domain_id', $decoyExport[1]['params']['values']); + $this->assertArrayNotHasKey('domain_id.name', $decoyExport[1]['params']['values']); + $this->assertArrayNotHasKey('domain_id:name', $decoyExport[1]['params']['values']); // Refresh managed entities with module disabled $allModules = [ -- 2.25.1