Skip to content

Commit

Permalink
Fix title empty if AddSegment is not called (#14295)
Browse files Browse the repository at this point in the history
* fix: title empty if AddSegment is not called

* style: renamed extension file and class

* style: sort usign, remove extra line

* test: added tests for PageTitleBuilder

* test: removed empty lines, String.Empty

* style: added const in test, line and string.Empty

* fix: use different check for empty title

fix: removed HtmlContentExtension, avoid unwant use of it
fix: changed tests code to use a private version of extension removed

* fix: moved HtmlContentBuilder in a best position

fix: init HtmlContentBuilder  with a size

* fix: comment, using var

* Update test/OrchardCore.Tests/DisplayManagement/Title/PageTitleBuilderTests.cs

* Update test/OrchardCore.Tests/DisplayManagement/Title/PageTitleBuilderTests.cs

* Update test/OrchardCore.Tests/DisplayManagement/Title/PageTitleBuilderTests.cs

---------

Co-authored-by: Hisham Bin Ateya <[email protected]>
  • Loading branch information
PiemP and hishamco committed Feb 1, 2024
1 parent d22c799 commit 0fdf04b
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Html;
using OrchardCore.DisplayManagement.Zones;
Expand All @@ -9,7 +10,7 @@ public class PageTitleBuilder : IPageTitleBuilder
private readonly static HtmlString _defaultTitleSeparator = new(" - ");

private readonly List<PositionalTitlePart> _titleParts;
private IHtmlContent _title;
private HtmlContentBuilder _title;
private IHtmlContent _fixedTitle;

public PageTitleBuilder()
Expand Down Expand Up @@ -48,7 +49,7 @@ public IHtmlContent GenerateTitle(IHtmlContent separator)
return _fixedTitle;
}

if (_title != null)
if (_title is { Count: > 0 })
{
return _title;
}
Expand All @@ -57,13 +58,14 @@ public IHtmlContent GenerateTitle(IHtmlContent separator)

_titleParts.Sort(FlatPositionComparer.Instance);

var htmlContentBuilder = new HtmlContentBuilder();

if (_titleParts.Count == 0)
{
return HtmlString.Empty;
}

// _titleParts.Count * 2 because we add a separator for each entry
var htmlContentBuilder = new HtmlContentBuilder(_titleParts.Count * 2);

