mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Adding regex validation for the RoutableAspect's slug and tossed in a few slugify tests
--HG-- extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4045627
This commit is contained in:
@@ -0,0 +1,54 @@
|
|||||||
|
using Autofac;
|
||||||
|
using Autofac.Builder;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using Orchard.Core.Common.Services;
|
||||||
|
|
||||||
|
namespace Orchard.Core.Tests.Common.Services {
|
||||||
|
[TestFixture]
|
||||||
|
public class RoutableServiceTests {
|
||||||
|
#region Setup/Teardown
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void Init() {
|
||||||
|
var builder = new ContainerBuilder();
|
||||||
|
builder.Register<RoutableService>().As<IRoutableService>();
|
||||||
|
IContainer container = builder.Build();
|
||||||
|
_routableService = container.Resolve<IRoutableService>();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
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 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"));
|
||||||
|
}
|
||||||
|
|
||||||
|
[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-\"-\"-\"/\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\"-\""));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void VeryLongStringTruncatedTo1000Chars() {
|
||||||
|
var veryVeryLongSlug = "this is a very long slug...";
|
||||||
|
for (var i = 0; i < 100; i++)
|
||||||
|
veryVeryLongSlug += "aaaaaaaaaa";
|
||||||
|
|
||||||
|
Assert.That(veryVeryLongSlug.Length, Is.AtLeast(1001));
|
||||||
|
Assert.That(_routableService.Slugify(veryVeryLongSlug).Length, Is.EqualTo(1000));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -78,6 +78,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Common\Providers\CommonAspectProviderTests.cs" />
|
<Compile Include="Common\Providers\CommonAspectProviderTests.cs" />
|
||||||
|
<Compile Include="Common\Services\RoutableServiceTests.cs" />
|
||||||
<Compile Include="Feeds\Controllers\FeedControllerTests.cs" />
|
<Compile Include="Feeds\Controllers\FeedControllerTests.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="Scheduling\ScheduledTaskManagerTests.cs" />
|
<Compile Include="Scheduling\ScheduledTaskManagerTests.cs" />
|
||||||
|
@@ -11,6 +11,7 @@ namespace Orchard.Core.Common.ViewModels {
|
|||||||
set { RoutableAspect.Record.Title = value; }
|
set { RoutableAspect.Record.Title = value; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[RegularExpression(@"^[^/:?#\[\]@!$&'()*+,;=\s](?(?=/)/[^/:?#\[\]@!$&'()*+,;=\s]|[^:?#\[\]@!$&'()*+,;=\s])*$")]
|
||||||
public string Slug {
|
public string Slug {
|
||||||
get { return RoutableAspect.Record.Slug; }
|
get { return RoutableAspect.Record.Slug; }
|
||||||
set { RoutableAspect.Record.Slug = value; }
|
set { RoutableAspect.Record.Slug = value; }
|
||||||
|
Reference in New Issue
Block a user