Adding a BaseTheme property to Theme

--HG--
branch : dev
This commit is contained in:
Nathan Heskew
2010-10-05 13:55:29 -07:00
parent 5b53005f14
commit 7b36d725b5
9 changed files with 282 additions and 34 deletions

View File

@@ -59,6 +59,7 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\lib\autofac\Autofac.dll</HintPath>
</Reference>
<Reference Include="Castle.Core, Version=1.1.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL" />
<Reference Include="FluentNHibernate, Version=1.0.0.593, Culture=neutral, PublicKeyToken=8aa435e3cb308880, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\lib\fluentnhibernate\FluentNHibernate.dll</HintPath>
@@ -127,6 +128,7 @@
<Compile Include="Roles\Controllers\AdminControllerTests.cs" />
<Compile Include="Roles\Services\RoleServiceTests.cs" />
<Compile Include="Settings\Blueprint\ShellDescriptorManagerTests.cs" />
<Compile Include="Themes\Services\ThemeServiceTests.cs" />
<Compile Include="Values.cs" />
<Compile Include="Users\Controllers\AdminControllerTests.cs" />
<Compile Include="Users\Services\MembershipServiceTests.cs" />
@@ -148,6 +150,10 @@
<Project>{C0C45321-B51D-4D8D-9B7B-AA4C2E0B2962}</Project>
<Name>Orchard.CodeGeneration</Name>
</ProjectReference>
<ProjectReference Include="..\Orchard.Web\Modules\Orchard.Modules\Orchard.Modules.csproj">
<Project>{17F86780-9A1F-4AA1-86F1-875EEC2730C7}</Project>
<Name>Orchard.Modules</Name>
</ProjectReference>
<ProjectReference Include="..\Orchard.Web\Modules\Orchard.Roles\Orchard.Roles.csproj">
<Project>{D10AD48F-407D-4DB5-A328-173EC7CB010F}</Project>
<Name>Orchard.Roles</Name>
@@ -156,6 +162,10 @@
<Project>{8C7FCBC2-E6E1-405E-BFB5-D8D9E67A09C4}</Project>
<Name>Orchard.Setup</Name>
</ProjectReference>
<ProjectReference Include="..\Orchard.Web\Modules\Orchard.Themes\Orchard.Themes.csproj">
<Project>{CDE24A24-01D3-403C-84B9-37722E18DFB7}</Project>
<Name>Orchard.Themes</Name>
</ProjectReference>
<ProjectReference Include="..\Orchard.Web\Modules\Orchard.Users\Orchard.Users.csproj">
<Project>{79AED36E-ABD0-4747-93D3-8722B042454B}</Project>
<Name>Orchard.Users</Name>

View File

@@ -0,0 +1,229 @@
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<ThemeService>().EnableDynamicProxy(context).As<IThemeService>();
builder.RegisterType<SettingsModuleInterceptor>().As<ISettingsModuleInterceptor>();
builder.RegisterType<SiteService>().As<ISiteService>();
builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>));
builder.RegisterType<DefaultContentManager>().As<IContentManager>();
builder.RegisterType<StubCacheManager>().As<ICacheManager>();
builder.RegisterType<ContentDefinitionManager>().As<IContentDefinitionManager>();
builder.RegisterType<DefaultContentManagerSession>().As<IContentManagerSession>();
builder.RegisterType<ShapeHelperFactory>().As<IShapeHelperFactory>();
builder.RegisterType<DefaultShapeFactory>().As<IShapeFactory>();
builder.RegisterType<DefaultShapeTableManager>().As<IShapeTableManager>();
builder.RegisterType<StubExtensionManager>().As<IExtensionManager>();
builder.RegisterType<DefaultContentQuery>().As<IContentQuery>();
builder.RegisterType<SiteSettingsPartHandler>().As<IContentHandler>();
builder.RegisterType<ThemeSiteSettingsPartHandler>().As<IContentHandler>();
builder.RegisterType<ModuleService>().As<IModuleService>();
builder.RegisterType<OrchardServices>().As<IOrchardServices>();
builder.RegisterType<StubShellDescriptorManager>().As<IShellDescriptorManager>();
builder.RegisterType<TransactionManager>().As<ITransactionManager>();
builder.RegisterType<Notifier>().As<INotifier>();
builder.RegisterType<StubAuthorizer>().As<IAuthorizer>();
builder.RegisterType(typeof(SettingsFormatter))
.As(typeof(IMapper<XElement, SettingsDictionary>))
.As(typeof(IMapper<SettingsDictionary, XElement>));
_session = _sessionFactory.OpenSession();
builder.RegisterInstance(new TestSessionLocator(_session)).As<ISessionLocator>();
_container = builder.Build();
_themeService = _container.Resolve<IThemeService>();
}
//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<ExtensionDescriptor> 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<FeatureDescriptor> AvailableFeatures() {
return AvailableExtensions().SelectMany(ed => ed.Features);
}
public IEnumerable<Feature> LoadFeatures(IEnumerable<FeatureDescriptor> 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<IVolatileToken> 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<ShellFeature> enabledFeatures, IEnumerable<ShellParameter> 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
}
}

