-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntegrationTestCollection.cs
132 lines (112 loc) · 4.79 KB
/
IntegrationTestCollection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using EPiServer.Core;
using EPiServer.Core.Transfer;
using EPiServer.DataAbstraction;
using EPiServer.Enterprise;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.ServiceLocation;
using EPiServer.Web;
using Xunit;
namespace IntegrationTestSample
{
[CollectionDefinition(Name)]
public class IntegrationTestCollection : ICollectionFixture<IntegrationTestCollectionFixture>
{
public const string Name = "IntegrationTest";
// Generate a "unique" database string for each time we run our tests
internal static readonly string ConnectionString = $"Data Source=(localdb)\\MSSQLLocalDB;Database=IntegrationTestSample.{Guid.NewGuid().ToString().Substring(0, 8)};Integrated Security=true;MultipleActiveResultSets=True";
}
public class IntegrationTestCollectionFixture : IDisposable
{
private readonly IDisposable _database;
private readonly InitializationEngine _engine;
public IntegrationTestCollectionFixture()
{
// Create a temporary database
_database = DatabaseHelper.Temporary(IntegrationTestCollection.ConnectionString);
// Initialize Episerver CMS - Including our IntegrationTestInitialization module
_engine = new InitializationEngine((IEnumerable<IInitializableModule>)null, HostType.TestFramework, new AssemblyList(true).AllowedAssemblies);
_engine.Initialize();
// Configure site and import content
SetupSite(_engine.Locate);
}
public void Dispose()
{
// Take down the CMS instance
if (_engine.InitializationState == InitializationState.Initialized)
{
_engine.Uninitialize();
}
// Removes the temporary database
_database.Dispose();
}
private static void SetupSite(ServiceLocationHelper locate)
{
// Ensure languages are enabled
EnabledLanguages(
locate.LanguageBranchRepository(),
new[] { "en", "de" }
);
// Import content from our embedded export package
var startPage = ImportEmbeddedPackage(
locate.Advanced.GetInstance<IDataImporter>(),
"IntegrationTestSample.TestPage.episerverdata"
);
// Create a site definition for our site
DefineSite(
locate.Advanced.GetInstance<ISiteDefinitionRepository>(),
"TestSite",
startPage,
"localhost:8080"
);
}
private static void EnabledLanguages(ILanguageBranchRepository languageRepository, IEnumerable<string> languages)
{
if (languages == null || !languages.Any())
return;
var shouldDisable = languageRepository.ListEnabled().Where(l => !languages.Contains(l.LanguageID, StringComparer.OrdinalIgnoreCase));
foreach (var language in languages.Select(x => CultureInfo.GetCultureInfo(x)))
{
languageRepository.Enable(language);
}
// Disable language secondly to avoid exception thrown if no languages are enabled.
foreach (var language in shouldDisable)
{
languageRepository.Disable(language.Culture);
}
}
private static ContentReference ImportEmbeddedPackage(IDataImporter importer, string embeddedResourceName)
{
// Load content package from embedded resources
var resources = Assembly.GetExecutingAssembly().GetManifestResourceStream(embeddedResourceName);
var options = new ImportOptions
{
AutoCloseStream = true,
KeepIdentity = true,
TransferType = TypeOfTransfer.Importing,
ValidateDestination = false
};
importer.Import(resources, ContentReference.RootPage, options);
// Root of Imported pages will be our start page
return importer.Status.ImportedRoot;
}
private static void DefineSite(ISiteDefinitionRepository siteDefinitionRepository, string siteName, ContentReference startPage, string siteUrl)
{
var existingSite = siteDefinitionRepository.Get(siteName);
if (existingSite != null) return;
// Define our site
var siteDefinition = new SiteDefinition
{
Name = siteName,
SiteUrl = new Uri($"http://{siteUrl}/", UriKind.Absolute),
StartPage = startPage
};
siteDefinitionRepository.Save(siteDefinition);
}
}
}