public PersonalisationContext(string placeholder) { PageContext pageContext = this.CreatePageContext(new System.Web.HttpContextWrapper(HttpContext.Current)); _context = ContextService.Get().Push(pageContext); var tracker = new NullTracker(new StandardSession()) { IsActive = true }; Context.Site.SetDisplayMode(DisplayMode.Normal, DisplayModeDuration.Temporary); _tracker = new TrackerSwitcher(tracker); _placeholder = ContextService.Get().Push(new PlaceholderContext(placeholder)); }
public void IsValidForRequest_CurrentRenderingIDMatch_ShouldReturnTrue(ValidateRenderingIdAttribute attribute, ControllerContext controllerContext, Guid id) { //arrange InitControllerContext(controllerContext); controllerContext.HttpContext.Request.Form.Add("uid", id.ToString()); ContextService.Get().Push(new RenderingContext { Rendering = new Rendering { UniqueId = id } }); //act and assert attribute.IsValidForRequest(controllerContext, null).Should().BeTrue(); }
public void IsValidForRequest_RenderingIdInFormNotGuid_ShouldReturnFalse(ValidateRenderingIdAttribute attribute, ControllerContext controllerContext, string formId, Guid id) { //arrange InitControllerContext(controllerContext); controllerContext.HttpContext.Request.Form.Add("uid", formId); ContextService.Get().Push(new RenderingContext { Rendering = new Rendering { UniqueId = id } }); //act and assert attribute.IsValidForRequest(controllerContext, null).Should().BeFalse(); }
public void Get_NoRenderingContextNoItemSiteContextNoDefaultSearchQuery_ShouldReturnNull() { var context = new RenderingContext { Rendering = new Rendering { Item = null } }; ContextService.Get().Push(context); var repository = new SearchContextRepository(); var searchContext = repository.Get(); searchContext.Should().BeNull(); }
public void Render(object model, RenderingContext context) { using (ContextService.Get().Push(context)) { var renderingModel = model as RenderingModel; if (renderingModel != null) { var renderingParameters = renderingModel.Rendering.Parameters.ToDictionary(k => k.Key, k => k.Value); _adaptee.Render(JObject.FromObject(renderingParameters), context); } else { _adaptee.Render(model, context); } } }
public void Setup() { route = new Mock <RouteBase>(); route .Setup(r => r.GetVirtualPath(It.IsAny <RequestContext>(), It.IsAny <RouteValueDictionary>())) .Returns(() => virtualPathData); virtualPathData = new VirtualPathData(route.Object, "~/VirtualPath"); routes = new RouteCollection(); routes.Add(route.Object); originalRouteData = new RouteData(); string returnValue = ""; httpContext = new Mock <HttpContextBase>(); httpContext .Setup(hc => hc.Request.ApplicationPath).Returns("~"); httpContext .Setup(hc => hc.Response.ApplyAppPathModifier(It.IsAny <string>())) .Callback <string>(s => returnValue = s) .Returns(() => returnValue); httpContext .Setup(hc => hc.Server.Execute(It.IsAny <IHttpHandler>(), It.IsAny <TextWriter>(), It.IsAny <bool>())); viewContext = new ViewContext { RequestContext = new RequestContext(httpContext.Object, originalRouteData) }; viewDataContainer = new Mock <IViewDataContainer>(); htmlHelper = new Mock <HtmlHelper>(viewContext, viewDataContainer.Object, routes); var requestContext = new RequestContext(httpContext.Object, originalRouteData); ContextService.Get().Push <PageContext>(new PageContext() { RequestContext = requestContext }); pageContext = new Mock <IPageContext>(); pageContext .Setup(x => x.RequestContext) .Returns(requestContext); }
public void DatasourceString_EmptyDatasource_ContextItemAsLocationRoot([ResolvePipeline("getRenderingDatasource")] EmptyPipeline processor, [Content] Item renderingItem) { //arrange ContextService.Get().Push(new PageContext()); PageContext.Current.Item = renderingItem; var renderingModel = new QueryableDatasourceRenderingModel(); //act renderingModel.Initialize(new Rendering { DataSource = string.Empty, RenderingItem = new RenderingItem(renderingItem) }); //assert renderingModel.DatasourceString.Should().Be("+location:" + PageContext.Current.Item.ID); }
public void DatasourceString_EmptyDatasource_ContextItemAsLocationRoot([Content] Item renderingItem, [Greedy] QueryableDatasourceRenderingModel renderingModel) { //arrange ContextService.Get().Push(new PageContext()); PageContext.Current.Item = renderingItem; //act renderingModel.Initialize(new Rendering { DataSource = string.Empty, RenderingItem = new RenderingItem(renderingItem) }); //assert renderingModel.DatasourceString.Should().Be("+location:" + PageContext.Current.Item.ID); }
public void DemoContent_RenderingContextItemInitialized_ShouldReturnDemoContentView(Db db, IContactProfileProvider contact, IProfileProvider profile, ITracker tracker) { //arrange var itemID = ID.NewID; db.Add(new DbItem("ctx", itemID, Templates.DemoContent.ID)); var controller = new DemoController(contact, profile); var context = new RenderingContext(); context.ContextItem = db.GetItem(itemID); ContextService.Get().Push(context); using (new TrackerSwitcher(tracker)) { controller.DemoContent().As <ViewResult>().Model.Should().BeOfType <DemoContent>(); } }
public void DatasourceString_IdAsDatasource_IDSetAsLocationRoot([Content] Item renderingItem, [Greedy] QueryableDatasourceRenderingModel renderingModel) { //arrange ContextService.Get().Push(new PageContext()); PageContext.Current.Item = renderingItem; var dataSource = ID.NewID.ToString(); //act renderingModel.Initialize(new Rendering { DataSource = dataSource, RenderingItem = new RenderingItem(renderingItem) }); //assert renderingModel.DatasourceString.Should().Be("+location:" + dataSource); }
public override void Render(TextWriter writer) { var current = ContextService.Get().GetCurrent <ViewContext>(); var currentWriter = current.Writer; try { current.Writer = writer; // in itemvisualization.getrenderings, the context is swithed to shell#lang cookie??? // so if you're logged in into sitecore cms, you'll get the renderings in an incorrect language! HttpContext.Current.Request.Cookies.Remove("shell#lang"); new SitecorePlaceholder(Rendering.RenderingItem).RenderView(current, Rendering); } finally { current.Writer = currentWriter; } }
public void AddUniqueFormId_CurrentRenderingNotNull_ShouldReturnHiddenInput(HtmlHelper helper) { var id = Guid.NewGuid(); ContextService.Get().Push(new RenderingContext() { Rendering = new Rendering() { UniqueId = id } }); helper.Sitecore().CurrentRendering.Should().NotBeNull(); var xml = XDocument.Parse(helper.AddUniqueFormId().ToString()); xml.Root.Name.LocalName.Should().Be("input"); xml.Root.GetAttributeValue("name").Should().Be("uid"); Guid.Parse(xml.Root.GetAttributeValue("value")).Should().Be(id); }
public void LatestNews_ShouldReturnViewResult(Db db, string itemName, ID itemId, INewsRepositoryFactory factory) { //Arrange var controller = new NewsController(factory); db.Add(new DbItem(itemName, itemId, Templates.NewsFolder.ID)); var contextItem = db.GetItem(itemId); var context = new RenderingContext(); context.Rendering = new Rendering(); context.Rendering.Item = contextItem; ContextService.Get().Push(context); //Act var list = controller.LatestNews(); //Assert list.Should().BeOfType <ViewResult>(); }
public void ShouldInitObjectProperties() { var context = new RenderingContext(); var rendering = new Rendering(); var properties = new RenderingProperties(rendering); properties["Parameters"] = "property1=5&property2=10"; context.Rendering = new Rendering { Properties = properties }; var repository = new RenderingPropertiesRepository(); ContextService.Get().Push(context); var resultObject = repository.Get <Model>(); resultObject.Property1.ShouldBeEquivalentTo(5); resultObject.Property2.ShouldBeEquivalentTo(10); }
protected override void DoRender(HtmlTextWriter output) { HttpContextWrapper httpCtxWrapper = _pageCtx.RequestContext.HttpContext as HttpContextWrapper; if (httpCtxWrapper != null) { using (ContextService.Get().Push <Sitecore.Mvc.Presentation.PageContext>(_pageCtx)) { ViewContext viewCtx = CreateViewContext(httpCtxWrapper, _pageCtx.RequestContext.RouteData, _rendering); using (ContextService.Get().Push <ViewContext>(viewCtx)) { PipelineService.Get().RunPipeline <RenderRenderingArgs>("mvc.renderRendering", new RenderRenderingArgs(_rendering, output)); } } } else { throw new Exception("Invalid HttpContextWrapper"); } }
public void ShouldReturnSearchSettings(Item item) { var itemId = ID.NewID; var db = new Db { new DbItem("item", itemId, Templates.SearchResults.ID) { { Templates.SearchResults.Fields.Root, itemId.ToString() } } }; var testItem = db.GetItem(itemId); var context = new RenderingContext(); context.Rendering = new Rendering(); context.Rendering.Item = testItem; ContextService.Get().Push(context); var repository = new SearchSettingsRepository(); var searchSettings = repository.Get(); searchSettings.Should().BeOfType <SearchSettings>(); }
public void Initialize_TemplateResolved_DatasourceTemplateShouldBeSet([Content] DbTemplate templateItem, [ResolvePipeline("getRenderingDatasource")] FakeDatasourceResolverPipeline processor, [Content] Item renderingItem) { //arrange processor.Item = renderingItem.Database.GetItem(templateItem.ID); var rendering = new Rendering { DataSource = "ds", RenderingItem = new RenderingItem(renderingItem) }; ContextService.Get().Push(new PageContext()); PageContext.Current.Item = renderingItem; var renderingModel = new QueryableDatasourceRenderingModel(); //act renderingModel.Initialize(rendering); //assert renderingModel.DatasourceTemplate.Should().NotBeNull(); renderingModel.DatasourceTemplate.ID.Should().Be(templateItem.ID); }
public void Setup() { areaData = new AreaRouteData() { Controller = "Home", Action = "Index", Area = "Temp", UseChildActionBehavior = true }; var stream = new MemoryStream(); routeData = new RouteData(); routeDataMock = new Mock <IRouteData>(); routeDataMock.Setup(x => x.Values) .Returns(routeData.Values); routeDataMock.Setup(x => x.DataTokens) .Returns(routeData.DataTokens); httpContextMock = HttpContextHelpers.GetMockHttpContext(); httpContextMock .Setup(x => x.Response) .Returns(new HttpResponseWrapper(new HttpResponse(new StreamWriter(stream)))); var requestContext = new RequestContext(httpContextMock.Object, routeData); ContextService.Get().Push <PageContext>(new PageContext() { RequestContext = requestContext }); pageContextMock = new Mock <IPageContext>(); pageContextMock .Setup(x => x.RequestContext) .Returns(requestContext); viewContextProviderMock = new Mock <IViewContextProvider>(); }
protected override MvcRenderingContext ResolveContext(ViewContext viewContext, IViewDataContainer viewDataContainer, TextWriter writer) { var context = ContextService.Get().GetCurrentOrDefault <RenderingContext>(); return(new MvcRenderingContext(viewContext, viewDataContainer, writer, context)); }
public void DemoContent_RenderingContextItemNotInitialized_ShouldThrowException([Greedy] DemoController sut, [Modest] RenderingContext context) { ContextService.Get().Push(context); sut.Invoking(x => x.DemoContent()).ShouldThrow <InvalidDataSourceItemException>(); }
public object GetQueryResult() { return(context.Get(QUERY_RESULT)); }
public void Configure(IWindsorContainer container) { container.Register(Component.For <GridContext>().UsingFactoryMethod(() => GridContext.GetFromRenderingContext(ContextService.Get().GetCurrent <RenderingContext>())).LifestyleTransient()); container.Register(Component.For <ISitecoreRenderingRepository>().ImplementedBy <SitecoreRenderingRepository>()); container.Register(Component.For <ISitecoreCacheManager>().ImplementedBy <SitecoreCacheManager>()); container.Register(Component.For <INitroTemplateHandlerFactory>().ImplementedBy <SitecoreMvcNitroTemplateHandlerFactory>()); container.Register(Component.For <Database>().UsingFactoryMethod(() => Context.Database)); }
public ViewContext GetCurrentViewContext() { return(ContextService.Get().GetCurrent <ViewContext>()); }
protected static HtmlHelper GetHtmlHelper() { ViewContext current = ContextService.Get().GetCurrent <ViewContext>(); return(new HtmlHelper(current, new ViewDataContainer(current.ViewData))); }
public static IDisposable Enter(PageRenderItemDefinitionContext context) { Assert.ArgumentNotNull(context, "context"); return(ContextService.Get().Push(context)); }
// GET: Snippet public ActionResult CompositeComponent() { var renderingContext = RenderingContext.CurrentOrNull; if (renderingContext == null) { throw new ApplicationException("Could not find current rendering context, aborting"); } var hasDataSource = !string.IsNullOrWhiteSpace(renderingContext.Rendering.DataSource); var pageContext = new PageContext() { RequestContext = this.ControllerContext.RequestContext, Item = !hasDataSource ? null : renderingContext.Rendering.Item }; var oldDisplayMode = global::Sitecore.Context.Site != null ? global::Sitecore.Context.Site.DisplayMode : DisplayMode.Normal; try { if (oldDisplayMode == DisplayMode.Edit && global::Sitecore.Context.Site != null) { //disable the editing of the nested component global::Sitecore.Context.Site.SetDisplayMode(DisplayMode.Preview, DisplayModeDuration.Temporary); } using (PlaceholderContext.Enter(new PlaceholderContext("/"))) using (ContextService.Get().Push <PageContext>(pageContext)) { var htmlHelper = new HtmlHelper(new ViewContext(), new ViewPage()); var stringWriter = new StringWriter(); if (oldDisplayMode == DisplayMode.Edit) { if (hasDataSource) { UrlString webSiteUrl = SiteContext.GetWebSiteUrl(Sitecore.Context.Database); webSiteUrl.Add("sc_mode", "edit"); webSiteUrl.Add("sc_itemid", pageContext.Item.ID.ToString()); webSiteUrl.Add("sc_lang", pageContext.Item.Language.ToString()); //copied style from bootstrap alert stringWriter.Write("<div role=\"alert\" class=\"alert alert-warning\" style=\"box-sizing: border-box; margin-bottom: 20px; border-top: rgb(250,235,204) 1px solid; border-right: rgb(250,235,204) 1px solid; white-space: normal; word-spacing: 0px; border-bottom: rgb(250,235,204) 1px solid; text-transform: none; color: rgb(138,109,59); padding-bottom: 15px; padding-top: 15px; font: 14px/20px 'Helvetica Neue', helvetica, arial, sans-serif; padding-left: 15px; border-left: rgb(250,235,204) 1px solid; widows: 1; letter-spacing: normal; padding-right: 15px; background-color: rgb(252,248,227); text-indent: 0px; border-radius: 4px; -webkit-text-stroke-width: 0px\">"); stringWriter.Write("<a class=\"alert-link\" style=\"box-sizing: border-box; text-decoration: none; font-weight: 700; color: rgb(102,81,44); background-color: transparent\" href=\""); stringWriter.Write(webSiteUrl); stringWriter.Write("\" target=\"_blank\" onmousedown=\"window.open(this.href)\">""); stringWriter.Write(pageContext.Item.DisplayName); stringWriter.Write("" is a 'composite component'. Click here to open it's editor</a><br /></div>"); } else { //copied style from bootstrap alert stringWriter.Write("<div role=\"alert\" class=\"alert alert-warning\" style=\"box-sizing: border-box; margin-bottom: 20px; border-top: rgb(250,235,204) 1px solid; border-right: rgb(250,235,204) 1px solid; white-space: normal; word-spacing: 0px; border-bottom: rgb(250,235,204) 1px solid; text-transform: none; color: rgb(138,109,59); padding-bottom: 15px; padding-top: 15px; font: 14px/20px 'Helvetica Neue', helvetica, arial, sans-serif; padding-left: 15px; border-left: rgb(250,235,204) 1px solid; widows: 1; letter-spacing: normal; padding-right: 15px; background-color: rgb(252,248,227); text-indent: 0px; border-radius: 4px; -webkit-text-stroke-width: 0px\">"); stringWriter.Write("<a class=\"alert-link\" style=\"box-sizing: border-box; text-decoration: none; font-weight: 700; color: rgb(102,81,44); background-color: transparent\" href=\"\" onmousedown=\""); stringWriter.Write("Sitecore.PageModes.PageEditor.postRequest('webedit:setdatasource(referenceId="); stringWriter.Write(renderingContext.Rendering.UniqueId.ToString("B").ToUpper()); stringWriter.Write(",renderingId="); stringWriter.Write(renderingContext.Rendering.RenderingItem.ID); stringWriter.Write(",id="); stringWriter.Write(renderingContext.Rendering.Item.ID); stringWriter.Write(")',null,false)"); stringWriter.Write("\" target=\"_blank\">This is a 'composite component' without a datasource. Click here to associate a composite component instance</a><br /></div>"); } } else { var enableAync = htmlHelper.GetCheckboxRenderingParameterValue("Enable Async"); var baseUrl = htmlHelper.GetRenderingParameter("Async Fetch Base Url"); var componentClass = enableAync ? "composite async" : "composite"; var tagAttributes = htmlHelper.GetContainerTagAttributes(componentClass); var asyncUrl = renderingContext.Rendering.Item.GetItemUrl(); if (!string.IsNullOrEmpty(baseUrl)) { asyncUrl = baseUrl + "/" + asyncUrl; } var asyncAttr = enableAync ? string.Format(@"data-src=""{0}""", asyncUrl) : string.Empty; stringWriter.Write(string.Format(@"<div {0} {1}>", tagAttributes, asyncAttr)); } if (hasDataSource) { var loadAsyncOnly = htmlHelper.GetCheckboxRenderingParameterValue("Load Content Async Only"); if (!loadAsyncOnly || oldDisplayMode == DisplayMode.Edit) { PipelineService.Get().RunPipeline <RenderPlaceholderArgs>("mvc.renderPlaceholder", new RenderPlaceholderArgs(pageContext.Item["PlaceholderName"] ?? "compositecontent", (TextWriter)stringWriter, new ContentRendering())); } } if (oldDisplayMode != DisplayMode.Edit) { stringWriter.Write("</div>"); } return(Content(stringWriter.ToString())); } } finally { global::Sitecore.Context.Site.SetDisplayMode(oldDisplayMode, DisplayModeDuration.Temporary); } }
// GET: Snippet public ActionResult CompositeComponent() { var renderingContext = RenderingContext.CurrentOrNull; if (renderingContext == null) { throw new ApplicationException("Could not find current rendering context, aborting"); } var hasDataSource = !string.IsNullOrWhiteSpace(renderingContext.Rendering.DataSource); var dataSourceItem = hasDataSource ? renderingContext.Rendering.Item : null; var pageContext = new PageContext() { RequestContext = this.ControllerContext.RequestContext, Item = dataSourceItem }; var oldDisplayMode = global::Sitecore.Context.Site != null ? global::Sitecore.Context.Site.DisplayMode : DisplayMode.Normal; try { if (oldDisplayMode == DisplayMode.Edit && global::Sitecore.Context.Site != null) { //disable the editing of the nested component global::Sitecore.Context.Site.SetDisplayMode(DisplayMode.Preview, DisplayModeDuration.Temporary); } using (PlaceholderContext.Enter(new PlaceholderContext("/"))) using (ContextService.Get().Push <PageContext>(pageContext)) { var stringWriter = new StringWriter(); if (oldDisplayMode == DisplayMode.Edit) { if (hasDataSource) { UrlString webSiteUrl = SiteContext.GetWebSiteUrl(global::Sitecore.Context.ContentDatabase ?? global::Sitecore.Context.Database); webSiteUrl.Add("sc_mode", "edit"); webSiteUrl.Add("sc_itemid", pageContext.Item.ID.ToString()); webSiteUrl.Add("sc_lang", pageContext.Item.Language.ToString()); //copied style from bootstrap alert stringWriter.Write("<div role=\"alert\" class=\"alert alert-warning\" style=\"box-sizing: border-box; margin-bottom: 20px; border-top: rgb(250,235,204) 1px solid; border-right: rgb(250,235,204) 1px solid; white-space: normal; word-spacing: 0px; border-bottom: rgb(250,235,204) 1px solid; text-transform: none; color: rgb(138,109,59); padding-bottom: 15px; padding-top: 15px; font: 14px/20px 'Helvetica Neue', helvetica, arial, sans-serif; padding-left: 15px; border-left: rgb(250,235,204) 1px solid; widows: 1; letter-spacing: normal; padding-right: 15px; background-color: rgb(252,248,227); text-indent: 0px; border-radius: 4px; -webkit-text-stroke-width: 0px\">"); stringWriter.Write("<a class=\"alert-link\" style=\"box-sizing: border-box; text-decoration: none; font-weight: 700; color: rgb(102,81,44); background-color: transparent\" href=\""); stringWriter.Write(webSiteUrl); stringWriter.Write("\" target=\"_blank\" onmousedown=\"window.open(this.href)\">""); stringWriter.Write(pageContext.Item.DisplayName); stringWriter.Write("" is a 'composite component'. Click here to open it's editor</a><br /></div>"); } else { //copied style from bootstrap alert stringWriter.Write("<div role=\"alert\" class=\"alert alert-warning\" style=\"box-sizing: border-box; margin-bottom: 20px; border-top: rgb(250,235,204) 1px solid; border-right: rgb(250,235,204) 1px solid; white-space: normal; word-spacing: 0px; border-bottom: rgb(250,235,204) 1px solid; text-transform: none; color: rgb(138,109,59); padding-bottom: 15px; padding-top: 15px; font: 14px/20px 'Helvetica Neue', helvetica, arial, sans-serif; padding-left: 15px; border-left: rgb(250,235,204) 1px solid; widows: 1; letter-spacing: normal; padding-right: 15px; background-color: rgb(252,248,227); text-indent: 0px; border-radius: 4px; -webkit-text-stroke-width: 0px\">"); stringWriter.Write("<a class=\"alert-link\" style=\"box-sizing: border-box; text-decoration: none; font-weight: 700; color: rgb(102,81,44); background-color: transparent\" href=\"\" onmousedown=\""); stringWriter.Write("Sitecore.PageModes.PageEditor.postRequest('webedit:setdatasource(referenceId="); stringWriter.Write(renderingContext.Rendering.UniqueId.ToString("B").ToUpper()); stringWriter.Write(",renderingId="); stringWriter.Write(renderingContext.Rendering.RenderingItem.ID); stringWriter.Write(",id="); stringWriter.Write(renderingContext.Rendering.Item.ID); stringWriter.Write(")',null,false)"); stringWriter.Write("\" target=\"_blank\">This is a 'composite component' without a datasource. Click here to associate a composite component instance</a><br /></div>"); } } if (hasDataSource && dataSourceItem != null && dataSourceItem?.TemplateID == new global::Sitecore.Data.ID(CompositeComponentInstanceTemplateId) && !string.IsNullOrEmpty(pageContext.Item?["PlaceholderName"])) { PipelineService.Get().RunPipeline <RenderPlaceholderArgs>("mvc.renderPlaceholder", new RenderPlaceholderArgs(pageContext.Item["PlaceholderName"], (TextWriter)stringWriter, new ContentRendering())); } return(Content(stringWriter.ToString())); } } finally { global::Sitecore.Context.Site.SetDisplayMode(oldDisplayMode, DisplayModeDuration.Temporary); } }
public void Configure(IServiceCollection serviceCollection) { serviceCollection.AddTransient(provider => GridContext.GetFromRenderingContext(ContextService.Get().GetCurrent <RenderingContext>())); serviceCollection.AddTransient <ISitecoreRenderingRepository, SitecoreRenderingRepository>(); serviceCollection.AddTransient <ISitecoreCacheManager, SitecoreCacheManager>(); serviceCollection.AddTransient <INitroTemplateHandlerFactory, SitecoreMvcNitroTemplateHandlerFactory>(); serviceCollection.AddTransient(provider => Context.Database); }