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:
skewed
2010-01-18 17:40:06 +00:00
parent 1a5996274d
commit 03d725b9d3
3 changed files with 56 additions and 0 deletions

View File

@@ -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));
}
}
}

View File

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

View File

@@ -11,6 +11,7 @@ namespace Orchard.Core.Common.ViewModels {
set { RoutableAspect.Record.Title = value; }
}
[RegularExpression(@"^[^/:?#\[\]@!$&'()*+,;=\s](?(?=/)/[^/:?#\[\]@!$&'()*+,;=\s]|[^:?#\[\]@!$&'()*+,;=\s])*$")]
public string Slug {
get { return RoutableAspect.Record.Slug; }
set { RoutableAspect.Record.Slug = value; }