|
| 1 | +using Microsoft.Extensions.Logging.Abstractions; |
| 2 | +using Microsoft.Extensions.Options; |
| 3 | +using Moq; |
| 4 | +using NUnit.Framework; |
| 5 | +using Umbraco.Cms.Core.Configuration.Models; |
| 6 | +using Umbraco.Cms.Core.Models.PublishedContent; |
| 7 | +using Umbraco.Cms.Core.PublishedCache; |
| 8 | +using Umbraco.Cms.Core.Routing; |
| 9 | +using Umbraco.Cms.Core.Services; |
| 10 | +using Umbraco.Cms.Core.Web; |
| 11 | + |
| 12 | +namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Routing; |
| 13 | + |
| 14 | +[TestFixture] |
| 15 | +public class ContentFinderByUrlNewTests |
| 16 | +{ |
| 17 | + private const int DomainContentId = 1233; |
| 18 | + private const int ContentId = 1234; |
| 19 | + private static readonly Guid _contentKey = Guid.NewGuid(); |
| 20 | + private const string ContentPath = "/test-page"; |
| 21 | + private const string DomainHost = "example.com"; |
| 22 | + |
| 23 | + [TestCase(ContentPath, true)] |
| 24 | + [TestCase("/missing-page", false)] |
| 25 | + public async Task Can_Find_Invariant_Content(string path, bool expectSuccess) |
| 26 | + { |
| 27 | + var mockContent = CreateMockPublishedContent(); |
| 28 | + |
| 29 | + var mockUmbracoContextAccessor = CreateMockUmbracoContextAccessor(); |
| 30 | + |
| 31 | + var mockDocumentUrlService = CreateMockDocumentUrlService(); |
| 32 | + |
| 33 | + var mockPublishedContentCache = CreateMockPublishedContentCache(mockContent); |
| 34 | + |
| 35 | + var sut = CreateContentFinder(mockUmbracoContextAccessor, mockDocumentUrlService, mockPublishedContentCache); |
| 36 | + |
| 37 | + var publishedRequestBuilder = CreatePublishedRequestBuilder(path); |
| 38 | + |
| 39 | + var result = await sut.TryFindContent(publishedRequestBuilder); |
| 40 | + |
| 41 | + Assert.AreEqual(expectSuccess, result); |
| 42 | + if (expectSuccess) |
| 43 | + { |
| 44 | + Assert.IsNotNull(publishedRequestBuilder.PublishedContent); |
| 45 | + } |
| 46 | + else |
| 47 | + { |
| 48 | + Assert.IsNull(publishedRequestBuilder.PublishedContent); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + [TestCase(ContentPath, true, false, true)] |
| 53 | + [TestCase("/missing-page", true, false, false)] |
| 54 | + [TestCase(ContentPath, true, true, true)] |
| 55 | + [TestCase(ContentPath, false, true, false)] |
| 56 | + public async Task Can_Find_Invariant_Content_With_Domain(string path, bool setDomain, bool useStrictDomainMatching, bool expectSuccess) |
| 57 | + { |
| 58 | + var mockContent = CreateMockPublishedContent(); |
| 59 | + |
| 60 | + var mockUmbracoContextAccessor = CreateMockUmbracoContextAccessor(); |
| 61 | + |
| 62 | + var mockDocumentUrlService = CreateMockDocumentUrlService(); |
| 63 | + |
| 64 | + var mockPublishedContentCache = CreateMockPublishedContentCache(mockContent); |
| 65 | + |
| 66 | + var sut = CreateContentFinder( |
| 67 | + mockUmbracoContextAccessor, |
| 68 | + mockDocumentUrlService, |
| 69 | + mockPublishedContentCache, |
| 70 | + new WebRoutingSettings |
| 71 | + { |
| 72 | + UseStrictDomainMatching = useStrictDomainMatching |
| 73 | + }); |
| 74 | + |
| 75 | + var publishedRequestBuilder = CreatePublishedRequestBuilder(path, withDomain: setDomain); |
| 76 | + |
| 77 | + var result = await sut.TryFindContent(publishedRequestBuilder); |
| 78 | + |
| 79 | + Assert.AreEqual(expectSuccess, result); |
| 80 | + if (expectSuccess) |
| 81 | + { |
| 82 | + Assert.IsNotNull(publishedRequestBuilder.PublishedContent); |
| 83 | + } |
| 84 | + else |
| 85 | + { |
| 86 | + Assert.IsNull(publishedRequestBuilder.PublishedContent); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private static Mock<IPublishedContent> CreateMockPublishedContent() |
| 91 | + { |
| 92 | + var mockContent = new Mock<IPublishedContent>(); |
| 93 | + mockContent |
| 94 | + .SetupGet(x => x.Id) |
| 95 | + .Returns(ContentId); |
| 96 | + mockContent |
| 97 | + .SetupGet(x => x.ContentType.ItemType) |
| 98 | + .Returns(PublishedItemType.Content); |
| 99 | + return mockContent; |
| 100 | + } |
| 101 | + |
| 102 | + private static Mock<IUmbracoContextAccessor> CreateMockUmbracoContextAccessor() |
| 103 | + { |
| 104 | + var mockUmbracoContext = new Mock<IUmbracoContext>(); |
| 105 | + var mockUmbracoContextAccessor = new Mock<IUmbracoContextAccessor>(); |
| 106 | + var umbracoContext = mockUmbracoContext.Object; |
| 107 | + mockUmbracoContextAccessor |
| 108 | + .Setup(x => x.TryGetUmbracoContext(out umbracoContext)) |
| 109 | + .Returns(true); |
| 110 | + return mockUmbracoContextAccessor; |
| 111 | + } |
| 112 | + |
| 113 | + private static Mock<IDocumentUrlService> CreateMockDocumentUrlService() |
| 114 | + { |
| 115 | + var mockDocumentUrlService = new Mock<IDocumentUrlService>(); |
| 116 | + mockDocumentUrlService |
| 117 | + .Setup(x => x.GetDocumentKeyByRoute(It.Is<string>(y => y == ContentPath), It.IsAny<string?>(), It.IsAny<int?>(), It.IsAny<bool>())) |
| 118 | + .Returns(_contentKey); |
| 119 | + return mockDocumentUrlService; |
| 120 | + } |
| 121 | + |
| 122 | + private static Mock<IPublishedContentCache> CreateMockPublishedContentCache(Mock<IPublishedContent> mockContent) |
| 123 | + { |
| 124 | + var mockPublishedContentCache = new Mock<IPublishedContentCache>(); |
| 125 | + mockPublishedContentCache |
| 126 | + .Setup(x => x.GetById(It.IsAny<bool>(), It.Is<Guid>(y => y == _contentKey))) |
| 127 | + .Returns(mockContent.Object); |
| 128 | + return mockPublishedContentCache; |
| 129 | + } |
| 130 | + |
| 131 | + private static ContentFinderByUrlNew CreateContentFinder( |
| 132 | + Mock<IUmbracoContextAccessor> mockUmbracoContextAccessor, |
| 133 | + Mock<IDocumentUrlService> mockDocumentUrlService, |
| 134 | + Mock<IPublishedContentCache> mockPublishedContentCache, |
| 135 | + WebRoutingSettings? webRoutingSettings = null) |
| 136 | + => new( |
| 137 | + new NullLogger<ContentFinderByUrlNew>(), |
| 138 | + mockUmbracoContextAccessor.Object, |
| 139 | + mockDocumentUrlService.Object, |
| 140 | + mockPublishedContentCache.Object, |
| 141 | + Mock.Of<IOptionsMonitor<WebRoutingSettings>>(x => x.CurrentValue == (webRoutingSettings ?? new WebRoutingSettings()))); |
| 142 | + |
| 143 | + private static PublishedRequestBuilder CreatePublishedRequestBuilder(string path, bool withDomain = false) |
| 144 | + { |
| 145 | + var publishedRequestBuilder = new PublishedRequestBuilder(new Uri($"https://example.com{path}"), Mock.Of<IFileService>()); |
| 146 | + if (withDomain) |
| 147 | + { |
| 148 | + publishedRequestBuilder.SetDomain(new DomainAndUri(new Domain(1, $"https://{DomainHost}/", DomainContentId, "en-US", false, 0), new Uri($"https://{DomainHost}{path}"))); |
| 149 | + } |
| 150 | + |
| 151 | + return publishedRequestBuilder; |
| 152 | + } |
| 153 | +} |
0 commit comments