Check ancestors for published culture

Fixed issue where if you were 3 levels deep and published only 1 culture, warning wouldn't fire
This commit is contained in:
Nikolaj Geisle
2021-10-15 11:47:48 +02:00
parent cc74e3f4fd
commit 48003e0b70
2 changed files with 116 additions and 1 deletions

View File

@@ -456,6 +456,100 @@ namespace Umbraco.Cms.Tests.Integration.Umbraco.Web.BackOffice.Controllers
Assert.AreEqual(expectedMessage, display.Notifications.FirstOrDefault(x => x.NotificationType == NotificationStyle.Warning)?.Message);
});
}
[Test]
public async Task PostSave_Validates_All_Ancestor_Cultures_Are_Considered()
{
var sweIso = "sv-SE";
ILocalizationService localizationService = GetRequiredService<ILocalizationService>();
//Create 2 new languages
localizationService.Save(new LanguageBuilder()
.WithCultureInfo(DkIso)
.WithIsDefault(false)
.Build());
localizationService.Save(new LanguageBuilder()
.WithCultureInfo(sweIso)
.WithIsDefault(false)
.Build());
IContentTypeService contentTypeService = GetRequiredService<IContentTypeService>();
IContentType contentType = new ContentTypeBuilder().WithContentVariation(ContentVariation.Culture).Build();
contentTypeService.Save(contentType);
Content content = new ContentBuilder()
.WithoutIdentity()
.WithContentType(contentType)
.WithCultureName(UsIso, "Root")
.Build();
IContentService contentService = GetRequiredService<IContentService>();
contentService.SaveAndPublish(content);
Content childContent = new ContentBuilder()
.WithoutIdentity()
.WithContentType(contentType)
.WithParent(content)
.WithCultureName(DkIso, "Barn")
.WithCultureName(UsIso, "Child")
.Build();
contentService.SaveAndPublish(childContent);
Content grandChildContent = new ContentBuilder()
.WithoutIdentity()
.WithContentType(contentType)
.WithParent(childContent)
.WithCultureName(sweIso, "Bjarn")
.Build();
ContentItemSave model = new ContentItemSaveBuilder()
.WithContent(grandChildContent)
.WithParentId(childContent.Id)
.WithAction(ContentSaveAction.PublishNew)
.Build();
ILanguage enLanguage = localizationService.GetLanguageByIsoCode(UsIso);
IDomainService domainService = GetRequiredService<IDomainService>();
var enDomain = new UmbracoDomain("/en")
{
RootContentId = content.Id,
LanguageId = enLanguage.Id
};
domainService.Save(enDomain);
ILanguage dkLanguage = localizationService.GetLanguageByIsoCode(DkIso);
var dkDomain = new UmbracoDomain("/dk")
{
RootContentId = childContent.Id,
LanguageId = dkLanguage.Id
};
domainService.Save(dkDomain);
var url = PrepareApiControllerUrl<ContentController>(x => x.PostSave(null));
HttpResponseMessage response = await Client.PostAsync(url, new MultipartFormDataContent
{
{ new StringContent(JsonConvert.SerializeObject(model)), "contentItem" }
});
var body = await response.Content.ReadAsStringAsync();
body = body.TrimStart(AngularJsonMediaTypeFormatter.XsrfPrefix);
ContentItemDisplay display = JsonConvert.DeserializeObject<ContentItemDisplay>(body);
ILocalizedTextService localizedTextService = GetRequiredService<ILocalizedTextService>();
var expectedMessage = localizedTextService.Localize("speechBubbles", "publishWithMissingDomain", new []{"sv-SE"});
Assert.Multiple(() =>
{
Assert.NotNull(display);
Assert.AreEqual(1, display.Notifications.Count(x => x.NotificationType == NotificationStyle.Warning));
Assert.AreEqual(expectedMessage, display.Notifications.FirstOrDefault(x => x.NotificationType == NotificationStyle.Warning)?.Message);
});
}
[Test]
public async Task PostSave_Validates_All_Cultures_Has_Domains()
{

View File

@@ -1456,7 +1456,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
return;
}
var publishedCultures = persistedContent.PublishedCultures.ToList();
var publishedCultures = GetPublishedCulturesFromAncestors(persistedContent).ToList();
// If only a single culture is published we shouldn't have any routing issues
if (publishedCultures.Count < 2)
{
@@ -1583,6 +1583,27 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
return true;
}
private IEnumerable<string> GetPublishedCulturesFromAncestors(IContent content)
{
if (content.ParentId == -1)
{
return content.PublishedCultures;
}
HashSet<string> publishedCultures = new ();
publishedCultures.UnionWith(content.PublishedCultures);
IEnumerable<int> ancestorIds = content.GetAncestorIds();
foreach (var id in ancestorIds)
{
IEnumerable<string> cultures = _contentService.GetById(id).PublishedCultures;
publishedCultures.UnionWith(cultures);
}
return publishedCultures;
}
/// <summary>
/// Adds a generic culture error for use in displaying the culture validation error in the save/publish/etc... dialogs
/// </summary>