2020-12-20 08:36:11 +01:00
// Copyright (c) Umbraco.
// See LICENSE for more details.
2020-09-28 15:43:28 +02:00
using System.Collections ;
using System.Collections.Generic ;
using System.ComponentModel ;
using System.IO ;
using System.Linq ;
using System.Reflection ;
2020-09-30 14:54:53 +02:00
using Microsoft.AspNetCore.Hosting ;
2020-10-27 10:53:01 +00:00
using Microsoft.Extensions.DependencyInjection ;
2021-08-09 13:42:52 -06:00
using Microsoft.Extensions.Logging.Abstractions ;
2020-12-20 08:36:11 +01:00
using Microsoft.Extensions.Options ;
2020-09-28 15:43:28 +02:00
using Moq ;
using NUnit.Framework ;
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 ;
2021-06-01 06:41:56 +02:00
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.Mail ;
using Umbraco.Cms.Core.Models ;
using Umbraco.Cms.Core.Models.Entities ;
using Umbraco.Cms.Core.Models.PublishedContent ;
using Umbraco.Cms.Core.Net ;
using Umbraco.Cms.Core.PropertyEditors ;
using Umbraco.Cms.Core.Routing ;
using Umbraco.Cms.Core.Runtime ;
2023-10-10 11:51:47 +02:00
using Umbraco.Cms.Core.Scoping ;
2021-02-09 10:22:42 +01:00
using Umbraco.Cms.Core.Serialization ;
using Umbraco.Cms.Core.Strings ;
2021-08-19 14:15:14 +01:00
using Umbraco.Cms.Infrastructure.Mail ;
2024-11-19 08:48:20 +01:00
using Umbraco.Cms.Infrastructure.Mail.Interfaces ;
2021-02-12 13:36:50 +01:00
using Umbraco.Cms.Infrastructure.Persistence ;
using Umbraco.Cms.Infrastructure.Persistence.Mappers ;
v10 SQLite support + distributed locking abstractions (#11922)
* Created Persistence.SQLite project skeleton.
* SQLite database initialization
* Various changes and hacks to make things work.
* WIP integration tests
* Fix thread safety tests
* Fix tests that relied on tie breaker sorting.
Spent a fair amount of time looking for a less lazy fix but gave up.
* Convert right join to left join ContentTypeRepository.PerformGetByQuery
SQLite doesn't support right join
* Fix test Can_Generate_Delete_SubQuery_Statement
Worth noting that NPoco.DatabaseTypes.SQLiteDatabaseType doesn't override
EscapeSqlIdentifier so NPoco will escape with [].
SQLite docs say > "A keyword enclosed in square brackets is an identifier.
This is not standard SQL.
This quoting mechanism is used by MS Access and SQL Server and is
included in SQLite for compatibility."
Also could have updated SqliteSyntaxProvider to match npoco but
decided against it.
* Fixes for paginated custom order by
* Fix tests broken by lack of unique indexes.
* Fix SqlServerTableByTableTest tests.
These tests didn't actually do anything as the tables already exist so schema creator just returned.
Did however point out that the default implementation for DoesTableExist just returns false so added a default naive implementation.
* Fix ValidateLoginSession - SelectTop must come later
* dry up database cleanup
* Fix up db migration tests.
We can't drop pk in sqlite without recreating table.
Test looks to be testing that add column works as intended which we can test.
* Prevent schema creation errors.
* SQLite ignore lock tests, WAL back on.
* Fix package schema tests
* Fix NPocoFetchTests - case sensitivity not under test
* Fix AdvancedMigrationTests (where possible)
Migrations probably need a good look later.
Maybe nuke old migrations and only support moving to v10 from v9.
If we do that can do some cleanup.
* Cleanup test database configuration
* Run integration tests against SQLite on build agent.
* Drop MS.Data.SQLite
System.Data.SQLite was quicker to roll out due to more CLR type mapping
* YAML
* Skip Umbraco.Tests.Integration.SqlCe
* Drop SqlServerTableByTable tests.
Until this week they did nothing anyway as they with NewSchemaPerTest
so the tests all passed as CreateTable was no op (already exists).
Also all of the tables are created in an empty database by SchemaValidationTest.cs
DatabaseSchemaCreation_Produces_DatabaseSchemaResult_With_Zero_Errors
* Might aswell run against macOS also.
* Copy azure pipelines task header layout
* Delete SQLCe projects
* Remove SQL CE specific code.
* Remove SQL CE NuSpec, template params, build script setup
* Delete umbraco-netcore-only.sln
* Add SkipTests solution configuration and use for codeql
* Remove reference to deleted nuspec file.
* Refactor ConnectionStrings WRT DataDirectory placeholder & ProviderName.
At this point you can try out SQLite support by setting the following
in appsettings.json and then completing the install process.
"ConnectionStrings": {
"umbracoDbDSN": "Data Source=|DataDirectory|/umbraco.sqlite",
"umbracoDbDSN_ProviderName": "System.Data.SQLite"
},
Not currently possible via installer UI without provider name pre-set in
configuration.
* Switch to Microsoft.Data.Sqlite
Some gross hacks but will be good to find out if this works
with apple silicon.
* Enable selection of SQLite via installer UI (also quick install)
* Remove SqlServerDbProviderFactoryCreator to cleanup a TODO
* Move SQL Server support to its own class library
* Add persistence dependencies to Umbraco.CMS metapackage
* Bugfix packages delete query
Created invalid query for SQLite.
* Try out cypress tests Linux + SQLite
* Prevent cypress test artifact upload failure on attempt 2+
* LocalDb bugfixes
* Drop redundant enum
* Move SqlClient constant
* Misc whitespace
* Remove IsSqlCe extension (TODO: drop non 9->10 migrations later).
* Umbraco.Persistence.* -> Umbraco.Cms.Persistence.*
* Display quick install defaults and per provider default database name.
* Misc remove old comment
* little re-arrange
* Remove almost all usages of IsSqlite extension.
* visual adjustments
* Custom Database Configuration is last step and should then say Install.
* use text instead of disabled inputs
* move legend, rename to Install
* Update SqlMainDomLock to work without distributed locks.
* Added IDistributedLockingMechanism interface and in memory impl.
* Drop locking from ISqlSyntaxProvider & wire up scope to abstraction.
* Added SqlServerDistributedLockingMechanism
* Move distributed locking interfaces and exceptions to Core + xmldocs.
* Fix tests, Misc cleanup, Add SQL distributed locking integration tests
* Provide mechanism to specify DistributedLockingMechanism in config
(even if added by composer)
* Nomplementation -> NoImplementation
* Fix misleading comment
* Integration tests use SqlServerDistributedLockingMechanism when possible
* Handle up-gradable locks SqlServerDistributedLockingMechanism.
TODO: InMemoryDistributedLockingMechanism.
Note: Nuked SqlServerDistributedLockingMechanismTests, will still sleep
at night.
Is covered by Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.LockTests
* Make tests pass for InMemoryDistributedLockingMechanism, pretty hacky.
* Tweak constraints on WithCollectionBuilder so i can drop bad constructor
* Added SqliteDistributedLockingMechanism
* Dropped InMemoryDistributedMechanism + magic
InMemoryDistributedMechanism was pretty rubbish and now we have
a decent implementation for SQLite as we no longer block readers
see 8d1f42b.
Also drop the CollectionBuilder setup, instead do the same as we do
for syntax providers etc, it's more automagical so we never require an
explicit selection although we are allowing for it.
However keeping the optional IUmbracoBuilder constructor param for
CollectionBuilders as it's extremely useful.
* Fix quick install "" database name.
* Hide Database Configuration section when a connection string is pre-set.
Doesn't seem worth it to extract db name from connection string.
* Ensure wal test 2+
* Fix logging inconsistencies.
* Ensure in transaction when obtaining locks + no-op the SQLite read lock.
There's no point in running the query just to make a single test pass.
* Fix installer database display names
* Allow SQLite shared cache without losing deferred transactions
* Opt into shared cache for new SQLite databases + fix filename
* Fix misc inconsistency in .gitignore
* Prefer our interceptor interface
* Restore DEBUG_DATABASES code OnConnectionOpened in case it's used.
* Back to private cache.
* Added retry strategy for SQLite + refactor out SQL server specific stuff
* Fix SQL server tests.
* Misc - Orphaned comment, incorrect casing.
* InMemory SQLite test database & turn shared cache back on everywhere.
Co-authored-by: Niels Lyngsø <niels.lyngso@gmail.com>
2022-03-11 16:14:20 +00:00
using Umbraco.Cms.Persistence.SqlServer.Services ;
2021-02-10 14:45:44 +01:00
using Umbraco.Cms.Tests.Common ;
Examine 2.0 integration (#10241)
* Init commit for examine 2.0 work, most old umb examine tests working, probably a lot that doesn't
* Gets Umbraco Examine tests passing and makes some sense out of them, fixes some underlying issues.
* Large refactor, remove TaskHelper, rename Notifications to be consistent, Gets all examine/lucene indexes building and startup ordered in the correct way, removes old files, creates new IUmbracoIndexingHandler for abstracting out all index operations for umbraco data, abstracts out IIndexRebuilder, Fixes Stack overflow with LiveModelsProvider and loading assemblies, ports some changes from v8 for startup handling with cold boots, refactors out LastSyncedFileManager
* fix up issues with rebuilding and management dashboard.
* removes old files, removes NetworkHelper, fixes LastSyncedFileManager implementation to ensure the machine name is used, fix up logging with cold boot state.
* Makes MainDom safer to use and makes PublishedSnapshotService lazily register with MainDom
* lazily acquire application id (fix unit tests)
* Fixes resource casing and missing test file
* Ensures caches when requiring internal services for PublishedSnapshotService, UseNuCache is a separate call, shouldn't be buried in AddWebComponents, was also causing issues in integration tests since nucache was being used for the Id2Key service.
* For UmbracoTestServerTestBase enable nucache services
* Fixing tests
* Fix another test
* Fixes tests, use TestHostingEnvironment, make Tests.Common use net5, remove old Lucene.Net.Contrib ref.
* Fixes up some review notes
* Fixes issue with doubly registering PublishedSnapshotService meanig there could be 2x instances of it
* Checks for parseexception when executing the query
* Use application root instead of duplicating functionality.
* Added Examine project to netcore only solution file
* Fixed casing issue with LazyLoad, that is not lowercase.
* uses cancellationToken instead of bool flag, fixes always reading lastId from the LastSyncedFileManager, fixes RecurringHostedServiceBase so that there isn't an overlapping thread for the same task type
* Fix tests
* remove legacy test project from solution file
* Fix test
Co-authored-by: Bjarke Berg <mail@bergmania.dk>
2021-05-18 18:31:38 +10:00
using Umbraco.Cms.Tests.Common.Testing ;
2021-02-09 13:32:34 +01:00
using Umbraco.Extensions ;
2020-09-28 15:43:28 +02:00
using File = System . IO . File ;
2021-02-09 10:22:42 +01:00
using IHostingEnvironment = Umbraco . Cms . Core . Hosting . IHostingEnvironment ;
2023-10-10 11:51:47 +02:00
using IScopeProvider = Umbraco . Cms . Infrastructure . Scoping . IScopeProvider ;
2020-09-28 15:43:28 +02:00
2022-06-21 08:09:38 +02:00
namespace Umbraco.Cms.Tests.UnitTests.TestHelpers ;
/// <summary>
/// Common helper properties and methods useful to testing
/// </summary>
public static class TestHelper
2020-09-28 15:43:28 +02:00
{
2022-06-21 08:09:38 +02:00
private static readonly TestHelperInternal s_testHelperInternal = new ( ) ;
2020-09-28 15:43:28 +02:00
/// <summary>
2022-06-21 08:09:38 +02:00
/// Gets the working directory of the test project.
2020-09-28 15:43:28 +02:00
/// </summary>
2022-06-21 08:09:38 +02:00
/// <value>The assembly directory.</value>
public static string WorkingDirectory = > s_testHelperInternal . WorkingDirectory ;
2020-09-28 15:43:28 +02:00
2023-10-10 11:51:47 +02:00
public static IScopeProvider ScopeProvider = > s_testHelperInternal . ScopeProvider ;
public static ICoreScopeProvider CoreScopeProvider = > s_testHelperInternal . ScopeProvider ;
2022-06-21 08:09:38 +02:00
public static IShortStringHelper ShortStringHelper = > s_testHelperInternal . ShortStringHelper ;
2020-09-28 15:43:28 +02:00
2022-06-21 08:09:38 +02:00
public static IJsonSerializer JsonSerializer = > s_testHelperInternal . JsonSerializer ;
2020-09-28 15:43:28 +02:00
2022-06-21 08:09:38 +02:00
public static IVariationContextAccessor VariationContextAccessor = > s_testHelperInternal . VariationContextAccessor ;
2020-10-21 08:50:15 +02:00
2022-06-21 08:09:38 +02:00
public static IBulkSqlInsertProvider BulkSqlInsertProvider = > s_testHelperInternal . BulkSqlInsertProvider ;
2020-10-21 08:50:15 +02:00
2022-06-21 08:09:38 +02:00
public static IMarchal Marchal = > s_testHelperInternal . Marchal ;
2020-09-28 15:43:28 +02:00
2022-06-21 08:09:38 +02:00
public static CoreDebugSettings CoreDebugSettings = > s_testHelperInternal . CoreDebugSettings ;
2020-09-28 15:43:28 +02:00
2022-06-21 08:09:38 +02:00
public static IIOHelper IOHelper = > s_testHelperInternal . IOHelper ;
2020-09-28 15:43:28 +02:00
2022-06-21 08:09:38 +02:00
public static IMainDom MainDom = > s_testHelperInternal . MainDom ;
2020-09-28 15:43:28 +02:00
2022-06-21 08:09:38 +02:00
public static UriUtility UriUtility = > s_testHelperInternal . UriUtility ;
2020-12-20 08:36:11 +01:00
2024-11-19 08:48:20 +01:00
public static IEmailSender EmailSender { get ; } = new EmailSender ( new NullLogger < EmailSender > ( ) , new TestOptionsMonitor < GlobalSettings > ( new GlobalSettings ( ) ) , Mock . Of < IEventAggregator > ( ) , Mock . Of < IEmailSenderClient > ( ) , null , null ) ;
2020-12-20 08:36:11 +01:00
2022-06-21 08:09:38 +02:00
public static ITypeFinder GetTypeFinder ( ) = > s_testHelperInternal . GetTypeFinder ( ) ;
2020-12-20 08:36:11 +01:00
2022-06-21 08:09:38 +02:00
public static TypeLoader GetMockedTypeLoader ( ) = > s_testHelperInternal . GetMockedTypeLoader ( ) ;
2020-12-20 08:36:11 +01:00
2022-06-21 08:09:38 +02:00
public static Lazy < ISqlContext > GetMockSqlContext ( )
{
var sqlContext = Mock . Of < ISqlContext > ( ) ;
var syntax = new SqlServerSyntaxProvider ( Options . Create ( new GlobalSettings ( ) ) ) ;
Mock . Get ( sqlContext ) . Setup ( x = > x . SqlSyntax ) . Returns ( syntax ) ;
return new Lazy < ISqlContext > ( ( ) = > sqlContext ) ;
}
2020-12-20 08:36:11 +01:00
2022-06-21 08:09:38 +02:00
public static MapperConfigurationStore CreateMaps ( ) = > new ( ) ;
2020-09-28 15:43:28 +02: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 static string MapPathForTestFiles ( string relativePath ) = >
s_testHelperInternal . MapPathForTestFiles ( relativePath ) ;
2020-09-28 15:43:28 +02:00
2022-06-21 08:09:38 +02:00
public static void InitializeContentDirectories ( ) = > CreateDirectories ( new [ ]
{
Constants . SystemDirectories . MvcViews ,
new GlobalSettings ( ) . UmbracoMediaPhysicalRootPath ,
Constants . SystemDirectories . AppPlugins ,
} ) ;
2020-09-28 15:43:28 +02:00
2022-06-21 08:09:38 +02:00
public static void CleanContentDirectories ( ) = > CleanDirectories ( new [ ]
{
Constants . SystemDirectories . MvcViews ,
new GlobalSettings ( ) . UmbracoMediaPhysicalRootPath ,
} ) ;
2020-09-28 15:43:28 +02:00
2022-06-21 08:09:38 +02:00
public static void CreateDirectories ( string [ ] directories )
{
foreach ( var directory in directories )
2020-09-28 15:43:28 +02:00
{
2022-06-21 08:09:38 +02:00
var directoryInfo = new DirectoryInfo ( IOHelper . MapPath ( directory ) ) ;
if ( directoryInfo . Exists = = false )
2020-09-28 15:43:28 +02:00
{
2022-06-21 08:09:38 +02:00
Directory . CreateDirectory ( IOHelper . MapPath ( directory ) ) ;
2020-09-28 15:43:28 +02:00
}
}
2022-06-21 08:09:38 +02:00
}
2020-09-28 15:43:28 +02:00
2022-06-21 08:09:38 +02:00
public static void CleanDirectories ( string [ ] directories )
{
var preserves = new Dictionary < string , string [ ] > { { Constants . SystemDirectories . MvcViews , new [ ] { "dummy.txt" } } } ;
2020-12-20 08:36:11 +01:00
2022-06-21 08:09:38 +02:00
foreach ( var directory in directories )
{
var directoryInfo = new DirectoryInfo ( IOHelper . MapPath ( directory ) ) ;
var preserve = preserves . ContainsKey ( directory ) ? preserves [ directory ] : null ;
if ( directoryInfo . Exists )
2020-09-28 15:43:28 +02:00
{
2022-06-21 08:09:38 +02:00
foreach ( var fileInfo in directoryInfo . GetFiles ( )
. Where ( x = > preserve = = null | | preserve . Contains ( x . Name ) = = false ) )
2020-12-20 08:36:11 +01:00
{
2022-06-21 08:09:38 +02:00
fileInfo . Delete ( ) ;
2020-12-20 08:36:11 +01:00
}
2020-09-28 15:43:28 +02:00
}
}
2022-06-21 08:09:38 +02:00
}
2020-09-28 15:43:28 +02:00
2022-06-21 08:09:38 +02:00
public static void CleanUmbracoSettingsConfig ( )
{
var currDir = new DirectoryInfo ( WorkingDirectory ) ;
2020-09-28 15:43:28 +02:00
2022-06-21 08:09:38 +02:00
var umbracoSettingsFile = Path . Combine ( currDir . Parent . Parent . FullName , "config" , "umbracoSettings.config" ) ;
if ( File . Exists ( umbracoSettingsFile ) )
{
File . Delete ( umbracoSettingsFile ) ;
2020-09-28 15:43:28 +02:00
}
2022-06-21 08:09:38 +02:00
}
2020-09-28 15:43:28 +02:00
2022-06-21 08:09:38 +02:00
// TODO: Move to Assertions or AssertHelper
2023-01-18 12:28:54 +01:00
// TODO: obsolete the dateTimeFormat thing and replace with dateDelta
2022-06-21 08:09:38 +02:00
public static void AssertPropertyValuesAreEqual (
object actual ,
object expected ,
string dateTimeFormat = null ,
Func < IEnumerable , IEnumerable > sorter = null ,
string [ ] ignoreProperties = null )
{
const int dateDeltaMilliseconds = 500 ; // .5s
2020-09-28 15:43:28 +02:00
2022-06-21 08:09:38 +02:00
var properties = expected . GetType ( ) . GetProperties ( ) ;
foreach ( var property in properties )
{
// ignore properties that are attributed with EditorBrowsableState.Never
var att = property . GetCustomAttribute < EditorBrowsableAttribute > ( false ) ;
if ( att ! = null & & att . State = = EditorBrowsableState . Never )
2020-09-28 15:43:28 +02:00
{
2022-06-21 08:09:38 +02:00
continue ;
}
2020-09-28 15:43:28 +02:00
2022-06-21 08:09:38 +02:00
// ignore explicitely ignored properties
if ( ignoreProperties ! = null & & ignoreProperties . Contains ( property . Name ) )
{
continue ;
}
2020-09-28 15:43:28 +02:00
2022-06-21 08:09:38 +02:00
var actualValue = property . GetValue ( actual , null ) ;
var expectedValue = property . GetValue ( expected , null ) ;
2020-09-28 15:43:28 +02:00
2022-06-21 08:09:38 +02:00
AssertAreEqual ( property , expectedValue , actualValue , sorter , dateDeltaMilliseconds ) ;
2020-09-28 15:43:28 +02:00
}
2022-06-21 08:09:38 +02:00
}
2020-09-28 15:43:28 +02:00
2022-06-21 08:09:38 +02:00
private static void AssertAreEqual ( PropertyInfo property , object expected , object actual , Func < IEnumerable , IEnumerable > sorter = null , int dateDeltaMilliseconds = 0 )
{
if ( ! ( expected is string ) & & expected is IEnumerable enumerable )
2020-09-28 15:43:28 +02:00
{
2022-06-21 08:09:38 +02:00
// sort property collection by alias, not by property ids
// on members, built-in properties don't have ids (always zero)
if ( expected is PropertyCollection )
2020-09-28 15:43:28 +02:00
{
2022-06-21 08:09:38 +02:00
sorter = e = > ( ( PropertyCollection ) e ) . OrderBy ( x = > x . Alias ) ;
2020-09-28 15:43:28 +02:00
}
2020-12-20 08:36:11 +01:00
2022-06-21 08:09:38 +02:00
// compare lists
AssertListsAreEqual ( property , ( IEnumerable ) actual , enumerable , sorter , dateDeltaMilliseconds ) ;
}
else if ( expected is DateTime expectedDateTime )
{
// compare date & time with delta
var actualDateTime = ( DateTime ) actual ;
var delta = ( actualDateTime - expectedDateTime ) . TotalMilliseconds ;
Assert . IsTrue (
Math . Abs ( delta ) < = dateDeltaMilliseconds ,
"Property {0}.{1} does not match. Expected: {2} but was: {3}" ,
property . DeclaringType . Name ,
property . Name ,
expected ,
actual ) ;
}
else if ( expected is Property expectedProperty )
{
// compare values
var actualProperty = ( Property ) actual ;
var expectedPropertyValues =
expectedProperty . Values . OrderBy ( x = > x . Culture ) . ThenBy ( x = > x . Segment ) . ToArray ( ) ;
var actualPropertyValues = actualProperty . Values . OrderBy ( x = > x . Culture ) . ThenBy ( x = > x . Segment ) . ToArray ( ) ;
if ( expectedPropertyValues . Length ! = actualPropertyValues . Length )
2020-09-28 15:43:28 +02:00
{
2022-06-21 08:09:38 +02:00
Assert . Fail (
$"{property.DeclaringType.Name}.{property.Name}: Expected {expectedPropertyValues.Length} but got {actualPropertyValues.Length}." ) ;
2020-09-28 15:43:28 +02:00
}
2022-06-21 08:09:38 +02:00
for ( var i = 0 ; i < expectedPropertyValues . Length ; i + + )
2020-09-28 15:43:28 +02:00
{
2020-12-20 08:36:11 +01:00
Assert . AreEqual (
2022-06-21 08:09:38 +02:00
expectedPropertyValues [ i ] . EditedValue ,
actualPropertyValues [ i ] . EditedValue ,
$"{property.DeclaringType.Name}.{property.Name}: Expected draft value \" { expectedPropertyValues [ i ] . EditedValue } \ " but got \"{actualPropertyValues[i].EditedValue}\"." ) ;
Assert . AreEqual (
expectedPropertyValues [ i ] . PublishedValue ,
actualPropertyValues [ i ] . PublishedValue ,
$"{property.DeclaringType.Name}.{property.Name}: Expected published value \" { expectedPropertyValues [ i ] . EditedValue } \ " but got \"{actualPropertyValues[i].EditedValue}\"." ) ;
2020-09-28 15:43:28 +02:00
}
}
2022-06-21 08:09:38 +02:00
else if ( expected is IDataEditor expectedEditor )
{
Assert . IsInstanceOf < IDataEditor > ( actual ) ;
var actualEditor = ( IDataEditor ) actual ;
Assert . AreEqual ( expectedEditor . Alias , actualEditor . Alias ) ;
// what else shall we test?
}
else
{
// directly compare values
Assert . AreEqual (
expected ,
actual ,
"Property {0}.{1} does not match. Expected: {2} but was: {3}" ,
property . DeclaringType . Name ,
property . Name ,
expected ? . ToString ( ) ? ? "<null>" ,
actual ? . ToString ( ) ? ? "<null>" ) ;
}
}
2020-09-28 15:43:28 +02:00
2022-06-21 08:09:38 +02:00
private static void AssertListsAreEqual (
PropertyInfo property ,
IEnumerable expected ,
IEnumerable actual ,
Func < IEnumerable , IEnumerable > sorter = null ,
int dateDeltaMilliseconds = 0 )
{
if ( sorter = = null )
2020-09-28 15:43:28 +02:00
{
2022-06-21 08:09:38 +02:00
// this is pretty hackerific but saves us some code to write
sorter = enumerable = >
2020-09-28 15:43:28 +02:00
{
2022-06-21 08:09:38 +02:00
// semi-generic way of ensuring any collection of IEntity are sorted by Ids for comparison
var entities = enumerable . OfType < IEntity > ( ) . ToList ( ) ;
return entities . Count > 0 ? entities . OrderBy ( x = > x . Id ) : entities ;
} ;
}
2020-09-28 15:43:28 +02:00
2022-06-21 08:09:38 +02:00
var expectedListEx = sorter ( expected ) . Cast < object > ( ) . ToList ( ) ;
var actualListEx = sorter ( actual ) . Cast < object > ( ) . ToList ( ) ;
2020-09-28 15:43:28 +02:00
2022-06-21 08:09:38 +02:00
if ( actualListEx . Count ! = expectedListEx . Count )
{
Assert . Fail (
"Collection {0}.{1} does not match. Expected IEnumerable containing {2} elements but was IEnumerable containing {3} elements" ,
property . PropertyType . Name ,
property . Name ,
expectedListEx . Count ,
actualListEx . Count ) ;
}
2020-09-28 15:43:28 +02:00
2022-06-21 08:09:38 +02:00
for ( var i = 0 ; i < actualListEx . Count ; i + + )
{
AssertAreEqual ( property , expectedListEx [ i ] , actualListEx [ i ] , sorter , dateDeltaMilliseconds ) ;
2020-09-28 15:43:28 +02:00
}
2022-06-21 08:09:38 +02:00
}
public static IUmbracoVersion GetUmbracoVersion ( ) = > s_testHelperInternal . GetUmbracoVersion ( ) ;
public static IServiceCollection GetServiceCollection ( ) = > new ServiceCollection ( ) . AddLazySupport ( ) ;
public static IHostingEnvironment GetHostingEnvironment ( ) = > s_testHelperInternal . GetHostingEnvironment ( ) ;
public static ILoggingConfiguration GetLoggingConfiguration ( IHostingEnvironment hostingEnv ) = >
s_testHelperInternal . GetLoggingConfiguration ( hostingEnv ) ;
public static IApplicationShutdownRegistry GetHostingEnvironmentLifetime ( ) = >
s_testHelperInternal . GetHostingEnvironmentLifetime ( ) ;
2020-09-28 15:43:28 +02:00
2022-06-21 08:09:38 +02:00
public static IIpResolver GetIpResolver ( ) = > s_testHelperInternal . GetIpResolver ( ) ;
2020-09-28 15:43:28 +02:00
2022-06-21 08:09:38 +02:00
public static IRequestCache GetRequestCache ( ) = > s_testHelperInternal . GetRequestCache ( ) ;
2020-09-28 15:43:28 +02:00
2022-06-21 08:09:38 +02:00
public static IPublishedUrlProvider GetPublishedUrlProvider ( ) = > s_testHelperInternal . GetPublishedUrlProvider ( ) ;
2020-09-28 15:43:28 +02:00
2022-06-21 08:09:38 +02:00
private class TestHelperInternal : TestHelperBase
{
public TestHelperInternal ( )
: base ( typeof ( TestHelperInternal ) . Assembly )
{
}
2020-09-28 15:43:28 +02:00
2022-06-21 08:09:38 +02:00
public override IBulkSqlInsertProvider BulkSqlInsertProvider { get ; } = Mock . Of < IBulkSqlInsertProvider > ( ) ;
2020-09-28 15:43:28 +02:00
2022-06-21 08:09:38 +02:00
public override IMarchal Marchal { get ; } = Mock . Of < IMarchal > ( ) ;
public override IHostingEnvironment GetHostingEnvironment ( )
{
var testPath = TestContext . CurrentContext . TestDirectory . Split ( "bin" ) [ 0 ] ;
return new TestHostingEnvironment (
Mock . Of < IOptionsMonitor < HostingSettings > > ( x = > x . CurrentValue = = new HostingSettings ( ) ) ,
Mock . Of < IOptionsMonitor < WebRoutingSettings > > ( x = > x . CurrentValue = = new WebRoutingSettings ( ) ) ,
Mock . Of < IWebHostEnvironment > (
x = >
x . WebRootPath = = "/" & &
x . ContentRootPath = = testPath ) ) ;
}
2020-09-28 15:43:28 +02:00
2022-06-21 08:09:38 +02:00
public override IApplicationShutdownRegistry GetHostingEnvironmentLifetime ( )
= > Mock . Of < IApplicationShutdownRegistry > ( ) ;
2020-09-30 14:54:53 +02:00
2022-06-21 08:09:38 +02:00
public override IIpResolver GetIpResolver ( )
= > Mock . Of < IIpResolver > ( ) ;
2020-09-28 15:43:28 +02:00
}
}