Skip to content

Commit

Permalink
Merge pull request #425 from Lombiq/issue/OSOE-916
Browse files Browse the repository at this point in the history
OSOE-916: Change "shift time" terminology to "time shift" in Lombiq.UITestingToolbox
  • Loading branch information
Piedone authored Nov 21, 2024
2 parents 28cf371 + 90d2bc6 commit 2d4ec85
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 27 deletions.
6 changes: 3 additions & 3 deletions Lombiq.Tests.UI.Samples/Tests/ShiftTimeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
namespace Lombiq.Tests.UI.Samples.Tests;

// When you enable the "Shift Time - Shortcuts - Lombiq UI Testing Toolbox" feature, it replaces Orchard Core's stock
// ICLock implementation with the custom ShiftTimeClock class. You can use the
// ~/Lombiq.Tests.UI.Samples/ShiftTime/Set?days=... action to update the ShiftTimeClock.Shift property for the current
// ICLock implementation with the custom TimeShiftingClock class. You can use the
// ~/Lombiq.Tests.UI.Samples/TimeShift/Set?days=... action to update the TimeShiftingClock.Shift property for the current
// tenant, which will trick any service that uses IClock into thinking you are in the future. This can be used to test
// features that can expire, such as a limited-time product discount in a web store, without having to wait.
public class ShiftTimeTests : UITestBase
Expand Down Expand Up @@ -46,7 +46,7 @@ await context.FillInCodeMirrorEditorWithRetriesAsync(
// This extension method navigates to the action which sets the time offset. You can set it in terms of
// days or seconds. Both accept fractions and negative values. If both days and seconds are set, they
// are added together.
await context.SetShiftTimeAsync(days: 10);
await context.SetTimeShiftAsync(TimeSpan.FromDays(10));

// Let's verify the date!
var tenDaysFromNow = await GetNowAsync(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
namespace Lombiq.Tests.UI.Shortcuts.Controllers;

[DevelopmentAndLocalhostOnly]
public class ShiftTimeController : Controller
public class TimeShiftController : Controller
{
private readonly IClock _clock;

public ShiftTimeController(IClock clock) => _clock = clock;
public TimeShiftController(IClock clock) => _clock = clock;

public IActionResult Set(double days, double seconds) =>
SetInner(_ => TimeSpan.FromDays(days) + TimeSpan.FromSeconds(seconds));
Expand All @@ -20,7 +20,7 @@ public IActionResult Add(double days, double seconds) =>
SetInner(current => current + TimeSpan.FromDays(days) + TimeSpan.FromSeconds(seconds));

private IActionResult SetInner(Func<TimeSpan, TimeSpan> edit) =>
ShiftTimeClock.UpdateClock(_clock, edit) is { } totalSeconds
TimeShiftingClock.UpdateClock(_clock, edit) is { } totalSeconds
? Ok(totalSeconds)
: BadRequest($"The clock is {_clock.GetType().FullName} instead of {nameof(ShiftTimeClock)}.");
: BadRequest($"The clock is {_clock.GetType().FullName} instead of {nameof(TimeShiftingClock)}.");
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Lombiq.Tests.UI.Shortcuts.Services;
/// cref="IClock"/> into believing it's the future or past. This service is registered as singleton, so setting <see
/// cref="Shift"/> will persist in the tenant across requests.
/// </summary>
public class ShiftTimeClock : IClock
public class TimeShiftingClock : IClock
{
private readonly Clock _inner = new();

Expand All @@ -34,18 +34,18 @@ public DateTimeOffset ConvertToTimeZone(DateTimeOffset dateTimeOffset, ITimeZone
/// Use this to safely update the <see cref="Shift"/> value.
/// </summary>
/// <param name="clock">
/// The injected service, if it's <see cref="ShiftTimeClock"/> then its <see cref="Shift"/> property is updated.
/// The injected service, if it's <see cref="TimeShiftingClock"/> then its <see cref="Shift"/> property is updated.
/// </param>
/// <param name="edit">Input is the current value of <see cref="Shift"/>, output is the new value.</param>
/// <returns>
/// The total seconds of the new value of <see cref="Shift"/>, or <see langword="null"/> if <paramref name="clock"/>
/// is not <see cref="ShiftTimeClock"/>.
/// is not <see cref="TimeShiftingClock"/>.
/// </returns>
public static double? UpdateClock(IClock clock, Func<TimeSpan, TimeSpan> edit)
{
if (clock is not ShiftTimeClock shiftTimeClock) return null;
if (clock is not TimeShiftingClock timeShiftingClock) return null;

shiftTimeClock.Shift = edit(shiftTimeClock.Shift);
return shiftTimeClock.Shift.TotalSeconds;
timeShiftingClock.Shift = edit(timeShiftingClock.Shift);
return timeShiftingClock.Shift.TotalSeconds;
}
}
10 changes: 5 additions & 5 deletions Lombiq.Tests.UI.Shortcuts/ShortcutsFeatureIds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ public static class ShortcutsFeatureIds
public const string Area = "Lombiq.Tests.UI.Shortcuts";

public const string Default = Area;
public const string FeatureToggleTestBench = Default + ".FeatureToggleTestBench";
public const string MediaCachePurge = Default + ".MediaCachePurge";
public const string ShiftTime = Default + ".ShiftTime";
public const string Swagger = Default + ".Swagger";
public const string Workflows = Default + ".Workflows";
public const string FeatureToggleTestBench = $"{Default}.{nameof(FeatureToggleTestBench)}";
public const string MediaCachePurge = $"{Default}.{nameof(MediaCachePurge)}";
public const string ShiftTime = $"{Default}.{nameof(ShiftTime)}";
public const string Swagger = $"{Default}.{nameof(Swagger)}";
public const string Workflows = $"{Default}.{nameof(Workflows)}";
}
2 changes: 1 addition & 1 deletion Lombiq.Tests.UI.Shortcuts/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ public sealed class SetTimeStartup : StartupBase
public override void ConfigureServices(IServiceCollection services)
{
services.RemoveImplementationsOf<IClock>();
services.AddSingleton<IClock, ShiftTimeClock>();
services.AddSingleton<IClock, TimeShiftingClock>();
}
}
18 changes: 10 additions & 8 deletions Lombiq.Tests.UI/Extensions/ShortcutsUITestContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -744,23 +744,25 @@ public static Task ExecuteJsonRecipeSiteSettingAsync<T>(this UITestContext conte
public static Task EnableTimeShiftingAsync(this UITestContext context) => context.EnableFeatureDirectlyAsync(ShiftTime);

/// <summary>
/// Sets the time shift to a specific value. If both <paramref name="days"/> and <paramref name="seconds"/> are
/// provided, then the <see cref="TimeSpan"/> values are added together.
/// Sets the time shift to a specific value.
/// </summary>
public static Task SetShiftTimeAsync(this UITestContext context, double days = 0, double seconds = 0)
public static Task SetTimeShiftAsync(this UITestContext context, TimeSpan time)
{
context.EnsureValidOrchardCoreTenantScope();
return context.GoToAsync<ShiftTimeController>(controller => controller.Set(days, seconds));
return time.TotalDays >= 1.0
? context.GoToAsync<TimeShiftController>(controller => controller.Set(time.TotalDays, 0))
: context.GoToAsync<TimeShiftController>(controller => controller.Set(0, time.TotalSeconds));
}

/// <summary>
/// Adds the specified value to the time shift. If both <paramref name="days"/> and <paramref name="seconds"/> are
/// provided, then the <see cref="TimeSpan"/> values for both are added. Negative values are supported as well.
/// Adds the specified value to the time shift.
/// </summary>
public static Task AddShiftTimeAsync(this UITestContext context, double days = 0, double seconds = 0)
public static Task AddTimeShiftAsync(this UITestContext context, TimeSpan time)
{
context.EnsureValidOrchardCoreTenantScope();
return context.GoToAsync<ShiftTimeController>(controller => controller.Add(days, seconds));
return time.TotalDays >= 1.0
? context.GoToAsync<TimeShiftController>(controller => controller.Add(time.TotalDays, 0))
: context.GoToAsync<TimeShiftController>(controller => controller.Add(0, time.TotalSeconds));
}

/// <summary>
Expand Down

0 comments on commit 2d4ec85

Please sign in to comment.