2020-12-20 08:36:11 +01:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
2014-05-12 13:48:56 +10:00
|
|
|
using System.Linq;
|
2014-05-09 13:26:16 +10:00
|
|
|
using NUnit.Framework;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Cms.Core.Models;
|
|
|
|
|
using Umbraco.Cms.Core.Models.Entities;
|
|
|
|
|
using Umbraco.Cms.Tests.Common.Builders;
|
|
|
|
|
using Umbraco.Cms.Tests.Common.Builders.Extensions;
|
2014-05-09 13:26:16 +10:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Models;
|
|
|
|
|
|
|
|
|
|
[TestFixture]
|
|
|
|
|
public class MacroTests
|
2014-05-09 13:26:16 +10:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
[SetUp]
|
|
|
|
|
public void SetUp() => _builder = new MacroBuilder();
|
2020-04-16 12:18:06 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
private MacroBuilder _builder;
|
2020-04-13 19:22:31 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void Can_Deep_Clone()
|
|
|
|
|
{
|
|
|
|
|
var macro = _builder
|
|
|
|
|
.WithId(1)
|
|
|
|
|
.WithUseInEditor(true)
|
|
|
|
|
.WithCacheDuration(3)
|
|
|
|
|
.WithAlias("test")
|
|
|
|
|
.WithName("Test")
|
|
|
|
|
.WithSource("~/script.cshtml")
|
|
|
|
|
.WithCacheByMember(true)
|
|
|
|
|
.WithDontRender(true)
|
|
|
|
|
.AddProperty()
|
|
|
|
|
.WithId(6)
|
|
|
|
|
.WithAlias("rewq")
|
|
|
|
|
.WithName("REWQ")
|
|
|
|
|
.WithSortOrder(1)
|
|
|
|
|
.WithEditorAlias("asdfasdf")
|
|
|
|
|
.Done()
|
|
|
|
|
.Build();
|
2014-05-09 13:26:16 +10:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var clone = (Macro)macro.DeepClone();
|
2014-05-09 13:26:16 +10:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.AreNotSame(clone, macro);
|
|
|
|
|
Assert.AreEqual(clone, macro);
|
|
|
|
|
Assert.AreEqual(clone.Id, macro.Id);
|
2014-05-09 13:26:16 +10:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.AreEqual(clone.Properties.Count, macro.Properties.Count);
|
2014-05-12 13:48:56 +10:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
for (var i = 0; i < clone.Properties.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
Assert.AreEqual(clone.Properties[i], macro.Properties[i]);
|
|
|
|
|
Assert.AreNotSame(clone.Properties[i], macro.Properties[i]);
|
|
|
|
|
}
|
2014-05-12 13:48:56 +10:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.AreNotSame(clone.Properties, macro.Properties);
|
|
|
|
|
Assert.AreNotSame(clone.AddedProperties, macro.AddedProperties);
|
|
|
|
|
Assert.AreNotSame(clone.RemovedProperties, macro.RemovedProperties);
|
2014-05-09 13:26:16 +10:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
// 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));
|
|
|
|
|
}
|
2014-05-09 13:26:16 +10:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
// Need to ensure the event handlers are wired.
|
|
|
|
|
var asDirty = (ICanBeDirty)clone;
|
2014-05-09 13:26:16 +10:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.IsFalse(asDirty.IsPropertyDirty("Properties"));
|
|
|
|
|
clone.Properties.Add(new MacroProperty(3, Guid.NewGuid(), "asdf", "SDF", 3, "asdfasdf"));
|
|
|
|
|
Assert.IsTrue(asDirty.IsPropertyDirty("Properties"));
|
|
|
|
|
Assert.AreEqual(1, clone.AddedProperties.Count());
|
|
|
|
|
clone.Properties.Remove("rewq");
|
|
|
|
|
Assert.AreEqual(1, clone.RemovedProperties.Count());
|
2014-05-09 13:26:16 +10:00
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
}
|