Skip to content

Commit

Permalink
Merge pull request #63 from Lombiq/issue/OSOE-548
Browse files Browse the repository at this point in the history
OSOE-548: Upgrade to Orchard Core 1.6 in Orchard-Base-Theme
  • Loading branch information
Piedone authored Jun 18, 2023
2 parents 41682b8 + 84bbf15 commit d175b27
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 43 deletions.
6 changes: 3 additions & 3 deletions Lombiq.BaseTheme.Samples/Lombiq.BaseTheme.Samples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="OrchardCore.Admin" Version="1.5.0" />
<PackageReference Include="OrchardCore.Theme.Targets" Version="1.5.0" />
<PackageReference Include="OrchardCore.Admin" Version="1.6.0" />
<PackageReference Include="OrchardCore.Theme.Targets" Version="1.6.0" />
</ItemGroup>

<ItemGroup>
Expand All @@ -39,7 +39,7 @@
</ItemGroup>

<ItemGroup Condition="'$(NuGetBuild)' == 'true'">
<PackageReference Include="Lombiq.NodeJs.Extensions" Version="1.2.3" />
<PackageReference Include="Lombiq.NodeJs.Extensions" Version="1.2.4" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,48 @@ await context.DoWithRetriesOrFailAsync(() =>
await context.ClickMainMenuPathAsync("Log In");
context.Exists(By.XPath("//form[@action = '/Login']/*[starts-with(name(), 'h') and contains(., 'Log in')]"));
}

public static async Task TestBaseThemeDependencyIsEnabledAsync(this UITestContext context)
{
await context.GoToAdminRelativeUrlAsync("/Features");
context.Exists(By.Id("btn-disable-Lombiq_HelpfulExtensions_Widgets"));
}

public static void TestBlogRecipeMenuItemsAddedToMainMenu(this UITestContext context)
{
context.Get(By.CssSelector(".menuWidget__content .nav-link[href='/']")).Text.Trim().ShouldBe("Home");
context.Get(By.CssSelector(".menuWidget__content .nav-link[href='/about']")).Text.Trim().ShouldBe("About");
}

public static async Task TestAddingMenuItemToBlogMainMenuAsync(this UITestContext context)
{
// The menu item has to be added through the admin by editing the menu like this because it can't be added
// through a recipe. The setup recipe in OSOCE executes the Blog recipe which already creates a menu with the
// "main-menu" alias. As it uses a random UUID for Content Item ID, it can't be updated from another recipe (if
// attempted the setup will throw "ValidationException: Your alias is already in use." exception).
// See https://github.com/Lombiq/Helpful-Libraries/issues/199 for a possible solution.
await context.GoToAdminRelativeUrlAsync("/Contents/ContentItems/Menu");
await context.ClickReliablyOnAsync(By.ClassName("edit"));

await context.ClickReliablyOnAsync(By.XPath("//button[contains(., 'Add Menu Item')]"));
await context.ClickReliablyOnAsync(By.XPath(
"//div[contains(@class, 'card') and .//h4[contains(., 'Content Menu Item')]]//div[contains(@class, 'card-footer')]//a"));
await context.ClickAndFillInWithRetriesAsync(By.Id("ContentMenuItemPart_Name"), "My Content");

await context.SetContentPickerByDisplayTextAsync(
"ContentMenuItemPart",
"SelectedContentItem",
"Man must explore, and this is exploration at its greatest");

await context.ClickPublishAsync();
await context.ClickPublishAsync();
context.ShouldBeSuccess();

await context.GoToHomePageAsync();
context
.Get(By.XPath("id('navigation')//li[contains(@class, 'menuWidget__topLevel')]/a[@href='/blog/post-1']"))
.Text
.Trim()
.ShouldBe("My Content");
}
}
2 changes: 1 addition & 1 deletion Lombiq.BaseTheme.Tests.UI/Lombiq.BaseTheme.Tests.UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</ItemGroup>

<ItemGroup Condition="'$(NuGetBuild)' == 'true'">
<PackageReference Include="Lombiq.Tests.UI" Version="6.1.0" />
<PackageReference Include="Lombiq.Tests.UI" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down

This file was deleted.

20 changes: 9 additions & 11 deletions Lombiq.BaseTheme/Lombiq.BaseTheme.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="OrchardCore.Menu" Version="1.5.0" />
<PackageReference Include="OrchardCore.Theme.Targets" Version="1.5.0" />
<PackageReference Include="OrchardCore.ContentManagement" Version="1.5.0" />
<PackageReference Include="OrchardCore.DisplayManagement" Version="1.5.0" />
<PackageReference Include="OrchardCore.ResourceManagement" Version="1.5.0" />
<PackageReference Include="OrchardCore.Theme.Targets" Version="1.6.0" />
<PackageReference Include="OrchardCore.Menu" Version="1.6.0" />
<PackageReference Include="OrchardCore.ContentManagement" Version="1.6.0" />
<PackageReference Include="OrchardCore.DisplayManagement" Version="1.6.0" />
<PackageReference Include="OrchardCore.ResourceManagement" Version="1.6.0" />
</ItemGroup>

<ItemGroup Condition="'$(NuGetBuild)' != 'true'">
Expand All @@ -46,9 +46,9 @@
</ItemGroup>

<ItemGroup Condition="'$(NuGetBuild)' == 'true'">
<PackageReference Include="Lombiq.HelpfulLibraries" Version="5.2.0" />
<PackageReference Include="Lombiq.HelpfulExtensions" Version="5.0.0" />
<PackageReference Include="Lombiq.NodeJs.Extensions" Version="1.2.3" />
<PackageReference Include="Lombiq.HelpfulLibraries" Version="6.0.0" />
<PackageReference Include="Lombiq.HelpfulExtensions" Version="6.0.0" />
<PackageReference Include="Lombiq.NodeJs.Extensions" Version="1.2.4" />

