Skip to content

Commit

Permalink
Fixes #271 - Updated LoggerTestUtils to lazy-load standard user profi…
Browse files Browse the repository at this point in the history
…le, using all possible translated names to ensure it works, regardless of the current user's specified language
  • Loading branch information
jongpie committed Jan 18, 2022
1 parent f190ab1 commit 031d6bf
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 5 deletions.
4 changes: 2 additions & 2 deletions config/scripts/build/generate-lwc-docs.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This script is used to generate the markdown files used by Github pages for lightning component jsdocs
npx jsdoc2md nebula-logger/main/logger-engine/lwc/logger/logger.js > ./docs/lightning-components/Logger.md
npx jsdoc2md nebula-logger/main/logger-engine/lwc/logger/logEntryBuilder.js > ./docs/lightning-components/LogEntryBuilder.md
npx jsdoc2md nebula-logger/core/main/logger-engine/lwc/logger/logger.js > ./docs/lightning-components/Logger.md
npx jsdoc2md nebula-logger/core/main/logger-engine/lwc/logger/logEntryBuilder.js > ./docs/lightning-components/LogEntryBuilder.md

$docsSubdirectories = "docs/lightning-components/*.*"
foreach($file in Get-ChildItem $docsSubdirectories) {
Expand Down
71 changes: 68 additions & 3 deletions nebula-logger/core/tests/common/classes/LoggerTestUtils.cls
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
// See LICENSE file or go to https://github.com/jongpie/NebulaLogger for full license details. //
//------------------------------------------------------------------------------------------------//

@SuppressWarnings('PMD.PropertyNamingConventions')
@SuppressWarnings('PMD.MethodNamingConventions, PMD.PropertyNamingConventions')
@IsTest
public class LoggerTestUtils {
private static final Map<Schema.SObjectType, Integer> SOBJECT_TYPE_TO_MOCK_ID_COUNT = new Map<Schema.SObjectType, Integer>();
private static final Profile STANDARD_USER_PROFILE = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1];

// Lazy-load & cache permission sets
// Lazy-load & cache permission sets & profiles
private static final Map<String, PermissionSet> PERMISSION_SETS_BY_NAME {
get {
if (PERMISSION_SETS_BY_NAME == null) {
Expand Down Expand Up @@ -60,6 +59,16 @@ public class LoggerTestUtils {
private set;
}

private static final Profile STANDARD_USER_PROFILE {
get {
if (STANDARD_USER_PROFILE == null) {
STANDARD_USER_PROFILE = queryStandardUserProfile();
}
return STANDARD_USER_PROFILE;
}
private set;
}

/**
* @description Assigns the permission set `LoggerAdmin` to the specified user ID
* @param userId The ID of the user that should be assigned the permission set
Expand Down Expand Up @@ -228,4 +237,60 @@ public class LoggerTestUtils {
}
return results;
}

private static Profile queryStandardUserProfile() {
// The Profile object does not have a DeveloperName field, and tandard profile names are (annoyingly) translated
// to the current user's language - to ensure that this // query works for all languages, the translated names
// have been included for these languages:
// 'en_US', 'de', 'es', 'fr', 'it', 'ja', 'sv', 'ko', 'zh_TW', 'zh_CN', 'pt_BR', 'nl_NL', 'da', 'th', 'fi', 'ru', 'es_MX', 'no'
List<String> translatedProfileNames = new List<String>{
'Standard User',
'Standardbenutzer',
'Usuario estándar',
'Utilisateur standard',
'Utente standard',
'標準ユーザ',
'Standardanvändare',
'표준 사용자',
'標準使用者',
'标准用户',
'Usuário Padrão',
'Standaardgebruiker',
'Standardbruger',
'ผู้ใช้มาตรฐาน',
'Vakiokäyttäjä',
'Стандартный пользователь',
'Usuario estándar',
'Standardbruker'
};
return [SELECT Id FROM Profile WHERE Name IN :translatedProfileNames LIMIT 1];
}

// Actual test methods for LoggerTestUtils - the goal of this class is to be a utility class for other test classes,
// but this test ensures that this utility class is working as expected. Yes, this does DML in a for-loop, which is normally
// HORRIBLE to do, but in this case, it's the only way to ensure that the current user's language has been updated
// to ensure that the profile's name is using the expected language
@SuppressWarnings('PMD.OperationWithLimitsInLoop')
@IsTest
static void it_should_return_standard_user_profile_in_all_languages() {
User currentUser = new User(Id = UserInfo.getUserId(), LanguageLocaleKey = 'en_US');
update currentUser;
Profile expectedProfile;
// Using 'en_US', get the expected profile using the name 'Standard Profile'
System.runAs(currentUser) {
currentUser = [SELECT Id, LanguageLocaleKey FROM User WHERE Id = :UserInfo.getUserId()];
System.assertEquals('en_US', currentUser.LanguageLocaleKey);
expectedProfile = [SELECT Id, Name FROM Profile WHERE Name = 'Standard User'];
}
// Test that each value of LanguageLocaleKey returns a value for STANDARD_USER_PROFILE
for (PicklistEntry picklistEntry : Schema.User.LanguageLocaleKey.getDescribe().getPickListValues()) {
User user = new User(Id = UserInfo.getUserId(), LanguageLocaleKey = picklistEntry.getValue());
update user;
System.runAs(user) {
currentUser = [SELECT Id, LanguageLocaleKey FROM User WHERE Id = :UserInfo.getUserId()];
System.assertEquals(picklistEntry.getValue(), currentUser.LanguageLocaleKey);
System.assertEquals(expectedProfile.Id, STANDARD_USER_PROFILE.Id);
}
}
}
}

0 comments on commit 031d6bf

Please sign in to comment.