Unverified Commit e7e5b654 authored by Hossein Ahmadian-Yazdi's avatar Hossein Ahmadian-Yazdi Committed by GitHub

[MM-19309] Name Display Lock (#973)

* [MM-19309] Updated redux to give correct name if admin settings are on

* Update users.test.js

* Fixed some spelling errors

* update config.ts

* updated tests
parent 50fd53a0
......@@ -29,6 +29,7 @@ const Preferences: Dictionary<any> = {
NAME_NAME_FORMAT: 'name_format',
DISPLAY_PREFER_NICKNAME: 'nickname_full_name',
DISPLAY_PREFER_FULL_NAME: 'full_name',
DISPLAY_PREFER_USERNAME: 'username',
MENTION_KEYS: 'mention_keys',
USE_MILITARY_TIME: 'use_military_time',
CATEGORY_SIDEBAR_SETTINGS: 'sidebar_settings',
......
......@@ -429,49 +429,36 @@ describe('Selectors.Users', () => {
first_name: 'First',
last_name: 'Last',
};
const testUser2 = {
...user1,
id: 'test_user_id_2',
username: 'username2',
first_name: 'First2',
last_name: 'Last2',
nickname: 'nick2',
};
const newProfiles = {
...profiles,
[testUser1.id]: testUser1,
[testUser2.id]: testUser2,
};
const newTestState = {
entities: {
users: {profiles: newProfiles},
preferences: {myPreferences: {}},
general: {config: {TeammateNameDisplay: General.TEAMMATE_NAME_DISPLAY.SHOW_NICKNAME_FULLNAME}},
preferences: {
myPreferences: {
[`${Preferences.CATEGORY_DISPLAY_SETTINGS}--${Preferences.NAME_NAME_FORMAT}`]: {
value: General.TEAMMATE_NAME_DISPLAY.SHOW_FULLNAME,
},
},
},
general: {
config: {
TeammateNameDisplay: General.TEAMMATE_NAME_DISPLAY.SHOW_USERNAME,
LockTeammateNameDisplay: 'false',
}
},
},
};
// should match full name, since nickname is not present
// Should show full name since preferences is being used and LockTeammateNameDisplay is false
assert.deepEqual(Selectors.makeGetDisplayName()(newTestState, testUser1.id), 'First Last');
// should match nickname since it's present
assert.deepEqual(Selectors.makeGetDisplayName()(newTestState, testUser2.id), 'nick2');
newTestState.entities.general.config.LockTeammateNameDisplay = 'true';
newTestState.entities.general = {config: {TeammateNameDisplay: General.TEAMMATE_NAME_DISPLAY.SHOW_USERNAME}};
// should match username
// Should show username since LockTeammateNameDisplay is true
assert.deepEqual(Selectors.makeGetDisplayName()(newTestState, testUser1.id), 'username');
newTestState.entities.general = {config: {TeammateNameDisplay: General.TEAMMATE_NAME_DISPLAY.SHOW_FULLNAME}};
// should match username
assert.deepEqual(Selectors.makeGetDisplayName()(newTestState, testUser2.id), 'First2 Last2');
// // should match default name "Someone" for not found user
assert.deepEqual(Selectors.makeGetDisplayName()(newTestState, 'not_exist_id'), 'Someone');
// // should match empty string when not using default fallback
assert.deepEqual(Selectors.makeGetDisplayName()(newTestState, 'not_exist_id', false), '');
});
it('shouldShowTermsOfService', () => {
......
......@@ -489,8 +489,11 @@ export function makeGetDisplayName(): (a: GlobalState, b: $ID<UserProfile>, c: b
(state: GlobalState, userId: string) => getUser(state, userId),
getTeammateNameDisplaySetting,
(state, _, useFallbackUsername = true) => useFallbackUsername,
(user, teammateNameDisplaySetting, useFallbackUsername) => {
return displayUsername(user, teammateNameDisplaySetting!, useFallbackUsername);
getConfig,
(user, teammateNameDisplaySetting, useFallbackUsername, config) => {
const useAdminTemmateNameDisplaySetting = config.LockTeammateNameDisplay === 'true';
const adminTeammateNameDisplaySetting = config.TeammateNameDisplay;
return displayUsername(user, teammateNameDisplaySetting!, useFallbackUsername, useAdminTemmateNameDisplaySetting, adminTeammateNameDisplaySetting);
}
);
}
......@@ -122,6 +122,7 @@ export type Config = {
LdapLoginFieldName: string;
LdapNicknameAttributeSet: string;
LdapPositionAttributeSet: string;
LockTeammateNameDisplay: string;
MaxFileSize: string;
MaxNotificationsPerChannel: string;
MinimumHashtagLength: string;
......
......@@ -20,6 +20,19 @@ describe('user utils', () => {
first_name: 'test',
last_name: 'user',
};
const testUser1 = {
id: 'test_user_id',
username: 'username',
first_name: 'First',
last_name: 'Last',
};
const testUser2 = {
id: 'test_user_id_2',
username: 'username2',
first_name: 'First2',
last_name: 'Last2',
nickname: 'nick2',
};
it('should return username', () => {
assert.equal(displayUsername(userObj, 'UNKNOWN_PREFERENCE'), 'testUser');
});
......@@ -53,6 +66,53 @@ describe('user utils', () => {
let noUserObj;
assert.equal(displayUsername(noUserObj, 'UNKNOWN_PREFERENCE', false), '');
});
it('should show full name since nickname is not present and lock name display is false', () => {
assert.equal(displayUsername(testUser1, Preferences.DISPLAY_PREFER_NICKNAME, true, false, Preferences.DISPLAY_PREFER_FULL_NAME), 'First Last');
});
it('should show nickname since it is present and lock name display is false', () => {
assert.equal(displayUsername(testUser2, Preferences.DISPLAY_PREFER_NICKNAME, true, false, Preferences.DISPLAY_PREFER_FULL_NAME), 'nick2');
});
it('should show username since it is present and lock name display is false', () => {
assert.equal(displayUsername(testUser1, Preferences.DISPLAY_PREFER_USERNAME, true, false, Preferences.DISPLAY_PREFER_NICKNAME), 'username');
assert.equal(displayUsername(testUser2, Preferences.DISPLAY_PREFER_USERNAME, true, false, Preferences.DISPLAY_PREFER_NICKNAME), 'username2');
});
it('should show full name since it is present and lock name display is false', () => {
assert.equal(displayUsername(testUser1, Preferences.DISPLAY_PREFER_FULL_NAME, true, false, Preferences.DISPLAY_PREFER_NICKNAME), 'First Last');
assert.equal(displayUsername(testUser2, Preferences.DISPLAY_PREFER_FULL_NAME, true, false, Preferences.DISPLAY_PREFER_NICKNAME), 'First2 Last2');
});
it('should match default name Someone for not found user', () => {
assert.equal(displayUsername(null, Preferences.DISPLAY_PREFER_FULL_NAME, true, false, Preferences.DISPLAY_PREFER_NICKNAME), 'Someone');
});
it('should match empty string when not using default fallback', () => {
assert.equal(displayUsername(null, Preferences.DISPLAY_PREFER_FULL_NAME, false, false, Preferences.DISPLAY_PREFER_NICKNAME), '');
});
it('should match full name since nickname is not present and locking teammate name display is true', () => {
assert.equal(displayUsername(testUser1, Preferences.DISPLAY_PREFER_FULL_NAME, true, true, Preferences.DISPLAY_PREFER_NICKNAME), 'First Last');
});
it('should match nickname since it\'s present and locking teammate name display is true', () => {
assert.equal(displayUsername(testUser2, Preferences.DISPLAY_PREFER_FULL_NAME, true, true, Preferences.DISPLAY_PREFER_NICKNAME), 'nick2');
});
it('should match username since teammate name display is true', () => {
assert.equal(displayUsername(testUser1, Preferences.DISPLAY_PREFER_FULL_NAME, true, true, Preferences.DISPLAY_PREFER_USERNAME), 'username');
assert.equal(displayUsername(testUser2, Preferences.DISPLAY_PREFER_FULL_NAME, true, true, Preferences.DISPLAY_PREFER_USERNAME), 'username2');
});
it('should match full name since teammate name display is true', () => {
assert.equal(displayUsername(testUser1, Preferences.DISPLAY_PREFER_USERNAME, true, true, Preferences.DISPLAY_PREFER_FULL_NAME), 'First Last');
assert.equal(displayUsername(testUser2, Preferences.DISPLAY_PREFER_USERNAME, true, true, Preferences.DISPLAY_PREFER_FULL_NAME), 'First2 Last2');
});
});
describe('filterProfilesMatchingTerm', () => {
......
......@@ -20,13 +20,15 @@ export function displayUsername(
user: UserProfile,
teammateNameDisplay: string,
useFallbackUsername = true,
useAdminTemmateNameDisplaySetting = false,
adminTeammateNameDisplaySetting = '',
): string {
let name = useFallbackUsername ? localizeMessage('channel_loader.someone', 'Someone') : '';
const nameDisplay = useAdminTemmateNameDisplaySetting ? adminTeammateNameDisplaySetting : teammateNameDisplay;
if (user) {
if (teammateNameDisplay === Preferences.DISPLAY_PREFER_NICKNAME) {
if (nameDisplay === Preferences.DISPLAY_PREFER_NICKNAME) {
name = user.nickname || getFullName(user);
} else if (teammateNameDisplay === Preferences.DISPLAY_PREFER_FULL_NAME) {
} else if (nameDisplay === Preferences.DISPLAY_PREFER_FULL_NAME) {
name = getFullName(user);
} else {
name = user.username;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment