Skip to content

Commit

Permalink
feat: Added support for Areas of practice.
Browse files Browse the repository at this point in the history
Merge pull request #264 from DFE-Digital/Feature/AreaOfPractic
  • Loading branch information
nnagepat authored Sep 15, 2023
2 parents c00d2bb + ecacb68 commit e1826fa
Show file tree
Hide file tree
Showing 13 changed files with 474 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace Childrens_Social_Care_CPD_Tests.Contentful;
public class EntityResolverTests
{
[Test]
[TestCase("areaOfPractice", typeof(AreaOfPractice))]
[TestCase("areaOfPracticeList", typeof(AreaOfPracticeList))]
[TestCase("applicationFeature", typeof(ApplicationFeature))]
[TestCase("applicationFeatures", typeof(ApplicationFeatures))]
[TestCase("columnLayout", typeof(ColumnLayout))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public partial class PartialsFactoryTests
{
public static object[] Successful_Resolves =
{
new object[] { new AreaOfPractice(), "_AreaOfPractice" },
new object[] { new AreaOfPracticeList(), "_AreaOfPracticeList" },
new object[] { new ColumnLayout(), "_ColumnLayout" },
new object[] { new Content(), "_Content" },
new object[] { new ContentLink(), "_ContentLink" },
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
using Childrens_Social_Care_CPD.Contentful.Models;
using Childrens_Social_Care_CPD.Contentful.Renderers;
using Contentful.Core.Models;
using FluentAssertions;
using Microsoft.AspNetCore.Html;
using Microsoft.Extensions.WebEncoders.Testing;
using NSubstitute;
using NUnit.Framework;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace Childrens_Social_Care_CPD_Tests.Contentful.Renderers;

public class InlineAreaOfPracticeListRendererTests
{
private IRenderer<ContentLink> _contentLinkRenderer;
private InlineAreaOfPracticeListRenderer _sut;

[SetUp]
public void Setup()
{
_contentLinkRenderer = Substitute.For<IRenderer<ContentLink>>();
_sut = new InlineAreaOfPracticeListRenderer(_contentLinkRenderer);
}

[Test]
public void AreaOfPractice_Renders_When_No_AreasOfPractice()
{
// arrange
var stringWriter = new StringWriter();
var areaOfPracticeList = new AreaOfPracticeList()
{
Title = "A Title",
Areas = new List<Content>()
};
_contentLinkRenderer.Render(Arg.Any<ContentLink>()).Returns(new HtmlString("AAA"));

// act
var htmlContent = _sut.Render(areaOfPracticeList);
htmlContent.WriteTo(stringWriter, new HtmlTestEncoder());
var actual = stringWriter.ToString();

// assert
actual.Should().Be("<span>HtmlEncode[[No Areas Of practice Available]]</span>");
}

[Test]
public void AreaOfPracticeList_Renders_Empty_When_AreaOfPractice_Is_Null()
{
// arrange
var stringWriter = new StringWriter();
var areaOfPracticeList = new AreaOfPracticeList()
{
Title = "A Title",
Areas = new List<Content>
{
new Content
{
Id = "id",
Title = "title",
Items = new List<IContent>()
}
}
};

_contentLinkRenderer.Render(Arg.Any<ContentLink>()).Returns(new HtmlString("AAA"));

// act
var htmlContent = _sut.Render(areaOfPracticeList);
htmlContent.WriteTo(stringWriter, new HtmlTestEncoder());
var actual = stringWriter.ToString();

// assert
actual.Should().Be(string.Empty);
}

[Test]
public void AreaOfPracticeList_Renders_Title()
{
// arrange
var stringWriter = new StringWriter();
var areaOfPracticeList = new AreaOfPracticeList
{
Title = "A Title",
Areas = new List<Content>
{
new Content
{
Id = "id",
Items = new List<IContent>()
{
new AreaOfPractice()
{
Title = "title"
}
}
}
}
};

_contentLinkRenderer.Render(Arg.Any<ContentLink>()).Returns(new HtmlString("AAA"));
var expected = $"^{Regex.Escape("<div class=\"HtmlEncode[[govuk-heading-s govuk-!-margin-bottom-1]]\"><h2>AAA</h2></div>")}.*";

// act
var htmlContent = _sut.Render(areaOfPracticeList);
htmlContent.WriteTo(stringWriter, new HtmlTestEncoder());
var actual = stringWriter.ToString();

// assert
actual.Should().MatchRegex(expected);
}

[Test]
public void AreaOfPracticeList_Renders_Summary()
{
// arrange
var stringWriter = new StringWriter();
var areaOfPractice = new AreaOfPractice()
{
Title = "title",
AreaOfPracticeListSummary = "summary"
};
var areaOfPracticeList = new AreaOfPracticeList()
{
Title = "A Title",
Areas = new List<Content>
{
new Content
{
Id = "id",
Items = new List<IContent>()
{
areaOfPractice
}
}
}
};

_contentLinkRenderer.Render(Arg.Any<ContentLink>()).Returns(new HtmlString("AAA"));
var expected = $".*?{Regex.Escape("<p class=\"HtmlEncode[[govuk-body]]\">HtmlEncode[[summary]]</p>")}.*";

// act
var htmlContent = _sut.Render(areaOfPracticeList);
htmlContent.WriteTo(stringWriter, new HtmlTestEncoder());
var actual = stringWriter.ToString();

// assert
actual.Should().MatchRegex(expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public class ParagraphRendererTests
private IRenderer<RoleList> _roleListRenderer;
private IRenderer<Hyperlink> _hyperlinkRenderer;
private IRenderer<ContentLink> _contentLinkRenderer;

private IRenderer<AreaOfPracticeList> _areaOfPracticeListRenderer;

private ParagraphRenderer _sut;

[SetUp]
Expand All @@ -28,8 +29,8 @@ public void Setup()
_roleListRenderer = Substitute.For<IRenderer<RoleList>>();
_hyperlinkRenderer = Substitute.For<IRenderer<Hyperlink>>();
_contentLinkRenderer = Substitute.For<IRenderer<ContentLink>>();

_sut = new ParagraphRenderer(_textRenderer, _roleListRenderer, _hyperlinkRenderer, _contentLinkRenderer);
_areaOfPracticeListRenderer = Substitute.For<IRenderer<AreaOfPracticeList>>();
_sut = new ParagraphRenderer(_textRenderer, _roleListRenderer, _hyperlinkRenderer, _contentLinkRenderer, _areaOfPracticeListRenderer);
}

[Test]
Expand Down Expand Up @@ -82,6 +83,7 @@ public void Paragraph_Renders_With_RoleList()
_roleListRenderer.Render(Arg.Any<RoleList>()).Returns(new HtmlString("BBB"));
_hyperlinkRenderer.Render(Arg.Any<Hyperlink>()).Returns(new HtmlString("CCC"));
_contentLinkRenderer.Render(Arg.Any<ContentLink>()).Returns(new HtmlString("DDD"));
_areaOfPracticeListRenderer.Render(Arg.Any<AreaOfPracticeList>()).Returns(new HtmlString("EEE"));

// act
var htmlContent = _sut.Render(paragraph);
Expand Down Expand Up @@ -109,6 +111,7 @@ public void Paragraph_Renders_With_Hyperlink()
_roleListRenderer.Render(Arg.Any<RoleList>()).Returns(new HtmlString("BBB"));
_hyperlinkRenderer.Render(Arg.Any<Hyperlink>()).Returns(new HtmlString("CCC"));
_contentLinkRenderer.Render(Arg.Any<ContentLink>()).Returns(new HtmlString("DDD"));
_areaOfPracticeListRenderer.Render(Arg.Any<AreaOfPracticeList>()).Returns(new HtmlString("EEE"));

// act
var htmlContent = _sut.Render(paragraph);
Expand Down Expand Up @@ -142,6 +145,7 @@ public void Paragraph_Renders_With_ContentLink()
_roleListRenderer.Render(Arg.Any<RoleList>()).Returns(new HtmlString("BBB"));
_hyperlinkRenderer.Render(Arg.Any<Hyperlink>()).Returns(new HtmlString("CCC"));
_contentLinkRenderer.Render(Arg.Any<ContentLink>()).Returns(new HtmlString("DDD"));
_areaOfPracticeListRenderer.Render(Arg.Any<AreaOfPracticeList>()).Returns(new HtmlString("EEE"));

// act
var htmlContent = _sut.Render(paragraph);
Expand All @@ -151,4 +155,38 @@ public void Paragraph_Renders_With_ContentLink()
// assert
actual.Should().Be($"<p class=\"HtmlEncode[[govuk-body-m]]\">DDD</p>");
}

[Test]
public void Paragraph_Renders_With_AreaOfPracticeList()
{
// arrange
var stringWriter = new StringWriter();
var paragraph = new Paragraph()
{
Content = new List<IContent>
{
new EntryStructure
{
Data = new EntryStructureData
{
Target = new AreaOfPracticeList()
}
}
}
};

_textRenderer.Render(Arg.Any<Text>()).Returns(new HtmlString("AAA"));
_roleListRenderer.Render(Arg.Any<RoleList>()).Returns(new HtmlString("BBB"));
_hyperlinkRenderer.Render(Arg.Any<Hyperlink>()).Returns(new HtmlString("CCC"));
_contentLinkRenderer.Render(Arg.Any<ContentLink>()).Returns(new HtmlString("DDD"));
_areaOfPracticeListRenderer.Render(Arg.Any<AreaOfPracticeList>()).Returns(new HtmlString("EEE"));

// act
var htmlContent = _sut.Render(paragraph);
htmlContent.WriteTo(stringWriter, new HtmlTestEncoder());
var actual = stringWriter.ToString();

// assert
actual.Should().Be($"<p class=\"HtmlEncode[[govuk-body-m]]\">EEE</p>");
}
}
2 changes: 2 additions & 0 deletions Childrens-Social-Care-CPD/Contentful/EntityResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public Type Resolve(string contentTypeId)
{
return contentTypeId switch
{
"areaOfPractice" => typeof(AreaOfPractice),
"areaOfPracticeList" => typeof(AreaOfPracticeList),
"applicationFeature" => typeof(ApplicationFeature),
"applicationFeatures" => typeof(ApplicationFeatures),
"columnLayout" => typeof(ColumnLayout),
Expand Down
16 changes: 16 additions & 0 deletions Childrens-Social-Care-CPD/Contentful/Models/AreaOfPractice.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Contentful.Core.Models;

namespace Childrens_Social_Care_CPD.Contentful.Models;

public class AreaOfPractice : IContent
{
public string Title { get; set; }
public string Summary { get; set; }
public string AreaOfPracticeListSummary { get; set; }
public string OtherNames { get; set; }
public Document WhatYoullDo { get; set; }
public Document SkillsAndKnowledge { get; set; }
public Document WhoYouWillWorkWith { get; set; }
public Document HowYouWillWork { get; set; }
public Document CurrentOpportunities { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Contentful.Core.Models;

namespace Childrens_Social_Care_CPD.Contentful.Models;

public class AreaOfPracticeList : IContent
{
public string Title { get; set; }
public List<Content> Areas { get; set; }
}
3 changes: 3 additions & 0 deletions Childrens-Social-Care-CPD/Contentful/PartialsFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public static string GetPartialFor(IContent item)
{
switch (item)
{
case (AreaOfPractice): return "_AreaOfPractice";
case (AreaOfPracticeList): return "_AreaOfPracticeList";
case (ColumnLayout): return "_ColumnLayout";
case (Content): return "_Content";
case (ContentLink): return "_ContentLink";
Expand All @@ -27,6 +29,7 @@ public static string GetPartialFor(IContent item)
case (RoleList): return "_RoleList";
case (SideMenu): return "_SideMenu";
case (TextBlock): return "_TextBlock";

default: return "_UnknownContentWarning";
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using Childrens_Social_Care_CPD.Contentful.Models;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Childrens_Social_Care_CPD.Contentful.Renderers;

internal class InlineAreaOfPracticeListRenderer : IRenderer<AreaOfPracticeList>
{
private readonly IRenderer<ContentLink> _contentLinkRenderer;

public InlineAreaOfPracticeListRenderer(IRenderer<ContentLink> contentLinkRenderer)
{
_contentLinkRenderer = contentLinkRenderer;
}

public IHtmlContent Render(AreaOfPracticeList item)
{
if (item.Areas.Count == 0)
{
return NoAreasOfpractice();
}

var htmlContentBuilder = new HtmlContentBuilder();

foreach (var contentItem in item.Areas)
{
var areaOfpractice = contentItem.Items.OfType<AreaOfPractice>().FirstOrDefault();
if (areaOfpractice == null) continue;

htmlContentBuilder.AppendHtml(AreaOfPracticeTitle(contentItem.Id, areaOfpractice));
htmlContentBuilder.AppendHtml(AreaOfPracticeSummary(areaOfpractice));
}

return htmlContentBuilder;
}

private static IHtmlContent NoAreasOfpractice()
{
var span = new TagBuilder("span");
span.InnerHtml.Append("No Areas Of practice Available");
return span;
}

private IHtmlContent AreaOfPracticeTitle(string id, AreaOfPractice areaOfPractice)
{
var div = new TagBuilder("div");
div.AddCssClass("govuk-heading-s govuk-!-margin-bottom-1");
var heading2 = new TagBuilder("h2");

var contentLink = new ContentLink()
{
Name = areaOfPractice.Title,
Uri = id
};

heading2.InnerHtml.AppendHtml(_contentLinkRenderer.Render(contentLink));

div.InnerHtml.AppendHtml(heading2);
return div;
}

private static IHtmlContent AreaOfPracticeSummary(AreaOfPractice areaOfPractice)
{
var htmlContentBuilder = new HtmlContentBuilder();
var p = new TagBuilder("p");
p.AddCssClass("govuk-body");
p.InnerHtml.Append(areaOfPractice.AreaOfPracticeListSummary);
htmlContentBuilder.AppendHtml(p);
return htmlContentBuilder;
}
}
Loading

0 comments on commit e1826fa

Please sign in to comment.