Files
Umbraco-CMS/tests/Umbraco.Tests.UnitTests/Umbraco.Core/DeliveryApi/PublishedContentCacheTests.cs
Mole 1258962429 V15: Remove Nucache (#17166)
* Remove nucache reference from Web.Common

* Get tests building-ish

* Move ReservedFieldNamesService to the right project

* Remove IPublishedSnapshotStatus

* Added functionality to the INavigationQueryService to get root keys

* Fixed issue with navigation

* Remove IPublishedSnapshot from UmbracoContext

* Begin removing usage of IPublishedSnapshot from PublishedContentExtensions

* Fix PublishedContentExtensions.cs

* Don't use snapshots in delivery media api

* Use IPublishedMediaCache in QueryMediaApiController

* Remove more usages of IPublishedSnapshotAccessor

* Comment out tests

* Remove more usages of PublishedSnapshotAccessor

* Remove PublishedSnapshot from property

* Fixed test build

* Fix errors

* Fix some tests

* Delete NuCache 🎉

* Implement DatabaseCacheRebuilder

* Remove usage of IPublishedSnapshotService

* Remove IPublishedSnapshotService

* Remove TestPublishedSnapshotAccessor and make tests build

* Don't test Snapshot cachelevel

It's no longer supported

* Fix BlockEditorConverter

Element != Element document type

* Remember to set cachemanager

* Fix RichTextParserTests

* Implement TryGetLevel on INavigationQueryService

* Fake level and obsolete it in PublishedContent

* Remove ChildrenForAllCultures

* Hack Path property on PublishedContent

* Remove usages of IPublishedSnapshot in tests

* More ConvertersTests

* Add hybrid cache to integration tests

We can actually do this now because we no longer save files on disk

* Rename IPublishedSnapshotRebuilder to ICacheRebuilder

* Comment out tests

* V15: Replacing the usages of Parent (navigation data) from IPublishedContent (#17125)

* Fix .Parent references in PublishedContentExtensions

* Add missing methods to FriendlyPublishedContentExtensions (ones that you were able to call on the content directly as they now require extra params)

* Fix references from the extension methods

* Fix dependencies in tests

* Replace IPublishedSnapshotAccessor with the content cache in tests

* Resolving more .Parent references

* Fix unit tests

* Obsolete and use extension methods

* Remove private method and use extension instead

* Moving code around

* Fix tests

* Fix more references

* Cleanup

* Fix more usages

* Resolve merge conflict

* Fix tests

* Cleanup

* Fix more tests

* Fixed unit tests

* Cleanup

* Replace last usages

---------

Co-authored-by: Bjarke Berg <mail@bergmania.dk>

* Remove usage of IPublishedSnapshotAccessor from IRequestItemProvider

* Post merge fixup

* Remo IPublishedSnapshot

* Add HasAny to IDocumentUrlService

* Fix TextBuilder

* Fix modelsbuilder tests

* Use explicit types

* Implement GetByContentType

* Support element types in PublishedContentTypeCache

* Run enlistments before publishing notifications

* Fix elements cache refreshing

* Implement GetByUdi

* Implement GetAtRoot

* Implement GetByRoute

* Reimplement GetRouteById

* Fix blocks unit tests

* Initialize domain cache on boot

* Only return routes with domains on non default lanauges

* V15: Replacing the usages of `Children` (navigation data) from `IPublishedContent` (#17159)

* Update params in PublishedContentExtensions to the general interfaces for the published cache and navigation service, so that we can use the extension methods on both documents and media

* Introduce GetParent() which uses the right services

* Fix obsolete message on .Parent

* Obsolete .Children

* Fix usages of Children for ApiMediaQueryService

* Fix usage in internal

* Fix usages in views

* Fix indentation

* Fix issue with delete language

* Update nuget pacakges

* Clear elements cache when content is deleted

instead of trying to update it

* Reset publishedModelFactory

* Fixed publishing

---------

Co-authored-by: Bjarke Berg <mail@bergmania.dk>
Co-authored-by: Elitsa Marinovska <21998037+elit0451@users.noreply.github.com>
Co-authored-by: kjac <kja@umbraco.dk>
2024-10-01 15:03:02 +02:00

215 lines
9.6 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;
using Umbraco.Cms.Infrastructure.HybridCache;
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 IPublishedContentCache _contentCache;
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);
_contentCache = contentCacheMock.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;
}
}