diff --git a/src/SeoToolkit.Tests/SeoToolkit.Tests/RedirectTests.cs b/src/SeoToolkit.Tests/SeoToolkit.Tests/RedirectTests.cs new file mode 100644 index 00000000..8c75629e --- /dev/null +++ b/src/SeoToolkit.Tests/SeoToolkit.Tests/RedirectTests.cs @@ -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(); + 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(); + var umbracoContext = new Mock(); + var domainCache = new Mock(); + + domainCache.Setup(it => it.GetAll(false)).Returns(Enumerable.Empty); + umbracoContext.Setup(it => it.Domains).Returns(() => domainCache.Object); + umbracoContextFactory.Setup(it => it.EnsureUmbracoContext()).Returns(() => new UmbracoContextReference(umbracoContext.Object, true, Mock.Of())); + + return umbracoContextFactory.Object; + } + } +} diff --git a/src/SeoToolkit.Tests/SeoToolkit.Tests/SeoToolkit.Tests.csproj b/src/SeoToolkit.Tests/SeoToolkit.Tests/SeoToolkit.Tests.csproj new file mode 100644 index 00000000..855816ef --- /dev/null +++ b/src/SeoToolkit.Tests/SeoToolkit.Tests/SeoToolkit.Tests.csproj @@ -0,0 +1,24 @@ + + + + net7.0 + enable + enable + + false + + + + + + + + + + + + + + + + diff --git a/src/SeoToolkit.Tests/SeoToolkit.Tests/Usings.cs b/src/SeoToolkit.Tests/SeoToolkit.Tests/Usings.cs new file mode 100644 index 00000000..cefced49 --- /dev/null +++ b/src/SeoToolkit.Tests/SeoToolkit.Tests/Usings.cs @@ -0,0 +1 @@ +global using NUnit.Framework; \ No newline at end of file diff --git a/src/SeoToolkit.Umbraco.Redirects.Core/Services/RedirectsService.cs b/src/SeoToolkit.Umbraco.Redirects.Core/Services/RedirectsService.cs index 45cd5a6a..fc631eb6 100644 --- a/src/SeoToolkit.Umbraco.Redirects.Core/Services/RedirectsService.cs +++ b/src/SeoToolkit.Umbraco.Redirects.Core/Services/RedirectsService.cs @@ -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, diff --git a/src/SeoToolkit.Umbraco.sln b/src/SeoToolkit.Umbraco.sln index 86d06fb2..f0be71e8 100644 --- a/src/SeoToolkit.Umbraco.sln +++ b/src/SeoToolkit.Umbraco.sln @@ -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 @@ -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 @@ -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}