Example #1
0
        public void ScreamingSnakeFilterTests()
        {
            var fieldFilter  = "~Electronic Arts";
            var ffCollection = MagicTokenTransformer.GetFieldTransforms(fieldFilter);

            Assert.NotNull(ffCollection.Transforms);
            Assert.Equal(1, ffCollection.Transforms.Length);

            var ff = ffCollection.Transforms.First();

            Assert.Equal("Electronic Arts", ff.FieldName);

            // Screaming Snake only alters the field value, it should always deploy
            Assert.True(ff.ShouldDeployFieldValue("P2W", "Lootboxes"), "ScreamingSnakeFilter did not want to deploy field (value,value)");
            Assert.True(ff.ShouldDeployFieldValue(null, "Lootboxes"), "ScreamingSnakeFilter did not want to deploy field (null, value)");
            Assert.True(ff.ShouldDeployFieldValue("P2W", null), "ScreamingSnakeFilter did not want to deploy field (value,null)");
            Assert.False(ff.ShouldDeployFieldValue(null, null), "ScreamingSnakeFilter wanted to deploy field (null, null)");
            Assert.False(ff.ShouldDeployFieldValue("SAMEVALUE_IS_GOOD", "samevalue is good"), "ScreamingSnakeFilter wanted deploy field (targetValue, samevalue)");

            Assert.Equal("LOOTBOXES", ff.GetFieldValue("P2W", "Lootboxes"));
            Assert.Equal("LOOTBOXES", ff.GetFieldValue(null, "Lootboxes"));
            Assert.Null(ff.GetFieldValue("P2W", null));
            Assert.Null(ff.GetFieldValue(null, null));
            Assert.Equal("LOOTBOXES_R_BAD_MKAY", ff.GetFieldValue("P2W", "Lootboxes r bad mkay"));
        }
Example #2
0
        public void LoremIpsumTitleFilterTests()
        {
            var fieldFilter  = ";Electronic Arts";
            var ffCollection = MagicTokenTransformer.GetFieldTransforms(fieldFilter);

            Assert.NotNull(ffCollection.Transforms);
            Assert.Equal(1, ffCollection.Transforms.Length);

            var ff = ffCollection.Transforms.First();

            Assert.Equal("Electronic Arts", ff.FieldName);

            // LoremIpsumTitle always forces a value (duh), it should always deploy
            Assert.True(ff.ShouldDeployFieldValue("P2W", "Lootboxes"), "LoremIpsumBodyFilterTests did not want to deploy field (value,value)");
            Assert.True(ff.ShouldDeployFieldValue(null, "Lootboxes"), "LoremIpsumBodyFilterTests did not want to deploy field (null, value)");
            Assert.True(ff.ShouldDeployFieldValue("P2W", null), "LoremIpsumBodyFilterTests did not want to deploy field (value,null)");
            Assert.True(ff.ShouldDeployFieldValue(null, null), "LoremIpsumBodyFilterTests did not want to deploy field (null, null)");

            // Except when the LoremIpsumTitle is already on the field
            Assert.False(ff.ShouldDeployFieldValue(MagicTokenTransformer.LoremIpsumTitle, MagicTokenTransformer.LoremIpsumTitle), "LoremIpsumBodyFilterTests wanted to deploy field (lit, lit)");

            Assert.Equal(MagicTokenTransformer.LoremIpsumTitle, ff.GetFieldValue("P2W", "Lootboxes"));
            Assert.Equal(MagicTokenTransformer.LoremIpsumTitle, ff.GetFieldValue(null, "Lootboxes"));
            Assert.Equal(MagicTokenTransformer.LoremIpsumTitle, ff.GetFieldValue("P2W", null));
            Assert.Equal(MagicTokenTransformer.LoremIpsumTitle, ff.GetFieldValue(null, null));
        }
