/// <summary> /// Creates a binding context with the route values populated to similute an Umbraco dynamically routed request /// </summary> private ModelBindingContext CreateBindingContextForUmbracoRequest( Type modelType, IPublishedContent publishedContent) { var builder = new PublishedRequestBuilder(new Uri("https://example.com"), Mock.Of <IFileService>()); builder.SetPublishedContent(publishedContent); var publishedRequest = builder.Build(); var httpContext = new DefaultHttpContext(); var routeData = new RouteData(); httpContext.Features.Set(new UmbracoRouteValues(publishedRequest, null)); var actionContext = new ActionContext(httpContext, routeData, new ActionDescriptor()); var metadataProvider = new EmptyModelMetadataProvider(); var routeValueDictionary = new RouteValueDictionary(); var valueProvider = new RouteValueProvider(BindingSource.Path, routeValueDictionary); return(new DefaultModelBindingContext { ActionContext = actionContext, ModelMetadata = metadataProvider.GetMetadataForType(modelType), ModelName = modelType.Name, ValueProvider = valueProvider, }); }
private UmbracoRouteValuesFactory GetFactory( out Mock <IPublishedRouter> publishedRouter, out IOptions <UmbracoRenderingDefaultsOptions> renderingDefaults, out IPublishedRequest request) { var builder = new PublishedRequestBuilder(new Uri("https://example.com"), Mock.Of <IFileService>()); builder.SetPublishedContent(Mock.Of <IPublishedContent>()); var builtRequest = request = builder.Build(); publishedRouter = new Mock <IPublishedRouter>(); publishedRouter.Setup(x => x.UpdateRequestAsync(It.IsAny <IPublishedRequest>(), null)) .Returns((IPublishedRequest r, IPublishedContent c) => Task.FromResult(builtRequest)) .Verifiable(); renderingDefaults = Mock.Of <IOptions <UmbracoRenderingDefaultsOptions> >(x => x.Value.DefaultControllerType == typeof(RenderController)); // add the default one var actionDescriptors = new List <ActionDescriptor> { new ControllerActionDescriptor { ControllerName = ControllerExtensions.GetControllerName <RenderController>(), ActionName = nameof(RenderController.Index), ControllerTypeInfo = typeof(RenderController).GetTypeInfo(), }, }; var actionSelector = new Mock <IActionSelector>(); actionSelector.Setup(x => x.SelectCandidates(It.IsAny <RouteContext>())).Returns(actionDescriptors); var factory = new UmbracoRouteValuesFactory( renderingDefaults, Mock.Of <IShortStringHelper>(), new UmbracoFeatures(), new ControllerActionSearcher( new NullLogger <ControllerActionSearcher>(), actionSelector.Object), publishedRouter.Object); return(factory); }
public void Mock_Current_Page() { var globalSettings = new GlobalSettings(); IHostingEnvironment hostingEnvironment = Mock.Of <IHostingEnvironment>(); IBackOfficeSecurityAccessor backofficeSecurityAccessor = Mock.Of <IBackOfficeSecurityAccessor>(); Mock.Get(backofficeSecurityAccessor).Setup(x => x.BackOfficeSecurity).Returns(Mock.Of <IBackOfficeSecurity>()); var umbracoContextFactory = TestUmbracoContextFactory.Create(globalSettings, _umbracoContextAccessor); UmbracoContextReference umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(); IUmbracoContext umbracoContext = umbracoContextReference.UmbracoContext; var umbracoContextAccessor = new TestUmbracoContextAccessor(umbracoContext); IPublishedContent content = Mock.Of <IPublishedContent>(publishedContent => publishedContent.Id == 12345); var builder = new PublishedRequestBuilder(umbracoContext.CleanedUmbracoUrl, Mock.Of <IFileService>()); builder.SetPublishedContent(content); IPublishedRequest publishedRequest = builder.Build(); var routeDefinition = new UmbracoRouteValues(publishedRequest, null); var httpContext = new DefaultHttpContext(); httpContext.Features.Set(routeDefinition); var ctrl = new TestSurfaceController(umbracoContextAccessor, Mock.Of <IPublishedContentQuery>(), Mock.Of <IPublishedUrlProvider>()) { ControllerContext = new ControllerContext() { HttpContext = httpContext, RouteData = new RouteData() } }; var result = ctrl.GetContentFromCurrentPage() as PublishedContentResult; Assert.AreEqual(12345, result.Content.Id); }