Some improvements around validation of content/media properties
This commit is contained in:
@@ -258,5 +258,14 @@ namespace Umbraco.Core.Models
|
||||
}
|
||||
Properties.Add(propertyType.CreatePropertyFromValue(value));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Boolean indicating whether the content and its properties are valid
|
||||
/// </summary>
|
||||
/// <returns>True if content is valid otherwise false</returns>
|
||||
public virtual bool IsValid()
|
||||
{
|
||||
return Properties.Any(property => !property.IsValid()) == false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -107,5 +107,11 @@ namespace Umbraco.Core.Models
|
||||
/// <param name="propertyTypeAlias">Alias of the PropertyType</param>
|
||||
/// <param name="value">Value to set for the Property</param>
|
||||
void SetValue(string propertyTypeAlias, object value);
|
||||
|
||||
/// <summary>
|
||||
/// Boolean indicating whether the content and its properties are valid
|
||||
/// </summary>
|
||||
/// <returns>True if content is valid otherwise false</returns>
|
||||
bool IsValid();
|
||||
}
|
||||
}
|
||||
@@ -83,7 +83,10 @@ namespace Umbraco.Core.Models
|
||||
/// <summary>
|
||||
/// Gets or Sets the value of the Property
|
||||
/// </summary>
|
||||
/// <remarks>Setting the value will trigger a type and value validation</remarks>
|
||||
/// <remarks>
|
||||
/// Setting the value will trigger a type validation.
|
||||
/// The type of the value has to be valid in order to be saved.
|
||||
/// </remarks>
|
||||
[DataMember]
|
||||
public object Value
|
||||
{
|
||||
@@ -91,13 +94,6 @@ namespace Umbraco.Core.Models
|
||||
set
|
||||
{
|
||||
bool typeValidation = _propertyType.IsPropertyTypeValid(value);
|
||||
bool valueValidation = _propertyType.IsPropertyValueValid(value);
|
||||
|
||||
if (!typeValidation && !valueValidation)
|
||||
throw new Exception(
|
||||
string.Format(
|
||||
"Both Type and Value validation failed. The value type: {0} does not match the DataType in PropertyType with alias: {1}",
|
||||
value.GetType(), Alias));
|
||||
|
||||
if (!typeValidation)
|
||||
throw new Exception(
|
||||
@@ -105,15 +101,32 @@ namespace Umbraco.Core.Models
|
||||
"Type validation failed. The value type: {0} does not match the DataType in PropertyType with alias: {1}",
|
||||
value.GetType(), Alias));
|
||||
|
||||
if (!valueValidation)
|
||||
throw new Exception(
|
||||
string.Format(
|
||||
"Validation failed for the Value, because it does not conform to the validation rules set for the PropertyType with alias: {0}",
|
||||
Alias));
|
||||
|
||||
_value = value;
|
||||
OnPropertyChanged(ValueSelector);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Boolean indicating whether the current value is valid
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// A valid value implies that it is ready for publishing.
|
||||
/// Invalid property values can be saved, but not published.
|
||||
/// </remarks>
|
||||
/// <returns>True is property value is valid, otherwise false</returns>
|
||||
public bool IsValid()
|
||||
{
|
||||
return IsValid(Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Boolean indicating whether the passed in value is valid
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <returns>True is property value is valid, otherwise false</returns>
|
||||
public bool IsValid(object value)
|
||||
{
|
||||
return _propertyType.IsPropertyValueValid(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,4 +13,16 @@
|
||||
</Provider>
|
||||
</FileSystemProviders>
|
||||
|
||||
<umbraco>
|
||||
<infrastructure>
|
||||
<repositories>
|
||||
<repositoryMapping interfaceShortTypeName="IContentRepository" repositoryFullTypeName="Umbraco.Core.Persistence.Repositories.ContentRepository, Umbraco.Core" cacheProviderFullTypeName="Umbraco.Core.Persistence.Caching.RuntimeCacheProvider, Umbraco.Core"/>
|
||||
<repositoryMapping interfaceShortTypeName="IContentTypeRepository" repositoryFullTypeName="Umbraco.Core.Persistence.Repositories.ContentTypeRepository, Umbraco.Core" cacheProviderFullTypeName="Umbraco.Core.Persistence.Caching.InMemoryCacheProvider, Umbraco.Core"/>
|
||||
</repositories>
|
||||
<publishingStrategy>
|
||||
<provider type="Umbraco.Web.Publishing.PublishingStrategy, Umbraco.Web" />
|
||||
</publishingStrategy>
|
||||
</infrastructure>
|
||||
</umbraco>
|
||||
|
||||
</configuration>
|
||||
99
src/Umbraco.Tests/Services/ContentServiceTests.cs
Normal file
99
src/Umbraco.Tests/Services/ContentServiceTests.cs
Normal file
@@ -0,0 +1,99 @@
|
||||
using System;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Core.Models;
|
||||
using Umbraco.Tests.TestHelpers;
|
||||
using Umbraco.Web.Services;
|
||||
|
||||
namespace Umbraco.Tests.Services
|
||||
{
|
||||
[TestFixture]
|
||||
public class ContentServiceTests : BaseWebTest
|
||||
{
|
||||
/*[Test]*/
|
||||
public void Can_Create_Content()
|
||||
{
|
||||
// Arrange
|
||||
var contentService = new ContentService();
|
||||
|
||||
// Act
|
||||
IContent content = contentService.CreateContent(-1, "umbTextpage");
|
||||
|
||||
// Assert
|
||||
Assert.That(content, Is.Not.Null);
|
||||
Assert.That(content.HasIdentity, Is.False);
|
||||
}
|
||||
|
||||
/*[Test]*/
|
||||
public void Cannot_Create_Content_With_Non_Existing_ContentType_Alias()
|
||||
{
|
||||
// Arrange
|
||||
var contentService = new ContentService();
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<Exception>(() => contentService.CreateContent(-1, "umbAliasDoesntExist"));
|
||||
}
|
||||
|
||||
public void Can_Get_Content_By_Id()
|
||||
{ }
|
||||
|
||||
public void Can_Get_Content_By_Level()
|
||||
{ }
|
||||
|
||||
public void Can_Get_Children_Of_Content_Id()
|
||||
{ }
|
||||
|
||||
public void Can_Get_All_Versions_Of_Content()
|
||||
{ }
|
||||
|
||||
public void Can_Get_Root_Content()
|
||||
{ }
|
||||
|
||||
public void Can_Get_Content_For_Expiration()
|
||||
{ }
|
||||
|
||||
public void Can_Get_Content_For_Release()
|
||||
{ }
|
||||
|
||||
public void Can_Get_Content_In_RecycleBin()
|
||||
{ }
|
||||
|
||||
public void Can_RePublish_All_Content()
|
||||
{ }
|
||||
|
||||
public void Can_Publish_Content()
|
||||
{ }
|
||||
|
||||
public void Can_Publish_Content_Children()
|
||||
{ }
|
||||
|
||||
public void Can_Save_And_Publish_Content()
|
||||
{ }
|
||||
|
||||
public void Can_Save_Content()
|
||||
{ }
|
||||
|
||||
public void Can_Bulk_Save_Content()
|
||||
{ }
|
||||
|
||||
public void Can_Delete_Content_Of_Specific_ContentType()
|
||||
{ }
|
||||
|
||||
public void Can_Delete_Content()
|
||||
{ }
|
||||
|
||||
public void Can_Move_Content_To_RecycleBin()
|
||||
{ }
|
||||
|
||||
public void Can_Move_Content()
|
||||
{ }
|
||||
|
||||
public void Can_Copy_Content()
|
||||
{ }
|
||||
|
||||
public void Can_Send_To_Publication()
|
||||
{ }
|
||||
|
||||
public void Can_Rollback_Version_On_Content()
|
||||
{ }
|
||||
}
|
||||
}
|
||||
@@ -80,6 +80,7 @@
|
||||
<Compile Include="Routing\NiceUrlsProviderWithDomainsTests.cs" />
|
||||
<Compile Include="Routing\uQueryGetNodeIdByUrlTests.cs" />
|
||||
<Compile Include="Routing\UrlsWithNestedDomains.cs" />
|
||||
<Compile Include="Services\ContentServiceTests.cs" />
|
||||
<Compile Include="Surface\PluginControllerAreaTests.cs" />
|
||||
<Compile Include="TestHelpers\BaseRoutingTest.cs" />
|
||||
<Compile Include="GlobalSettingsTests.cs" />
|
||||
|
||||
@@ -185,6 +185,8 @@ namespace Umbraco.Web.Services
|
||||
|
||||
var list = new List<IContent>();
|
||||
|
||||
//Consider creating a Path query instead of recursive method
|
||||
//var query = Query<IContent>.Builder.Where(x => x.Path.StartsWith("-1"));
|
||||
var rootContent = GetRootContent();
|
||||
foreach (var content in rootContent)
|
||||
{
|
||||
@@ -193,8 +195,12 @@ namespace Umbraco.Web.Services
|
||||
|
||||
foreach (var item in list)
|
||||
{
|
||||
((Content)item).ChangePublishedState(true);
|
||||
repository.AddOrUpdate(item);
|
||||
//Only publish valid content - Might need to change the flat list as it could pose problems for children of invalid content
|
||||
if(item.IsValid())
|
||||
{
|
||||
((Content)item).ChangePublishedState(true);
|
||||
repository.AddOrUpdate(item);
|
||||
}
|
||||
}
|
||||
|
||||
unitOfWork.Commit();
|
||||
@@ -224,13 +230,19 @@ namespace Umbraco.Web.Services
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(unitOfWork);
|
||||
|
||||
//Consider creating a Path query instead of recursive method
|
||||
//var query = Query<IContent>.Builder.Where(x => x.Path.StartsWith(content.Path));
|
||||
var list = GetChildrenDeep(content.Id);
|
||||
list.Add(content);
|
||||
|
||||
foreach (var item in list)
|
||||
{
|
||||
((Content)item).ChangePublishedState(true);
|
||||
repository.AddOrUpdate(item);
|
||||
//Only publish valid content - Might need to change the flat list as it could pose problems for children of invalid content
|
||||
if (item.IsValid())
|
||||
{
|
||||
((Content) item).ChangePublishedState(true);
|
||||
repository.AddOrUpdate(item);
|
||||
}
|
||||
}
|
||||
|
||||
unitOfWork.Commit();
|
||||
@@ -283,7 +295,10 @@ namespace Umbraco.Web.Services
|
||||
{
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(unitOfWork);
|
||||
|
||||
|
||||
if (!content.IsValid())
|
||||
return false;//Content contains invalid property values and can therefore not be published
|
||||
|
||||
((Content)content).ChangePublishedState(true);
|
||||
repository.AddOrUpdate(content);
|
||||
unitOfWork.Commit();
|
||||
|
||||
Reference in New Issue
Block a user