View File

@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Autofac;
@@ -10,7 +8,6 @@ using NUnit.Framework;
using Orchard.DisplayManagement.Descriptors;
using Orchard.DisplayManagement.Implementation;
using Orchard.DisplayManagement.Shapes;
using Orchard.Tests.Stubs;
using Orchard.Themes;
namespace Orchard.Tests.DisplayManagement {
@@ -42,6 +39,7 @@ namespace Orchard.Tests.DisplayManagement {
public string HomePage { get; set; }
public string Tags { get; set; }
public string Zones { get; set; }
public string BaseTheme { get; set; }
}

View File

@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Autofac;
using Orchard.Commands;
@@ -9,7 +8,6 @@ using Orchard.Commands.Builtin;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Handlers;
using Orchard.ContentManagement.MetaData.Builders;
using Orchard.Core.Shapes;
using Orchard.Data.Migration.Interpreters;
using Orchard.Data.Providers;
using Orchard.Data.Migration;
@@ -18,13 +16,10 @@ using Orchard.DisplayManagement.Descriptors;
using Orchard.DisplayManagement.Descriptors.ShapeAttributeStrategy;
using Orchard.DisplayManagement.Descriptors.ShapeTemplateStrategy;
using Orchard.DisplayManagement.Implementation;
using Orchard.DisplayManagement.Shapes;
using Orchard.Environment;
using Orchard.Environment.Extensions;
using Orchard.Environment.Extensions.Models;
using Orchard.Localization;
using Orchard.Mvc;
using Orchard.Mvc.Filters;
using Orchard.Mvc.ModelBinders;
using Orchard.Mvc.Routes;
using Orchard.Mvc.ViewEngines;
@@ -116,6 +111,7 @@ namespace Orchard.Setup {
public string HomePage { get; set; }
public string Tags { get; set; }
public string Zones { get; set; }
public string BaseTheme { get; set; }
}
private readonly SafeModeTheme _theme = new SafeModeTheme {

View File

@@ -8,5 +8,6 @@
public string HomePage { get; set; }
public string Tags { get; set; }
public string Zones { get; set; }
public string BaseTheme { get; set; }
}
}

View File

