Skip to content

Commit 8b4849b

Browse files
authored
Fix issue with preview in delivery API for MNTP property editor (#19668)
* Passes the preview flag to the cache retrieval when resolving the delivery API object for the MNTP property editor. * Added unit test verifying fix and adjusted mocks for tests to acoomodate. * Provided preview flag for Razor rendering.
1 parent 7b929a7 commit 8b4849b

File tree

4 files changed

+51
-7
lines changed

4 files changed

+51
-7
lines changed

src/Umbraco.Core/PropertyEditors/ValueConverters/MultiNodeTreePickerValueConverter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public override Type GetPropertyValueType(IPublishedPropertyType propertyType)
115115
udi,
116116
ref objectType,
117117
UmbracoObjectTypes.Document,
118-
id => _contentCache.GetById(guidUdi.Guid));
118+
id => _contentCache.GetById(preview, guidUdi.Guid));
119119
break;
120120
case Constants.UdiEntityType.Media:
121121
multiNodeTreePickerItem = GetPublishedContent(
@@ -205,7 +205,7 @@ public Type GetDeliveryApiPropertyValueType(IPublishedPropertyType propertyType)
205205
{
206206
Constants.UdiEntityType.Document => entityTypeUdis.Select(udi =>
207207
{
208-
IPublishedContent? content = _contentCache.GetById(udi.Guid);
208+
IPublishedContent? content = _contentCache.GetById(preview, udi.Guid);
209209
return content != null
210210
? _apiContentBuilder.Build(content)
211211
: null;

tests/Umbraco.Tests.UnitTests/Umbraco.Core/DeliveryApi/MultiNodeTreePickerValueConverterTests.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public void MultiNodeTreePickerValueConverter_InMultiMode_ConvertsValueToListOfC
7373

7474
var otherContentKey = Guid.NewGuid();
7575
var otherContent = SetupPublishedContent("The other page", otherContentKey, PublishedItemType.Content, PublishedContentType);
76-
RegisterContentWithProviders(otherContent.Object);
76+
RegisterContentWithProviders(otherContent.Object, false);
7777

7878
var valueConverter = MultiNodeTreePickerValueConverter();
7979

@@ -94,6 +94,28 @@ public void MultiNodeTreePickerValueConverter_InMultiMode_ConvertsValueToListOfC
9494
Assert.AreEqual("TheContentType", result.Last().ContentType);
9595
}
9696

97+
[Test]
98+
public void MultiNodeTreePickerValueConverter_InSingleMode_WithPreview_ConvertsValueToListOfContent()
99+
{
100+
var publishedDataType = MultiNodePickerPublishedDataType(false, Constants.UdiEntityType.Document);
101+
var publishedPropertyType = new Mock<IPublishedPropertyType>();
102+
publishedPropertyType.SetupGet(p => p.DataType).Returns(publishedDataType);
103+
104+
var valueConverter = MultiNodeTreePickerValueConverter();
105+
106+
Assert.AreEqual(typeof(IEnumerable<IApiContent>), valueConverter.GetDeliveryApiPropertyValueType(publishedPropertyType.Object));
107+
108+
var inter = new Udi[] { new GuidUdi(Constants.UdiEntityType.Document, DraftContent.Key) };
109+
var result = valueConverter.ConvertIntermediateToDeliveryApiObject(Mock.Of<IPublishedElement>(), publishedPropertyType.Object, PropertyCacheLevel.Element, inter, true, false) as IEnumerable<IApiContent>;
110+
Assert.NotNull(result);
111+
Assert.AreEqual(1, result.Count());
112+
Assert.AreEqual(DraftContent.Name, result.First().Name);
113+
Assert.AreEqual(DraftContent.Key, result.First().Id);
114+
Assert.AreEqual("/the-draft-page-url/", result.First().Route.Path);
115+
Assert.AreEqual("TheContentType", result.First().ContentType);
116+
Assert.IsEmpty(result.First().Properties);
117+
}
118+
97119
[Test]
98120
[TestCase(Constants.UdiEntityType.Document)]
99121
[TestCase("content")]
@@ -113,7 +135,7 @@ public void MultiNodeTreePickerValueConverter_RendersContentProperties(string en
113135
.Setup(p => p.GetUrl(content.Object, It.IsAny<UrlMode>(), It.IsAny<string?>(), It.IsAny<Uri?>()))
114136
.Returns(content.Object.UrlSegment);
115137
PublishedContentCacheMock
116-
.Setup(pcc => pcc.GetById(key))
138+
.Setup(pcc => pcc.GetById(false, key))
117139
.Returns(content.Object);
118140

119141
var publishedDataType = MultiNodePickerPublishedDataType(false, entityType);

tests/Umbraco.Tests.UnitTests/Umbraco.Core/DeliveryApi/OutputExpansionStrategyTestBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ protected void SetupContentMock(Mock<IPublishedContent> content, params IPublish
327327
var urlSegment = "url-segment";
328328
ConfigurePublishedContentMock(content, key, name, urlSegment, _contentType, properties);
329329

330-
RegisterContentWithProviders(content.Object);
330+
RegisterContentWithProviders(content.Object, false);
331331
}
332332

333333
protected void SetupMediaMock(Mock<IPublishedContent> media, params IPublishedProperty[] properties)

tests/Umbraco.Tests.UnitTests/Umbraco.Core/DeliveryApi/PropertyValueConverterTests.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public class PropertyValueConverterTests : DeliveryApiTests
2323

2424
protected IPublishedContent PublishedMedia { get; private set; }
2525

26+
protected IPublishedContent DraftContent { get; private set; }
27+
2628
protected IPublishedContentType PublishedContentType { get; private set; }
2729

2830
protected IPublishedContentType PublishedMediaType { get; private set; }
@@ -59,10 +61,21 @@ public override void Setup()
5961
var publishedMedia = SetupPublishedContent("The media", mediaKey, PublishedItemType.Media, publishedMediaType.Object);
6062
PublishedMedia = publishedMedia.Object;
6163

64+
var draftContentKey = Guid.NewGuid();
65+
var draftContent = SetupPublishedContent("The page (draft)", draftContentKey, PublishedItemType.Content, publishedContentType.Object);
66+
DraftContent = draftContent.Object;
67+
6268
PublishedContentCacheMock = new Mock<IPublishedContentCache>();
6369
PublishedContentCacheMock
6470
.Setup(pcc => pcc.GetById(contentKey))
6571
.Returns(publishedContent.Object);
72+
PublishedContentCacheMock
73+
.Setup(pcc => pcc.GetById(false, contentKey))
74+
.Returns(publishedContent.Object);
75+
PublishedContentCacheMock
76+
.Setup(pcc => pcc.GetById(true, draftContentKey))
77+
.Returns(draftContent.Object);
78+
6679
PublishedMediaCacheMock = new Mock<IPublishedMediaCache>();
6780
PublishedMediaCacheMock
6881
.Setup(pcc => pcc.GetById(mediaKey))
@@ -77,6 +90,9 @@ public override void Setup()
7790
PublishedUrlProviderMock
7891
.Setup(p => p.GetUrl(publishedContent.Object, It.IsAny<UrlMode>(), It.IsAny<string?>(), It.IsAny<Uri?>()))
7992
.Returns("the-page-url");
93+
PublishedUrlProviderMock
94+
.Setup(p => p.GetUrl(draftContent.Object, It.IsAny<UrlMode>(), It.IsAny<string?>(), It.IsAny<Uri?>()))
95+
.Returns("the-draft-page-url");
8096
PublishedUrlProviderMock
8197
.Setup(p => p.GetMediaUrl(publishedMedia.Object, It.IsAny<UrlMode>(), It.IsAny<string?>(), It.IsAny<string?>(), It.IsAny<Uri?>()))
8298
.Returns("the-media-url");
@@ -97,14 +113,20 @@ protected Mock<IPublishedContent> SetupPublishedContent(string name, Guid key, P
97113
return content;
98114
}
99115

100-
protected void RegisterContentWithProviders(IPublishedContent content)
116+
protected void RegisterContentWithProviders(IPublishedContent content, bool preview)
101117
{
102118
PublishedUrlProviderMock
103119
.Setup(p => p.GetUrl(content, It.IsAny<UrlMode>(), It.IsAny<string?>(), It.IsAny<Uri?>()))
104120
.Returns(content.UrlSegment);
105121
PublishedContentCacheMock
106-
.Setup(pcc => pcc.GetById(content.Key))
122+
.Setup(pcc => pcc.GetById(preview, content.Key))
107123
.Returns(content);
124+
if (preview is false)
125+
{
126+
PublishedContentCacheMock
127+
.Setup(pcc => pcc.GetById(content.Key))
128+
.Returns(content);
129+
}
108130
}
109131

110132
protected void RegisterMediaWithProviders(IPublishedContent media)

0 commit comments

Comments
 (0)