for (var i = 0; i < _titleParts.Count; i++)
{
htmlContentBuilder.AppendHtml(_titleParts[i].Value);
Expand All @@ -82,6 +84,7 @@ public IHtmlContent GenerateTitle(IHtmlContent separator)
public void Clear()
{
_fixedTitle = null;
_title = null;
_titleParts.Clear();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
using Cysharp.Text;

namespace OrchardCore.DisplayManagement.Title
{
public class PageTitleBuilderTests
{
private const string Separator = " - ";
private const string SimpleTitle = "I'm a title";
private const string FirstPartTitle = "first part";
private const string SecondPartTitle = "second part";

private readonly IServiceProvider _serviceProvider;

public PageTitleBuilderTests()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddScoped<IPageTitleBuilder, PageTitleBuilder>();

_serviceProvider = serviceCollection.BuildServiceProvider();
}

[Fact]
public void GenerateTitleEmpty()
{
// Arrange
var pageTitleBuilder = _serviceProvider.GetService<IPageTitleBuilder>();

// Act & Assert
Assert.Equal(string.Empty, ToString(pageTitleBuilder.GenerateTitle()));
}

[Fact]
public void FixedTitleSet()
{
// Arrange
var pageTitleBuilder = _serviceProvider.GetService<IPageTitleBuilder>();
pageTitleBuilder.SetFixedTitle(new HtmlString(SimpleTitle));

// Act & Assert
Assert.Equal(SimpleTitle, ToString(pageTitleBuilder.GenerateTitle()));
}

[Fact]
public void FixedTitleClearAndCheck()
{
// Arrange
var pageTitleBuilder = _serviceProvider.GetService<IPageTitleBuilder>();
pageTitleBuilder.SetFixedTitle(new HtmlString(SimpleTitle));
pageTitleBuilder.Clear();

// Act & Assert
Assert.Equal(string.Empty, ToString(pageTitleBuilder.GenerateTitle()));
}

[Fact]
public void FixedTitleAddSegment()
{
// Arrange
var pageTitleBuilder = _serviceProvider.GetService<IPageTitleBuilder>();
pageTitleBuilder.SetFixedTitle(new HtmlString(SimpleTitle));

// Act & Assert
pageTitleBuilder.AddSegment(new HtmlString("you don't see me"));
Assert.Equal(SimpleTitle, ToString(pageTitleBuilder.GenerateTitle()));
}

[Fact]
public void TitleAddSegment()
{
// Arrange
var pageTitleBuilder = _serviceProvider.GetService<IPageTitleBuilder>();
pageTitleBuilder.Clear();

// Act & Assert
pageTitleBuilder.AddSegment(new HtmlString(SimpleTitle), "after");
Assert.Equal(SimpleTitle, ToString(pageTitleBuilder.GenerateTitle()));
}

[Fact]
public void TitleMultiAddSegment()
{
// Arrange
var pageTitleBuilder = _serviceProvider.GetService<IPageTitleBuilder>();
pageTitleBuilder.Clear();

// Act & Assert
pageTitleBuilder.AddSegment(new HtmlString(FirstPartTitle), "after");
Assert.Equal(FirstPartTitle, ToString(pageTitleBuilder.GenerateTitle(new HtmlString(Separator))));
pageTitleBuilder.AddSegment(new HtmlString(SecondPartTitle), "after");
Assert.Equal($"{FirstPartTitle}{Separator}{SecondPartTitle}", ToString(pageTitleBuilder.GenerateTitle(new HtmlString(Separator))));
}

[Fact]
public void TitleAddAndClear()
{
// Arrange
var pageTitleBuilder = _serviceProvider.GetService<IPageTitleBuilder>();
pageTitleBuilder.Clear();

// Act & Assert
pageTitleBuilder.AddSegment(new HtmlString(FirstPartTitle), "after");
Assert.Equal(FirstPartTitle, ToString(pageTitleBuilder.GenerateTitle(new HtmlString(Separator))));
pageTitleBuilder.Clear();
Assert.Equal(string.Empty, ToString(pageTitleBuilder.GenerateTitle(new HtmlString(Separator))));
}

[Fact]
public void TitleMultiGenerateTitle()
{
// Arrange
var pageTitleBuilder = _serviceProvider.GetService<IPageTitleBuilder>();
pageTitleBuilder.Clear();

// Act
pageTitleBuilder.AddSegment(new HtmlString(FirstPartTitle), "after");

// Assert
Assert.Equal(FirstPartTitle, ToString(pageTitleBuilder.GenerateTitle(new HtmlString(Separator))));
Assert.Equal(FirstPartTitle, ToString(pageTitleBuilder.GenerateTitle(new HtmlString(Separator))));
}

[Fact]
public void TitleAddSegments()
{
// Arrange
var pageTitleBuilder = (PageTitleBuilder)_serviceProvider.GetService<IPageTitleBuilder>();
pageTitleBuilder.Clear();

var elements = new IHtmlContent[]
{
new HtmlString(FirstPartTitle),
new HtmlString(SecondPartTitle)
};

pageTitleBuilder.AddSegments(elements, "after");

// Act & Assert
Assert.Equal($"{FirstPartTitle}{Separator}{SecondPartTitle}", ToString(pageTitleBuilder.GenerateTitle(new HtmlString(Separator))));
}

private static string ToString(IHtmlContent content)
{
using var writer = new ZStringWriter();
content.WriteTo(writer, HtmlEncoder.Default);

return writer.ToString();
}
}
}

0 comments on commit 0fdf04b

Please sign in to comment.