Skip to content

Commit ab32bac

Browse files
authored
Property source level variation should only be applied when configured (#16270)
1 parent ba9ddd1 commit ab32bac

2 files changed

Lines changed: 32 additions & 5 deletions

File tree

src/Umbraco.PublishedCache.NuCache/Property.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ internal class Property : PublishedPropertyBase
2525
// the invariant-neutral source and inter values
2626
private readonly object? _sourceValue;
2727
private readonly ContentVariation _variations;
28-
private bool _sourceValueIsInvariant;
28+
private readonly ContentVariation _sourceVariations;
2929

3030
// the variant and non-variant object values
3131
private CacheValues? _cacheValues;
@@ -88,7 +88,7 @@ public Property(
8888
// this variable is used for contextualizing the variation level when calculating property values.
8989
// it must be set to the union of variance (the combination of content type and property type variance).
9090
_variations = propertyType.Variations | content.ContentType.Variations;
91-
_sourceValueIsInvariant = propertyType.Variations is ContentVariation.Nothing;
91+
_sourceVariations = propertyType.Variations;
9292
}
9393

9494
// clone for previewing as draft a published content that is published and has no draft
@@ -104,7 +104,7 @@ public Property(Property origin, PublishedContent content)
104104
_isMember = origin._isMember;
105105
_publishedSnapshotAccessor = origin._publishedSnapshotAccessor;
106106
_variations = origin._variations;
107-
_sourceValueIsInvariant = origin._sourceValueIsInvariant;
107+
_sourceVariations = origin._sourceVariations;
108108
}
109109

110110
// used to cache the CacheValues of this property
@@ -148,9 +148,14 @@ public override bool HasValue(string? culture = null, string? segment = null)
148148

149149
public override object? GetSourceValue(string? culture = null, string? segment = null)
150150
{
151-
_content.VariationContextAccessor.ContextualizeVariation(_variations, _content.Id, ref culture, ref segment);
151+
_content.VariationContextAccessor.ContextualizeVariation(_sourceVariations, _content.Id, ref culture, ref segment);
152+
153+
// source values are tightly bound to the property/schema culture and segment configurations, so we need to
154+
// sanitize the contextualized culture/segment states before using them to access the source values.
155+
culture = _sourceVariations.VariesByCulture() ? culture : string.Empty;
156+
segment = _sourceVariations.VariesBySegment() ? segment : string.Empty;
152157

153-
if (_sourceValueIsInvariant || (culture == string.Empty && segment == string.Empty))
158+
if (culture == string.Empty && segment == string.Empty)
154159
{
155160
return _sourceValue;
156161
}

tests/Umbraco.Tests.UnitTests/Umbraco.Core/Published/PublishedContentVarianceTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,28 @@ public void Content_Culture_And_Segment_Variation_Can_Get_Culture_And_Segment_Va
7676
Assert.AreEqual(expectedValue, value);
7777
}
7878

79+
[TestCase(DaCulture, Segment1, "DaDk property value")]
80+
[TestCase(DaCulture, Segment2, "DaDk property value")]
81+
[TestCase(EnCulture, Segment1, "EnUs property value")]
82+
[TestCase(EnCulture, Segment2, "EnUs property value")]
83+
public void Content_Culture_And_Segment_Variation_Can_Get_Culture_Variant_Property(string culture, string segment, string expectedValue)
84+
{
85+
var content = CreatePublishedContent(ContentVariation.CultureAndSegment, ContentVariation.Culture, variationContextCulture: culture, variationContextSegment: segment);
86+
var value = GetPropertyValue(content);
87+
Assert.AreEqual(expectedValue, value);
88+
}
89+
90+
[TestCase(DaCulture, Segment1, "Segment1 property value")]
91+
[TestCase(DaCulture, Segment2, "Segment2 property value")]
92+
[TestCase(EnCulture, Segment1, "Segment1 property value")]
93+
[TestCase(EnCulture, Segment2, "Segment2 property value")]
94+
public void Content_Culture_And_Segment_Variation_Can_Get_Segment_Variant_Property(string culture, string segment, string expectedValue)
95+
{
96+
var content = CreatePublishedContent(ContentVariation.CultureAndSegment, ContentVariation.Segment, variationContextCulture: culture, variationContextSegment: segment);
97+
var value = GetPropertyValue(content);
98+
Assert.AreEqual(expectedValue, value);
99+
}
100+
79101
private object? GetPropertyValue(IPublishedContent content) => content.GetProperty(PropertyTypeAlias)!.GetValue();
80102

81103
private IPublishedContent CreatePublishedContent(ContentVariation contentTypeVariation, ContentVariation propertyTypeVariation, string? variationContextCulture = null, string? variationContextSegment = null)

0 commit comments

Comments
 (0)