Refactors a bit of the BuildDtos method and adds a unit test
This commit is contained in:
@@ -93,20 +93,36 @@ namespace Umbraco.Core.Persistence.Factories
|
||||
return dto;
|
||||
}
|
||||
|
||||
public static IEnumerable<PropertyDataDto> BuildDtos(int currentVersionId, int publishedVersionId, IEnumerable<Property> properties,
|
||||
ILanguageRepository languageRepository, out bool edited, out HashSet<string> editedCultures, IEnumerable<string> availableCultures = null)
|
||||
/// <summary>
|
||||
/// Creates a collection of <see cref="PropertyDataDto"/> from a collection of <see cref="Property"/>
|
||||
/// </summary>
|
||||
/// <param name="contentVariation">
|
||||
/// The <see cref="ContentVariation"/> of the entity containing the collection of <see cref="Property"/>
|
||||
/// </param>
|
||||
/// <param name="currentVersionId"></param>
|
||||
/// <param name="publishedVersionId"></param>
|
||||
/// <param name="properties">The properties to map</param>
|
||||
/// <param name="languageRepository"></param>
|
||||
/// <param name="edited">out parameter indicating that one or more properties have been edited</param>
|
||||
/// <param name="editedCultures">out parameter containing a collection of of edited cultures when the contentVariation varies by culture</param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<PropertyDataDto> BuildDtos(ContentVariation contentVariation, int currentVersionId, int publishedVersionId, IEnumerable<Property> properties,
|
||||
ILanguageRepository languageRepository, out bool edited, out HashSet<string> editedCultures)
|
||||
{
|
||||
var propertyDataDtos = new List<PropertyDataDto>();
|
||||
edited = false;
|
||||
editedCultures = null; // don't allocate unless necessary
|
||||
string defaultCulture = null; //don't allocate unless necessary
|
||||
|
||||
var entityVariesByCulture = contentVariation.VariesByCulture();
|
||||
|
||||
foreach (var property in properties)
|
||||
{
|
||||
if (property.PropertyType.IsPublishing)
|
||||
{
|
||||
var editingCultures = availableCultures?.Any() ?? property.PropertyType.VariesByCulture();
|
||||
|
||||
if (editingCultures && editedCultures == null) editedCultures = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
//create the resulting hashset if it's not created and the entity varies by culture
|
||||
if (entityVariesByCulture && editedCultures == null)
|
||||
editedCultures = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
// publishing = deal with edit and published values
|
||||
foreach (var propertyValue in property.Values)
|
||||
@@ -127,19 +143,22 @@ namespace Umbraco.Core.Persistence.Factories
|
||||
var sameValues = propertyValue.PublishedValue == null ? propertyValue.EditedValue == null : propertyValue.PublishedValue.Equals(propertyValue.EditedValue);
|
||||
edited |= !sameValues;
|
||||
|
||||
if (editingCultures && // cultures can be edited, ie CultureNeutral is supported
|
||||
propertyValue.Culture != null && propertyValue.Segment == null && // and value is CultureNeutral
|
||||
!sameValues) // and edited and published are different
|
||||
if (entityVariesByCulture // cultures can be edited, ie CultureNeutral is supported
|
||||
&& propertyValue.Culture != null && propertyValue.Segment == null // and value is CultureNeutral
|
||||
&& !sameValues) // and edited and published are different
|
||||
{
|
||||
editedCultures.Add(propertyValue.Culture); // report culture as edited
|
||||
}
|
||||
|
||||
// flag culture as edited if it contains an edited invariant property
|
||||
if (propertyValue.Culture == null &&
|
||||
availableCultures !=null && !sameValues&&
|
||||
editingCultures)
|
||||
if (!property.PropertyType.VariesByCulture()
|
||||
&& !sameValues // and edited and published are different
|
||||
&& entityVariesByCulture) //only when the entity is variant
|
||||
{
|
||||
editedCultures.Add(availableCultures.FirstOrDefault());
|
||||
if (defaultCulture == null)
|
||||
defaultCulture = languageRepository.GetDefaultIsoCode();
|
||||
|
||||
editedCultures.Add(defaultCulture);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -352,7 +352,7 @@ namespace Umbraco.Core.Persistence.Repositories.Implement
|
||||
}
|
||||
|
||||
// persist the property data
|
||||
var propertyDataDtos = PropertyFactory.BuildDtos(content.VersionId, content.PublishedVersionId, entity.Properties, LanguageRepository, out var edited, out var editedCultures);
|
||||
var propertyDataDtos = PropertyFactory.BuildDtos(content.ContentType.Variations, content.VersionId, content.PublishedVersionId, entity.Properties, LanguageRepository, out var edited, out var editedCultures);
|
||||
foreach (var propertyDataDto in propertyDataDtos)
|
||||
Database.Insert(propertyDataDto);
|
||||
|
||||
@@ -527,8 +527,8 @@ namespace Umbraco.Core.Persistence.Repositories.Implement
|
||||
Database.Execute(deletePropertyDataSql);
|
||||
|
||||
// insert property data
|
||||
var propertyDataDtos = PropertyFactory.BuildDtos(content.VersionId, publishing ? content.PublishedVersionId : 0,
|
||||
entity.Properties, LanguageRepository, out var edited, out var editedCultures, content.AvailableCultures);
|
||||
var propertyDataDtos = PropertyFactory.BuildDtos(content.ContentType.Variations, content.VersionId, publishing ? content.PublishedVersionId : 0,
|
||||
entity.Properties, LanguageRepository, out var edited, out var editedCultures);
|
||||
foreach (var propertyDataDto in propertyDataDtos)
|
||||
Database.Insert(propertyDataDto);
|
||||
|
||||
|
||||
@@ -284,7 +284,7 @@ namespace Umbraco.Core.Persistence.Repositories.Implement
|
||||
Database.Insert(mediaVersionDto);
|
||||
|
||||
// persist the property data
|
||||
var propertyDataDtos = PropertyFactory.BuildDtos(media.VersionId, 0, entity.Properties, LanguageRepository, out _, out _);
|
||||
var propertyDataDtos = PropertyFactory.BuildDtos(media.ContentType.Variations, media.VersionId, 0, entity.Properties, LanguageRepository, out _, out _);
|
||||
foreach (var propertyDataDto in propertyDataDtos)
|
||||
Database.Insert(propertyDataDto);
|
||||
|
||||
@@ -341,7 +341,7 @@ namespace Umbraco.Core.Persistence.Repositories.Implement
|
||||
// replace the property data
|
||||
var deletePropertyDataSql = SqlContext.Sql().Delete<PropertyDataDto>().Where<PropertyDataDto>(x => x.VersionId == media.VersionId);
|
||||
Database.Execute(deletePropertyDataSql);
|
||||
var propertyDataDtos = PropertyFactory.BuildDtos(media.VersionId, 0, entity.Properties, LanguageRepository, out _, out _);
|
||||
var propertyDataDtos = PropertyFactory.BuildDtos(media.ContentType.Variations, media.VersionId, 0, entity.Properties, LanguageRepository, out _, out _);
|
||||
foreach (var propertyDataDto in propertyDataDtos)
|
||||
Database.Insert(propertyDataDto);
|
||||
|
||||
|
||||
@@ -310,7 +310,7 @@ namespace Umbraco.Core.Persistence.Repositories.Implement
|
||||
Database.Insert(dto);
|
||||
|
||||
// persist the property data
|
||||
var propertyDataDtos = PropertyFactory.BuildDtos(member.VersionId, 0, entity.Properties, LanguageRepository, out _, out _);
|
||||
var propertyDataDtos = PropertyFactory.BuildDtos(member.ContentType.Variations, member.VersionId, 0, entity.Properties, LanguageRepository, out _, out _);
|
||||
foreach (var propertyDataDto in propertyDataDtos)
|
||||
Database.Insert(propertyDataDto);
|
||||
|
||||
@@ -375,7 +375,7 @@ namespace Umbraco.Core.Persistence.Repositories.Implement
|
||||
// replace the property data
|
||||
var deletePropertyDataSql = SqlContext.Sql().Delete<PropertyDataDto>().Where<PropertyDataDto>(x => x.VersionId == member.VersionId);
|
||||
Database.Execute(deletePropertyDataSql);
|
||||
var propertyDataDtos = PropertyFactory.BuildDtos(member.VersionId, 0, entity.Properties, LanguageRepository, out _, out _);
|
||||
var propertyDataDtos = PropertyFactory.BuildDtos(member.ContentType.Variations, member.VersionId, 0, entity.Properties, LanguageRepository, out _, out _);
|
||||
foreach (var propertyDataDto in propertyDataDtos)
|
||||
Database.Insert(propertyDataDto);
|
||||
|
||||
|
||||
@@ -792,6 +792,54 @@ namespace Umbraco.Tests.Services
|
||||
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Pending_Invariant_Property_Changes_Affect_Default_Language_Edited_State()
|
||||
{
|
||||
// Arrange
|
||||
|
||||
var langGB = new Language("en-GB") { IsDefault = true };
|
||||
var langFr = new Language("fr-FR");
|
||||
|
||||
ServiceContext.LocalizationService.Save(langFr);
|
||||
ServiceContext.LocalizationService.Save(langGB);
|
||||
|
||||
var contentType = MockedContentTypes.CreateMetaContentType();
|
||||
contentType.Variations = ContentVariation.Culture;
|
||||
foreach(var prop in contentType.PropertyTypes)
|
||||
prop.Variations = ContentVariation.Culture;
|
||||
var keywordsProp = contentType.PropertyTypes.Single(x => x.Alias == "metakeywords");
|
||||
keywordsProp.Variations = ContentVariation.Nothing; // this one is invariant
|
||||
|
||||
ServiceContext.ContentTypeService.Save(contentType);
|
||||
|
||||
IContent content = new Content("content", -1, contentType);
|
||||
content.SetCultureName("content-en", langGB.IsoCode);
|
||||
content.SetCultureName("content-fr", langFr.IsoCode);
|
||||
content.PublishCulture(langGB.IsoCode);
|
||||
content.PublishCulture(langFr.IsoCode);
|
||||
Assert.IsTrue(ServiceContext.ContentService.SavePublishing(content).Success);
|
||||
|
||||
//re-get
|
||||
content = ServiceContext.ContentService.GetById(content.Id);
|
||||
Assert.AreEqual(PublishedState.Published, content.PublishedState);
|
||||
Assert.IsTrue(content.IsCulturePublished(langGB.IsoCode));
|
||||
Assert.IsTrue(content.IsCulturePublished(langFr.IsoCode));
|
||||
Assert.IsFalse(content.IsCultureEdited(langGB.IsoCode));
|
||||
Assert.IsFalse(content.IsCultureEdited(langFr.IsoCode));
|
||||
|
||||
//update the invariant property and save a pending version
|
||||
content.SetValue("metakeywords", "hello");
|
||||
ServiceContext.ContentService.Save(content);
|
||||
|
||||
//re-get
|
||||
content = ServiceContext.ContentService.GetById(content.Id);
|
||||
Assert.AreEqual(PublishedState.Published, content.PublishedState);
|
||||
Assert.IsTrue(content.IsCulturePublished(langGB.IsoCode));
|
||||
Assert.IsTrue(content.IsCulturePublished(langFr.IsoCode));
|
||||
Assert.IsTrue(content.IsCultureEdited(langGB.IsoCode));
|
||||
Assert.IsFalse(content.IsCultureEdited(langFr.IsoCode));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Can_Publish_Content_Variation_And_Detect_Changed_Cultures()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user