Ejemplo n.º 1
0
        /// <summary>
        /// Makes a matched group sticky for the visitor via a cookie setting according to group definition
        /// </summary>
        /// <param name="definition">Matched group definition</param>
        /// <param name="groupNodeId">Id of the matched groups node</param>
        internal static void MakeStickyMatch(PersonalisationGroupDefinition definition, int groupNodeId)
        {
            if (definition.Duration == PersonalisationGroupDefinitionDuration.Page)
            {
                return;
            }

            var httpContext = HttpContext.Current;
            var key         = GetCookieKeyForMatchedGroups(definition.Duration);
            var cookie      = httpContext.Request.Cookies[key];

            if (cookie != null)
            {
                cookie.Value = AppendGroupNodeId(cookie.Value, groupNodeId);
            }
            else
            {
                cookie = new HttpCookie(key)
                {
                    Value    = groupNodeId.ToString(),
                    HttpOnly = true,
                };
            }

            if (definition.Duration == PersonalisationGroupDefinitionDuration.Visitor)
            {
                var cookieExpiryInDays = PersonalisationGroupsConfig.Value.PersistentMatchedGroupsCookieExpiryInDays;
                cookie.Expires = DateTime.Now.AddDays(cookieExpiryInDays);
            }

            httpContext.Response.Cookies.Add(cookie);
        }
        /// <summary>
        /// Makes a matched group sticky for the visitor via a cookie setting according to group definition
        /// </summary>
        /// <param name="definition">Matched group definition</param>
        /// <param name="groupNodeId">Id of the matched groups node</param>
        public void MakeStickyMatch(PersonalisationGroupDefinition definition, int groupNodeId)
        {
            if (definition.Duration == PersonalisationGroupDefinitionDuration.Page)
            {
                return;
            }

            var key   = GetCookieKeyForMatchedGroups(definition.Duration);
            var value = _cookieProvider.GetCookieValue(key);

            if (!string.IsNullOrEmpty(value))
            {
                value = AppendGroupNodeId(value, groupNodeId);
            }
            else
            {
                value = groupNodeId.ToString();
            }

            DateTime?expires = null;

            if (definition.Duration == PersonalisationGroupDefinitionDuration.Visitor)
            {
                var cookieExpiryInDays = _config.PersistentMatchedGroupsCookieExpiryInDays;
                expires = _dateTimeProvider.GetCurrentDateTime().AddDays(cookieExpiryInDays);
            }

            _cookieProvider.SetCookie(key, value, expires);
        }
        public bool IsStickyMatch(PersonalisationGroupDefinition definition, int groupNodeId)
        {
            if (definition.Duration == PersonalisationGroupDefinitionDuration.Page)
            {
                return(false);
            }

            var key         = GetCookieKeyForMatchedGroups(definition.Duration);
            var cookieValue = _cookieProvider.GetCookieValue(key);

            return(!string.IsNullOrEmpty(cookieValue) && IsGroupMatched(cookieValue, groupNodeId));
        }
Ejemplo n.º 4
0
        internal static bool IsStickyMatch(PersonalisationGroupDefinition definition, int groupNodeId)
        {
            if (definition.Duration == PersonalisationGroupDefinitionDuration.Page)
            {
                return(false);
            }

            var httpContext = HttpContext.Current;
            var key         = GetCookieKeyForMatchedGroups(definition.Duration);
            var cookie      = httpContext.Request.Cookies[key];

            return(!string.IsNullOrEmpty(cookie?.Value) && IsGroupMatched(cookie.Value, groupNodeId));
        }
        private void AppendMatchedGroupDetailToVisitorHashString(StringBuilder sb, PersonalisationGroupDefinition definition, string name)
        {
            var matchCount = CountMatchingDefinitionDetails(definition);
            var matched    = (definition.Match == PersonalisationGroupDefinitionMatch.Any && matchCount > 0) ||
                             (definition.Match == PersonalisationGroupDefinitionMatch.All && matchCount == definition.Details.Count());

            if (sb.Length > 0)
            {
                sb.Append(',');
            }

            sb.AppendFormat("{0}={1}", name, matched);
        }
