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 4 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
@@ -0,0 +1,16 @@
using System.IO;
using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Html;

PiemP marked this conversation as resolved.
Show resolved Hide resolved
namespace OrchardCore.DisplayManagement.Extensions;

public static class HtmlContentExtensions
{
public static string GetString(this IHtmlContent content)
{
using var writer = new StringWriter();
content.WriteTo(writer, HtmlEncoder.Default);

return writer.ToString();
}
}
PiemP marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Html;
using OrchardCore.DisplayManagement.Zones;
using OrchardCore.DisplayManagement.Extensions;

namespace OrchardCore.DisplayManagement.Title
{
Expand Down Expand Up @@ -48,7 +50,7 @@ public IHtmlContent GenerateTitle(IHtmlContent separator)
return _fixedTitle;
}

if (_title != null)
if (_title != null && String.Empty != _title.GetString())
PiemP marked this conversation as resolved.
Show resolved Hide resolved
{
return _title;
}
Expand Down Expand Up @@ -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,134 @@
using OrchardCore.DisplayManagement;
using OrchardCore.DisplayManagement.Extensions;
using OrchardCore.DisplayManagement.Title;
using OrchardCore.Environment.Extensions;
using OrchardCore.Localization;
using OrchardCore.Tests.Stubs;

namespace OrchardCore.DisplayManagement.Title
{
public class PageTitleBuilderTests
{
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("", pageTitleBuilder.GenerateTitle().GetString());
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("I'm a title"));
PiemP marked this conversation as resolved.
Show resolved Hide resolved

Assert.Equal("I'm a title", pageTitleBuilder.GenerateTitle().GetString());
}

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

Assert.Equal("", pageTitleBuilder.GenerateTitle().GetString());
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("I'm a title"));

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

Assert.Equal("I'm a title", pageTitleBuilder.GenerateTitle().GetString());
}


[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("you have to see me"), "after");

Assert.Equal("you have to see me", pageTitleBuilder.GenerateTitle().GetString());
}

[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("first part"), "after");

Assert.Equal("first part", pageTitleBuilder.GenerateTitle(new HtmlString(" - ")).GetString());

pageTitleBuilder.AddSegment(new HtmlString("second part"), "after");

Assert.Equal("first part - second part", pageTitleBuilder.GenerateTitle(new HtmlString(" - ")).GetString());
}

[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("first part"), "after");

Assert.Equal("first part", pageTitleBuilder.GenerateTitle(new HtmlString(" - ")).GetString());

pageTitleBuilder.Clear();

Assert.Equal("", pageTitleBuilder.GenerateTitle(new HtmlString(" - ")).GetString());
}

[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("first part"), "after");

Assert.Equal("first part", pageTitleBuilder.GenerateTitle(new HtmlString(" - ")).GetString());

PiemP marked this conversation as resolved.
Show resolved Hide resolved
Assert.Equal("first part", pageTitleBuilder.GenerateTitle(new HtmlString(" - ")).GetString());
}

[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("the first part"),
new HtmlString("the second one")
};
pageTitleBuilder.AddSegments(elements, "after");
hishamco marked this conversation as resolved.
Show resolved Hide resolved

Assert.Equal("the first part - the second one", pageTitleBuilder.GenerateTitle(new HtmlString(" - ")).GetString());
}
}
}