Skip to content

Commit

Permalink
Ensure forward slash on conten type routes. Fixes #1245
Browse files Browse the repository at this point in the history
  • Loading branch information
tidyui committed Jun 23, 2020
1 parent 57dd9c9 commit 9964c49
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
13 changes: 11 additions & 2 deletions core/Piranha.AttributeBuilder/PageTypeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,20 @@ protected override PageType GetContentType(Type type)
foreach (PageTypeRouteAttribute route in routes)
{
if (!string.IsNullOrWhiteSpace(route.Title) && !string.IsNullOrWhiteSpace(route.Route))
pageType.Routes.Add(new ContentTypeRoute
{
var contentRoute = new ContentTypeRoute
{
Title = route.Title,
Route = route.Route
});
};

// Make sure the route starts with a forward slash
if (!contentRoute.Route.StartsWith("/"))
{
contentRoute.Route = $"/{ contentRoute.Route }";
}
pageType.Routes.Add(contentRoute);
}
}

// Add default custom editors
Expand Down
11 changes: 9 additions & 2 deletions core/Piranha.AttributeBuilder/PostTypeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,18 @@ protected override PostType GetContentType(Type type)
{
if (!string.IsNullOrWhiteSpace(route.Title) && !string.IsNullOrWhiteSpace(route.Route))
{
postType.Routes.Add(new ContentTypeRoute
var contentRoute = new ContentTypeRoute
{
Title = route.Title,
Route = route.Route
});
};

// Make sure the route starts with a forward slash
if (!contentRoute.Route.StartsWith("/"))
{
contentRoute.Route = $"/{ contentRoute.Route }";
}
postType.Routes.Add(contentRoute);
}
}

Expand Down
48 changes: 48 additions & 0 deletions test/Piranha.Tests/AttributeBuilder/TypeBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public class SimplePageType
public Extend.Fields.TextField Body { get; set; }
}

[PageType(Id = "Routed", Title = "Routed Page Type")]
[PageTypeRoute(Title = "Default", Route = "pageroute")]
public class RoutedPageType
{
}

[PageType(Id = "Archive", Title = "Archive Page Type", UseBlocks = false, IsArchive = true)]
[PageTypeArchiveItem(typeof(SimplePostType))]
public class ArchivePageType
Expand Down Expand Up @@ -64,6 +70,12 @@ public class SimplePostType
public Extend.Fields.TextField Body { get; set; }
}

[PostType(Id = "Routed", Title = "Routed Post Type")]
[PostTypeRoute(Title = "Default", Route = "postroute")]
public class RoutedPostType
{
}

[PostType(Id = "Complex", Title = "Complex Post Type")]
[PostTypeRoute(Title = "Default", Route = "/complex")]
[PostTypeEditor(Title = "Custom Editor", Component = "will be replaced", Icon = "will be replaced")]
Expand Down Expand Up @@ -159,6 +171,24 @@ public async Task AddSimplePageType()
}
}

[Fact]
public async Task AddForwardSlashToPageRoutes()
{
using (var api = CreateApi())
{
var builder = new PageTypeBuilder(api)
.AddType(typeof(RoutedPageType));
builder.Build();

var type = await api.PageTypes.GetByIdAsync("Routed");

Assert.NotNull(type);

Assert.NotEmpty(type.Routes);
Assert.StartsWith("/", type.Routes[0]);
}
}

[Fact]
public async Task AddArchivePageType()
{
Expand Down Expand Up @@ -241,6 +271,24 @@ public async Task AddSimplePostType()
}
}

[Fact]
public async Task AddForwardSlashToPostRoutes()
{
using (var api = CreateApi())
{
var builder = new PostTypeBuilder(api)
.AddType(typeof(RoutedPostType));
builder.Build();

var type = await api.PostTypes.GetByIdAsync("Routed");

Assert.NotNull(type);

Assert.NotEmpty(type.Routes);
Assert.StartsWith("/", type.Routes[0]);
}
}

[Fact]
public async Task AddComplexPostType()
{
Expand Down

0 comments on commit 9964c49

Please sign in to comment.