Files
Umbraco-CMS/tests/Umbraco.Tests.UnitTests/Umbraco.Core/Models/MacroTests.cs

81 lines
2.7 KiB
C#
Raw Normal View History

// Copyright (c) Umbraco.
// See LICENSE for more details.
using System;
using System.Linq;
using System.Reflection;
2014-05-09 13:26:16 +10:00
using NUnit.Framework;
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
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Models
2014-05-09 13:26:16 +10:00
{
[TestFixture]
public class MacroTests
{
private MacroBuilder _builder;
[SetUp]
public void SetUp() => _builder = new MacroBuilder();
2014-05-09 13:26:16 +10:00
[Test]
public void Can_Deep_Clone()
{
Macro 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
var clone = (Macro)macro.DeepClone();
Assert.AreNotSame(clone, macro);
Assert.AreEqual(clone, macro);
Assert.AreEqual(clone.Id, macro.Id);
Assert.AreEqual(clone.Properties.Count, macro.Properties.Count);
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-09 13:26:16 +10:00
Assert.AreNotSame(clone.Properties, macro.Properties);
Assert.AreNotSame(clone.AddedProperties, macro.AddedProperties);
Assert.AreNotSame(clone.RemovedProperties, macro.RemovedProperties);
// This double verifies by reflection
PropertyInfo[] allProps = clone.GetType().GetProperties();
foreach (PropertyInfo propertyInfo in allProps)
{
2014-05-09 13:26:16 +10:00
Assert.AreEqual(propertyInfo.GetValue(clone, null), propertyInfo.GetValue(macro, null));
}
2014-05-09 13:26:16 +10:00
// Need to ensure the event handlers are wired.
2014-05-09 13:26:16 +10:00
var asDirty = (ICanBeDirty)clone;
Assert.IsFalse(asDirty.IsPropertyDirty("Properties"));
2017-05-12 14:49:44 +02:00
clone.Properties.Add(new MacroProperty(3, Guid.NewGuid(), "asdf", "SDF", 3, "asdfasdf"));
2014-05-09 13:26:16 +10:00
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
}