Skip to content

Commit

Permalink
IBX-8429: Respect field groups' order based on field definitions' ord…
Browse files Browse the repository at this point in the history
…er (#1295)

* IBX-8429: Respect field groups' order based on field definitions' order

* IBX-8429: Name fallback

* IBX-8429: Added test coverage

* IBX-8429: Simplified code

* IBX-8429: Rollback conditional

* IBX-8429: Simplified code
  • Loading branch information
barw4 authored Aug 7, 2024
1 parent 0e8c9f3 commit 7e6bb94
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
19 changes: 7 additions & 12 deletions src/lib/Util/FieldDefinitionGroupsUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 12 additions & 5 deletions tests/lib/Util/FieldDefinitionGroupsUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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));
}
}
Expand Down

0 comments on commit 7e6bb94

Please sign in to comment.