diff --git a/src/Umbraco.Core/StringExtensions.cs b/src/Umbraco.Core/StringExtensions.cs index 8d4883a6fc..cb693fce29 100644 --- a/src/Umbraco.Core/StringExtensions.cs +++ b/src/Umbraco.Core/StringExtensions.cs @@ -663,6 +663,17 @@ namespace Umbraco.Core : alternative; } + /// + /// Gets the short string helper. + /// + /// This is so that unit tests that do not initialize the resolver do not + /// fail and fall back to defaults. When running the whole Umbraco, CoreBootManager + /// does initialise the resolver. + private static IShortStringHelper ShortStringHelper + { + get { return ShortStringHelperResolver.HasCurrent ? ShortStringHelperResolver.Current.Helper : new LegacyShortStringHelper(); } + } + /// /// Returns a new string in which all occurences of specified strings are replaced by other specified strings. /// @@ -671,7 +682,7 @@ namespace Umbraco.Core /// The filtered string. public static string ReplaceMany(this string text, IDictionary replacements) { - return ShortStringHelperResolver.Current.Helper.ReplaceMany(text, replacements); + return ShortStringHelper.ReplaceMany(text, replacements); } // FORMAT STRINGS @@ -699,7 +710,7 @@ namespace Umbraco.Core [UmbracoWillObsolete("This method should be removed. Use ToUrlSegment instead.")] public static string ToUrlAlias(this string value, IDictionary charReplacements, bool replaceDoubleDashes, bool stripNonAscii, bool urlEncode) { - var helper = ShortStringHelperResolver.Current.Helper; + var helper = ShortStringHelper; var legacy = helper as LegacyShortStringHelper; return legacy != null ? legacy.LegacyToUrlAlias(value, charReplacements, replaceDoubleDashes, stripNonAscii, urlEncode) @@ -723,7 +734,7 @@ namespace Umbraco.Core [UmbracoWillObsolete("This method should be removed. Use ToUrlSegment instead.")] public static string FormatUrl(this string url) { - var helper = ShortStringHelperResolver.Current.Helper; + var helper = ShortStringHelper; var legacy = helper as LegacyShortStringHelper; return legacy != null ? legacy.LegacyFormatUrl(url) : helper.CleanStringForUrlSegment(url); } @@ -738,7 +749,7 @@ namespace Umbraco.Core /// The safe alias. public static string ToSafeAlias(this string alias) { - return ShortStringHelperResolver.Current.Helper.CleanStringForSafeAlias(alias); + return ShortStringHelper.CleanStringForSafeAlias(alias); } /// @@ -749,7 +760,7 @@ namespace Umbraco.Core /// The safe alias. public static string ToSafeAlias(this string alias, CultureInfo culture) { - return ShortStringHelperResolver.Current.Helper.CleanStringForSafeAlias(alias, culture); + return ShortStringHelper.CleanStringForSafeAlias(alias, culture); } /// @@ -790,7 +801,7 @@ namespace Umbraco.Core [UmbracoWillObsolete("This method should be removed. Use ToSafeAlias instead.")] public static string ToUmbracoAlias(this string phrase, StringAliasCaseType caseType = StringAliasCaseType.CamelCase, bool removeSpaces = false) { - var helper = ShortStringHelperResolver.Current.Helper; + var helper = ShortStringHelper; var legacy = helper as LegacyShortStringHelper; return legacy != null ? legacy.LegacyCleanStringForUmbracoAlias(phrase) : helper.CleanStringForSafeAlias(phrase); } @@ -804,7 +815,7 @@ namespace Umbraco.Core /// The safe url segment. public static string ToUrlSegment(this string text) { - return ShortStringHelperResolver.Current.Helper.CleanStringForUrlSegment(text); + return ShortStringHelper.CleanStringForUrlSegment(text); } /// @@ -815,7 +826,7 @@ namespace Umbraco.Core /// The safe url segment. public static string ToUrlSegment(this string text, CultureInfo culture) { - return ShortStringHelperResolver.Current.Helper.CleanStringForUrlSegment(text, culture); + return ShortStringHelper.CleanStringForUrlSegment(text, culture); } // note: LegacyShortStringHelper will produce 100% backward-compatible output for ConvertCase. @@ -835,7 +846,7 @@ namespace Umbraco.Core [UmbracoWillObsolete("This method should be removed. Use CleanString instead.")] public static string ConvertCase(this string phrase, StringAliasCaseType cases) { - var helper = ShortStringHelperResolver.Current.Helper; + var helper = ShortStringHelper; var legacy = helper as LegacyShortStringHelper; var cases2 = cases.ToCleanStringType() & CleanStringType.CaseMask; return legacy != null @@ -855,7 +866,7 @@ namespace Umbraco.Core /// The string is cleaned in the context of the IShortStringHelper default culture. public static string ToCleanString(string text, CleanStringType stringType) { - return ShortStringHelperResolver.Current.Helper.CleanString(text, stringType); + return ShortStringHelper.CleanString(text, stringType); } /// @@ -869,7 +880,7 @@ namespace Umbraco.Core /// The string is cleaned in the context of the IShortStringHelper default culture. public static string ToCleanString(string text, CleanStringType stringType, char separator) { - return ShortStringHelperResolver.Current.Helper.CleanString(text, stringType, separator); + return ShortStringHelper.CleanString(text, stringType, separator); } /// @@ -882,7 +893,7 @@ namespace Umbraco.Core /// The clean string. public static string ToCleanString(string text, CleanStringType stringType, CultureInfo culture) { - return ShortStringHelperResolver.Current.Helper.CleanString(text, stringType, culture); + return ShortStringHelper.CleanString(text, stringType, culture); } /// @@ -896,7 +907,7 @@ namespace Umbraco.Core /// The clean string. public static string ToCleanString(string text, CleanStringType stringType, char separator, CultureInfo culture) { - return ShortStringHelperResolver.Current.Helper.CleanString(text, stringType, separator, culture); + return ShortStringHelper.CleanString(text, stringType, separator, culture); } // note: LegacyShortStringHelper will produce 100% backward-compatible output for SplitPascalCasing. @@ -909,7 +920,7 @@ namespace Umbraco.Core /// The splitted text. public static string SplitPascalCasing(this string phrase) { - return ShortStringHelperResolver.Current.Helper.SplitPascalCasing(phrase, ' '); + return ShortStringHelper.SplitPascalCasing(phrase, ' '); } } } diff --git a/src/Umbraco.Core/Strings/ContentBaseExtensions.cs b/src/Umbraco.Core/Strings/ContentBaseExtensions.cs index 1ab2840906..1abb9f614c 100644 --- a/src/Umbraco.Core/Strings/ContentBaseExtensions.cs +++ b/src/Umbraco.Core/Strings/ContentBaseExtensions.cs @@ -1,4 +1,5 @@ -using System.Globalization; +using System.Collections.Generic; +using System.Globalization; using System.Linq; using Umbraco.Core.Models; @@ -9,6 +10,22 @@ namespace Umbraco.Core.Strings /// internal static class ContentBaseExtensions { + /// + /// Gets the url segment providers. + /// + /// This is so that unit tests that do not initialize the resolver do not + /// fail and fall back to defaults. When running the whole Umbraco, CoreBootManager + /// does initialise the resolver. + private static IEnumerable UrlSegmentProviders + { + get + { + return UrlSegmentProviderResolver.HasCurrent + ? UrlSegmentProviderResolver.Current.Providers + : new IUrlSegmentProvider[] { new DefaultUrlSegmentProvider() }; + } + } + /// /// Gets the default url segment for a specified content. /// @@ -16,8 +33,7 @@ namespace Umbraco.Core.Strings /// The url segment. public static string GetUrlSegment(this IContentBase content) { - var urlSegmentProviders = UrlSegmentProviderResolver.Current.Providers; - var url = urlSegmentProviders.Select(p => p.GetUrlSegment(content)).First(u => u != null); + var url = UrlSegmentProviders.Select(p => p.GetUrlSegment(content)).First(u => u != null); url = url ?? new DefaultUrlSegmentProvider().GetUrlSegment(content); // be safe return url; } @@ -30,8 +46,7 @@ namespace Umbraco.Core.Strings /// The url segment. public static string GetUrlSegment(this IContentBase content, CultureInfo culture) { - var urlSegmentProviders = UrlSegmentProviderResolver.Current.Providers; - var url = urlSegmentProviders.Select(p => p.GetUrlSegment(content, culture)).First(u => u != null); + var url = UrlSegmentProviders.Select(p => p.GetUrlSegment(content, culture)).First(u => u != null); url = url ?? new DefaultUrlSegmentProvider().GetUrlSegment(content, culture); // be safe return url; }