private void Process(PublishedContentRequest request)
        {
            //Are we rendering a published content (sanity check)?
            if (request?.PublishedContent == null)
            {
                return;
            }

            //Are there any experiments running
            var experiments = new GetApplicableCachedExperiments
            {
                ContentId = request.PublishedContent.Id
            }.ExecuteAsync().Result;

            //variations of the same content will be applied in the order the experiments were created,
            //if they override the same property variation of the newest experiment wins
            var variationsToApply = new List <IPublishedContentVariation>();

            foreach (var experiment in experiments)
            {
                var eventArgs = new BeforeExperimentExecutedArgs(experiment, request.RoutingContext?.UmbracoContext?.HttpContext);
                OnBeforeExperimentExecuted(eventArgs);

                if (eventArgs.SkipExperiment) //allow the developer to opt out
                {
                    continue;
                }

                var experimentId = experiment.GoogleExperiment.Id;

                //Has the user been previously exposed to this experiment?
                var assignedVariationId = GetAssignedVariation(request, experiment.Id);
                if (assignedVariationId != null)
                {
                    if (ShouldApplyVariation(experiment, assignedVariationId.Value))
                    {
                        var variationContent = GetVariationContent(experiment, assignedVariationId.Value);
                        variationsToApply.Add(new PublishedContentVariation(variationContent, experimentId, assignedVariationId.Value));
                    }
                }
                //Should the user be included in the experiment?
                else if (ShouldVisitorParticipate(experiment))
                {
                    //Choose a variation for the user
                    var variationId = SelectVariation(experiment);
                    AssignVariationToUser(request, experiment.Id, variationId);
                    if (ShouldApplyVariation(experiment, variationId))
                    {
                        var variationContent = GetVariationContent(experiment, variationId);
                        variationsToApply.Add(new PublishedContentVariation(variationContent, experimentId, variationId));
                    }
                }
                else
                {
                    AssignVariationToUser(request, experiment.Id, -1);
                }
            }

            //the visitor is excluded or we didn't find the content needed for variations, just show original
            if (variationsToApply.Count <= 0)
            {
                return;
            }

            var variedContent = new VariedContent(request.PublishedContent, variationsToApply.ToArray());

            request.PublishedContent = variedContent;
            request.SetIsInitialPublishedContent();

            //Reset the published content now we have set the initial content
            request.PublishedContent = PublishedContentModelFactoryResolver.Current.Factory.CreateModel(variedContent);

            request.TrySetTemplate(variedContent.GetTemplateAlias());
        }
        public override void Initialize()
        {
            base.Initialize();

            settings = SettingsForTests.GenerateMockSettings();
            SettingsForTests.ConfigureSettings(settings);

            var routeData = new RouteData();

            routeData.Values["controller"] = "DummyTestController";
            routeData.Values["action"]     = "Index";

            routingContext = GetRoutingContext("http://localhost", -1, routeData, umbracoSettings: settings);
            umbracoContext = routingContext.UmbracoContext;

            httpContext = umbracoContext.HttpContext;
            var httpContextMock = Mock.Get(httpContext);

            httpContextMock.Setup(x => x.Items).Returns(
                new ListDictionary()
            {
                { HttpContextExtensions.HttpContextItemName, umbracoContext }
            }
                );

            response     = httpContext.Response;
            responseMock = Mock.Get(response);
            responseMock.Setup(r => r.ContentType).Returns("text/html");
            responseMock.SetupProperty(r => r.Filter, Stream.Null);

            var experimentsPipeline = new ExperimentsPipeline();

            experimentsPipeline.OnApplicationStarted(null, ApplicationContext);

            var originalMock = new Mock <IPublishedContent>();

            originalMock.Setup(c => c.Properties).Returns(new List <IPublishedProperty>());
            experimentMock = new Mock <IExperiment>();
            experimentMock.Setup(e => e.Id).Returns("TESTPERIMENT");

            publishedContentVariationMock = new Mock <IPublishedContentVariation>();
            publishedContentVariationMock.SetupAllProperties();
            publishedContentVariationMock.SetupProperty(p => p.Content, originalMock.Object);

            variedContent = new VariedContent(originalMock.Object, new [] { publishedContentVariationMock.Object });

            var docRequest = new PublishedContentRequest(
                new Uri("/", UriKind.Relative),
                routingContext,
                settings.WebRouting,
                s => Enumerable.Empty <string>())
            {
                PublishedContent = variedContent
            };

            docRequest.SetIsInitialPublishedContent();

            umbracoContext.PublishedContentRequest = docRequest;

            var viewContext = new Mock <ViewContext>();

            viewContext.Setup(c => c.HttpContext).Returns(httpContext);

            actionExecutedMock = new Mock <ActionExecutedContext>(MockBehavior.Strict);
            actionExecutedMock.Setup(f => f.IsChildAction).Returns(false);
            actionExecutedMock.Setup(f => f.HttpContext).Returns(httpContext);

            resultExecutedMock = new Mock <ResultExecutedContext>(MockBehavior.Strict);
            resultExecutedMock.Setup(f => f.HttpContext).Returns(httpContext);

            htmlHelper = new HtmlHelper(viewContext.Object, Mock.Of <IViewDataContainer>());
        }
示例#3
0
        private void Process(PublishedContentRequest request)
        {
            //Are we rendering a published content (sanity check)?
            if (request?.PublishedContent == null)
            {
                return;
            }

            //Are there any experiments running
            var experiments = new GetApplicableCachedExperiments
            {
                ContentId = request.PublishedContent.Id
            }.ExecuteAsync().Result;

            //variations of the same content will be applied in the order the experiments were created,
            //if they override the same property variation of the newest experiment wins
            var variationsToApply = new List <IPublishedContentVariation>();

            foreach (var experiment in experiments)
            {
                var experimentId = experiment.GoogleExperiment.Id;

                //Has the user been previously exposed to this experiment?
                var assignedVariationId = GetAssignedVariation(request, experiment.Id);
                if (assignedVariationId != null)
                {
                    var variation = GetVariation(request, experiment, assignedVariationId.Value);
                    if (variation != null)
                    {
                        variationsToApply.Add(new PublishedContentVariation(variation, experimentId, assignedVariationId.Value));
                    }
                }
                //Should the user be included in the experiment?
                else if (ShouldVisitorParticipate(experiment))
                {
                    //Choose a variation for the user
                    var variationId = SelectVariation(experiment);
                    var variation   = GetVariation(request, experiment, variationId);
                    if (variation != null)
                    {
                        variationsToApply.Add(new PublishedContentVariation(variation, experimentId, variationId));
                    }
                }
                else
                {
                    //should we assign -1 as variation (remember we showed nothing? - maybe in case we decide to exclude if say only 60% traffic is covered)
                }
            }

            //the visitor is excluded or we didn't find the content needed for variations, just show original
            if (variationsToApply.Count <= 0)
            {
                return;
            }

            var variedContent = new VariedContent(request.PublishedContent, variationsToApply.ToArray());

            request.PublishedContent = variedContent;
            request.SetIsInitialPublishedContent();

            //Reset the published content now we have set the initial content
            request.PublishedContent = PublishedContentModelFactoryResolver.Current.Factory.CreateModel(variedContent);

            request.TrySetTemplate(variedContent.GetTemplateAlias());
        }