Separated member and member group builder and model unit tests, and other minor updates from PR review.

This commit is contained in:
Andy Butland
2020-03-31 17:37:34 +02:00
parent 374147d5e8
commit 919861bf36
7 changed files with 288 additions and 157 deletions
@@ -7,7 +7,7 @@ using Umbraco.Tests.Common.Builders.Interfaces;
namespace Umbraco.Tests.Common.Builders
{
public class PropertyGroupBuilder
: ChildBuilderBase<MemberTypeBuilder, PropertyGroup>, // TODO: likely want to genericise this, so can use for document and media types too.
: ChildBuilderBase<MemberTypeBuilder, PropertyGroup>, // TODO: likely want to generalise this, so can use for document and media types too.
IWithNameBuilder,
IWithSortOrderBuilder
{
@@ -11,26 +11,11 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Infrastructure.Models
{
private readonly DataTypeBuilder _builder = new DataTypeBuilder();
private const int _testId = 3123;
[Test]
public void Is_Built_Correctly()
{
// Arrange
// Act
var dtd = _builder
.WithId(_testId)
.Build();
// Assert
Assert.AreEqual(_testId, dtd.Id);
}
[Test]
public void Can_Deep_Clone()
{
var dtd = _builder
.WithId(_testId)
var dtd = _builder
.WithId(3123)
.Build();
var clone = (DataType) dtd.DeepClone();
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Newtonsoft.Json;
using NUnit.Framework;
@@ -14,34 +13,6 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Infrastructure.Models
{
private readonly MemberGroupBuilder _builder = new MemberGroupBuilder();
private const int _testId = 6;
private const string _testName = "Test Group";
private const int _testCreatorId = 4;
private readonly Guid _testKey = Guid.NewGuid();
private readonly DateTime _testCreateDate = DateTime.Now.AddHours(-1);
private readonly DateTime _testUpdateDate = DateTime.Now;
private readonly KeyValuePair<string, object> _testAdditionalData1 = new KeyValuePair<string, object>("test1", 123);
private readonly KeyValuePair<string, object> _testAdditionalData2 = new KeyValuePair<string, object>("test2", "hello");
[Test]
public void Is_Built_Correctly()
{
// Arrange
// Act
var group = BuildMemberGroup();
// Assert
Assert.AreEqual(_testId, group.Id);
Assert.AreEqual(_testKey, group.Key);
Assert.AreEqual(_testName, group.Name);
Assert.AreEqual(_testCreateDate, group.CreateDate);
Assert.AreEqual(_testUpdateDate, group.UpdateDate);
Assert.AreEqual(_testCreatorId, group.CreatorId);
Assert.AreEqual(2, group.AdditionalData.Count);
Assert.AreEqual(_testAdditionalData1.Value, group.AdditionalData[_testAdditionalData1.Key]);
Assert.AreEqual(_testAdditionalData2.Value, group.AdditionalData[_testAdditionalData2.Key]);
}
[Test]
public void Can_Deep_Clone()
{
@@ -81,15 +52,15 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Infrastructure.Models
private MemberGroup BuildMemberGroup()
{
return _builder
.WithId(_testId)
.WithKey(_testKey)
.WithName(_testName)
.WithCreatorId(_testCreatorId)
.WithCreateDate(_testCreateDate)
.WithUpdateDate(_testUpdateDate)
.WithId(6)
.WithKey(Guid.NewGuid())
.WithName("Test Group")
.WithCreatorId(4)
.WithCreateDate(DateTime.Now)
.WithUpdateDate(DateTime.Now)
.AddAdditionalData()
.WithKeyValue(_testAdditionalData1.Key, _testAdditionalData1.Value)
.WithKeyValue(_testAdditionalData2.Key, _testAdditionalData2.Value)
.WithKeyValue("test1", 123)
.WithKeyValue("test2", "hello")
.Done()
.Build();
}
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Newtonsoft.Json;
@@ -16,65 +15,6 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Infrastructure.Models
{
private readonly MemberBuilder _builder = new MemberBuilder();
private const int _testMemberTypeId = 99;
private const string _testMemberTypeAlias = "memberType";
private const string _testMemberTypeName = "Member Type";
private const string _testMemberTypePropertyGroupName = "Content";
private const int _testId = 6;
private const string _testName = "Fred";
private const string _testUsername = "fred";
private const string _testRawPasswordValue = "raw pass";
private const string _testEmail = "email@email.com";
private const int _testCreatorId = 22;
private const int _testLevel = 3;
private const string _testPath = "-1, 4, 10";
private const bool _testIsApproved = true;
private const bool _testIsLockedOut = true;
private const int _testSortOrder = 5;
private const bool _testTrashed = false;
private readonly Guid _testKey = Guid.NewGuid();
private readonly DateTime _testCreateDate = DateTime.Now.AddHours(-1);
private readonly DateTime _testUpdateDate = DateTime.Now;
private readonly DateTime _testLastLockoutDate = DateTime.Now.AddHours(-2);
private readonly DateTime _testLastLoginDate = DateTime.Now.AddHours(-3);
private readonly DateTime _testLastPasswordChangeDate = DateTime.Now.AddHours(-4);
private readonly (string alias, string name, string description, int sortOrder, int dataTypeId) _testPropertyType1 = ("title", "Title", string.Empty, 1, -88);
private readonly (string alias, string name, string description, int sortOrder, int dataTypeId) _testPropertyType2 = ("bodyText", "Body Text", string.Empty, 2, -87);
private readonly (string alias, string name, string description, int sortOrder, int dataTypeId) _testPropertyType3 = ("author", "Author", "Writer of the article", 1, -88);
private readonly string[] _testGroups = new string[] { "group1", "group2" };
private readonly KeyValuePair<string, object> _testPropertyData1 = new KeyValuePair<string, object>("title", "Name member");
private readonly KeyValuePair<string, object> _testPropertyData2 = new KeyValuePair<string, object>("bodyText", "This is a subpage");
private readonly KeyValuePair<string, object> _testPropertyData3 = new KeyValuePair<string, object>("author", "John Doe");
private readonly KeyValuePair<string, object> _testAdditionalData1 = new KeyValuePair<string, object>("test1", 123);
private readonly KeyValuePair<string, object> _testAdditionalData2 = new KeyValuePair<string, object>("test2", "hello");
[Test]
public void Is_Built_Correctly()
{
// Arrange
// Act
var member = BuildMember();
// Assert
Assert.AreEqual(_testMemberTypeId, member.ContentTypeId);
Assert.AreEqual(_testMemberTypeAlias, member.ContentType.Alias);
Assert.AreEqual(_testMemberTypeName, member.ContentType.Name);
Assert.AreEqual(_testId, member.Id);
Assert.AreEqual(_testKey, member.Key);
Assert.AreEqual(_testName, member.Name);
Assert.AreEqual(_testCreateDate, member.CreateDate);
Assert.AreEqual(_testUpdateDate, member.UpdateDate);
Assert.AreEqual(_testCreatorId, member.CreatorId);
Assert.AreEqual(_testGroups, member.Groups.ToArray());
Assert.AreEqual(10, member.Properties.Count); // 7 from membership properties group, 3 custom
Assert.AreEqual(_testPropertyData1.Value, member.GetValue<string>(_testPropertyData1.Key));
Assert.AreEqual(_testPropertyData2.Value, member.GetValue<string>(_testPropertyData2.Key));
Assert.AreEqual(_testPropertyData3.Value, member.GetValue<string>(_testPropertyData3.Key));
Assert.AreEqual(2, member.AdditionalData.Count);
Assert.AreEqual(_testAdditionalData1.Value, member.AdditionalData[_testAdditionalData1.Key]);
Assert.AreEqual(_testAdditionalData2.Value, member.AdditionalData[_testAdditionalData2.Key]);
}
[Test]
public void Can_Deep_Clone()
{
@@ -141,72 +81,69 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Infrastructure.Models
{
return _builder
.AddMemberType()
.WithId(_testMemberTypeId)
.WithAlias(_testMemberTypeAlias)
.WithName(_testMemberTypeName)
.WithId(99)
.WithAlias("memberType")
.WithName("Member Type")
.WithMembershipPropertyGroup()
.AddPropertyGroup()
.WithName(_testMemberTypePropertyGroupName)
.WithName("Content")
.WithSortOrder(1)
.AddPropertyType()
.WithPropertyEditorAlias(Constants.PropertyEditors.Aliases.TextBox)
.WithValueStorageType(ValueStorageType.Nvarchar)
.WithAlias(_testPropertyType1.alias)
.WithName(_testPropertyType1.name)
.WithSortOrder(_testPropertyType1.sortOrder)
.WithDataTypeId(_testPropertyType1.dataTypeId)
.WithAlias("title")
.WithName("Title")
.WithSortOrder(1)
.WithDataTypeId(-88)
.Done()
.AddPropertyType()
.WithPropertyEditorAlias(Constants.PropertyEditors.Aliases.TextBox)
.WithValueStorageType(ValueStorageType.Ntext)
.WithAlias(_testPropertyType2.alias)
.WithName(_testPropertyType2.name)
.WithSortOrder(_testPropertyType2.sortOrder)
.WithDataTypeId(_testPropertyType2.dataTypeId)
.WithAlias("bodyText")
.WithName("Body text")
.WithSortOrder(2)
.WithDataTypeId(-87)
.Done()
.AddPropertyType()
.WithPropertyEditorAlias(Constants.PropertyEditors.Aliases.TextBox)
.WithValueStorageType(ValueStorageType.Nvarchar)
.WithAlias(_testPropertyType3.alias)
.WithName(_testPropertyType3.name)
.WithDescription(_testPropertyType3.description)
.WithSortOrder(_testPropertyType3.sortOrder)
.WithDataTypeId(_testPropertyType3.dataTypeId)
.WithAlias("author")
.WithName("Author")
.WithDescription("Name of the author")
.WithSortOrder(3)
.WithDataTypeId(-88)
.Done()
.Done()
.Done()
.WithId(_testId)
.WithKey(_testKey)
.WithName(_testName)
.WithUserName(_testUsername)
.WithRawPasswordValue(_testRawPasswordValue)
.WithEmail(_testEmail)
.WithCreatorId(_testCreatorId)
.WithCreateDate(_testCreateDate)
.WithUpdateDate(_testUpdateDate)
.WithId(10)
.WithKey(Guid.NewGuid())
.WithName("Fred")
.WithUserName("fred")
.WithRawPasswordValue("raw pass")
.WithEmail("email@email.com")
.WithCreatorId(22)
.WithCreateDate(DateTime.Now)
.WithUpdateDate(DateTime.Now)
.WithFailedPasswordAttempts(22)
.WithLevel(_testLevel)
.WithPath(_testPath)
.WithIsApproved(_testIsApproved)
.WithIsLockedOut(_testIsLockedOut)
.WithLastLockoutDate(_testLastLockoutDate)
.WithLastLoginDate(_testLastLoginDate)
.WithLastPasswordChangeDate(_testLastPasswordChangeDate)
.WithSortOrder(_testSortOrder)
.WithTrashed(_testTrashed)
.WithLevel(3)
.WithPath("-1, 4, 10")
.WithIsApproved(true)
.WithIsLockedOut(true)
.WithSortOrder(5)
.WithTrashed(false)
.AddMemberGroups()
.WithValue(_testGroups[0])
.WithValue(_testGroups[1])
.WithValue("Group 1")
.WithValue("Group 2")
.Done()
.AddAdditionalData()
.WithKeyValue(_testAdditionalData1.Key, _testAdditionalData1.Value)
.WithKeyValue(_testAdditionalData2.Key, _testAdditionalData2.Value)
.WithKeyValue("test1", 123)
.WithKeyValue("test2", "hello")
.Done()
.WithPropertyIdsIncrementingFrom(200)
.AddPropertyData()
.WithKeyValue(_testPropertyData1.Key, _testPropertyData1.Value)
.WithKeyValue(_testPropertyData2.Key, _testPropertyData2.Value)
.WithKeyValue(_testPropertyData3.Key, _testPropertyData3.Value)
.WithKeyValue("title", "Name member")
.WithKeyValue("bodyText", "This is a subpage")
.WithKeyValue("author", "John Doe")
.Done()
.Build();
}
@@ -0,0 +1,27 @@
using NUnit.Framework;
using Umbraco.Tests.Common.Builders;
using Umbraco.Tests.Common.Builders.Extensions;
namespace Umbraco.Tests.UnitTests.Umbraco.Tests.Common.Builders
{
[TestFixture]
public class DataTypeBuilderTests
{
[Test]
public void Is_Built_Correctly()
{
// Arrange
const int testId = 3123;
var builder = new DataTypeBuilder();
// Act
var dtd = builder
.WithId(testId)
.Build();
// Assert
Assert.AreEqual(testId, dtd.Id);
}
}
}
@@ -0,0 +1,158 @@
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Tests.Common.Builders;
using Umbraco.Tests.Common.Builders.Extensions;
namespace Umbraco.Tests.UnitTests.Umbraco.Tests.Common.Builders
{
[TestFixture]
public class MemberBuilderTests
{
private class PropertyTypeDetail
{
public string Alias { get; set; }
public string Name { get; set; }
public string Description { get; set; } = string.Empty;
public int SortOrder { get; set; }
public int DataTypeId { get; set; }
}
[Test]
public void Is_Built_Correctly()
{
// Arrange
const int testMemberTypeId = 99;
const string testMemberTypeAlias = "memberType";
const string testMemberTypeName = "Member Type";
const string testMemberTypePropertyGroupName = "Content";
const int testId = 10;
const string testName = "Fred";
const string testUsername = "fred";
const string testRawPasswordValue = "raw pass";
const string testEmail = "email@email.com";
const int testCreatorId = 22;
const int testLevel = 3;
const string testPath = "-1, 4, 10";
const bool testIsApproved = true;
const bool testIsLockedOut = true;
const int testSortOrder = 5;
const bool testTrashed = false;
var testKey = Guid.NewGuid();
var testCreateDate = DateTime.Now.AddHours(-1);
var testUpdateDate = DateTime.Now;
var testLastLockoutDate = DateTime.Now.AddHours(-2);
var testLastLoginDate = DateTime.Now.AddHours(-3);
var testLastPasswordChangeDate = DateTime.Now.AddHours(-4);
var testPropertyType1 = new PropertyTypeDetail { Alias = "title", Name = "Title", SortOrder = 1, DataTypeId = -88 };
var testPropertyType2 = new PropertyTypeDetail { Alias = "bodyText", Name = "Body Text", SortOrder = 2, DataTypeId = -87 };
var testPropertyType3 = new PropertyTypeDetail { Alias = "author", Name = "Author", Description = "Writer of the article", SortOrder = 1, DataTypeId = -88 };
var testGroups = new string[] { "group1", "group2" };
var testPropertyData1 = new KeyValuePair<string, object>("title", "Name member");
var testPropertyData2 = new KeyValuePair<string, object>("bodyText", "This is a subpage");
var testPropertyData3 = new KeyValuePair<string, object>("author", "John Doe");
var testAdditionalData1 = new KeyValuePair<string, object>("test1", 123);
var testAdditionalData2 = new KeyValuePair<string, object>("test2", "hello");
var builder = new MemberBuilder();
// Act
var member = builder
.AddMemberType()
.WithId(testMemberTypeId)
.WithAlias(testMemberTypeAlias)
.WithName(testMemberTypeName)
.WithMembershipPropertyGroup()
.AddPropertyGroup()
.WithName(testMemberTypePropertyGroupName)
.WithSortOrder(1)
.AddPropertyType()
.WithPropertyEditorAlias(Constants.PropertyEditors.Aliases.TextBox)
.WithValueStorageType(ValueStorageType.Nvarchar)
.WithAlias(testPropertyType1.Alias)
.WithName(testPropertyType1.Name)
.WithSortOrder(testPropertyType1.SortOrder)
.WithDataTypeId(testPropertyType1.DataTypeId)
.Done()
.AddPropertyType()
.WithPropertyEditorAlias(Constants.PropertyEditors.Aliases.TextBox)
.WithValueStorageType(ValueStorageType.Ntext)
.WithAlias(testPropertyType2.Alias)
.WithName(testPropertyType2.Name)
.WithSortOrder(testPropertyType2.SortOrder)
.WithDataTypeId(testPropertyType2.DataTypeId)
.Done()
.AddPropertyType()
.WithPropertyEditorAlias(Constants.PropertyEditors.Aliases.TextBox)
.WithValueStorageType(ValueStorageType.Nvarchar)
.WithAlias(testPropertyType3.Alias)
.WithName(testPropertyType3.Name)
.WithDescription(testPropertyType3.Description)
.WithSortOrder(testPropertyType3.SortOrder)
.WithDataTypeId(testPropertyType3.DataTypeId)
.Done()
.Done()
.Done()
.WithId(testId)
.WithKey(testKey)
.WithName(testName)
.WithUserName(testUsername)
.WithRawPasswordValue(testRawPasswordValue)
.WithEmail(testEmail)
.WithCreatorId(testCreatorId)
.WithCreateDate(testCreateDate)
.WithUpdateDate(testUpdateDate)
.WithFailedPasswordAttempts(22)
.WithLevel(testLevel)
.WithPath(testPath)
.WithIsApproved(testIsApproved)
.WithIsLockedOut(testIsLockedOut)
.WithLastLockoutDate(testLastLockoutDate)
.WithLastLoginDate(testLastLoginDate)
.WithLastPasswordChangeDate(testLastPasswordChangeDate)
.WithSortOrder(testSortOrder)
.WithTrashed(testTrashed)
.AddMemberGroups()
.WithValue(testGroups[0])
.WithValue(testGroups[1])
.Done()
.AddAdditionalData()
.WithKeyValue(testAdditionalData1.Key, testAdditionalData1.Value)
.WithKeyValue(testAdditionalData2.Key, testAdditionalData2.Value)
.Done()
.WithPropertyIdsIncrementingFrom(200)
.AddPropertyData()
.WithKeyValue(testPropertyData1.Key, testPropertyData1.Value)
.WithKeyValue(testPropertyData2.Key, testPropertyData2.Value)
.WithKeyValue(testPropertyData3.Key, testPropertyData3.Value)
.Done()
.Build();
// Assert
Assert.AreEqual(testMemberTypeId, member.ContentTypeId);
Assert.AreEqual(testMemberTypeAlias, member.ContentType.Alias);
Assert.AreEqual(testMemberTypeName, member.ContentType.Name);
Assert.AreEqual(testId, member.Id);
Assert.AreEqual(testKey, member.Key);
Assert.AreEqual(testName, member.Name);
Assert.AreEqual(testCreateDate, member.CreateDate);
Assert.AreEqual(testUpdateDate, member.UpdateDate);
Assert.AreEqual(testCreatorId, member.CreatorId);
Assert.AreEqual(testGroups, member.Groups.ToArray());
Assert.AreEqual(10, member.Properties.Count); // 7 from membership properties group, 3 custom
Assert.AreEqual(testPropertyData1.Value, member.GetValue<string>(testPropertyData1.Key));
Assert.AreEqual(testPropertyData2.Value, member.GetValue<string>(testPropertyData2.Key));
Assert.AreEqual(testPropertyData3.Value, member.GetValue<string>(testPropertyData3.Key));
Assert.AreEqual(2, member.AdditionalData.Count);
Assert.AreEqual(testAdditionalData1.Value, member.AdditionalData[testAdditionalData1.Key]);
Assert.AreEqual(testAdditionalData2.Value, member.AdditionalData[testAdditionalData2.Key]);
}
}
}
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;
using Umbraco.Tests.Common.Builders;
using Umbraco.Tests.Common.Builders.Extensions;
namespace Umbraco.Tests.UnitTests.Umbraco.Tests.Common.Builders
{
[TestFixture]
public class MemberGroupBuilderTests
{
[Test]
public void Is_Built_Correctly()
{
// Arrange
const int testId = 6;
const string testName = "Test Group";
const int testCreatorId = 4;
var testKey = Guid.NewGuid();
var testCreateDate = DateTime.Now.AddHours(-1);
var testUpdateDate = DateTime.Now;
var testAdditionalData1 = new KeyValuePair<string, object>("test1", 123);
var testAdditionalData2 = new KeyValuePair<string, object>("test2", "hello");
var builder = new MemberGroupBuilder();
// Act
var group = builder
.WithId(testId)
.WithKey(testKey)
.WithName(testName)
.WithCreatorId(testCreatorId)
.WithCreateDate(testCreateDate)
.WithUpdateDate(testUpdateDate)
.AddAdditionalData()
.WithKeyValue(testAdditionalData1.Key, testAdditionalData1.Value)
.WithKeyValue(testAdditionalData2.Key, testAdditionalData2.Value)
.Done()
.Build();
// Assert
Assert.AreEqual(testId, group.Id);
Assert.AreEqual(testKey, group.Key);
Assert.AreEqual(testName, group.Name);
Assert.AreEqual(testCreateDate, group.CreateDate);
Assert.AreEqual(testUpdateDate, group.UpdateDate);
Assert.AreEqual(testCreatorId, group.CreatorId);
Assert.AreEqual(3, group.AdditionalData.Count); // previousName is added as part of the MemberGroup construction, plus the 2 we've added.
Assert.AreEqual(testAdditionalData1.Value, group.AdditionalData[testAdditionalData1.Key]);
Assert.AreEqual(testAdditionalData2.Value, group.AdditionalData[testAdditionalData2.Key]);
}
}
}