-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix title empty if AddSegment is not called (#14295)
* 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
Showing
2 changed files
with
156 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
test/OrchardCore.Tests/DisplayManagement/Title/PageTitleBuilderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |