2010-01-05 21:49:48 +00:00
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
2009-12-30 19:16:17 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using Autofac;
|
|
|
|
|
using Moq;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using Orchard.ContentManagement;
|
|
|
|
|
using Orchard.ContentManagement.Drivers;
|
|
|
|
|
using Orchard.ContentManagement.Handlers;
|
2010-04-07 13:30:18 -07:00
|
|
|
|
using Orchard.Environment.AutofacUtil;
|
2010-01-05 10:54:12 +00:00
|
|
|
|
using Orchard.Mvc.ViewModels;
|
2010-01-05 02:28:47 +00:00
|
|
|
|
using Orchard.UI.Zones;
|
2009-12-30 19:16:17 +00:00
|
|
|
|
|
|
|
|
|
namespace Orchard.Tests.ContentManagement {
|
|
|
|
|
[TestFixture]
|
2010-01-05 21:49:48 +00:00
|
|
|
|
public class ContentPartDriverHandlerTests {
|
2009-12-30 19:16:17 +00:00
|
|
|
|
private IContainer _container;
|
|
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
|
public void Init() {
|
|
|
|
|
var builder = new ContainerBuilder();
|
2010-04-02 15:17:13 -07:00
|
|
|
|
//builder.RegisterModule(new ImplicitCollectionSupportModule());
|
|
|
|
|
builder.RegisterType<ContentPartDriverHandler>().As<IContentHandler>();
|
2009-12-30 19:16:17 +00:00
|
|
|
|
_container = builder.Build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void DriverHandlerShouldNotThrowException() {
|
|
|
|
|
var contentHandler = _container.Resolve<IContentHandler>();
|
|
|
|
|
contentHandler.BuildDisplayModel(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void AllDriversShouldBeCalled() {
|
2010-01-05 21:49:48 +00:00
|
|
|
|
var driver1 = new Mock<IContentPartDriver>();
|
|
|
|
|
var driver2 = new Mock<IContentPartDriver>();
|
2010-04-02 15:17:13 -07:00
|
|
|
|
var builder = new ContainerUpdater();
|
|
|
|
|
builder.RegisterInstance(driver1.Object);
|
|
|
|
|
builder.RegisterInstance(driver2.Object);
|
|
|
|
|
builder.Update(_container);
|
2009-12-30 19:16:17 +00:00
|
|
|
|
var contentHandler = _container.Resolve<IContentHandler>();
|
|
|
|
|
|
2010-01-05 21:49:48 +00:00
|
|
|
|
var ctx = new BuildDisplayModelContext(new ContentItemViewModel(new ContentItem()), null);
|
2009-12-30 19:16:17 +00:00
|
|
|
|
|
|
|
|
|
driver1.Verify(x => x.BuildDisplayModel(ctx), Times.Never());
|
|
|
|
|
contentHandler.BuildDisplayModel(ctx);
|
|
|
|
|
driver1.Verify(x => x.BuildDisplayModel(ctx));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void TestDriverCanAddDisplay() {
|
|
|
|
|
var driver = new StubPartDriver();
|
2010-04-02 15:17:13 -07:00
|
|
|
|
var builder = new ContainerUpdater();
|
|
|
|
|
builder.RegisterInstance(driver).As<IContentPartDriver>();
|
|
|
|
|
builder.Update(_container);
|
2009-12-30 19:16:17 +00:00
|
|
|
|
|
|
|
|
|
var contentHandler = _container.Resolve<IContentHandler>();
|
|
|
|
|
|
|
|
|
|
var item = new ContentItem();
|
|
|
|
|
item.Weld(new StubPart { Foo = new[] { "a", "b", "c" } });
|
|
|
|
|
|
2010-01-05 21:49:48 +00:00
|
|
|
|
var ctx = new BuildDisplayModelContext(new ContentItemViewModel(item), "");
|
2010-01-05 10:54:12 +00:00
|
|
|
|
Assert.That(ctx.ViewModel.Zones.Count(), Is.EqualTo(0));
|
2009-12-30 19:16:17 +00:00
|
|
|
|
contentHandler.BuildDisplayModel(ctx);
|
2010-01-05 10:54:12 +00:00
|
|
|
|
Assert.That(ctx.ViewModel.Zones.Count(), Is.EqualTo(1));
|
|
|
|
|
Assert.That(ctx.ViewModel.Zones.Single().Key, Is.EqualTo("topmeta"));
|
2010-01-05 21:49:48 +00:00
|
|
|
|
Assert.That(ctx.ViewModel.Zones.Single().Value.Items.OfType<ContentPartDisplayZoneItem>().Single().Prefix, Is.EqualTo("Stub"));
|
2009-12-30 19:16:17 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-05 21:49:48 +00:00
|
|
|
|
public class StubPartDriver : ContentPartDriver<StubPart> {
|
2009-12-30 19:16:17 +00:00
|
|
|
|
protected override string Prefix {
|
|
|
|
|
get { return "Stub"; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override DriverResult Display(StubPart part, string displayType) {
|
|
|
|
|
var viewModel = new StubViewModel { Foo = string.Join(",", part.Foo) };
|
|
|
|
|
if (displayType.StartsWith("Summary"))
|
2010-01-05 21:49:48 +00:00
|
|
|
|
return ContentPartTemplate(viewModel, "StubViewModelTerse").Location("topmeta");
|
2009-12-30 19:16:17 +00:00
|
|
|
|
|
2010-01-05 21:49:48 +00:00
|
|
|
|
return ContentPartTemplate(viewModel).Location("topmeta");
|
2009-12-30 19:16:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override DriverResult Editor(StubPart part) {
|
|
|
|
|
var viewModel = new StubViewModel { Foo = string.Join(",", part.Foo) };
|
2010-01-05 21:49:48 +00:00
|
|
|
|
return ContentPartTemplate(viewModel).Location("last", "10");
|
2009-12-30 19:16:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override DriverResult Editor(StubPart part, IUpdateModel updater) {
|
|
|
|
|
var viewModel = new StubViewModel { Foo = string.Join(",", part.Foo) };
|
|
|
|
|
updater.TryUpdateModel(viewModel, Prefix, null, null);
|
|
|
|
|
part.Foo = viewModel.Foo.Split(new[] { ',' }).Select(x => x.Trim()).ToArray();
|
2010-01-05 21:49:48 +00:00
|
|
|
|
return ContentPartTemplate(viewModel).Location("last", "10");
|
2009-12-30 19:16:17 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class StubPart : ContentPart {
|
|
|
|
|
public string[] Foo { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class StubViewModel {
|
|
|
|
|
[Required]
|
|
|
|
|
public string Foo { get; set; }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|