Fix post title generation

This commit is contained in:
Robert Marshall 2020-01-03 18:58:27 +00:00
parent c40a7fab26
commit 92e479457c
2 changed files with 13 additions and 1 deletions

View file

@ -13,6 +13,17 @@ namespace Website.Tests.Models {
post.Url.Should().Be("new-title"); post.Url.Should().Be("new-title");
} }
[Theory]
[InlineData("new:title", "new-title")]
[InlineData("new: title", "new-title")]
[InlineData("new$title", "new-title")]
[InlineData("new TITle", "new-title")]
public void UpdateTitle_WithUnsafeCharsInTitle_RegeneratesSafeUrl(string title, string url) {
var post = new BlogPost();
post.UpdateTitle(title);
post.Url.Should().Be(url);
}
[Fact] [Fact]
public void UpdateDraft_WithContent_UpdatesDraftProperty() { public void UpdateDraft_WithContent_UpdatesDraftProperty() {
var post = new BlogPost(); var post = new BlogPost();

View file

@ -1,4 +1,5 @@
using System; using System;
using System.Text.RegularExpressions;
using Website.Data.States; using Website.Data.States;
namespace Website.Models namespace Website.Models
@ -29,7 +30,7 @@ namespace Website.Models
public int UserId { get; private set; } public int UserId { get; private set; }
private void GenerateUrl() { private void GenerateUrl() {
Url = Title.Replace(' ', '-'); Url = Regex.Replace(Title, @"[^a-zA-Z0-9\.]+", "-").ToLower();
} }
public void UpdateTitle(string title) { public void UpdateTitle(string title) {