-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmailTestController.apxc
More file actions
322 lines (276 loc) · 9.23 KB
/
EmailTestController.apxc
File metadata and controls
322 lines (276 loc) · 9.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
@IsTest
private class EmailTestControllerTest {
private static final String CONTACT_EMAIL = 'test@email.com';
private static final String MISSING_EMAIL = 'missing@email.com';
// ---------------------------------------------------------------------
// Test Data Setup
// ---------------------------------------------------------------------
@testSetup
static void setupTestData() {
Contact testContact = new Contact(
FirstName = 'Test',
LastName = 'Contact',
Email = CONTACT_EMAIL
);
insert testContact;
Account testAccount = new Account(
Name = 'Test Account'
);
insert testAccount;
Opportunity testOpportunity = new Opportunity(
Name = 'Test Opportunity',
StageName = 'Prospecting',
CloseDate = Date.today().addDays(30),
AccountId = testAccount.Id
);
insert testOpportunity;
Case testCase = new Case(
Subject = 'Test Case Subject',
Status = 'New',
Origin = 'Phone',
ContactId = testContact.Id,
AccountId = testAccount.Id
);
insert testCase;
User runAsUser = [
SELECT Id
FROM User
WHERE IsActive = true
LIMIT 1
];
System.runAs(runAsUser) {
insert new EmailTemplate(
Name = 'Contact Lightning Template',
DeveloperName = 'Contact_Lightning_Template',
Subject = 'Hello World',
HtmlValue = '<html><body>Hello World</body></html>',
FolderId = UserInfo.getUserId(),
TemplateType = 'custom',
UiType = 'SFX',
RelatedEntityType = 'Contact',
IsActive = true
);
insert new EmailTemplate(
Name = 'Case Lightning Template',
DeveloperName = 'Case_Lightning_Template',
Subject = 'Case Update',
HtmlValue = '<html><body>Case Update</body></html>',
FolderId = UserInfo.getUserId(),
TemplateType = 'custom',
UiType = 'SFX',
RelatedEntityType = 'Case',
IsActive = true
);
}
}
// ---------------------------------------------------------------------
// Template Retrieval
// ---------------------------------------------------------------------
@IsTest
static void testGetEmailTemplates() {
Test.startTest();
List<OptionWrapper> templates =
EmailTestController.getEmailTemplates();
Test.stopTest();
System.assertNotEquals(
null,
templates,
'Templates list should not be null.'
);
System.assert(
templates.size() >= 2,
'Expected Lightning email templates to be returned.'
);
Set<String> labels = new Set<String>();
for (OptionWrapper wrapper : templates) {
labels.add(wrapper.label);
}
System.assert(
labels.contains('Contact Lightning Template'),
'Contact template should be returned.'
);
System.assert(
labels.contains('Case Lightning Template'),
'Case template should be returned.'
);
}
// ---------------------------------------------------------------------
// Template Target Object
// ---------------------------------------------------------------------
@IsTest
static void testGetTargetObjectTypeForCaseTemplate() {
EmailTemplate caseTemplate = [
SELECT Id
FROM EmailTemplate
WHERE DeveloperName = 'Case_Lightning_Template'
LIMIT 1
];
Test.startTest();
String relatedType =
EmailTestController.getTargetObjectType(caseTemplate.Id);
Test.stopTest();
System.assertEquals(
'Case',
relatedType,
'RelatedEntityType should be Case.'
);
}
// ---------------------------------------------------------------------
// Record Retrieval
// ---------------------------------------------------------------------
@IsTest
static void testGetRecordsForOpportunityUsesName() {
Test.startTest();
List<OptionWrapper> records =
EmailTestController.getRecords('Opportunity');
Test.stopTest();
System.assert(
records.size() > 0,
'Opportunity records should be returned.'
);
System.assertEquals(
'Test Opportunity',
records[0].label,
'Opportunity should use Name as the display label.'
);
}
@IsTest
static void testGetRecordsForCaseUsesCaseNumber() {
Case existingCase = [
SELECT CaseNumber
FROM Case
LIMIT 1
];
Test.startTest();
List<OptionWrapper> records =
EmailTestController.getRecords('Case');
Test.stopTest();
System.assert(
records.size() > 0,
'Case records should be returned.'
);
System.assertEquals(
existingCase.CaseNumber,
records[0].label,
'Case should use CaseNumber as the display label.'
);
}
@IsTest
static void testGetRecordsUnsupportedObjectThrowsHandledException() {
Boolean exceptionThrown = false;
Test.startTest();
try {
EmailTestController.getRecords('DefinitelyNotAnObject');
} catch (AuraHandledException ex) {
exceptionThrown = true;
System.assert(
ex.getMessage().contains('Unsupported object type'),
'Expected unsupported object type message.'
);
}
Test.stopTest();
System.assertEquals(
true,
exceptionThrown,
'An AuraHandledException should have been thrown.'
);
}
// ---------------------------------------------------------------------
// Send Test Email – Contact
// ---------------------------------------------------------------------
@IsTest
static void testSendTestEmailToContactRecord() {
EmailTemplate contactTemplate = [
SELECT Id
FROM EmailTemplate
WHERE DeveloperName = 'Contact_Lightning_Template'
LIMIT 1
];
Contact testContact = [
SELECT Id, Email
FROM Contact
WHERE Email = :CONTACT_EMAIL
LIMIT 1
];
Test.startTest();
EmailTestController.sendTestEmail(
contactTemplate.Id,
testContact.Id,
testContact.Email
);
Integer emailInvocations = Limits.getEmailInvocations();
Test.stopTest();
System.assertEquals(
1,
emailInvocations,
'One email invocation should occur for Contact send.'
);
}
// ---------------------------------------------------------------------
// Send Test Email – Case
// ---------------------------------------------------------------------
@IsTest
static void testSendTestEmailToCaseRecord() {
EmailTemplate caseTemplate = [
SELECT Id
FROM EmailTemplate
WHERE DeveloperName = 'Case_Lightning_Template'
LIMIT 1
];
Case testCase = [
SELECT Id
FROM Case
LIMIT 1
];
Test.startTest();
EmailTestController.sendTestEmail(
caseTemplate.Id,
testCase.Id,
CONTACT_EMAIL
);
Integer emailInvocations = Limits.getEmailInvocations();
Test.stopTest();
System.assertEquals(
1,
emailInvocations,
'One email invocation should occur for Case send.'
);
}
@IsTest
static void testSendTestEmailToCaseWithoutMatchingContactThrowsHandledException() {
EmailTemplate caseTemplate = [
SELECT Id
FROM EmailTemplate
WHERE DeveloperName = 'Case_Lightning_Template'
LIMIT 1
];
Case testCase = [
SELECT Id
FROM Case
LIMIT 1
];
Boolean exceptionThrown = false;
Test.startTest();
try {
EmailTestController.sendTestEmail(
caseTemplate.Id,
testCase.Id,
MISSING_EMAIL
);
} catch (AuraHandledException ex) {
exceptionThrown = true;
System.assert(
ex.getMessage().contains(
'A valid Contact with the given email address is required'
),
'Expected missing Contact message.'
);
}
Test.stopTest();
System.assertEquals(
true,
exceptionThrown,
'An AuraHandledException should be thrown when no matching Contact exists.'
);
}
}