From 3e3f91c14d8fd99f7b51d416cac23504a60e55aa Mon Sep 17 00:00:00 2001 From: Nikolaj Date: Wed, 26 Aug 2020 09:55:14 +0200 Subject: [PATCH] No longer use relative path to find views --- .../FileNames/UiFileNames.cs | 14 +-- .../AutoFixture/AutoMoqDataAttribute.cs | 1 + .../Umbraco.Web.Common/FileNameTests.cs | 108 ++++++++++++++++++ .../InstallControllerTest.cs | 56 --------- 4 files changed, 112 insertions(+), 67 deletions(-) create mode 100644 src/Umbraco.Tests.UnitTests/Umbraco.Web.Common/FileNameTests.cs delete mode 100644 src/Umbraco.Tests.UnitTests/Umbraco.Web.Common/InstallControllerTest.cs diff --git a/src/Umbraco.Tests.Integration/FileNames/UiFileNames.cs b/src/Umbraco.Tests.Integration/FileNames/UiFileNames.cs index b5ebc3f5bf..d671420791 100644 --- a/src/Umbraco.Tests.Integration/FileNames/UiFileNames.cs +++ b/src/Umbraco.Tests.Integration/FileNames/UiFileNames.cs @@ -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); - } - } } } diff --git a/src/Umbraco.Tests.UnitTests/AutoFixture/AutoMoqDataAttribute.cs b/src/Umbraco.Tests.UnitTests/AutoFixture/AutoMoqDataAttribute.cs index adb1166cd8..625e1ca619 100644 --- a/src/Umbraco.Tests.UnitTests/AutoFixture/AutoMoqDataAttribute.cs +++ b/src/Umbraco.Tests.UnitTests/AutoFixture/AutoMoqDataAttribute.cs @@ -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()); diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Web.Common/FileNameTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Web.Common/FileNameTests.cs new file mode 100644 index 0000000000..aca6abbbbd --- /dev/null +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Web.Common/FileNameTests.cs @@ -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 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())).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."); + } + + } + } +} diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Web.Common/InstallControllerTest.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Web.Common/InstallControllerTest.cs deleted file mode 100644 index 484d7e48a6..0000000000 --- a/src/Umbraco.Tests.UnitTests/Umbraco.Web.Common/InstallControllerTest.cs +++ /dev/null @@ -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())).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"); - } - } -}