diff --git a/src/lib/Util/FieldDefinitionGroupsUtil.php b/src/lib/Util/FieldDefinitionGroupsUtil.php index 1d5225e8be..ebbb5ef24a 100644 --- a/src/lib/Util/FieldDefinitionGroupsUtil.php +++ b/src/lib/Util/FieldDefinitionGroupsUtil.php @@ -30,22 +30,17 @@ public function __construct(FieldsGroupsList $fieldsGroupsListHelper) */ public function groupFieldDefinitions(iterable $fieldDefinitions): array { - $fieldDefinitionsByGroup = []; - foreach ($this->fieldsGroupsListHelper->getGroups() as $groupId => $groupName) { - $fieldDefinitionsByGroup[$groupId] = [ - 'name' => $groupName, - 'fieldDefinitions' => [], - ]; - } + $fieldsGroups = $this->fieldsGroupsListHelper->getGroups(); + $defaultGroup = $this->fieldsGroupsListHelper->getDefaultGroup(); + $fieldDefinitionsByGroup = []; foreach ($fieldDefinitions as $fieldDefinition) { - $groupId = $fieldDefinition->fieldGroup; - if (!$groupId) { - $groupId = $this->fieldsGroupsListHelper->getDefaultGroup(); - } + $groupId = $fieldDefinition->fieldGroup ?: $defaultGroup; + $fieldDefinitionsByGroup[$groupId]['name'] = $fieldDefinitionsByGroup[$groupId]['name'] + ?? $fieldsGroups[$groupId] + ?? $defaultGroup; $fieldDefinitionsByGroup[$groupId]['fieldDefinitions'][] = $fieldDefinition; - $fieldDefinitionsByGroup[$groupId]['name'] = $fieldDefinitionsByGroup[$groupId]['name'] ?? $groupId; } return $fieldDefinitionsByGroup; diff --git a/tests/lib/Util/FieldDefinitionGroupsUtilTest.php b/tests/lib/Util/FieldDefinitionGroupsUtilTest.php index 2168800a92..dff6eb4ec6 100644 --- a/tests/lib/Util/FieldDefinitionGroupsUtilTest.php +++ b/tests/lib/Util/FieldDefinitionGroupsUtilTest.php @@ -17,21 +17,27 @@ class FieldDefinitionGroupsUtilTest extends TestCase public function testGroupFieldDefinitions() { $randomGroupFieldDefinition = new FieldDefinition(['fieldGroup' => 'random']); + $testGroupFieldDefinition = new FieldDefinition(['fieldGroup' => 'test']); $defaultGroupFieldDefinition = new FieldDefinition(); $fieldDefinitions = [ $randomGroupFieldDefinition, + $testGroupFieldDefinition, $defaultGroupFieldDefinition, ]; $groupedFieldDefinitions = [ - 'content' => [ - 'name' => 'Content', - 'fieldDefinitions' => [$defaultGroupFieldDefinition], - ], 'random' => [ 'name' => 'Random', 'fieldDefinitions' => [$randomGroupFieldDefinition], ], + 'test' => [ + 'name' => 'Test', + 'fieldDefinitions' => [$testGroupFieldDefinition], + ], + 'content' => [ + 'name' => 'Content', + 'fieldDefinitions' => [$defaultGroupFieldDefinition], + ], ]; $fieldsGroupsListHelper = $this->getMockBuilder(FieldsGroupsList::class)->getMock(); @@ -40,10 +46,11 @@ public function testGroupFieldDefinitions() ->willReturn('content'); $fieldsGroupsListHelper ->method('getGroups') - ->willReturn(['random' => 'Random', 'content' => 'Content']); + ->willReturn(['another' => 'Another', 'test' => 'Test', 'random' => 'Random', 'content' => 'Content']); $util = new FieldDefinitionGroupsUtil($fieldsGroupsListHelper); + $this->assertSame($groupedFieldDefinitions, $util->groupFieldDefinitions($fieldDefinitions)); $this->assertEquals($groupedFieldDefinitions, $util->groupFieldDefinitions($fieldDefinitions)); } }