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

OSOE-93: Add extension methods #162

Merged
merged 11 commits into from
May 30, 2022
Merged
13 changes: 12 additions & 1 deletion Lombiq.Tests.UI.Samples/.htmlvalidate.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,16 @@
"wcag/h37": "off",
"wcag/h67": "off",
"wcag/h71": "off"
}
},

"elements": [
"html5",
{
"form": {
"attributes": {
"method": [ "get", "post", "GET", "POST" ]
}
}
}
]
}
25 changes: 24 additions & 1 deletion Lombiq.Tests.UI.Shortcuts/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using OrchardCore.Entities;
using OrchardCore.Settings;
using OrchardCore.Users;
using OrchardCore.Users.Models;
using System.Threading.Tasks;

namespace Lombiq.Tests.UI.Shortcuts.Controllers;
Expand All @@ -12,11 +15,16 @@ public class AccountController : Controller
{
private readonly UserManager<IUser> _userManager;
private readonly SignInManager<IUser> _userSignInManager;
private readonly ISiteService _siteService;

public AccountController(UserManager<IUser> userManager, SignInManager<IUser> userSignInManager)
public AccountController(
UserManager<IUser> userManager,
SignInManager<IUser> userSignInManager,
ISiteService siteService)
{
_userManager = userManager;
_userSignInManager = userSignInManager;
_siteService = siteService;
}

[AllowAnonymous]
Expand All @@ -37,4 +45,19 @@ public async Task<IActionResult> SignOutDirectly()

return Ok();
}

[AllowAnonymous]
public async Task<ActionResult> SetUserRegistrationType(UserRegistrationType type)
{
var settings = await _siteService.LoadSiteSettingsAsync();
var registrationSettings = settings.As<RegistrationSettings>();

registrationSettings.UsersCanRegister = type;

settings.Put(registrationSettings);
sarahelsaig marked this conversation as resolved.
Show resolved Hide resolved

await _siteService.UpdateSiteSettingsAsync(settings);

return Ok();
}
}
66 changes: 66 additions & 0 deletions Lombiq.Tests.UI.Shortcuts/Controllers/ThemeController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using Lombiq.HelpfulLibraries.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using OrchardCore.Admin;
using OrchardCore.DisplayManagement.Extensions;
using OrchardCore.Environment.Extensions;
using OrchardCore.Environment.Shell;
using OrchardCore.Modules.Manifest;
using OrchardCore.Themes.Services;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace Lombiq.Tests.UI.Shortcuts.Controllers;

[DevelopmentAndLocalhostOnly]
public class ThemeController : Controller
{
private readonly IShellFeaturesManager _shellFeaturesManager;
private readonly ISiteThemeService _siteThemeService;
private readonly IAdminThemeService _adminThemeService;

public ThemeController(
IShellFeaturesManager shellFeaturesManager,
ISiteThemeService siteThemeService,
IAdminThemeService adminThemeService)
{
_shellFeaturesManager = shellFeaturesManager;
_siteThemeService = siteThemeService;
_adminThemeService = adminThemeService;
}

[AllowAnonymous]
public async Task<IActionResult> SelectTheme(string id)
{
var themeFeature = (await _shellFeaturesManager.GetAvailableFeaturesAsync())
.FirstOrDefault(feature => feature.IsTheme() && feature.Id == id);

if (themeFeature == null)
{
return NotFound();
}

if (IsAdminTheme(themeFeature.Extension.Manifest))
{
await _adminThemeService.SetAdminThemeAsync(id);
}
else
{
await _siteThemeService.SetSiteThemeAsync(id);
}

var enabledFeatures = await _shellFeaturesManager.GetEnabledFeaturesAsync();
var isEnabled = enabledFeatures.Any(x => x.Extension.Id == themeFeature.Id);
sarahelsaig marked this conversation as resolved.
Show resolved Hide resolved

if (!isEnabled)
{
await _shellFeaturesManager.EnableFeaturesAsync(new[] { themeFeature }, force: true);
}

return Ok();
}

private static bool IsAdminTheme(IManifestInfo manifest) =>
manifest.Tags.Any(x => string.Equals(x, ManifestConstants.AdminTag, StringComparison.OrdinalIgnoreCase));
sarahelsaig marked this conversation as resolved.
Show resolved Hide resolved
}
3 changes: 2 additions & 1 deletion Lombiq.Tests.UI.Shortcuts/Lombiq.Tests.UI.Shortcuts.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@

<ItemGroup>
<PackageReference Include="OrchardCore.Module.Targets" Version="1.3.0" />
<PackageReference Include="OrchardCore.Users.Abstractions" Version="1.3.0" />
<PackageReference Include="OrchardCore.Themes" Version="1.3.0" />
<PackageReference Include="OrchardCore.Users" Version="1.3.0" />
<PackageReference Include="OrchardCore.Media.Abstractions" Version="1.3.0" />
<PackageReference Include="OrchardCore.Recipes.Abstractions" Version="1.3.0" />
<PackageReference Include="OrchardCore.Abstractions" Version="1.3.0" />
Expand Down
2 changes: 1 addition & 1 deletion Lombiq.Tests.UI.Shortcuts/Manifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Name = "Shortcuts - Lombiq UI Testing Toolbox",
Author = "Lombiq Technologies",
Website = "https://github.com/Lombiq/UI-Testing-Toolbox",
Version = "2.1.1"
Version = "2.1.2-alpha"
)]

[assembly: Feature(
Expand Down
13 changes: 12 additions & 1 deletion Lombiq.Tests.UI/.htmlvalidate.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,16 @@
"wcag/h37": "off",
"wcag/h67": "off",
"wcag/h71": "off"
}
},

"elements": [
"html5",
{
"form": {
"attributes": {
"method": [ "get", "post", "GET", "POST" ]
}
}
}
]
}
15 changes: 15 additions & 0 deletions Lombiq.Tests.UI/Extensions/ShortcutsUITestContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Lombiq.Tests.UI.Shortcuts.Controllers;
using Lombiq.Tests.UI.Shortcuts.Models;
using OpenQA.Selenium;
using OrchardCore.Users.Models;
using RestEase;
using Shouldly;
using System;
Expand Down Expand Up @@ -77,6 +78,13 @@ public static async Task<string> GetCurrentUserNameAsync(this UITestContext cont
return userNameContainer["UserName: ".Length..];
}

/// <summary>
/// Sets the registration type in site settings.
/// <c>Lombiq.Tests.UI.Shortcuts</c> enabled.
sarahelsaig marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
public static Task SetUserRegistrationTypeAsync(this UITestContext context, UserRegistrationType type) =>
context.GoToAsync<AccountController>(controller => controller.SetUserRegistrationType(type));

/// <summary>
/// Enables the feature with the given ID directly, without anything else happening on the admin Features page. The
/// target app needs to have <c>Lombiq.Tests.UI.Shortcuts</c> enabled.
Expand Down Expand Up @@ -182,4 +190,11 @@ public interface IShortcutsApi
[Get("api/ApplicationInfo")]
Task<ApplicationInfo> GetApplicationInfoAsync();
}

/// <summary>
/// Selects theme by id.
/// <c>Lombiq.Tests.UI.Shortcuts</c> enabled.
sarahelsaig marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
public static Task SelectThemeAsync(this UITestContext context, string id) =>
context.GoToAsync<ThemeController>(controller => controller.SelectTheme(id));
}