Merge pull request #7963 from emmagarland/feature/netcore-model-unit-tests-settings

Netcore: Migrate UmbracoSettings tests from UnitTests to Integration Tests
This commit is contained in:
Bjarke Berg
2020-04-27 10:34:35 +02:00
committed by GitHub
25 changed files with 162 additions and 146 deletions

View File

@@ -116,7 +116,7 @@ namespace Umbraco.Tests.Common
/// </summary>
/// <param name="relativePath"></param>
/// <returns></returns>
public string MapPathForTestFiles(string relativePath)
public virtual string MapPathForTestFiles(string relativePath)
{
if (!relativePath.StartsWith("~/"))
throw new ArgumentException("relativePath must start with '~/'", nameof(relativePath));

View File

@@ -5,9 +5,7 @@ using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NUnit.Framework;
using Umbraco.Configuration.Models;
using Umbraco.Core;
using Umbraco.Core.Configuration;
using Umbraco.Core.Logging;
@@ -63,7 +61,7 @@ namespace Umbraco.Tests.Integration.Extensions
var dbFactory = app.ApplicationServices.GetRequiredService<IUmbracoDatabaseFactory>();
if (!dbFactory.Configured)
{
dbFactory.Configure(db.ConnectionString, Umbraco.Core.Constants.DatabaseProviders.SqlServer);
dbFactory.Configure(db.ConnectionString, Constants.DatabaseProviders.SqlServer);
}
// In the case that we've initialized the schema, it means that we are installed so we'll want to ensure that

View File

@@ -2,7 +2,6 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Umbraco.Core.Composing;
using Umbraco.Tests.Integration.Implementations;
namespace Umbraco.Tests.Integration.Extensions

View File

@@ -17,8 +17,6 @@ using Umbraco.Net;
using Umbraco.Core.Persistence;
using Umbraco.Core.Runtime;
using Umbraco.Tests.Common;
using Umbraco.Web.BackOffice;
using Umbraco.Web.BackOffice.AspNetCore;
using Umbraco.Web.Common.AspNetCore;
using IHostingEnvironment = Umbraco.Core.Hosting.IHostingEnvironment;
@@ -43,7 +41,8 @@ namespace Umbraco.Tests.Integration.Implementations
var hostEnvironment = new Mock<IWebHostEnvironment>();
hostEnvironment.Setup(x => x.ApplicationName).Returns("UmbracoIntegrationTests");
hostEnvironment.Setup(x => x.ContentRootPath).Returns(() => Assembly.GetExecutingAssembly().GetRootDirectorySafe());
hostEnvironment.Setup(x => x.ContentRootPath)
.Returns(() => Assembly.GetExecutingAssembly().GetRootDirectorySafe());
hostEnvironment.Setup(x => x.WebRootPath).Returns(() => WorkingDirectory);
_hostEnvironment = hostEnvironment.Object;
@@ -79,9 +78,11 @@ namespace Umbraco.Tests.Integration.Implementations
}
}
public IUmbracoBootPermissionChecker UmbracoBootPermissionChecker { get; } = new TestUmbracoBootPermissionChecker();
public IUmbracoBootPermissionChecker UmbracoBootPermissionChecker { get; } =
new TestUmbracoBootPermissionChecker();
public AppCaches AppCaches { get; } = new AppCaches(NoAppCache.Instance, NoAppCache.Instance, new IsolatedCaches(type => NoAppCache.Instance));
public AppCaches AppCaches { get; } = new AppCaches(NoAppCache.Instance, NoAppCache.Instance,
new IsolatedCaches(type => NoAppCache.Instance));
public IProfilingLogger Logger { get; private set; }
@@ -91,7 +92,8 @@ namespace Umbraco.Tests.Integration.Implementations
public IWebHostEnvironment GetWebHostEnvironment() => _hostEnvironment;
public override IDbProviderFactoryCreator DbProviderFactoryCreator => new SqlServerDbProviderFactoryCreator(Constants.DbProviderNames.SqlServer, DbProviderFactories.GetFactory);
public override IDbProviderFactoryCreator DbProviderFactoryCreator =>
new SqlServerDbProviderFactoryCreator(Constants.DbProviderNames.SqlServer, DbProviderFactories.GetFactory);
public override IBulkSqlInsertProvider BulkSqlInsertProvider => new SqlServerBulkSqlInsertProvider();
@@ -100,7 +102,8 @@ namespace Umbraco.Tests.Integration.Implementations
public override IBackOfficeInfo GetBackOfficeInfo()
{
if (_backOfficeInfo == null)
_backOfficeInfo = new AspNetCoreBackOfficeInfo(SettingsForTests.GetDefaultGlobalSettings(GetUmbracoVersion()));
_backOfficeInfo =
new AspNetCoreBackOfficeInfo(SettingsForTests.GetDefaultGlobalSettings(GetUmbracoVersion()));
return _backOfficeInfo;
}
@@ -113,5 +116,22 @@ namespace Umbraco.Tests.Integration.Implementations
public override IIpResolver GetIpResolver() => _ipResolver;
/// <summary>
/// Some test files are copied to the /bin (/bin/debug) on build, this is a utility to return their physical path based on a virtual path name
/// </summary>
/// <param name="relativePath"></param>
/// <returns></returns>
public override string MapPathForTestFiles(string relativePath)
{
if (!relativePath.StartsWith("~/"))
throw new ArgumentException("relativePath must start with '~/'", nameof(relativePath));
var codeBase = typeof(TestHelperBase).Assembly.CodeBase;
var uri = new Uri(codeBase);
var path = uri.LocalPath;
var bin = Path.GetDirectoryName(path);
return relativePath.Replace("~/", bin + "/");
}
}
}

