Files
Umbraco-CMS/tests/Umbraco.Tests.UnitTests/Umbraco.Core/Services/SystemInformationServiceTests.cs
Mole 859505e751 Models builder: Move InMemoryAuto models builder and razor runtime compilation into its own package to enable hot reload (#20187)
* Move in memory models builder out of core

* Move runtime validations into backoffice development project

* Obsolete ModelsMode enum

* Move the InMemoryModelsbuilder/RRC novel into the Backoffice development umbraco builder extension

* Add runtime validator to warn if InMemoryAuto is selected but the package isn't installed

* Add backoffice development to template

* Remove propertyGroup

* Remove oopsie

* Check for modelsbuilder in notification handler instead of runtime validator

* Update src/Umbraco.Cms.Api.Management/Controllers/ModelsBuilder/BuildModelsBuilderController.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update src/Umbraco.Infrastructure/Runtime/RuntimeModeValidators/ModelsBuilderModeValidator.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Remove ModelsMode enum and ModelsModeExtensions

* Apply suggestions from code review

Co-authored-by: Kenn Jacobsen <kja@umbraco.dk>

* Move project to source folder

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Kenn Jacobsen <kja@umbraco.dk>
2025-09-23 11:58:09 +02:00

173 lines
6.5 KiB
C#

using System.Globalization;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Moq;
using NPoco;
using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Configuration;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Semver;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Sync;
using Umbraco.Cms.DevelopmentMode.Backoffice.InMemoryAuto;
using Umbraco.Cms.Infrastructure.Persistence;
using Umbraco.Cms.Infrastructure.Telemetry.Providers;
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Services;
[TestFixture]
public class SystemInformationServiceTests
{
[OneTimeSetUp]
public void CreateMocks() => CreateUmbracoVersion(9, 0, 0);
private IUmbracoVersion _umbracoVersion;
[Test]
[TestCase("en-US")]
[TestCase("de-DE")]
[TestCase("en-NZ")]
[TestCase("sv-SE")]
public void GetCorrectDefaultLanguageTest(string culture)
{
var userDataService = CreateSystemInformationService(culture);
var defaultLanguage = userDataService.GetTroubleshootingInformation().FirstOrDefault(x => x.Key == "Default Language");
Assert.Multiple(() =>
{
Assert.IsNotNull(defaultLanguage);
Assert.AreEqual(culture, defaultLanguage.Value);
});
}
[Test]
[TestCase("en-US")]
[TestCase("de-DE")]
[TestCase("en-NZ")]
[TestCase("sv-SE")]
public void GetCorrectCultureTest(string culture)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
var userDataService = CreateSystemInformationService(culture);
var currentCulture = userDataService.GetTroubleshootingInformation().FirstOrDefault(x => x.Key == "Current Culture");
Assert.Multiple(() =>
{
Assert.IsNotNull(currentCulture);
Assert.AreEqual(culture, currentCulture.Value);
});
}
[Test]
[TestCase("en-US")]
[TestCase("de-DE")]
[TestCase("en-NZ")]
[TestCase("sv-SE")]
public void GetCorrectUICultureTest(string culture)
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
var userDataService = CreateSystemInformationService(culture);
var currentCulture = userDataService.GetTroubleshootingInformation().FirstOrDefault(x => x.Key == "Current UI Culture");
Assert.Multiple(() =>
{
Assert.IsNotNull(currentCulture);
Assert.AreEqual(culture, currentCulture.Value);
});
}
[Test]
[TestCase("en-US")]
[TestCase("de-DE")]
[TestCase("en-NZ")]
[TestCase("sv-SE")]
public void RunTimeInformationNotNullTest(string culture)
{
var userDataService = CreateSystemInformationService(culture);
var userData = userDataService.GetTroubleshootingInformation().ToList();
Assert.Multiple(() =>
{
Assert.IsNotNull(userData.Select(x => x.Key == "Server OS"));
Assert.IsNotNull(userData.Select(x => x.Key == "Server Framework"));
Assert.IsNotNull(userData.Select(x => x.Key == "Current Webserver"));
});
}
[Test]
[TestCase(Constants.ModelsBuilder.ModelsModes.Nothing)]
[TestCase(ModelsModeConstants.InMemoryAuto)]
[TestCase(Constants.ModelsBuilder.ModelsModes.SourceCodeAuto)]
[TestCase(Constants.ModelsBuilder.ModelsModes.SourceCodeManual)]
public void ReportsModelsModeCorrectly(string modelsMode)
{
var userDataService = CreateSystemInformationService(modelsMode: modelsMode);
var userData = userDataService.GetTroubleshootingInformation().ToArray();
var actual = userData.FirstOrDefault(x => x.Key == "Models Builder Mode");
Assert.IsNotNull(actual.Value);
Assert.AreEqual(modelsMode, actual.Value);
}
[Test]
[TestCase(RuntimeMode.BackofficeDevelopment)]
[TestCase(RuntimeMode.Development)]
[TestCase(RuntimeMode.Production)]
public void ReportsRuntimeModeCorrectly(RuntimeMode runtimeMode)
{
var userDataService = CreateSystemInformationService(runtimeMode: runtimeMode);
var userData = userDataService.GetTroubleshootingInformation().ToArray();
var actual = userData.FirstOrDefault(x => x.Key == "Runtime Mode");
Assert.IsNotNull(actual.Value);
Assert.AreEqual(runtimeMode.ToString(), actual.Value);
}
[Test]
[TestCase(true)]
[TestCase(false)]
public void ReportsDebugModeCorrectly(bool isDebug)
{
var userDataService = CreateSystemInformationService(isDebug: isDebug);
var userData = userDataService.GetTroubleshootingInformation().ToArray();
var actual = userData.FirstOrDefault(x => x.Key == "Debug Mode");
Assert.IsNotNull(actual.Value);
Assert.AreEqual(isDebug.ToString(), actual.Value);
}
private ISystemTroubleshootingInformationService CreateSystemInformationService(
string culture = "",
string modelsMode = ModelsModeConstants.InMemoryAuto,
bool isDebug = true,
RuntimeMode runtimeMode = RuntimeMode.BackofficeDevelopment)
{
var localizationService = CreateILocalizationService(culture);
var databaseMock = new Mock<IUmbracoDatabase>();
databaseMock.Setup(x => x.DatabaseType.GetProviderName()).Returns("SQL");
return new SystemTroubleshootingInformationTelemetryProvider(
_umbracoVersion,
localizationService,
Mock.Of<IOptionsMonitor<ModelsBuilderSettings>>(x => x.CurrentValue == new ModelsBuilderSettings { ModelsMode = modelsMode }),
Mock.Of<IOptionsMonitor<HostingSettings>>(x => x.CurrentValue == new HostingSettings { Debug = isDebug }),
Mock.Of<IHostEnvironment>(),
Mock.Of<IUmbracoDatabaseFactory>(x => x.CreateDatabase() == Mock.Of<IUmbracoDatabase>(y => y.DatabaseType == DatabaseType.SQLite)),
Mock.Of<IServerRoleAccessor>(),
Mock.Of<IOptionsMonitor<RuntimeSettings>>(x => x.CurrentValue == new RuntimeSettings { Mode = runtimeMode }));
}
private ILocalizationService CreateILocalizationService(string culture)
{
var localizationService = new Mock<ILocalizationService>();
localizationService.Setup(x => x.GetDefaultLanguageIsoCode()).Returns(culture);
return localizationService.Object;
}
private void CreateUmbracoVersion(int major, int minor, int patch)
{
var umbracoVersion = new Mock<IUmbracoVersion>();
var semVersion = new SemVersion(major, minor, patch);
umbracoVersion.Setup(x => x.SemanticVersion).Returns(semVersion);
_umbracoVersion = umbracoVersion.Object;
}
}