|
| 1 | +using Microsoft.AspNetCore.Http; |
| 2 | +using NUnit.Framework; |
| 3 | +using Umbraco.Cms.Core; |
| 4 | +using Umbraco.Cms.Core.Cache; |
| 5 | +using Umbraco.Cms.Core.DeliveryApi; |
| 6 | +using Umbraco.Cms.Core.Models; |
| 7 | +using Umbraco.Cms.Core.Models.PublishedContent; |
| 8 | +using Umbraco.Cms.Core.Services; |
| 9 | +using Umbraco.Cms.Core.Services.Changes; |
| 10 | +using Umbraco.Cms.Core.Web; |
| 11 | +using Umbraco.Cms.Tests.Common.Builders; |
| 12 | +using Umbraco.Cms.Tests.Common.Builders.Extensions; |
| 13 | +using Umbraco.Cms.Tests.Common.Testing; |
| 14 | +using Umbraco.Cms.Tests.Integration.Testing; |
| 15 | + |
| 16 | +namespace Umbraco.Cms.Tests.Integration.Umbraco.Core.PublishedContent; |
| 17 | + |
| 18 | +[TestFixture] |
| 19 | +[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)] |
| 20 | +public class PublishedContentFallbackTests : UmbracoIntegrationTest |
| 21 | +{ |
| 22 | + private IContentService ContentService => GetRequiredService<IContentService>(); |
| 23 | + |
| 24 | + private IContentTypeService ContentTypeService => GetRequiredService<IContentTypeService>(); |
| 25 | + |
| 26 | + private IUmbracoContextAccessor UmbracoContextAccessor => GetRequiredService<IUmbracoContextAccessor>(); |
| 27 | + |
| 28 | + private IUmbracoContextFactory UmbracoContextFactory => GetRequiredService<IUmbracoContextFactory>(); |
| 29 | + |
| 30 | + private IVariationContextAccessor VariationContextAccessor => GetRequiredService<IVariationContextAccessor>(); |
| 31 | + |
| 32 | + private IPublishedValueFallback PublishedValueFallback => GetRequiredService<IPublishedValueFallback>(); |
| 33 | + |
| 34 | + private ContentCacheRefresher ContentCacheRefresher => GetRequiredService<ContentCacheRefresher>(); |
| 35 | + |
| 36 | + private IApiContentBuilder ApiContentBuilder => GetRequiredService<IApiContentBuilder>(); |
| 37 | + |
| 38 | + protected override void CustomTestSetup(IUmbracoBuilder builder) |
| 39 | + => builder |
| 40 | + .AddUmbracoHybridCache() |
| 41 | + .AddDeliveryApi(); |
| 42 | + |
| 43 | + [SetUp] |
| 44 | + public void SetUpTest() |
| 45 | + { |
| 46 | + var httpContextAccessor = GetRequiredService<IHttpContextAccessor>(); |
| 47 | + |
| 48 | + httpContextAccessor.HttpContext = new DefaultHttpContext |
| 49 | + { |
| 50 | + Request = |
| 51 | + { |
| 52 | + Scheme = "https", |
| 53 | + Host = new HostString("localhost"), |
| 54 | + Path = "/", |
| 55 | + QueryString = new QueryString(string.Empty) |
| 56 | + }, |
| 57 | + RequestServices = Services |
| 58 | + }; |
| 59 | + } |
| 60 | + |
| 61 | + [TestCase("Invariant title", "Segmented title", "Segmented title")] |
| 62 | + [TestCase(null, "Segmented title", "Segmented title")] |
| 63 | + [TestCase("Invariant title", null, "Invariant title")] |
| 64 | + [TestCase(null, null, null)] |
| 65 | + public async Task Property_Value_Performs_Fallback_To_Default_Segment_For_Templated_Rendering(string? invariantTitle, string? segmentedTitle, string? expectedResult) |
| 66 | + { |
| 67 | + var publishedContent = await SetupSegmentedContentAsync(invariantTitle, segmentedTitle); |
| 68 | + |
| 69 | + // NOTE: the TextStringValueConverter.ConvertIntermediateToObject() explicitly converts a null source value to an empty string |
| 70 | + |
| 71 | + var segmentedResult = publishedContent.Value<string>(PublishedValueFallback, "title", segment: "s1"); |
| 72 | + Assert.AreEqual(expectedResult ?? string.Empty, segmentedResult); |
| 73 | + |
| 74 | + var invariantResult = publishedContent.Value<string>(PublishedValueFallback, "title", segment: string.Empty); |
| 75 | + Assert.AreEqual(invariantTitle ?? string.Empty, invariantResult); |
| 76 | + } |
| 77 | + |
| 78 | + [TestCase("Invariant title", "Segmented title", "Segmented title")] |
| 79 | + [TestCase(null, "Segmented title", "Segmented title")] |
| 80 | + [TestCase("Invariant title", null, "Invariant title")] |
| 81 | + [TestCase(null, null, null)] |
| 82 | + public async Task Property_Value_Performs_Fallback_To_Default_Segment_For_Delivery_Api_Output(string? invariantTitle, string? segmentedTitle, string? expectedResult) |
| 83 | + { |
| 84 | + UmbracoContextFactory.EnsureUmbracoContext(); |
| 85 | + |
| 86 | + var publishedContent = await SetupSegmentedContentAsync(invariantTitle, segmentedTitle); |
| 87 | + |
| 88 | + VariationContextAccessor.VariationContext = new VariationContext(culture: null, segment: "s1"); |
| 89 | + var apiContent = ApiContentBuilder.Build(publishedContent); |
| 90 | + Assert.IsNotNull(apiContent); |
| 91 | + Assert.IsTrue(apiContent.Properties.TryGetValue("title", out var segmentedValue)); |
| 92 | + Assert.AreEqual(expectedResult, segmentedValue); |
| 93 | + |
| 94 | + VariationContextAccessor.VariationContext = new VariationContext(culture: null, segment: null); |
| 95 | + apiContent = ApiContentBuilder.Build(publishedContent); |
| 96 | + Assert.IsNotNull(apiContent); |
| 97 | + Assert.IsTrue(apiContent.Properties.TryGetValue("title", out var invariantValue)); |
| 98 | + Assert.AreEqual(invariantTitle, invariantValue); |
| 99 | + } |
| 100 | + |
| 101 | + private async Task<IPublishedContent> SetupSegmentedContentAsync(string? invariantTitle, string? segmentedTitle) |
| 102 | + { |
| 103 | + var contentType = new ContentTypeBuilder() |
| 104 | + .WithAlias("theContentType") |
| 105 | + .WithContentVariation(ContentVariation.Segment) |
| 106 | + .AddPropertyType() |
| 107 | + .WithAlias("title") |
| 108 | + .WithName("Title") |
| 109 | + .WithDataTypeId(Constants.DataTypes.Textbox) |
| 110 | + .WithPropertyEditorAlias(Constants.PropertyEditors.Aliases.TextBox) |
| 111 | + .WithValueStorageType(ValueStorageType.Nvarchar) |
| 112 | + .WithVariations(ContentVariation.Segment) |
| 113 | + .Done() |
| 114 | + .WithAllowAsRoot(true) |
| 115 | + .Build(); |
| 116 | + await ContentTypeService.CreateAsync(contentType, Constants.Security.SuperUserKey); |
| 117 | + |
| 118 | + var content = new ContentBuilder() |
| 119 | + .WithContentType(contentType) |
| 120 | + .WithName("Content") |
| 121 | + .Build(); |
| 122 | + content.SetValue("title", invariantTitle); |
| 123 | + content.SetValue("title", segmentedTitle, segment: "s1"); |
| 124 | + ContentService.Save(content); |
| 125 | + ContentService.Publish(content, ["*"]); |
| 126 | + |
| 127 | + ContentCacheRefresher.Refresh([new ContentCacheRefresher.JsonPayload { ChangeTypes = TreeChangeTypes.RefreshAll }]); |
| 128 | + |
| 129 | + UmbracoContextAccessor.Clear(); |
| 130 | + var umbracoContext = UmbracoContextFactory.EnsureUmbracoContext().UmbracoContext; |
| 131 | + var publishedContent = umbracoContext.Content.GetById(content.Key); |
| 132 | + Assert.IsNotNull(publishedContent); |
| 133 | + |
| 134 | + return publishedContent; |
| 135 | + } |
| 136 | +} |
0 commit comments