<Content Include="Assets/Styles/**/*.*">
<IncludeInPackage>true</IncludeInPackage>
Expand All @@ -63,9 +63,7 @@
<!-- This is necessary, because if we try to invoke the recipe from the migration file using the "recipe" step it will
load forever and eventually cause a timeout. -->
<Target Name="Copy LayersAndZones to Migrations" AfterTargets="AfterResolveReferences">
<Copy SourceFiles="$(ProjectDir)Recipes\Lombiq.BaseTheme.LayersAndZones.recipe.json"
DestinationFiles="$(ProjectDir)Migrations\Lombiq.BaseTheme.LayersAndZones.recipe.json"
SkipUnchangedFiles="true" />
<Copy SourceFiles="$(ProjectDir)Recipes\Lombiq.BaseTheme.LayersAndZones.recipe.json" DestinationFiles="$(ProjectDir)Migrations\Lombiq.BaseTheme.LayersAndZones.recipe.json" SkipUnchangedFiles="true" />
</Target>

</Project>
27 changes: 17 additions & 10 deletions Lombiq.BaseTheme/Services/MainMenuNavigationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
using AngleSharp.Html.Parser;
using Lombiq.HelpfulLibraries.OrchardCore.Navigation;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.Extensions.Localization;
using Newtonsoft.Json.Linq;
using OrchardCore.ContentManagement;
using OrchardCore.Menu.Models;
using OrchardCore.Navigation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -19,8 +21,8 @@ public class MainMenuNavigationProvider : MainMenuNavigationProviderBase
{
private readonly IContentHandleManager _contentHandleManager;
private readonly IContentManager _contentManager;
private readonly IUrlHelperFactory _urlHelperFactory;
private readonly IActionContextAccessor _actionContextAccessor;

private readonly Lazy<IUrlHelper> _urlHelperLazy;

public MainMenuNavigationProvider(
IHttpContextAccessor hca,
Expand All @@ -33,8 +35,8 @@ public MainMenuNavigationProvider(
{
_contentHandleManager = contentHandleManager;
_contentManager = contentManager;
_urlHelperFactory = urlHelperFactory;
_actionContextAccessor = actionContextAccessor;

_urlHelperLazy = new(() => urlHelperFactory.GetUrlHelper(actionContextAccessor.ActionContext!));
}

protected override async Task BuildAsync(NavigationBuilder builder)
Expand All @@ -58,7 +60,7 @@ private async Task AddAsync(NavigationBuilder builder, ContentItem menuItem)

if (menuItem.As<LinkMenuItemPart>() is { } linkMenuItemPart)
{
builder.Add(text, menu => menu.Url(linkMenuItemPart.Url));
builder.Add(text, menu => menu.Url(GetUrl(linkMenuItemPart.Url)));
}
else if (menuItem.As<ContentMenuItemPart>() is { } contentMenuItemPart)
{
Expand All @@ -73,7 +75,9 @@ private async Task AddAsync(NavigationBuilder builder, ContentItem menuItem)
{
var nodeList = new HtmlParser().ParseFragment($"<div>{htmlMenuItemPart.Html}</div>", contextElement: null!);
var textContent = string.Concat(nodeList.Select(x => x.Text()));
builder.Add(new LocalizedString(textContent, textContent), menu => menu.Url("#").LocalNav());
var url = string.IsNullOrEmpty(htmlMenuItemPart.Url) ? "#" : GetUrl(htmlMenuItemPart.Url);

builder.Add(T[textContent], menu => menu.Url(url).LocalNav());
}
else if (menuItem.As<MenuItemsListPart>() is { } menuItemsListPart)
{
Expand All @@ -85,26 +89,29 @@ await builder.AddAsync(text, menu =>
private async Task AddContentMenuItemPartAsync(NavigationBuilder builder, LocalizedString text, IEnumerable<string> ids)
{
var contentItems = (await _contentManager.GetAsync(ids)).AsList();
var urlHelper = _urlHelperFactory.GetUrlHelper(_actionContextAccessor.ActionContext!);

if (contentItems.Count == 1)
{
var contentItem = contentItems.Single();
if (string.IsNullOrEmpty(text.Value)) text = GetTitle(contentItem);
builder.Add(text, menu => menu.Url(urlHelper.DisplayContentItem(contentItem)));
builder.Add(text, menu => UseDisplayUrl(menu, contentItem));
}
else
{
builder.Add(text, menu =>
{
foreach (var contentItem in contentItems)
{
menu.Add(GetTitle(contentItem), child => child.Url(urlHelper.DisplayContentItem(contentItem)));
menu.Add(GetTitle(contentItem), child => UseDisplayUrl(child, contentItem));
}
});
}
}

private string GetUrl(string contentPath) => _urlHelperLazy.Value.Content(contentPath);

private void UseDisplayUrl(NavigationItemBuilder menu, IContent content) =>
menu.Url(_urlHelperLazy.Value.DisplayContentItem(content));

private static LocalizedString GetTitle(ContentItem contentItem) =>
new(contentItem.DisplayText, contentItem.DisplayText);
}
2 changes: 1 addition & 1 deletion Lombiq.BaseTheme/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Lombiq.BaseTheme.Extensions;
using Lombiq.BaseTheme.Middlewares;
using Lombiq.BaseTheme.Migrations;
using Lombiq.BaseTheme.Services;
Expand All @@ -10,6 +9,7 @@
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using OrchardCore.Data.Migration;
using OrchardCore.Modules;
using OrchardCore.Navigation;
using OrchardCore.ResourceManagement;
Expand Down

0 comments on commit d175b27

Please sign in to comment.