Example #3
0
        public void ForcedFieldValueFilterTests()
        {
            var fieldFilter  = "+Electronic Arts[${settings:Lootboxes}]";
            var ffCollection = MagicTokenTransformer.GetFieldTransforms(fieldFilter);

            Assert.NotNull(ffCollection.Transforms);
            Assert.Equal(1, ffCollection.Transforms.Length);

            var ff = ffCollection.Transforms.First();

            Assert.Equal("Electronic Arts", ff.FieldName);
            Assert.Equal("${settings:Lootboxes}", ff.ForcedValue);

            // ForcedFieldValue always forces a value (duh), it should always deploy
            Assert.True(ff.ShouldDeployFieldValue("P2W", "Lootboxes"), "ForcedFieldValueFilter did not want to deploy field (value,value)");
            Assert.True(ff.ShouldDeployFieldValue(null, "Lootboxes"), "ForcedFieldValueFilter did not want to deploy field (null, value)");
            Assert.True(ff.ShouldDeployFieldValue("P2W", null), "ForcedFieldValueFilter did not want to deploy field (value,null)");
            Assert.True(ff.ShouldDeployFieldValue(null, null), "ForcedFieldValueFilter did not want to deploy field (null, null)");
            Assert.False(ff.ShouldDeployFieldValue("${settings:Lootboxes}", "samevalue"), "ForcedFieldValueFilter wanted to deploy field (targetValue, samevalue)");

            Assert.Equal("${settings:Lootboxes}", ff.GetFieldValue("P2W", "Lootboxes"));
            Assert.Equal("${settings:Lootboxes}", ff.GetFieldValue(null, "Lootboxes"));
            Assert.Equal("${settings:Lootboxes}", ff.GetFieldValue("P2W", null));
            Assert.Equal("${settings:Lootboxes}", ff.GetFieldValue(null, null));
        }
Example #4
0
        public void SitecoreSettingsFilterTests()
        {
            var fieldFilter  = "$Electronic Arts[Configuration.Lootboxes]";
            var ffCollection = MagicTokenTransformer.GetFieldTransforms(fieldFilter);

            Assert.NotNull(ffCollection.Transforms);
            Assert.Equal(1, ffCollection.Transforms.Length);

            var ff = ffCollection.Transforms.First();

            Assert.Equal("Electronic Arts", ff.FieldName);
            Assert.Equal("Configuration.Lootboxes", ff.ForcedValue);

            // SitecoreSettings always forces a value (duh), it should always deploy
            Assert.True(ff.ShouldDeployFieldValue("P2W", "Lootboxes"), "SitecoreSettingsValueFilter did not want to deploy field (value,value)");
            Assert.True(ff.ShouldDeployFieldValue(null, "Lootboxes"), "SitecoreSettingsValueFilter did not want to deploy field (null, value)");
            Assert.True(ff.ShouldDeployFieldValue("P2W", null), "SitecoreSettingsValueFilter did not want to deploy field (value,null)");
            Assert.True(ff.ShouldDeployFieldValue(null, null), "SitecoreSettingsValueFilter did not want to deploy field (null, null)");

            using (new SettingsSwitcher("Configuration.Lootboxes", "false"))
            {
                Assert.Equal("false", ff.GetFieldValue("P2W", "Lootboxes"));
                Assert.Equal("false", ff.GetFieldValue(null, "Lootboxes"));
                Assert.Equal("false", ff.GetFieldValue("P2W", null));
                Assert.Equal("false", ff.GetFieldValue(null, null));

                // except when the field value already matches the setting
                Assert.False(ff.ShouldDeployFieldValue("false", "samevalue"), "SitecoreSettingsValueFilter wanted to deploy field (targetValue, samevalue)");
            }
        }
Example #5
0
        private IList <PresetTreeRoot> ParsePreset(XmlNode configuration, string configurationName)
        {
            var fieldTransforms = GetOptionalAttribute(configuration, "fieldTransforms");
            FieldTransformsCollection filters = null;

            if (!string.IsNullOrEmpty(fieldTransforms))
            {
                filters = MagicTokenTransformer.GetFieldTransforms(fieldTransforms);
            }

            var presets = configuration.ChildNodes
                          .Cast <XmlNode>()
                          .Where(node => node.Name == "include")
                          .Select(x => CreateIncludeEntry(x, filters))
                          .ToList();

            var names = new HashSet <string>();

            foreach (var preset in presets)
            {
                if (!names.Contains(preset.Name))
                {
                    names.Add(preset.Name);
                    continue;
                }

                throw new InvalidOperationException($"Multiple predicate include nodes in configuration '{configurationName}' had the same name '{preset.Name}'. This is not allowed. Note that this can occur if you did not specify the name attribute and two include entries end in an item with the same name. Use the name attribute on the include tag to give a unique name.");
            }

            return(presets);
        }
