Merge pull request #8731 from umbraco/netcore/feature/FileNameTests

NetCore: Add file name tests
This commit is contained in:
Bjarke Berg
2020-08-28 09:50:13 +02:00
committed by GitHub
5 changed files with 130 additions and 13 deletions

View File

@@ -8,6 +8,8 @@ using Moq;
using Umbraco.Core.BackOffice;
using Umbraco.Core.Configuration;
using Umbraco.Web.BackOffice.Controllers;
using Umbraco.Core;
using Umbraco.Web.Common.Install;
namespace Umbraco.Tests.UnitTests.AutoFixture
{
@@ -36,12 +38,26 @@ namespace Umbraco.Tests.UnitTests.AutoFixture
(a,b,c) => BackOfficeIdentityUser.CreateNew(Mock.Of<IGlobalSettings>(),a,b,c)));
fixture
.Customize(new ConstructorCustomization(typeof(UsersController), new GreedyConstructorQuery()))
.Customize(new ConstructorCustomization(typeof(InstallController), new GreedyConstructorQuery()))
.Customize(new ConstructorCustomization(typeof(PreviewController), new GreedyConstructorQuery()))
.Customize(new ConstructorCustomization(typeof(BackOfficeController), new GreedyConstructorQuery()))
.Customize(new ConstructorCustomization(typeof(BackOfficeUserManager), new GreedyConstructorQuery()))
.Customize(new AutoMoqCustomization());
// When requesting an IUserStore ensure we actually uses a IUserLockoutStore
fixture.Customize<IUserStore<BackOfficeIdentityUser>>(cc => cc.FromFactory(() => Mock.Of<IUserLockoutStore<BackOfficeIdentityUser>>()));
fixture.Customize<ConfigConnectionString>(
u => u.FromFactory<string, string, string>(
(a, b, c) => new ConfigConnectionString(a, b, c)));
fixture.Customize<IUmbracoVersion>(
u => u.FromFactory(
() => new UmbracoVersion()));
var connectionStrings = Mock.Of<IConnectionStrings>();
Mock.Get(connectionStrings).Setup(x => x[Constants.System.UmbracoConnectionName]).Returns((ConfigConnectionString)new ConfigConnectionString(string.Empty, string.Empty, string.Empty));
fixture.Customize<IConnectionStrings>(x => x.FromFactory(() => connectionStrings ));

View File

