[php8-compact][REF] Fix failing custom group test on php8 by better handling strings...
authorSeamus Lee <seamuslee001@gmail.com>
Wed, 16 Jun 2021 01:59:33 +0000 (01:59 +0000)
committerSeamus Lee <seamuslee001@gmail.com>
Fri, 18 Jun 2021 04:42:55 +0000 (14:42 +1000)
CRM/Core/BAO/CustomGroup.php
CRM/Custom/Form/Group.php
tests/phpunit/api/v3/CustomGroupTest.php

index 568e92a6336ff9c74e3f2bdc644eb8510e60e84f..44c4e958c0f8fa385e5bb2161e5492e93466557e 100644 (file)
@@ -74,7 +74,21 @@ class CRM_Core_BAO_CustomGroup extends CRM_Core_DAO_CustomGroup {
       $extendsChildType = $params['extends_entity_column_value'];
     }
     if (!CRM_Utils_System::isNull($extendsChildType)) {
-      $extendsChildType = implode(CRM_Core_DAO::VALUE_SEPARATOR, $extendsChildType);
+      $registeredSubTypes = self::getSubTypes()[$extendsEntity];
+      if (is_array($extendsChildType)) {
+        foreach ($extendsChildType as $childType) {
+          if (!array_key_exists($childType, $registeredSubTypes) && !in_array($childType, $registeredSubTypes, TRUE)) {
+            throw new CRM_Core_Exception('Supplied Sub type is not valid for the specified entitiy');
+          }
+        }
+      }
+      else {
+        if (!array_key_exists($extendsChildType, $registeredSubTypes) && !in_array($extendsChildType, $registeredSubTypes, TRUE)) {
+          throw new CRM_Core_Exception('Supplied Sub type is not valid for the specified entitiy');
+        }
+        $extendsChildType = [$extendsChildType];
+      }
+      $extendsChildType = implode(CRM_Core_DAO::VALUE_SEPARATOR, (array) $extendsChildType);
       if (CRM_Utils_Array::value(0, $extends) == 'Relationship') {
         $extendsChildType = str_replace(['_a_b', '_b_a'], [
           '',
@@ -2200,4 +2214,39 @@ SELECT  civicrm_custom_group.id as groupID, civicrm_custom_group.title as groupT
     return [$multipleFieldGroups, $groupTree];
   }
 
+  public static function getSubTypes(): array {
+    $sel2 = [];
+    $activityType = CRM_Core_PseudoConstant::activityType(FALSE, TRUE, FALSE, 'label', TRUE);
+
+    $eventType = CRM_Core_OptionGroup::values('event_type');
+    $grantType = CRM_Core_OptionGroup::values('grant_type');
+    $campaignTypes = CRM_Campaign_PseudoConstant::campaignType();
+    $membershipType = CRM_Member_BAO_MembershipType::getMembershipTypes(FALSE);
+    $participantRole = CRM_Core_OptionGroup::values('participant_role');
+
+    asort($activityType);
+    asort($eventType);
+    asort($grantType);
+    asort($membershipType);
+    asort($participantRole);
+
+    $sel2['Event'] = $eventType;
+    $sel2['Grant'] = $grantType;
+    $sel2['Activity'] = $activityType;
+    $sel2['Campaign'] = $campaignTypes;
+    $sel2['Membership'] = $membershipType;
+    $sel2['ParticipantRole'] = $participantRole;
+    $sel2['ParticipantEventName'] = CRM_Event_PseudoConstant::event(NULL, FALSE, "( is_template IS NULL OR is_template != 1 )");
+    $sel2['ParticipantEventType'] = $eventType;
+    $sel2['Contribution'] = CRM_Contribute_PseudoConstant::financialType();
+    $sel2['Relationship'] = CRM_Custom_Form_Group::getRelationshipTypes();
+
+    $sel2['Individual'] = CRM_Contact_BAO_ContactType::subTypePairs('Individual', FALSE, NULL);
+    $sel2['Household'] = CRM_Contact_BAO_ContactType::subTypePairs('Household', FALSE, NULL);
+    $sel2['Organization'] = CRM_Contact_BAO_ContactType::subTypePairs('Organization', FALSE, NULL);
+
+    CRM_Core_BAO_CustomGroup::getExtendedObjectTypes($sel2);
+    return $sel2;
+  }
+
 }
index ebb516f7993977a80333708617752681cb548999..2b1cf9158eeb3e3906c54a5627940467f036d019 100644 (file)
@@ -176,38 +176,8 @@ class CRM_Custom_Form_Group extends CRM_Core_Form {
     $this->assign('contactTypes', json_encode($contactTypes));
 
     $sel1 = ["" => ts("- select -")] + CRM_Core_SelectValues::customGroupExtends();
-    $sel2 = [];
-    $activityType = CRM_Core_PseudoConstant::activityType(FALSE, TRUE, FALSE, 'label', TRUE);
-
-    $eventType = CRM_Core_OptionGroup::values('event_type');
-    $grantType = CRM_Core_OptionGroup::values('grant_type');
-    $campaignTypes = CRM_Campaign_PseudoConstant::campaignType();
-    $membershipType = CRM_Member_BAO_MembershipType::getMembershipTypes(FALSE);
-    $participantRole = CRM_Core_OptionGroup::values('participant_role');
-
     ksort($sel1);
-    asort($activityType);
-    asort($eventType);
-    asort($grantType);
-    asort($membershipType);
-    asort($participantRole);
-
-    $sel2['Event'] = $eventType;
-    $sel2['Grant'] = $grantType;
-    $sel2['Activity'] = $activityType;
-    $sel2['Campaign'] = $campaignTypes;
-    $sel2['Membership'] = $membershipType;
-    $sel2['ParticipantRole'] = $participantRole;
-    $sel2['ParticipantEventName'] = CRM_Event_PseudoConstant::event(NULL, FALSE, "( is_template IS NULL OR is_template != 1 )");
-    $sel2['ParticipantEventType'] = $eventType;
-    $sel2['Contribution'] = CRM_Contribute_PseudoConstant::financialType();
-    $sel2['Relationship'] = self::getRelationshipTypes();
-
-    $sel2['Individual'] = CRM_Contact_BAO_ContactType::subTypePairs('Individual', FALSE, NULL);
-    $sel2['Household'] = CRM_Contact_BAO_ContactType::subTypePairs('Household', FALSE, NULL);
-    $sel2['Organization'] = CRM_Contact_BAO_ContactType::subTypePairs('Organization', FALSE, NULL);
-
-    CRM_Core_BAO_CustomGroup::getExtendedObjectTypes($sel2);
+    $sel2 = CRM_Core_BAO_CustomGroup::getSubTypes();
 
     foreach ($sel2 as $main => $sub) {
       if (!empty($sel2[$main])) {
index e689a66bf40132566a0c3e1d95cfcc9c2ebc4132..36e5f6f759c95115f298103d8536c65d5c63bf7f 100644 (file)
@@ -175,8 +175,7 @@ class api_v3_CustomGroupTest extends CiviUnitTestCase {
       'is_active' => 1,
     ];
 
-    $result = $this->callAPIFailure('custom_group', 'create', $params,
-      'implode(): Invalid arguments passed');
+    $result = $this->callAPIFailure('custom_group', 'create', $params, 'Supplied Sub type is not valid for the specified entitiy');
   }
 
   /**