Example #6
0
        protected virtual PresetTreeRoot CreateIncludeEntry(XmlNode configuration, FieldTransformsCollection predicateFieldFilterCollection)
        {
            string database = GetExpectedAttribute(configuration, "database");
            string path     = GetExpectedAttribute(configuration, "path");

            // ReSharper disable once PossibleNullReferenceException
            var    name      = configuration.Attributes["name"];
            string nameValue = name == null?path.Substring(path.LastIndexOf('/') + 1) : name.Value;

            var root = new PresetTreeRoot(nameValue, path, database);

            root.Exclusions = configuration.ChildNodes
                              .OfType <XmlElement>()
                              .Where(element => element.Name.Equals("exclude"))
                              .Select(excludeNode => CreateExcludeEntry(excludeNode, root))
                              .ToList();

            string fieldFilter = GetOptionalAttribute(configuration, "fieldTransforms");
            FieldTransformsCollection localFieldFilters = null;

            if (!string.IsNullOrEmpty(fieldFilter))
            {
                localFieldFilters = MagicTokenTransformer.GetFieldTransforms(fieldFilter);
            }

            FieldTransformsCollection finalFilters = null;

            if (localFieldFilters != null)
            {
                if (predicateFieldFilterCollection != null)
                {
                    finalFilters = predicateFieldFilterCollection.MergeFilters(localFieldFilters);
                }
                else
                {
                    finalFilters = localFieldFilters;
                }
            }
            else
            {
                finalFilters = predicateFieldFilterCollection;
            }

            if (finalFilters != null)
            {
                root.FieldValueManipulator = finalFilters;
            }

            return(root);
        }
Example #7
0
        public void FieldFilterParsingTests()
        {
            var fieldFilter  = "+Electronic Arts[${settings:Lootboxes}]";
            var ffCollection = MagicTokenTransformer.GetFieldTransforms(fieldFilter);

            Assert.NotNull(ffCollection.Transforms);
            Assert.Equal(1, ffCollection.Transforms.Length);

            fieldFilter  = "!Fisk,+Electronic Arts[${settings:Lootboxes}]";
            ffCollection = MagicTokenTransformer.GetFieldTransforms(fieldFilter);
            Assert.NotNull(ffCollection.Transforms);
            Assert.Equal(2, ffCollection.Transforms.Length);
            Assert.Equal("Fisk", ffCollection.Transforms[0].FieldName);
            Assert.Equal("Electronic Arts", ffCollection.Transforms[1].FieldName);

            fieldFilter  = "~Activision,+Electronic Arts[${settings:Lootboxes}],~Ubisoft";
            ffCollection = MagicTokenTransformer.GetFieldTransforms(fieldFilter);
            Assert.NotNull(ffCollection.Transforms);
            Assert.Equal(3, ffCollection.Transforms.Length);
            Assert.Equal("Activision", ffCollection.Transforms[0].FieldName);
            Assert.Equal("Electronic Arts", ffCollection.Transforms[1].FieldName);
            Assert.Equal("Ubisoft", ffCollection.Transforms[2].FieldName);

            fieldFilter = "~Activision,+Electronic Arts[${settings:Lootboxes},~Ubisoft";
            Assert.Throws(typeof(MalformedFieldFilterException), () => MagicTokenTransformer.GetFieldTransforms(fieldFilter));

            fieldFilter = "~Activision,+Electronic Arts,~Ubisoft";
            Assert.Throws(typeof(MalformedFieldFilterException), () => MagicTokenTransformer.GetFieldTransforms(fieldFilter));

            fieldFilter = "~Activision,+Electronic Arts],~Ubisoft";
            Assert.Throws(typeof(MalformedFieldFilterException), () => MagicTokenTransformer.GetFieldTransforms(fieldFilter));

            fieldFilter = "~Activision,!Electronic Arts,~Ubisoft,~Activision";
            Assert.Throws(typeof(DuplicateFieldsException), () => MagicTokenTransformer.GetFieldTransforms(fieldFilter));

            fieldFilter  = "   ~Activision,! Electronic Arts,  ~Ubisoft,  ~InterPlay      ";
            ffCollection = MagicTokenTransformer.GetFieldTransforms(fieldFilter);
            Assert.NotNull(ffCollection.Transforms);
            Assert.Equal(4, ffCollection.Transforms.Length);
            Assert.Equal("Activision", ffCollection.Transforms[0].FieldName);
            Assert.Equal("Electronic Arts", ffCollection.Transforms[1].FieldName);
            Assert.Equal("Ubisoft", ffCollection.Transforms[2].FieldName);
            Assert.Equal("InterPlay", ffCollection.Transforms[3].FieldName);
        }