@@ -0,0 +1,103 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using AutoFixture.NUnit3;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Moq;
using NUnit.Framework;
using Umbraco.Core.Configuration;
using Umbraco.Core.Hosting;
using Umbraco.Tests.UnitTests.AutoFixture;
using Umbraco.Web.BackOffice.Controllers;
using Umbraco.Web.Common.Install;
namespace Umbraco.Tests.UnitTests.Umbraco.Web.Common
{
[TestFixture]
internal class FileNameTests
{
private string GetViewName(ViewResult viewResult, string separator = "/")
{
var sections = viewResult.ViewName.Split(separator);
return sections[^1];
}
private IEnumerable<string> GetUiFiles(IEnumerable<string> pathFromNetCore)
{
var sourceRoot = TestContext.CurrentContext.TestDirectory.Split("Umbraco.Tests.UnitTests")[0];
var pathToFiles = Path.Combine(sourceRoot, "Umbraco.Web.UI.NetCore");
foreach (var pathSection in pathFromNetCore)
{
pathToFiles = Path.Combine(pathToFiles, pathSection);
}
return new DirectoryInfo(pathToFiles).GetFiles().Select(f => f.Name).ToArray();
}
[Test]
[AutoMoqData]
public async Task InstallViewExists(
[Frozen] IHostingEnvironment hostingEnvironment,
InstallController sut)
{
Mock.Get(hostingEnvironment).Setup(x => x.ToAbsolute(It.IsAny<string>())).Returns("http://localhost/");
var viewResult = await sut.Index() as ViewResult;
var fileName = GetViewName(viewResult, Path.DirectorySeparatorChar.ToString());
var views = GetUiFiles(new[] { "Umbraco", "UmbracoInstall" });
Assert.True(views.Contains(fileName), $"Expected {fileName} to exist, but it didn't");
}
[Test]
[AutoMoqData]
public void PreviewViewExists(
[Frozen] IGlobalSettings globalSettings,
PreviewController sut)
{
Mock.Get(globalSettings).Setup(x => x.UmbracoPath).Returns("/");
var viewResult = sut.Index() as ViewResult;
var fileName = GetViewName(viewResult);
var views = GetUiFiles(new[] { "Umbraco", "UmbracoBackOffice" });
Assert.True(views.Contains(fileName), $"Expected {fileName} to exist, but it didn't");
}
[Test]
[AutoMoqData]
public async Task BackOfficeDefaultExists(
[Frozen] IGlobalSettings globalSettings,
[Frozen] IHostingEnvironment hostingEnvironment,
[Frozen] ITempDataDictionary tempDataDictionary,
BackOfficeController sut)
{
Mock.Get(globalSettings).Setup(x => x.UmbracoPath).Returns("/");
Mock.Get(hostingEnvironment).Setup(x => x.ToAbsolute("/")).Returns("http://localhost/");
Mock.Get(hostingEnvironment).SetupGet(x => x.ApplicationVirtualPath).Returns("/");
sut.TempData = tempDataDictionary;
var viewResult = await sut.Default() as ViewResult;
var fileName = GetViewName(viewResult);
var views = GetUiFiles(new[] { "Umbraco", "UmbracoBackOffice" });
Assert.True(views.Contains(fileName), $"Expected {fileName} to exist, but it didn't");
}
[Test]
public void LanguageFilesAreLowercase()
{
var files = GetUiFiles(new[] { "Umbraco", "config", "lang" });
foreach (var fileName in files)
{
Assert.AreEqual(fileName.ToLower(), fileName,
$"Language files must be all lowercase but {fileName} is not lowercase.");
}
}
}
}

View File

@@ -47,6 +47,16 @@ namespace Umbraco.Extensions
return linkGenerator.GetPathByAction(nameof(InstallController.Index), ControllerExtensions.GetControllerName<InstallController>(), new { area = Constants.Web.Mvc.InstallArea });
}
/// <summary>
/// Returns the URL for the installer api
/// </summary>
/// <param name="linkGenerator"></param>
/// <returns></returns>
public static string GetInstallerApiUrl(this LinkGenerator linkGenerator)
{
return linkGenerator.GetPathByAction(nameof(InstallController.Index), ControllerExtensions.GetControllerName<InstallApiController>(), new { area = Constants.Web.Mvc.InstallArea });
}
/// <summary>
/// Return the Url for a Web Api service
/// </summary>

View File

@@ -32,18 +32,6 @@ namespace Umbraco.Extensions
return url.Action("Default", ControllerExtensions.GetControllerName(backOfficeControllerType), new { area = Constants.Web.Mvc.BackOfficeApiArea });
}
/// <summary>
/// Return the installer API url
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static string GetInstallerApiUrl(this IUrlHelper url)
{
// there is no default action here so we need to get it by action and trim the action
return url.Action("GetSetup", ControllerExtensions.GetControllerName<InstallApiController>(), new { area = Constants.Web.Mvc.InstallArea })
.TrimEnd("GetSetup");
}
/// <summary>
/// Return the Url for a Web Api service
/// </summary>

View File

@@ -81,7 +81,7 @@ namespace Umbraco.Web.Common.Install
}
// gen the install base url
ViewData.SetInstallApiBaseUrl(Url.GetInstallerApiUrl());
ViewData.SetInstallApiBaseUrl(_linkGenerator.GetInstallerApiUrl());
// get the base umbraco folder
var baseFolder = _hostingEnvironment.ToAbsolute(_globalSettings.UmbracoPath);