@@ -33,9 +33,9 @@ namespace Orchard.Themes.Services {
protected virtual ISite CurrentSite { get; [UsedImplicitly] private set; }
public ITheme GetSiteTheme() {
string currentThemeName = CurrentSite.As<ThemeSiteSettingsPart>().Record.CurrentThemeName;
string currentThemeName = CurrentSite.As<ThemeSiteSettingsPart>().CurrentThemeName;
if (String.IsNullOrEmpty(currentThemeName)) {
if (string.IsNullOrEmpty(currentThemeName)) {
return null;
}
@@ -43,24 +43,35 @@ namespace Orchard.Themes.Services {
}
public void SetSiteTheme(string themeName) {
if (GetThemeByName(themeName) != null) {
if (string.IsNullOrWhiteSpace(themeName))
return;
var currentTheme = CurrentSite.As<ThemeSiteSettingsPart>().Record.CurrentThemeName;
//todo: (heskew) need messages given in addition to all of these early returns so something meaningful can be presented to the user
var themeToSet = GetThemeByName(themeName);
if (themeToSet == null)
return;
if ( !String.IsNullOrEmpty(currentTheme) ) {
_moduleService.DisableFeatures(new[] {currentTheme}, true);
}
if ( !String.IsNullOrEmpty(themeName) ) {
_moduleService.EnableFeatures(new[] {themeName}, true);
}
CurrentSite.As<ThemeSiteSettingsPart>().Record.CurrentThemeName = themeName;
//if there's a base theme, it needs to be present
ITheme baseTheme = null;
if (!string.IsNullOrWhiteSpace(themeToSet.BaseTheme)) {
baseTheme = GetThemeByName(themeToSet.BaseTheme);
if (baseTheme == null)
return;
}
var currentTheme = CurrentSite.As<ThemeSiteSettingsPart>().CurrentThemeName;
if ( !string.IsNullOrEmpty(currentTheme) )
_moduleService.DisableFeatures(new[] {currentTheme}, true);
if (baseTheme != null)
_moduleService.EnableFeatures(new[] {themeToSet.BaseTheme});
_moduleService.EnableFeatures(new[] {themeToSet.ThemeName}, true);
CurrentSite.As<ThemeSiteSettingsPart>().Record.CurrentThemeName = themeToSet.ThemeName;
}
public ITheme GetRequestTheme(RequestContext requestContext) {
var requestTheme = _themeSelectors
.Select(x => x.GetTheme(requestContext))
.Where(x => x != null)
@@ -80,7 +91,7 @@ namespace Orchard.Themes.Services {
public ITheme GetThemeByName(string name) {
foreach (var descriptor in _extensionManager.AvailableExtensions()) {
if (String.Equals(descriptor.Name, name, StringComparison.OrdinalIgnoreCase)) {
if (string.Equals(descriptor.Name, name, StringComparison.OrdinalIgnoreCase)) {
return CreateTheme(descriptor);
}
}
@@ -91,11 +102,10 @@ namespace Orchard.Themes.Services {
/// Loads only enabled themes
/// </summary>
public IEnumerable<ITheme> GetInstalledThemes() {
var themes = new List<ITheme>();
foreach (var descriptor in _extensionManager.AvailableExtensions()) {
if (!String.Equals(descriptor.ExtensionType, "Theme", StringComparison.OrdinalIgnoreCase)) {
if (!string.Equals(descriptor.ExtensionType, "Theme", StringComparison.OrdinalIgnoreCase)) {
continue;
}
@@ -116,16 +126,17 @@ namespace Orchard.Themes.Services {
_extensionManager.UninstallExtension("Theme", themeName);
}
private ITheme CreateTheme(ExtensionDescriptor descriptor) {
private static ITheme CreateTheme(ExtensionDescriptor descriptor) {
return new Theme {
Author = descriptor.Author ?? String.Empty,
Description = descriptor.Description ?? String.Empty,
DisplayName = descriptor.DisplayName ?? String.Empty,
HomePage = descriptor.WebSite ?? String.Empty,
Author = descriptor.Author ?? "",
Description = descriptor.Description ?? "",
DisplayName = descriptor.DisplayName ?? "",
HomePage = descriptor.WebSite ?? "",
ThemeName = descriptor.Name,
Version = descriptor.Version ?? String.Empty,
Tags = descriptor.Tags ?? String.Empty,
Zones = descriptor.Zones ?? String.Empty,
Version = descriptor.Version ?? "",
Tags = descriptor.Tags ?? "",
Zones = descriptor.Zones ?? "",
BaseTheme = descriptor.BaseTheme ?? "",
};
}
}

View File

@@ -127,6 +127,7 @@ namespace Orchard.Environment.Extensions.Folders {
Tags = GetValue(fields, "tags"),
AntiForgery = GetValue(fields, "antiforgery"),
Zones = GetValue(fields, "zones"),
BaseTheme = GetValue(fields, "basetheme"),
};
extensionDescriptor.Features = GetFeaturesForExtension(GetMapping(fields, "features"), extensionDescriptor);

View File

@@ -27,6 +27,7 @@ namespace Orchard.Environment.Extensions.Models {
public string Tags { get; set; }
public string AntiForgery { get; set; }
public string Zones { get; set; }
public string BaseTheme { get; set; }
public IEnumerable<FeatureDescriptor> Features { get; set; }
}

View File

@@ -11,5 +11,6 @@
string HomePage { get; set; }
string Tags { get; set; }
string Zones { get; set; }
string BaseTheme { get; set; }
}
}