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

fix: title empty if AddSegment is not called #14295

Merged
merged 14 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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,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,141 @@
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";


PiemP marked this conversation as resolved.
Show resolved Hide resolved
private readonly IServiceProvider _serviceProvider;

public PageTitleBuilderTests()
{
IServiceCollection serviceCollection = new ServiceCollection();
PiemP marked this conversation as resolved.
Show resolved Hide resolved

serviceCollection.AddScoped<IPageTitleBuilder, PageTitleBuilder>();

_serviceProvider = serviceCollection.BuildServiceProvider();
}

[Fact]
public void GenerateTitleEmpty()
{
var pageTitleBuilder = _serviceProvider.GetService<IPageTitleBuilder>();
PiemP marked this conversation as resolved.
Show resolved Hide resolved

PiemP marked this conversation as resolved.
Show resolved Hide resolved
Assert.Equal(string.Empty, ToString(pageTitleBuilder.GenerateTitle()));
PiemP marked this conversation as resolved.
Show resolved Hide resolved
}

[Fact]
public void FixedTitleSet()
{
var pageTitleBuilder = _serviceProvider.GetService<IPageTitleBuilder>();
PiemP marked this conversation as resolved.
Show resolved Hide resolved
pageTitleBuilder.SetFixedTitle(new HtmlString(SimpleTitle));

Assert.Equal(SimpleTitle, ToString(pageTitleBuilder.GenerateTitle()));
PiemP marked this conversation as resolved.
Show resolved Hide resolved
}

[Fact]
public void FixedTitleClearAndCheck()
{
var pageTitleBuilder = _serviceProvider.GetService<IPageTitleBuilder>();
PiemP marked this conversation as resolved.
Show resolved Hide resolved
pageTitleBuilder.SetFixedTitle(new HtmlString(SimpleTitle));
pageTitleBuilder.Clear();

Assert.Equal(string.Empty, ToString(pageTitleBuilder.GenerateTitle()));
PiemP marked this conversation as resolved.
Show resolved Hide resolved
}

[Fact]
public void FixedTitleAddSegment()
{
var pageTitleBuilder = _serviceProvider.GetService<IPageTitleBuilder>();
PiemP marked this conversation as resolved.
Show resolved Hide resolved
pageTitleBuilder.SetFixedTitle(new HtmlString(SimpleTitle));

pageTitleBuilder.AddSegment(new HtmlString("you don't see me"));
PiemP marked this conversation as resolved.
Show resolved Hide resolved

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


[Fact]
public void TitleAddSegment()
{
var pageTitleBuilder = _serviceProvider.GetService<IPageTitleBuilder>();
PiemP marked this conversation as resolved.
Show resolved Hide resolved
pageTitleBuilder.Clear();

pageTitleBuilder.AddSegment(new HtmlString(SimpleTitle), "after");
PiemP marked this conversation as resolved.
Show resolved Hide resolved

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

[Fact]
public void TitleMultiAddSegment()
{
var pageTitleBuilder = _serviceProvider.GetService<IPageTitleBuilder>();
PiemP marked this conversation as resolved.
Show resolved Hide resolved
pageTitleBuilder.Clear();

pageTitleBuilder.AddSegment(new HtmlString(FirstPartTitle), "after");
PiemP marked this conversation as resolved.
Show resolved Hide resolved

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()
{
var pageTitleBuilder = _serviceProvider.GetService<IPageTitleBuilder>();
PiemP marked this conversation as resolved.
Show resolved Hide resolved
pageTitleBuilder.Clear();

pageTitleBuilder.AddSegment(new HtmlString(FirstPartTitle), "after");
PiemP marked this conversation as resolved.
Show resolved Hide resolved

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()
{
var pageTitleBuilder = _serviceProvider.GetService<IPageTitleBuilder>();
PiemP marked this conversation as resolved.
Show resolved Hide resolved
pageTitleBuilder.Clear();

pageTitleBuilder.AddSegment(new HtmlString(FirstPartTitle), "after");
PiemP marked this conversation as resolved.
Show resolved Hide resolved

Assert.Equal(FirstPartTitle, ToString(pageTitleBuilder.GenerateTitle(new HtmlString(Separator))));
PiemP marked this conversation as resolved.
Show resolved Hide resolved
Assert.Equal(FirstPartTitle, ToString(pageTitleBuilder.GenerateTitle(new HtmlString(Separator))));
}

[Fact]
public void TitleAddSegments()
{
var pageTitleBuilder = (PageTitleBuilder)_serviceProvider.GetService<IPageTitleBuilder>();
PiemP marked this conversation as resolved.
Show resolved Hide resolved
pageTitleBuilder.Clear();

var elements = new IHtmlContent[]
{
new HtmlString(FirstPartTitle),
new HtmlString(SecondPartTitle)
};
pageTitleBuilder.AddSegments(elements, "after");
hishamco marked this conversation as resolved.
Show resolved Hide resolved

Assert.Equal($"{FirstPartTitle}{Separator}{SecondPartTitle}", ToString(pageTitleBuilder.GenerateTitle(new HtmlString(Separator))));
PiemP marked this conversation as resolved.
Show resolved Hide resolved
}

hishamco marked this conversation as resolved.
Show resolved Hide resolved

private static string ToString(IHtmlContent content)
hishamco marked this conversation as resolved.
Show resolved Hide resolved
{
using var writer = new ZStringWriter();
content.WriteTo(writer, HtmlEncoder.Default);

return writer.ToString();
}
}
}