Files
Umbraco-CMS/tests/Umbraco.Tests.UnitTests/Umbraco.Web.Common/FileNameTests.cs
Paul Johnson 00133e880d Move test projects from src/ to tests/ (#11357)
* Update gitignore

* Move csproj

* Update project references

* Update solutions

* Update build scripts

* Tests used to share editorconfig with projects in src

* Fix broken tests.

* Stop copying around .editorconfig

merged root one with linting

* csharp_style_expression_bodied -> suggestion

* Move StyleCop rulesets to matching directories and update shared build properties

* Remove legacy build files, update NuGet.cofig and solution files

* Restore myget source

* Clean up .gitignore

* Update .gitignore

* Move new test classes to tests after merge

* Gitignore + nuget config

* Move new test

Co-authored-by: Ronald Barendse <ronald@barend.se>
2021-10-18 08:14:04 +01:00

112 lines
4.1 KiB
C#

// Copyright (c) Umbraco.
// See LICENSE for more details.
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 Microsoft.Extensions.Options;
using Moq;
using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Hosting;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Tests.UnitTests.AutoFixture;
using Umbraco.Cms.Web.BackOffice.Controllers;
using Umbraco.Cms.Web.BackOffice.Install;
namespace Umbraco.Cms.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 root = TestContext.CurrentContext.TestDirectory.Split("tests")[0];
var pathToFiles = Path.Combine(root, "src", "Umbraco.Web.UI");
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());
IEnumerable<string> views = GetUiFiles(new[] { "umbraco", "UmbracoInstall" });
Assert.True(views.Contains(fileName), $"Expected {fileName} to exist, but it didn't");
}
[Test]
[AutoMoqData]
public void PreviewViewExists(
[Frozen] IOptions<GlobalSettings> globalSettings,
PreviewController sut)
{
globalSettings.Value.UmbracoPath = "/";
var viewResult = sut.Index() as ViewResult;
var fileName = GetViewName(viewResult);
IEnumerable<string> 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] IOptions<GlobalSettings> globalSettings,
[Frozen] IHostingEnvironment hostingEnvironment,
[Frozen] ITempDataDictionary tempDataDictionary,
[Frozen] IRuntimeState runtimeState,
BackOfficeController sut)
{
globalSettings.Value.UmbracoPath = "/";
Mock.Get(hostingEnvironment).Setup(x => x.ToAbsolute("/")).Returns("http://localhost/");
Mock.Get(hostingEnvironment).SetupGet(x => x.ApplicationVirtualPath).Returns("/");
Mock.Get(runtimeState).Setup(x => x.Level).Returns(RuntimeLevel.Run);
sut.TempData = tempDataDictionary;
var viewResult = await sut.Default() as ViewResult;
var fileName = GetViewName(viewResult);
IEnumerable<string> views = GetUiFiles(new[] { "umbraco", "UmbracoBackOffice" });
Assert.True(views.Contains(fileName), $"Expected {fileName} to exist, but it didn't");
}
[Test]
public void LanguageFilesAreLowercase()
{
IEnumerable<string> 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.");
}
}
}
}