No longer use relative path to find views

This commit is contained in:
Nikolaj
2020-08-26 09:55:14 +02:00
parent b944142905
commit 3e3f91c14d
4 changed files with 112 additions and 67 deletions

View File

@@ -5,15 +5,17 @@ using System.IO;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Umbraco.Tests.Integration.Testing;
namespace Umbraco.Tests.Integration.FileNames
{
[TestFixture]
public class UiFileNames
public class UiFileNames : UmbracoIntegrationTest
{
[Test]
public void MacroTemplates()
{
var basePath = IOHelper.MapPath("~/");
var files = Directory.GetFiles(@"..\\..\\..\\..\\Umbraco.Web.UI.NetCore\\umbraco\\PartialViewMacros\\Templates");
foreach(var file in files)
{
@@ -23,16 +25,6 @@ namespace Umbraco.Tests.Integration.FileNames
}
}
[Test]
public void LanguageFilesAreLowercase()
{
var files = Directory.GetFiles(@"..\\..\\..\\..\\Umbraco.Web.UI.NetCore\\umbraco\\config\\lang");
foreach(var file in files)
{
var fileName = file.Split("\\").Last();
Assert.AreEqual(fileName.ToLower(), fileName);
}
}
}
}

View File

@@ -40,6 +40,7 @@ namespace Umbraco.Tests.UnitTests.AutoFixture
.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());

View File

@@ -0,0 +1,108 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Moq;
using NUnit.Framework;
using Umbraco.Core.Configuration;
using Umbraco.Tests.UnitTests.AutoFixture;
using Umbraco.Web.Common.Install;
using Umbraco.Core;
using AutoFixture.NUnit3;
using Umbraco.Core.Hosting;
using System.IO;
using System.Reflection;
using System.Linq;
using Umbraco.Web.BackOffice.Controllers;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using AutoFixture;
using Microsoft.AspNetCore.Http;
namespace Umbraco.Tests.UnitTests.Umbraco.Web.Common
{
[TestFixture]
class FileNameTests
{
private string getViewName(ViewResult viewResult, string seperator = "/")
{
var sections = viewResult.ViewName.Split(seperator);
return sections[sections.Length - 1];
}
private 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, "\\");
// TODO: Don't use DirectoryInfo to get contents of UmbracoInstall, use something that works everywhere.
var views = getUiFiles(new string[] { "Umbraco", "UmbracoInstall" });
Assert.True(views.Contains(fileName), $"Expected {fileName} to exist, but it didn't");
}
[Test, AutoMoqData]
public void PrviewViewExists(
[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 string[] {"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 string[] { "umbraco", "UmbracoBackOffice" });
Assert.True(views.Contains(fileName), $"Expected {fileName} to exist, but it didn't");
}
[Test]
public void LanguageFilesAreLowercase()
{
var files = getUiFiles(new string[] { "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

@@ -1,56 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Moq;
using NUnit.Framework;
using Umbraco.Core.Configuration;
using Umbraco.Tests.UnitTests.AutoFixture;
using Umbraco.Web.Common.Install;
using Umbraco.Core;
using AutoFixture.NUnit3;
using Umbraco.Core.Hosting;
using System.IO;
using System.Reflection;
using System.Linq;
using Umbraco.Web.BackOffice.Controllers;
namespace Umbraco.Tests.UnitTests.Umbraco.Web.Common
{
[TestFixture]
class InstallControllerTest // TODO: Is this the right place?
{
[Test, AutoMoqData]
public async Task InstallViewExists(
[Frozen] IHostingEnvironment hostingEnvironment,
InstallController sut)
{
Mock.Get(hostingEnvironment).Setup(x => x.ToAbsolute(It.IsAny<string>())).Returns("/");
var viewResult = await sut.Index() as ViewResult;
var sections = viewResult.ViewName.Split("\\");
var fileName = sections[sections.Length - 1];
// TODO: Don't use DirectoryInfo to get contents of UmbracoInstall, use something that works everywhere.
var views = new DirectoryInfo(@"..\\..\\..\\..\\Umbraco.Web.UI.NetCore\\umbraco\\UmbracoInstall").GetFiles()
.Select(f => f.Name).ToArray();
Assert.True(views.Contains(fileName), $"Expected {fileName} to exist, but it didn't");
}
[Test, AutoMoqData]
public void PrviewViewExists(
[Frozen] IGlobalSettings globalSettings,
PreviewController sut)
{
Mock.Get(globalSettings).Setup(x => x.UmbracoPath).Returns("/");
var viewResult = sut.Index() as ViewResult;
var sections = viewResult.ViewName.Split("/");
var fileName = sections[sections.Length - 1];
var views = new DirectoryInfo(@"..\\..\\..\\..\\Umbraco.Web.UI.NetCore\\umbraco\\UmbracoBackOffice").GetFiles()
.Select(f => f.Name).ToArray();
Assert.True(views.Contains(fileName), $"Expected {fileName} to exist, but it didn't");
}
}
}