Skip to content

Commit

Permalink
Property source level variation should only be applied when configured (
Browse files Browse the repository at this point in the history
  • Loading branch information
kjac committed May 13, 2024
1 parent ba9ddd1 commit ab32bac
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/Umbraco.PublishedCache.NuCache/Property.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal class Property : PublishedPropertyBase
// the invariant-neutral source and inter values
private readonly object? _sourceValue;
private readonly ContentVariation _variations;
private bool _sourceValueIsInvariant;
private readonly ContentVariation _sourceVariations;

// the variant and non-variant object values
private CacheValues? _cacheValues;
Expand Down Expand Up @@ -88,7 +88,7 @@ internal class Property : PublishedPropertyBase
// this variable is used for contextualizing the variation level when calculating property values.
// it must be set to the union of variance (the combination of content type and property type variance).
_variations = propertyType.Variations | content.ContentType.Variations;
_sourceValueIsInvariant = propertyType.Variations is ContentVariation.Nothing;
_sourceVariations = propertyType.Variations;
}

// clone for previewing as draft a published content that is published and has no draft
Expand All @@ -104,7 +104,7 @@ public Property(Property origin, PublishedContent content)
_isMember = origin._isMember;
_publishedSnapshotAccessor = origin._publishedSnapshotAccessor;
_variations = origin._variations;
_sourceValueIsInvariant = origin._sourceValueIsInvariant;
_sourceVariations = origin._sourceVariations;
}

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

public override object? GetSourceValue(string? culture = null, string? segment = null)
{
_content.VariationContextAccessor.ContextualizeVariation(_variations, _content.Id, ref culture, ref segment);
_content.VariationContextAccessor.ContextualizeVariation(_sourceVariations, _content.Id, ref culture, ref segment);

// source values are tightly bound to the property/schema culture and segment configurations, so we need to
// sanitize the contextualized culture/segment states before using them to access the source values.
culture = _sourceVariations.VariesByCulture() ? culture : string.Empty;
segment = _sourceVariations.VariesBySegment() ? segment : string.Empty;

if (_sourceValueIsInvariant || (culture == string.Empty && segment == string.Empty))
if (culture == string.Empty && segment == string.Empty)
{
return _sourceValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,28 @@ public void Content_Culture_And_Segment_Variation_Can_Get_Culture_And_Segment_Va
Assert.AreEqual(expectedValue, value);
}

[TestCase(DaCulture, Segment1, "DaDk property value")]
[TestCase(DaCulture, Segment2, "DaDk property value")]
[TestCase(EnCulture, Segment1, "EnUs property value")]
[TestCase(EnCulture, Segment2, "EnUs property value")]
public void Content_Culture_And_Segment_Variation_Can_Get_Culture_Variant_Property(string culture, string segment, string expectedValue)
{
var content = CreatePublishedContent(ContentVariation.CultureAndSegment, ContentVariation.Culture, variationContextCulture: culture, variationContextSegment: segment);
var value = GetPropertyValue(content);
Assert.AreEqual(expectedValue, value);
}

[TestCase(DaCulture, Segment1, "Segment1 property value")]
[TestCase(DaCulture, Segment2, "Segment2 property value")]
[TestCase(EnCulture, Segment1, "Segment1 property value")]
[TestCase(EnCulture, Segment2, "Segment2 property value")]
public void Content_Culture_And_Segment_Variation_Can_Get_Segment_Variant_Property(string culture, string segment, string expectedValue)
{
var content = CreatePublishedContent(ContentVariation.CultureAndSegment, ContentVariation.Segment, variationContextCulture: culture, variationContextSegment: segment);
var value = GetPropertyValue(content);
Assert.AreEqual(expectedValue, value);
}

private object? GetPropertyValue(IPublishedContent content) => content.GetProperty(PropertyTypeAlias)!.GetValue();

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

0 comments on commit ab32bac

Please sign in to comment.