diff --git a/src/System Application/App/Feature Key/src/FeatureManagementFacade.Codeunit.al b/src/System Application/App/Feature Key/src/FeatureManagementFacade.Codeunit.al index 1dd2316029..849b29a924 100644 --- a/src/System Application/App/Feature Key/src/FeatureManagementFacade.Codeunit.al +++ b/src/System Application/App/Feature Key/src/FeatureManagementFacade.Codeunit.al @@ -25,6 +25,17 @@ codeunit 2611 "Feature Management Facade" exit(FeatureManagementImpl.IsEnabled(FeatureId)); end; + /// + /// Returns true if the feature is enabled and data update, if required, is complete. + /// + /// the feature id in the system table "Feature Key" + /// specifies if inserts are allowed while checking for feature being enabled + /// if the feature is fully enabled + procedure IsEnabled(FeatureId: Text[50]; AllowInsert: Boolean): Boolean; + begin + exit(FeatureManagementImpl.IsEnabled(FeatureId, AllowInsert)); + end; + /// /// Updates the status in "Feature Data Update Status" records related to all companies. /// Also sends the notification reminding user to sign in again after feature is enabled/disabled. diff --git a/src/System Application/App/Feature Key/src/FeatureManagementImpl.Codeunit.al b/src/System Application/App/Feature Key/src/FeatureManagementImpl.Codeunit.al index 59f51bde16..a926642324 100644 --- a/src/System Application/App/Feature Key/src/FeatureManagementImpl.Codeunit.al +++ b/src/System Application/App/Feature Key/src/FeatureManagementImpl.Codeunit.al @@ -81,7 +81,7 @@ codeunit 2610 "Feature Management Impl." /// /// Inserts record to "Feature Data Update Status" table to show the feature status per company. /// - local procedure InitializeFeatureDataUpdateStatus(FeatureKey: Record "Feature Key"; var FeatureDataUpdateStatus: Record "Feature Data Update Status") + local procedure InitializeFeatureDataUpdateStatus(FeatureKey: Record "Feature Key"; var FeatureDataUpdateStatus: Record "Feature Data Update Status"; AllowInsert: Boolean) var FeatureManagementFacade: Codeunit "Feature Management Facade"; InitializeHandled: Boolean; @@ -106,7 +106,8 @@ codeunit 2610 "Feature Management Impl." end; // If the table extension is not in sync during upgrade then Get() always returns False, // so the following insert will fail if the record does exist. - if FeatureDataUpdateStatus.Insert() then; + if AllowInsert then + if FeatureDataUpdateStatus.Insert() then; end; /// @@ -162,7 +163,7 @@ codeunit 2610 "Feature Management Impl." if FeatureDataUpdateStatus."Background Task" then UpdateBackgroundTaskStatus(FeatureDataUpdateStatus); end else - InitializeFeatureDataUpdateStatus(FeatureKey, FeatureDataUpdateStatus); + InitializeFeatureDataUpdateStatus(FeatureKey, FeatureDataUpdateStatus, true); end; /// @@ -322,15 +323,26 @@ codeunit 2610 "Feature Management Impl." /// /// Returns true if the feature is enabled and data update, if required, is complete. /// - /// the feature id in the system table "Feature Key" - /// if the feature is fully enabled + /// The feature id in the system table "Feature Key" + /// If the feature is fully enabled procedure IsEnabled(FeatureId: Text[50]): Boolean; + begin + exit(IsEnabled(FeatureId, true)); + end; + + /// + /// Returns true if the feature is enabled and data update, if required, is complete. + /// + /// The feature id in the system table "Feature Key" + /// Specifies if inserts are allowed while checking for feature being enabled + /// If the feature is fully enabled + procedure IsEnabled(FeatureId: Text[50]; AllowInsert: Boolean): Boolean; var FeatureKey: Record "Feature Key"; FeatureDataUpdateStatus: Record "Feature Data Update Status"; begin if FeatureKey.Get(FeatureId) then begin - InitializeFeatureDataUpdateStatus(FeatureKey, FeatureDataUpdateStatus); + InitializeFeatureDataUpdateStatus(FeatureKey, FeatureDataUpdateStatus, AllowInsert); exit(FeatureDataUpdateStatus."Feature Status" in ["Feature Status"::Complete, "Feature Status"::Enabled]) end; end; diff --git a/src/System Application/Test/Feature Key/src/FeatureKeyTest.Codeunit.al b/src/System Application/Test/Feature Key/src/FeatureKeyTest.Codeunit.al index ff40e16958..e4322ba1db 100644 --- a/src/System Application/Test/Feature Key/src/FeatureKeyTest.Codeunit.al +++ b/src/System Application/Test/Feature Key/src/FeatureKeyTest.Codeunit.al @@ -503,6 +503,63 @@ codeunit 135003 "Feature Key Test" asserterror error('') // roll back end; + [Test] + procedure T108_IsEnabledAllowInsertTrue() + var + FeatureDataUpdateStatus: Record "Feature Data Update Status"; + Enabled: Option "None","All Users"; + ID: Text[50]; + begin + PermissionsMock.Set('Feature Key Admin'); + Initialize(); + ID := 'SalesPrices'; + // [GIVEN] Feature Key 'X', where "Enabled" is 'All Users' with data update required + SetFeatureEnabled(ID, Enabled::"All Users"); + + // [GIVEN] FeatureDataUpdateStatus for Company 'A' does not exist + FeatureDataUpdateStatus.DeleteAll(); + + // [WHEN] run IsEnabled() with AllowInsert = true + // [THEN] result is False, because it requires data update + Assert.IsFalse(FeatureManagementFacade.IsEnabled(ID, true), 'should be disabled'); + + // [THEN] FeatureDataUpdateStatus for Company 'A' is created + FeatureDataUpdateStatus.SetRange("Feature Key", ID); + FeatureDataUpdateStatus.SetRange("Company Name", CopyStr(CompanyName(), 1, MaxStrLen(FeatureDataUpdateStatus."Company Name"))); + Assert.RecordIsNotEmpty(FeatureDataUpdateStatus); + + asserterror Error('') // roll back + end; + + [Test] + procedure T109_IsEnabledAllowInsertFalse() + var + FeatureDataUpdateStatus: Record "Feature Data Update Status"; + Enabled: Option "None","All Users"; + ID: Text[50]; + begin + PermissionsMock.Set('Feature Key Admin'); + Initialize(); + ID := 'SalesPrices'; + // [GIVEN] Feature Key 'X', where "Enabled" is 'All Users' with data update required + SetFeatureEnabled(ID, Enabled::"All Users"); + + // [GIVEN] FeatureDataUpdateStatus for Company 'A' does not exist + FeatureDataUpdateStatus.DeleteAll(); + + // [WHEN] run IsEnabled() with AllowInsert = false + // [THEN] result is False, because it requires data update + Assert.IsFalse(FeatureManagementFacade.IsEnabled(ID, false), 'should be disabled'); + + // [THEN] FeatureDataUpdateStatus for Company 'A' is not created + FeatureDataUpdateStatus.SetRange("Feature Key", ID); + FeatureDataUpdateStatus.SetRange("Company Name", CopyStr(CompanyName(), 1, MaxStrLen(FeatureDataUpdateStatus."Company Name"))); + Assert.RecordIsEmpty(FeatureDataUpdateStatus); + + asserterror Error('') // roll back + end; + + local procedure Initialize() begin LibraryVariableStorage.Clear(); @@ -528,12 +585,9 @@ codeunit 135003 "Feature Key Test" local procedure MockFeatureStatus(ID: Text[50]; Enabled: Option "None","All Users"; Status: Enum "Feature Status") var - FeatureKey: Record "Feature Key"; FeatureDataUpdateStatus: Record "Feature Data Update Status"; begin - FeatureKey.Get(ID); - FeatureKey.Enabled := Enabled; - FeatureKey.Modify(); + SetFeatureEnabled(ID, Enabled); FeatureDataUpdateStatus.DeleteAll(); FeatureDataUpdateStatus."Feature Key" := ID; @@ -543,6 +597,15 @@ codeunit 135003 "Feature Key Test" FeatureDataUpdateStatus.Insert(); end; + local procedure SetFeatureEnabled(ID: Text[50]; Enabled: Option "None","All Users") + var + FeatureKey: Record "Feature Key"; + begin + FeatureKey.Get(ID); + FeatureKey.Enabled := Enabled; + FeatureKey.Modify(); + end; + local procedure MockStatusInAnotherCompany(ID: Text[50]; DataUpdateRequired: Boolean; FeatureStatus: Enum "Feature Status") Name: Text[30] var FeatureDataUpdateStatus: Record "Feature Data Update Status";