APIv4 - Exclude disabled custom fields
authorColeman Watts <coleman@civicrm.org>
Wed, 25 May 2022 19:02:29 +0000 (15:02 -0400)
committerColeman Watts <coleman@civicrm.org>
Thu, 26 May 2022 01:22:27 +0000 (21:22 -0400)
Civi/Api4/Service/Spec/SpecGatherer.php
tests/phpunit/api/v4/Custom/BasicCustomFieldTest.php
tests/phpunit/api/v4/Custom/CustomValueTest.php

index 94a4abc073a9eade95903be3c45899ac3cfc4aa0..5053ae84d9f82358277c14346a94fcf2b933fc73 100644 (file)
@@ -122,6 +122,7 @@ class SpecGatherer {
 
     $query = CustomField::get(FALSE)
       ->setSelect(['custom_group_id.name', 'custom_group_id.title', '*'])
+      ->addWhere('is_active', '=', TRUE)
       ->addWhere('custom_group_id.is_multiple', '=', '0');
 
     // Contact custom groups are extra complicated because contact_type can be a value for extends
@@ -196,6 +197,7 @@ class SpecGatherer {
   private function getCustomGroupFields($customGroup, RequestSpec $specification) {
     $customFields = CustomField::get(FALSE)
       ->addWhere('custom_group_id.name', '=', $customGroup)
+      ->addWhere('is_active', '=', TRUE)
       ->setSelect(['custom_group_id.name', 'custom_group_id.table_name', 'custom_group_id.title', '*'])
       ->execute();
 
index c12421fdfe2ad2fac88a1890250c4eb4679c3a57..41137824eea5a0643bab7cc77d0db920116dcc8f 100644 (file)
@@ -95,6 +95,25 @@ class BasicCustomFieldTest extends CustomTestBase {
       ->execute()
       ->first();
     $this->assertEquals(NULL, $contact['MyIndividualFields.FavColor']);
+
+    // Disable the field and it disappears from getFields and from the API output.
+    CustomField::update(FALSE)
+      ->addWhere('custom_group_id:name', '=', 'MyIndividualFields')
+      ->addWhere('name', '=', 'FavColor')
+      ->addValue('is_active', FALSE)
+      ->execute();
+
+    $getFields = Contact::getFields(FALSE)
+      ->execute()->column('name');
+    $this->assertContains('first_name', $getFields);
+    $this->assertNotContains('MyIndividualFields.FavColor', $getFields);
+
+    $contact = Contact::get(FALSE)
+      ->addSelect('MyIndividualFields.FavColor')
+      ->addWhere('id', '=', $contactId)
+      ->execute()
+      ->first();
+    $this->assertArrayNotHasKey('MyIndividualFields.FavColor', $contact);
   }
 
   public function testWithTwoFields() {
index dd632c2f6f0d74fe6449108c5b5ca88ea2774fbf..ffe2f517d00fcb4af2e1c38aa92d90fa148a81da 100644 (file)
@@ -90,7 +90,7 @@ class CustomValueTest extends CustomTestBase {
     $this->assertEquals('secondary', $entity['searchable']);
 
     // Retrieve and check the fields of CustomValue = Custom_$group
-    $fields = CustomValue::getFields($group)->setLoadOptions(TRUE)->setCheckPermissions(FALSE)->execute();
+    $fields = CustomValue::getFields($group, FALSE)->setLoadOptions(TRUE)->execute();
     $expectedResult = [
       [
         'custom_group' => $group,
@@ -252,6 +252,16 @@ class CustomValueTest extends CustomTestBase {
       }
     }
 
+    // Disable a field
+    CustomField::update(FALSE)
+      ->addValue('is_active', FALSE)
+      ->addWhere('id', '=', $multiField['id'])
+      ->execute();
+
+    $result = CustomValue::get($group)->execute()->single();
+    $this->assertArrayHasKey($colorFieldName, $result);
+    $this->assertArrayNotHasKey($multiFieldName, $result);
+
     // CASE 4: Test CustomValue::delete
     // There is only record left whose id = 3, delete that record on basis of criteria id = 3
     CustomValue::delete($group)->addWhere("id", "=", 3)->execute();