Ejemplo n.º 6
0
        private static IPublishedContent CreateContent(PersonalisationGroupDefinition definition = null)
        {
            var mockContent = new Mock <IPublishedContent>();

            if (definition != null)
            {
                var property = new Mock <IPublishedProperty>();
                property.Setup(x => x.Alias).Returns(Core.AppConstants.PersonalisationGroupDefinitionPropertyAlias);
                property.Setup(x => x.GetValue(It.IsAny <string>(), It.IsAny <string>())).Returns(definition);
                property.Setup(x => x.HasValue(It.IsAny <string>(), It.IsAny <string>())).Returns(true);
                mockContent.Setup(x => x.GetProperty(Core.AppConstants.PersonalisationGroupDefinitionPropertyAlias)).Returns(property.Object);
            }

            return(mockContent.Object);
        }
        public void PersonalisationGroupMatcher_CountMatchingDefinitionDetails_WithDefinitonForMatchAll_AndMatchesAll_ReturnsCount()
        {
            // Arrange
            var definition = new PersonalisationGroupDefinition
            {
                Match   = PersonalisationGroupDefinitionMatch.All,
                Details = new List <PersonalisationGroupDefinitionDetail>
                {
                    TestHelpers.Definitions.MatchingDayOfWeekDefinition(),
                TestHelpers.Definitions.MatchingTimeOfDayDefinition(),
                }
            };

            // Act
            var result = PersonalisationGroupMatcher.CountMatchingDefinitionDetails(definition);

            // Assert
            Assert.AreEqual(2, result);
        }
        public void PersonalisationGroupMatcher_CountMatchingDefinitionDetails_WithDefinitonForMatchAny_AndMatchingFirst_ReturnsShortCutCount()
        {
            // Arrange
            var definition = new PersonalisationGroupDefinition
            {
                Match   = PersonalisationGroupDefinitionMatch.All,
                Details = new List <PersonalisationGroupDefinitionDetail>
                {
                    Definitions.MatchingDayOfWeekDefinition(),
                Definitions.NonMatchingDayOfWeekDefinition(),
                Definitions.MatchingTimeOfDayDefinition(),
                }
            };

            // Act
            var result = PersonalisationGroupMatcher.CountMatchingDefinitionDetails(definition);

            // Assert
            Assert.AreEqual(1, result);
        }
Ejemplo n.º 9
0
        public void GroupMatchingService_MatchSingleGroup_WithSingleDefinitionDetail_DoesNotMatch_ReturnsFalse()
        {
            // Arrange
            var sut        = CreateGroupMatchingService();
            var definition = new PersonalisationGroupDefinition
            {
                Match   = PersonalisationGroupDefinitionMatch.All,
                Details = new List <PersonalisationGroupDefinitionDetail>
                {
                    Definitions.NonMatchingDayOfWeekDefinition(),
                }
            };
            var groupContent = CreateContent(definition);

            // Act
            var result = sut.MatchGroup(groupContent);

            // Assert
            Assert.IsFalse(result);
        }
Ejemplo n.º 10
0
        public void GroupMatchingService_MatchSingleGroup_WithMultipleDefinitionDetails_ForAll_MatchingAll_ReturnsTrue()
        {
            // Arrange
            var sut        = CreateGroupMatchingService();
            var definition = new PersonalisationGroupDefinition
            {
                Match   = PersonalisationGroupDefinitionMatch.All,
                Details = new List <PersonalisationGroupDefinitionDetail>
                {
                    Definitions.MatchingDayOfWeekDefinition(),
                Definitions.MatchingTimeOfDayDefinition(),
                }
            };
            var groupContent = CreateContent(definition);

            // Act
            var result = sut.MatchGroup(groupContent);

            // Assert
            Assert.IsTrue(result);
        }
