--HG--
branch : mvc3p1
This commit is contained in:
Louis DeJardin
2010-08-26 18:34:12 -07:00
10 changed files with 112 additions and 112 deletions

View File

@@ -101,7 +101,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Common\Providers\CommonPartProviderTests.cs" />
<Compile Include="Common\Services\RoutableServiceTests.cs" />
<Compile Include="Routable\Services\RoutableServiceTests.cs" />
<Compile Include="Feeds\Controllers\FeedControllerTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Scheduling\ScheduledTaskManagerTests.cs" />

View File

@@ -5,6 +5,7 @@ using JetBrains.Annotations;
using Moq;
using NUnit.Framework;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Aspects;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers;
using Orchard.ContentManagement.MetaData;
@@ -14,12 +15,16 @@ using Orchard.Core.Routable;
using Orchard.Core.Routable.Handlers;
using Orchard.Core.Routable.Models;
using Orchard.Core.Routable.Services;
using Orchard.Data;
using Orchard.Environment;
using Orchard.Security;
using Orchard.Tests.Modules;
using System.Web.Mvc;
using System.Web.Routing;
using Orchard.Tests.Stubs;
using Orchard.UI.Notify;
namespace Orchard.Core.Tests.Common.Services {
namespace Orchard.Core.Tests.Routable.Services {
[TestFixture]
public class RoutableServiceTests : DatabaseEnabledTestsBase {
[SetUp]
@@ -32,6 +37,10 @@ namespace Orchard.Core.Tests.Common.Services {
builder.RegisterType<DefaultContentManager>().As<IContentManager>();
builder.RegisterType<DefaultContentManagerSession>().As<IContentManagerSession>();
builder.RegisterInstance(new Mock<IContentDefinitionManager>().Object);
builder.RegisterInstance(new Mock<ITransactionManager>().Object);
builder.RegisterInstance(new Mock<IAuthorizer>().Object);
builder.RegisterInstance(new Mock<INotifier>().Object);
builder.RegisterType<OrchardServices>().As<IOrchardServices>();
builder.RegisterType<ThingHandler>().As<IContentHandler>();
builder.RegisterType<StuffHandler>().As<IContentHandler>();
@@ -41,7 +50,6 @@ namespace Orchard.Core.Tests.Common.Services {
builder.RegisterType<DefaultContentQuery>().As<IContentQuery>();
builder.RegisterInstance(new UrlHelper(new RequestContext(new StubHttpContext("~/"), new RouteData()))).As<UrlHelper>();
builder.RegisterType<RoutePartHandler>().As<IContentHandler>();
}
private IRoutableService _routableService;
@@ -52,12 +60,17 @@ namespace Orchard.Core.Tests.Common.Services {
var thing = contentManager.Create<Thing>(ThingDriver.ContentType.Name, t => {
t.As<RoutePart>().Record = new RoutePartRecord();
t.Title = "Please do not use any of the following characters in your slugs: \":\", \"/\", \"?\", \"#\", \"[\", \"]\", \"@\", \"!\", \"$\", \"&\", \"'\", \"(\", \")\", \"*\", \"+\", \",\", \";\", \"=\"";
t.Title = "Please do not use any of the following characters in your slugs: \":\", \"?\", \"#\", \"[\", \"]\", \"@\", \"!\", \"$\", \"&\", \"'\", \"(\", \")\", \"*\", \"+\", \",\", \";\", \"=\"";
});
_routableService.FillSlug(thing.As<RoutePart>());
_routableService.FillSlugFromTitle(thing.As<RoutePart>());
Assert.That(thing.Slug, Is.EqualTo("please-do-not-use-any-of-the-following-characters-in-your-slugs-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\""));
Assert.That(thing.Slug, Is.EqualTo("please-do-not-use-any-of-the-following-characters-in-your-slugs-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\""));
}
[Test]
public void SlashInSlugIsAllowed() {
Assert.That(_routableService.IsSlugValid("some/page"), Is.True);
}
[Test]
@@ -72,7 +85,7 @@ namespace Orchard.Core.Tests.Common.Services {
public void InvalidCharacterShouldBeRefusedInSlugs() {
Assert.That(_routableService.IsSlugValid("aaaa-_aaaa"), Is.True);
foreach (var c in @"/:?#[]@!$&'()*+,;= ") {
foreach (var c in @":?#[]@!$&'()*+,;= ") {
Assert.That(_routableService.IsSlugValid("a" + c + "b"), Is.False);
}
}
@@ -80,18 +93,12 @@ namespace Orchard.Core.Tests.Common.Services {
[Test]
public void VeryLongStringTruncatedTo1000Chars() {
var contentManager = _container.Resolve<IContentManager>();
var veryVeryLongTitle = "this is a very long title...";
for (var i = 0; i < 100; i++)
veryVeryLongTitle += "aaaaaaaaaa";
var thing = contentManager.Create<Thing>(ThingDriver.ContentType.Name, t => {
t.As<RoutePart>().Record = new RoutePartRecord();
t.Title = veryVeryLongTitle;
});
_routableService.FillSlug(thing.As<RoutePart>());
var thing = CreateRoutePart(veryVeryLongTitle);
_routableService.FillSlugFromTitle(thing);
Assert.That(veryVeryLongTitle.Length, Is.AtLeast(1001));
Assert.That(thing.Slug.Length, Is.EqualTo(1000));
@@ -99,86 +106,72 @@ namespace Orchard.Core.Tests.Common.Services {
[Test]
public void NoExistingLikeSlugsGeneratesSameSlug() {
string slug = _routableService.GenerateUniqueSlug("woohoo", null);
string slug = _routableService.GenerateUniqueSlug(CreateRoutePart("woohoo"), null);
Assert.That(slug, Is.EqualTo("woohoo"));
}
[Test]
public void ExistingSingleLikeSlugThatsAConflictGeneratesADash2() {
string slug = _routableService.GenerateUniqueSlug("woohoo", new List<string> { "woohoo" });
string slug = _routableService.GenerateUniqueSlug(CreateRoutePart("woohoo"), new List<string> { "woohoo" });
Assert.That(slug, Is.EqualTo("woohoo-2"));
}
[Test]
public void ExistingSingleLikeSlugThatsNotAConflictGeneratesSameSlug() {
string slug = _routableService.GenerateUniqueSlug("woohoo", new List<string> { "woohoo-2" });
string slug = _routableService.GenerateUniqueSlug(CreateRoutePart("woohoo"), new List<string> { "woohoo-2" });
Assert.That(slug, Is.EqualTo("woohoo"));
}
[Test]
public void ExistingLikeSlugsWithAConflictGeneratesADashVNext() {
string slug = _routableService.GenerateUniqueSlug("woohoo", new List<string> { "woohoo", "woohoo-2" });
string slug = _routableService.GenerateUniqueSlug(CreateRoutePart("woohoo"), new List<string> { "woohoo", "woohoo-2" });
Assert.That(slug, Is.EqualTo("woohoo-3"));
}
[Test]
public void ExistingSlugsWithVersionGapsAndNoMatchGeneratesSameSlug() {
string slug = _routableService.GenerateUniqueSlug("woohoo", new List<string> { "woohoo-2", "woohoo-4", "woohoo-5" });
string slug = _routableService.GenerateUniqueSlug(CreateRoutePart("woohoo"), new List<string> { "woohoo-2", "woohoo-4", "woohoo-5" });
Assert.That(slug, Is.EqualTo("woohoo"));
}
[Test]
public void ExistingSlugsWithVersionGapsAndAMatchGeneratesADash2() {
string slug = _routableService.GenerateUniqueSlug("woohoo-2", new List<string> { "woohoo-2", "woohoo-4", "woohoo-5" });
string slug = _routableService.GenerateUniqueSlug(CreateRoutePart("woohoo-2"), new List<string> { "woohoo-2", "woohoo-4", "woohoo-5" });
Assert.That(slug, Is.EqualTo("woohoo-2-2"));
}
[Test]
public void GeneratedSlugIsLowerCased() {
var contentManager = _container.Resolve<IContentManager>();
var thing = CreateRoutePart("This Is Some Interesting Title");
var thing = contentManager.Create<Thing>(ThingDriver.ContentType.Name, t => {
t.As<RoutePart>().Record = new RoutePartRecord();
t.Title = "This Is Some Interesting Title";
});
_routableService.FillSlug(thing.As<RoutePart>());
_routableService.FillSlugFromTitle(thing);
Assert.That(thing.Slug, Is.EqualTo("this-is-some-interesting-title"));
}
[Test, Ignore("Fix pending")]
public void GeneratedSlugsShouldBeUniqueAmongContentType() {
var contentManager = _container.Resolve<IContentManager>();
[Test]
public void SlugInConflictWithAnExistingItemsPathIsVersioned() {
var thing1 = CreateRoutePart("bar", "bar", "foo");
var thing2 = CreateRoutePart("fooslashbar", "foo/bar");
var thing1 = contentManager.Create<Thing>(ThingDriver.ContentType.Name, t => {
t.As<RoutePart>().Record = new RoutePartRecord();
t.Title = "This Is Some Interesting Title";
});
var thing2 = contentManager.Create<Thing>(ThingDriver.ContentType.Name, t => {
t.As<RoutePart>().Record = new RoutePartRecord();
t.Title = "This Is Some Interesting Title";
});
Assert.AreNotEqual(thing1.Slug, thing2.Slug);
Assert.That(thing2.Slug, Is.EqualTo("foo/bar-2"));
}
[Test]
public void SlugsCanBeDuplicatedAccrossContentTypes() {
private RoutePart CreateRoutePart(string title, string slug = "", string containerPath = "") {
var contentManager = _container.Resolve<IContentManager>();
var thing = contentManager.Create<Thing>(ThingDriver.ContentType.Name, t => {
t.As<RoutePart>().Record = new RoutePartRecord();
t.Title = "This Is Some Interesting Title";
});
var stuff = contentManager.Create<Stuff>(StuffDriver.ContentType.Name, s => {
s.As<RoutePart>().Record = new RoutePartRecord();
s.Title = "This Is Some Interesting Title";
});
Assert.AreEqual(thing.Slug, stuff.Slug);
return contentManager.Create<Thing>(ThingDriver.ContentType.Name, t => {
t.As<RoutePart>().Record = new RoutePartRecord();
if (!string.IsNullOrWhiteSpace(slug))
t.As<RoutePart>().Slug = slug;
t.Title = title;
if (!string.IsNullOrWhiteSpace(containerPath)) {
t.As<ICommonPart>().Container = contentManager.Create<Thing>(ThingDriver.ContentType.Name, tt => {
tt.As<RoutePart>().Path = containerPath;
tt.As<RoutePart>().Slug = containerPath;
});
}
})
.As<RoutePart>();
}

View File

@@ -59,9 +59,9 @@ namespace Orchard.Core.Routable.Drivers {
model.DisplayLeadingPath = path.Substring(0, path.Length - slug.Length);
}
else {
var containerSlug = part.GetContainerSlug();
model.DisplayLeadingPath = !string.IsNullOrWhiteSpace(containerSlug)
? string.Format("{0}/", containerSlug)
var containerPath = part.GetContainerPath();
model.DisplayLeadingPath = !string.IsNullOrWhiteSpace(containerPath)
? string.Format("{0}/", containerPath)
: "";
}
@@ -76,10 +76,9 @@ namespace Orchard.Core.Routable.Drivers {
updater.TryUpdateModel(model, Prefix, null, null);
part.Title = model.Title;
part.Slug = model.Slug;
part.Path = part.GetPathFromSlug(model.Slug);
if (!_routableService.IsSlugValid(part.Slug)) {
updater.AddModelError("Routable.Slug", T("Please do not use any of the following characters in your slugs: \"/\", \":\", \"?\", \"#\", \"[\", \"]\", \"@\", \"!\", \"$\", \"&\", \"'\", \"(\", \")\", \"*\", \"+\", \",\", \";\", \"=\". No spaces are allowed (please use dashes or underscores instead)."));
updater.AddModelError("Routable.Slug", T("Please do not use any of the following characters in your slugs: \":\", \"?\", \"#\", \"[\", \"]\", \"@\", \"!\", \"$\", \"&\", \"'\", \"(\", \")\", \"*\", \"+\", \",\", \";\", \"=\". No spaces are allowed (please use dashes or underscores instead)."));
}
string originalSlug = part.Slug;
@@ -89,7 +88,7 @@ namespace Orchard.Core.Routable.Drivers {
}
// TEMP: path format patterns replaces this logic
part.Path = part.GetPathFromSlug(part.Slug);
part.Path = part.GetPathWithSlug(part.Slug);
if (part.ContentItem.Id != 0 && model.PromoteToHomePage && _routableHomePageProvider != null) {
CurrentSite.HomePage = _routableHomePageProvider.GetSettingValue(part.ContentItem.Id);

View File

@@ -1,24 +1,50 @@
using System.Web.Routing;
using System;
using System.Web.Routing;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Handlers;
using Orchard.Core.Routable.Models;
using Orchard.Core.Routable.Services;
using Orchard.Data;
using Orchard.Localization;
using Orchard.UI.Notify;
namespace Orchard.Core.Routable.Handlers {
public class RoutePartHandler : ContentHandler {
private readonly IOrchardServices _services;
private readonly IRoutablePathConstraint _routablePathConstraint;
private readonly IRoutableService _routableService;
public RoutePartHandler(IRepository<RoutePartRecord> repository, IRoutablePathConstraint routablePathConstraint) {
public RoutePartHandler(IOrchardServices services, IRepository<RoutePartRecord> repository, IRoutablePathConstraint routablePathConstraint, IRoutableService routableService) {
_services = services;
_routablePathConstraint = routablePathConstraint;
_routableService = routableService;
T = NullLocalizer.Instance;
Filters.Add(StorageFilter.For(repository));
Action<RoutePart> processSlug = (
routable => {
var originalSlug = routable.Slug;
if (!_routableService.ProcessSlug(routable)) {
_services.Notifier.Warning(T("Slugs in conflict. \"{0}\" is already set for a previously created {2} so now it has the slug \"{1}\"",
originalSlug, routable.Slug, routable.ContentItem.ContentType));
}
// TEMP: path format patterns replaces this logic
routable.Path = routable.GetPathWithSlug(routable.Slug);
});
OnPublished<RoutePart>((context, routable) => {
if (context.PublishingItemVersionRecord != null)
processSlug(routable);
if (!string.IsNullOrEmpty(routable.Path))
_routablePathConstraint.AddPath(routable.Path);
});
OnIndexing<RoutePart>((context, part) => context.DocumentIndex.Add("title", part.Record.Title).RemoveTags().Analyze());
}
public Localizer T { get; set; }
}
public class RoutePartHandlerBase : ContentHandlerBase {

View File

@@ -18,24 +18,24 @@ namespace Orchard.Core.Routable.Models {
set { Record.Path = value; }
}
public string GetContainerSlug() {
public string GetContainerPath() {
var commonAspect = this.As<ICommonPart>();
if (commonAspect != null && commonAspect.Container != null) {
var routable = commonAspect.Container.As<IRoutableAspect>();
if (routable != null) {
return routable.Slug;
return routable.Path;
}
}
return null;
}
public string GetPathFromSlug(string slug) {
public string GetPathWithSlug(string slug) {
// TEMP: path format patterns replaces this logic
var containerSlug = GetContainerSlug();
if (string.IsNullOrEmpty(containerSlug)) {
var containerPath = GetContainerPath();
if (string.IsNullOrEmpty(containerPath)) {
return slug;
}
return containerSlug + "/" + slug;
return containerPath + "/" + slug;
}
}
}

View File

@@ -4,14 +4,13 @@ using Orchard.Core.Routable.Models;
namespace Orchard.Core.Routable.Services {
public interface IRoutableService : IDependency {
void FillSlug<TModel>(TModel model) where TModel : RoutePart;
void FillSlug<TModel>(TModel model, Func<string, string> generateSlug) where TModel : RoutePart;
string GenerateUniqueSlug(string slugCandidate, IEnumerable<string> existingSlugs);
void FillSlugFromTitle<TModel>(TModel model) where TModel : RoutePart;
string GenerateUniqueSlug(RoutePart part, IEnumerable<string> existingPaths);
/// <summary>
/// Returns any content item of the specified content type with similar slugs
/// Returns any content item with similar path
/// </summary>
IEnumerable<RoutePart> GetSimilarSlugs(string contentType, string slug);
IEnumerable<RoutePart> GetSimilarPaths(string path);
/// <summary>
/// Validates the given slug

View File

@@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Orchard.ContentManagement;
using Orchard.Core.Common.Models;
using Orchard.Core.Routable.Models;
namespace Orchard.Core.Routable.Services {
@@ -14,7 +13,7 @@ namespace Orchard.Core.Routable.Services {
_contentManager = contentManager;
}
public void FillSlug<TModel>(TModel model) where TModel : RoutePart {
public void FillSlugFromTitle<TModel>(TModel model) where TModel : RoutePart {
if (!string.IsNullOrEmpty(model.Slug) || string.IsNullOrEmpty(model.Title))
return;
@@ -30,18 +29,12 @@ namespace Orchard.Core.Routable.Services {
model.Slug = slug.ToLowerInvariant();
}
public void FillSlug<TModel>(TModel model, Func<string, string> generateSlug) where TModel : RoutePart {
if (!string.IsNullOrEmpty(model.Slug) || string.IsNullOrEmpty(model.Title))
return;
model.Slug = generateSlug(model.Title).ToLowerInvariant();
}
public string GenerateUniqueSlug(string slugCandidate, IEnumerable<string> existingSlugs) {
if (existingSlugs == null || !existingSlugs.Contains(slugCandidate))
public string GenerateUniqueSlug(RoutePart part, IEnumerable<string> existingPaths) {
var slugCandidate = part.Slug;
if (existingPaths == null || !existingPaths.Contains(part.Path))
return slugCandidate;
int? version = existingSlugs.Select(s => GetSlugVersion(slugCandidate, s)).OrderBy(i => i).LastOrDefault();
int? version = existingPaths.Select(s => GetSlugVersion(slugCandidate, s)).OrderBy(i => i).LastOrDefault();
return version != null
? string.Format("{0}-{1}", slugCandidate, version)
@@ -60,43 +53,38 @@ namespace Orchard.Core.Routable.Services {
: null;
}
public IEnumerable<RoutePart> GetSimilarSlugs(string contentType, string slug)
{
public IEnumerable<RoutePart> GetSimilarPaths(string path) {
return
_contentManager.Query().Join<RoutePartRecord>()
.List()
.Select(i => i.As<RoutePart>())
.Where(routable => routable.Path != null && routable.Path.Equals(slug, StringComparison.OrdinalIgnoreCase)) // todo: for some reason the filter doesn't work within the query, even without StringComparison or StartsWith
.Where(routable => routable.Path != null && routable.Path.StartsWith(path, StringComparison.OrdinalIgnoreCase)) // todo: for some reason the filter doesn't work within the query, even without StringComparison or StartsWith
.ToArray();
}
public bool IsSlugValid(string slug) {
// see http://tools.ietf.org/html/rfc3987 for prohibited chars
return slug == null || String.IsNullOrEmpty(slug.Trim()) || Regex.IsMatch(slug, @"^[^/:?#\[\]@!$&'()*+,;=\s]+$");
return slug == null || String.IsNullOrEmpty(slug.Trim()) || Regex.IsMatch(slug, @"^[^:?#\[\]@!$&'()*+,;=\s]+$");
}
public bool ProcessSlug(RoutePart part)
{
FillSlug(part);
public bool ProcessSlug(RoutePart part) {
FillSlugFromTitle(part);
if (string.IsNullOrEmpty(part.Slug))
{
return true;
}
var slugsLikeThis = GetSimilarSlugs(part.ContentItem.ContentType, part.Path);
part.Path = part.GetPathWithSlug(part.Slug);
var pathsLikeThis = GetSimilarPaths(part.Path);
// If the part is already a valid content item, don't include it in the list
// of slug to consider for conflict detection
if (part.ContentItem.Id != 0)
slugsLikeThis = slugsLikeThis.Where(p => p.ContentItem.Id != part.ContentItem.Id);
// Don't include *this* part in the list
// of slugs to consider for conflict detection
pathsLikeThis = pathsLikeThis.Where(p => p.ContentItem.Id != part.ContentItem.Id);
//todo: (heskew) need better messages
if (slugsLikeThis.Count() > 0)
{
if (pathsLikeThis.Count() > 0) {
var originalSlug = part.Slug;
//todo: (heskew) make auto-uniqueness optional
part.Slug = GenerateUniqueSlug(part.Slug, slugsLikeThis.Select(p => p.Slug));
part.Slug = GenerateUniqueSlug(part, pathsLikeThis.Select(p => p.Path));
if (originalSlug != part.Slug) {
return false;

View File

@@ -100,7 +100,7 @@ namespace Orchard.Blogs.Commands {
post.As<ICommonPart>().Container = blog;
var slug = Slugify(postName);
post.As<RoutePart>().Slug = slug;
post.As<RoutePart>().Path = post.As<RoutePart>().GetPathFromSlug(slug);
post.As<RoutePart>().Path = post.As<RoutePart>().GetPathWithSlug(slug);
post.As<RoutePart>().Title = postName;
post.As<BodyPart>().Text = item.Element("description").Value;
_contentManager.Create(post);

View File

@@ -182,8 +182,8 @@ namespace Orchard.Blogs.Services {
if (blogPost.Is<RoutePart>()) {
blogPost.As<RoutePart>().Title = title;
blogPost.As<RoutePart>().Slug = slug;
_routableService.FillSlug(blogPost.As<RoutePart>());
blogPost.As<RoutePart>().Path = blogPost.As<RoutePart>().GetPathFromSlug(blogPost.As<RoutePart>().Slug);
_routableService.FillSlugFromTitle(blogPost.As<RoutePart>());
blogPost.As<RoutePart>().Path = blogPost.As<RoutePart>().GetPathWithSlug(blogPost.As<RoutePart>().Slug);
}
_contentManager.Create(blogPost.ContentItem, VersionOptions.Draft);

View File

@@ -825,11 +825,6 @@ table .button {
.settings legend {
margin:0 0 -.4em 0;
}
/* todo: This style is the same as textMedium. Consolidate if possible */
.settings input.text, .settings input.text-box {
width:26em;
}
/* Fields
----------------------------------------------------------*/