/// <summary> /// Gets an Umbraco context. /// </summary> /// <returns>An Umbraco context.</returns> /// <remarks>This should be the minimum Umbraco context.</remarks> public UmbracoContext GetUmbracoContextMock(IUmbracoContextAccessor accessor = null) { var httpContext = Mock.Of <HttpContextBase>(); var publishedSnapshotMock = new Mock <IPublishedSnapshot>(); publishedSnapshotMock.Setup(x => x.Members).Returns(Mock.Of <IPublishedMemberCache>()); var publishedSnapshot = publishedSnapshotMock.Object; var publishedSnapshotServiceMock = new Mock <IPublishedSnapshotService>(); publishedSnapshotServiceMock.Setup(x => x.CreatePublishedSnapshot(It.IsAny <string>())).Returns(publishedSnapshot); var publishedSnapshotService = publishedSnapshotServiceMock.Object; var umbracoSettings = GetUmbracoSettings(); var globalSettings = GetGlobalSettings(); var urlProviders = new UrlProviderCollection(Enumerable.Empty <IUrlProvider>()); if (accessor == null) { accessor = new TestUmbracoContextAccessor(); } var umbracoContextFactory = new UmbracoContextFactory( accessor, publishedSnapshotService, new TestVariationContextAccessor(), new TestDefaultCultureAccessor(), umbracoSettings, globalSettings, urlProviders, Mock.Of <IUserService>()); return(umbracoContextFactory.EnsureUmbracoContext(httpContext).UmbracoContext); }
public void Matches_Default_Index() { var globalSettings = TestObjects.GetGlobalSettings(); var attr = new RenderIndexActionSelectorAttribute(); var req = new RequestContext(); var umbracoContextFactory = new UmbracoContextFactory( Current.UmbracoContextAccessor, Mock.Of <IPublishedSnapshotService>(), new TestVariationContextAccessor(), new TestDefaultCultureAccessor(), TestObjects.GetUmbracoSettings(), globalSettings, new UrlProviderCollection(Enumerable.Empty <IUrlProvider>()), new MediaUrlProviderCollection(Enumerable.Empty <IMediaUrlProvider>()), Mock.Of <IUserService>()); var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of <HttpContextBase>()); var umbCtx = umbracoContextReference.UmbracoContext; var umbracoContextAccessor = new TestUmbracoContextAccessor(umbCtx); var ctrl = new MatchesDefaultIndexController { UmbracoContextAccessor = umbracoContextAccessor }; var controllerCtx = new ControllerContext(req, ctrl); var result = attr.IsValidForRequest(controllerCtx, GetRenderMvcControllerIndexMethodFromCurrentType(ctrl.GetType())); Assert.IsTrue(result); }
public void Can_Construct_And_Get_Result() { var globalSettings = TestObjects.GetGlobalSettings(); var umbracoContextFactory = new UmbracoContextFactory( Current.UmbracoContextAccessor, Mock.Of <IPublishedSnapshotService>(), new TestVariationContextAccessor(), new TestDefaultCultureAccessor(), TestObjects.GetUmbracoSettings(), globalSettings, new UrlProviderCollection(Enumerable.Empty <IUrlProvider>()), new MediaUrlProviderCollection(Enumerable.Empty <IMediaUrlProvider>()), Mock.Of <IUserService>()); var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of <HttpContextBase>()); var umbracoContext = umbracoContextReference.UmbracoContext; var umbracoContextAccessor = new TestUmbracoContextAccessor(umbracoContext); var ctrl = new TestSurfaceController(umbracoContextAccessor); var result = ctrl.Index(); Assert.IsNotNull(result); }
/// <summary> /// Gets an Umbraco context. /// </summary> /// <returns>An Umbraco context.</returns> /// <remarks>This should be the minimum Umbraco context.</remarks> public IUmbracoContext GetUmbracoContextMock(IUmbracoContextAccessor accessor = null) { var publishedSnapshotMock = new Mock <IPublishedSnapshot>(); publishedSnapshotMock.Setup(x => x.Members).Returns(Mock.Of <IPublishedMemberCache>()); var publishedSnapshot = publishedSnapshotMock.Object; var publishedSnapshotServiceMock = new Mock <IPublishedSnapshotService>(); publishedSnapshotServiceMock.Setup(x => x.CreatePublishedSnapshot(It.IsAny <string>())).Returns(publishedSnapshot); var publishedSnapshotService = publishedSnapshotServiceMock.Object; var globalSettings = GetGlobalSettings(); if (accessor == null) { accessor = new TestUmbracoContextAccessor(); } var httpContextAccessor = TestHelper.GetHttpContextAccessor(); var umbracoContextFactory = new UmbracoContextFactory( accessor, publishedSnapshotService, new TestVariationContextAccessor(), new TestDefaultCultureAccessor(), globalSettings, Mock.Of <IUserService>(), TestHelper.GetHostingEnvironment(), TestHelper.UriUtility, httpContextAccessor, new AspNetCookieManager(httpContextAccessor)); return(umbracoContextFactory.EnsureUmbracoContext().UmbracoContext); }
public void ParseLocalLinks(string input, string result) { var serviceCtxMock = new TestObjects(null).GetServiceContextMock(); //setup a mock entity service from the service context to return an integer for a GUID var entityService = Mock.Get(serviceCtxMock.EntityService); //entityService.Setup(x => x.GetId(It.IsAny<Guid>(), It.IsAny<UmbracoObjectTypes>())) // .Returns((Guid id, UmbracoObjectTypes objType) => // { // return Attempt.Succeed(1234); // }); //setup a mock url provider which we'll use fo rtesting var testUrlProvider = new Mock <IUrlProvider>(); testUrlProvider .Setup(x => x.GetUrl(It.IsAny <UmbracoContext>(), It.IsAny <IPublishedContent>(), It.IsAny <UrlProviderMode>(), It.IsAny <string>(), It.IsAny <Uri>())) .Returns((UmbracoContext umbCtx, IPublishedContent content, UrlProviderMode mode, string culture, Uri url) => UrlInfo.Url("/my-test-url")); var globalSettings = SettingsForTests.GenerateMockGlobalSettings(); var contentType = new PublishedContentType(666, "alias", PublishedItemType.Content, Enumerable.Empty <string>(), Enumerable.Empty <PublishedPropertyType>(), ContentVariation.Nothing); var publishedContent = Mock.Of <IPublishedContent>(); Mock.Get(publishedContent).Setup(x => x.Id).Returns(1234); Mock.Get(publishedContent).Setup(x => x.ContentType).Returns(contentType); var contentCache = Mock.Of <IPublishedContentCache>(); Mock.Get(contentCache).Setup(x => x.GetById(It.IsAny <int>())).Returns(publishedContent); Mock.Get(contentCache).Setup(x => x.GetById(It.IsAny <Guid>())).Returns(publishedContent); var snapshot = Mock.Of <IPublishedSnapshot>(); Mock.Get(snapshot).Setup(x => x.Content).Returns(contentCache); var snapshotService = Mock.Of <IPublishedSnapshotService>(); Mock.Get(snapshotService).Setup(x => x.CreatePublishedSnapshot(It.IsAny <string>())).Returns(snapshot); var umbracoContextFactory = new UmbracoContextFactory( Umbraco.Web.Composing.Current.UmbracoContextAccessor, snapshotService, new TestVariationContextAccessor(), new TestDefaultCultureAccessor(), Mock.Of <IUmbracoSettingsSection>(section => section.WebRouting == Mock.Of <IWebRoutingSection>(routingSection => routingSection.UrlProviderMode == "Auto")), globalSettings, new UrlProviderCollection(new[] { testUrlProvider.Object }), Mock.Of <IUserService>()); using (var reference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of <HttpContextBase>())) { var output = TemplateUtilities.ParseInternalLinks(input, reference.UmbracoContext.UrlProvider); Assert.AreEqual(result, output); } }
public void ParseLocalLinks(string input, string result) { //setup a mock url provider which we'll use for testing var testUrlProvider = new Mock <IUrlProvider>(); testUrlProvider .Setup(x => x.GetUrl(It.IsAny <UmbracoContext>(), It.IsAny <IPublishedContent>(), It.IsAny <UrlMode>(), It.IsAny <string>(), It.IsAny <Uri>())) .Returns((UmbracoContext umbCtx, IPublishedContent content, UrlMode mode, string culture, Uri url) => UrlInfo.Url("/my-test-url")); var globalSettings = SettingsForTests.GenerateMockGlobalSettings(); var contentType = new PublishedContentType(666, "alias", PublishedItemType.Content, Enumerable.Empty <string>(), Enumerable.Empty <PublishedPropertyType>(), ContentVariation.Nothing); var publishedContent = Mock.Of <IPublishedContent>(); Mock.Get(publishedContent).Setup(x => x.Id).Returns(1234); Mock.Get(publishedContent).Setup(x => x.ContentType).Returns(contentType); var contentCache = Mock.Of <IPublishedContentCache>(); Mock.Get(contentCache).Setup(x => x.GetById(It.IsAny <int>())).Returns(publishedContent); Mock.Get(contentCache).Setup(x => x.GetById(It.IsAny <Guid>())).Returns(publishedContent); var snapshot = Mock.Of <IPublishedSnapshot>(); Mock.Get(snapshot).Setup(x => x.Content).Returns(contentCache); var snapshotService = Mock.Of <IPublishedSnapshotService>(); Mock.Get(snapshotService).Setup(x => x.CreatePublishedSnapshot(It.IsAny <string>())).Returns(snapshot); var media = Mock.Of <IPublishedContent>(); Mock.Get(media).Setup(x => x.Url).Returns("/media/1001/my-image.jpg"); var mediaCache = Mock.Of <IPublishedMediaCache>(); Mock.Get(mediaCache).Setup(x => x.GetById(It.IsAny <Guid>())).Returns(media); var umbracoContextAccessor = new TestUmbracoContextAccessor(); var umbracoContextFactory = new UmbracoContextFactory( umbracoContextAccessor, snapshotService, new TestVariationContextAccessor(), new TestDefaultCultureAccessor(), Mock.Of <IUmbracoSettingsSection>(section => section.WebRouting == Mock.Of <IWebRoutingSection>(routingSection => routingSection.UrlProviderMode == "Auto")), globalSettings, new UrlProviderCollection(new[] { testUrlProvider.Object }), new MediaUrlProviderCollection(Enumerable.Empty <IMediaUrlProvider>()), Mock.Of <IUserService>()); using (var reference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of <HttpContextBase>())) { var output = TemplateUtilities.ParseInternalLinks(input, reference.UmbracoContext.UrlProvider, mediaCache); Assert.AreEqual(result, output); } }
public void Mock_Current_Page() { var webRoutingSettings = Mock.Of <IWebRoutingSection>(section => section.UrlProviderMode == "Auto"); var globalSettings = TestObjects.GetGlobalSettings(); var umbracoContextFactory = new UmbracoContextFactory( Current.UmbracoContextAccessor, Mock.Of <IPublishedSnapshotService>(), new TestVariationContextAccessor(), new TestDefaultCultureAccessor(), Mock.Of <IUmbracoSettingsSection>(section => section.WebRouting == webRoutingSettings), globalSettings, new UrlProviderCollection(Enumerable.Empty <IUrlProvider>()), new MediaUrlProviderCollection(Enumerable.Empty <IMediaUrlProvider>()), Mock.Of <IUserService>()); var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of <HttpContextBase>()); var umbracoContext = umbracoContextReference.UmbracoContext; var umbracoContextAccessor = new TestUmbracoContextAccessor(umbracoContext); var content = Mock.Of <IPublishedContent>(publishedContent => publishedContent.Id == 12345); var contextBase = umbracoContext.HttpContext; var publishedRouter = BaseWebTest.CreatePublishedRouter(TestObjects.GetUmbracoSettings().WebRouting); var frequest = publishedRouter.CreateRequest(umbracoContext, new Uri("http://localhost/test")); frequest.PublishedContent = content; var routeDefinition = new RouteDefinition { PublishedRequest = frequest }; var routeData = new RouteData(); routeData.DataTokens.Add(Core.Constants.Web.UmbracoRouteDefinitionDataToken, routeDefinition); var ctrl = new TestSurfaceController(umbracoContextAccessor, new UmbracoHelper()); ctrl.ControllerContext = new ControllerContext(contextBase, routeData, ctrl); var result = ctrl.GetContentFromCurrentPage() as PublishedContentResult; Assert.AreEqual(12345, result.Content.Id); }
public void Can_Lookup_Content() { var publishedSnapshot = new Mock <IPublishedSnapshot>(); publishedSnapshot.Setup(x => x.Members).Returns(Mock.Of <IPublishedMemberCache>()); var content = new Mock <IPublishedContent>(); content.Setup(x => x.Id).Returns(2); var publishedSnapshotService = new Mock <IPublishedSnapshotService>(); var globalSettings = TestObjects.GetGlobalSettings(); var umbracoContextFactory = new UmbracoContextFactory( Current.UmbracoContextAccessor, publishedSnapshotService.Object, new TestVariationContextAccessor(), new TestDefaultCultureAccessor(), Mock.Of <IUmbracoSettingsSection>(section => section.WebRouting == Mock.Of <IWebRoutingSection>(routingSection => routingSection.UrlProviderMode == "Auto")), globalSettings, new UrlProviderCollection(Enumerable.Empty <IUrlProvider>()), new MediaUrlProviderCollection(Enumerable.Empty <IMediaUrlProvider>()), Mock.Of <IUserService>()); var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of <HttpContextBase>()); var umbracoContext = umbracoContextReference.UmbracoContext; var umbracoContextAccessor = new TestUmbracoContextAccessor(umbracoContext); var helper = new UmbracoHelper( content.Object, Mock.Of <ITagQuery>(), Mock.Of <ICultureDictionaryFactory>(), Mock.Of <IUmbracoComponentRenderer>(), Mock.Of <IPublishedContentQuery>(query => query.Content(2) == content.Object), new MembershipHelper(umbracoContext.HttpContext, Mock.Of <IPublishedMemberCache>(), Mock.Of <MembershipProvider>(), Mock.Of <RoleProvider>(), Mock.Of <IMemberService>(), Mock.Of <IMemberTypeService>(), Mock.Of <IUserService>(), Mock.Of <IPublicAccessService>(), AppCaches.Disabled, Mock.Of <ILogger>())); var ctrl = new TestSurfaceController(umbracoContextAccessor, helper); var result = ctrl.GetContent(2) as PublishedContentResult; Assert.IsNotNull(result); Assert.IsNotNull(result.Content); Assert.AreEqual(2, result.Content.Id); }