2012-07-26 21:12:54 +06:00
using System ;
2013-09-06 20:21:47 +02:00
using System.Collections.Generic ;
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 ;
2012-08-09 04:15:35 +06:00
using SqlCE4Umbraco ;
2012-11-29 09:07:14 -01:00
using Umbraco.Core ;
using Umbraco.Core.IO ;
2012-08-09 04:15:35 +06:00
using umbraco.DataLayer ;
2012-08-17 04:27:47 +06:00
using GlobalSettings = umbraco . GlobalSettings ;
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>
2012-07-26 21:12:54 +06:00
/// Common helper properties and methods useful to testing
/// </summary>
public static class TestHelper
{
2012-08-09 04:15:35 +06:00
/// <summary>
/// Clears an initialized database
/// </summary>
public static void ClearDatabase ( )
{
2012-12-31 13:57:29 -01:00
var databaseSettings = ConfigurationManager . ConnectionStrings [ Core . Configuration . GlobalSettings . UmbracoConnectionName ] ;
2013-01-14 15:06:23 -01:00
var dataHelper = DataLayerHelper . CreateSqlHelper ( databaseSettings . ConnectionString , false ) as SqlCEHelper ;
2012-12-31 13:57:29 -01:00
2012-08-09 04:15:35 +06:00
if ( dataHelper = = null )
throw new InvalidOperationException ( "The sql helper for unit tests must be of type SqlCEHelper, check the ensure the connection string used for this test is set to use SQLCE" ) ;
2012-12-31 13:57:29 -01:00
2012-08-09 04:15:35 +06:00
dataHelper . ClearDatabase ( ) ;
}
2013-01-30 14:46:38 -01:00
public static void DropForeignKeys ( string table )
{
var databaseSettings = ConfigurationManager . ConnectionStrings [ Core . Configuration . GlobalSettings . UmbracoConnectionName ] ;
var dataHelper = DataLayerHelper . CreateSqlHelper ( databaseSettings . ConnectionString , false ) as SqlCEHelper ;
if ( dataHelper = = null )
throw new InvalidOperationException ( "The sql helper for unit tests must be of type SqlCEHelper, check the ensure the connection string used for this test is set to use SQLCE" ) ;
dataHelper . DropForeignKeys ( table ) ;
}
2012-07-26 21:12:54 +06:00
/// <summary>
/// Gets the current assembly directory.
/// </summary>
/// <value>The assembly directory.</value>
static public string CurrentAssemblyDirectory
{
get
{
2012-11-15 21:46:54 +05:00
var codeBase = typeof ( TestHelper ) . Assembly . CodeBase ;
2012-07-26 21:12:54 +06:00
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 + "/" ) ;
}
2012-07-31 02:34:57 +06:00
2015-01-09 10:51:15 +11:00
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
}
public static void CleanContentDirectories ( )
{
CleanDirectories ( new [ ] { SystemDirectories . Masterpages , SystemDirectories . MvcViews , SystemDirectories . Media } ) ;
}
public static void CreateDirectories ( string [ ] directories )
{
foreach ( var directory in directories )
{
var directoryInfo = new DirectoryInfo ( IOHelper . MapPath ( directory ) ) ;
if ( directoryInfo . Exists = = false )
Directory . CreateDirectory ( IOHelper . MapPath ( directory ) ) ;
}
}
public static void CleanDirectories ( string [ ] directories )
2013-09-06 20:21:47 +02:00
{
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 ;
2012-11-29 09:07:14 -01:00
if ( directoryInfo . Exists )
2013-09-06 20:21:47 +02:00
directoryInfo . GetFiles ( ) . Where ( x = > preserve = = null | | preserve . Contains ( x . Name ) = = false ) . ForEach ( x = > x . Delete ( ) ) ;
2012-11-29 09:07:14 -01:00
}
}
2013-08-23 16:22:26 +02:00
public static void CleanUmbracoSettingsConfig ( )
{
var currDir = new DirectoryInfo ( CurrentAssemblyDirectory ) ;
var umbracoSettingsFile = Path . Combine ( currDir . Parent . Parent . FullName , "config" , "umbracoSettings.config" ) ;
if ( File . Exists ( umbracoSettingsFile ) )
File . Delete ( umbracoSettingsFile ) ;
}
2012-07-26 21:12:54 +06:00
}
}