/// <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);
        }
예제 #2
0
        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);
        }
예제 #3
0
        public UmbracoTestFixture()
        {
            Customize(new AutoNSubstituteCustomization());

            ContextAccessor          = this.Freeze <IUmbracoContextAccessor>();
            MediaUrlProvider         = this.Freeze <IMediaUrlProvider>();
            UrlProvider              = this.Freeze <IUrlProvider>();
            PublishedSnapshotService = this.Freeze <IPublishedSnapshotService>();
            VariationContextAccessor = this.Freeze <IVariationContextAccessor>();
            DefaultCultureAccessor   = this.Freeze <IDefaultCultureAccessor>();
            GlobalSettings           = this.Freeze <IGlobalSettings>();
            UserService              = this.Freeze <IUserService>();
            DomainCache              = this.Freeze <IDomainCache>();
            RedirectUrlService       = this.Freeze <IRedirectUrlService>();
            UmbracoSettingsSection   = this.Freeze <UmbracoSettingsSection>();

            SimpleWorkerRequest = new SimpleWorkerRequest("/", @"c:\", "null.aspx", string.Empty, TextWriter.Null);

            ContextFactory = new UmbracoContextFactory(
                ContextAccessor,
                PublishedSnapshotService,
                VariationContextAccessor,
                DefaultCultureAccessor,
                UmbracoSettingsSection,
                GlobalSettings,
                new UrlProviderCollection(new[] { UrlProvider }),
                new MediaUrlProviderCollection(new[] { MediaUrlProvider }),
                UserService);
        }
예제 #4
0
        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);
        }
예제 #5
0
        /// <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);
            }
        }
예제 #7
0
        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 static IUmbracoContextFactory Create(
            GlobalSettings globalSettings = null,
            IUmbracoContextAccessor umbracoContextAccessor = null,
            IHttpContextAccessor httpContextAccessor       = null,
            IPublishedUrlProvider publishedUrlProvider     = null)
        {
            if (globalSettings == null)
            {
                globalSettings = new GlobalSettings();
            }

            if (umbracoContextAccessor == null)
            {
                umbracoContextAccessor = new TestUmbracoContextAccessor();
            }

            if (httpContextAccessor == null)
            {
                httpContextAccessor = Mock.Of <IHttpContextAccessor>();
            }

            if (publishedUrlProvider == null)
            {
                publishedUrlProvider = Mock.Of <IPublishedUrlProvider>();
            }

            var contentCache = new Mock <IPublishedContentCache>();
            var mediaCache   = new Mock <IPublishedMediaCache>();
            var snapshot     = new Mock <IPublishedSnapshot>();

            snapshot.Setup(x => x.Content).Returns(contentCache.Object);
            snapshot.Setup(x => x.Media).Returns(mediaCache.Object);
            var snapshotService = new Mock <IPublishedSnapshotService>();

            snapshotService.Setup(x => x.CreatePublishedSnapshot(It.IsAny <string>())).Returns(snapshot.Object);

            IHostingEnvironment hostingEnvironment = TestHelper.GetHostingEnvironment();

            var umbracoContextFactory = new UmbracoContextFactory(
                umbracoContextAccessor,
                snapshotService.Object,
                new UmbracoRequestPaths(Options.Create(globalSettings), hostingEnvironment),
                hostingEnvironment,
                new UriUtility(hostingEnvironment),
                new AspNetCoreCookieManager(httpContextAccessor),
                httpContextAccessor);

            return(umbracoContextFactory);
        }
예제 #9
0
        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 static IUmbracoContextFactory Create(IGlobalSettings globalSettings                 = null, IUrlProvider urlProvider = null,
                                                    IMediaUrlProvider mediaUrlProvider             = null,
                                                    IUmbracoContextAccessor umbracoContextAccessor = null)
        {
            if (globalSettings == null)
            {
                globalSettings = SettingsForTests.GenerateMockGlobalSettings();
            }
            if (urlProvider == null)
            {
                urlProvider = Mock.Of <IUrlProvider>();
            }
            if (mediaUrlProvider == null)
            {
                mediaUrlProvider = Mock.Of <IMediaUrlProvider>();
            }
            if (umbracoContextAccessor == null)
            {
                umbracoContextAccessor = new TestUmbracoContextAccessor();
            }

            var contentCache = new Mock <IPublishedContentCache>();
            var mediaCache   = new Mock <IPublishedMediaCache>();
            var snapshot     = new Mock <IPublishedSnapshot>();

            snapshot.Setup(x => x.Content).Returns(contentCache.Object);
            snapshot.Setup(x => x.Media).Returns(mediaCache.Object);
            var snapshotService = new Mock <IPublishedSnapshotService>();

            snapshotService.Setup(x => x.CreatePublishedSnapshot(It.IsAny <string>())).Returns(snapshot.Object);

            var umbracoContextFactory = new UmbracoContextFactory(
                umbracoContextAccessor,
                snapshotService.Object,
                new TestVariationContextAccessor(),
                new TestDefaultCultureAccessor(),
                Mock.Of <IUmbracoSettingsSection>(section => section.WebRouting == Mock.Of <IWebRoutingSection>(routingSection => routingSection.UrlProviderMode == "Auto")),
                globalSettings,
                new UrlProviderCollection(new[] { urlProvider }),
                new MediaUrlProviderCollection(new[] { mediaUrlProvider }),
                Mock.Of <IUserService>());

            return(umbracoContextFactory);
        }
예제 #11
0
        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);
        }
        public void CanHandleEvent()
        {
            // refreshers.HandleEvents wants a UmbracoContext
            // which wants an HttpContext, which we build using a SimpleWorkerRequest
            // which requires these to be non-null
            var domain = Thread.GetDomain();

            if (domain.GetData(".appPath") == null)
            {
                domain.SetData(".appPath", "");
            }
            if (domain.GetData(".appVPath") == null)
            {
                domain.SetData(".appVPath", "");
            }

            // create some event definitions
            var definitions = new IEventDefinition[]
            {
                // works because that event definition maps to an empty handler
                new EventDefinition <IContentTypeService, SaveEventArgs <IContentType> >(null, Current.Services.ContentTypeService, new SaveEventArgs <IContentType>(Enumerable.Empty <IContentType>()), "Saved"),
            };

            var umbracoContextFactory = new UmbracoContextFactory(
                new TestUmbracoContextAccessor(),
                Mock.Of <IPublishedSnapshotService>(),
                new TestVariationContextAccessor(),
                new TestDefaultCultureAccessor(),
                TestObjects.GetUmbracoSettings(),
                TestObjects.GetGlobalSettings(),
                new UrlProviderCollection(Enumerable.Empty <IUrlProvider>()),
                new MediaUrlProviderCollection(Enumerable.Empty <IMediaUrlProvider>()),
                Mock.Of <IUserService>());

            // just assert it does not throw
            var refreshers = new DistributedCacheBinder(null, umbracoContextFactory, null);

            refreshers.HandleEvents(definitions);
        }