Refactored simple conversion methods.
Added unit tests to cover the methods.
This commit is contained in:
@@ -1,6 +1,14 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using LightInject;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Core.Cache;
|
||||
using Umbraco.Core.Composing;
|
||||
using Umbraco.Core.Logging;
|
||||
using Umbraco.Tests.TestHelpers;
|
||||
using Umbraco.Web;
|
||||
|
||||
namespace Umbraco.Tests.FrontEnd
|
||||
@@ -8,6 +16,33 @@ namespace Umbraco.Tests.FrontEnd
|
||||
[TestFixture]
|
||||
public class UmbracoHelperTests
|
||||
{
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
// fixme - bad in a unit test - but Udi has a static ctor that wants it?!
|
||||
var container = new Mock<IServiceContainer>();
|
||||
var globalSettings = SettingsForTests.GenerateMockGlobalSettings();
|
||||
|
||||
container
|
||||
.Setup(x => x.GetInstance(typeof(TypeLoader)))
|
||||
.Returns(new TypeLoader(
|
||||
NullCacheProvider.Instance,
|
||||
globalSettings,
|
||||
new ProfilingLogger(Mock.Of<ILogger>(), Mock.Of<IProfiler>())
|
||||
)
|
||||
);
|
||||
|
||||
Current.Container = container.Object;
|
||||
|
||||
Udi.ResetUdiTypes();
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
Current.Reset();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Truncate_Simple()
|
||||
{
|
||||
@@ -68,7 +103,7 @@ namespace Umbraco.Tests.FrontEnd
|
||||
key2 = "value2",
|
||||
Key3 = "Value3",
|
||||
keY4 = "valuE4"
|
||||
};
|
||||
};
|
||||
var encryptedRouteString = UmbracoHelper.CreateEncryptedRouteString("FormController", "FormAction", "", additionalRouteValues);
|
||||
var result = encryptedRouteString.DecryptWithMachineKey();
|
||||
var expectedResult = "c=FormController&a=FormAction&ar=&key1=value1&key2=value2&Key3=Value3&keY4=valuE4";
|
||||
@@ -146,7 +181,7 @@ namespace Umbraco.Tests.FrontEnd
|
||||
{
|
||||
var text = "Hello world, <b>this</b> is some text <a href='blah'>with a link</a>";
|
||||
|
||||
string [] tags = {"b"};
|
||||
string[] tags = { "b" };
|
||||
|
||||
var helper = new UmbracoHelper();
|
||||
|
||||
@@ -166,5 +201,233 @@ namespace Umbraco.Tests.FrontEnd
|
||||
|
||||
Assert.AreEqual("Hello world, is some text with a link", result);
|
||||
}
|
||||
|
||||
// ------- Int32 conversion tests
|
||||
[Test]
|
||||
public static void Converting_boxed_34_to_an_int_returns_34()
|
||||
{
|
||||
// Arrange
|
||||
const int sample = 34;
|
||||
|
||||
// Act
|
||||
bool success = UmbracoHelper.ConvertIdObjectToInt(
|
||||
sample,
|
||||
out int result
|
||||
);
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(success);
|
||||
Assert.That(result, Is.EqualTo(34));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public static void Converting_string_54_to_an_int_returns_54()
|
||||
{
|
||||
// Arrange
|
||||
const string sample = "54";
|
||||
|
||||
// Act
|
||||
bool success = UmbracoHelper.ConvertIdObjectToInt(
|
||||
sample,
|
||||
out int result
|
||||
);
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(success);
|
||||
Assert.That(result, Is.EqualTo(54));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public static void Converting_hello_to_an_int_returns_false()
|
||||
{
|
||||
// Arrange
|
||||
const string sample = "Hello";
|
||||
|
||||
// Act
|
||||
bool success = UmbracoHelper.ConvertIdObjectToInt(
|
||||
sample,
|
||||
out int result
|
||||
);
|
||||
|
||||
// Assert
|
||||
Assert.IsFalse(success);
|
||||
Assert.That(result, Is.EqualTo(0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public static void Converting_unsupported_object_to_an_int_returns_false()
|
||||
{
|
||||
// Arrange
|
||||
var clearlyWillNotConvertToInt = new StringBuilder(0);
|
||||
|
||||
// Act
|
||||
bool success = UmbracoHelper.ConvertIdObjectToInt(
|
||||
clearlyWillNotConvertToInt,
|
||||
out int result
|
||||
);
|
||||
|
||||
// Assert
|
||||
Assert.IsFalse(success);
|
||||
Assert.That(result, Is.EqualTo(0));
|
||||
}
|
||||
|
||||
// ------- GUID conversion tests
|
||||
[Test]
|
||||
public static void Converting_boxed_guid_to_a_guid_returns_original_guid_value()
|
||||
{
|
||||
// Arrange
|
||||
Guid sample = Guid.NewGuid();
|
||||
|
||||
// Act
|
||||
bool success = UmbracoHelper.ConvertIdObjectToGuid(
|
||||
sample,
|
||||
out Guid result
|
||||
);
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(success);
|
||||
Assert.That(result, Is.EqualTo(sample));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public static void Converting_string_guid_to_a_guid_returns_original_guid_value()
|
||||
{
|
||||
// Arrange
|
||||
Guid sample = Guid.NewGuid();
|
||||
|
||||
// Act
|
||||
bool success = UmbracoHelper.ConvertIdObjectToGuid(
|
||||
sample.ToString(),
|
||||
out Guid result
|
||||
);
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(success);
|
||||
Assert.That(result, Is.EqualTo(sample));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public static void Converting_hello_to_a_guid_returns_false()
|
||||
{
|
||||
// Arrange
|
||||
const string sample = "Hello";
|
||||
|
||||
// Act
|
||||
bool success = UmbracoHelper.ConvertIdObjectToGuid(
|
||||
sample,
|
||||
out Guid result
|
||||
);
|
||||
|
||||
// Assert
|
||||
Assert.IsFalse(success);
|
||||
Assert.That(result, Is.EqualTo(new Guid("00000000-0000-0000-0000-000000000000")));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public static void Converting_unsupported_object_to_a_guid_returns_false()
|
||||
{
|
||||
// Arrange
|
||||
var clearlyWillNotConvertToGuid = new StringBuilder(0);
|
||||
|
||||
// Act
|
||||
bool success = UmbracoHelper.ConvertIdObjectToGuid(
|
||||
clearlyWillNotConvertToGuid,
|
||||
out Guid result
|
||||
);
|
||||
|
||||
// Assert
|
||||
Assert.IsFalse(success);
|
||||
Assert.That(result, Is.EqualTo(new Guid("00000000-0000-0000-0000-000000000000")));
|
||||
}
|
||||
|
||||
// ------- UDI Conversion Tests
|
||||
/// <remarks>
|
||||
/// This requires PluginManager.Current to be initialised before
|
||||
/// running.
|
||||
/// </remarks>
|
||||
[Test]
|
||||
public static void Converting_boxed_udi_to_a_udi_returns_original_udi_value()
|
||||
{
|
||||
// Arrange
|
||||
Udi.ResetUdiTypes();
|
||||
Udi sample = new GuidUdi(Constants.UdiEntityType.AnyGuid, Guid.NewGuid());
|
||||
|
||||
// Act
|
||||
bool success = UmbracoHelper.ConvertIdObjectToUdi(
|
||||
sample,
|
||||
out Udi result
|
||||
);
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(success);
|
||||
Assert.That(result, Is.EqualTo(sample));
|
||||
}
|
||||
|
||||
/// <remarks>
|
||||
/// This requires PluginManager.Current to be initialised before
|
||||
/// running.
|
||||
/// </remarks>
|
||||
[Test]
|
||||
public static void Converting_string_udi_to_a_udi_returns_original_udi_value()
|
||||
{
|
||||
// Arrange
|
||||
Udi.ResetUdiTypes();
|
||||
Udi sample = new GuidUdi(Constants.UdiEntityType.AnyGuid, Guid.NewGuid());
|
||||
|
||||
// Act
|
||||
bool success = UmbracoHelper.ConvertIdObjectToUdi(
|
||||
sample.ToString(),
|
||||
out Udi result
|
||||
);
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(success, "Conversion of UDI failed.");
|
||||
Assert.That(result, Is.EqualTo(sample));
|
||||
}
|
||||
|
||||
/// <remarks>
|
||||
/// This requires PluginManager.Current to be initialised before
|
||||
/// running.
|
||||
/// </remarks>
|
||||
[Test]
|
||||
public static void Converting_hello_to_a_udi_returns_false()
|
||||
{
|
||||
// Arrange
|
||||
Udi.ResetUdiTypes();
|
||||
const string sample = "Hello";
|
||||
|
||||
// Act
|
||||
bool success = UmbracoHelper.ConvertIdObjectToUdi(
|
||||
sample,
|
||||
out Udi result
|
||||
);
|
||||
|
||||
// Assert
|
||||
Assert.IsFalse(success);
|
||||
Assert.That(result, Is.Null);
|
||||
}
|
||||
|
||||
/// <remarks>
|
||||
/// This requires PluginManager.Current to be initialised before
|
||||
/// running.
|
||||
/// </remarks>
|
||||
[Test]
|
||||
public static void Converting_unsupported_object_to_a_udi_returns_false()
|
||||
{
|
||||
// Arrange
|
||||
Udi.ResetUdiTypes();
|
||||
|
||||
var clearlyWillNotConvertToGuid = new StringBuilder(0);
|
||||
|
||||
// Act
|
||||
bool success = UmbracoHelper.ConvertIdObjectToUdi(
|
||||
clearlyWillNotConvertToGuid,
|
||||
out Udi result
|
||||
);
|
||||
|
||||
// Assert
|
||||
Assert.IsFalse(success);
|
||||
Assert.That(result, Is.Null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -601,37 +601,40 @@ namespace Umbraco.Web
|
||||
return ContentQuery.ContentAtRoot();
|
||||
}
|
||||
|
||||
private static bool ConvertIdObjectToInt(object id, out int intId)
|
||||
/// <remarks>Had to change to internal for testing.</remarks>
|
||||
internal static bool ConvertIdObjectToInt(object id, out int intId)
|
||||
{
|
||||
var s = id as string;
|
||||
if (s != null)
|
||||
switch (id)
|
||||
{
|
||||
return int.TryParse(s, out intId);
|
||||
}
|
||||
case string s:
|
||||
return int.TryParse(s, out intId);
|
||||
|
||||
if (id is int)
|
||||
{
|
||||
intId = (int) id;
|
||||
return true;
|
||||
case int i:
|
||||
intId = i;
|
||||
return true;
|
||||
|
||||
default:
|
||||
intId = default;
|
||||
return false;
|
||||
}
|
||||
intId = default(int);
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool ConvertIdObjectToGuid(object id, out Guid guidId)
|
||||
/// <remarks>Had to change to internal for testing.</remarks>
|
||||
internal static bool ConvertIdObjectToGuid(object id, out Guid guidId)
|
||||
{
|
||||
var s = id as string;
|
||||
if (s != null)
|
||||
switch (id)
|
||||
{
|
||||
return Guid.TryParse(s, out guidId);
|
||||
case string s:
|
||||
return Guid.TryParse(s, out guidId);
|
||||
|
||||
case Guid g:
|
||||
guidId = g;
|
||||
return true;
|
||||
|
||||
default:
|
||||
guidId = default;
|
||||
return false;
|
||||
}
|
||||
if (id is Guid)
|
||||
{
|
||||
guidId = (Guid) id;
|
||||
return true;
|
||||
}
|
||||
guidId = default(Guid);
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool ConvertIdsObjectToInts(IEnumerable<object> ids, out IEnumerable<int> intIds)
|
||||
@@ -665,17 +668,22 @@ namespace Umbraco.Web
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool ConvertIdObjectToUdi(object id, out Udi guidId)
|
||||
/// <remarks>Had to change to internal for testing.</remarks>
|
||||
internal static bool ConvertIdObjectToUdi(object id, out Udi guidId)
|
||||
{
|
||||
if (id is string s)
|
||||
return Udi.TryParse(s, out guidId);
|
||||
if (id is Udi)
|
||||
switch (id)
|
||||
{
|
||||
guidId = (Udi) id;
|
||||
return true;
|
||||
case string s:
|
||||
return Udi.TryParse(s, out guidId);
|
||||
|
||||
case Udi u:
|
||||
guidId = u;
|
||||
return true;
|
||||
|
||||
default:
|
||||
guidId = default;
|
||||
return false;
|
||||
}
|
||||
guidId = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
Reference in New Issue
Block a user