Example #8
0
        public void ClearFieldFilterTests()
        {
            var fieldFilter  = "!Electronic Arts";
            var ffCollection = MagicTokenTransformer.GetFieldTransforms(fieldFilter);

            Assert.NotNull(ffCollection.Transforms);
            Assert.Equal(1, ffCollection.Transforms.Length);

            var ff = ffCollection.Transforms.First();

            Assert.Equal("Electronic Arts", ff.FieldName);

            Assert.True(ff.ShouldDeployFieldValue("P2W", "Lootboxes"), "ClearFilter did not want to deploy field (value,value)");
            Assert.True(ff.ShouldDeployFieldValue(null, "Lootboxes"), "ClearFilter did not want to deploy field (null, value)");
            Assert.True(ff.ShouldDeployFieldValue("P2W", null), "ClearFilter did not want to deploy field (value,null)");
            Assert.True(ff.ShouldDeployFieldValue(null, null), "ClearFilter did not want to deploy field (null, null)");

            Assert.Null(ff.GetFieldValue("P2W", "Lootboxes"));
            Assert.Null(ff.GetFieldValue(null, "Lootboxes"));
            Assert.Null(ff.GetFieldValue("P2W", null));
            Assert.Null(ff.GetFieldValue(null, null));
        }
Example #9
0
        public void OnlyIfNullOrEmptyFieldFilterTests()
        {
            var fieldFilter  = "?Electronic Arts";
            var ffCollection = MagicTokenTransformer.GetFieldTransforms(fieldFilter);

            Assert.NotNull(ffCollection.Transforms);
            Assert.Equal(1, ffCollection.Transforms.Length);

            var ff = ffCollection.Transforms.First();

            Assert.Equal("Electronic Arts", ff.FieldName);

            Assert.False(ff.ShouldDeployFieldValue("P2W", "Lootboxes"), "OnlyIfNullOrEmptyFilter wanted to deploy field (value,value)");
            Assert.True(ff.ShouldDeployFieldValue(null, "Lootboxes"), "OnlyIfNullOrEmptyFilter did not want to deploy field (null, value)");
            Assert.False(ff.ShouldDeployFieldValue("P2W", null), "OnlyIfNullOrEmptyFilter wanted to deploy field (value,null)");
            Assert.True(ff.ShouldDeployFieldValue(null, null), "OnlyIfNullOrEmptyFilter did not want to deploy field (null, null)");

            Assert.Throws(typeof(InvalidOperationException), () => ff.GetFieldValue("P2W", "Lootboxes"));
            Assert.Equal("Lootboxes", ff.GetFieldValue(null, "Lootboxes"));
            Assert.Throws(typeof(InvalidOperationException), () => ff.GetFieldValue("P2W", null));
            Assert.Null(ff.GetFieldValue(null, null));
        }
Example #10
0
        public void LoremIpsumBodyFilterTests()
        {
            var fieldFilter  = ":Electronic Arts";
            var ffCollection = MagicTokenTransformer.GetFieldTransforms(fieldFilter);

            Assert.NotNull(ffCollection.Transforms);
            Assert.Equal(1, ffCollection.Transforms.Length);

            var ff = ffCollection.Transforms.First();

            Assert.Equal("Electronic Arts", ff.FieldName);

            // LoremIpsumBody always forces a value (duh), it should always deploy
            Assert.True(ff.ShouldDeployFieldValue("P2W", "Lootboxes"), "LoremIpsumBodyFilterTests did not want to deploy field (value,value)");
            Assert.True(ff.ShouldDeployFieldValue(null, "Lootboxes"), "LoremIpsumBodyFilterTests did not want to deploy field (null, value)");
            Assert.True(ff.ShouldDeployFieldValue("P2W", null), "LoremIpsumBodyFilterTests did not want to deploy field (value,null)");
            Assert.True(ff.ShouldDeployFieldValue(null, null), "LoremIpsumBodyFilterTests did not want to deploy field (null, null)");

            Assert.Equal(MagicTokenTransformer.LoremIpsumBody, ff.GetFieldValue("P2W", "Lootboxes"));
            Assert.Equal(MagicTokenTransformer.LoremIpsumBody, ff.GetFieldValue(null, "Lootboxes"));
            Assert.Equal(MagicTokenTransformer.LoremIpsumBody, ff.GetFieldValue("P2W", null));
            Assert.Equal(MagicTokenTransformer.LoremIpsumBody, ff.GetFieldValue(null, null));
        }
