From 9d96a4411f2d98f7ae80765b48b67f870292514d Mon Sep 17 00:00:00 2001 From: Shannon Date: Fri, 9 May 2014 13:26:16 +1000 Subject: [PATCH] Fixes deep clone of macro object --- src/Umbraco.Core/Models/Macro.cs | 22 ++++++++--- src/Umbraco.Tests/Models/MacroTests.cs | 51 ++++++++++++++++++++++++++ src/Umbraco.Tests/Umbraco.Tests.csproj | 1 + 3 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 src/Umbraco.Tests/Models/MacroTests.cs diff --git a/src/Umbraco.Core/Models/Macro.cs b/src/Umbraco.Core/Models/Macro.cs index d81f49fc4f..b892b8064e 100644 --- a/src/Umbraco.Core/Models/Macro.cs +++ b/src/Umbraco.Core/Models/Macro.cs @@ -109,9 +109,9 @@ namespace Umbraco.Core.Models private string _scriptAssembly; private string _scriptPath; private string _xslt; - private readonly MacroPropertyCollection _properties; - private readonly List _addedProperties; - private readonly List _removedProperties; + private MacroPropertyCollection _properties; + private List _addedProperties; + private List _removedProperties; private static readonly PropertyInfo AliasSelector = ExpressionHelper.GetPropertyInfo(x => x.Alias); private static readonly PropertyInfo NameSelector = ExpressionHelper.GetPropertyInfo(x => x.Name); @@ -397,7 +397,19 @@ namespace Umbraco.Core.Models { get { return _properties; } } - - + + public override object DeepClone() + { + var clone = (Macro)base.DeepClone(); + + clone._addedProperties = new List(); + clone._removedProperties = new List(); + clone._properties = new MacroPropertyCollection(); + clone._properties.CollectionChanged += clone.PropertiesChanged; + + clone.ResetDirtyProperties(false); + + return clone; + } } } \ No newline at end of file diff --git a/src/Umbraco.Tests/Models/MacroTests.cs b/src/Umbraco.Tests/Models/MacroTests.cs new file mode 100644 index 0000000000..273971c93c --- /dev/null +++ b/src/Umbraco.Tests/Models/MacroTests.cs @@ -0,0 +1,51 @@ +using NUnit.Framework; +using Umbraco.Core.Models; +using Umbraco.Core.Models.EntityBase; +using Umbraco.Tests.TestHelpers; + +namespace Umbraco.Tests.Models +{ + [TestFixture] + public class MacroTests + { + [SetUp] + public void Init() + { + var config = SettingsForTests.GetDefault(); + SettingsForTests.ConfigureSettings(config); + } + + [Test] + public void Can_Deep_Clone() + { + var macro = new Macro(1, true, 3, "test", "Test", "blah", "blah", "xslt", false, true, true, "script"); + + var clone = (Macro)macro.DeepClone(); + + Assert.AreNotSame(clone, macro); + Assert.AreEqual(clone, macro); + Assert.AreEqual(clone.Id, macro.Id); + + Assert.AreNotSame(clone.Properties, macro.Properties); + Assert.AreNotSame(clone.AddedProperties, macro.AddedProperties); + Assert.AreNotSame(clone.RemovedProperties, macro.RemovedProperties); + + //This double verifies by reflection + var allProps = clone.GetType().GetProperties(); + foreach (var propertyInfo in allProps) + { + Assert.AreEqual(propertyInfo.GetValue(clone, null), propertyInfo.GetValue(macro, null)); + } + + //need to ensure the event handlers are wired + + var asDirty = (ICanBeDirty)clone; + + Assert.IsFalse(asDirty.IsPropertyDirty("Properties")); + clone.Properties.Add(new MacroProperty(3, "asdf", "SDF", 3, "asdfasdf")); + Assert.IsTrue(asDirty.IsPropertyDirty("Properties")); + + } + + } +} \ No newline at end of file diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index b36d94f3e1..fa9a84acc0 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -178,6 +178,7 @@ +