2012-07-26 21:12:54 +06:00
using System ;
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-07-31 02:34:57 +06:00
using log4net.Config ;
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
{
2012-07-26 21:12:54 +06:00
/// <summary>
/// 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-08-09 04:15:35 +06:00
/// <summary>
/// Initializes a new database
/// </summary>
public static void InitializeDatabase ( )
{
2013-08-19 12:26:01 +02:00
ConfigurationManager . AppSettings . Set ( Core . Configuration . GlobalSettings . UmbracoConnectionName , @"datalayer=SQLCE4Umbraco.SqlCEHelper,SQLCE4Umbraco;data source=|DataDirectory|\UmbracoPetaPocoTests.sdf;Flush Interval=1;File Access Retry Timeout=10" ) ;
2012-08-09 04:15:35 +06:00
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 ) ;
2012-08-09 04:15:35 +06:00
var installer = dataHelper . Utility . CreateInstaller ( ) ;
if ( installer . CanConnect )
{
installer . Install ( ) ;
}
}
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
public static void SetupLog4NetForTests ( )
{
XmlConfigurator . Configure ( new FileInfo ( MapPathForTest ( "~/unit-test-log4net.config" ) ) ) ;
}
2012-11-29 09:07:14 -01:00
public static void InitializeContentDirectories ( )
{
CreateDirectories ( new [ ] { SystemDirectories . Masterpages , SystemDirectories . MvcViews , SystemDirectories . Media } ) ;
}
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 )
{
foreach ( var directory in directories )
{
var directoryInfo = new DirectoryInfo ( IOHelper . MapPath ( directory ) ) ;
if ( directoryInfo . Exists )
directoryInfo . GetFiles ( ) . ForEach ( x = > x . Delete ( ) ) ;
}
}
2013-08-23 16:22:26 +02:00
public static void EnsureUmbracoSettingsConfig ( )
{
var currDir = new DirectoryInfo ( CurrentAssemblyDirectory ) ;
var configPath = Path . Combine ( currDir . Parent . Parent . FullName , "config" ) ;
if ( Directory . Exists ( configPath ) = = false )
Directory . CreateDirectory ( configPath ) ;
var umbracoSettingsFile = Path . Combine ( currDir . Parent . Parent . FullName , "config" , "umbracoSettings.config" ) ;
if ( File . Exists ( umbracoSettingsFile ) = = false )
File . Copy (
currDir . Parent . Parent . Parent . GetDirectories ( "Umbraco.Web.UI" )
. First ( )
. GetDirectories ( "config" ) . First ( )
. GetFiles ( "umbracoSettings.Release.config" ) . First ( ) . FullName ,
Path . Combine ( currDir . Parent . Parent . FullName , "config" , "umbracoSettings.config" ) ,
true ) ;
Core . Configuration . UmbracoSettings . SettingsFilePath = IOHelper . MapPath ( SystemDirectories . Config + Path . DirectorySeparatorChar , false ) ;
}
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
}
}