diff --git a/src/Orchard.Core.Tests/Common/Providers/CommonPartProviderTests.cs b/src/Orchard.Core.Tests/Common/Providers/CommonPartProviderTests.cs index fcc636ac3..8f1222e2b 100644 --- a/src/Orchard.Core.Tests/Common/Providers/CommonPartProviderTests.cs +++ b/src/Orchard.Core.Tests/Common/Providers/CommonPartProviderTests.cs @@ -1,13 +1,17 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Web.Routing; using Autofac; using JetBrains.Annotations; using Moq; using NUnit.Framework; using Orchard.ContentManagement.Aspects; +using Orchard.ContentManagement.Drivers; +using Orchard.ContentManagement.Drivers.Coordinators; using Orchard.ContentManagement.MetaData; using Orchard.Core.Common; +using Orchard.Core.Common.Drivers; using Orchard.Core.Common.Handlers; using Orchard.Core.Common.Models; using Orchard.ContentManagement; @@ -27,6 +31,7 @@ using Orchard.Tests.Modules; using Orchard.Core.Common.ViewModels; using System.Web.Mvc; using Orchard.Tests.Stubs; +using Orchard.Themes; namespace Orchard.Core.Tests.Common.Providers { [TestFixture] @@ -41,12 +46,19 @@ namespace Orchard.Core.Tests.Common.Providers { 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.RegisterInstance(new Mock().Object); + builder.RegisterInstance(new Mock().Object); + builder.RegisterInstance(new Mock().Object); + builder.RegisterInstance(new Mock().Object); + builder.RegisterType().As(); + builder.RegisterType().As(); + builder.RegisterType().As(); _authn = new Mock(); _authz = new Mock(); @@ -152,7 +164,7 @@ namespace Orchard.Core.Tests.Common.Providers { contentManager.UpdateEditor(item.ContentItem, updater); } - [Test, Ignore("Fix pending")] + [Test] public void PublishingShouldFailIfOwnerIsEmpty() { var contentManager = _container.Resolve(); @@ -222,9 +234,9 @@ namespace Orchard.Core.Tests.Common.Providers { Assert.That(item1.CreatedUtc, Is.EqualTo(createUtc)); Assert.That(item2.CreatedUtc, Is.EqualTo(createUtc)); - // both instances non-versioned dates show the most recent publish - Assert.That(item1.PublishedUtc, Is.EqualTo(publish2Utc)); - Assert.That(item2.PublishedUtc, Is.EqualTo(publish2Utc)); + // both instances non-versioned dates show the earliest publish date + Assert.That(item1.PublishedUtc, Is.EqualTo(publish1Utc)); + Assert.That(item2.PublishedUtc, Is.EqualTo(publish1Utc)); // version1 versioned dates show create was upfront and publish was oldest Assert.That(item1.VersionCreatedUtc, Is.EqualTo(createUtc)); diff --git a/src/Orchard.Web/Core/Common/Drivers/CommonPartDriver.cs b/src/Orchard.Web/Core/Common/Drivers/CommonPartDriver.cs index cf735b2bd..49d41bd5a 100644 --- a/src/Orchard.Web/Core/Common/Drivers/CommonPartDriver.cs +++ b/src/Orchard.Web/Core/Common/Drivers/CommonPartDriver.cs @@ -51,14 +51,10 @@ namespace Orchard.Core.Common.Drivers { ContainerEditor(part, null, shapeHelper)); } - protected override DriverResult Editor(CommonPart instance, IUpdateModel updater, dynamic shapeHelper) { - // this event is hooked so the modified timestamp is changed when an edit-post occurs. - instance.ModifiedUtc = _clock.UtcNow; - instance.VersionModifiedUtc = _clock.UtcNow; - + protected override DriverResult Editor(CommonPart part, IUpdateModel updater, dynamic shapeHelper) { return Combined( - OwnerEditor(instance, updater, shapeHelper), - ContainerEditor(instance, updater, shapeHelper)); + OwnerEditor(part, updater, shapeHelper), + ContainerEditor(part, updater, shapeHelper)); } DriverResult OwnerEditor(CommonPart part, IUpdateModel updater, dynamic shapeHelper) { diff --git a/src/Orchard.Web/Core/Common/Handlers/CommonPartHandler.cs b/src/Orchard.Web/Core/Common/Handlers/CommonPartHandler.cs index 71dd57e6d..8e8fd8316 100644 --- a/src/Orchard.Web/Core/Common/Handlers/CommonPartHandler.cs +++ b/src/Orchard.Web/Core/Common/Handlers/CommonPartHandler.cs @@ -1,4 +1,6 @@ +using System.Linq; using JetBrains.Annotations; +using Orchard.ContentManagement.MetaData; using Orchard.Core.Common.Models; using Orchard.Data; using Orchard.Localization; @@ -13,40 +15,40 @@ namespace Orchard.Core.Common.Handlers { private readonly IClock _clock; private readonly IAuthenticationService _authenticationService; private readonly IContentManager _contentManager; + private readonly IContentDefinitionManager _contentDefinitionManager; public CommonPartHandler( IRepository commonRepository, IRepository commonVersionRepository, IClock clock, IAuthenticationService authenticationService, - IContentManager contentManager) { + IContentManager contentManager, + IContentDefinitionManager contentDefinitionManager) { _clock = clock; _authenticationService = authenticationService; _contentManager = contentManager; + _contentDefinitionManager = contentDefinitionManager; T = NullLocalizer.Instance; Filters.Add(StorageFilter.For(commonRepository)); Filters.Add(StorageFilter.For(commonVersionRepository)); + Filters.Add(new ActivatingFilter>(ContentTypeWithACommonPart)); + OnInitializing(PropertySetHandlers); OnInitializing(AssignCreatingOwner); - OnInitializing>(AssignCreatingDates); + OnInitializing(AssignCreatingDates); OnInitializing>(AssignCreatingDates); OnLoaded(LazyLoadHandlers); - OnVersioning(CopyOwnerAndContainer); - + OnVersioned(AssignVersioningDates); OnVersioned>(AssignVersioningDates); - OnPublishing>(AssignPublishingDates); + OnPublishing(AssignPublishingDates); OnPublishing>(AssignPublishingDates); - //OnGetDisplayViewModel(); - //OnGetEditorViewModel(GetEditor); - //OnUpdateEditorViewModel(UpdateEditor); - OnIndexing((context, commonPart) => context.DocumentIndex .Add("type", commonPart.ContentItem.ContentType).Store() .Add("author", commonPart.Owner.UserName).Store() @@ -58,6 +60,9 @@ namespace Orchard.Core.Common.Handlers { public Localizer T { get; set; } + bool ContentTypeWithACommonPart(string typeName) { + return _contentDefinitionManager.GetTypeDefinition(typeName).Parts.Any(part => part.PartDefinition.Name == "CommonPart"); + } void AssignCreatingOwner(InitializingContentContext context, CommonPart part) { // and use the current user as Owner @@ -66,10 +71,10 @@ namespace Orchard.Core.Common.Handlers { } } - void AssignCreatingDates(InitializingContentContext context, ContentPart part) { + void AssignCreatingDates(InitializingContentContext context, CommonPart part) { // assign default create/modified dates - part.Record.CreatedUtc = _clock.UtcNow; - part.Record.ModifiedUtc = _clock.UtcNow; + part.CreatedUtc = _clock.UtcNow; + part.ModifiedUtc = _clock.UtcNow; } void AssignCreatingDates(InitializingContentContext context, ContentPart part) { @@ -78,22 +83,31 @@ namespace Orchard.Core.Common.Handlers { part.Record.ModifiedUtc = _clock.UtcNow; } - void AssignVersioningDates(VersionContentContext context, ContentPart existing, ContentPart building) { - // assign create/modified dates for the new version - building.Record.CreatedUtc = _clock.UtcNow; - building.Record.ModifiedUtc = _clock.UtcNow; + void AssignVersioningDates(VersionContentContext context, CommonPart existing, CommonPart building) { + // assign the created + building.CreatedUtc = existing.CreatedUtc ?? _clock.UtcNow; + // persist and published dates + building.PublishedUtc = existing.PublishedUtc; + // assign modified date for the new version + building.ModifiedUtc = _clock.UtcNow; + } + void AssignVersioningDates(VersionContentContext context, ContentPart existing, ContentPart building) { + // assign the created date + building.Record.CreatedUtc = _clock.UtcNow; + // assign modified date for the new version + building.Record.ModifiedUtc = _clock.UtcNow; // publish date should be null until publish method called building.Record.PublishedUtc = null; } - void AssignPublishingDates(PublishContentContext context, ContentPart part) { + void AssignPublishingDates(PublishContentContext context, CommonPart part) { // don't assign dates when unpublishing if (context.PublishingItemVersionRecord == null) return; - - // assign version-agnostic publish date - part.Record.PublishedUtc = _clock.UtcNow; + + // set the initial published date + part.PublishedUtc = part.PublishedUtc ?? _clock.UtcNow; } void AssignPublishingDates(PublishContentContext context, ContentPart part) { @@ -101,13 +115,8 @@ namespace Orchard.Core.Common.Handlers { if (context.PublishingItemVersionRecord == null) return; - // assign version-specific publish date - part.Record.PublishedUtc = _clock.UtcNow; - } - - private static void CopyOwnerAndContainer(VersionContentContext c, CommonPart c1, CommonPart c2) { - c2.Owner = c1.Owner; - c2.Container = c1.Container; + // assign the version's published date + part.Record.PublishedUtc = part.Record.PublishedUtc ?? _clock.UtcNow; } void LazyLoadHandlers(LoadContentContext context, CommonPart part) { @@ -120,28 +129,22 @@ namespace Orchard.Core.Common.Handlers { // add handlers that will update records when part properties are set part.OwnerField.Setter(user => { - if (user == null) { - part.Record.OwnerId = 0; - } - else { - part.Record.OwnerId = user.ContentItem.Id; - } - return user; - }); + part.Record.OwnerId = user == null + ? 0 + : user.ContentItem.Id; + return user; + }); // Force call to setter if we had already set a value if (part.OwnerField.Value != null) part.OwnerField.Value = part.OwnerField.Value; part.ContainerField.Setter(container => { - if (container == null) { - part.Record.Container = null; - } - else { - part.Record.Container = container.ContentItem.Record; - } - return container; - }); + part.Record.Container = container == null + ? null + : container.ContentItem.Record; + return container; + }); // Force call to setter if we had already set a value if (part.ContainerField.Value != null) diff --git a/src/Orchard.Web/Core/Common/Handlers/ItemReferenceContentFieldHandler.cs b/src/Orchard.Web/Core/Common/Handlers/ItemReferenceContentFieldHandler.cs deleted file mode 100644 index 0e2afb3bd..000000000 --- a/src/Orchard.Web/Core/Common/Handlers/ItemReferenceContentFieldHandler.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System.Collections.Generic; -using Orchard.ContentManagement.Drivers; -using Orchard.ContentManagement.Handlers; - -namespace Orchard.Core.Common.Handlers { - public class ItemReferenceContentFieldHandler : ContentFieldHandler { - public ItemReferenceContentFieldHandler(IEnumerable contentFieldDrivers) : base(contentFieldDrivers) { } - } -} \ No newline at end of file diff --git a/src/Orchard.Web/Core/Common/Models/CommonPart.cs b/src/Orchard.Web/Core/Common/Models/CommonPart.cs index c452fa3f3..bccf97127 100644 --- a/src/Orchard.Web/Core/Common/Models/CommonPart.cs +++ b/src/Orchard.Web/Core/Common/Models/CommonPart.cs @@ -50,9 +50,8 @@ namespace Orchard.Core.Common.Models { return PartVersionRecord == null ? CreatedUtc : PartVersionRecord.CreatedUtc; } set { - if (PartVersionRecord != null) { + if (PartVersionRecord != null) PartVersionRecord.CreatedUtc = value; - } } } @@ -61,9 +60,8 @@ namespace Orchard.Core.Common.Models { return PartVersionRecord == null ? PublishedUtc : PartVersionRecord.PublishedUtc; } set { - if (PartVersionRecord != null) { + if (PartVersionRecord != null) PartVersionRecord.PublishedUtc = value; - } } } @@ -72,9 +70,8 @@ namespace Orchard.Core.Common.Models { return PartVersionRecord == null ? ModifiedUtc : PartVersionRecord.ModifiedUtc; } set { - if (PartVersionRecord != null) { + if (PartVersionRecord != null) PartVersionRecord.ModifiedUtc = value; - } } } } diff --git a/src/Orchard.Web/Core/Orchard.Core.csproj b/src/Orchard.Web/Core/Orchard.Core.csproj index 8892ebcee..345fdcb04 100644 --- a/src/Orchard.Web/Core/Orchard.Core.csproj +++ b/src/Orchard.Web/Core/Orchard.Core.csproj @@ -70,6 +70,7 @@ + @@ -131,7 +132,6 @@ - diff --git a/src/Orchard.Web/Modules/Orchard.PublishLater/ViewModels/PublishLaterViewModel.cs b/src/Orchard.Web/Modules/Orchard.PublishLater/ViewModels/PublishLaterViewModel.cs index 4c5392c16..8f1dccebe 100644 --- a/src/Orchard.Web/Modules/Orchard.PublishLater/ViewModels/PublishLaterViewModel.cs +++ b/src/Orchard.Web/Modules/Orchard.PublishLater/ViewModels/PublishLaterViewModel.cs @@ -1,6 +1,5 @@ using System; using Orchard.ContentManagement; -using Orchard.Core.Common.Models; using Orchard.PublishLater.Models; namespace Orchard.PublishLater.ViewModels { @@ -30,8 +29,6 @@ namespace Orchard.PublishLater.ViewModels { get { return IsPublished || ContentItem.ContentManager.Get(ContentItem.Id, VersionOptions.Published) != null; } } - public DateTime? VersionPublishedUtc { get { return ContentItem.As() == null ? null : ContentItem.As().VersionPublishedUtc; } } - public DateTime? ScheduledPublishUtc { get; set; } public string ScheduledPublishDate { get; set; }