Ejemplo n.º 1
0
    /// <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,
        });
    }
    public async Task Lookup_By_Url_Alias(
        string relativeUrl,
        int nodeMatch,
        [Frozen] IPublishedContentCache publishedContentCache,
        [Frozen] IUmbracoContextAccessor umbracoContextAccessor,
        [Frozen] IUmbracoContext umbracoContext,
        [Frozen] IVariationContextAccessor variationContextAccessor,
        IFileService fileService,
        ContentFinderByUrlAlias sut,
        IPublishedContent[] rootContents,
        IPublishedProperty urlProperty)
    {
        // Arrange
        var absoluteUrl      = "http://localhost" + relativeUrl;
        var variationContext = new VariationContext();

        var contentItem = rootContents[0];

        Mock.Get(umbracoContextAccessor).Setup(x => x.TryGetUmbracoContext(out umbracoContext)).Returns(true);
        Mock.Get(umbracoContext).Setup(x => x.Content).Returns(publishedContentCache);
        Mock.Get(publishedContentCache).Setup(x => x.GetAtRoot(null)).Returns(rootContents);
        Mock.Get(contentItem).Setup(x => x.Id).Returns(nodeMatch);
        Mock.Get(contentItem).Setup(x => x.GetProperty(Constants.Conventions.Content.UrlAlias)).Returns(urlProperty);
        Mock.Get(urlProperty).Setup(x => x.GetValue(null, null)).Returns(relativeUrl);

        Mock.Get(variationContextAccessor).Setup(x => x.VariationContext).Returns(variationContext);
        var publishedRequestBuilder = new PublishedRequestBuilder(new Uri(absoluteUrl, UriKind.Absolute), fileService);

        // Act
        var result = await sut.TryFindContent(publishedRequestBuilder);

        Assert.IsTrue(result);
        Assert.AreEqual(publishedRequestBuilder.PublishedContent.Id, nodeMatch);
    }
Ejemplo n.º 3
0
    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);
    }
Ejemplo n.º 4
0
    /// <inheritdoc />
    public async Task <IPublishedRequestBuilder> CreateRequestAsync(Uri uri)
    {
        // trigger the Creating event - at that point the URL can be changed
        // this is based on this old task here: https://issues.umbraco.org/issue/U4-7914 which was fulfiled by
        // this PR https://github.com/umbraco/Umbraco-CMS/pull/1137
        // It's to do with proxies, quote:

        /*
         *  "Thinking about another solution.
         *  We already have an event, PublishedContentRequest.Prepared, which triggers once the request has been prepared and domain, content, template have been figured out -- but before it renders -- so ppl can change things before rendering.
         *  Wondering whether we could have a event, PublishedContentRequest.Preparing, which would trigger before the request is prepared, and would let ppl change the value of the request's URI (which by default derives from the HttpContext request).
         *  That way, if an in-between equipement changes the URI, you could replace it with the original, public-facing URI before we process the request, meaning you could register your HTTPS domain and it would work. And you would have to supply code for each equipment. Less magic in Core."
         */

        // but now we'll just have one event for creating so if people wish to change the URL here they can but nothing else
        var creatingRequest = new CreatingRequestNotification(uri);
        await _eventAggregator.PublishAsync(creatingRequest);

        var publishedRequestBuilder = new PublishedRequestBuilder(creatingRequest.Url, _fileService);

        return(publishedRequestBuilder);
    }
Ejemplo n.º 5
0
        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);
        }
Ejemplo n.º 6
0
    /// <inheritdoc />
    public async Task <IPublishedRequest> UpdateRequestAsync(
        IPublishedRequest request,
        IPublishedContent?publishedContent)
    {
        // store the original (if any)
        IPublishedContent?content = request.PublishedContent;

        IPublishedRequestBuilder builder = new PublishedRequestBuilder(request.Uri, _fileService);

        // ensure we keep the previous domain and culture
        if (request.Domain is not null)
        {
            builder.SetDomain(request.Domain);
        }
        builder.SetCulture(request.Culture);

        // set to the new content (or null if specified)
        builder.SetPublishedContent(publishedContent);

        // re-route
        await RouteRequestInternalAsync(builder, true);

        // return if we are redirect
        if (builder.IsRedirect())
        {
            return(BuildRequest(builder));
        }

        // this will occur if publishedContent is null and the last chance finders also don't assign content
        if (!builder.HasPublishedContent())
        {
            // means the engine could not find a proper document to handle 404
            // restore the saved content so we know it exists
            builder.SetPublishedContent(content);
        }

        return(BuildRequest(builder));
    }