Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #359 from DFE-Digital/update-file-name-format
Browse files Browse the repository at this point in the history
Update the template file name
  • Loading branch information
jkempster34 authored May 12, 2022
2 parents 951b062 + 49aedef commit cc14622
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 18 deletions.
26 changes: 20 additions & 6 deletions Frontend.Tests/PagesTests/TaskList/HtbDocument/DownloadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,45 +21,59 @@ protected DownloadTests()
};

_createHtbDocument.Setup(s => s.Execute(ProjectUrn0001)).ReturnsAsync(new CreateProjectTemplateResponse
{Document = new byte[] {0, 1}});
{
Document = new byte[] { 0, 1 }
});

FoundInformationForProject.Project.TransferringAcademies[0].IncomingTrustName = "Incoming Trust";
FoundInformationForProject.Project.OutgoingTrustName = "Outgoing Trust";
FoundInformationForProject.Project.Reference = "SW-MAT-10000001";
}

public class GetTests : DownloadTests
{
[Fact]
public async void GivenId_GetsProjectInformation()
{
FoundInformationForProject.Project.TransferringAcademies[0].IncomingTrustName = "Incoming Trust";
await _subject.OnGetAsync();

GetInformationForProject.Verify(s => s.Execute(ProjectUrn0001), Times.Once);
}

[Fact]
public async void GivenId_SetsFileName()
{
await _subject.OnGetAsync();

Assert.Equal("SW-MAT-10000001_Outgoing-Trust_Incoming-Trust_project-template", _subject.FileName);
}
}

public class GetDownloadTests : DownloadTests
{
[Fact]
public async void GivenId_GeneratesAnHtbDocumentForTheProject()
{
FoundInformationForProject.Project.TransferringAcademies[0].IncomingTrustName = "Incoming Trust";
await _subject.OnGetGenerateDocumentAsync();

_createHtbDocument.Verify(s => s.Execute(ProjectUrn0001), Times.Once);
}

[Fact]
public async void GivenId_ReturnsAFileWithTheGeneratedDocument()
{
var fileContents = new byte[] {1, 2, 3, 4};
var fileContents = new byte[] { 1, 2, 3, 4 };
var createDocumentResponse = new CreateProjectTemplateResponse
{
Document = fileContents
};
_createHtbDocument.Setup(s => s.Execute(ProjectUrn0001)).ReturnsAsync(createDocumentResponse);
FoundInformationForProject.Project.TransferringAcademies[0].IncomingTrustName = "Incoming Trust";

var response = await _subject.OnGetGenerateDocumentAsync();
var fileResponse = Assert.IsType<FileContentResult>(response);

var fileResponse = Assert.IsType<FileContentResult>(response);
Assert.Equal(fileContents, fileResponse.FileContents);
Assert.Equal("SW-MAT-10000001_Outgoing-Trust_Incoming-Trust_project-template.docx", fileResponse.FileDownloadName);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Frontend/Pages/TaskList/HtbDocument/Download.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<div class="dfe-c-attachment__details">
<h2 class="dfe-c-attachment__title">
<a class="govuk-link govuk-link--no-visited-state" target="_blank" rel="noopener" download asp-page-handler="GenerateDocument" asp-route-urn="@Model.Urn" data-test="download-htb">
@Model.IncomingTrustName, @Model.ProjectReference project template
@Model.FileName
</a>
</h2>
<p class="dfe-c-attachment__metadata">
Expand Down
41 changes: 34 additions & 7 deletions Frontend/Pages/TaskList/HtbDocument/Download.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Linq;
using System.Threading.Tasks;
using Data.Models;
using Frontend.Models;
using Frontend.Services.Interfaces;
using Helpers;
Expand All @@ -11,28 +12,54 @@ public class Download : CommonPageModel
{
private readonly ICreateProjectTemplate _createProjectTemplate;
private readonly IGetInformationForProject _getInformationForProject;

public Download(ICreateProjectTemplate createProjectTemplate,
IGetInformationForProject getInformationForProject)
{
_createProjectTemplate = createProjectTemplate;
_getInformationForProject = getInformationForProject;
}


public string FileName { get; private set; }

public async Task<IActionResult> OnGetAsync()
{
var projectInformation = await _getInformationForProject.Execute(Urn);
ProjectReference = projectInformation.Project.Reference;
IncomingTrustName = projectInformation.Project.IncomingTrustName.ToTitleCase();
var project = await GetProject();
ProjectReference = project.Reference;
FileName = GenerateFormattedFileName(project);

return Page();
}

public async Task<IActionResult> OnGetGenerateDocumentAsync()
{
var projectInformation = await _getInformationForProject.Execute(Urn);
var project = await GetProject();
FileName = GenerateFormattedFileName(project);

var document = await _createProjectTemplate.Execute(Urn);

return File(document.Document.ToArray(),
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
$"ProjectTemplateFor{projectInformation.Project.IncomingTrustName.ToTitleCase().Replace(" ","")}{projectInformation.Project.Reference}.docx");
$"{FileName}.docx");
}

private async Task<Project> GetProject()
{
var projectInformation = await _getInformationForProject.Execute(Urn);
return projectInformation.Project;
}

private static string GenerateFormattedFileName(Project project)
{
var formattedOutgoingTrustName = FormatTrustName(project.OutgoingTrustName);
var formattedIncomingTrustName = FormatTrustName(project.IncomingTrustName);

return $"{project.Reference}_{formattedOutgoingTrustName}_{formattedIncomingTrustName}_project-template";
}

private static string FormatTrustName(string trustName)
{
return trustName.ToTitleCase().ToHyphenated().RemoveNonAlphanumericOrWhiteSpace();
}
}
}
63 changes: 63 additions & 0 deletions Helpers.Tests/StringHelperTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Text;
using Xunit;

