diff --git a/config/linters/lint-staged.config.js b/config/linters/lint-staged.config.js index f3da5a360..d6b01938a 100644 --- a/config/linters/lint-staged.config.js +++ b/config/linters/lint-staged.config.js @@ -2,7 +2,7 @@ module.exports = { 'sfdx-project.json': () => { return `npm run package:version:number:fix`; }, - '*.{apex,cls,cmp,component,css,html,js,json,md,page,trigger,xml,yaml,yml}': filenames => filenames.map(filename => `prettier --write '${filename}'`), + '*.{apex,cls,cmp,component,css,html,js,json,md,page,trigger,yaml,yml}': filenames => filenames.map(filename => `prettier --write '${filename}'`), '**/lwc/**': filenames => { return [`eslint --config ./config/linters/.eslintrc.json ${filenames.join(' ')} --fix`]; // FIXME this command should only run tests for the changed LWCs (instead of running tests for all LWCs) diff --git a/nebula-logger/core/main/logger-engine/classes/LoggerSObjectProxy.cls b/nebula-logger/core/main/logger-engine/classes/LoggerSObjectProxy.cls index ca5489f96..ec10d94cd 100644 --- a/nebula-logger/core/main/logger-engine/classes/LoggerSObjectProxy.cls +++ b/nebula-logger/core/main/logger-engine/classes/LoggerSObjectProxy.cls @@ -17,7 +17,7 @@ public without sharing class LoggerSObjectProxy { * class acts as a substitute for a `Schema.AuthSession` record to provide the abilility to mock the data during unit & integration tests. */ @SuppressWarnings('PMD.FieldNamingConventions, PMD.VariableNamingConventions') - public class AuthSession { + public class AuthSession extends SObjectProxy { public Id Id; public String LoginType; public Id LoginHistoryId; @@ -36,7 +36,7 @@ public without sharing class LoggerSObjectProxy { * class acts as a substitute for a `Schema.LoginHistory` record to provide the abilility to mock the data during unit & integration tests. */ @SuppressWarnings('PMD.FieldNamingConventions, PMD.VariableNamingConventions') - public class LoginHistory { + public class LoginHistory extends SObjectProxy { public String Application; public String Browser; public String Platform; @@ -50,7 +50,7 @@ public without sharing class LoggerSObjectProxy { * strongly-typed references to fields (properties). */ @SuppressWarnings('PMD.FieldNamingConventions, PMD.VariableNamingConventions') - public class Network { + public class Network extends SObjectProxy { public String Id; public String Name; public String UrlPathPrefix; @@ -63,7 +63,7 @@ public without sharing class LoggerSObjectProxy { * strongly-typed references to fields (properties). */ @SuppressWarnings('PMD.FieldNamingConventions, PMD.VariableNamingConventions') - public class OmniProcess { + public class OmniProcess extends SObjectProxy { public Id CreatedById; public Schema.User CreatedBy; public Datetime CreatedDate; @@ -74,4 +74,10 @@ public without sharing class LoggerSObjectProxy { public String OmniProcessType; public String UniqueName; } + + private abstract class SObjectProxy { + public String serialize() { + return System.JSON.serializePretty(this); + } + } } diff --git a/nebula-logger/core/tests/log-management/classes/LogEntryEventHandler_Tests.cls b/nebula-logger/core/tests/log-management/classes/LogEntryEventHandler_Tests.cls index 43afa2e1c..517f3aed9 100644 --- a/nebula-logger/core/tests/log-management/classes/LogEntryEventHandler_Tests.cls +++ b/nebula-logger/core/tests/log-management/classes/LogEntryEventHandler_Tests.cls @@ -644,12 +644,12 @@ private class LogEntryEventHandler_Tests { System.Assert.isFalse(LoggerParameter.QUERY_AUTH_SESSION_DATA_SYNCHRONOUSLY); MockLoggerEngineDataSelector mockSelector = new MockLoggerEngineDataSelector(); LoggerEngineDataSelector.setMock(mockSelector); - LoggerSObjectProxy.LoginHistory mockLoginHistoryProxy = new LoggerSObjectProxy.LoginHistory(null); + LoggerSObjectProxy.LoginHistory mockLoginHistoryProxy = new LoggerSObjectProxy.LoginHistory(); mockLoginHistoryProxy.Application = 'Application'; mockLoginHistoryProxy.Browser = 'Browser'; mockLoginHistoryProxy.Platform = 'Platform'; mockLoginHistoryProxy.UserId = System.UserInfo.getUserId(); - LoggerSObjectProxy.AuthSession mockAuthSessionProxy = new LoggerSObjectProxy.AuthSession(null); + LoggerSObjectProxy.AuthSession mockAuthSessionProxy = new LoggerSObjectProxy.AuthSession(); mockAuthSessionProxy.Id = LoggerMockDataCreator.createId(Schema.AuthSession.SObjectType); mockAuthSessionProxy.LoginHistory = mockLoginHistoryProxy; mockAuthSessionProxy.LoginHistoryId = LoggerMockDataCreator.createId(Schema.LoginHistory.SObjectType); diff --git a/nebula-logger/core/tests/logger-engine/classes/LogEntryEventBuilder_Tests.cls b/nebula-logger/core/tests/logger-engine/classes/LogEntryEventBuilder_Tests.cls index e79878a4c..85a96c6df 100644 --- a/nebula-logger/core/tests/logger-engine/classes/LogEntryEventBuilder_Tests.cls +++ b/nebula-logger/core/tests/logger-engine/classes/LogEntryEventBuilder_Tests.cls @@ -215,12 +215,12 @@ private class LogEntryEventBuilder_Tests { System.Assert.isTrue(LoggerParameter.QUERY_AUTH_SESSION_DATA_SYNCHRONOUSLY); MockLoggerEngineDataSelector mockSelector = new MockLoggerEngineDataSelector(); LoggerEngineDataSelector.setMock(mockSelector); - LoggerSObjectProxy.LoginHistory mockLoginHistoryProxy = new LoggerSObjectProxy.LoginHistory(null); + LoggerSObjectProxy.LoginHistory mockLoginHistoryProxy = new LoggerSObjectProxy.LoginHistory(); mockLoginHistoryProxy.Application = 'Application'; mockLoginHistoryProxy.Browser = 'Browser'; mockLoginHistoryProxy.Platform = 'Platform'; mockLoginHistoryProxy.UserId = System.UserInfo.getUserId(); - LoggerSObjectProxy.AuthSession mockAuthSessionProxy = new LoggerSObjectProxy.AuthSession(null); + LoggerSObjectProxy.AuthSession mockAuthSessionProxy = new LoggerSObjectProxy.AuthSession(); mockAuthSessionProxy.Id = LoggerMockDataCreator.createId(Schema.AuthSession.SObjectType); mockAuthSessionProxy.LoginHistory = mockLoginHistoryProxy; mockAuthSessionProxy.LoginHistoryId = LoggerMockDataCreator.createId(Schema.LoginHistory.SObjectType); @@ -1789,24 +1789,23 @@ private class LogEntryEventBuilder_Tests { } @IsTest - static void it_should_set_network_fields_for_experience_site_user_using_stub_network_record() { - String mockNetworkId = LoggerMockDataCreator.createId(Schema.Account.SObjectType); - String mockNetworkName = 'An Account record, substituting for a Network record'; - String mockNetworkUrlPrefix = 'some string value for URL prefix'; - LoggerSObjectProxy.NETWORK_FIELD_NAMES.urlPathPrefixField = Schema.Account.Description.toString(); - Schema.Account stubNetworkRecord = new Schema.Account(Description = mockNetworkUrlPrefix, Id = mockNetworkId, Name = mockNetworkName); - LogEntryEventBuilder.networkId = stubNetworkRecord.Id; + static void it_should_set_network_fields_for_experience_site_user_using_mock_network_proxy() { + LoggerSObjectProxy.Network mockNetworkProxy = new LoggerSObjectProxy.Network(); + mockNetworkProxy.Id = LoggerMockDataCreator.createId(Schema.Account.SObjectType); + mockNetworkProxy.Name = 'Some Network Name'; + mockNetworkProxy.UrlPathPrefix = 'some string value for URL prefix'; + LogEntryEventBuilder.networkId = mockNetworkProxy.Id; LoggerEngineDataSelector.useMocks(); - LoggerEngineDataSelector.mockNetworkRecords.add(stubNetworkRecord); + LoggerEngineDataSelector.mockNetworkProxies.add(mockNetworkProxy); LogEntryEvent__e logEntryEvent = new LogEntryEventBuilder(getUserSettings(), System.LoggingLevel.INFO, true).getLogEntryEvent(); - System.Assert.areEqual(mockNetworkId, logEntryEvent.NetworkId__c); - System.Assert.areEqual(mockNetworkName, logEntryEvent.NetworkName__c); - System.Assert.areEqual(System.Network.getLoginUrl(mockNetworkId), logEntryEvent.NetworkLoginUrl__c); - System.Assert.areEqual(System.Network.getLogoutUrl(mockNetworkId), logEntryEvent.NetworkLogoutUrl__c); - System.Assert.areEqual(System.Network.getSelfRegUrl(mockNetworkId), logEntryEvent.NetworkSelfRegistrationUrl__c); - System.Assert.areEqual(mockNetworkUrlPrefix, logEntryEvent.NetworkUrlPathPrefix__c); + System.Assert.areEqual(mockNetworkProxy.Id, logEntryEvent.NetworkId__c); + System.Assert.areEqual(mockNetworkProxy.Name, logEntryEvent.NetworkName__c); + System.Assert.areEqual(System.Network.getLoginUrl(mockNetworkProxy.Id), logEntryEvent.NetworkLoginUrl__c); + System.Assert.areEqual(System.Network.getLogoutUrl(mockNetworkProxy.Id), logEntryEvent.NetworkLogoutUrl__c); + System.Assert.areEqual(System.Network.getSelfRegUrl(mockNetworkProxy.Id), logEntryEvent.NetworkSelfRegistrationUrl__c); + System.Assert.areEqual(mockNetworkProxy.UrlPathPrefix, logEntryEvent.NetworkUrlPathPrefix__c); } @IsTest @@ -1815,13 +1814,17 @@ private class LogEntryEventBuilder_Tests { return; } - SObject queriedNetworkRecord = System.Database.query('SELECT Id, Name, UrlPathPrefix FROM Network LIMIT 1'); // Ideally, this type of test would run using System.runAs() and a test user setup for the queried Network record // But, setting up a test user in a test context can run into a lot of issues due to custom fields, validation rules, etc. // that have been deployed to an org. So, just use the mockNetworkRecords variable, even though this is a real Network record + SObject queriedNetworkRecord = System.Database.query('SELECT Id, Name, UrlPathPrefix FROM Network LIMIT 1'); + LoggerSObjectProxy.Network networkProxy = (LoggerSObjectProxy.Network) System.JSON.deserialize( + System.JSON.serialize(queriedNetworkRecord), + LoggerSObjectProxy.Network.class + ); LogEntryEventBuilder.networkId = queriedNetworkRecord.Id; LoggerEngineDataSelector.useMocks(); - LoggerEngineDataSelector.mockNetworkRecords.add(queriedNetworkRecord); + LoggerEngineDataSelector.mockNetworkProxies.add(networkProxy); LogEntryEvent__e logEntryEvent = new LogEntryEventBuilder(getUserSettings(), System.LoggingLevel.INFO, true).getLogEntryEvent(); diff --git a/nebula-logger/core/tests/logger-engine/classes/LoggerSObjectProxy_Tests.cls b/nebula-logger/core/tests/logger-engine/classes/LoggerSObjectProxy_Tests.cls index 1e1510a18..59323dda8 100644 --- a/nebula-logger/core/tests/logger-engine/classes/LoggerSObjectProxy_Tests.cls +++ b/nebula-logger/core/tests/logger-engine/classes/LoggerSObjectProxy_Tests.cls @@ -9,6 +9,21 @@ private class LoggerSObjectProxy_Tests { private static final Boolean IS_EXPERIENCE_CLOUD_ENABLED = System.Type.forName('Schema.Network') != null; private static final Boolean IS_OMNISTUDIO_ENABLED = System.Type.forName('Schema.OmniProcess') != null; + @IsTest + static void it_supports_constructing_proxy_classes() { + // This test is a bit silly, it's primarily here because the other tests uses JSON.deserialize(), + // which doesn't provided code coverage + LoggerSObjectProxy.AuthSession authSessionProxy = new LoggerSObjectProxy.AuthSession(); + LoggerSObjectProxy.LoginHistory loginHistoryProxy = new LoggerSObjectProxy.LoginHistory(); + LoggerSObjectProxy.Network networkProxy = new LoggerSObjectProxy.Network(); + LoggerSObjectProxy.OmniProcess omniProcessProxy = new LoggerSObjectProxy.OmniProcess(); + + System.Assert.areEqual(System.JSON.serializePretty(authSessionProxy), authSessionProxy.serialize()); + System.Assert.areEqual(System.JSON.serializePretty(loginHistoryProxy), loginHistoryProxy.serialize()); + System.Assert.areEqual(System.JSON.serializePretty(networkProxy), networkProxy.serialize()); + System.Assert.areEqual(System.JSON.serializePretty(omniProcessProxy), omniProcessProxy.serialize()); + } + @IsTest static void it_correctly_deserializes_auth_session_records_to_proxies() { List authSessionRecords = [ @@ -39,10 +54,10 @@ private class LoggerSObjectProxy_Tests { for (Integer i = 0; i < authSessionRecords.size(); i++) { Schema.AuthSession authSessionRecord = authSessionRecords.get(i); LoggerSObjectProxy.AuthSession authSessionProxy = authSessionProxies.get(i); - System.Assert.areEqual(authSessionRecord.LoginHistory.Application, authSessionProxy.LoginHistory.Application); - System.Assert.areEqual(authSessionRecord.LoginHistory.Browser, authSessionProxy.LoginHistory.Browser); - System.Assert.areEqual(authSessionRecord.LoginHistory.Platform, authSessionProxy.LoginHistory.Platform); - System.Assert.areEqual(authSessionRecord.LoginHistory.UserId, authSessionProxy.LoginHistory.UserId); + System.Assert.areEqual(authSessionRecord.LoginHistory?.Application, authSessionProxy.LoginHistory?.Application); + System.Assert.areEqual(authSessionRecord.LoginHistory?.Browser, authSessionProxy.LoginHistory?.Browser); + System.Assert.areEqual(authSessionRecord.LoginHistory?.Platform, authSessionProxy.LoginHistory?.Platform); + System.Assert.areEqual(authSessionRecord.LoginHistory?.UserId, authSessionProxy.LoginHistory?.UserId); System.Assert.areEqual(authSessionRecord.LoginHistoryId, authSessionProxy.LoginHistoryId); System.Assert.areEqual(authSessionRecord.LoginType, authSessionProxy.LoginType); System.Assert.areEqual(authSessionRecord.LogoutUrl, authSessionProxy.LogoutUrl); diff --git a/package.json b/package.json index d803f002e..ef0581f5e 100644 --- a/package.json +++ b/package.json @@ -54,8 +54,8 @@ "plugin:version:create:logger-admin-dashboard": "sf package version create --json --package \"Nebula Logger - Core Plugin - Logger Admin Dashboard\" --skip-ancestor-check --code-coverage --installation-key-bypass --wait 30", "plugin:version:create:slack": "sf package version create --json --package \"Nebula Logger - Core Plugin - Slack\" --skip-ancestor-check --code-coverage --installation-key-bypass --wait 30", "prepare": "husky install && chmod +x ./.husky/pre-commit", - "prettier:fix": "prettier --write \"**/*.{cls,cmp,component,css,html,js,json,md,page,trigger,xml,yaml,yml}\"", - "prettier:verify": "prettier --list-different \"**/*.{cls,cmp,component,css,html,js,json,md,page,trigger,xml,yaml,yml}\"", + "prettier:fix": "prettier --write \"**/*.{cls,cmp,component,css,html,js,json,md,page,trigger,yaml,yml}\"", + "prettier:verify": "prettier --list-different \"**/*.{cls,cmp,component,css,html,js,json,md,page,trigger,yaml,yml}\"", "scan:fix:lwc": "eslint --config ./config/linters/.eslintrc.json **/lwc/** --fix", "scan": "npm run scan:apex && npm run scan:lwc", "scan:apex": "sf scanner:run --pmdconfig ./config/linters/pmd-ruleset.xml --target ./nebula-logger/ --engine pmd --severity-threshold 3",