Ejemplo n.º 11
0
        public void GroupMatchingService_ScoreSingleGroup_WithSingleDefinitionDetail_DoesNotMatch_ReturnCorrectScore()
        {
            // Arrange
            var sut        = CreateGroupMatchingService();
            var definition = new PersonalisationGroupDefinition
            {
                Match   = PersonalisationGroupDefinitionMatch.All,
                Score   = 50,
                Details = new List <PersonalisationGroupDefinitionDetail>
                {
                    Definitions.NonMatchingDayOfWeekDefinition(),
                }
            };
            var groupContent = CreateContent(definition);

            // Act
            var result = sut.ScoreGroup(groupContent);

            // Assert
            Assert.AreEqual(0, result);
        }
        /// <summary>
        /// Gets a count of the number of the definition details for a given personalisation group definition that matches
        /// the current site visitor
        /// </summary>
        /// <param name="definition">Personalisation group definition</param>
        /// <returns>Number of definition details that match</returns>
        private static int CountMatchingDefinitionDetails(PersonalisationGroupDefinition definition)
        {
            var matchCount = 0;

            foreach (var detail in definition.Details)
            {
                var isMatch = PersonalisationGroupMatcher.IsMatch(detail);
                if (isMatch)
                {
                    matchCount++;
                }

                // We can short-cut here if matching any and found one match, or matching all and found one mismatch
                if ((isMatch && definition.Match == PersonalisationGroupDefinitionMatch.Any) ||
                    (!isMatch && definition.Match == PersonalisationGroupDefinitionMatch.All))
                {
                    break;
                }
            }

            return(matchCount);
        }
Ejemplo n.º 13
0
        public void GroupMatchingService_ScoreSingleGroup_WithMultipleScore_ForAny_MatchingOne_ReturnCorrectScore()
        {
            // Arrange
            var sut        = CreateGroupMatchingService();
            var definition = new PersonalisationGroupDefinition
            {
                Match   = PersonalisationGroupDefinitionMatch.Any,
                Score   = 50,
                Details = new List <PersonalisationGroupDefinitionDetail>
                {
                    Definitions.NonMatchingDayOfWeekDefinition(),
                Definitions.MatchingTimeOfDayDefinition(),
                }
            };
            var groupContent = CreateContent(definition);

            // Act
            var result = sut.ScoreGroup(groupContent);

            // Assert
            Assert.AreEqual(50, result);
        }
        /// <summary>
        /// Makes a matched group sticky for the visitor via a cookie setting according to group definition
        /// </summary>
        /// <param name="definition">Matched group definition</param>
        /// <param name="groupNodeId">Id of the matched groups node</param>
        private static void MakeStickyMatch(PersonalisationGroupDefinition definition, int groupNodeId)
        {
            if (definition.Duration == PersonalisationGroupDefinitionDuration.Page)
            {
                return;
            }

            var httpContext = HttpContext.Current;
            var key         = GetCookieKeyForMatchedGroups(definition.Duration);
            var cookie      = httpContext.Request.Cookies[key];

            if (cookie != null)
            {
                cookie.Value = AppendGroupNodeId(cookie.Value, groupNodeId);
            }
            else
            {
                cookie = new HttpCookie(key)
                {
                    Value    = groupNodeId.ToString(),
                    HttpOnly = true,
                };
            }

            if (definition.Duration == PersonalisationGroupDefinitionDuration.Visitor)
            {
                int cookieExpiryInDays;
                if (!int.TryParse(ConfigurationManager.AppSettings[AppConstants.ConfigKeys.PersistentMatchedGroupsCookieExpiryInDays], out cookieExpiryInDays))
                {
                    cookieExpiryInDays = AppConstants.DefaultMatchedGroupsTrackingCookieExpiryInDays;
                }

                cookie.Expires = DateTime.Now.AddDays(cookieExpiryInDays);
            }

            httpContext.Response.Cookies.Add(cookie);
        }