Example #11
0
        public void ExcludeFieldFilterTests()
        {
            var fieldFilter  = "-Electronic Arts";
            var ffCollection = MagicTokenTransformer.GetFieldTransforms(fieldFilter);

            Assert.NotNull(ffCollection.Transforms);
            Assert.Equal(1, ffCollection.Transforms.Length);

            var ff = ffCollection.Transforms.First();

            Assert.Equal("Electronic Arts", ff.FieldName);

            Assert.False(ff.ShouldDeployFieldValue("P2W", "Lootboxes"), "ExcludeFilter wanted to deploy field (value,value)");
            Assert.False(ff.ShouldDeployFieldValue(null, "Lootboxes"), "ExcludeFilter wanted to deploy field (null, value)");
            Assert.False(ff.ShouldDeployFieldValue("P2W", null), "ExcludeFilter wanted to deploy field (value,null)");
            Assert.False(ff.ShouldDeployFieldValue(null, null), "ExcludeFilter wanted to deploy field (null, null)");

            // Calling GetResult on an explicitly ignored field should unconditionally fail as all calls to ShouldDeployFieldValue() would have returned false
            Assert.Throws(typeof(InvalidOperationException), () => ff.GetFieldValue("P2W", "Lootboxes"));
            Assert.Throws(typeof(InvalidOperationException), () => ff.GetFieldValue("P2W", null));
            Assert.Throws(typeof(InvalidOperationException), () => ff.GetFieldValue(null, "Lootboxes"));
            Assert.Throws(typeof(InvalidOperationException), () => ff.GetFieldValue(null, null));
        }
        private IList <PresetTreeRoot> ParsePreset(XmlNode configuration, string configurationName)
        {
            var fieldTransforms = GetOptionalAttribute(configuration, "fieldTransforms");
            FieldTransformsCollection filters = null;

            if (!string.IsNullOrEmpty(fieldTransforms))
            {
                filters = MagicTokenTransformer.GetFieldTransforms(fieldTransforms);
            }

            List <XmlNode> predicatePresetNodes = new List <XmlNode>();

            if (_predicatePresetHandler != null)
            {
                var predicatePresetElements = configuration.ChildNodes
                                              .Cast <XmlNode>()
                                              .Where(node => node.Name == "preset")
                                              .Cast <XmlElement>()
                                              .ToList();

                foreach (var element in predicatePresetElements)
                {
                    var id = element.Attributes["id"]?.Value;
                    var predicatePreset = _predicatePresetHandler.GetPresetById(id);
                    if (predicatePreset == null)
                    {
                        if (string.IsNullOrWhiteSpace(id))
                        {
                            id = "blank/missing";
                        }

                        throw new InvalidOperationException($"Configuration '{configurationName}' referenced a Predicate Preset with id '{id}' that could not be located.");
                    }

                    predicatePresetNodes.AddRange(predicatePreset.ApplyPreset(element));
                }
            }

            var predicatePresets = predicatePresetNodes
                                   .Select(x => CreateIncludeEntry(x, filters))
                                   .ToList();

            var includePresets = configuration.ChildNodes
                                 .Cast <XmlNode>()
                                 .Where(node => node.Name == "include")
                                 .Select(x => CreateIncludeEntry(x, filters))
                                 .ToList();

            includePresets.AddRange(predicatePresets);

            var names = new HashSet <string>();

            foreach (var preset in includePresets)
            {
                if (!names.Contains(preset.Name))
                {
                    names.Add(preset.Name);
                    continue;
                }

                throw new InvalidOperationException($"Multiple predicate include nodes in configuration '{configurationName}' had the same name '{preset.Name}'. This is not allowed. Note that this can occur if you did not specify the name attribute and two include entries end in an item with the same name. Use the name attribute on the include tag to give a unique name.");
            }

            return(includePresets);
        }