2020-12-19 08:17:35 +01:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
2020-12-08 10:42:26 +11:00
|
|
|
using System;
|
2020-03-13 12:08:25 +11:00
|
|
|
using System.IO;
|
2020-03-13 14:43:41 +11:00
|
|
|
using System.Reflection;
|
2020-10-27 10:53:01 +00:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2020-09-18 08:39:38 +02:00
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
2023-10-10 11:51:47 +02:00
|
|
|
using Microsoft.Extensions.Options;
|
2020-03-13 12:08:25 +11:00
|
|
|
using Moq;
|
2021-02-09 10:22:42 +01:00
|
|
|
using Umbraco.Cms.Core;
|
|
|
|
|
using Umbraco.Cms.Core.Cache;
|
|
|
|
|
using Umbraco.Cms.Core.Composing;
|
|
|
|
|
using Umbraco.Cms.Core.Configuration;
|
|
|
|
|
using Umbraco.Cms.Core.Configuration.Models;
|
|
|
|
|
using Umbraco.Cms.Core.Diagnostics;
|
2023-10-10 11:51:47 +02:00
|
|
|
using Umbraco.Cms.Core.DistributedLocking;
|
|
|
|
|
using Umbraco.Cms.Core.Events;
|
2021-02-09 10:22:42 +01:00
|
|
|
using Umbraco.Cms.Core.Hosting;
|
|
|
|
|
using Umbraco.Cms.Core.IO;
|
|
|
|
|
using Umbraco.Cms.Core.Logging;
|
|
|
|
|
using Umbraco.Cms.Core.Models.PublishedContent;
|
|
|
|
|
using Umbraco.Cms.Core.Net;
|
|
|
|
|
using Umbraco.Cms.Core.Routing;
|
|
|
|
|
using Umbraco.Cms.Core.Runtime;
|
|
|
|
|
using Umbraco.Cms.Core.Serialization;
|
|
|
|
|
using Umbraco.Cms.Core.Strings;
|
2021-02-12 13:36:50 +01:00
|
|
|
using Umbraco.Cms.Infrastructure.Persistence;
|
2023-10-10 11:51:47 +02:00
|
|
|
using Umbraco.Cms.Infrastructure.Persistence.SqlSyntax;
|
|
|
|
|
using Umbraco.Cms.Infrastructure.Scoping;
|
2021-02-15 12:42:26 +01:00
|
|
|
using Umbraco.Cms.Infrastructure.Serialization;
|
2021-02-10 14:45:44 +01:00
|
|
|
using Umbraco.Cms.Tests.Common.TestHelpers;
|
2021-02-09 11:26:22 +01:00
|
|
|
using Umbraco.Extensions;
|
2020-03-13 12:08:25 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
namespace Umbraco.Cms.Tests.Common;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Common helper properties and methods useful to testing
|
|
|
|
|
/// </summary>
|
|
|
|
|
public abstract class TestHelperBase
|
2020-03-13 12:08:25 +11:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
private readonly ITypeFinder _typeFinder;
|
|
|
|
|
private IIOHelper _ioHelper;
|
|
|
|
|
private UriUtility _uriUtility;
|
|
|
|
|
private string _workingDir;
|
|
|
|
|
|
|
|
|
|
protected TestHelperBase(Assembly entryAssembly)
|
|
|
|
|
{
|
|
|
|
|
MainDom = new SimpleMainDom();
|
2024-04-09 09:06:48 +02:00
|
|
|
_typeFinder = new TypeFinder(NullLoggerFactory.Instance.CreateLogger<TypeFinder>(), new DefaultUmbracoAssemblyProvider(entryAssembly, NullLoggerFactory.Instance), null);
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
|
|
|
|
|
2020-03-13 12:08:25 +11:00
|
|
|
/// <summary>
|
2022-06-21 08:09:38 +02:00
|
|
|
/// Gets the working directory of the test project.
|
2020-03-13 12:08:25 +11:00
|
|
|
/// </summary>
|
2022-06-21 08:09:38 +02:00
|
|
|
public string WorkingDirectory
|
2020-03-13 12:08:25 +11:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
get
|
2020-03-13 12:08:25 +11:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
if (_workingDir != null)
|
2020-03-13 12:08:25 +11:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
return _workingDir;
|
|
|
|
|
}
|
2020-03-31 18:01:27 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
// Azure DevOps can only store a database in certain locations so we will need to detect if we are running
|
|
|
|
|
// on a build server and if so we'll use the temp path.
|
|
|
|
|
var dir = string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("System_DefaultWorkingDirectory"))
|
|
|
|
|
? Path.Combine(Assembly.GetExecutingAssembly().GetRootDirectorySafe(), "TEMP")
|
|
|
|
|
: Path.Combine(Path.GetTempPath(), "UmbracoTests", "TEMP");
|
2020-12-19 08:17:35 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
if (!Directory.Exists(dir))
|
|
|
|
|
{
|
|
|
|
|
_ = Directory.CreateDirectory(dir);
|
2020-03-13 12:08:25 +11:00
|
|
|
}
|
2022-06-21 08:09:38 +02:00
|
|
|
|
|
|
|
|
_workingDir = dir;
|
|
|
|
|
return _workingDir;
|
2020-03-13 12:08:25 +11:00
|
|
|
}
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
2020-03-13 12:08:25 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public IShortStringHelper ShortStringHelper { get; } =
|
|
|
|
|
new DefaultShortStringHelper(new DefaultShortStringHelperConfig());
|
2023-10-10 11:51:47 +02:00
|
|
|
public IScopeProvider ScopeProvider
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
var loggerFactory = NullLoggerFactory.Instance;
|
|
|
|
|
var fileSystems = new FileSystems(
|
|
|
|
|
loggerFactory,
|
|
|
|
|
Mock.Of<IIOHelper>(),
|
|
|
|
|
Mock.Of<IOptions<GlobalSettings>>(),
|
|
|
|
|
Mock.Of<IHostingEnvironment>());
|
|
|
|
|
var mediaFileManager = new MediaFileManager(
|
|
|
|
|
Mock.Of<IFileSystem>(),
|
|
|
|
|
Mock.Of<IMediaPathScheme>(),
|
|
|
|
|
loggerFactory.CreateLogger<MediaFileManager>(),
|
|
|
|
|
Mock.Of<IShortStringHelper>(),
|
|
|
|
|
Mock.Of<IServiceProvider>(),
|
|
|
|
|
Options.Create(new ContentSettings()));
|
|
|
|
|
var databaseFactory = new Mock<IUmbracoDatabaseFactory>();
|
|
|
|
|
var database = new Mock<IUmbracoDatabase>();
|
|
|
|
|
var sqlContext = new Mock<ISqlContext>();
|
|
|
|
|
|
|
|
|
|
var lockingMechanism = new Mock<IDistributedLockingMechanism>();
|
|
|
|
|
lockingMechanism.Setup(x => x.ReadLock(It.IsAny<int>(), It.IsAny<TimeSpan?>()))
|
|
|
|
|
.Returns(Mock.Of<IDistributedLock>());
|
|
|
|
|
lockingMechanism.Setup(x => x.WriteLock(It.IsAny<int>(), It.IsAny<TimeSpan?>()))
|
|
|
|
|
.Returns(Mock.Of<IDistributedLock>());
|
|
|
|
|
|
|
|
|
|
var lockingMechanismFactory = new Mock<IDistributedLockingMechanismFactory>();
|
|
|
|
|
lockingMechanismFactory.Setup(x => x.DistributedLockingMechanism)
|
|
|
|
|
.Returns(lockingMechanism.Object);
|
|
|
|
|
|
|
|
|
|
// Setup mock of database factory to return mock of database.
|
|
|
|
|
databaseFactory.Setup(x => x.CreateDatabase()).Returns(database.Object);
|
|
|
|
|
databaseFactory.Setup(x => x.SqlContext).Returns(sqlContext.Object);
|
|
|
|
|
|
|
|
|
|
// Setup mock of database to return mock of sql SqlContext
|
|
|
|
|
database.Setup(x => x.SqlContext).Returns(sqlContext.Object);
|
|
|
|
|
|
|
|
|
|
var syntaxProviderMock = new Mock<ISqlSyntaxProvider>();
|
|
|
|
|
|
|
|
|
|
// Setup mock of ISqlContext to return syntaxProviderMock
|
|
|
|
|
sqlContext.Setup(x => x.SqlSyntax).Returns(syntaxProviderMock.Object);
|
|
|
|
|
|
|
|
|
|
return new ScopeProvider(
|
|
|
|
|
new AmbientScopeStack(),
|
|
|
|
|
new AmbientScopeContextStack(),
|
|
|
|
|
lockingMechanismFactory.Object,
|
|
|
|
|
databaseFactory.Object,
|
|
|
|
|
fileSystems,
|
|
|
|
|
new TestOptionsMonitor<CoreDebugSettings>(new CoreDebugSettings()),
|
|
|
|
|
mediaFileManager,
|
|
|
|
|
loggerFactory,
|
|
|
|
|
Mock.Of<IEventAggregator>());
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-19 08:17:35 +01:00
|
|
|
|
2024-02-22 11:22:57 +01:00
|
|
|
public IJsonSerializer JsonSerializer { get; } = new SystemTextJsonSerializer();
|
2020-12-19 08:17:35 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public IVariationContextAccessor VariationContextAccessor { get; } = new TestVariationContextAccessor();
|
2020-12-19 08:17:35 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public abstract IBulkSqlInsertProvider BulkSqlInsertProvider { get; }
|
2020-12-19 08:17:35 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public abstract IMarchal Marchal { get; }
|
2020-12-19 08:17:35 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public CoreDebugSettings CoreDebugSettings { get; } = new();
|
2020-03-13 12:08:25 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public IIOHelper IOHelper
|
|
|
|
|
{
|
|
|
|
|
get
|
2020-03-13 14:43:41 +11:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
if (_ioHelper == null)
|
2020-03-13 14:43:41 +11:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
var hostingEnvironment = GetHostingEnvironment();
|
|
|
|
|
|
|
|
|
|
if (TestEnvironment.IsWindows)
|
2020-11-18 04:49:03 -08:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
_ioHelper = new IOHelperWindows(hostingEnvironment);
|
|
|
|
|
}
|
|
|
|
|
else if (TestEnvironment.IsLinux)
|
|
|
|
|
{
|
|
|
|
|
_ioHelper = new IOHelperLinux(hostingEnvironment);
|
|
|
|
|
}
|
|
|
|
|
else if (TestEnvironment.IsOSX)
|
|
|
|
|
{
|
|
|
|
|
_ioHelper = new IOHelperOSX(hostingEnvironment);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new NotSupportedException("Unexpected OS");
|
2020-11-18 04:49:03 -08:00
|
|
|
}
|
2020-03-13 14:43:41 +11:00
|
|
|
}
|
2022-06-21 08:09:38 +02:00
|
|
|
|
|
|
|
|
return _ioHelper;
|
2020-03-13 14:43:41 +11:00
|
|
|
}
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
2020-03-13 12:08:25 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public IMainDom MainDom { get; }
|
2020-12-19 08:17:35 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public UriUtility UriUtility
|
|
|
|
|
{
|
|
|
|
|
get
|
2020-03-13 14:43:41 +11:00
|
|
|
{
|
Resolved more warnings, and marked more warning types as errors (#16991)
* Fix warnings SA1111, SA1028, SA1500, IDE1270 in Umbraco.Web.Website, and updated rules.
* Remove warnings: IDE0270: Null check can be simplified
* More SqlServer project warnings resolved
* CS0105 namespace appeared already
* Suppress warning until implementation:
#pragma warning disable CS0162 // Unreachable code detected
#pragma warning disable CS0618 // Type or member is obsolete
CS0162 remove unreachable code
SA1028 remove trailing whitespace
SA1106 no empty statements
CS1570 malformed XML
CS1572 corrected xml parameter
CS1573 param tag added
IDE0007 var not explicit
IDE0008 explicit not var
IDE0057 simplify substring
IDE0074 compound assignment
CA1825 array.empty
Down to 3479 warnings
* - SA1116, SA117 params on same line
- IDE0057 substring simplified
Specific warnings for Umbraco.Tests.Benchmarks
* Fixed IDE0074 compound assignment and added specific warnings for Umbraco.Tests.Common
* Specific warnings for Umbraco.Tests.Integration and Umbraco.Tests.Common
Fixed:
- SA1111, SA1116, SA117 params and line formatting (not all as there are many)
- SA1122 string.Empty
- IDE0057 simplify substring
- IDE0044,IDE0044 make field readonly
- IDE1006 naming rule violation (add _)
- SA1111 closing parenthesis on line of last parameter
- SA1649 filename match type name
- SA1312,SA1306 lowercase variable and field names
* Fixed various warnings where they are more straight-forward, including:
- SA1649 file name match type name
- SA111 parenthesis on line of last parameter
- IDE0028 simplify collection initializer
- SA1306 lower-case letter field
- IDE044 readonly field
- SA1122 string.Empty
- SA1116 params same line
- IDE1006 upper casing
- IDE0041 simplify null check
Updated the following projects to only list their remaining specific warning codes:
- Umbraco.Tests.UnitTests
Typo in `Umbraco.Web.Website` project
* Reverted test change
* Now 1556 warnings.
Fixed various warnings where they are more straight-forward, including:
- SA1111/SA1116/SA1119 parenthesis
- SA1117 params
- SA1312 lowercase variable
- SA1121 built-in type
- SA1500/SA1513/SA1503 formatting braces
- SA1400 declare access modifier
- SA1122 string.Empty
- SA1310 no underscore
- IDE0049 name simplified
- IDE0057 simplify substring
- IDE0074 compound assignment
- IDE0032 use auto-property
- IDE0037 simplify member name
- IDE0008 explicit type not var
- IDE0016/IDE0270/IDE0041 simplify null checks
- IDE0048/SA1407 clarity in arithmetic
- IDE1006 correct param names
- IDE0042 deconstruct variable
- IDE0044 readonly
- IDE0018 inline variable declarations
- IDE0074/IDE0054 compound assignment
- IDE1006 naming
- CS1573 param XML
- CS0168 unused variable
Comment formatting in project files for consistency.
Updated all projects to only list remaining specific warning codes as warnings instead of errors (errors is now default).
* Type not var, and more warning exceptions
* Tweaked merge issue, readded comment about rollback
* Readded comment re rollback.
* Readded comments
* Comment tweak
* Comment tweak
2024-09-24 12:56:28 +01:00
|
|
|
_uriUtility ??= new UriUtility(GetHostingEnvironment());
|
2022-06-21 08:09:38 +02:00
|
|
|
|
|
|
|
|
return _uriUtility;
|
2020-03-13 14:43:41 +11:00
|
|
|
}
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
2020-12-19 08:17:35 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public ITypeFinder GetTypeFinder() => _typeFinder;
|
2020-03-13 12:08:25 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public TypeLoader GetMockedTypeLoader() =>
|
|
|
|
|
new(
|
|
|
|
|
Mock.Of<ITypeFinder>(),
|
2024-11-07 12:20:22 +01:00
|
|
|
Mock.Of<ILogger<TypeLoader>>());
|
2020-03-31 17:27:51 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
/// <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>
|
|
|
|
|
public virtual string MapPathForTestFiles(string relativePath)
|
|
|
|
|
{
|
|
|
|
|
if (!relativePath.StartsWith("~/"))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("relativePath must start with '~/'", nameof(relativePath));
|
2020-03-13 12:08:25 +11:00
|
|
|
}
|
|
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var codeBase = typeof(TestHelperBase).Assembly.CodeBase;
|
|
|
|
|
var uri = new Uri(codeBase);
|
|
|
|
|
var path = uri.LocalPath;
|
|
|
|
|
var bin = Path.GetDirectoryName(path);
|
2020-03-13 12:08:25 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
return relativePath.Replace("~/", bin + "/");
|
|
|
|
|
}
|
2020-03-13 12:08:25 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public IUmbracoVersion GetUmbracoVersion() => new UmbracoVersion();
|
2020-12-19 08:17:35 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public IServiceCollection GetRegister() => new ServiceCollection();
|
2020-03-13 12:08:25 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public abstract IHostingEnvironment GetHostingEnvironment();
|
2020-03-13 12:08:25 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public abstract IApplicationShutdownRegistry GetHostingEnvironmentLifetime();
|
2020-03-13 12:08:25 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public abstract IIpResolver GetIpResolver();
|
2020-03-13 12:08:25 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public IRequestCache GetRequestCache() => new DictionaryAppCache();
|
2020-04-22 14:23:56 +10:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public IPublishedUrlProvider GetPublishedUrlProvider()
|
|
|
|
|
{
|
|
|
|
|
var mock = new Mock<IPublishedUrlProvider>();
|
|
|
|
|
|
|
|
|
|
return mock.Object;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ILoggingConfiguration GetLoggingConfiguration(IHostingEnvironment hostingEnv = null)
|
|
|
|
|
{
|
|
|
|
|
hostingEnv ??= GetHostingEnvironment();
|
|
|
|
|
return new LoggingConfiguration(
|
2023-07-10 14:36:20 +02:00
|
|
|
Path.Combine(hostingEnv.ApplicationPhysicalPath, "umbraco", "Logs"));
|
2020-03-13 12:08:25 +11:00
|
|
|
}
|
|
|
|
|
}
|