Skip to content

Commit 4b3e640

Browse files
Fix category import
1 parent 930e2ed commit 4b3e640

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

webapp/src/Service/ImportExportService.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -804,13 +804,26 @@ public function importGroupsJson(array $data, ?string &$message = null, ?array &
804804
{
805805
// TODO: can we have this use the DTO?
806806
$groupData = [];
807-
foreach ($data as $group) {
807+
foreach ($data as $index => $group) {
808+
if (isset($group['types'])) {
809+
$types = [];
810+
$typeMapping = array_flip(TeamCategory::TYPES_TO_STRING);
811+
foreach ($group['types'] as $type) {
812+
if (!isset($typeMapping[$type])) {
813+
$message = sprintf('Invalid group type at index %d: %s', $index, $type);
814+
return -1;
815+
}
816+
$types[] = $typeMapping[$type];
817+
}
818+
} else {
819+
$types = [TeamCategory::TYPE_SCORING, TeamCategory::TYPE_BADGE_TOP];
820+
}
808821
$groupData[] = [
809822
'categoryid' => @$group['id'],
810823
'icpc_id' => @$group['icpc_id'],
811824
'name' => $group['name'] ?? '',
812825
'visible' => !($group['hidden'] ?? false),
813-
'types' => $group['types'] ?? [TeamCategory::TYPE_SCORING, TeamCategory::TYPE_BADGE_TOP],
826+
'types' => $types,
814827
'sortorder' => @$group['sortorder'],
815828
'color' => @$group['color'],
816829
'css_class' => @$group['css_class'],
@@ -825,7 +838,7 @@ public function importGroupsJson(array $data, ?string &$message = null, ?array &
825838
* Import group data from the given array
826839
*
827840
* @param array<array{categoryid: string, icpc_id?: string, name: string, visible?: bool,
828-
* types?: string[]|null, sortorder?: int|null, color?: string|null, css_class?: string|null,
841+
* types?: int[]|null, sortorder?: int|null, color?: string|null, css_class?: string|null,
829842
* allow_self_registration: bool}> $groupData
830843
* @param TeamCategory[]|null $saved The saved groups
831844
*
@@ -858,6 +871,7 @@ protected function importGroupData(
858871
}
859872
$added = true;
860873
}
874+
/** @var list<int> $types */
861875
$types = $groupItem['types'] ?? [TeamCategory::TYPE_SCORING, TeamCategory::TYPE_BADGE_TOP];
862876
$sortOrder = $groupItem['sortorder'] ?? null;
863877
if (in_array(TeamCategory::TYPE_SCORING, $types, true) && $sortOrder === null) {

webapp/tests/Unit/Service/ImportExportServiceTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,16 @@ public function testImportGroupsJson(): void
10581058
}, {
10591059
"id": "23",
10601060
"name": "Spectators"
1061+
}, {
1062+
"id": "24",
1063+
"name": "Color",
1064+
"types": ["background"],
1065+
"color": "#123123"
1066+
}, {
1067+
"id": "25",
1068+
"name": "CSS",
1069+
"types": ["css-class"],
1070+
"css_class": "test"
10611071
}]
10621072
EOF;
10631073

@@ -1066,15 +1076,32 @@ public function testImportGroupsJson(): void
10661076
'externalid' => '13337',
10671077
'name' => 'Companies',
10681078
'icpcid' => '123',
1079+
'sortorder' => 0,
10691080
'visible' => false,
10701081
], [
10711082
'externalid' => '47',
10721083
'name' => 'Participants',
1084+
'sortorder' => 0,
10731085
'visible' => true,
10741086
], [
10751087
'externalid' => '23',
10761088
'name' => 'Spectators',
1089+
'sortorder' => 0,
1090+
'visible' => true,
1091+
], [
1092+
'externalid' => '24',
1093+
'name' => 'Color',
1094+
'types' => [TeamCategory::TYPE_BACKGROUND],
1095+
'sortorder' => null,
1096+
'visible' => true,
1097+
'color' => '#123123',
1098+
], [
1099+
'externalid' => '25',
1100+
'name' => 'CSS',
1101+
'types' => [TeamCategory::TYPE_CSS_CLASS],
1102+
'sortorder' => null,
10771103
'visible' => true,
1104+
'cssClass' => 'test',
10781105
],
10791106
];
10801107

@@ -1098,7 +1125,11 @@ public function testImportGroupsJson(): void
10981125
self::assertNotNull($category, "Team cagegory $data[name] does not exist");
10991126
self::assertEquals($data['icpcid'] ?? null, $category->getIcpcId());
11001127
self::assertEquals($data['name'], $category->getName());
1128+
self::assertEquals($data['types'] ?? [TeamCategory::TYPE_SCORING, TeamCategory::TYPE_BADGE_TOP], $category->getTypes());
11011129
self::assertEquals($data['visible'], $category->getVisible());
1130+
self::assertEquals($data['sortorder'] ?? null, $category->getSortorder());
1131+
self::assertEquals($data['color'] ?? null, $category->getColor());
1132+
self::assertEquals($data['cssClass'] ?? null, $category->getCssClass());
11021133
}
11031134
}
11041135

0 commit comments

Comments
 (0)