diff --git a/src/Umbraco.Core/Logging/LogHelper.cs b/src/Umbraco.Core/Logging/LogHelper.cs index e972818289..5f5c70eda9 100644 --- a/src/Umbraco.Core/Logging/LogHelper.cs +++ b/src/Umbraco.Core/Logging/LogHelper.cs @@ -60,13 +60,7 @@ namespace Umbraco.Core.Logging } #endregion - #region Warn - public static void Warn(Type callingType, string message) - { - var logger = LogManager.GetLogger(callingType); - if (logger != null) - logger.Warn(PrefixThreadId(message)); - } + #region Warn public static void Warn(Type callingType, string message, params object[] format) { @@ -75,30 +69,44 @@ namespace Umbraco.Core.Logging logger.WarnFormat(PrefixThreadId(message), format); } - /// - /// Adds a warn log - /// - /// - /// - public static void Warn(string message) + public static void Warn(Type callingType, TraceContext trace, string message, params object[] format) { - var logger = LoggerFor(); + if (trace != null) + { + trace.Warn(string.Format(message, format)); + } + + var logger = LogManager.GetLogger(callingType); if (logger != null) - logger.Warn(PrefixThreadId(message)); + logger.WarnFormat(PrefixThreadId(message), format); + } /// /// Adds a warn log /// /// - /// + /// /// - public static void Warn(string format, params object[] items) + public static void Warn(string message, params object[] items) { var logger = LoggerFor(); if (logger != null) - logger.WarnFormat(PrefixThreadId(format), items); + logger.WarnFormat(PrefixThreadId(message), items); + } + + public static void Warn(string message, TraceContext trace, params object[] items) + { + if (trace != null) + { + trace.Warn(string.Format(message, items)); + } + + var logger = LoggerFor(); + if (logger != null) + logger.WarnFormat(PrefixThreadId(message), items); } + #endregion #region Info diff --git a/src/Umbraco.Tests/LibraryTests.cs b/src/Umbraco.Tests/LibraryTests.cs new file mode 100644 index 0000000000..25e1ecb1b8 --- /dev/null +++ b/src/Umbraco.Tests/LibraryTests.cs @@ -0,0 +1,107 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NUnit.Framework; +using Umbraco.Tests.TestHelpers; +using Umbraco.Web; +using umbraco; + +namespace Umbraco.Tests +{ + + /// + /// Tests for the legacy library class + /// + [TestFixture] + public class LibraryTests : BaseRoutingTest + { + public override void Initialize() + { + base.Initialize(); + + //set the current umbraco context and a published content store + PublishedContentStoreResolver.Current = new PublishedContentStoreResolver( + new DefaultPublishedContentStore()); + + var routingContext = GetRoutingContext("/test", 1234); + UmbracoContext.Current = routingContext.UmbracoContext; + } + + public override void TearDown() + { + base.TearDown(); + UmbracoContext.Current = null; + PublishedContentStoreResolver.Reset(); + } + + protected override bool RequiresDbSetup + { + get { return false; } + } + + [Test] + public void Get_Item_User_Property() + { + var val = library.GetItem(1173, "content"); + var legacyVal = LegacyGetItem(1173, "content"); + Assert.AreEqual(legacyVal, val); + Assert.AreEqual("
This is some content
", val); + } + + [Test] + public void Get_Item_Document_Property() + { + //first test a single static val + var val = library.GetItem(1173, "template"); + var legacyVal = LegacyGetItem(1173, "template"); + Assert.AreEqual(legacyVal, val); + Assert.AreEqual("1234", val); + + //now test them all to see if they all match legacy + foreach(var s in new[]{"id","parentID","level","writerID","template","sortOrder","createDate","updateDate","nodeName","writerName","path"}) + { + val = library.GetItem(1173, s); + legacyVal = LegacyGetItem(1173, s); + Assert.AreEqual(legacyVal, val); + } + } + + [Test] + public void Get_Item_Invalid_Property() + { + var val = library.GetItem(1173, "dontfindme"); + var legacyVal = LegacyGetItem(1173, "dontfindme"); + Assert.AreEqual(legacyVal, val); + Assert.AreEqual("", val); + } + + /// + /// The old method, just using this to make sure we're returning the correct exact data as before. + /// + /// + /// + /// + private string LegacyGetItem(int nodeId, string alias) + { + var umbracoXML = UmbracoContext.Current.GetXml(); + string xpath = UmbracoSettings.UseLegacyXmlSchema ? "./data [@alias='{0}']" : "./{0}"; + if (umbracoXML.GetElementById(nodeId.ToString()) != null) + if ( + ",id,parentID,level,writerID,template,sortOrder,createDate,updateDate,nodeName,writerName,path," + . + IndexOf("," + alias + ",") > -1) + return umbracoXML.GetElementById(nodeId.ToString()).Attributes.GetNamedItem(alias).Value; + else if ( + umbracoXML.GetElementById(nodeId.ToString()).SelectSingleNode(string.Format(xpath, alias)) != + null) + return + umbracoXML.GetElementById(nodeId.ToString()).SelectSingleNode(string.Format(xpath, alias)).ChildNodes[0]. + Value; //.Value + "*"; + else + return string.Empty; + else + return string.Empty; + } + } +} diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index a19010773c..a55a13e47c 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -56,6 +56,7 @@ + diff --git a/src/Umbraco.Web/Dictionary/UmbracoCultureDictionary.cs b/src/Umbraco.Web/Dictionary/UmbracoCultureDictionary.cs index 7598a97957..4417ed592e 100644 --- a/src/Umbraco.Web/Dictionary/UmbracoCultureDictionary.cs +++ b/src/Umbraco.Web/Dictionary/UmbracoCultureDictionary.cs @@ -1,6 +1,8 @@ using System; using System.Dynamic; using System.Globalization; +using System.Web; +using Umbraco.Core.Logging; using umbraco.cms.businesslogic; using umbraco.cms.businesslogic.language; @@ -22,10 +24,10 @@ namespace Umbraco.Web.Dictionary { return new global::umbraco.cms.businesslogic.Dictionary.DictionaryItem(key).Value(Language.id); } - catch (Exception) + catch (Exception e) { - //NOTE: SD: I'm not sure why this is here but was copied from the UmbracoCultureDictionary in the macroEngines project - // which previously seems to have worked so I'm leaving it for now. + var trace = UmbracoContext.Current != null ? UmbracoContext.Current.HttpContext.Trace : null; + LogHelper.Warn("Error returning dictionary item '" + key + "'", trace, e); return string.Empty; } } diff --git a/src/Umbraco.Web/umbraco.presentation/library.cs b/src/Umbraco.Web/umbraco.presentation/library.cs index 53049cf2c3..701e689ff1 100644 --- a/src/Umbraco.Web/umbraco.presentation/library.cs +++ b/src/Umbraco.Web/umbraco.presentation/library.cs @@ -434,26 +434,46 @@ namespace umbraco /// Returns a string with the data from the given element of a node public static string GetItem(int nodeID, String alias) { - XmlDocument umbracoXML = UmbracoContext.Current.GetXml(); + var doc = PublishedContentStoreResolver.Current.PublishedContentStore.GetDocumentById( + Umbraco.Web.UmbracoContext.Current, + nodeID); - string xpath = UmbracoSettings.UseLegacyXmlSchema ? "./data [@alias='{0}']" : "./{0}"; + if (doc == null) + return string.Empty; + + switch (alias) + { + case "id": + return doc.Id.ToString(); + case "version": + return doc.Version.ToString(); + case "parentID": + return doc.Parent.Id.ToString(); + case "level": + return doc.Level.ToString(); + case "writerID": + return doc.WriterId.ToString(); + case "template": + return doc.TemplateId.ToString(); + case "sortOrder": + return doc.SortOrder.ToString(); + case "createDate": + return doc.CreateDate.ToString("yyyy-MM-dd'T'HH:mm:ss"); + case "updateDate": + return doc.UpdateDate.ToString("yyyy-MM-dd'T'HH:mm:ss"); + case "nodeName": + return doc.Name; + case "writerName": + return doc.WriterName; + case "path": + return doc.Path; + case "creatorName": + return doc.CreatorName; + } + + var prop = doc.GetProperty(alias); + return prop == null ? string.Empty : prop.Value.ToString(); - if (umbracoXML.GetElementById(nodeID.ToString()) != null) - if ( - ",id,version,parentID,level,writerID,editDataType,template,sortOrder,createDate,updateDate,nodeName,writerName,path," - . - IndexOf("," + alias + ",") > -1) - return umbracoXML.GetElementById(nodeID.ToString()).Attributes.GetNamedItem(alias).Value; - else if ( - umbracoXML.GetElementById(nodeID.ToString()).SelectSingleNode(string.Format(xpath, alias)) != - null) - return - umbracoXML.GetElementById(nodeID.ToString()).SelectSingleNode(string.Format(xpath, alias)).ChildNodes[0]. - Value; //.Value + "*"; - else - return string.Empty; - else - return string.Empty; } /// @@ -1492,17 +1512,7 @@ namespace umbraco /// A dictionary items value as a string. public static string GetDictionaryItem(string Key) { - try - { - Language l = Language.GetByCultureCode(System.Threading.Thread.CurrentThread.CurrentUICulture.Name); - return new Dictionary.DictionaryItem(Key).Value(l.id); - //return new Dictionary.DictionaryItem(Key).Value(GetCurrentLanguageId()); - } - catch (Exception errDictionary) - { - HttpContext.Current.Trace.Warn("library", "Error returning dictionary item '" + Key + "'", errDictionary); - return string.Empty; - } + return GetUmbracoHelper().GetDictionaryValue(Key); } ///