124 lines
4.7 KiB
OpenEdge ABL
124 lines
4.7 KiB
OpenEdge ABL
@isTest
|
|
private class ActiveUserCountTrigger_Test {
|
|
|
|
@isTest
|
|
static void testInsertActiveUser() {
|
|
// Count active users before inserting a new one
|
|
Integer beforeCount = [SELECT COUNT() FROM User WHERE IsActive = true];
|
|
|
|
Profile p = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1];
|
|
|
|
Test.startTest();
|
|
User testUser = new User(
|
|
FirstName = 'Test',
|
|
LastName = 'ActiveCount',
|
|
Email = 'testactivecount@test.hicglobal.com',
|
|
Username = 'testactivecount' + System.currentTimeMillis() + '@test.hicglobal.com',
|
|
Alias = 'tacnt',
|
|
TimeZoneSidKey = 'Asia/Kolkata',
|
|
LocaleSidKey = 'en_IN',
|
|
EmailEncodingKey = 'UTF-8',
|
|
LanguageLocaleKey = 'en_US',
|
|
ProfileId = p.Id,
|
|
IsActive = true
|
|
);
|
|
insert testUser;
|
|
Test.stopTest();
|
|
|
|
// After stopTest the @future has executed.
|
|
// We just verify the handler ran without error and the count is correct.
|
|
Integer afterCount = [SELECT COUNT() FROM User WHERE IsActive = true];
|
|
System.assert(afterCount >= beforeCount, 'Active user count should be equal or greater after insert');
|
|
}
|
|
|
|
@isTest
|
|
static void testDeactivateUser() {
|
|
Profile p = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1];
|
|
|
|
User testUser = new User(
|
|
FirstName = 'Test',
|
|
LastName = 'Deactivate',
|
|
Email = 'testdeactivate@test.hicglobal.com',
|
|
Username = 'testdeactivate' + System.currentTimeMillis() + '@test.hicglobal.com',
|
|
Alias = 'tdeac',
|
|
TimeZoneSidKey = 'Asia/Kolkata',
|
|
LocaleSidKey = 'en_IN',
|
|
EmailEncodingKey = 'UTF-8',
|
|
LanguageLocaleKey = 'en_US',
|
|
ProfileId = p.Id,
|
|
IsActive = true
|
|
);
|
|
insert testUser;
|
|
|
|
Integer beforeDeactivate = [SELECT COUNT() FROM User WHERE IsActive = true];
|
|
|
|
Test.startTest();
|
|
testUser.IsActive = false;
|
|
update testUser;
|
|
Test.stopTest();
|
|
|
|
Integer afterDeactivate = [SELECT COUNT() FROM User WHERE IsActive = true];
|
|
System.assertEquals(beforeDeactivate - 1, afterDeactivate,
|
|
'Active user count should decrease by 1 after deactivation');
|
|
}
|
|
|
|
@isTest
|
|
static void testNoChangeDoesNotFire() {
|
|
// Updating a user without changing IsActive should NOT trigger the recount
|
|
Profile p = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1];
|
|
|
|
User testUser = new User(
|
|
FirstName = 'Test',
|
|
LastName = 'NoChange',
|
|
Email = 'testnochange@test.hicglobal.com',
|
|
Username = 'testnochange' + System.currentTimeMillis() + '@test.hicglobal.com',
|
|
Alias = 'tnoch',
|
|
TimeZoneSidKey = 'Asia/Kolkata',
|
|
LocaleSidKey = 'en_IN',
|
|
EmailEncodingKey = 'UTF-8',
|
|
LanguageLocaleKey = 'en_US',
|
|
ProfileId = p.Id,
|
|
IsActive = true
|
|
);
|
|
insert testUser;
|
|
|
|
Test.startTest();
|
|
testUser.LastName = 'NoChangeUpdated';
|
|
update testUser;
|
|
Test.stopTest();
|
|
|
|
// If we get here without error, the trigger correctly skipped
|
|
System.assert(true, 'Trigger should not fire when IsActive does not change');
|
|
}
|
|
|
|
@isTest
|
|
static void testProfileChange() {
|
|
// Changing a user's profile should trigger recount
|
|
List<Profile> profiles = [SELECT Id FROM Profile WHERE UserLicense.Name = 'Salesforce' LIMIT 2];
|
|
// Need at least 2 Salesforce-licensed profiles to test a switch
|
|
if (profiles.size() < 2) return;
|
|
|
|
User testUser = new User(
|
|
FirstName = 'Test',
|
|
LastName = 'ProfileSwitch',
|
|
Email = 'testprofileswitch@test.hicglobal.com',
|
|
Username = 'testprofileswitch' + System.currentTimeMillis() + '@test.hicglobal.com',
|
|
Alias = 'tprof',
|
|
TimeZoneSidKey = 'Asia/Kolkata',
|
|
LocaleSidKey = 'en_IN',
|
|
EmailEncodingKey = 'UTF-8',
|
|
LanguageLocaleKey = 'en_US',
|
|
ProfileId = profiles[0].Id,
|
|
IsActive = true
|
|
);
|
|
insert testUser;
|
|
|
|
Test.startTest();
|
|
testUser.ProfileId = profiles[1].Id;
|
|
update testUser;
|
|
Test.stopTest();
|
|
|
|
// If we get here without error, the trigger handled the profile change
|
|
System.assert(true, 'Trigger should fire when ProfileId changes for Salesforce-licensed user');
|
|
}
|
|
} |