Files
Umbraco-CMS/src/Umbraco.Tests/Routing/NiceUrlProviderTests.cs
2013-01-23 09:41:00 -01:00

167 lines
6.0 KiB
C#

using System;
using System.Configuration;
using System.Collections.Generic;
using NUnit.Framework;
using Umbraco.Tests.TestHelpers;
using Umbraco.Web.Routing;
namespace Umbraco.Tests.Routing
{
[TestFixture]
public class NiceUrlProviderTests : BaseRoutingTest
{
public override void Initialize()
{
base.Initialize();
Umbraco.Core.Configuration.UmbracoSettings.UseLegacyXmlSchema = false;
}
public override void TearDown()
{
base.TearDown();
ConfigurationManager.AppSettings.Set("umbracoUseDirectoryUrls", "");
ConfigurationManager.AppSettings.Set("umbracoHideTopLevelNodeFromPath", "");
}
internal override IRoutesCache GetRoutesCache()
{
return new DefaultRoutesCache(false);
}
/// <summary>
/// This checks that when we retreive a NiceUrl for multiple items that there are no issues with cache overlap
/// and that they are all cached correctly.
/// </summary>
[Test]
public void Ensure_Cache_Is_Correct()
{
var routingContext = GetRoutingContext("/test", 1111);
ConfigurationManager.AppSettings.Set("umbracoUseDirectoryUrls", "true");
Umbraco.Core.Configuration.UmbracoSettings.AddTrailingSlash = false;
var samples = new Dictionary<int, string> {
{ 1046, "/home" },
{ 1173, "/home/sub1" },
{ 1174, "/home/sub1/sub2" },
{ 1176, "/home/sub1/sub-3" },
{ 1177, "/home/sub1/custom-sub-1" },
{ 1178, "/home/sub1/custom-sub-2" },
{ 1175, "/home/sub-2" },
{ 1172, "/test-page" }
};
foreach (var sample in samples)
{
var result = routingContext.NiceUrlProvider.GetNiceUrl(sample.Key);
Assert.AreEqual(sample.Value, result);
}
var randomSample = new KeyValuePair<int, string>(1177, "/home/sub1/custom-sub-1");
for (int i = 0; i < 5; i++)
{
var result = routingContext.NiceUrlProvider.GetNiceUrl(randomSample.Key);
Assert.AreEqual(randomSample.Value, result);
}
var cachedRoutes = ((DefaultRoutesCache)routingContext.UmbracoContext.RoutesCache).GetCachedRoutes();
Assert.AreEqual(8, cachedRoutes.Count);
foreach (var sample in samples)
{
Assert.IsTrue(cachedRoutes.ContainsKey(sample.Key));
Assert.AreEqual(sample.Value, cachedRoutes[sample.Key]);
}
var cachedIds = ((DefaultRoutesCache)routingContext.UmbracoContext.RoutesCache).GetCachedIds();
Assert.AreEqual(8, cachedIds.Count);
foreach (var sample in samples)
{
var key = sample.Value;
Assert.IsTrue(cachedIds.ContainsKey(key));
Assert.AreEqual(sample.Key, cachedIds[key]);
}
}
// test hideTopLevelNodeFromPath false
[TestCase(1046, "/home/")]
[TestCase(1173, "/home/sub1/")]
[TestCase(1174, "/home/sub1/sub2/")]
[TestCase(1176, "/home/sub1/sub-3/")]
[TestCase(1177, "/home/sub1/custom-sub-1/")]
[TestCase(1178, "/home/sub1/custom-sub-2/")]
[TestCase(1175, "/home/sub-2/")]
[TestCase(1172, "/test-page/")]
public void Get_Nice_Url_Not_Hiding_Top_Level(int nodeId, string niceUrlMatch)
{
var routingContext = GetRoutingContext("/test", 1111);
ConfigurationManager.AppSettings.Set("umbracoUseDirectoryUrls", "true");
ConfigurationManager.AppSettings.Set("umbracoHideTopLevelNodeFromPath", "false");
Umbraco.Core.Configuration.UmbracoSettings.UseDomainPrefixes = false;
var result = routingContext.NiceUrlProvider.GetNiceUrl(nodeId);
Assert.AreEqual(niceUrlMatch, result);
}
// no need for umbracoUseDirectoryUrls test = should be handled by UriUtilityTests
// test hideTopLevelNodeFromPath true
[TestCase(1046, "/")]
[TestCase(1173, "/sub1/")]
[TestCase(1174, "/sub1/sub2/")]
[TestCase(1176, "/sub1/sub-3/")]
[TestCase(1177, "/sub1/custom-sub-1/")]
[TestCase(1178, "/sub1/custom-sub-2/")]
[TestCase(1175, "/sub-2/")]
[TestCase(1172, "/test-page/")] // not hidden because not first root
public void Get_Nice_Url_Hiding_Top_Level(int nodeId, string niceUrlMatch)
{
var routingContext = GetRoutingContext("/test", 1111);
ConfigurationManager.AppSettings.Set("umbracoUseDirectoryUrls", "true");
ConfigurationManager.AppSettings.Set("umbracoHideTopLevelNodeFromPath", "true");
Umbraco.Core.Configuration.UmbracoSettings.UseDomainPrefixes = false;
var result = routingContext.NiceUrlProvider.GetNiceUrl(nodeId);
Assert.AreEqual(niceUrlMatch, result);
}
[Test]
public void Get_Nice_Url_Relative_Or_Absolute()
{
var routingContext = GetRoutingContext("http://example.com/test", 1111);
ConfigurationManager.AppSettings.Set("umbracoUseDirectoryUrls", "true");
ConfigurationManager.AppSettings.Set("umbracoHideTopLevelNodeFromPath", "false");
Umbraco.Core.Configuration.UmbracoSettings.UseDomainPrefixes = false;
Assert.AreEqual("/home/sub1/custom-sub-1/", routingContext.NiceUrlProvider.GetNiceUrl(1177));
Umbraco.Core.Configuration.UmbracoSettings.UseDomainPrefixes = true;
Assert.AreEqual("http://example.com/home/sub1/custom-sub-1/", routingContext.NiceUrlProvider.GetNiceUrl(1177));
Umbraco.Core.Configuration.UmbracoSettings.UseDomainPrefixes = false;
routingContext.NiceUrlProvider.EnforceAbsoluteUrls = true;
Assert.AreEqual("http://example.com/home/sub1/custom-sub-1/", routingContext.NiceUrlProvider.GetNiceUrl(1177));
}
[Test]
public void Get_Nice_Url_Unpublished()
{
var routingContext = GetRoutingContext("http://example.com/test", 1111);
ConfigurationManager.AppSettings.Set("umbracoUseDirectoryUrls", "true");
ConfigurationManager.AppSettings.Set("umbracoHideTopLevelNodeFromPath", "false");
Umbraco.Core.Configuration.UmbracoSettings.UseDomainPrefixes = false;
Assert.AreEqual("#", routingContext.NiceUrlProvider.GetNiceUrl(999999));
Umbraco.Core.Configuration.UmbracoSettings.UseDomainPrefixes = true;
Assert.AreEqual("#", routingContext.NiceUrlProvider.GetNiceUrl(999999));
Umbraco.Core.Configuration.UmbracoSettings.UseDomainPrefixes = false;
routingContext.NiceUrlProvider.EnforceAbsoluteUrls = true;
Assert.AreEqual("#", routingContext.NiceUrlProvider.GetNiceUrl(999999));
}
}
}