namespace Helpers.Tests
{
public class StringHelperTests
{
public class ToHtmlName
{
[Fact]
public void ReplacesCharacters()
{
const string text = "some]text[for.testing";

var result = text.ToHtmlName();

Assert.Equal("some_text_for_testing", result);
}
}

public class ToTitleCase
{
[Theory]
[InlineData("A TITLE", "A Title")]
[InlineData("a title", "A Title")]
public void FormatsAsTitleCase(string input, string expectedOutput)
{
var result = input.ToTitleCase();

Assert.Equal(expectedOutput, result);
}
}

public class ToHyphenated
{
[Theory]
[InlineData("some text", "some-text")]
[InlineData("some text", "some-text")]
[InlineData("some\ttext", "some-text")]
public void ReplacesWhiteSpaceWithHyphens(string input, string expectedOutput)
{
var result = input.ToHyphenated();

Assert.Equal(expectedOutput, result);
}
}

public class RemoveNonAlphanumericOrWhiteSpace
{
[Fact]
public void RemovesCharacters()
{
const string text = "some text-with-punctuation_and'numbers99][()";

var result = text.RemoveNonAlphanumericOrWhiteSpace();

Assert.Equal("some text-with-punctuation_andnumbers99", result);
}
}
}
}
21 changes: 17 additions & 4 deletions Helpers/StringHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Globalization;
using System.Text.RegularExpressions;

namespace Helpers
{
Expand All @@ -7,14 +8,26 @@ public static class StringHelper
public static string ToHtmlName(this string propertyName)
{
return propertyName.Replace('.', '_')
.Replace('[','_')
.Replace(']','_');
}
.Replace('[', '_')
.Replace(']', '_');
}

public static string ToTitleCase(this string str)
{
var textInfo = CultureInfo.CurrentCulture.TextInfo;
return textInfo.ToTitleCase(str.ToLower());
}

public static string ToHyphenated(this string str)
{
var whitespaceRegex = new Regex(@"\s+");
return whitespaceRegex.Replace(str, "-");
}

public static string RemoveNonAlphanumericOrWhiteSpace(this string str)
{
var notAlphanumericWhiteSpaceOrHyphen = new Regex(@"[^\w\s-]");
return notAlphanumericWhiteSpaceOrHyphen.Replace(str, string.Empty);
}
}
}

0 comments on commit cc14622

Please sign in to comment.