Files
Umbraco-CMS/tests/Umbraco.Tests.UnitTests/Umbraco.Core/DeliveryApi/PublishedContentCacheTests.cs
Bjarke Berg 734b3cce2c Determine urls at save and publish time (#17033)
* Started work on service

* temp work

* temp commit

* Temp commit

* Added more routing logic

* Fixed tests

* Refactor and prepare for isdraft

* Work on drafts

* Fixed tests

* Move to enlistment to ensure caches is only updated on scope complete

* Clean up and handle null cultures

* Added functionality to the INavigationQueryService to get root keys

* Added migration

* Fixed issue with navigation

* Added migration

* Temp commit, move to cache refreshers.

* Fixed issues

* List urls

* fix build

* Fixed integration tests

* Refactor to create new content finder instead of changing the old

* rollback wrong commited line

* Clean up, and use docuemnt url service for index

* Fixed List endpoin

* Do not use Navigation service in methods intended by management api

* Fixed examine tests

* Make methods virtual

* Use domain from published request

* Use hybrid cache from new content finder

* Eliminate nucache usage

* Fixed issue with delivery api and url generation

* Fixed linux tests

* Added hybrid cache to all integration tests
2024-09-27 09:12:19 +02:00

221 lines
10 KiB
C#

using Microsoft.Extensions.Options;
using Moq;
using NUnit.Framework;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.DeliveryApi;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.PublishedCache;
using Umbraco.Cms.Core.Services;
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.DeliveryApi;
[TestFixture]
public class PublishedContentCacheTests : DeliveryApiTests
{
private readonly Guid _contentOneId = Guid.Parse("19AEAC73-DB4E-4CFC-AB06-0AD14A89A613");
private readonly Guid _contentTwoId = Guid.Parse("4EF11E1E-FB50-4627-8A86-E10ED6F4DCE4");
private IPublishedSnapshotAccessor _publishedSnapshotAccessor = null!;
private IPublishedContentCache _contentCacheMock;
private IDocumentUrlService _documentUrlService;
[SetUp]
public void Setup()
{
var contentTypeOneMock = new Mock<IPublishedContentType>();
contentTypeOneMock.SetupGet(m => m.Alias).Returns("theContentType");
var contentOneMock = new Mock<IPublishedContent>();
ConfigurePublishedContentMock(contentOneMock, _contentOneId, "Content One", "content-one", contentTypeOneMock.Object, Array.Empty<IPublishedProperty>());
var contentTypeTwoMock = new Mock<IPublishedContentType>();
contentTypeTwoMock.SetupGet(m => m.Alias).Returns("theOtherContentType");
var contentTwoMock = new Mock<IPublishedContent>();
ConfigurePublishedContentMock(contentTwoMock, _contentTwoId, "Content Two", "content-two", contentTypeTwoMock.Object, Array.Empty<IPublishedProperty>());
var documentUrlService = new Mock<IDocumentUrlService>();
documentUrlService
.Setup(x => x.GetDocumentKeyByRoute("content-one", It.IsAny<string?>(), It.IsAny<int?>(), It.IsAny<bool>()))
.Returns(_contentOneId);
documentUrlService
.Setup(x => x.GetDocumentKeyByRoute("content-two", It.IsAny<string?>(), It.IsAny<int?>(), It.IsAny<bool>()))
.Returns(_contentTwoId);
var contentCacheMock = new Mock<IPublishedContentCache>();
contentCacheMock
.Setup(m => m.GetByRoute(It.IsAny<bool>(), "content-one", null, null))
.Returns(contentOneMock.Object);
contentCacheMock
.Setup(m => m.GetById(It.IsAny<bool>(), _contentOneId))
.Returns(contentOneMock.Object);
contentCacheMock
.Setup(m => m.GetByRoute(It.IsAny<bool>(), "content-two", null, null))
.Returns(contentTwoMock.Object);
contentCacheMock
.Setup(m => m.GetById(It.IsAny<bool>(), _contentTwoId))
.Returns(contentTwoMock.Object);
var publishedSnapshotMock = new Mock<IPublishedSnapshot>();
publishedSnapshotMock.Setup(m => m.Content).Returns(contentCacheMock.Object);
var publishedSnapshot = publishedSnapshotMock.Object;
var publishedSnapshotAccessorMock = new Mock<IPublishedSnapshotAccessor>();
publishedSnapshotAccessorMock.Setup(m => m.TryGetPublishedSnapshot(out publishedSnapshot)).Returns(true);
_publishedSnapshotAccessor = publishedSnapshotAccessorMock.Object;
_contentCacheMock = contentCacheMock.Object;
_documentUrlService = documentUrlService.Object;
}
[Test]
public void PublishedContentCache_CanGetById()
{
var publishedContentCache = new ApiPublishedContentCache(CreateRequestPreviewService(), CreateRequestCultureService(), CreateDeliveryApiSettings(), _documentUrlService, _contentCacheMock);
var content = publishedContentCache.GetById(_contentOneId);
Assert.IsNotNull(content);
Assert.AreEqual(_contentOneId, content.Key);
Assert.AreEqual("content-one", content.UrlSegment);
Assert.AreEqual("theContentType", content.ContentType.Alias);
}
[Test]
public void PublishedContentCache_CanGetByRoute()
{
var publishedContentCache = new ApiPublishedContentCache(CreateRequestPreviewService(), CreateRequestCultureService(), CreateDeliveryApiSettings(), _documentUrlService, _contentCacheMock);
var content = publishedContentCache.GetByRoute("content-two");
Assert.IsNotNull(content);
Assert.AreEqual(_contentTwoId, content.Key);
Assert.AreEqual("content-two", content.UrlSegment);
Assert.AreEqual("theOtherContentType", content.ContentType.Alias);
}
[Test]
public void PublishedContentCache_CanGetByIds()
{
var publishedContentCache = new ApiPublishedContentCache(CreateRequestPreviewService(), CreateRequestCultureService(), CreateDeliveryApiSettings(), _documentUrlService, _contentCacheMock);
var content = publishedContentCache.GetByIds(new[] { _contentOneId, _contentTwoId }).ToArray();
Assert.AreEqual(2, content.Length);
Assert.AreEqual(_contentOneId, content.First().Key);
Assert.AreEqual(_contentTwoId, content.Last().Key);
}
[TestCase(true)]
[TestCase(false)]
public void PublishedContentCache_GetById_SupportsDenyList(bool denied)
{
var denyList = denied ? new[] { "theOtherContentType" } : null;
var publishedContentCache = new ApiPublishedContentCache(CreateRequestPreviewService(), CreateRequestCultureService(), CreateDeliveryApiSettings(denyList), _documentUrlService, _contentCacheMock);
var content = publishedContentCache.GetById(_contentTwoId);
if (denied)
{
Assert.IsNull(content);
}
else
{
Assert.IsNotNull(content);
}
}
[TestCase(true)]
[TestCase(false)]
public void PublishedContentCache_GetByRoute_SupportsDenyList(bool denied)
{
var denyList = denied ? new[] { "theContentType" } : null;
var publishedContentCache = new ApiPublishedContentCache(CreateRequestPreviewService(), CreateRequestCultureService(), CreateDeliveryApiSettings(denyList), _documentUrlService, _contentCacheMock);
var content = publishedContentCache.GetByRoute("content-one");
if (denied)
{
Assert.IsNull(content);
}
else
{
Assert.IsNotNull(content);
}
}
[TestCase("theContentType")]
[TestCase("theOtherContentType")]
public void PublishedContentCache_GetByIds_SupportsDenyList(string deniedContentType)
{
var denyList = new[] { deniedContentType };
var publishedContentCache = new ApiPublishedContentCache(CreateRequestPreviewService(), CreateRequestCultureService(), CreateDeliveryApiSettings(denyList), _documentUrlService, _contentCacheMock);
var content = publishedContentCache.GetByIds(new[] { _contentOneId, _contentTwoId }).ToArray();
Assert.AreEqual(1, content.Length);
if (deniedContentType == "theContentType")
{
Assert.AreEqual(_contentTwoId, content.First().Key);
}
else
{
Assert.AreEqual(_contentOneId, content.First().Key);
}
}
[Test]
public void PublishedContentCache_GetById_CanRetrieveContentTypesOutsideTheDenyList()
{
var denyList = new[] { "theContentType" };
var publishedContentCache = new ApiPublishedContentCache(CreateRequestPreviewService(), CreateRequestCultureService(), CreateDeliveryApiSettings(denyList), _documentUrlService, _contentCacheMock);
var content = publishedContentCache.GetById(_contentTwoId);
Assert.IsNotNull(content);
Assert.AreEqual(_contentTwoId, content.Key);
Assert.AreEqual("content-two", content.UrlSegment);
Assert.AreEqual("theOtherContentType", content.ContentType.Alias);
}
[Test]
public void PublishedContentCache_GetByRoute_CanRetrieveContentTypesOutsideTheDenyList()
{
var denyList = new[] { "theOtherContentType" };
var publishedContentCache = new ApiPublishedContentCache(CreateRequestPreviewService(), CreateRequestCultureService(), CreateDeliveryApiSettings(denyList), _documentUrlService, _contentCacheMock);
var content = publishedContentCache.GetByRoute("content-one");
Assert.IsNotNull(content);
Assert.AreEqual(_contentOneId, content.Key);
Assert.AreEqual("content-one", content.UrlSegment);
Assert.AreEqual("theContentType", content.ContentType.Alias);
}
[Test]
public void PublishedContentCache_GetByIds_CanDenyAllRequestedContent()
{
var denyList = new[] { "theContentType", "theOtherContentType" };
var publishedContentCache = new ApiPublishedContentCache(CreateRequestPreviewService(), CreateRequestCultureService(), CreateDeliveryApiSettings(denyList), _documentUrlService, _contentCacheMock);
var content = publishedContentCache.GetByIds(new[] { _contentOneId, _contentTwoId }).ToArray();
Assert.IsEmpty(content);
}
[Test]
public void PublishedContentCache_DenyListIsCaseInsensitive()
{
var denyList = new[] { "THEcontentTYPE" };
var publishedContentCache = new ApiPublishedContentCache(CreateRequestPreviewService(), CreateRequestCultureService(), CreateDeliveryApiSettings(denyList), _documentUrlService, _contentCacheMock);
var content = publishedContentCache.GetByRoute("content-one");
Assert.IsNull(content);
}
private IRequestCultureService CreateRequestCultureService()
{
var mock = new Mock<IRequestCultureService>();
return mock.Object;
}
private IRequestPreviewService CreateRequestPreviewService(bool isPreview = false)
{
var previewServiceMock = new Mock<IRequestPreviewService>();
previewServiceMock.Setup(m => m.IsPreview()).Returns(isPreview);
return previewServiceMock.Object;
}
private IOptionsMonitor<DeliveryApiSettings> CreateDeliveryApiSettings(string[]? disallowedContentTypeAliases = null)
{
var deliveryApiSettings = new DeliveryApiSettings { DisallowedContentTypeAliases = disallowedContentTypeAliases ?? Array.Empty<string>() };
var deliveryApiOptionsMonitorMock = new Mock<IOptionsMonitor<DeliveryApiSettings>>();
deliveryApiOptionsMonitorMock.SetupGet(s => s.CurrentValue).Returns(deliveryApiSettings);
return deliveryApiOptionsMonitorMock.Object;
}
}