Skip to content

Commit

Permalink
227: Fix global regex redirect (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickdemooij9 committed Oct 12, 2023
1 parent ca1e828 commit b37e81b
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 4 deletions.
49 changes: 49 additions & 0 deletions src/SeoToolkit.Tests/SeoToolkit.Tests/RedirectTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Moq;
using SeoToolkit.Umbraco.Redirects.Core.Interfaces;
using SeoToolkit.Umbraco.Redirects.Core.Models.Business;
using SeoToolkit.Umbraco.Redirects.Core.Services;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.PublishedCache;
using Umbraco.Cms.Core.Routing;
using Umbraco.Cms.Core.Web;

namespace SeoToolkit.Tests
{
[TestFixture]
public class RedirectTests
{
[Test]
public void TestRegexAllSitesRedirect()
{
// Arrange
var redirectId = 1;
var redirectRepository = new Mock<IRedirectsRepository>();
redirectRepository.Setup(it => it.GetAllRegexRedirects()).Returns(() => new Redirect[]
{
new Redirect { Domain = null, IsRegex = true, Id = redirectId, OldUrl = "^/test" }
});

var umbracoContextFactory = GetContextFactoryWithDomain();
var redirectService = new RedirectsService(redirectRepository.Object, umbracoContextFactory);

// Act
var redirect = redirectService.GetByUrl(new Uri("https://test.nl/test123"));

// Assert
Assert.IsNotNull(redirect);
}

private IUmbracoContextFactory GetContextFactoryWithDomain()
{
var umbracoContextFactory = new Mock<IUmbracoContextFactory>();
var umbracoContext = new Mock<IUmbracoContext>();
var domainCache = new Mock<IDomainCache>();

domainCache.Setup(it => it.GetAll(false)).Returns(Enumerable.Empty<Domain>);
umbracoContext.Setup(it => it.Domains).Returns(() => domainCache.Object);
umbracoContextFactory.Setup(it => it.EnsureUmbracoContext()).Returns(() => new UmbracoContextReference(umbracoContext.Object, true, Mock.Of<IUmbracoContextAccessor>()));

return umbracoContextFactory.Object;
}
}
}
24 changes: 24 additions & 0 deletions src/SeoToolkit.Tests/SeoToolkit.Tests/SeoToolkit.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="Moq" Version="4.20.69" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\SeoToolkit.Umbraco\SeoToolkit.Umbraco.csproj" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions src/SeoToolkit.Tests/SeoToolkit.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using NUnit.Framework;
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ it.Domain is null &&

var regexRedirects = _redirectsRepository.GetAllRegexRedirects().Where(it =>
{
if (it.Domain?.Id == 0) return true;
if (domain != null && it.Domain?.Id == domain.Id) return true;
if (it.Domain == null) return true;
if (domain != null && it.Domain.Id == domain.Id) return true;
if (domain is null &&
!string.IsNullOrWhiteSpace(it.CustomDomain) &&
(it.CustomDomain.Equals(customDomainWithoutScheme,
Expand Down
13 changes: 11 additions & 2 deletions src/SeoToolkit.Umbraco.sln
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SeoToolkit.Umbraco.Core", "
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Redirects", "Redirects", "{B0B49325-8248-4DB8-889D-211D9408D7A5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SeoToolkit.Umbraco.Redirects", "SeoToolkit.Umbraco.Redirects\SeoToolkit.Umbraco.Redirects.csproj", "{7C9ABE0D-106C-48C0-B0CF-DBBCD8AF334E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SeoToolkit.Umbraco.Redirects", "SeoToolkit.Umbraco.Redirects\SeoToolkit.Umbraco.Redirects.csproj", "{7C9ABE0D-106C-48C0-B0CF-DBBCD8AF334E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SeoToolkit.Umbraco.Redirects.Core", "SeoToolkit.Umbraco.Redirects.Core\SeoToolkit.Umbraco.Redirects.Core.csproj", "{566D1DBF-EC21-405C-BB92-424C4A1C1B88}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SeoToolkit.Umbraco.Redirects.Core", "SeoToolkit.Umbraco.Redirects.Core\SeoToolkit.Umbraco.Redirects.Core.csproj", "{566D1DBF-EC21-405C-BB92-424C4A1C1B88}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{410B1EAC-B947-4E17-A53E-835CA5948B5A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SeoToolkit.Tests", "SeoToolkit.Tests\SeoToolkit.Tests\SeoToolkit.Tests.csproj", "{A722B608-441C-40F2-962A-DD481E3FF849}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -123,6 +127,10 @@ Global
{566D1DBF-EC21-405C-BB92-424C4A1C1B88}.Debug|Any CPU.Build.0 = Debug|Any CPU
{566D1DBF-EC21-405C-BB92-424C4A1C1B88}.Release|Any CPU.ActiveCfg = Release|Any CPU
{566D1DBF-EC21-405C-BB92-424C4A1C1B88}.Release|Any CPU.Build.0 = Release|Any CPU
{A722B608-441C-40F2-962A-DD481E3FF849}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A722B608-441C-40F2-962A-DD481E3FF849}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A722B608-441C-40F2-962A-DD481E3FF849}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A722B608-441C-40F2-962A-DD481E3FF849}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -140,6 +148,7 @@ Global
{5B460335-8786-4FA9-911B-AEC5A752B88C} = {5641B436-FCCB-4D1A-9D25-6A39B95031CB}
{7C9ABE0D-106C-48C0-B0CF-DBBCD8AF334E} = {B0B49325-8248-4DB8-889D-211D9408D7A5}
{566D1DBF-EC21-405C-BB92-424C4A1C1B88} = {B0B49325-8248-4DB8-889D-211D9408D7A5}
{A722B608-441C-40F2-962A-DD481E3FF849} = {410B1EAC-B947-4E17-A53E-835CA5948B5A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {551ADBA5-3DFB-4300-929C-2BE0DB346636}
Expand Down

0 comments on commit b37e81b

Please sign in to comment.