diff --git a/src/Orchard.Core.Tests/Common/Services/RoutableServiceTests.cs b/src/Orchard.Core.Tests/Common/Services/RoutableServiceTests.cs index fc8792226..a8a42cb06 100644 --- a/src/Orchard.Core.Tests/Common/Services/RoutableServiceTests.cs +++ b/src/Orchard.Core.Tests/Common/Services/RoutableServiceTests.cs @@ -1,54 +1,115 @@ -using Autofac; +using System; +using System.Collections.Generic; +using Autofac; using Autofac.Builder; +using JetBrains.Annotations; +using Moq; using NUnit.Framework; +using Orchard.ContentManagement; +using Orchard.ContentManagement.Drivers; +using Orchard.ContentManagement.Handlers; +using Orchard.ContentManagement.Records; +using Orchard.Core.Common.Models; +using Orchard.Core.Common.Providers; +using Orchard.Core.Common.Records; using Orchard.Core.Common.Services; +using Orchard.Data; +using Orchard.Security; +using Orchard.Tests.Packages; namespace Orchard.Core.Tests.Common.Services { [TestFixture] - public class RoutableServiceTests { - #region Setup/Teardown - + public class RoutableServiceTests : DatabaseEnabledTestsBase { [SetUp] - public void Init() { - var builder = new ContainerBuilder(); - builder.Register().As(); - IContainer container = builder.Build(); - _routableService = container.Resolve(); + public override void Init() { + base.Init(); + _routableService = _container.Resolve(); } - #endregion + public override void Register(ContainerBuilder builder) { + builder.Register().As(); + builder.Register().As(); + builder.Register().As(); + } private IRoutableService _routableService; - //[Test] - //public void BeginningSlashesShouldBeReplacedByADash() { - // Assert.That(_routableService.Slugify("/slug"), Is.EqualTo("-slug")); - // Assert.That(_routableService.Slugify("//slug"), Is.EqualTo("-slug")); - // Assert.That(_routableService.Slugify("//////////////slug"), Is.EqualTo("-slug")); - //} + [Test] + public void InvalidCharactersShouldBeReplacedByADash() { + var contentManager = _container.Resolve(); - //[Test] - //public void MultipleSlashesShouldBecomeOne() { - // Assert.That(_routableService.Slugify("/slug//with///lots/of////s/lashes"), Is.EqualTo("-slug/with/lots/of/s/lashes")); - // Assert.That(_routableService.Slugify("slug/with/a/couple//slashes"), Is.EqualTo("slug/with/a/couple/slashes")); - //} + var thing = contentManager.Create(ThingDriver.ContentType.Name, t => { + t.As().Record = new RoutableRecord(); + t.Title = "Please do not use any of the following characters in your slugs: \":\", \"/\", \"?\", \"#\", \"[\", \"]\", \"@\", \"!\", \"$\", \"&\", \"'\", \"(\", \")\", \"*\", \"+\", \",\", \";\", \"=\""; + }); - //[Test] - //public void InvalidCharactersShouldBeReplacedByADash() { - // Assert.That(_routableService.Slugify( - // "Please do not use any of the following characters in your slugs: \":\", \"/\", \"?\", \"#\", \"[\", \"]\", \"@\", \"!\", \"$\", \"&\", \"'\", \"(\", \")\", \"*\", \"+\", \",\", \";\", \"=\""), - // Is.EqualTo( - // "Please-do-not-use-any-of-the-following-characters-in-your-slugs-\"-\"-\"/\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"")); - //} + _routableService.FillSlug(thing.As()); - //[Test] - //public void VeryLongStringTruncatedTo1000Chars() { - // var veryVeryLongSlug = "this is a very long slug..."; - // for (var i = 0; i < 100; i++) - // veryVeryLongSlug += "aaaaaaaaaa"; + Assert.That(thing.Slug, Is.EqualTo("Please-do-not-use-any-of-the-following-characters-in-your-slugs-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"")); + } - // Assert.That(veryVeryLongSlug.Length, Is.AtLeast(1001)); - // Assert.That(_routableService.Slugify(veryVeryLongSlug).Length, Is.EqualTo(1000)); - //} + [Test] + public void VeryLongStringTruncatedTo1000Chars() { + var contentManager = _container.Resolve(); + + var veryVeryLongTitle = "this is a very long slug..."; + for (var i = 0; i < 100; i++) + veryVeryLongTitle += "aaaaaaaaaa"; + + var thing = contentManager.Create(ThingDriver.ContentType.Name, t => { + t.As().Record = new RoutableRecord(); + t.Title = veryVeryLongTitle; + }); + + _routableService.FillSlug(thing.As()); + + Assert.That(veryVeryLongTitle.Length, Is.AtLeast(1001)); + Assert.That(thing.Slug.Length, Is.EqualTo(1000)); + } + + protected override IEnumerable DatabaseTypes { + get { + return new[] { + typeof(RoutableRecord), + typeof(ContentItemRecord), + typeof(ContentItemVersionRecord), + typeof(ContentTypeRecord), + typeof(CommonRecord), + typeof(CommonVersionRecord), + }; + } + } + + [UsedImplicitly] + public class ThingHandler : ContentHandler { + public ThingHandler() + { + Filters.Add(new ActivatingFilter(ThingDriver.ContentType.Name)); + Filters.Add(new ActivatingFilter>(ThingDriver.ContentType.Name)); + Filters.Add(new ActivatingFilter(ThingDriver.ContentType.Name)); + Filters.Add(new ActivatingFilter(ThingDriver.ContentType.Name)); + } + } + + public class Thing : ContentPart { + public int Id { get { return ContentItem.Id; } } + + public string Title { + get { return this.As().Title; } + set { this.As().Title = value; } + } + + public string Slug { + get { return this.As().Slug; } + set { this.As().Slug = value; } + } + } + + public class ThingDriver : ContentItemDriver { + public readonly static ContentType ContentType = new ContentType { + Name = "thing", + DisplayName = "Thing" + }; + } } } \ No newline at end of file diff --git a/src/Orchard.Web/Core/Common/Views/EditorTemplates/Parts/Common.Routable.ascx b/src/Orchard.Web/Core/Common/Views/EditorTemplates/Parts/Common.Routable.ascx index 60cbe87dc..b901e4a8c 100644 --- a/src/Orchard.Web/Core/Common/Views/EditorTemplates/Parts/Common.Routable.ascx +++ b/src/Orchard.Web/Core/Common/Views/EditorTemplates/Parts/Common.Routable.ascx @@ -16,6 +16,8 @@ <% using (this.Capture("end-of-page-scripts")) { %>