2020-12-05 11:12:55 +01:00
// Copyright (c) Umbraco.
// See LICENSE for more details.
2020-04-19 09:05:09 +02:00
using System ;
2020-06-17 16:39:28 +02:00
using System.Collections.Generic ;
using System.Globalization ;
2021-02-18 11:06:02 +01:00
using Umbraco.Cms.Core ;
using Umbraco.Cms.Core.Models ;
using Umbraco.Cms.Tests.Common.Builders.Extensions ;
using Umbraco.Cms.Tests.Common.Builders.Interfaces ;
using Umbraco.Cms.Tests.Common.Extensions ;
using Constants = Umbraco . Cms . Core . Constants ;
2020-04-19 09:05:09 +02:00
2021-02-18 11:06:02 +01:00
namespace Umbraco.Cms.Tests.Common.Builders
2020-04-19 09:05:09 +02:00
{
public class ContentBuilder
: BuilderBase < Content > ,
IBuildContentTypes ,
2021-10-07 13:36:18 +02:00
IBuildContentCultureInfosCollection ,
2020-04-19 09:05:09 +02:00
IWithIdBuilder ,
IWithKeyBuilder ,
IWithParentIdBuilder ,
IWithCreatorIdBuilder ,
IWithCreateDateBuilder ,
IWithUpdateDateBuilder ,
IWithNameBuilder ,
IWithTrashedBuilder ,
IWithLevelBuilder ,
IWithPathBuilder ,
2020-06-17 16:39:28 +02:00
IWithSortOrderBuilder ,
2020-10-11 09:13:31 +02:00
IWithCultureInfoBuilder ,
IWithPropertyValues
2020-04-19 09:05:09 +02:00
{
private ContentTypeBuilder _contentTypeBuilder ;
2021-10-07 13:36:18 +02:00
private ContentCultureInfosCollectionBuilder _contentCultureInfosCollectionBuilder ;
2020-04-19 09:05:09 +02:00
private GenericDictionaryBuilder < ContentBuilder , string , object > _propertyDataBuilder ;
private int? _id ;
2020-10-20 14:49:52 +02:00
private int? _versionId ;
2020-04-19 09:05:09 +02:00
private Guid ? _key ;
private DateTime ? _createDate ;
private DateTime ? _updateDate ;
private int? _parentId ;
2020-10-07 14:54:14 +02:00
private IContent _parent ;
2020-04-19 09:05:09 +02:00
private string _name ;
private int? _creatorId ;
private int? _level ;
private string _path ;
private int? _sortOrder ;
private bool? _trashed ;
2020-06-17 16:39:28 +02:00
private CultureInfo _cultureInfo ;
2020-06-09 07:49:26 +02:00
private IContentType _contentType ;
2021-10-07 13:36:18 +02:00
private ContentCultureInfosCollection _contentCultureInfosCollection ;
2020-12-05 11:12:55 +01:00
private readonly IDictionary < string , string > _cultureNames = new Dictionary < string , string > ( ) ;
2020-10-07 11:23:31 +02:00
private object _propertyValues ;
private string _propertyValuesCulture ;
private string _propertyValuesSegment ;
2020-04-19 09:05:09 +02:00
2020-10-20 14:49:52 +02:00
public ContentBuilder WithVersionId ( int versionId )
2020-04-19 09:05:09 +02:00
{
2020-10-20 14:49:52 +02:00
_versionId = versionId ;
return this ;
2020-04-19 09:05:09 +02:00
}
2020-10-07 14:54:14 +02:00
public ContentBuilder WithParent ( IContent parent )
{
_parentId = null ;
_parent = parent ;
return this ;
}
2020-10-07 11:23:31 +02:00
public ContentBuilder WithContentType ( IContentType contentType )
{
_contentTypeBuilder = null ;
2021-10-19 23:11:54 +11:00
_contentType = contentType ;
2020-10-07 11:23:31 +02:00
return this ;
}
2021-10-07 13:36:18 +02:00
public ContentBuilder WithContentCultureInfosCollection (
ContentCultureInfosCollection contentCultureInfosCollection )
{
_contentCultureInfosCollectionBuilder = null ;
_contentCultureInfosCollection = contentCultureInfosCollection ;
return this ;
}
2020-10-07 11:23:31 +02:00
public ContentBuilder WithCultureName ( string culture , string name = "" )
{
if ( string . IsNullOrWhiteSpace ( name ) )
{
if ( _cultureNames . TryGetValue ( culture , out _ ) )
{
_cultureNames . Remove ( culture ) ;
}
}
else
{
_cultureNames [ culture ] = name ;
}
return this ;
}
2020-10-20 14:49:52 +02:00
public ContentTypeBuilder AddContentType ( )
{
_contentType = null ;
var builder = new ContentTypeBuilder ( this ) ;
_contentTypeBuilder = builder ;
return builder ;
}
2020-10-07 11:23:31 +02:00
public GenericDictionaryBuilder < ContentBuilder , string , object > AddPropertyData ( )
{
var builder = new GenericDictionaryBuilder < ContentBuilder , string , object > ( this ) ;
_propertyDataBuilder = builder ;
return builder ;
}
2021-10-07 13:36:18 +02:00
public ContentCultureInfosCollectionBuilder AddContentCultureInfosCollection ( )
{
_contentCultureInfosCollection = null ;
var builder = new ContentCultureInfosCollectionBuilder ( this ) ;
_contentCultureInfosCollectionBuilder = builder ;
return builder ;
}
2020-04-19 09:05:09 +02:00
public override Content Build ( )
{
2020-10-07 11:23:31 +02:00
var id = _id ? ? 0 ;
2020-10-20 14:49:52 +02:00
var versionId = _versionId ? ? 0 ;
2020-12-05 11:12:55 +01:00
Guid key = _key ? ? Guid . NewGuid ( ) ;
2020-04-19 09:05:09 +02:00
var parentId = _parentId ? ? - 1 ;
2020-12-05 11:12:55 +01:00
IContent parent = _parent ? ? null ;
DateTime createDate = _createDate ? ? DateTime . Now ;
DateTime updateDate = _updateDate ? ? DateTime . Now ;
2020-04-19 09:05:09 +02:00
var name = _name ? ? Guid . NewGuid ( ) . ToString ( ) ;
2020-10-07 11:23:31 +02:00
var creatorId = _creatorId ? ? 0 ;
2020-04-19 09:05:09 +02:00
var level = _level ? ? 1 ;
2020-04-28 12:06:01 +02:00
var path = _path ? ? $"-1,{id}" ;
2020-04-19 09:05:09 +02:00
var sortOrder = _sortOrder ? ? 0 ;
var trashed = _trashed ? ? false ;
2020-06-17 16:39:28 +02:00
var culture = _cultureInfo ? . Name ? ? null ;
2020-10-07 11:23:31 +02:00
var propertyValues = _propertyValues ? ? null ;
var propertyValuesCulture = _propertyValuesCulture ? ? null ;
var propertyValuesSegment = _propertyValuesSegment ? ? null ;
2020-04-19 09:05:09 +02:00
2020-06-09 07:49:26 +02:00
if ( _contentTypeBuilder is null & & _contentType is null )
2020-04-19 09:05:09 +02:00
{
2020-10-08 12:04:40 +02:00
throw new InvalidOperationException ( "A content item cannot be constructed without providing a content type. Use AddContentType() or WithContentType()." ) ;
2020-04-19 09:05:09 +02:00
}
2020-12-05 11:12:55 +01:00
IContentType contentType = _contentType ? ? _contentTypeBuilder . Build ( ) ;
2020-04-19 09:05:09 +02:00
2020-10-07 14:54:14 +02:00
Content content ;
if ( parent ! = null )
{
2020-10-11 22:31:54 +02:00
content = new Content ( name , parent , contentType , culture ) ;
2020-10-07 14:54:14 +02:00
}
else
2020-04-19 09:05:09 +02:00
{
2020-10-07 14:54:14 +02:00
content = new Content ( name , parentId , contentType , culture ) ;
}
content . Id = id ;
2020-10-20 14:49:52 +02:00
content . VersionId = versionId ;
2020-10-07 14:54:14 +02:00
content . Key = key ;
content . CreateDate = createDate ;
content . UpdateDate = updateDate ;
content . CreatorId = creatorId ;
content . Level = level ;
content . Path = path ;
content . SortOrder = sortOrder ;
content . Trashed = trashed ;
2020-04-19 09:05:09 +02:00
2021-10-19 23:11:54 +11:00
if ( contentType . DefaultTemplate ? . Id > 0 )
{
content . TemplateId = contentType . DefaultTemplate . Id ;
}
2020-12-05 11:12:55 +01:00
foreach ( KeyValuePair < string , string > cultureName in _cultureNames )
2020-06-17 16:39:28 +02:00
{
content . SetCultureName ( cultureName . Value , cultureName . Key ) ;
}
2020-10-07 11:23:31 +02:00
if ( _propertyDataBuilder ! = null | | propertyValues ! = null )
2020-04-19 09:05:09 +02:00
{
2020-10-07 11:23:31 +02:00
if ( _propertyDataBuilder ! = null )
{
2020-12-05 11:12:55 +01:00
IDictionary < string , object > propertyData = _propertyDataBuilder . Build ( ) ;
foreach ( KeyValuePair < string , object > keyValuePair in propertyData )
2020-10-07 11:23:31 +02:00
{
2020-10-08 12:04:21 +02:00
content . SetValue ( keyValuePair . Key , keyValuePair . Value ) ;
2020-10-07 11:23:31 +02:00
}
}
else
2020-04-19 09:05:09 +02:00
{
2020-10-07 11:23:31 +02:00
content . PropertyValues ( propertyValues , propertyValuesCulture , propertyValuesSegment ) ;
2020-04-19 09:05:09 +02:00
}
2020-06-09 07:49:26 +02:00
content . ResetDirtyProperties ( false ) ;
2020-04-19 09:05:09 +02:00
}
2021-10-07 13:36:18 +02:00
if ( _contentCultureInfosCollection is not null | | _contentCultureInfosCollectionBuilder is not null )
{
ContentCultureInfosCollection contentCultureInfos =
_contentCultureInfosCollection ? ? _contentCultureInfosCollectionBuilder . Build ( ) ;
content . PublishCultureInfos = contentCultureInfos ;
}
2020-06-09 07:49:26 +02:00
return content ;
}
2020-12-05 11:12:55 +01:00
public static Content CreateBasicContent ( IContentType contentType , int id = 0 ) = >
new ContentBuilder ( )
2020-12-02 22:30:44 +01:00
. WithId ( id )
2020-10-07 11:23:31 +02:00
. WithContentType ( contentType )
. WithName ( "Home" )
. Build ( ) ;
2020-04-19 09:05:09 +02:00
2020-12-05 11:12:55 +01:00
public static Content CreateSimpleContent ( IContentType contentType ) = >
new ContentBuilder ( )
2020-10-07 11:23:31 +02:00
. WithContentType ( contentType )
. WithName ( "Home" )
. WithPropertyValues ( new
2020-12-05 11:12:55 +01:00
{
title = "Welcome to our Home page" ,
bodyText = "This is the welcome message on the first page" ,
author = "John Doe"
} )
2020-10-07 11:23:31 +02:00
. Build ( ) ;
2020-06-17 16:39:28 +02:00
2021-12-16 13:44:20 +01:00
public static Content CreateSimpleContent ( IContentType contentType , string name , int parentId = - 1 , string? culture = null , string? segment = null ) = >
2020-12-05 11:12:55 +01:00
new ContentBuilder ( )
2020-10-07 11:23:31 +02:00
. WithContentType ( contentType )
. WithName ( name )
. WithParentId ( parentId )
2020-12-05 11:12:55 +01:00
. WithPropertyValues (
new
2020-10-07 11:23:31 +02:00
{
title = "Welcome to our Home page" ,
bodyText = "This is the welcome message on the first page" ,
author = "John Doe"
2020-12-05 11:12:55 +01:00
} ,
culture ,
segment )
2020-10-07 11:23:31 +02:00
. Build ( ) ;
2020-06-17 16:39:28 +02:00
2021-12-16 13:44:20 +01:00
public static Content CreateSimpleContent ( IContentType contentType , string name , IContent parent , string? culture = null , string? segment = null , bool setPropertyValues = true )
2020-10-07 14:54:14 +02:00
{
2020-12-05 11:12:55 +01:00
ContentBuilder builder = new ContentBuilder ( )
2020-10-07 14:54:14 +02:00
. WithContentType ( contentType )
. WithName ( name )
. WithParent ( parent ) ;
2020-10-09 13:08:23 +02:00
if ( ! ( culture is null ) )
{
builder = builder . WithCultureName ( culture , name ) ;
}
2020-10-07 14:54:14 +02:00
if ( setPropertyValues )
{
2020-12-05 11:12:55 +01:00
builder = builder . WithPropertyValues (
new
{
title = name + " Subpage" ,
bodyText = "This is a subpage" ,
author = "John Doe"
} ,
culture ,
segment ) ;
2020-10-07 14:54:14 +02:00
}
2020-12-05 11:12:55 +01:00
Content content = builder . Build ( ) ;
2020-10-07 14:54:14 +02:00
content . ResetDirtyProperties ( false ) ;
return content ;
}
2020-10-14 08:02:43 +02:00
public static IEnumerable < Content > CreateTextpageContent ( IContentType contentType , int parentId , int amount )
{
var list = new List < Content > ( ) ;
2020-12-05 11:12:55 +01:00
for ( var i = 0 ; i < amount ; i + + )
2020-10-14 08:02:43 +02:00
{
var name = "Textpage No-" + i ;
var content = new Content ( name , parentId , contentType ) { CreatorId = 0 , WriterId = 0 } ;
object obj =
new
{
title = name + " title" ,
bodyText = string . Format ( "This is a textpage based on the {0} ContentType" , contentType . Alias ) ,
keywords = "text,page,meta" ,
description = "This is the meta description for a textpage"
} ;
content . PropertyValues ( obj ) ;
content . ResetDirtyProperties ( false ) ;
list . Add ( content ) ;
}
return list ;
}
2020-12-05 11:12:55 +01:00
public static Content CreateTextpageContent ( IContentType contentType , string name , int parentId ) = >
new ContentBuilder ( )
2020-10-07 11:23:31 +02:00
. WithId ( 0 )
. WithContentType ( contentType )
. WithName ( name )
. WithParentId ( parentId )
2020-12-05 11:12:55 +01:00
. WithPropertyValues (
new
2020-10-07 11:23:31 +02:00
{
title = name + " textpage" ,
bodyText = string . Format ( "This is a textpage based on the {0} ContentType" , contentType . Alias ) ,
keywords = "text,page,meta" ,
description = "This is the meta description for a textpage"
} )
. Build ( ) ;
2020-06-17 16:39:28 +02:00
2020-10-11 09:13:31 +02:00
public static IEnumerable < Content > CreateMultipleTextpageContent ( IContentType contentType , int parentId , int amount )
{
var list = new List < Content > ( ) ;
for ( var i = 0 ; i < amount ; i + + )
{
var name = "Textpage No-" + i ;
2020-12-05 11:12:55 +01:00
Content content = new ContentBuilder ( )
2020-10-11 09:13:31 +02:00
. WithName ( name )
. WithParentId ( parentId )
. WithContentType ( contentType )
. WithPropertyValues ( new
{
title = name + " title" ,
bodyText = $"This is a textpage based on the {contentType.Alias} ContentType" ,
keywords = "text,page,meta" ,
description = "This is the meta description for a textpage"
} )
. Build ( ) ;
list . Add ( content ) ;
}
return list ;
}
2020-10-11 22:31:54 +02:00
public static Content CreateAllTypesContent ( IContentType contentType , string name , int parentId )
{
2020-12-05 11:12:55 +01:00
Content content = new ContentBuilder ( )
2020-10-11 22:31:54 +02:00
. WithName ( name )
. WithParentId ( parentId )
. WithContentType ( contentType )
. Build ( ) ;
2020-10-14 08:02:43 +02:00
2020-10-11 22:31:54 +02:00
content . SetValue ( "isTrue" , true ) ;
content . SetValue ( "number" , 42 ) ;
content . SetValue ( "bodyText" , "Lorem Ipsum Body Text Test" ) ;
content . SetValue ( "singleLineText" , "Single Line Text Test" ) ;
content . SetValue ( "multilineText" , "Multiple lines \n in one box" ) ;
content . SetValue ( "upload" , "/media/1234/koala.jpg" ) ;
content . SetValue ( "label" , "Non-editable label" ) ;
content . SetValue ( "dateTime" , DateTime . Now . AddDays ( - 20 ) ) ;
content . SetValue ( "colorPicker" , "black" ) ;
content . SetValue ( "ddlMultiple" , "1234,1235" ) ;
content . SetValue ( "rbList" , "random" ) ;
content . SetValue ( "date" , DateTime . Now . AddDays ( - 10 ) ) ;
content . SetValue ( "ddl" , "1234" ) ;
content . SetValue ( "chklist" , "randomc" ) ;
content . SetValue ( "contentPicker" , Udi . Create ( Constants . UdiEntityType . Document , new Guid ( "74ECA1D4-934E-436A-A7C7-36CC16D4095C" ) ) . ToString ( ) ) ;
content . SetValue ( "mediaPicker" , Udi . Create ( Constants . UdiEntityType . Media , new Guid ( "44CB39C8-01E5-45EB-9CF8-E70AAF2D1691" ) ) . ToString ( ) ) ;
content . SetValue ( "memberPicker" , Udi . Create ( Constants . UdiEntityType . Member , new Guid ( "9A50A448-59C0-4D42-8F93-4F1D55B0F47D" ) ) . ToString ( ) ) ;
content . SetValue ( "multiUrlPicker" , "[{\"name\":\"https://test.com\",\"url\":\"https://test.com\"}]" ) ;
content . SetValue ( "tags" , "this,is,tags" ) ;
return content ;
}
2020-04-19 09:05:09 +02:00
int? IWithIdBuilder . Id
{
get = > _id ;
set = > _id = value ;
}
Guid ? IWithKeyBuilder . Key
{
get = > _key ;
set = > _key = value ;
}
int? IWithCreatorIdBuilder . CreatorId
{
get = > _creatorId ;
set = > _creatorId = value ;
}
DateTime ? IWithCreateDateBuilder . CreateDate
{
get = > _createDate ;
set = > _createDate = value ;
}
DateTime ? IWithUpdateDateBuilder . UpdateDate
{
get = > _updateDate ;
set = > _updateDate = value ;
}
string IWithNameBuilder . Name
{
get = > _name ;
set = > _name = value ;
}
bool? IWithTrashedBuilder . Trashed
{
get = > _trashed ;
set = > _trashed = value ;
}
int? IWithLevelBuilder . Level
{
get = > _level ;
set = > _level = value ;
}
string IWithPathBuilder . Path
{
get = > _path ;
set = > _path = value ;
}
int? IWithSortOrderBuilder . SortOrder
{
get = > _sortOrder ;
set = > _sortOrder = value ;
}
2020-10-11 09:13:31 +02:00
2020-04-19 09:05:09 +02:00
int? IWithParentIdBuilder . ParentId
{
get = > _parentId ;
set = > _parentId = value ;
}
2020-10-11 09:13:31 +02:00
2020-06-17 16:39:28 +02:00
CultureInfo IWithCultureInfoBuilder . CultureInfo
{
get = > _cultureInfo ;
set = > _cultureInfo = value ;
}
2020-10-11 09:13:31 +02:00
object IWithPropertyValues . PropertyValues
{
get = > _propertyValues ;
set = > _propertyValues = value ;
}
string IWithPropertyValues . PropertyValuesCulture
{
get = > _propertyValuesCulture ;
set = > _propertyValuesCulture = value ;
}
string IWithPropertyValues . PropertyValuesSegment
{
get = > _propertyValuesSegment ;
set = > _propertyValuesSegment = value ;
}
2020-04-19 09:05:09 +02:00
}
}