From 92ef78289a9545db3ddf0b131d83b3401f7c1567 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Fri, 18 Jun 2021 14:42:44 +1000 Subject: [PATCH] Fix bug found by Eileen on creating Custom Fields of Paritipant event type and add unit tests to lock in fix --- CRM/Core/BAO/CustomGroup.php | 21 +++++++++++++++++- tests/phpunit/api/v3/CustomGroupTest.php | 28 ++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/CRM/Core/BAO/CustomGroup.php b/CRM/Core/BAO/CustomGroup.php index 9f18bfde45..d8c189a09e 100644 --- a/CRM/Core/BAO/CustomGroup.php +++ b/CRM/Core/BAO/CustomGroup.php @@ -51,7 +51,8 @@ class CRM_Core_BAO_CustomGroup extends CRM_Core_DAO_CustomGroup { $extendsChildType = $params['extends_entity_column_value']; } if (!CRM_Utils_System::isNull($extendsChildType)) { - $registeredSubTypes = self::getSubTypes()[$params['extends']]; + $b = self::getMungedEntity($params['extends'], $params['extends_entity_column_id'] ?? NULL); + $registeredSubTypes = self::getSubTypes()[$b]; if (is_array($extendsChildType)) { foreach ($extendsChildType as $childType) { if (!array_key_exists($childType, $registeredSubTypes) && !in_array($childType, $registeredSubTypes, TRUE)) { @@ -2228,4 +2229,22 @@ SELECT civicrm_custom_group.id as groupID, civicrm_custom_group.title as groupT return $sel2; } + /** + * Get the munged entity. + * + * This is the entity eg. Relationship or the name of the sub entity + * e.g ParticipantRole. + * + * @param string $extends + * @param int|null $extendsEntityColumn + * + * @return string + */ + protected static function getMungedEntity($extends, $extendsEntityColumn = NULL) { + if (!$extendsEntityColumn || $extendsEntityColumn === 'null') { + return $extends; + } + return CRM_Core_OptionGroup::values('custom_data_type', FALSE, FALSE, FALSE, NULL, 'name')[$extendsEntityColumn]; + } + } diff --git a/tests/phpunit/api/v3/CustomGroupTest.php b/tests/phpunit/api/v3/CustomGroupTest.php index 36e5f6f759..1c38a6c09f 100644 --- a/tests/phpunit/api/v3/CustomGroupTest.php +++ b/tests/phpunit/api/v3/CustomGroupTest.php @@ -368,4 +368,32 @@ class api_v3_CustomGroupTest extends CiviUnitTestCase { $this->customGroupDelete($customGroupId); } + /** + * Test that as per the form that if the extends column is passed as + * - ['ParticipantEventType', [4]] Where 4 = Meeting Event Type that we can create a custom group correctly + */ + public function testParticipantEntityCustomGroup() { + $customGroup = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, ['extends' => ['ParticipantEventType', [4]]])); + $result = array_shift($customGroup['values']); + $this->assertEquals(3, $result['extends_entity_column_id']); + $this->assertEquals('Participant', $result['extends']); + $this->customGroupDelete($result['id']); + } + + /** + * Test that without any fields we can change the entity type of the custom group and fields are correctly updated + */ + public function testChangeEntityCustomGroup() { + $customGroup = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, ['extends' => ['ParticipantEventType', [4]]])); + $result = array_shift($customGroup['values']); + $this->assertEquals(3, $result['extends_entity_column_id']); + $this->assertEquals('Participant', $result['extends']); + $customGroup = $this->callAPISuccess($this->_entity, 'create', ['id' => $customGroup['id'], 'extends' => ['Individual', []]]); + $result = array_shift($customGroup['values']); + $this->assertTrue(empty($result['extends_entity_column_id'])); + $this->assertTrue(empty($result['extends_entity_column_value'])); + $this->assertEquals('Individual', $result['extends']); + $this->customGroupDelete($result['id']); + } + } -- 2.25.1