Skip to content

Commit 48003e0

Browse files
author
Nikolaj Geisle
committed
Check ancestors for published culture
Fixed issue where if you were 3 levels deep and published only 1 culture, warning wouldn't fire
1 parent cc74e3f commit 48003e0

File tree

2 files changed

+116
-1
lines changed

2 files changed

+116
-1
lines changed

src/Umbraco.Tests.Integration/Umbraco.Web.BackOffice/Controllers/ContentControllerTests.cs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,100 @@ public async Task PostSave_Validates_Domains_Exist()
456456
Assert.AreEqual(expectedMessage, display.Notifications.FirstOrDefault(x => x.NotificationType == NotificationStyle.Warning)?.Message);
457457
});
458458
}
459+
460+
[Test]
461+
public async Task PostSave_Validates_All_Ancestor_Cultures_Are_Considered()
462+
{
463+
var sweIso = "sv-SE";
464+
ILocalizationService localizationService = GetRequiredService<ILocalizationService>();
465+
//Create 2 new languages
466+
localizationService.Save(new LanguageBuilder()
467+
.WithCultureInfo(DkIso)
468+
.WithIsDefault(false)
469+
.Build());
470+
471+
localizationService.Save(new LanguageBuilder()
472+
.WithCultureInfo(sweIso)
473+
.WithIsDefault(false)
474+
.Build());
475+
476+
IContentTypeService contentTypeService = GetRequiredService<IContentTypeService>();
477+
IContentType contentType = new ContentTypeBuilder().WithContentVariation(ContentVariation.Culture).Build();
478+
contentTypeService.Save(contentType);
479+
480+
Content content = new ContentBuilder()
481+
.WithoutIdentity()
482+
.WithContentType(contentType)
483+
.WithCultureName(UsIso, "Root")
484+
.Build();
485+
486+
IContentService contentService = GetRequiredService<IContentService>();
487+
contentService.SaveAndPublish(content);
488+
489+
Content childContent = new ContentBuilder()
490+
.WithoutIdentity()
491+
.WithContentType(contentType)
492+
.WithParent(content)
493+
.WithCultureName(DkIso, "Barn")
494+
.WithCultureName(UsIso, "Child")
495+
.Build();
496+
497+
contentService.SaveAndPublish(childContent);
498+
499+
Content grandChildContent = new ContentBuilder()
500+
.WithoutIdentity()
501+
.WithContentType(contentType)
502+
.WithParent(childContent)
503+
.WithCultureName(sweIso, "Bjarn")
504+
.Build();
505+
506+
507+
ContentItemSave model = new ContentItemSaveBuilder()
508+
.WithContent(grandChildContent)
509+
.WithParentId(childContent.Id)
510+
.WithAction(ContentSaveAction.PublishNew)
511+
.Build();
512+
513+
ILanguage enLanguage = localizationService.GetLanguageByIsoCode(UsIso);
514+
IDomainService domainService = GetRequiredService<IDomainService>();
515+
var enDomain = new UmbracoDomain("/en")
516+
{
517+
RootContentId = content.Id,
518+
LanguageId = enLanguage.Id
519+
};
520+
domainService.Save(enDomain);
521+
522+
ILanguage dkLanguage = localizationService.GetLanguageByIsoCode(DkIso);
523+
var dkDomain = new UmbracoDomain("/dk")
524+
{
525+
RootContentId = childContent.Id,
526+
LanguageId = dkLanguage.Id
527+
};
528+
domainService.Save(dkDomain);
529+
530+
var url = PrepareApiControllerUrl<ContentController>(x => x.PostSave(null));
531+
532+
HttpResponseMessage response = await Client.PostAsync(url, new MultipartFormDataContent
533+
{
534+
{ new StringContent(JsonConvert.SerializeObject(model)), "contentItem" }
535+
});
536+
537+
var body = await response.Content.ReadAsStringAsync();
538+
body = body.TrimStart(AngularJsonMediaTypeFormatter.XsrfPrefix);
539+
ContentItemDisplay display = JsonConvert.DeserializeObject<ContentItemDisplay>(body);
540+
541+
542+
ILocalizedTextService localizedTextService = GetRequiredService<ILocalizedTextService>();
543+
var expectedMessage = localizedTextService.Localize("speechBubbles", "publishWithMissingDomain", new []{"sv-SE"});
544+
545+
Assert.Multiple(() =>
546+
{
547+
Assert.NotNull(display);
548+
Assert.AreEqual(1, display.Notifications.Count(x => x.NotificationType == NotificationStyle.Warning));
549+
Assert.AreEqual(expectedMessage, display.Notifications.FirstOrDefault(x => x.NotificationType == NotificationStyle.Warning)?.Message);
550+
});
551+
}
552+
459553
[Test]
460554
public async Task PostSave_Validates_All_Cultures_Has_Domains()
461555
{

src/Umbraco.Web.BackOffice/Controllers/ContentController.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1456,7 +1456,7 @@ internal void AddDomainWarnings(IContent persistedContent, string[] culturesPubl
14561456
return;
14571457
}
14581458

1459-
var publishedCultures = persistedContent.PublishedCultures.ToList();
1459+
var publishedCultures = GetPublishedCulturesFromAncestors(persistedContent).ToList();
14601460
// If only a single culture is published we shouldn't have any routing issues
14611461
if (publishedCultures.Count < 2)
14621462
{
@@ -1583,6 +1583,27 @@ private bool PublishCulture(IContent persistentContent, IEnumerable<ContentVaria
15831583
return true;
15841584
}
15851585

1586+
private IEnumerable<string> GetPublishedCulturesFromAncestors(IContent content)
1587+
{
1588+
if (content.ParentId == -1)
1589+
{
1590+
return content.PublishedCultures;
1591+
}
1592+
1593+
HashSet<string> publishedCultures = new ();
1594+
publishedCultures.UnionWith(content.PublishedCultures);
1595+
1596+
IEnumerable<int> ancestorIds = content.GetAncestorIds();
1597+
1598+
foreach (var id in ancestorIds)
1599+
{
1600+
IEnumerable<string> cultures = _contentService.GetById(id).PublishedCultures;
1601+
publishedCultures.UnionWith(cultures);
1602+
}
1603+
1604+
return publishedCultures;
1605+
1606+
}
15861607
/// <summary>
15871608
/// Adds a generic culture error for use in displaying the culture validation error in the save/publish/etc... dialogs
15881609
/// </summary>

0 commit comments

Comments
 (0)