Files
Umbraco-CMS/src/Umbraco.Tests/Testing/ContentBaseExtensions.cs

49 lines
2.0 KiB
C#
Raw Normal View History

using System;
using System.Linq;
2019-02-05 19:58:33 +01:00
using Umbraco.Core;
using Umbraco.Core.Composing;
using Umbraco.Core.Models;
namespace Umbraco.Tests.Testing
{
public static class ContentBaseExtensions
{
/// <summary>
2019-02-05 19:58:33 +01:00
/// Set property values by alias with an anonymous object.
/// </summary>
/// <remarks>Does not support variants.</remarks>
public static void PropertyValues(this IContentBase content, object value, string culture = null, string segment = null)
{
if (value == null)
throw new Exception("No properties has been passed in");
2019-02-05 19:58:33 +01:00
var contentType = Current.Services.ContentTypeBaseServices.GetContentTypeOf(content);
var propertyInfos = value.GetType().GetProperties();
foreach (var propertyInfo in propertyInfos)
{
//Check if a PropertyType with alias exists thus being a valid property
2019-02-05 19:58:33 +01:00
var propertyType = contentType.CompositionPropertyTypes.FirstOrDefault(x => x.Alias == propertyInfo.Name);
if (propertyType == null)
throw new Exception($"The property alias {propertyInfo.Name} is not valid, because no PropertyType with this alias exists");
//Check if a Property with the alias already exists in the collection thus being updated or inserted
var item = content.Properties.FirstOrDefault(x => x.Alias == propertyInfo.Name);
if (item != null)
{
item.SetValue(propertyInfo.GetValue(value, null), culture, segment);
//Update item with newly added value
content.Properties.Add(item);
}
else
{
//Create new Property to add to collection
var property = propertyType.CreateProperty();
property.SetValue(propertyInfo.GetValue(value, null), culture, segment);
content.Properties.Add(property);
}
}
}
}
}