More code and tests written for deep cloning.

This commit is contained in:
Shannon
2014-04-15 20:31:32 +10:00
parent 0e4e12d0ba
commit dcac5d4709
2 changed files with 108 additions and 0 deletions

View File

@@ -438,6 +438,13 @@ namespace Umbraco.Core.Models.Membership
}
}
public override object DeepClone()
{
var clone = (User)base.DeepClone();
clone.UserType = (UserType)UserType.DeepClone();
return clone;
}
/// <summary>
/// Internal class used to wrap the user in a profile
/// </summary>
@@ -461,6 +468,24 @@ namespace Umbraco.Core.Models.Membership
get { return _user.Name; }
set { _user.Name = value; }
}
protected bool Equals(UserProfile other)
{
return _user.Equals(other._user);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((UserProfile) obj);
}
public override int GetHashCode()
{
return _user.GetHashCode();
}
}
}
}

View File

@@ -1,9 +1,92 @@
using System;
using NUnit.Framework;
using Umbraco.Core.Models;
using Umbraco.Core.Models.Membership;
namespace Umbraco.Tests.Models
{
[TestFixture]
public class UserTypeTests
{
[Test]
public void Can_Deep_Clone()
{
var item = new UserType()
{
Id = 3,
Key = Guid.NewGuid(),
UpdateDate = DateTime.Now,
CreateDate = DateTime.Now,
Name = "Test",
Alias = "test",
Permissions = new[] {"a", "b", "c"}
};
var clone = (User)item.DeepClone();
Assert.AreNotSame(clone, item);
Assert.AreEqual(clone, item);
//Verify normal properties with reflection
var allProps = clone.GetType().GetProperties();
foreach (var propertyInfo in allProps)
{
Assert.AreEqual(propertyInfo.GetValue(clone, null), propertyInfo.GetValue(item, null));
}
}
}
[TestFixture]
public class UserTests
{
[Test]
public void Can_Deep_Clone()
{
var item = new User(new UserType(){Id = 3})
{
Id = 3,
Key = Guid.NewGuid(),
UpdateDate = DateTime.Now,
CreateDate = DateTime.Now,
Name = "Test",
Comments = "comments",
DefaultPermissions = new[]{"a","b","c"},
DefaultToLiveEditing = false,
Email = "test@test.com",
Language = "en",
FailedPasswordAttempts = 3,
IsApproved = true,
IsLockedOut = true,
LastLockoutDate = DateTime.Now,
LastLoginDate = DateTime.Now,
LastPasswordChangeDate = DateTime.Now,
Password = "test pass",
PasswordAnswer = "answer",
PasswordQuestion = "question",
//ProviderUserKey = "user key",
SessionTimeout = 5,
StartContentId = 3,
StartMediaId = 8,
Username = "username"
};
var clone = (User)item.DeepClone();
Assert.AreNotSame(clone, item);
Assert.AreEqual(clone, item);
Assert.AreNotSame(clone.UserType, item.UserType);
Assert.AreEqual(clone.UserType, item.UserType);
//Verify normal properties with reflection
var allProps = clone.GetType().GetProperties();
foreach (var propertyInfo in allProps)
{
Assert.AreEqual(propertyInfo.GetValue(clone, null), propertyInfo.GetValue(item, null));
}
}
}
[TestFixture]
public class UmbracoEntityTests
{