From 92602e3480aa055ddaa6af40fb852fc0778d5aa9 Mon Sep 17 00:00:00 2001 From: Shannon Deminick Date: Sat, 25 Aug 2012 07:07:00 +0700 Subject: [PATCH] Added unit tests for legacy DynamicNode which now just runs tests on the abstract class so we share tests between DynamicDocument and DynamicNode. Fixes: #U4-691 Fixes: #U4-690 --- .hgignore | 1 + .../Configuration/UmbracoSettings.cs | 105 ++--- .../DynamicDocumentCustomExtensionMethods.cs | 35 ++ .../DynamicDocument/DynamicDocumentTests.cs | 77 ++++ .../DynamicDocumentTestsBase.cs | 241 ++++++++++++ .../DynamicDocument/DynamicNodeTests.cs | 46 +++ src/Umbraco.Tests/DynamicDocumentTests.cs | 365 ------------------ src/Umbraco.Tests/Umbraco.Tests.csproj | 6 +- .../umbraco/developer/Xslt/editXslt.aspx | 15 +- .../RazorDynamicNode/DynamicNode.cs | 97 ++--- .../RazorDynamicNode/DynamicNodeList.cs | 15 +- .../RazorDynamicNode/DynamicNodeWalker.cs | 18 +- 12 files changed, 531 insertions(+), 490 deletions(-) create mode 100644 src/Umbraco.Tests/DynamicDocument/DynamicDocumentCustomExtensionMethods.cs create mode 100644 src/Umbraco.Tests/DynamicDocument/DynamicDocumentTests.cs create mode 100644 src/Umbraco.Tests/DynamicDocument/DynamicDocumentTestsBase.cs create mode 100644 src/Umbraco.Tests/DynamicDocument/DynamicNodeTests.cs delete mode 100644 src/Umbraco.Tests/DynamicDocumentTests.cs diff --git a/.hgignore b/.hgignore index fde466ac03..d460c85be5 100644 --- a/.hgignore +++ b/.hgignore @@ -46,3 +46,4 @@ src/Umbraco.Tests/config/trees.config src/Umbraco.Web.UI/web.config src/Umbraco.Tests/config/404handlers.config src/Umbraco.Web.UI/Views/* +src/Umbraco.Tests/config/umbracoSettings.config diff --git a/src/Umbraco.Core/Configuration/UmbracoSettings.cs b/src/Umbraco.Core/Configuration/UmbracoSettings.cs index 48242226b0..12643fa10c 100644 --- a/src/Umbraco.Core/Configuration/UmbracoSettings.cs +++ b/src/Umbraco.Core/Configuration/UmbracoSettings.cs @@ -2,6 +2,7 @@ using System; using System.Diagnostics; using System.IO; using System.Linq; +using System.Threading; using System.Web; using System.Web.Caching; using System.Xml; @@ -434,59 +435,65 @@ namespace Umbraco.Core.Configuration } } - public static IEnumerable RazorDataTypeModelStaticMapping + private static IEnumerable _razorDataTypeModelStaticMapping; + private static readonly ReaderWriterLockSlim Lock = new ReaderWriterLockSlim(); + + public static IEnumerable RazorDataTypeModelStaticMapping { get { - if (HttpContext.Current != null && HttpContext.Current.Cache != null && HttpContext.Current.Cache["settings.scripting.razor.dataTypeModelStaticMappings"] != null) - { - return HttpContext.Current.Cache["settings.scripting.razor.dataTypeModelStaticMappings"] as List; - } - /* - - DigibizAdvancedMediaPicker.RazorModel.ModelBinder - DigibizAdvancedMediaPicker.RazorModel.ModelBinder - - */ - List items = new List(); - XmlNode root = GetKeyAsNode("/settings/scripting/razor/dataTypeModelStaticMappings"); - if (root != null) - { - foreach (XmlNode element in root.SelectNodes(".//mapping")) - { - string propertyTypeAlias = null, nodeTypeAlias = null; - Guid? dataTypeGuid = null; - if (!string.IsNullOrEmpty(element.InnerText)) - { - if (element.Attributes["dataTypeGuid"] != null) - { - dataTypeGuid = (Guid?)new Guid(element.Attributes["dataTypeGuid"].Value); - } - if (element.Attributes["propertyTypeAlias"] != null && !string.IsNullOrEmpty(element.Attributes["propertyTypeAlias"].Value)) - { - propertyTypeAlias = element.Attributes["propertyTypeAlias"].Value; - } - if (element.Attributes["nodeTypeAlias"] != null && !string.IsNullOrEmpty(element.Attributes["nodeTypeAlias"].Value)) - { - nodeTypeAlias = element.Attributes["nodeTypeAlias"].Value; - } - items.Add(new RazorDataTypeModelStaticMappingItem() - { - DataTypeGuid = dataTypeGuid, - PropertyTypeAlias = propertyTypeAlias, - NodeTypeAlias = nodeTypeAlias, - TypeName = element.InnerText, - Raw = element.OuterXml - }); - } - } - } - if (HttpContext.Current != null && HttpContext.Current.Cache != null) - { - HttpContext.Current.Cache.Add("settings.scripting.razor.dataTypeModelStaticMappings", items, null, Cache.NoAbsoluteExpiration, new TimeSpan(0, 20, 0), CacheItemPriority.AboveNormal, null); - } - return items; + /* + + DigibizAdvancedMediaPicker.RazorModel.ModelBinder + DigibizAdvancedMediaPicker.RazorModel.ModelBinder + + */ + using (var l = new UpgradeableReadLock(Lock)) + { + if (_razorDataTypeModelStaticMapping == null) + { + l.UpgradeToWriteLock(); + + List items = new List(); + XmlNode root = GetKeyAsNode("/settings/scripting/razor/dataTypeModelStaticMappings"); + if (root != null) + { + foreach (XmlNode element in root.SelectNodes(".//mapping")) + { + string propertyTypeAlias = null, nodeTypeAlias = null; + Guid? dataTypeGuid = null; + if (!string.IsNullOrEmpty(element.InnerText)) + { + if (element.Attributes["dataTypeGuid"] != null) + { + dataTypeGuid = (Guid?)new Guid(element.Attributes["dataTypeGuid"].Value); + } + if (element.Attributes["propertyTypeAlias"] != null && !string.IsNullOrEmpty(element.Attributes["propertyTypeAlias"].Value)) + { + propertyTypeAlias = element.Attributes["propertyTypeAlias"].Value; + } + if (element.Attributes["nodeTypeAlias"] != null && !string.IsNullOrEmpty(element.Attributes["nodeTypeAlias"].Value)) + { + nodeTypeAlias = element.Attributes["nodeTypeAlias"].Value; + } + items.Add(new RazorDataTypeModelStaticMappingItem() + { + DataTypeGuid = dataTypeGuid, + PropertyTypeAlias = propertyTypeAlias, + NodeTypeAlias = nodeTypeAlias, + TypeName = element.InnerText, + Raw = element.OuterXml + }); + } + } + } + + _razorDataTypeModelStaticMapping = items; + } + + return _razorDataTypeModelStaticMapping; + } } } diff --git a/src/Umbraco.Tests/DynamicDocument/DynamicDocumentCustomExtensionMethods.cs b/src/Umbraco.Tests/DynamicDocument/DynamicDocumentCustomExtensionMethods.cs new file mode 100644 index 0000000000..8c7f8af33f --- /dev/null +++ b/src/Umbraco.Tests/DynamicDocument/DynamicDocumentCustomExtensionMethods.cs @@ -0,0 +1,35 @@ +using System.Collections.Generic; +using Umbraco.Core.Dynamics; + +namespace Umbraco.Tests.DynamicDocument +{ + public static class DynamicDocumentCustomExtensionMethods + { + + public static string DynamicDocumentNoParameters(this Core.Dynamics.DynamicDocument doc) + { + return "Hello world"; + } + + public static string DynamicDocumentCustomString(this Core.Dynamics.DynamicDocument doc, string custom) + { + return custom; + } + + public static string DynamicDocumentMultiParam(this Core.Dynamics.DynamicDocument doc, string custom, int i, bool b) + { + return custom + i + b; + } + + public static string DynamicDocumentListMultiParam(this DynamicDocumentList doc, string custom, int i, bool b) + { + return custom + i + b; + } + + public static string DynamicDocumentEnumerableMultiParam(this IEnumerable doc, string custom, int i, bool b) + { + return custom + i + b; + } + + } +} \ No newline at end of file diff --git a/src/Umbraco.Tests/DynamicDocument/DynamicDocumentTests.cs b/src/Umbraco.Tests/DynamicDocument/DynamicDocumentTests.cs new file mode 100644 index 0000000000..666b98b5dc --- /dev/null +++ b/src/Umbraco.Tests/DynamicDocument/DynamicDocumentTests.cs @@ -0,0 +1,77 @@ +using System; +using NUnit.Framework; +using Umbraco.Core.Dynamics; +using Umbraco.Core.PropertyEditors; +using Umbraco.Web; +using umbraco.BusinessLogic; +using umbraco.cms.businesslogic.template; + +namespace Umbraco.Tests.DynamicDocument +{ + [TestFixture] + public class DynamicDocumentTests : DynamicDocumentTestsBase + { + public override void Initialize() + { + base.Initialize(); + + DynamicDocumentDataSourceResolver.Current = new DynamicDocumentDataSourceResolver( + new TestDynamicDocumentDataSource()); + + PropertyEditorValueConvertersResolver.Current = new PropertyEditorValueConvertersResolver( + new[] + { + typeof(DatePickerPropertyEditorValueConverter), + typeof(TinyMcePropertyEditorValueConverter), + typeof(YesNoPropertyEditorValueConverter) + }); + } + + public override void TearDown() + { + base.TearDown(); + + DynamicDocumentDataSourceResolver.Reset(); + PropertyEditorValueConvertersResolver.Reset(); + } + + private class TestDynamicDocumentDataSource : IDynamicDocumentDataSource + { + public Guid GetDataType(string docTypeAlias, string propertyAlias) + { + if (propertyAlias == "content") + { + //return the rte type id + return Guid.Parse("5e9b75ae-face-41c8-b47e-5f4b0fd82f83"); + } + + + return Guid.Empty; + } + } + + protected override dynamic GetDynamicNode(int id) + { + var template = Template.MakeNew("test", new User(0)); + var ctx = GetUmbracoContext("/test", template); + var contentStore = new XmlPublishedContentStore(); + var doc = contentStore.GetDocumentById(ctx, id); + Assert.IsNotNull(doc); + var dynamicNode = new Core.Dynamics.DynamicDocument(doc); + Assert.IsNotNull(dynamicNode); + return dynamicNode.AsDynamic(); + } + + [Test] + public void Custom_Extension_Methods() + { + var asDynamic = GetDynamicNode(1173); + + Assert.AreEqual("Hello world", asDynamic.DynamicDocumentNoParameters()); + Assert.AreEqual("Hello world!", asDynamic.DynamicDocumentCustomString("Hello world!")); + Assert.AreEqual("Hello world!" + 123 + false, asDynamic.DynamicDocumentMultiParam("Hello world!", 123, false)); + Assert.AreEqual("Hello world!" + 123 + false, asDynamic.Children.DynamicDocumentListMultiParam("Hello world!", 123, false)); + Assert.AreEqual("Hello world!" + 123 + false, asDynamic.Children.DynamicDocumentEnumerableMultiParam("Hello world!", 123, false)); + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Tests/DynamicDocument/DynamicDocumentTestsBase.cs b/src/Umbraco.Tests/DynamicDocument/DynamicDocumentTestsBase.cs new file mode 100644 index 0000000000..1adabb1833 --- /dev/null +++ b/src/Umbraco.Tests/DynamicDocument/DynamicDocumentTestsBase.cs @@ -0,0 +1,241 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using NUnit.Framework; +using Umbraco.Core; +using Umbraco.Tests.TestHelpers; + +namespace Umbraco.Tests.DynamicDocument +{ + [TestFixture] + public abstract class DynamicDocumentTestsBase : BaseWebTest + { + /// + /// Returns the dynamic node/document to run tests against + /// + /// + /// + protected abstract dynamic GetDynamicNode(int id); + + [Test] + public void Ensure_TinyMCE_Converted_Type_User_Property() + { + var asDynamic = GetDynamicNode(1173); + + Assert.IsTrue(TypeHelper.IsTypeAssignableFrom(asDynamic.Content.GetType())); + Assert.AreEqual("
This is some content
", asDynamic.Content.ToString()); + } + + [Test] + public void Get_Children_With_Pluralized_Alias() + { + var asDynamic = GetDynamicNode(1173); + + Action doAssert = d => + { + Assert.IsTrue(TypeHelper.IsTypeAssignableFrom(d)); + var casted = (IEnumerable)d; + Assert.AreEqual(2, casted.Count()); + }; + + doAssert(asDynamic.Homes); //pluralized alias + doAssert(asDynamic.homes); //pluralized alias + doAssert(asDynamic.CustomDocuments); //pluralized alias + doAssert(asDynamic.customDocuments); //pluralized alias + } + + [Test] + public void GetPropertyValue_Non_Reflected() + { + var asDynamic = GetDynamicNode(1174); + + Assert.AreEqual("Custom data with same property name as the member name", asDynamic.GetPropertyValue("creatorName")); + Assert.AreEqual("Custom data with same property name as the member name", asDynamic.GetPropertyValue("CreatorName")); + } + + [Test] + public void GetPropertyValue_Reflected() + { + var asDynamic = GetDynamicNode(1174); + + Assert.AreEqual("admin", asDynamic.GetPropertyValue("@creatorName")); + Assert.AreEqual("admin", asDynamic.GetPropertyValue("@CreatorName")); + } + + [Test] + public void Get_User_Property_With_Same_Name_As_Member_Property() + { + var asDynamic = GetDynamicNode(1174); + + Assert.AreEqual("Custom data with same property name as the member name", asDynamic.creatorName); + + //because CreatorName is defined on DynamicNode, it will not return the user defined property + Assert.AreEqual("admin", asDynamic.CreatorName); + } + + [Test] + public void Get_Member_Property() + { + var asDynamic = GetDynamicNode(1173); + + Assert.AreEqual((int) 2, (int) asDynamic.Level); + Assert.AreEqual((int) 2, (int) asDynamic.level); + + Assert.AreEqual((int) 1046, (int) asDynamic.ParentId); + Assert.AreEqual((int) 1046, (int) asDynamic.parentId); + } + + [Test] + public void Get_Children() + { + var asDynamic = GetDynamicNode(1173); + + var children = asDynamic.Children; + Assert.IsTrue(TypeHelper.IsTypeAssignableFrom(children)); + + var childrenAsList = asDynamic.ChildrenAsList; //test ChildrenAsList too + Assert.IsTrue(TypeHelper.IsTypeAssignableFrom(childrenAsList)); + + var castChildren = (IEnumerable)children; + Assert.AreEqual(4, castChildren.Count()); + + var castChildrenAsList = (IEnumerable)childrenAsList; + Assert.AreEqual(4, castChildrenAsList.Count()); + } + + [Test] + public void Ancestor_Or_Self() + { + var asDynamic = GetDynamicNode(1173); + + var result = asDynamic.AncestorOrSelf(); + + Assert.IsNotNull(result); + + Assert.AreEqual((int) 1046, (int) result.Id); + } + + [Test] + public void Ancestors_Or_Self() + { + var asDynamic = GetDynamicNode(1174); + + var result = asDynamic.AncestorsOrSelf(); + + Assert.IsNotNull(result); + + var list = (IEnumerable)result; + Assert.AreEqual(3, list.Count()); + Assert.IsTrue(list.Select(x => ((dynamic)x).Id).ContainsAll(new dynamic[] { 1174, 1173, 1046 })); + } + + [Test] + public void Ancestors() + { + var asDynamic = GetDynamicNode(1174); + + var result = asDynamic.Ancestors(); + + Assert.IsNotNull(result); + + var list = (IEnumerable)result; + Assert.AreEqual(2, list.Count()); + Assert.IsTrue(list.Select(x => ((dynamic)x).Id).ContainsAll(new dynamic[] { 1173, 1046 })); + } + + [Test] + public void Descendants_Or_Self() + { + var asDynamic = GetDynamicNode(1046); + + var result = asDynamic.DescendantsOrSelf(); + + Assert.IsNotNull(result); + + var list = (IEnumerable)result; + Assert.AreEqual(7, list.Count()); + Assert.IsTrue(list.Select(x => ((dynamic)x).Id).ContainsAll(new dynamic[] { 1046, 1173, 1174, 1176, 1175 })); + } + + [Test] + public void Descendants() + { + var asDynamic = GetDynamicNode(1046); + + var result = asDynamic.Descendants(); + + Assert.IsNotNull(result); + + var list = (IEnumerable)result; + Assert.AreEqual(6, list.Count()); + Assert.IsTrue(list.Select(x => ((dynamic)x).Id).ContainsAll(new dynamic[] { 1173, 1174, 1176, 1175 })); + } + + [Test] + public void Up() + { + var asDynamic = GetDynamicNode(1173); + + var result = asDynamic.Up(); + + Assert.IsNotNull(result); + + Assert.AreEqual((int) 1046, (int) result.Id); + } + + [Test] + public void Down() + { + var asDynamic = GetDynamicNode(1173); + + var result = asDynamic.Down(); + + Assert.IsNotNull(result); + + Assert.AreEqual((int) 1174, (int) result.Id); + } + + [Test] + public void Next() + { + var asDynamic = GetDynamicNode(1173); + + var result = asDynamic.Next(); + + Assert.IsNotNull(result); + + Assert.AreEqual((int) 1175, (int) result.Id); + } + + [Test] + public void Next_Without_Sibling() + { + var asDynamic = GetDynamicNode(1178); + + Assert.IsNull(asDynamic.Next()); + } + + [Test] + public void Previous_Without_Sibling() + { + var asDynamic = GetDynamicNode(1173); + + Assert.IsNull(asDynamic.Previous()); + } + + [Test] + public void Previous() + { + var asDynamic = GetDynamicNode(1176); + + var result = asDynamic.Previous(); + + Assert.IsNotNull(result); + + Assert.AreEqual((int) 1174, (int) result.Id); + } + + } +} \ No newline at end of file diff --git a/src/Umbraco.Tests/DynamicDocument/DynamicNodeTests.cs b/src/Umbraco.Tests/DynamicDocument/DynamicNodeTests.cs new file mode 100644 index 0000000000..d21e3ad34d --- /dev/null +++ b/src/Umbraco.Tests/DynamicDocument/DynamicNodeTests.cs @@ -0,0 +1,46 @@ +using System.IO; +using NUnit.Framework; +using Umbraco.Core.Configuration; +using Umbraco.Tests.TestHelpers; +using Umbraco.Web; +using umbraco.BusinessLogic; +using umbraco.IO; +using umbraco.MacroEngines; +using umbraco.NodeFactory; +using umbraco.cms.businesslogic.template; +using System.Linq; + +namespace Umbraco.Tests.DynamicDocument +{ + [TestFixture] + public class DynamicNodeTests : DynamicDocumentTestsBase + { + public override void Initialize() + { + base.Initialize(); + //copy the umbraco settings file over + var currDir = new DirectoryInfo(TestHelper.CurrentAssemblyDirectory); + File.Copy( + currDir.Parent.Parent.Parent.GetDirectories("Umbraco.Web.UI") + .First() + .GetDirectories("config").First() + .GetFiles("umbracoSettings.Release.config").First().FullName, + Path.Combine(currDir.Parent.Parent.FullName, "config", "umbracoSettings.config"), + true); + + UmbracoSettings.SettingsFilePath = IOHelper.MapPath(SystemDirectories.Config, false); + } + + protected override dynamic GetDynamicNode(int id) + { + var template = Template.MakeNew("test", new User(0)); + var ctx = GetUmbracoContext("/test", template); + var contentStore = new XmlPublishedContentStore(); + var node = new DynamicNode( + new DynamicBackingItem( + new Node(ctx.GetXml().SelectSingleNode("//*[@id='" + id + "' and @isDoc]")))); + Assert.IsNotNull(node); + return (dynamic) node; + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Tests/DynamicDocumentTests.cs b/src/Umbraco.Tests/DynamicDocumentTests.cs deleted file mode 100644 index 120e8cd992..0000000000 --- a/src/Umbraco.Tests/DynamicDocumentTests.cs +++ /dev/null @@ -1,365 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Linq; -using System.Web; -using NUnit.Framework; -using Umbraco.Core; -using Umbraco.Core.Dynamics; -using Umbraco.Core.Models; -using Umbraco.Core.PropertyEditors; -using Umbraco.Tests.TestHelpers; -using Umbraco.Web; -using umbraco.BusinessLogic; -using umbraco.cms.businesslogic.template; - -namespace Umbraco.Tests -{ - [TestFixture] - public class DynamicDocumentTests : BaseWebTest - { - public override void Initialize() - { - base.Initialize(); - - DynamicDocumentDataSourceResolver.Current = new DynamicDocumentDataSourceResolver( - new TestDynamicDocumentDataSource()); - - PropertyEditorValueConvertersResolver.Current = new PropertyEditorValueConvertersResolver( - new[] - { - typeof(DatePickerPropertyEditorValueConverter), - typeof(TinyMcePropertyEditorValueConverter), - typeof(YesNoPropertyEditorValueConverter) - }); - } - - public override void TearDown() - { - base.TearDown(); - - DynamicDocumentDataSourceResolver.Reset(); - PropertyEditorValueConvertersResolver.Reset(); - } - - private DynamicDocument GetDynamicNode(int id) - { - var template = Template.MakeNew("test", new User(0)); - var ctx = GetUmbracoContext("/test", template); - var contentStore = new XmlPublishedContentStore(); - var doc = contentStore.GetDocumentById(ctx, id); - Assert.IsNotNull(doc); - var dynamicNode = new DynamicDocument(doc); - Assert.IsNotNull(dynamicNode); - return dynamicNode; - } - - [Test] - public void Custom_Extension_Methods() - { - var dynamicNode = GetDynamicNode(1173); - var asDynamic = dynamicNode.AsDynamic(); - - Assert.AreEqual("Hello world", asDynamic.DynamicDocumentNoParameters()); - Assert.AreEqual("Hello world!", asDynamic.DynamicDocumentCustomString("Hello world!")); - Assert.AreEqual("Hello world!" + 123 + false, asDynamic.DynamicDocumentMultiParam("Hello world!", 123, false)); - Assert.AreEqual("Hello world!" + 123 + false, asDynamic.Children.DynamicDocumentListMultiParam("Hello world!", 123, false)); - Assert.AreEqual("Hello world!" + 123 + false, asDynamic.Children.DynamicDocumentEnumerableMultiParam("Hello world!", 123, false)); - } - - - [Test] - public void Custom_Extension_Method_With_Params() - { - var dynamicNode = GetDynamicNode(1173); - var asDynamic = dynamicNode.AsDynamic(); - - Assert.AreEqual("Hello world", asDynamic.ReturnHelloWorld()); - } - - [Test] - public void Ensure_TinyMCE_Converted_Type_User_Property() - { - var dynamicNode = GetDynamicNode(1173); - var asDynamic = dynamicNode.AsDynamic(); - - Assert.IsTrue(TypeHelper.IsTypeAssignableFrom(asDynamic.Content.GetType())); - Assert.AreEqual("
This is some content
", asDynamic.Content.ToString()); - } - - [Test] - public void Get_Children_With_Pluralized_Alias() - { - var dynamicNode = GetDynamicNode(1173); - var asDynamic = dynamicNode.AsDynamic(); - - Action doAssert = d => - { - Assert.IsTrue(TypeHelper.IsTypeAssignableFrom(d)); - var casted = (IEnumerable)d; - Assert.AreEqual(2, casted.Count()); - }; - - doAssert(asDynamic.Homes); //pluralized alias - doAssert(asDynamic.homes); //pluralized alias - doAssert(asDynamic.CustomDocuments); //pluralized alias - doAssert(asDynamic.customDocuments); //pluralized alias - } - - [Test] - public void GetPropertyValue_Non_Reflected() - { - var dynamicNode = GetDynamicNode(1174); - var asDynamic = dynamicNode.AsDynamic(); - - Assert.AreEqual("Custom data with same property name as the member name", asDynamic.GetPropertyValue("creatorName")); - Assert.AreEqual("Custom data with same property name as the member name", asDynamic.GetPropertyValue("CreatorName")); - } - - [Test] - public void GetPropertyValue_Reflected() - { - var dynamicNode = GetDynamicNode(1174); - var asDynamic = dynamicNode.AsDynamic(); - - Assert.AreEqual("admin", asDynamic.GetPropertyValue("@creatorName")); - Assert.AreEqual("admin", asDynamic.GetPropertyValue("@CreatorName")); - } - - [Test] - public void Get_User_Property_With_Same_Name_As_Member_Property() - { - var dynamicNode = GetDynamicNode(1174); - var asDynamic = dynamicNode.AsDynamic(); - - Assert.AreEqual("Custom data with same property name as the member name", asDynamic.creatorName); - - //because CreatorName is defined on DynamicNode, it will not return the user defined property - Assert.AreEqual("admin", asDynamic.CreatorName); - } - - [Test] - public void Get_Member_Property() - { - var dynamicNode = GetDynamicNode(1173); - var asDynamic = dynamicNode.AsDynamic(); - - Assert.AreEqual(2, asDynamic.Level); - Assert.AreEqual(2, asDynamic.level); - - Assert.AreEqual(1046, asDynamic.ParentId); - Assert.AreEqual(1046, asDynamic.parentId); - } - - [Test] - public void Get_Children() - { - var dynamicNode = GetDynamicNode(1173); - var asDynamic = dynamicNode.AsDynamic(); - - var children = asDynamic.Children; - Assert.IsTrue(TypeHelper.IsTypeAssignableFrom(children)); - - var childrenAsList = asDynamic.ChildrenAsList; //test ChildrenAsList too - Assert.IsTrue(TypeHelper.IsTypeAssignableFrom(childrenAsList)); - - var castChildren = (IEnumerable)children; - Assert.AreEqual(4, castChildren.Count()); - - var castChildrenAsList = (IEnumerable)childrenAsList; - Assert.AreEqual(4, castChildrenAsList.Count()); - } - - [Test] - public void Ancestor_Or_Self() - { - var dynamicNode = GetDynamicNode(1173); - var asDynamic = dynamicNode.AsDynamic(); - - var result = asDynamic.AncestorOrSelf(); - - Assert.IsNotNull(result); - - Assert.AreEqual(1046, result.Id); - } - - [Test] - public void Ancestors_Or_Self() - { - var dynamicNode = GetDynamicNode(1174); - var asDynamic = dynamicNode.AsDynamic(); - - var result = asDynamic.AncestorsOrSelf(); - - Assert.IsNotNull(result); - - var list = (IEnumerable)result; - Assert.AreEqual(3, list.Count()); - Assert.IsTrue(list.Select(x => x.Id).ContainsAll(new[] { 1174, 1173, 1046 })); - } - - [Test] - public void Ancestors() - { - var dynamicNode = GetDynamicNode(1174); - var asDynamic = dynamicNode.AsDynamic(); - - var result = asDynamic.Ancestors(); - - Assert.IsNotNull(result); - - var list = (IEnumerable)result; - Assert.AreEqual(2, list.Count()); - Assert.IsTrue(list.Select(x => x.Id).ContainsAll(new[] { 1173, 1046 })); - } - - [Test] - public void Descendants_Or_Self() - { - var dynamicNode = GetDynamicNode(1046); - var asDynamic = dynamicNode.AsDynamic(); - - var result = asDynamic.DescendantsOrSelf(); - - Assert.IsNotNull(result); - - var list = (IEnumerable)result; - Assert.AreEqual(7, list.Count()); - Assert.IsTrue(list.Select(x => x.Id).ContainsAll(new[] { 1046, 1173, 1174, 1176, 1175 })); - } - - [Test] - public void Descendants() - { - var dynamicNode = GetDynamicNode(1046); - var asDynamic = dynamicNode.AsDynamic(); - - var result = asDynamic.Descendants(); - - Assert.IsNotNull(result); - - var list = (IEnumerable)result; - Assert.AreEqual(6, list.Count()); - Assert.IsTrue(list.Select(x => x.Id).ContainsAll(new[] { 1173, 1174, 1176, 1175 })); - } - - [Test] - public void Up() - { - var dynamicNode = GetDynamicNode(1173); - var asDynamic = dynamicNode.AsDynamic(); - - var result = asDynamic.Up(); - - Assert.IsNotNull(result); - - Assert.AreEqual(1046, result.Id); - } - - [Test] - public void Down() - { - var dynamicNode = GetDynamicNode(1173); - var asDynamic = dynamicNode.AsDynamic(); - - var result = asDynamic.Down(); - - Assert.IsNotNull(result); - - Assert.AreEqual(1174, result.Id); - } - - [Test] - public void Next() - { - var dynamicNode = GetDynamicNode(1173); - var asDynamic = dynamicNode.AsDynamic(); - - var result = asDynamic.Next(); - - Assert.IsNotNull(result); - - Assert.AreEqual(1175, result.Id); - } - - [Test] - public void Next_Without_Sibling() - { - var dynamicNode = GetDynamicNode(1178); - var asDynamic = dynamicNode.AsDynamic(); - - Assert.IsNull(asDynamic.Next()); - } - - [Test] - public void Previous_Without_Sibling() - { - var dynamicNode = GetDynamicNode(1173); - var asDynamic = dynamicNode.AsDynamic(); - - Assert.IsNull(asDynamic.Previous()); - } - - [Test] - public void Previous() - { - var dynamicNode = GetDynamicNode(1176); - var asDynamic = dynamicNode.AsDynamic(); - - var result = asDynamic.Previous(); - - Assert.IsNotNull(result); - - Assert.AreEqual(1174, result.Id); - } - - #region Classes used in test - - private class TestDynamicDocumentDataSource : IDynamicDocumentDataSource - { - public Guid GetDataType(string docTypeAlias, string propertyAlias) - { - if (propertyAlias == "content") - { - //return the rte type id - return Guid.Parse("5e9b75ae-face-41c8-b47e-5f4b0fd82f83"); - } - - - return Guid.Empty; - } - } - - #endregion - } - - public static class DynamicDocumentCustomExtensionMethods - { - - public static string DynamicDocumentNoParameters(this DynamicDocument doc) - { - return "Hello world"; - } - - public static string DynamicDocumentCustomString(this DynamicDocument doc, string custom) - { - return custom; - } - - public static string DynamicDocumentMultiParam(this DynamicDocument doc, string custom, int i, bool b) - { - return custom + i + b; - } - - public static string DynamicDocumentListMultiParam(this DynamicDocumentList doc, string custom, int i, bool b) - { - return custom + i + b; - } - - public static string DynamicDocumentEnumerableMultiParam(this IEnumerable doc, string custom, int i, bool b) - { - return custom + i + b; - } - - } -} \ No newline at end of file diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index a2f2523822..a2eeb8ce18 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -58,7 +58,10 @@ - + + + + @@ -151,6 +154,7 @@ Umbraco.Web + xcopy "$(ProjectDir)"..\..\lib\SQLCE4\amd64\*.* "$(TargetDir)amd64\" /Y /F /E /D diff --git a/src/Umbraco.Web.UI/umbraco/developer/Xslt/editXslt.aspx b/src/Umbraco.Web.UI/umbraco/developer/Xslt/editXslt.aspx index 2c11e6b2a6..4e96930591 100644 --- a/src/Umbraco.Web.UI/umbraco/developer/Xslt/editXslt.aspx +++ b/src/Umbraco.Web.UI/umbraco/developer/Xslt/editXslt.aspx @@ -55,10 +55,17 @@ function xsltVisualize() { - xsltSnippet = UmbEditor.GetSelection(); - if (xsltSnippet == '') - UmbEditor.GetCode() - + xsltSnippet = UmbEditor.IsSimpleEditor + ? jQuery("#<%= editorSource.ClientID %>").getSelection().text + : UmbEditor._editor.selection(); + + if (xsltSnippet == '') { + xsltSnippet = UmbEditor.IsSimpleEditor + ? jQuery("#<%= editorSource.ClientID %>").val() + : UmbEditor.GetCode(); + // alert('Please select the xslt to visualize'); + } + UmbClientMgr.openModalWindow('<%= umbraco.IO.IOHelper.ResolveUrl(umbraco.IO.SystemDirectories.Umbraco) %>/developer/xslt/xsltVisualize.aspx', 'Visualize XSLT', true, 550, 650); } diff --git a/src/umbraco.MacroEngines/RazorDynamicNode/DynamicNode.cs b/src/umbraco.MacroEngines/RazorDynamicNode/DynamicNode.cs index 8bceb6090c..15c292ca77 100644 --- a/src/umbraco.MacroEngines/RazorDynamicNode/DynamicNode.cs +++ b/src/umbraco.MacroEngines/RazorDynamicNode/DynamicNode.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Threading; using System.Web; using Umbraco.Core; +using Umbraco.Core.Logging; using umbraco.interfaces; using System.Collections; using System.Reflection; @@ -452,7 +453,6 @@ namespace umbraco.MacroEngines var foundTypes = new Dictionary, Type>(); - HttpContext.Current.Trace.Write("RazorDataTypeModelTypes cache is empty, populating cache using PluginTypeResolver..."); try { PluginManager.Current.ResolveRazorDataTypeModels() @@ -477,13 +477,14 @@ namespace umbraco.MacroEngines foundTypes.Add(key, item.Value); } }); - HttpContext.Current.Trace.Write(string.Format("{0} items added to cache...", foundTypes.Count)); - var i = 1; - foreach (var item in foundTypes) - { - HttpContext.Current.Trace.Write(string.Format("{0}/{1}: {2}@{4} => {3}", i, foundTypes.Count, item.Key.Item1, item.Value.FullName, item.Key.Item2)); - i++; - } + + //NOTE: We really dont need to log this? + //var i = 1; + //foreach (var item in foundTypes) + //{ + // HttpContext.Current.Trace.Write(string.Format("{0}/{1}: {2}@{4} => {3}", i, foundTypes.Count, item.Key.Item1, item.Value.FullName, item.Key.Item2)); + // i++; + //} //there is no error, so set the collection _razorDataTypeModelTypes = foundTypes; @@ -491,8 +492,8 @@ namespace umbraco.MacroEngines } catch (Exception ex) { - HttpContext.Current.Trace.Warn("Exception occurred while populating cache, will keep RazorDataTypeModelTypes to null so that this error remains visible and you don't end up with an empty cache with silent failure."); - HttpContext.Current.Trace.Warn(string.Format("The exception was {0} and the message was {1}. {2}", ex.GetType().FullName, ex.Message, ex.StackTrace)); + LogHelper.Warn("Exception occurred while populating cache, will keep RazorDataTypeModelTypes to null so that this error remains visible and you don't end up with an empty cache with silent failure." + + string.Format("The exception was {0} and the message was {1}. {2}", ex.GetType().FullName, ex.Message, ex.StackTrace)); } } @@ -541,18 +542,14 @@ namespace umbraco.MacroEngines //contextAlias is the node which the property data was returned from Guid dataType = ContentType.GetDataType(data.ContextAlias, data.Alias); - HttpContext.Current.Trace.Write(string.Format("RazorDynamicNode got datatype {0} for {1} on {2}", dataType, data.Alias, data.ContextAlias)); - HttpContext.Current.Trace.Write(string.Format("Checking for a RazorDataTypeModel for data type guid {0}...", dataType)); - HttpContext.Current.Trace.Write("Checking the RazorDataTypeModelTypes static mappings to see if there is a static mapping..."); - var staticMapping = UmbracoSettings.RazorDataTypeModelStaticMapping.FirstOrDefault(mapping => { return mapping.Applies(dataType, data.ContextAlias, data.Alias); }); if (staticMapping != null) { - HttpContext.Current.Trace.Write(string.Format("Found a staticMapping defined {0}, instantiating type and attempting to apply model...", staticMapping.Raw)); + Type dataTypeType = Type.GetType(staticMapping.TypeName); if (dataTypeType != null) { @@ -564,21 +561,15 @@ namespace umbraco.MacroEngines } else { - HttpContext.Current.Trace.Write("Failed"); - HttpContext.Current.Trace.Warn(string.Format("Failed to create the instance of the model binder")); + LogHelper.Warn(string.Format("Failed to create the instance of the model binder")); } } else { - HttpContext.Current.Trace.Warn(string.Format("staticMapping type name {0} came back as null from Type.GetType; check the casing, assembly presence, assembly framework version, namespace", staticMapping.TypeName)); + LogHelper.Warn(string.Format("staticMapping type name {0} came back as null from Type.GetType; check the casing, assembly presence, assembly framework version, namespace", staticMapping.TypeName)); } } - else - { - HttpContext.Current.Trace.Write(string.Format("There isn't a staticMapping defined so checking the RazorDataTypeModelTypes cache...")); - } - - + if (RazorDataTypeModelTypes != null && RazorDataTypeModelTypes.Any(model => model.Key.Item1 == dataType) && dataType != Guid.Empty) { var razorDataTypeModelDefinition = RazorDataTypeModelTypes.Where(model => model.Key.Item1 == dataType).OrderByDescending(model => model.Key.Item2).FirstOrDefault(); @@ -593,26 +584,25 @@ namespace umbraco.MacroEngines } else { - HttpContext.Current.Trace.Write("Failed"); - HttpContext.Current.Trace.Warn(string.Format("Failed to create the instance of the model binder")); + LogHelper.Warn(string.Format("Failed to create the instance of the model binder")); } } else { - HttpContext.Current.Trace.Write("Failed"); - HttpContext.Current.Trace.Warn(string.Format("Could not get the dataTypeType for the RazorDataTypeModel")); + LogHelper.Warn(string.Format("Could not get the dataTypeType for the RazorDataTypeModel")); } } else { - if (RazorDataTypeModelTypes == null) - { - HttpContext.Current.Trace.Write(string.Format("RazorDataTypeModelTypes is null, probably an exception while building the cache, falling back to ConvertPropertyValueByDataType", dataType)); - } - else - { - HttpContext.Current.Trace.Write(string.Format("GUID {0} does not have a DataTypeModel, falling back to ConvertPropertyValueByDataType", dataType)); - } + //NOTE: Do we really want to log this? I'm not sure. + //if (RazorDataTypeModelTypes == null) + //{ + // HttpContext.Current.Trace.Write(string.Format("RazorDataTypeModelTypes is null, probably an exception while building the cache, falling back to ConvertPropertyValueByDataType", dataType)); + //} + //else + //{ + // HttpContext.Current.Trace.Write(string.Format("GUID {0} does not have a DataTypeModel, falling back to ConvertPropertyValueByDataType", dataType)); + //} } @@ -678,43 +668,30 @@ namespace umbraco.MacroEngines } private bool TryCreateInstanceRazorDataTypeModel(Guid dataType, Type dataTypeType, string value, out object result) { - HttpContext.Current.Trace.Write(string.Format("Found dataType {0} for GUID {1}", dataTypeType.FullName, dataType)); IRazorDataTypeModel razorDataTypeModel = Activator.CreateInstance(dataTypeType, false) as IRazorDataTypeModel; - HttpContext.Current.Trace.Write(string.Format("Instantiating {0}...", dataTypeType.FullName)); if (razorDataTypeModel != null) { - HttpContext.Current.Trace.Write("Success"); object instance = null; - HttpContext.Current.Trace.Write("Calling Init on razorDataTypeModel"); if (razorDataTypeModel.Init(n.Id, value, out instance)) { - if (instance != null) + if (instance == null) { - HttpContext.Current.Trace.Write(string.Format("razorDataTypeModel successfully instantiated and returned a valid instance of type {0}", instance.GetType().FullName)); + LogHelper.Warn("razorDataTypeModel successfully instantiated but returned null for instance"); } - else - { - HttpContext.Current.Trace.Warn("razorDataTypeModel successfully instantiated but returned null for instance"); - } - result = instance; + result = instance; return true; } else { - if (instance != null) + if (instance == null) { - HttpContext.Current.Trace.Write(string.Format("razorDataTypeModel returned false but returned a valid instance of type {0}", instance.GetType().FullName)); - } - else - { - HttpContext.Current.Trace.Warn("razorDataTypeModel successfully instantiated but returned null for instance"); + LogHelper.Warn("razorDataTypeModel successfully instantiated but returned null for instance"); } } } else { - HttpContext.Current.Trace.Write("Failed"); - HttpContext.Current.Trace.Warn(string.Format("DataTypeModel {0} failed to instantiate, perhaps it is lacking a parameterless constructor or doesn't implement IRazorDataTypeModel?", dataTypeType.FullName)); + LogHelper.Warn(string.Format("DataTypeModel {0} failed to instantiate, perhaps it is lacking a parameterless constructor or doesn't implement IRazorDataTypeModel?", dataTypeType.FullName)); } result = null; return false; @@ -1335,9 +1312,13 @@ namespace umbraco.MacroEngines get { if (n == null) return null; return n.PropertiesAsList; } } - public List ChildrenAsList + public DynamicNodeList ChildrenAsList { - get { if (n == null) return null; return n.ChildrenAsList; } + get + { + return GetChildrenAsList; + //if (n == null) return null; return n.ChildrenAsList; + } } public IProperty GetProperty(string alias) @@ -1438,7 +1419,7 @@ namespace umbraco.MacroEngines { if (this.ownerList == null && this.Parent != null) { - var list = this.Parent.ChildrenAsList.ConvertAll(n => new DynamicNode(n)); + var list = this.Parent.ChildrenAsList.Select(n => new DynamicNode(n)); this.ownerList = new DynamicNodeList(list); } if (this.ownerList != null) diff --git a/src/umbraco.MacroEngines/RazorDynamicNode/DynamicNodeList.cs b/src/umbraco.MacroEngines/RazorDynamicNode/DynamicNodeList.cs index 0d1a3df208..e6c275d459 100644 --- a/src/umbraco.MacroEngines/RazorDynamicNode/DynamicNodeList.cs +++ b/src/umbraco.MacroEngines/RazorDynamicNode/DynamicNodeList.cs @@ -12,7 +12,7 @@ using System.Linq.Expressions; using System.Linq.Dynamic; namespace umbraco.MacroEngines { - public class DynamicNodeList : DynamicObject, IEnumerable + public class DynamicNodeList : DynamicObject, IEnumerable { public List Items; public List get_Items() @@ -403,10 +403,15 @@ namespace umbraco.MacroEngines return result; } - public IEnumerator GetEnumerator() - { - return Items.GetEnumerator(); - } + public IEnumerator GetEnumerator() + { + return Items.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } public IQueryable Where(string predicate, params object[] values) { diff --git a/src/umbraco.MacroEngines/RazorDynamicNode/DynamicNodeWalker.cs b/src/umbraco.MacroEngines/RazorDynamicNode/DynamicNodeWalker.cs index 9c53388bb1..cb8b2526d5 100644 --- a/src/umbraco.MacroEngines/RazorDynamicNode/DynamicNodeWalker.cs +++ b/src/umbraco.MacroEngines/RazorDynamicNode/DynamicNodeWalker.cs @@ -80,8 +80,9 @@ namespace umbraco.MacroEngines { if (context.ownerList == null && context.Parent != null) { - var list = context.Parent.ChildrenAsList.ConvertAll(n => new DynamicNode(n)); - context.ownerList = new DynamicNodeList(list); + //var list = context.Parent.ChildrenAsList.Select(n => new DynamicNode(n)); + //context.ownerList = new DynamicNodeList(list); + context.ownerList = context.Parent.ChildrenAsList; } if (context.ownerList != null) { @@ -105,7 +106,7 @@ namespace umbraco.MacroEngines { if (context.ownerList == null && context.Parent != null) { - var list = context.Parent.ChildrenAsList.ConvertAll(n => new DynamicNode(n)); + var list = context.Parent.ChildrenAsList.Select(n => new DynamicNode(n)); context.ownerList = new DynamicNodeList(list); } if (context.ownerList != null) @@ -130,7 +131,7 @@ namespace umbraco.MacroEngines { if (context.ownerList == null && context.Parent != null) { - var list = context.Parent.ChildrenAsList.ConvertAll(n => new DynamicNode(n)); + var list = context.Parent.ChildrenAsList.Select(n => new DynamicNode(n)); context.ownerList = new DynamicNodeList(list); } if (context.ownerList != null) @@ -169,7 +170,7 @@ namespace umbraco.MacroEngines { if (context.ownerList == null && context.Parent != null) { - var list = context.Parent.ChildrenAsList.ConvertAll(n => new DynamicNode(n)); + var list = context.Parent.ChildrenAsList.Select(n => new DynamicNode(n)); context.ownerList = new DynamicNodeList(list); } if (context.ownerList != null) @@ -203,8 +204,9 @@ namespace umbraco.MacroEngines { if (context.ownerList == null && context.Parent != null) { - var list = context.Parent.ChildrenAsList.ConvertAll(n => new DynamicNode(n)); - context.ownerList = new DynamicNodeList(list); + //var list = context.Parent.ChildrenAsList.Select(n => new DynamicNode(n)); + //context.ownerList = new DynamicNodeList(list); + context.ownerList = context.Parent.ChildrenAsList; } if (context.ownerList != null) { @@ -228,7 +230,7 @@ namespace umbraco.MacroEngines { if (context.ownerList == null && context.Parent != null) { - var list = context.Parent.ChildrenAsList.ConvertAll(n => new DynamicNode(n)); + var list = context.Parent.ChildrenAsList.Select(n => new DynamicNode(n)); context.ownerList = new DynamicNodeList(list); } if (context.ownerList != null)