Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EES-5085 Fix incorrect slugs for updated publication titles with dashes and spaces #4793

Merged
merged 2 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
using Xunit;
using static GovUk.Education.ExploreEducationStatistics.Content.Model.NamingUtils;

namespace GovUk.Education.ExploreEducationStatistics.Content.Model.Tests
namespace GovUk.Education.ExploreEducationStatistics.Content.Model.Tests;

public abstract class NamingUtilTests
{
public class NamingUtilTests
public class SlugFromTitleTests
{

[Fact]
public void ReleaseSlugFromTitle_CalendarYear()
public void CalendarYearTitle()
{
var slug = SlugFromTitle("calendar year 2019");
Assert.Equal("calendar-year-2019", slug);
Assert.Equal("calendar-year-2019", SlugFromTitle("calendar year 2019"));
}

[Fact]
public void ReleaseSlugFromTitle_NonCalendarYear()
public void NonCalendarYearTitle()
{
var slug = SlugFromTitle("tax year 2019/20");
Assert.Equal("tax-year-2019-20", slug);
Assert.Equal("tax-year-2019-20", SlugFromTitle("tax year 2019/20"));
}


[Fact]
public void GenerateSlugFromTitle()

[Theory]
[InlineData("title", "title")]
[InlineData("TITLE", "title")]
[InlineData("A sentence with spaces", "a-sentence-with-spaces")]
[InlineData("A - sentence - with - - dashes - and -- spaces", "a-sentence-with-dashes-and-spaces")]
[InlineData("A sentence with !@£('\\) non alpha numeric characters", "a-sentence-with-non-alpha-numeric-characters")]
[InlineData("A sentence with non alpha numeric characters at the end !@£('\\)", "a-sentence-with-non-alpha-numeric-characters-at-the-end")]
[InlineData("a sentence with big spaces ", "a-sentence-with-big-spaces")]
[InlineData("a sentence with numbers 1 2 3 and 4", "a-sentence-with-numbers-1-2-3-and-4")]
public void EdgeCaseTitles(string title, string expectedSlug)
{
Assert.Equal("title", SlugFromTitle("title"));
Assert.Equal("title", SlugFromTitle("TITLE"));
Assert.Equal("a-sentence-with-spaces",SlugFromTitle("A sentence with spaces"));
Assert.Equal("a-sentence-with-non-alpha-numeric-characters",SlugFromTitle("A sentence with !@£('\\) non alpha numeric characters"));
Assert.Equal("a-sentence-with-non-alpha-numeric-characters-at-the-end", SlugFromTitle("A sentence with non alpha numeric characters at the end !@£('\\)"));
Assert.Equal("a-sentence-with-big-spaces", SlugFromTitle("a sentence with big spaces "));
Assert.Equal("a-sentence-with-numbers-1-2-3-and-4", SlugFromTitle("a sentence with numbers 1 2 3 and 4"));
Assert.Equal(expectedSlug, SlugFromTitle(title));
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ describe('slugFromTitle', () => {
);
});

test('converts a sentence with dashes and spaces', () => {
expect(
slugFromTitle('A - sentence - with - - dashes - and -- spaces'),
).toEqual('a-sentence-with-dashes-and-spaces');
});

test('converts a sentence with non alphanumeric characters', () => {
expect(
slugFromTitle("A sentence with !@£('\\) non alpha numeric characters"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,5 @@
* Duplicates NamingUtils#SlugFromTitle in the backend
*/
export default function slugFromTitle(title: string) {
return title
.replace(/[^\w-]+/g, ' ')
.trim()
.toLowerCase()
.replace(/\s+/g, '-');
return title.replace(/\W+/g, ' ').trim().toLowerCase().replace(/\s+/g, '-');
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ Force Tags Admin Local Dev AltersData


*** Variables ***
${PUBLICATION_NAME}= UI tests-publish publication update %{RUN_IDENTIFIER}
${PUBLIC_METHODOLOGY_URL_ENDING}= /methodology/ui-tests-publish-publication-update-%{RUN_IDENTIFIER}
${PUBLICATION_NAME}= UI tests - publish methodology publication update %{RUN_IDENTIFIER}
${PUBLIC_METHODOLOGY_URL_ENDING}= /methodology/ui-tests-publish-methodology-publication-update-%{RUN_IDENTIFIER}
${PUBLICATION_NAME_UPDATED}= ${PUBLICATION_NAME} updated
${PUBLIC_PUBLICATION_URL_ENDING}= /find-statistics/ui-tests-publish-publication-update-%{RUN_IDENTIFIER}
${PUBLIC_PUBLICATION_URL_ENDING}= /find-statistics/ui-tests-publish-methodology-publication-update-%{RUN_IDENTIFIER}
${EXPECTED_PUBLIC_PUBLICATION_URL_ENDING}= %{PUBLIC_URL}${PUBLIC_PUBLICATION_URL_ENDING}
${RELEASE_NAME}= Academic year Q1
${ACADEMIC_YEAR}= /2046-47
Expand Down