APIv4 - Trim output of Export action to only non-default values
authorcolemanw <coleman@civicrm.org>
Sun, 1 Oct 2023 22:21:11 +0000 (18:21 -0400)
committercolemanw <coleman@civicrm.org>
Tue, 3 Oct 2023 01:20:40 +0000 (21:20 -0400)
Civi/Api4/Generic/ExportAction.php
tests/phpunit/api/v4/Custom/ExportCustomGroupTest.php
tests/phpunit/api/v4/Entity/ManagedEntityTest.php

index d7903a70e6930ec50ad033dadc4e7c3ee5810403..13b660ec7792991ac03fb6f27f54f5dc54206eae 100644 (file)
@@ -161,6 +161,12 @@ class ExportAction extends AbstractAction {
         unset($record[$fieldName]);
       }
     }
+    // Unset values that match the default
+    foreach ($allFields as $fieldName => $field) {
+      if (($record[$fieldName] ?? NULL) === $field['default_value']) {
+        unset($record[$fieldName]);
+      }
+    }
     $export = [
       'name' => $name,
       'entity' => $entityType,
index 429b4dffb6f8df65951c3b5a7bb25c236af56270..e36d3ba8b81932cf8fba61ca9279a68d88d3c6db 100644 (file)
@@ -66,7 +66,7 @@ class ExportCustomGroupTest extends CustomTestBase {
     // Should be only name, not id
     $this->assertArrayNotHasKey('option_group_id', $export[5]['params']['values']);
     // Field with no options
-    $this->assertNull($export[6]['params']['values']['option_group_id']);
+    $this->assertTrue(!isset($export[6]['params']['values']['option_group_id']));
     $this->assertArrayNotHasKey('option_group_id.name', $export[6]['params']['values']);
     $this->assertArrayNotHasKey('option_values', $export[6]['params']['values']);
   }
index 5aad9c475d4b4f579863358aa0409d7edec821b4..7c739a4e2d9e48955971ac46f43f90ed67abeff0 100644 (file)
@@ -410,7 +410,7 @@ class ManagedEntityTest extends TestCase implements HeadlessInterface, Transacti
       ->addChain('export', OptionGroup::export()->setId('$id'))
       ->execute()->first();
     $this->assertEquals('from_email_address', $result['export'][1]['params']['values']['option_group_id.name']);
-    $this->assertNull($result['export'][1]['params']['values']['visibility_id']);
+    $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) {