Skip to content

Commit 048615d

Browse files
committed
Update HOT_CourseHandlerTest.cls
1 parent 6aa92a3 commit 048615d

File tree

1 file changed

+134
-32
lines changed

1 file changed

+134
-32
lines changed
Lines changed: 134 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
@isTest
22
private class HOT_CourseHandlerTest {
3-
43
@testSetup
54
static void setup() {
5+
TestDataFactory.insertEncryptionKey();
6+
67
Course__c testCourse = new Course__c(
78
Name = 'Test Course',
89
DescriptionFormatted__c = 'Course description for testing',
@@ -16,33 +17,90 @@ private class HOT_CourseHandlerTest {
1617
RegistrationDeadline__c = DateTime.now().addDays(-1)
1718
);
1819

19-
insert testCourse;
20+
Course__c capacityCourse = new Course__c(
21+
Name = 'Capacity Course',
22+
DescriptionFormatted__c = 'Course capacity for testing',
23+
DescriptionShort__c = 'Short capacity description',
24+
Region2__c = 'Oslo',
25+
Active__c = true,
26+
MaxNumberOfParticipants__c = 1,
27+
RegistrationPlaceName__c = 'Vitaminveien 20, 0920 Oslo',
28+
RegistrationFromDateTime__c = DateTime.now(),
29+
RegistrationToDateTime__c = DateTime.now().addHours(3),
30+
RegistrationDeadline__c = DateTime.now().addDays(-1)
31+
);
32+
33+
List<Course__c> coursesToInsert = new List<Course__c>{ testCourse, capacityCourse };
34+
insert coursesToInsert;
2035
}
2136

22-
@isTest
23-
static void testOnAfterInsert() {
24-
Course__c newCourse = new Course__c(
25-
Name = 'Trigger Inserted Course',
26-
DescriptionFormatted__c = 'Triggered test course',
27-
DescriptionShort__c = 'Short description',
28-
Region2__c = 'Rogaland',
29-
Active__c = true,
30-
MaxNumberOfParticipants__c = 15,
31-
RegistrationFromDateTime__c = DateTime.now(),
32-
RegistrationToDateTime__c = DateTime.now().addHours(3),
33-
RegistrationDeadline__c = DateTime.now().addDays(-1)
34-
);
35-
36-
System.Test.startTest();
37-
insert newCourse;
38-
System.Test.stopTest();
39-
40-
Course__c result = [SELECT Id, Name, OutlookCalendar__c, OutlookOfficeCalendar__c, GoogleCalendar__c FROM Course__c WHERE Id = :newCourse.Id LIMIT 1];
41-
42-
System.assertNotEquals(null, result.OutlookCalendar__c, 'Outlook calendar URL should have been generated by HOT_CourseHandler.');
43-
System.assertNotEquals(null, result.OutlookOfficeCalendar__c, 'Outlook office calendar URL should have been generated by HOT_CourseHandler.');
44-
System.assertNotEquals(null, result.GoogleCalendar__c, 'Google calendar URL should have been generated by HOT_CourseHandler.');
45-
}
37+
private static List<CourseRegistration__c> createRegistrationsBulk(
38+
Id courseId,
39+
Map<String, Integer> statusToCount
40+
) {
41+
List<CourseRegistration__c> regsToInsert = new List<CourseRegistration__c>();
42+
43+
for (String status : statusToCount.keySet()) {
44+
Integer count = statusToCount.get(status);
45+
for (Integer i = 0; i < count; i++) {
46+
regsToInsert.add(
47+
new CourseRegistration__c(
48+
ContactFirstName__c = 'Test' + i,
49+
ContactLastName__c = 'User' + i,
50+
ContactEmail__c = 'testuser' + Math.random() + i + '@example.com',
51+
Course__c = courseId,
52+
Status__c = status,
53+
NumberOfParticipants__c = 1
54+
)
55+
);
56+
}
57+
}
58+
59+
insert regsToInsert;
60+
return regsToInsert;
61+
}
62+
63+
@isTest
64+
static void testOnAfterInsert() {
65+
Course__c newCourse = new Course__c(
66+
Name = 'Trigger Inserted Course',
67+
DescriptionFormatted__c = 'Triggered test course',
68+
DescriptionShort__c = 'Short description',
69+
Region2__c = 'Rogaland',
70+
Active__c = true,
71+
MaxNumberOfParticipants__c = 15,
72+
RegistrationFromDateTime__c = DateTime.now(),
73+
RegistrationToDateTime__c = DateTime.now().addHours(3),
74+
RegistrationDeadline__c = DateTime.now().addDays(-1)
75+
);
76+
77+
System.Test.startTest();
78+
insert newCourse;
79+
System.Test.stopTest();
80+
81+
Course__c result = [
82+
SELECT Id, Name, OutlookCalendar__c, OutlookOfficeCalendar__c, GoogleCalendar__c
83+
FROM Course__c
84+
WHERE Id = :newCourse.Id
85+
LIMIT 1
86+
];
87+
88+
System.assertNotEquals(
89+
null,
90+
result.OutlookCalendar__c,
91+
'Outlook calendar URL should have been generated by HOT_CourseHandler.'
92+
);
93+
System.assertNotEquals(
94+
null,
95+
result.OutlookOfficeCalendar__c,
96+
'Outlook office calendar URL should have been generated by HOT_CourseHandler.'
97+
);
98+
System.assertNotEquals(
99+
null,
100+
result.GoogleCalendar__c,
101+
'Google calendar URL should have been generated by HOT_CourseHandler.'
102+
);
103+
}
46104

47105
@isTest
48106
static void testOnAfterUpdatedCourseUrl() {
@@ -62,16 +120,60 @@ private class HOT_CourseHandlerTest {
62120

63121
Course__c result = [
64122
SELECT Name, OutlookCalendar__c, RegistrationPlaceName__c
65-
FROM Course__c WHERE Id = :updatedCourse.Id
123+
FROM Course__c
124+
WHERE Id = :updatedCourse.Id
66125
];
67126

68127
String endcodedUpdatedName = EncodingUtil.urlEncode(result.Name, 'UTF-8');
69-
System.assert(result.OutlookCalendar__c.contains(endcodedUpdatedName),
70-
'OutlookCalendar__c URL should contain the updated course endcoded Name');
128+
System.assert(
129+
result.OutlookCalendar__c.contains(endcodedUpdatedName),
130+
'OutlookCalendar__c URL should contain the updated course endcoded Name'
131+
);
71132

72133
String encodedUpdatedPlace = EncodingUtil.urlEncode(result.RegistrationPlaceName__c, 'UTF-8');
73-
System.assert(result.OutlookCalendar__c.contains(encodedUpdatedPlace),
74-
'OutlookCalendar__c URL should contain the updated course place encoded RegistrationPlaceName__c.');
134+
System.assert(
135+
result.OutlookCalendar__c.contains(encodedUpdatedPlace),
136+
'OutlookCalendar__c URL should contain the updated course place encoded RegistrationPlaceName__c.'
137+
);
138+
}
139+
140+
@isTest
141+
static void testPromotionWithSignedUpAndWaitlist() {
142+
Course__c course = [
143+
SELECT Id, Name, MaxNumberOfParticipants__c
144+
FROM Course__c
145+
WHERE Name = 'Capacity Course'
146+
LIMIT 1
147+
];
148+
149+
Map<String, Integer> statusMap = new Map<String, Integer>{ 'Påmeldt' => 1, 'Venteliste' => 2 };
150+
List<CourseRegistration__c> allRegs = createRegistrationsBulk(course.Id, statusMap);
151+
152+
List<CourseRegistration__c> waitlistRegs = new List<CourseRegistration__c>();
153+
for (CourseRegistration__c r : allRegs) {
154+
if (r.Status__c == 'Venteliste') {
155+
waitlistRegs.add(r);
156+
}
157+
}
158+
159+
course.MaxNumberOfParticipants__c = 3;
160+
161+
System.Test.startTest();
162+
update course;
163+
System.Test.stopTest();
164+
165+
List<CourseRegistration__c> result = [
166+
SELECT Status__c
167+
FROM CourseRegistration__c
168+
WHERE Id IN :waitlistRegs
169+
];
75170

171+
for (CourseRegistration__c r : result) {
172+
System.assertEquals(
173+
'Påmeldt',
174+
r.Status__c,
175+
'Waitlisted registration should be promoted when capacity increases.'
176+
);
177+
}
76178
}
77-
}
179+
}

0 commit comments

Comments
 (0)