From 379d361487b2c4b18ae0830d6457cfda561fda32 Mon Sep 17 00:00:00 2001 From: Andy Butland Date: Fri, 4 Jul 2025 08:02:00 +0200 Subject: [PATCH 1/3] Passes the preview flag to the cache retrieval when resolving the delivery API object for the MNTP property editor. --- .../ValueConverters/MultiNodeTreePickerValueConverter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Umbraco.Core/PropertyEditors/ValueConverters/MultiNodeTreePickerValueConverter.cs b/src/Umbraco.Core/PropertyEditors/ValueConverters/MultiNodeTreePickerValueConverter.cs index 96e84bfb836f..57ceaf965658 100644 --- a/src/Umbraco.Core/PropertyEditors/ValueConverters/MultiNodeTreePickerValueConverter.cs +++ b/src/Umbraco.Core/PropertyEditors/ValueConverters/MultiNodeTreePickerValueConverter.cs @@ -205,7 +205,7 @@ public Type GetDeliveryApiPropertyValueType(IPublishedPropertyType propertyType) { Constants.UdiEntityType.Document => entityTypeUdis.Select(udi => { - IPublishedContent? content = _contentCache.GetById(udi.Guid); + IPublishedContent? content = _contentCache.GetById(preview, udi.Guid); return content != null ? _apiContentBuilder.Build(content) : null; From 0ac7f76474f8f72ab1646d1edb71a129a5483890 Mon Sep 17 00:00:00 2001 From: Andy Butland Date: Fri, 4 Jul 2025 08:33:21 +0200 Subject: [PATCH 2/3] Added unit test verifying fix and adjusted mocks for tests to acoomodate. --- .../MultiNodeTreePickerValueConverterTests.cs | 26 +++++++++++++++++-- .../OutputExpansionStrategyTestBase.cs | 2 +- .../PropertyValueConverterTests.cs | 26 +++++++++++++++++-- 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/tests/Umbraco.Tests.UnitTests/Umbraco.Core/DeliveryApi/MultiNodeTreePickerValueConverterTests.cs b/tests/Umbraco.Tests.UnitTests/Umbraco.Core/DeliveryApi/MultiNodeTreePickerValueConverterTests.cs index 59dd0cee8740..4c93f44ce573 100644 --- a/tests/Umbraco.Tests.UnitTests/Umbraco.Core/DeliveryApi/MultiNodeTreePickerValueConverterTests.cs +++ b/tests/Umbraco.Tests.UnitTests/Umbraco.Core/DeliveryApi/MultiNodeTreePickerValueConverterTests.cs @@ -73,7 +73,7 @@ public void MultiNodeTreePickerValueConverter_InMultiMode_ConvertsValueToListOfC var otherContentKey = Guid.NewGuid(); var otherContent = SetupPublishedContent("The other page", otherContentKey, PublishedItemType.Content, PublishedContentType); - RegisterContentWithProviders(otherContent.Object); + RegisterContentWithProviders(otherContent.Object, false); var valueConverter = MultiNodeTreePickerValueConverter(); @@ -94,6 +94,28 @@ public void MultiNodeTreePickerValueConverter_InMultiMode_ConvertsValueToListOfC Assert.AreEqual("TheContentType", result.Last().ContentType); } + [Test] + public void MultiNodeTreePickerValueConverter_InSingleMode_WithPreview_ConvertsValueToListOfContent() + { + var publishedDataType = MultiNodePickerPublishedDataType(false, Constants.UdiEntityType.Document); + var publishedPropertyType = new Mock(); + publishedPropertyType.SetupGet(p => p.DataType).Returns(publishedDataType); + + var valueConverter = MultiNodeTreePickerValueConverter(); + + Assert.AreEqual(typeof(IEnumerable), valueConverter.GetDeliveryApiPropertyValueType(publishedPropertyType.Object)); + + var inter = new Udi[] { new GuidUdi(Constants.UdiEntityType.Document, DraftContent.Key) }; + var result = valueConverter.ConvertIntermediateToDeliveryApiObject(Mock.Of(), publishedPropertyType.Object, PropertyCacheLevel.Element, inter, true, false) as IEnumerable; + Assert.NotNull(result); + Assert.AreEqual(1, result.Count()); + Assert.AreEqual(DraftContent.Name, result.First().Name); + Assert.AreEqual(DraftContent.Key, result.First().Id); + Assert.AreEqual("/the-draft-page-url/", result.First().Route.Path); + Assert.AreEqual("TheContentType", result.First().ContentType); + Assert.IsEmpty(result.First().Properties); + } + [Test] [TestCase(Constants.UdiEntityType.Document)] [TestCase("content")] @@ -113,7 +135,7 @@ public void MultiNodeTreePickerValueConverter_RendersContentProperties(string en .Setup(p => p.GetUrl(content.Object, It.IsAny(), It.IsAny(), It.IsAny())) .Returns(content.Object.UrlSegment); PublishedContentCacheMock - .Setup(pcc => pcc.GetById(key)) + .Setup(pcc => pcc.GetById(false, key)) .Returns(content.Object); var publishedDataType = MultiNodePickerPublishedDataType(false, entityType); diff --git a/tests/Umbraco.Tests.UnitTests/Umbraco.Core/DeliveryApi/OutputExpansionStrategyTestBase.cs b/tests/Umbraco.Tests.UnitTests/Umbraco.Core/DeliveryApi/OutputExpansionStrategyTestBase.cs index 3051bced8aeb..0f6c3eb16827 100644 --- a/tests/Umbraco.Tests.UnitTests/Umbraco.Core/DeliveryApi/OutputExpansionStrategyTestBase.cs +++ b/tests/Umbraco.Tests.UnitTests/Umbraco.Core/DeliveryApi/OutputExpansionStrategyTestBase.cs @@ -327,7 +327,7 @@ protected void SetupContentMock(Mock content, params IPublish var urlSegment = "url-segment"; ConfigurePublishedContentMock(content, key, name, urlSegment, _contentType, properties); - RegisterContentWithProviders(content.Object); + RegisterContentWithProviders(content.Object, false); } protected void SetupMediaMock(Mock media, params IPublishedProperty[] properties) diff --git a/tests/Umbraco.Tests.UnitTests/Umbraco.Core/DeliveryApi/PropertyValueConverterTests.cs b/tests/Umbraco.Tests.UnitTests/Umbraco.Core/DeliveryApi/PropertyValueConverterTests.cs index 59238902f71a..23dbd79d9c9b 100644 --- a/tests/Umbraco.Tests.UnitTests/Umbraco.Core/DeliveryApi/PropertyValueConverterTests.cs +++ b/tests/Umbraco.Tests.UnitTests/Umbraco.Core/DeliveryApi/PropertyValueConverterTests.cs @@ -23,6 +23,8 @@ public class PropertyValueConverterTests : DeliveryApiTests protected IPublishedContent PublishedMedia { get; private set; } + protected IPublishedContent DraftContent { get; private set; } + protected IPublishedContentType PublishedContentType { get; private set; } protected IPublishedContentType PublishedMediaType { get; private set; } @@ -59,10 +61,21 @@ public override void Setup() var publishedMedia = SetupPublishedContent("The media", mediaKey, PublishedItemType.Media, publishedMediaType.Object); PublishedMedia = publishedMedia.Object; + var draftContentKey = Guid.NewGuid(); + var draftContent = SetupPublishedContent("The page (draft)", draftContentKey, PublishedItemType.Content, publishedContentType.Object); + DraftContent = draftContent.Object; + PublishedContentCacheMock = new Mock(); PublishedContentCacheMock .Setup(pcc => pcc.GetById(contentKey)) .Returns(publishedContent.Object); + PublishedContentCacheMock + .Setup(pcc => pcc.GetById(false, contentKey)) + .Returns(publishedContent.Object); + PublishedContentCacheMock + .Setup(pcc => pcc.GetById(true, draftContentKey)) + .Returns(draftContent.Object); + PublishedMediaCacheMock = new Mock(); PublishedMediaCacheMock .Setup(pcc => pcc.GetById(mediaKey)) @@ -77,6 +90,9 @@ public override void Setup() PublishedUrlProviderMock .Setup(p => p.GetUrl(publishedContent.Object, It.IsAny(), It.IsAny(), It.IsAny())) .Returns("the-page-url"); + PublishedUrlProviderMock + .Setup(p => p.GetUrl(draftContent.Object, It.IsAny(), It.IsAny(), It.IsAny())) + .Returns("the-draft-page-url"); PublishedUrlProviderMock .Setup(p => p.GetMediaUrl(publishedMedia.Object, It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) .Returns("the-media-url"); @@ -97,14 +113,20 @@ protected Mock SetupPublishedContent(string name, Guid key, P return content; } - protected void RegisterContentWithProviders(IPublishedContent content) + protected void RegisterContentWithProviders(IPublishedContent content, bool preview) { PublishedUrlProviderMock .Setup(p => p.GetUrl(content, It.IsAny(), It.IsAny(), It.IsAny())) .Returns(content.UrlSegment); PublishedContentCacheMock - .Setup(pcc => pcc.GetById(content.Key)) + .Setup(pcc => pcc.GetById(preview, content.Key)) .Returns(content); + if (preview is false) + { + PublishedContentCacheMock + .Setup(pcc => pcc.GetById(content.Key)) + .Returns(content); + } } protected void RegisterMediaWithProviders(IPublishedContent media) From a7cda01e923a6c76e2f2682459131032625a07b6 Mon Sep 17 00:00:00 2001 From: Andy Butland Date: Mon, 7 Jul 2025 06:43:32 +0200 Subject: [PATCH 3/3] Provided preview flag for Razor rendering. --- .../ValueConverters/MultiNodeTreePickerValueConverter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Umbraco.Core/PropertyEditors/ValueConverters/MultiNodeTreePickerValueConverter.cs b/src/Umbraco.Core/PropertyEditors/ValueConverters/MultiNodeTreePickerValueConverter.cs index 57ceaf965658..727c28f0777c 100644 --- a/src/Umbraco.Core/PropertyEditors/ValueConverters/MultiNodeTreePickerValueConverter.cs +++ b/src/Umbraco.Core/PropertyEditors/ValueConverters/MultiNodeTreePickerValueConverter.cs @@ -115,7 +115,7 @@ public override Type GetPropertyValueType(IPublishedPropertyType propertyType) udi, ref objectType, UmbracoObjectTypes.Document, - id => _contentCache.GetById(guidUdi.Guid)); + id => _contentCache.GetById(preview, guidUdi.Guid)); break; case Constants.UdiEntityType.Media: multiNodeTreePickerItem = GetPublishedContent(