mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-14 10:54:50 +08:00
Merge
--HG-- branch : dev
This commit is contained in:
@@ -6,13 +6,18 @@ using JetBrains.Annotations;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using Orchard.ContentManagement.Aspects;
|
||||
using Orchard.Core.Common;
|
||||
using Orchard.Core.Common.Handlers;
|
||||
using Orchard.Core.Common.Models;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.Handlers;
|
||||
using Orchard.ContentManagement.Records;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Security;
|
||||
using Orchard.Tests.Modules;
|
||||
using Orchard.Mvc.ViewModels;
|
||||
using Orchard.Core.Common.ViewModels;
|
||||
using System.Web.Mvc;
|
||||
|
||||
namespace Orchard.Core.Tests.Common.Providers {
|
||||
[TestFixture]
|
||||
@@ -29,6 +34,7 @@ namespace Orchard.Core.Tests.Common.Providers {
|
||||
_authn = new Mock<IAuthenticationService>();
|
||||
_authz = new Mock<IAuthorizationService>();
|
||||
_membership = new Mock<IMembershipService>();
|
||||
|
||||
builder.RegisterInstance(_authn.Object);
|
||||
builder.RegisterInstance(_authz.Object);
|
||||
builder.RegisterInstance(_membership.Object);
|
||||
@@ -73,7 +79,83 @@ namespace Orchard.Core.Tests.Common.Providers {
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PublishingShouldSetPublishUtc() {
|
||||
public void PublishingShouldFailIfOwnerIsUnknown()
|
||||
{
|
||||
var contentManager = _container.Resolve<IContentManager>();
|
||||
var updateModel = new Mock<IUpdateModel>();
|
||||
|
||||
var user = contentManager.New<IUser>("user");
|
||||
_authn.Setup(x => x.GetAuthenticatedUser()).Returns(user);
|
||||
|
||||
var createUtc = _clock.UtcNow;
|
||||
var item = contentManager.Create<ICommonAspect>("test-item", VersionOptions.Draft, init => { });
|
||||
var viewModel = new OwnerEditorViewModel() { Owner = "user" };
|
||||
updateModel.Setup(x => x.TryUpdateModel(viewModel, "", null, null)).Returns(true);
|
||||
contentManager.UpdateEditorModel(item.ContentItem, updateModel.Object);
|
||||
}
|
||||
|
||||
class UpdatModelStub : IUpdateModel {
|
||||
|
||||
ModelStateDictionary _modelState = new ModelStateDictionary();
|
||||
|
||||
public ModelStateDictionary ModelErrors
|
||||
{
|
||||
get { return _modelState; }
|
||||
}
|
||||
|
||||
public string Owner { get; set; }
|
||||
|
||||
public bool TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) where TModel : class {
|
||||
(model as OwnerEditorViewModel).Owner = Owner;
|
||||
return true;
|
||||
}
|
||||
|
||||
public void AddModelError(string key, LocalizedString errorMessage) {
|
||||
_modelState.AddModelError(key, errorMessage.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PublishingShouldNotThrowExceptionIfOwnerIsNull()
|
||||
{
|
||||
var contentManager = _container.Resolve<IContentManager>();
|
||||
|
||||
var item = contentManager.Create<ICommonAspect>("test-item", VersionOptions.Draft, init => { });
|
||||
|
||||
var user = contentManager.New<IUser>("user");
|
||||
_authn.Setup(x => x.GetAuthenticatedUser()).Returns(user);
|
||||
_authz.Setup(x => x.TryCheckAccess(Permissions.ChangeOwner, user, item)).Returns(true);
|
||||
|
||||
item.Owner = user;
|
||||
|
||||
var updater = new UpdatModelStub() { Owner = null };
|
||||
|
||||
contentManager.UpdateEditorModel(item.ContentItem, updater);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PublishingShouldFailIfOwnerIsEmpty()
|
||||
{
|
||||
var contentManager = _container.Resolve<IContentManager>();
|
||||
|
||||
var item = contentManager.Create<ICommonAspect>("test-item", VersionOptions.Draft, init => { });
|
||||
|
||||
var user = contentManager.New<IUser>("user");
|
||||
_authn.Setup(x => x.GetAuthenticatedUser()).Returns(user);
|
||||
_authz.Setup(x => x.TryCheckAccess(Permissions.ChangeOwner, user, item)).Returns(true);
|
||||
|
||||
item.Owner = user;
|
||||
|
||||
var updater = new UpdatModelStub() {Owner = ""};
|
||||
|
||||
contentManager.UpdateEditorModel(item.ContentItem, updater);
|
||||
|
||||
Assert.That(updater.ModelErrors.ContainsKey("CommonAspect.Owner"), Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PublishingShouldSetPublishUtc()
|
||||
{
|
||||
var contentManager = _container.Resolve<IContentManager>();
|
||||
|
||||
var createUtc = _clock.UtcNow;
|
||||
@@ -81,7 +163,7 @@ namespace Orchard.Core.Tests.Common.Providers {
|
||||
|
||||
Assert.That(item.CreatedUtc, Is.EqualTo(createUtc));
|
||||
Assert.That(item.PublishedUtc, Is.Null);
|
||||
|
||||
|
||||
_clock.Advance(TimeSpan.FromMinutes(1));
|
||||
var publishUtc = _clock.UtcNow;
|
||||
|
||||
@@ -91,6 +173,7 @@ namespace Orchard.Core.Tests.Common.Providers {
|
||||
Assert.That(item.PublishedUtc, Is.EqualTo(publishUtc));
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void VersioningItemShouldCreatedAndPublishedUtcValuesPerVersion() {
|
||||
var contentManager = _container.Resolve<IContentManager>();
|
||||
|
@@ -10,6 +10,10 @@ using Orchard.ContentManagement.Records;
|
||||
using Orchard.Core.Common.Models;
|
||||
using Orchard.Core.Common.Services;
|
||||
using Orchard.Tests.Modules;
|
||||
using Orchard.Core.Common.Handlers;
|
||||
using System.Web.Mvc;
|
||||
using System.Web.Routing;
|
||||
using Orchard.Tests.Stubs;
|
||||
|
||||
namespace Orchard.Core.Tests.Common.Services {
|
||||
[TestFixture]
|
||||
@@ -23,7 +27,13 @@ namespace Orchard.Core.Tests.Common.Services {
|
||||
public override void Register(ContainerBuilder builder) {
|
||||
builder.RegisterType<DefaultContentManager>().As<IContentManager>();
|
||||
builder.RegisterType<ThingHandler>().As<IContentHandler>();
|
||||
builder.RegisterType<StuffHandler>().As<IContentHandler>();
|
||||
builder.RegisterType<RoutableService>().As<IRoutableService>();
|
||||
|
||||
builder.RegisterType<DefaultContentQuery>().As<IContentQuery>();
|
||||
builder.RegisterInstance(new UrlHelper(new RequestContext(new StubHttpContext("~/"), new RouteData()))).As<UrlHelper>();
|
||||
builder.RegisterType<RoutableAspectHandler>().As<IContentHandler>();
|
||||
|
||||
}
|
||||
|
||||
private IRoutableService _routableService;
|
||||
@@ -42,6 +52,24 @@ namespace Orchard.Core.Tests.Common.Services {
|
||||
Assert.That(thing.Slug, Is.EqualTo("please-do-not-use-any-of-the-following-characters-in-your-slugs-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\""));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EmptySlugsShouldBeConsideredValid() {
|
||||
// so that automatic generation on Publish occurs
|
||||
Assert.That(_routableService.IsSlugValid(null), Is.True);
|
||||
Assert.That(_routableService.IsSlugValid(String.Empty), Is.True);
|
||||
Assert.That(_routableService.IsSlugValid(" "), Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InvalidCharacterShouldBeRefusedInSlugs() {
|
||||
Assert.That(_routableService.IsSlugValid("aaaa-_aaaa"), Is.True);
|
||||
|
||||
foreach (var c in @"/:?#[]@!$&'()*+,;= ") {
|
||||
Assert.That(_routableService.IsSlugValid("a" + c + "b"), Is.False);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void VeryLongStringTruncatedTo1000Chars() {
|
||||
var contentManager = _container.Resolve<IContentManager>();
|
||||
@@ -111,6 +139,47 @@ namespace Orchard.Core.Tests.Common.Services {
|
||||
Assert.That(thing.Slug, Is.EqualTo("this-is-some-interesting-title"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GeneratedSlugsShouldBeUniqueAmongContentType()
|
||||
{
|
||||
var contentManager = _container.Resolve<IContentManager>();
|
||||
|
||||
var thing1 = contentManager.Create<Thing>(ThingDriver.ContentType.Name, t =>
|
||||
{
|
||||
t.As<RoutableAspect>().Record = new RoutableRecord();
|
||||
t.Title = "This Is Some Interesting Title";
|
||||
});
|
||||
|
||||
var thing2 = contentManager.Create<Thing>(ThingDriver.ContentType.Name , t =>
|
||||
{
|
||||
t.As<RoutableAspect>().Record = new RoutableRecord();
|
||||
t.Title = "This Is Some Interesting Title";
|
||||
});
|
||||
|
||||
Assert.AreNotEqual(thing1.Slug, thing2.Slug);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SlugsCanBeDuplicatedAccrossContentTypes()
|
||||
{
|
||||
var contentManager = _container.Resolve<IContentManager>();
|
||||
|
||||
var thing = contentManager.Create<Thing>(ThingDriver.ContentType.Name, t =>
|
||||
{
|
||||
t.As<RoutableAspect>().Record = new RoutableRecord();
|
||||
t.Title = "This Is Some Interesting Title";
|
||||
});
|
||||
|
||||
var stuff = contentManager.Create<Stuff>(StuffDriver.ContentType.Name, s =>
|
||||
{
|
||||
s.As<RoutableAspect>().Record = new RoutableRecord();
|
||||
s.Title = "This Is Some Interesting Title";
|
||||
});
|
||||
|
||||
Assert.AreEqual(thing.Slug, stuff.Slug);
|
||||
}
|
||||
|
||||
|
||||
protected override IEnumerable<Type> DatabaseTypes {
|
||||
get {
|
||||
return new[] {
|
||||
@@ -155,5 +224,43 @@ namespace Orchard.Core.Tests.Common.Services {
|
||||
DisplayName = "Thing"
|
||||
};
|
||||
}
|
||||
|
||||
[UsedImplicitly]
|
||||
public class StuffHandler : ContentHandler
|
||||
{
|
||||
public StuffHandler()
|
||||
{
|
||||
Filters.Add(new ActivatingFilter<Stuff>(StuffDriver.ContentType.Name));
|
||||
Filters.Add(new ActivatingFilter<ContentPart<CommonVersionRecord>>(StuffDriver.ContentType.Name));
|
||||
Filters.Add(new ActivatingFilter<CommonAspect>(StuffDriver.ContentType.Name));
|
||||
Filters.Add(new ActivatingFilter<RoutableAspect>(StuffDriver.ContentType.Name));
|
||||
}
|
||||
}
|
||||
|
||||
public class Stuff : ContentPart
|
||||
{
|
||||
public int Id { get { return ContentItem.Id; } }
|
||||
|
||||
public string Title
|
||||
{
|
||||
get { return this.As<RoutableAspect>().Title; }
|
||||
set { this.As<RoutableAspect>().Title = value; }
|
||||
}
|
||||
|
||||
public string Slug
|
||||
{
|
||||
get { return this.As<RoutableAspect>().Slug; }
|
||||
set { this.As<RoutableAspect>().Slug = value; }
|
||||
}
|
||||
}
|
||||
|
||||
public class StuffDriver : ContentItemDriver<Stuff>
|
||||
{
|
||||
public readonly static ContentType ContentType = new ContentType
|
||||
{
|
||||
Name = "stuff",
|
||||
DisplayName = "Stuff"
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
@@ -60,6 +60,9 @@
|
||||
<HintPath>..\..\lib\sqlite\System.Data.SQLite.DLL</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.Web.Abstractions">
|
||||
<RequiredTargetFramework>3.5</RequiredTargetFramework>
|
||||
</Reference>
|
||||
<Reference Include="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\lib\aspnetmvc\System.Web.Mvc.dll</HintPath>
|
||||
|
Reference in New Issue
Block a user