using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Xml.Linq; using Autofac; using NHibernate; using NUnit.Framework; using Orchard.Caching; using Orchard.ContentManagement; using Orchard.ContentManagement.Handlers; using Orchard.ContentManagement.MetaData; using Orchard.ContentManagement.MetaData.Models; using Orchard.ContentManagement.MetaData.Services; using Orchard.ContentManagement.Records; using Orchard.Core.Settings.Descriptor.Records; using Orchard.Core.Settings.Handlers; using Orchard.Core.Settings.Metadata; using Orchard.Core.Settings.Models; using Orchard.Core.Settings.Services; using Orchard.Data; using Orchard.DisplayManagement; using Orchard.DisplayManagement.Descriptors; using Orchard.DisplayManagement.Implementation; using Orchard.Environment; using Orchard.Environment.AutofacUtil.DynamicProxy2; using Orchard.Environment.Descriptor; using Orchard.Environment.Descriptor.Models; using Orchard.Environment.Extensions; using Orchard.Environment.Extensions.Models; using Orchard.Localization; using Orchard.Modules; using Orchard.Modules.Services; using Orchard.Security; using Orchard.Security.Permissions; using Orchard.Settings; using Orchard.Tests.Stubs; using Orchard.Themes; using Orchard.Themes.Handlers; using Orchard.Themes.Models; using Orchard.Themes.Services; using Orchard.UI.Notify; namespace Orchard.Tests.Modules.Themes.Services { [TestFixture] public class ThemeServiceTests { private IThemeService _themeService; private IContainer _container; private ISessionFactory _sessionFactory; private ISession _session; [TestFixtureSetUp] public void InitFixture() { var databaseFileName = System.IO.Path.GetTempFileName(); _sessionFactory = DataUtility.CreateSessionFactory(databaseFileName, typeof(ThemeSiteSettingsPartRecord), typeof(SiteSettingsPartRecord), typeof(ContentItemVersionRecord), typeof(ContentItemRecord), typeof(ContentTypeRecord)); } [TestFixtureTearDown] public void TermFixture() { } [SetUp] public void Init() { var context = new DynamicProxyContext(); var builder = new ContainerBuilder(); builder.RegisterModule(new SettingsModule()); builder.RegisterType().EnableDynamicProxy(context).As(); builder.RegisterType().As(); builder.RegisterType().As(); builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>)); builder.RegisterType().As(); builder.RegisterType().As(); builder.RegisterType().As(); builder.RegisterType().As(); builder.RegisterType().As(); builder.RegisterType().As(); builder.RegisterType().As(); builder.RegisterType().As(); builder.RegisterType().As(); builder.RegisterType().As(); builder.RegisterType().As(); builder.RegisterType().As(); builder.RegisterType().As(); builder.RegisterType().As(); builder.RegisterType().As(); builder.RegisterType().As(); builder.RegisterType().As(); builder.RegisterType(typeof(SettingsFormatter)) .As(typeof(IMapper)) .As(typeof(IMapper)); _session = _sessionFactory.OpenSession(); builder.RegisterInstance(new TestSessionLocator(_session)).As(); _container = builder.Build(); _themeService = _container.Resolve(); } //todo: test theme feature enablement [Test] public void ThemeWithNoBaseThemeCanBeSetAsSiteTheme() { _themeService.SetSiteTheme("ThemeOne"); var siteTheme = _themeService.GetSiteTheme(); Assert.That(siteTheme.ThemeName, Is.EqualTo("ThemeOne")); } [Test] public void ThemeWithAvailableBaseThemeCanBeSetAsSiteTheme() { _themeService.SetSiteTheme("ThemeTwo"); var siteTheme = _themeService.GetSiteTheme(); Assert.That(siteTheme.ThemeName, Is.EqualTo("ThemeTwo")); Assert.That(siteTheme.BaseTheme, Is.EqualTo("ThemeOne")); } [Test] public void ThemeWithUnavailableBaseThemeCanBeSetAsSiteTheme() { _themeService.SetSiteTheme("ThemeOne"); _themeService.SetSiteTheme("ThemeThree"); var siteTheme = _themeService.GetSiteTheme(); Assert.That(siteTheme.ThemeName, Is.EqualTo("ThemeOne")); } #region Stubs public class TestSessionLocator : ISessionLocator { private readonly ISession _session; public TestSessionLocator(ISession session) { _session = session; } public ISession For(Type entityType) { return _session; } } public class StubAuthorizer : IAuthorizer { public bool Authorize(Permission permission) { return true; } public bool Authorize(Permission permission, LocalizedString message) { return true; } public bool Authorize(Permission permission, IContent content, LocalizedString message) { return true; } } public class StubExtensionManager : IExtensionManager { public IEnumerable AvailableExtensions() { var extensions = new[] { new ExtensionDescriptor {Name = "ThemeOne", ExtensionType = "Theme"}, new ExtensionDescriptor {Name = "ThemeTwo", BaseTheme = "ThemeOne", ExtensionType = "Theme"}, new ExtensionDescriptor {Name = "ThemeThree", BaseTheme = "TheThemeThatIsntThere", ExtensionType = "Theme"}, }; foreach (var extension in extensions) { extension.Features = new[] { new FeatureDescriptor { Extension = extension, Name = extension.Name } }; yield return extension; } } public IEnumerable AvailableFeatures() { return AvailableExtensions().SelectMany(ed => ed.Features); } public IEnumerable LoadFeatures(IEnumerable featureDescriptors) { return featureDescriptors.Select(FrameworkFeature); } private static Feature FrameworkFeature(FeatureDescriptor descriptor) { return new Feature { Descriptor = descriptor }; } public void InstallExtension(string extensionType, HttpPostedFileBase extensionBundle) { throw new NotImplementedException(); } public void UninstallExtension(string extensionType, string extensionName) { throw new NotImplementedException(); } public void Monitor(Action monitor) { throw new NotImplementedException(); } } public class StubShellDescriptorManager : IShellDescriptorManager { private readonly ShellDescriptorRecord _shellDescriptorRecord = new ShellDescriptorRecord(); public ShellDescriptor GetShellDescriptor() { return GetShellDescriptorFromRecord(_shellDescriptorRecord); } public void UpdateShellDescriptor(int priorSerialNumber, IEnumerable enabledFeatures, IEnumerable parameters) { _shellDescriptorRecord.Features.Clear(); foreach (var feature in enabledFeatures) { _shellDescriptorRecord.Features.Add(new ShellFeatureRecord { Name = feature.Name, ShellDescriptorRecord = null }); } _shellDescriptorRecord.Parameters.Clear(); foreach (var parameter in parameters) { _shellDescriptorRecord.Parameters.Add(new ShellParameterRecord { Component = parameter.Component, Name = parameter.Name, Value = parameter.Value, ShellDescriptorRecord = null }); } } private static ShellDescriptor GetShellDescriptorFromRecord(ShellDescriptorRecord shellDescriptorRecord) { return new ShellDescriptor { SerialNumber = shellDescriptorRecord.SerialNumber, Features = shellDescriptorRecord.Features.Select(descriptorFeatureRecord => new ShellFeature {Name = descriptorFeatureRecord.Name}).ToList(), Parameters = shellDescriptorRecord.Parameters.Select(descriptorParameterRecord => new ShellParameter { Component = descriptorParameterRecord.Component, Name = descriptorParameterRecord.Name, Value = descriptorParameterRecord.Value }).ToList() }; } } #endregion } }