Files
Umbraco-CMS/src/Umbraco.Tests/Web/WebExtensionMethodTests.cs

146 lines
5.9 KiB
C#
Raw Normal View History

2016-10-13 21:08:07 +02:00
using System.Collections.Generic;
2016-02-17 11:21:15 +01:00
using System.IO;
2019-04-15 15:57:35 +02:00
using System.Linq;
2016-02-17 11:21:15 +01:00
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Moq;
using NUnit.Framework;
using Umbraco.Core.Services;
using Umbraco.Tests.Common;
using Umbraco.Tests.TestHelpers;
2016-11-07 19:12:09 +01:00
using Umbraco.Tests.TestHelpers.Stubs;
using Umbraco.Tests.Testing;
using Umbraco.Tests.Testing.Objects.Accessors;
2016-02-17 11:21:15 +01:00
using Umbraco.Web;
using Umbraco.Web.Mvc;
using Umbraco.Web.PublishedCache;
2016-02-17 11:21:15 +01:00
using Umbraco.Web.Routing;
using Umbraco.Web.Security;
2017-05-30 18:13:11 +02:00
using Current = Umbraco.Web.Composing.Current;
2016-02-17 11:21:15 +01:00
namespace Umbraco.Tests.Web
{
[TestFixture]
[UmbracoTest(WithApplication = true)]
public class WebExtensionMethodTests : UmbracoTestBase
2016-02-17 11:21:15 +01:00
{
[Test]
public void RouteDataExtensions_GetUmbracoContext()
{
var httpContextAccessor = TestHelper.GetHttpContextAccessor();
2020-02-13 12:29:08 +01:00
2016-10-18 17:09:26 +02:00
var umbCtx = new UmbracoContext(
httpContextAccessor,
2017-10-31 12:48:24 +01:00
Mock.Of<IPublishedSnapshotService>(),
new WebSecurity(httpContextAccessor, ServiceContext.UserService, TestObjects.GetGlobalSettings(), IOHelper),
TestObjects.GetGlobalSettings(),
2019-12-04 14:03:39 +01:00
new TestVariationContextAccessor(),
IOHelper,
UriUtility,
new AspNetCookieManager(httpContextAccessor));
2016-02-17 11:21:15 +01:00
var r1 = new RouteData();
r1.DataTokens.Add(Core.Constants.Web.UmbracoContextDataToken, umbCtx);
2016-06-09 11:57:13 +02:00
Assert.IsTrue(r1.DataTokens.ContainsKey(Core.Constants.Web.UmbracoContextDataToken));
Assert.AreSame(umbCtx, r1.DataTokens[Core.Constants.Web.UmbracoContextDataToken]);
2016-02-17 11:21:15 +01:00
}
[Test]
public void ControllerContextExtensions_GetUmbracoContext_From_RouteValues()
{
var httpContextAccessor = TestHelper.GetHttpContextAccessor();
2020-02-13 12:29:08 +01:00
2016-10-18 17:09:26 +02:00
var umbCtx = new UmbracoContext(
httpContextAccessor,
2017-10-31 12:48:24 +01:00
Mock.Of<IPublishedSnapshotService>(),
new WebSecurity(httpContextAccessor, ServiceContext.UserService, TestObjects.GetGlobalSettings(), IOHelper),
TestObjects.GetGlobalSettings(),
2019-12-04 14:03:39 +01:00
new TestVariationContextAccessor(),
IOHelper,
UriUtility,
new AspNetCookieManager(httpContextAccessor));
2016-02-17 11:21:15 +01:00
var r1 = new RouteData();
r1.DataTokens.Add(Core.Constants.Web.UmbracoContextDataToken, umbCtx);
var ctx1 = CreateViewContext(new ControllerContext(Mock.Of<HttpContextBase>(), r1, new MyController()));
var r2 = new RouteData();
r2.DataTokens.Add("ParentActionViewContext", ctx1);
var ctx2 = CreateViewContext(new ControllerContext(Mock.Of<HttpContextBase>(), r2, new MyController()));
var r3 = new RouteData();
r3.DataTokens.Add("ParentActionViewContext", ctx2);
var ctx3 = CreateViewContext(new ControllerContext(Mock.Of<HttpContextBase>(), r3, new MyController()));
var result = ctx3.GetUmbracoContext();
Assert.IsNotNull(result);
Assert.AreSame(umbCtx, result);
}
[Test]
2016-06-09 11:57:13 +02:00
public void ControllerContextExtensions_GetUmbracoContext_From_Current()
2016-02-17 11:21:15 +01:00
{
var httpContextAccessor = TestHelper.GetHttpContextAccessor();
2020-02-13 12:29:08 +01:00
2016-10-18 17:09:26 +02:00
var umbCtx = new UmbracoContext(
httpContextAccessor,
2017-10-31 12:48:24 +01:00
Mock.Of<IPublishedSnapshotService>(),
new WebSecurity(httpContextAccessor, ServiceContext.UserService, TestObjects.GetGlobalSettings(), IOHelper),
TestObjects.GetGlobalSettings(),
2019-12-04 14:03:39 +01:00
new TestVariationContextAccessor(),
IOHelper,
UriUtility,
new AspNetCookieManager(httpContextAccessor));
2016-06-09 11:57:13 +02:00
var httpContext = Mock.Of<HttpContextBase>();
2016-02-17 11:21:15 +01:00
var r1 = new RouteData();
2016-06-09 11:57:13 +02:00
var ctx1 = CreateViewContext(new ControllerContext(httpContext, r1, new MyController()));
2016-02-17 11:21:15 +01:00
var r2 = new RouteData();
r2.DataTokens.Add("ParentActionViewContext", ctx1);
2016-06-09 11:57:13 +02:00
var ctx2 = CreateViewContext(new ControllerContext(httpContext, r2, new MyController()));
2016-02-17 11:21:15 +01:00
var r3 = new RouteData();
r3.DataTokens.Add("ParentActionViewContext", ctx2);
2016-06-09 11:57:13 +02:00
var ctx3 = CreateViewContext(new ControllerContext(httpContext, r3, new MyController()));
2016-10-13 21:08:07 +02:00
Current.UmbracoContextAccessor = new TestUmbracoContextAccessor();
2016-10-18 17:09:26 +02:00
Current.UmbracoContextAccessor.UmbracoContext = umbCtx;
2016-02-17 11:21:15 +01:00
var result = ctx3.GetUmbracoContext();
Assert.IsNotNull(result);
Assert.AreSame(umbCtx, result);
}
[Test]
public void ControllerContextExtensions_GetDataTokenInViewContextHierarchy()
{
var r1 = new RouteData();
r1.DataTokens.Add("hello", "world");
2016-06-09 11:57:13 +02:00
r1.DataTokens.Add("r", "1");
2016-02-17 11:21:15 +01:00
var ctx1 = CreateViewContext(new ControllerContext(Mock.Of<HttpContextBase>(), r1, new MyController()));
var r2 = new RouteData();
r2.DataTokens.Add("ParentActionViewContext", ctx1);
2016-06-09 11:57:13 +02:00
r2.DataTokens.Add("r", "2");
2016-02-17 11:21:15 +01:00
var ctx2 = CreateViewContext(new ControllerContext(Mock.Of<HttpContextBase>(), r2, new MyController()));
var r3 = new RouteData();
r3.DataTokens.Add("ParentActionViewContext", ctx2);
2016-06-09 11:57:13 +02:00
r3.DataTokens.Add("r", "3");
2016-02-17 11:21:15 +01:00
var ctx3 = CreateViewContext(new ControllerContext(Mock.Of<HttpContextBase>(), r3, new MyController()));
var result = ctx3.GetDataTokenInViewContextHierarchy("hello");
Assert.IsNotNull(result as string);
2016-06-09 11:57:13 +02:00
Assert.AreEqual((string) result, "world");
2016-02-17 11:21:15 +01:00
}
2016-06-09 11:57:13 +02:00
private static ViewContext CreateViewContext(ControllerContext ctx)
2016-02-17 11:21:15 +01:00
{
2016-06-09 11:57:13 +02:00
return new ViewContext(ctx, Mock.Of<IView>(), new ViewDataDictionary(), new TempDataDictionary(), new StringWriter());
2016-02-17 11:21:15 +01:00
}
private class MyController : Controller
2016-06-09 11:57:13 +02:00
{ }
2016-02-17 11:21:15 +01:00
}
}