View File

@@ -1,13 +1,13 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Umbraco.Core.Configuration;
using Umbraco.Core.Logging;
using Umbraco.Web.Common.AspNetCore;
using IHostingEnvironment = Umbraco.Core.Hosting.IHostingEnvironment;
namespace Umbraco.Tests.Integration.Implementations
{
public class TestHostingEnvironment : AspNetCoreHostingEnvironment, Umbraco.Core.Hosting.IHostingEnvironment
public class TestHostingEnvironment : AspNetCoreHostingEnvironment, IHostingEnvironment
{
public TestHostingEnvironment(IHostingSettings hostingSettings, IWebHostEnvironment webHostEnvironment)
: base(hostingSettings, webHostEnvironment)
@@ -20,6 +20,6 @@ namespace Umbraco.Tests.Integration.Implementations
/// <remarks>
/// This is specifically used by IOHelper and we want this to return false so that the root path is manually calcualted which is what we want for tests.
/// </remarks>
bool Umbraco.Core.Hosting.IHostingEnvironment.IsHosted { get; } = false;
bool IHostingEnvironment.IsHosted { get; } = false;
}
}

View File

@@ -12,7 +12,6 @@ using Umbraco.Core.Configuration;
using Umbraco.Core.Logging;
using Umbraco.Core.Migrations.Install;
using Umbraco.Core.Persistence;
using Umbraco.Web.Common.RuntimeMinification;
namespace Umbraco.Tests.Integration.Testing
{
@@ -122,7 +121,7 @@ namespace Umbraco.Tests.Integration.Testing
}
else
{
_dbFactory.Configure(conn.ConnectionString, Umbraco.Core.Constants.DatabaseProviders.SqlServer);
_dbFactory.Configure(conn.ConnectionString, Constants.DatabaseProviders.SqlServer);
using var database = (UmbracoDatabase)_dbFactory.CreateDatabase();
// track each db command ran as part of creating the database so we can replay these

View File

@@ -0,0 +1,70 @@
using System;
using System.Linq;
using NUnit.Framework;
using Umbraco.Core;
using Umbraco.Core.Configuration.UmbracoSettings;
namespace Umbraco.Tests.Integration.Umbraco.Configuration.UmbracoSettings
{
[TestFixture]
public class ContentElementDefaultTests : ContentElementTests
{
protected override bool TestingDefaults => true;
[Test]
public override void DisableHtmlEmail()
{
Assert.IsTrue(ContentSettings.DisableHtmlEmail == false);
}
[Test]
public override void Can_Set_Multiple()
{
Assert.IsTrue(ContentSettings.Error404Collection.Count() == 1);
Assert.IsTrue(ContentSettings.Error404Collection.ElementAt(0).Culture == null);
Assert.IsTrue(ContentSettings.Error404Collection.ElementAt(0).ContentId == 1);
}
[Test]
public override void ImageAutoFillProperties()
{
Assert.IsTrue(ContentSettings.ImageAutoFillProperties.Count() == 1);
Assert.IsTrue(ContentSettings.ImageAutoFillProperties.ElementAt(0).Alias == "umbracoFile");
Assert.IsTrue(ContentSettings.ImageAutoFillProperties.ElementAt(0).WidthFieldAlias == "umbracoWidth");
Assert.IsTrue(ContentSettings.ImageAutoFillProperties.ElementAt(0).HeightFieldAlias == "umbracoHeight");
Assert.IsTrue(ContentSettings.ImageAutoFillProperties.ElementAt(0).LengthFieldAlias == "umbracoBytes");
Assert.IsTrue(ContentSettings.ImageAutoFillProperties.ElementAt(0).ExtensionFieldAlias == "umbracoExtension");
}
/// <summary>
/// Whitelist is empty in default settings file and is not populated by default, but disallowed is empty and is populated by default
/// </summary>
/// <param name="extension"></param>
/// <param name="expected"></param>
[Test]
[TestCase("png", true)]
[TestCase("jpg", true)]
[TestCase("gif", true)]
[TestCase("bmp", true)]
[TestCase("php", true)]
[TestCase("ashx", false)]
[TestCase("config", false)]
[TestCase("test", true)]
public override void IsFileAllowedForUpload_WithWhitelist(string extension, bool expected)
{
Console.WriteLine("Extension being tested: {0}", extension);
Console.WriteLine("Expected IsAllowed?: {0}", expected);
Console.WriteLine("AllowedUploadFiles: {0}", ContentSettings.AllowedUploadFiles);
Console.WriteLine("DisallowedUploadFiles: {0}", ContentSettings.DisallowedUploadFiles);
bool allowedContainsExtension = ContentSettings.AllowedUploadFiles.Any(x => x.InvariantEquals(extension));
bool disallowedContainsExtension = ContentSettings.DisallowedUploadFiles.Any(x => x.InvariantEquals(extension));
Console.WriteLine("AllowedContainsExtension: {0}", allowedContainsExtension);
Console.WriteLine("DisallowedContainsExtension: {0}", disallowedContainsExtension);
Assert.AreEqual(expected, ContentSettings.IsFileAllowedForUpload(extension));
}
}
}

View File

@@ -1,14 +1,12 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using NUnit.Framework;
using Umbraco.Core;
using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.UmbracoSettings;
using Umbraco.Core.Macros;
namespace Umbraco.Tests.Configurations.UmbracoSettings
namespace Umbraco.Tests.Integration.Umbraco.Configuration.UmbracoSettings
{
[TestFixture]
public class ContentElementTests : UmbracoSettingsTests
@@ -96,25 +94,23 @@ namespace Umbraco.Tests.Configurations.UmbracoSettings
[TestCase("png", true)]
[TestCase("jpg", true)]
[TestCase("gif", true)]
// TODO: Why does it flip to TestingDefaults=true for these two tests on AppVeyor. WHY?
//[TestCase("bmp", false)]
//[TestCase("php", false)]
[TestCase("bmp", false)]
[TestCase("php", false)]
[TestCase("ashx", false)]
[TestCase("config", false)]
public void IsFileAllowedForUpload_WithWhitelist(string extension, bool expected)
[TestCase("test", false)]
public virtual void IsFileAllowedForUpload_WithWhitelist(string extension, bool expected)
{
// Make really sure that defaults are NOT used
TestingDefaults = false;
Console.WriteLine("Extension being tested: {0}", extension);
Console.WriteLine("Expected IsAllowed?: {0}", expected);
Console.WriteLine("AllowedUploadFiles: {0}", ContentSettings.AllowedUploadFiles);
Console.WriteLine("DisallowedUploadFiles: {0}", ContentSettings.DisallowedUploadFiles);
Debug.WriteLine("Extension being tested", extension);
Debug.WriteLine("AllowedUploadFiles: {0}", ContentSettings.AllowedUploadFiles);
Debug.WriteLine("DisallowedUploadFiles: {0}", ContentSettings.DisallowedUploadFiles);
bool allowedContainsExtension = ContentSettings.AllowedUploadFiles.Any(x => x.InvariantEquals(extension));
bool disallowedContainsExtension = ContentSettings.DisallowedUploadFiles.Any(x => x.InvariantEquals(extension));
var allowedContainsExtension = ContentSettings.AllowedUploadFiles.Any(x => x.InvariantEquals(extension));
var disallowedContainsExtension = ContentSettings.DisallowedUploadFiles.Any(x => x.InvariantEquals(extension));
Debug.WriteLine("AllowedContainsExtension: {0}", allowedContainsExtension);
Debug.WriteLine("DisallowedContainsExtension: {0}", disallowedContainsExtension);
Console.WriteLine("AllowedContainsExtension: {0}", allowedContainsExtension);
Console.WriteLine("DisallowedContainsExtension: {0}", disallowedContainsExtension);
Assert.AreEqual(expected, ContentSettings.IsFileAllowedForUpload(extension));
}

View File

@@ -1,15 +1,11 @@
using System.Linq;
using NUnit.Framework;
using NUnit.Framework;
namespace Umbraco.Tests.Configurations.UmbracoSettings
namespace Umbraco.Tests.Integration.Umbraco.Configuration.UmbracoSettings
{
[TestFixture]
public class LoggingElementDefaultTests : LoggingElementTests
{
protected override bool TestingDefaults
{
get { return true; }
}
protected override bool TestingDefaults => true;
[Test]
public override void MaxLogAge()

View File

@@ -1,7 +1,6 @@
using System.Linq;
using NUnit.Framework;
using NUnit.Framework;
namespace Umbraco.Tests.Configurations.UmbracoSettings
namespace Umbraco.Tests.Integration.Umbraco.Configuration.UmbracoSettings
{
[TestFixture]
public class LoggingElementTests : UmbracoSettingsTests

View File

@@ -0,0 +1,10 @@
using NUnit.Framework;
namespace Umbraco.Tests.Integration.Umbraco.Configuration.UmbracoSettings
{
[TestFixture]
public class RequestHandlerElementDefaultTests : RequestHandlerElementTests
{
protected override bool TestingDefaults => true;
}
}

View File

@@ -2,7 +2,7 @@
using NUnit.Framework;
using Umbraco.Core;
namespace Umbraco.Tests.Configurations.UmbracoSettings
namespace Umbraco.Tests.Integration.Umbraco.Configuration.UmbracoSettings
{
[TestFixture]
public class RequestHandlerElementTests : UmbracoSettingsTests

View File

@@ -0,0 +1,10 @@
using NUnit.Framework;
namespace Umbraco.Tests.Integration.Umbraco.Configuration.UmbracoSettings
{
[TestFixture]
public class SecurityElementDefaultTests : SecurityElementTests
{
protected override bool TestingDefaults => true;
}
}

View File

@@ -1,7 +1,6 @@
using NUnit.Framework;
using Umbraco.Core;
namespace Umbraco.Tests.Configurations.UmbracoSettings
namespace Umbraco.Tests.Integration.Umbraco.Configuration.UmbracoSettings
{
[TestFixture]
public class SecurityElementTests : UmbracoSettingsTests

View File

@@ -4,9 +4,9 @@ using System.IO;
using NUnit.Framework;
using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.UmbracoSettings;
using Umbraco.Tests.TestHelpers;
using Umbraco.Tests.Integration.Implementations;
namespace Umbraco.Tests.Configurations.UmbracoSettings
namespace Umbraco.Tests.Integration.Umbraco.Configuration.UmbracoSettings
{
public abstract class UmbracoSettingsTests
{
@@ -15,7 +15,8 @@ namespace Umbraco.Tests.Configurations.UmbracoSettings
[SetUp]
public void Init()
{
var config = new FileInfo(TestHelper.MapPathForTestFiles("~/Configurations/UmbracoSettings/web.config"));
var testHelper = new TestHelper();
var config = new FileInfo(testHelper.MapPathForTestFiles("~/Umbraco.Configuration/Configurations/web.config"));
var fileMap = new ExeConfigurationFileMap() { ExeConfigFilename = config.FullName };
var configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

View File

@@ -1,15 +1,11 @@
using NUnit.Framework;
namespace Umbraco.Tests.Configurations.UmbracoSettings
namespace Umbraco.Tests.Integration.Umbraco.Configuration.UmbracoSettings
{
[TestFixture]
public class WebRoutingElementDefaultTests : WebRoutingElementTests
{
protected override bool TestingDefaults
{
get { return true; }
}
protected override bool TestingDefaults => true;
[Test]
public override void UrlProviderMode()

View File

@@ -1,6 +1,6 @@
using NUnit.Framework;
namespace Umbraco.Tests.Configurations.UmbracoSettings
namespace Umbraco.Tests.Integration.Umbraco.Configuration.UmbracoSettings
{
[TestFixture]
public class WebRoutingElementTests : UmbracoSettingsTests

View File

@@ -32,4 +32,16 @@
<ProjectReference Include="..\Umbraco.Web.BackOffice\Umbraco.Web.BackOffice.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="Umbraco.Configuration\Configurations\umbracoSettings.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Umbraco.Configuration\Configurations\umbracoSettings.minimal.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Umbraco.Configuration\Configurations\web.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@@ -1,40 +0,0 @@
using System.Linq;
using NUnit.Framework;
namespace Umbraco.Tests.Configurations.UmbracoSettings
{
[TestFixture]
public class ContentElementDefaultTests : ContentElementTests
{
protected override bool TestingDefaults
{
get { return true; }
}
[Test]
public override void DisableHtmlEmail()
{
Assert.IsTrue(ContentSettings.DisableHtmlEmail == false);
}
[Test]
public override void Can_Set_Multiple()
{
Assert.IsTrue(ContentSettings.Error404Collection.Count() == 1);
Assert.IsTrue(ContentSettings.Error404Collection.ElementAt(0).Culture == null);
Assert.IsTrue(ContentSettings.Error404Collection.ElementAt(0).ContentId == 1);
}
[Test]
public override void ImageAutoFillProperties()
{
Assert.IsTrue(ContentSettings.ImageAutoFillProperties.Count() == 1);
Assert.IsTrue(ContentSettings.ImageAutoFillProperties.ElementAt(0).Alias == "umbracoFile");
Assert.IsTrue(ContentSettings.ImageAutoFillProperties.ElementAt(0).WidthFieldAlias == "umbracoWidth");
Assert.IsTrue(ContentSettings.ImageAutoFillProperties.ElementAt(0).HeightFieldAlias == "umbracoHeight");
Assert.IsTrue(ContentSettings.ImageAutoFillProperties.ElementAt(0).LengthFieldAlias == "umbracoBytes");
Assert.IsTrue(ContentSettings.ImageAutoFillProperties.ElementAt(0).ExtensionFieldAlias == "umbracoExtension");
}
}
}

View File

@@ -1,13 +0,0 @@
using NUnit.Framework;
namespace Umbraco.Tests.Configurations.UmbracoSettings
{
[TestFixture]
public class RequestHandlerElementDefaultTests : RequestHandlerElementTests
{
protected override bool TestingDefaults
{
get { return true; }
}
}
}

View File

@@ -1,13 +0,0 @@
using NUnit.Framework;
namespace Umbraco.Tests.Configurations.UmbracoSettings
{
[TestFixture]
public class SecurityElementDefaultTests : SecurityElementTests
{
protected override bool TestingDefaults
{
get { return true; }
}
}
}

View File

@@ -304,17 +304,6 @@
<Compile Include="Cache\AppCacheTests.cs" />
<Compile Include="Cache\HttpRequestAppCacheTests.cs" />
<Compile Include="Cache\RuntimeAppCacheTests.cs" />
<Compile Include="Configurations\UmbracoSettings\ContentElementDefaultTests.cs" />
<Compile Include="Configurations\UmbracoSettings\ContentElementTests.cs" />
<Compile Include="Configurations\UmbracoSettings\LoggingElementDefaultTests.cs" />
<Compile Include="Configurations\UmbracoSettings\LoggingElementTests.cs" />
<Compile Include="Configurations\UmbracoSettings\RequestHandlerElementDefaultTests.cs" />
<Compile Include="Configurations\UmbracoSettings\RequestHandlerElementTests.cs" />
<Compile Include="Configurations\UmbracoSettings\SecurityElementDefaultTests.cs" />
<Compile Include="Configurations\UmbracoSettings\SecurityElementTests.cs" />
<Compile Include="Configurations\UmbracoSettings\UmbracoSettingsTests.cs" />
<Compile Include="Configurations\UmbracoSettings\WebRoutingElementDefaultTests.cs" />
<Compile Include="Configurations\UmbracoSettings\WebRoutingElementTests.cs" />
<Compile Include="Web\Controllers\ContentControllerUnitTests.cs" />
<Compile Include="Web\Controllers\FilterAllowedOutgoingContentAttributeTests.cs" />
<Compile Include="Web\Controllers\MediaControllerUnitTests.cs" />
@@ -514,14 +503,6 @@
<None Include="App.config">
<SubType>Designer</SubType>
</None>
<None Include="Configurations\UmbracoSettings\umbracoSettings.minimal.config">
<SubType>Designer</SubType>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Configurations\UmbracoSettings\umbracoSettings.config">
<SubType>Designer</SubType>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Logging\UmbracoTraceLog.UNITTEST.20181112.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
@@ -590,10 +571,6 @@
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<None Include="Configurations\UmbracoSettings\web.config">
<SubType>Designer</SubType>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<Content Include="Logging\logviewer.searches.config.js">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>