2017-07-20 11:21:28 +02:00
using System ;
2016-06-07 12:20:10 +02:00
using System.Collections ;
2013-09-06 20:21:47 +02:00
using System.Collections.Generic ;
2016-06-07 12:20:10 +02:00
using System.ComponentModel ;
2012-08-09 04:15:35 +06:00
using System.Configuration ;
2012-07-26 21:12:54 +06:00
using System.IO ;
2013-08-23 16:22:26 +02:00
using System.Linq ;
2012-07-26 21:12:54 +06:00
using System.Reflection ;
2016-11-03 10:39:21 +01:00
using System.Threading ;
2016-06-07 12:20:10 +02:00
using NUnit.Framework ;
2012-11-29 09:07:14 -01:00
using Umbraco.Core ;
using Umbraco.Core.IO ;
2017-11-10 11:27:12 +01:00
using Umbraco.Core.Models ;
2018-01-15 11:32:30 +01:00
using Umbraco.Core.Models.Entities ;
2018-03-19 13:36:31 +01:00
using Umbraco.Core.PropertyEditors ;
2017-11-10 11:27:12 +01:00
using File = System . IO . File ;
2012-07-26 21:12:54 +06:00
2012-07-31 00:02:27 +06:00
namespace Umbraco.Tests.TestHelpers
2012-08-17 04:27:47 +06:00
{
2015-01-22 15:16:10 +11:00
/// <summary>
2017-07-20 11:21:28 +02:00
/// Common helper properties and methods useful to testing
/// </summary>
public static class TestHelper
{
/// <summary>
/// Gets the current assembly directory.
/// </summary>
/// <value>The assembly directory.</value>
static public string CurrentAssemblyDirectory
{
get
{
var codeBase = typeof ( TestHelper ) . Assembly . CodeBase ;
var uri = new Uri ( codeBase ) ;
var path = uri . LocalPath ;
return Path . GetDirectoryName ( path ) ;
}
}
/// <summary>
/// Maps the given <paramref name="relativePath"/> making it rooted on <see cref="CurrentAssemblyDirectory"/>. <paramref name="relativePath"/> must start with <code>~/</code>
/// </summary>
/// <param name="relativePath">The relative path.</param>
/// <returns></returns>
public static string MapPathForTest ( string relativePath )
{
if ( ! relativePath . StartsWith ( "~/" ) )
throw new ArgumentException ( "relativePath must start with '~/'" , "relativePath" ) ;
return relativePath . Replace ( "~/" , CurrentAssemblyDirectory + "/" ) ;
}
public static void InitializeContentDirectories ( )
2012-11-29 09:07:14 -01:00
{
2013-07-10 17:28:15 +10:00
CreateDirectories ( new [ ] { SystemDirectories . Masterpages , SystemDirectories . MvcViews , SystemDirectories . Media , SystemDirectories . AppPlugins } ) ;
2012-11-29 09:07:14 -01:00
}
2017-07-20 11:21:28 +02:00
public static void CleanContentDirectories ( )
{
CleanDirectories ( new [ ] { SystemDirectories . Masterpages , SystemDirectories . MvcViews , SystemDirectories . Media } ) ;
}
2012-11-29 09:07:14 -01:00
2017-07-20 11:21:28 +02:00
public static void CreateDirectories ( string [ ] directories )
2012-11-29 09:07:14 -01:00
{
foreach ( var directory in directories )
{
var directoryInfo = new DirectoryInfo ( IOHelper . MapPath ( directory ) ) ;
if ( directoryInfo . Exists = = false )
Directory . CreateDirectory ( IOHelper . MapPath ( directory ) ) ;
}
}
2017-07-20 11:21:28 +02:00
public static void CleanDirectories ( string [ ] directories )
{
var preserves = new Dictionary < string , string [ ] >
{
{ SystemDirectories . Masterpages , new [ ] { "dummy.txt" } } ,
{ SystemDirectories . MvcViews , new [ ] { "dummy.txt" } }
} ;
2012-11-29 09:07:14 -01:00
foreach ( var directory in directories )
{
var directoryInfo = new DirectoryInfo ( IOHelper . MapPath ( directory ) ) ;
2013-09-06 20:21:47 +02:00
var preserve = preserves . ContainsKey ( directory ) ? preserves [ directory ] : null ;
2018-05-03 15:09:56 +02:00
if ( directoryInfo . Exists )
foreach ( var x in directoryInfo . GetFiles ( ) . Where ( x = > preserve = = null | | preserve . Contains ( x . Name ) = = false ) )
x . Delete ( ) ;
2012-11-29 09:07:14 -01:00
}
}
2013-08-23 16:22:26 +02:00
2017-07-20 11:21:28 +02:00
public static void CleanUmbracoSettingsConfig ( )
2013-08-23 16:22:26 +02:00
{
var currDir = new DirectoryInfo ( CurrentAssemblyDirectory ) ;
var umbracoSettingsFile = Path . Combine ( currDir . Parent . Parent . FullName , "config" , "umbracoSettings.config" ) ;
if ( File . Exists ( umbracoSettingsFile ) )
File . Delete ( umbracoSettingsFile ) ;
}
2017-11-10 11:27:12 +01:00
// fixme obsolete the dateTimeFormat thing and replace with dateDelta
public static void AssertPropertyValuesAreEqual ( object actual , object expected , string dateTimeFormat = null , Func < IEnumerable , IEnumerable > sorter = null , string [ ] ignoreProperties = null )
2016-06-07 12:20:10 +02:00
{
2017-11-10 11:27:12 +01:00
const int dateDeltaMilliseconds = 500 ; // .5s
2016-06-07 12:20:10 +02:00
var properties = expected . GetType ( ) . GetProperties ( ) ;
foreach ( var property in properties )
{
2017-11-10 11:27:12 +01:00
// ignore properties that are attributed with EditorBrowsableState.Never
2016-06-07 12:20:10 +02:00
var att = property . GetCustomAttribute < EditorBrowsableAttribute > ( false ) ;
if ( att ! = null & & att . State = = EditorBrowsableState . Never )
continue ;
2017-11-10 11:27:12 +01:00
// ignore explicitely ignored properties
2016-06-07 12:20:10 +02:00
if ( ignoreProperties ! = null & & ignoreProperties . Contains ( property . Name ) )
continue ;
var actualValue = property . GetValue ( actual , null ) ;
2017-11-10 11:27:12 +01:00
var expectedValue = property . GetValue ( expected , null ) ;
2016-06-07 12:20:10 +02:00
2017-11-10 11:27:12 +01:00
AssertAreEqual ( property , expectedValue , actualValue , sorter , dateDeltaMilliseconds ) ;
}
}
2016-10-10 19:18:49 +02:00
2017-11-10 11:27:12 +01: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 )
{
// compare lists
AssertListsAreEqual ( property , ( IEnumerable ) actual , ( IEnumerable ) expected , 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 ;
2018-04-21 09:57:28 +02:00
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 ( ) ;
2017-11-10 11:27:12 +01:00
if ( expectedPropertyValues . Length ! = actualPropertyValues . Length )
Assert . Fail ( $"{property.DeclaringType.Name}.{property.Name}: Expected {expectedPropertyValues.Length} but got {actualPropertyValues.Length}." ) ;
for ( var i = 0 ; i < expectedPropertyValues . Length ; i + + )
2016-06-07 12:20:10 +02:00
{
2017-11-21 14:19:16 +01:00
Assert . AreEqual ( 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}\"." ) ;
2016-06-07 12:20:10 +02:00
}
}
2018-03-19 13:36:31 +01: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?
}
2017-11-10 11:27:12 +01:00
else
{
// directly compare values
2018-02-07 13:35:59 +01:00
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>" ) ;
2017-11-10 11:27:12 +01:00
}
2016-06-07 12:20:10 +02:00
}
2017-11-10 11:27:12 +01:00
private static void AssertListsAreEqual ( PropertyInfo property , IEnumerable expected , IEnumerable actual , Func < IEnumerable , IEnumerable > sorter = null , int dateDeltaMilliseconds = 0 )
2016-06-07 12:20:10 +02:00
{
if ( sorter = = null )
{
2017-11-10 11:27:12 +01:00
// this is pretty hackerific but saves us some code to write
2016-06-07 12:20:10 +02:00
sorter = enumerable = >
{
2017-11-10 11:27:12 +01:00
// semi-generic way of ensuring any collection of IEntity are sorted by Ids for comparison
2016-06-07 12:20:10 +02:00
var entities = enumerable . OfType < IEntity > ( ) . ToList ( ) ;
2017-11-10 11:27:12 +01:00
return entities . Count > 0 ? ( IEnumerable ) entities . OrderBy ( x = > x . Id ) : entities ;
2016-06-07 12:20:10 +02:00
} ;
2017-07-20 11:21:28 +02:00
}
2016-06-07 12:20:10 +02:00
2017-11-10 11:27:12 +01:00
var expectedListEx = sorter ( expected ) . Cast < object > ( ) . ToList ( ) ;
var actualListEx = sorter ( actual ) . Cast < object > ( ) . ToList ( ) ;
2016-06-07 12:20:10 +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 ) ;
2017-11-10 11:27:12 +01:00
for ( var i = 0 ; i < actualListEx . Count ; i + + )
AssertAreEqual ( property , expectedListEx [ i ] , actualListEx [ i ] , sorter , dateDeltaMilliseconds ) ;
2016-06-07 12:20:10 +02:00
}
2016-11-03 10:39:21 +01:00
public static void DeleteDirectory ( string path )
{
Try ( ( ) = >
{
2016-10-31 11:08:28 +01:00
if ( Directory . Exists ( path ) = = false ) return ;
2016-11-03 10:39:21 +01:00
foreach ( var file in Directory . EnumerateFiles ( path , "*" , SearchOption . AllDirectories ) )
File . Delete ( file ) ;
} ) ;
Try ( ( ) = >
{
2016-10-31 11:08:28 +01:00
if ( Directory . Exists ( path ) = = false ) return ;
2016-11-03 10:39:21 +01:00
Directory . Delete ( path , true ) ;
} ) ;
}
public static void TryAssert ( Action action , int maxTries = 5 , int waitMilliseconds = 200 )
{
Try < AssertionException > ( action , maxTries , waitMilliseconds ) ;
}
public static void Try ( Action action , int maxTries = 5 , int waitMilliseconds = 200 )
{
Try < Exception > ( action , maxTries , waitMilliseconds ) ;
}
2013-08-23 16:22:26 +02:00
2016-11-03 10:39:21 +01:00
public static void Try < T > ( Action action , int maxTries = 5 , int waitMilliseconds = 200 )
where T : Exception
{
var tries = 0 ;
while ( true )
{
try
{
action ( ) ;
break ;
}
catch ( T )
{
if ( tries + + > maxTries )
throw ;
Thread . Sleep ( waitMilliseconds ) ;
}
}
}
2016-06-07 12:20:10 +02:00
}
2017-07-20 11:21:28 +02:00
}