|
| 1 | +using Microsoft.Extensions.Logging; |
| 2 | +using Microsoft.Extensions.Logging.Abstractions; |
| 3 | +using Moq; |
| 4 | +using NUnit.Framework; |
| 5 | +using Umbraco.Cms.Core; |
| 6 | +using Umbraco.Cms.Core.Cache; |
| 7 | +using Umbraco.Cms.Core.Models; |
| 8 | +using Umbraco.Cms.Core.Models.PublishedContent; |
| 9 | +using Umbraco.Cms.Core.PublishedCache; |
| 10 | +using Umbraco.Cms.Core.Routing; |
| 11 | +using Umbraco.Cms.Core.Services; |
| 12 | +using Umbraco.Cms.Core.Web; |
| 13 | +using Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement; |
| 14 | +using Umbraco.Cms.Infrastructure.Routing; |
| 15 | +using Umbraco.Cms.Infrastructure.Scoping; |
| 16 | +using Umbraco.Cms.Tests.Common.Testing; |
| 17 | +using Umbraco.Cms.Tests.Integration.Testing; |
| 18 | + |
| 19 | +namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Routing; |
| 20 | + |
| 21 | +[TestFixture] |
| 22 | +[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)] |
| 23 | +public class RedirectTrackerTests : UmbracoIntegrationTestWithContent |
| 24 | +{ |
| 25 | + private IRedirectUrlService RedirectUrlService => GetRequiredService<IRedirectUrlService>(); |
| 26 | + |
| 27 | + private IContent _rootPage; |
| 28 | + private IContent _testPage; |
| 29 | + |
| 30 | + public override void CreateTestData() |
| 31 | + { |
| 32 | + base.CreateTestData(); |
| 33 | + |
| 34 | + var rootContent = ContentService.GetRootContent().First(); |
| 35 | + _rootPage = rootContent; |
| 36 | + var subPages = ContentService.GetPagedChildren(rootContent.Id, 0, 3, out _).ToList(); |
| 37 | + _testPage = subPages[0]; |
| 38 | + } |
| 39 | + |
| 40 | + [Test] |
| 41 | + public void Can_Create_Redirects() |
| 42 | + { |
| 43 | + IDictionary<(int ContentId, string Culture), (Guid ContentKey, string OldRoute)> dict = |
| 44 | + new Dictionary<(int ContentId, string Culture), (Guid ContentKey, string OldRoute)> |
| 45 | + { |
| 46 | + [(_testPage.Id, "en")] = (_testPage.Key, "/old-route"), |
| 47 | + }; |
| 48 | + var redirectTracker = CreateRedirectTracker(); |
| 49 | + |
| 50 | + redirectTracker.CreateRedirects(dict); |
| 51 | + |
| 52 | + var redirects = RedirectUrlService.GetContentRedirectUrls(_testPage.Key); |
| 53 | + Assert.AreEqual(1, redirects.Count()); |
| 54 | + var redirect = redirects.First(); |
| 55 | + Assert.AreEqual("/old-route", redirect.Url); |
| 56 | + } |
| 57 | + |
| 58 | + [Test] |
| 59 | + public void Will_Remove_Self_Referencing_Redirects() |
| 60 | + { |
| 61 | + CreateExistingRedirect(); |
| 62 | + |
| 63 | + var redirects = RedirectUrlService.GetContentRedirectUrls(_testPage.Key); |
| 64 | + Assert.IsTrue(redirects.Any(x => x.Url == "/new-route")); // Ensure self referencing redirect exists. |
| 65 | + |
| 66 | + IDictionary<(int ContentId, string Culture), (Guid ContentKey, string OldRoute)> dict = |
| 67 | + new Dictionary<(int ContentId, string Culture), (Guid ContentKey, string OldRoute)> |
| 68 | + { |
| 69 | + [(_testPage.Id, "en")] = (_testPage.Key, "/old-route"), |
| 70 | + }; |
| 71 | + |
| 72 | + var redirectTracker = CreateRedirectTracker(); |
| 73 | + redirectTracker.CreateRedirects(dict); |
| 74 | + |
| 75 | + redirects = RedirectUrlService.GetContentRedirectUrls(_testPage.Key); |
| 76 | + Assert.AreEqual(1, redirects.Count()); |
| 77 | + var redirect = redirects.First(); |
| 78 | + Assert.AreEqual("/old-route", redirect.Url); |
| 79 | + } |
| 80 | + |
| 81 | + private RedirectUrlRepository CreateRedirectUrlRepository() => |
| 82 | + new( |
| 83 | + (IScopeAccessor)ScopeProvider, |
| 84 | + AppCaches.Disabled, |
| 85 | + new NullLogger<RedirectUrlRepository>()); |
| 86 | + |
| 87 | + private IRedirectTracker CreateRedirectTracker() |
| 88 | + { |
| 89 | + var contentType = new Mock<IPublishedContentType>(); |
| 90 | + contentType.SetupGet(c => c.Variations).Returns(ContentVariation.Nothing); |
| 91 | + |
| 92 | + var cultures = new Dictionary<string, PublishedCultureInfo> |
| 93 | + { |
| 94 | + { "en", new PublishedCultureInfo("en", "english", "/en/", DateTime.UtcNow) }, |
| 95 | + }; |
| 96 | + |
| 97 | + var rootContent = new Mock<IPublishedContent>(); |
| 98 | + rootContent.SetupGet(c => c.Id).Returns(_rootPage.Id); |
| 99 | + rootContent.SetupGet(c => c.Key).Returns(_rootPage.Key); |
| 100 | + rootContent.SetupGet(c => c.Name).Returns(_rootPage.Name); |
| 101 | + rootContent.SetupGet(c => c.Path).Returns(_rootPage.Path); |
| 102 | + |
| 103 | + var content = new Mock<IPublishedContent>(); |
| 104 | + content.SetupGet(c => c.Id).Returns(_testPage.Id); |
| 105 | + content.SetupGet(c => c.Key).Returns(_testPage.Key); |
| 106 | + content.SetupGet(c => c.Name).Returns(_testPage.Name); |
| 107 | + content.SetupGet(c => c.Path).Returns(_testPage.Path); |
| 108 | + content.SetupGet(c => c.ContentType).Returns(contentType.Object); |
| 109 | + content.SetupGet(c => c.Cultures).Returns(cultures); |
| 110 | + |
| 111 | + IPublishedContentCache contentCache = Mock.Of<IPublishedContentCache>(); |
| 112 | + Mock.Get(contentCache) |
| 113 | + .Setup(x => x.GetRouteById(_testPage.Id, "en")) |
| 114 | + .Returns("/new-route"); |
| 115 | + |
| 116 | + UmbracoContextReference contextReference = new UmbracoContextReference(Mock.Of<IUmbracoContext>(), false, Mock.Of<IUmbracoContextAccessor>()); |
| 117 | + Mock.Get(contextReference.UmbracoContext) |
| 118 | + .Setup(x => x.Content) |
| 119 | + .Returns(contentCache); |
| 120 | + |
| 121 | + IUmbracoContextFactory contextFactory = Mock.Of<IUmbracoContextFactory>(); |
| 122 | + Mock.Get(contextFactory) |
| 123 | + .Setup(x => x.EnsureUmbracoContext()) |
| 124 | + .Returns(contextReference); |
| 125 | + |
| 126 | + return new RedirectTracker( |
| 127 | + contextFactory, |
| 128 | + GetRequiredService<IVariationContextAccessor>(), |
| 129 | + GetRequiredService<ILocalizationService>(), |
| 130 | + RedirectUrlService, |
| 131 | + GetRequiredService<ILogger<RedirectTracker>>()); |
| 132 | + } |
| 133 | + |
| 134 | + private void CreateExistingRedirect() |
| 135 | + { |
| 136 | + using var scope = ScopeProvider.CreateScope(); |
| 137 | + var repository = CreateRedirectUrlRepository(); |
| 138 | + repository.Save(new RedirectUrl { ContentKey = _testPage.Key, Url = "/new-route", Culture = "en" }); |
| 139 | + scope.Complete(); |
| 140 | + } |
| 141 | +} |
0 commit comments