Class SitecoreChildrenConfiguration
Inheritance: Glass.Mapper.Configuration.ChildrenConfiguration
        public void MapToProperty_ItemHasThreeChildren_ThreeObjectAreCreated()
        {
            //Assign
            using (Db database = new Db
            {
                new Sitecore.FakeDb.DbItem("TestItem")
                {
                    new Sitecore.FakeDb.DbItem("Child1"),
                    new Sitecore.FakeDb.DbItem("Child2"),
                    new Sitecore.FakeDb.DbItem("Child3")

                }
            })
            {

                var item = database.GetItem("/sitecore/content/TestItem");
                var mapper = new SitecoreChildrenMapper();

                var config = new SitecoreChildrenConfiguration();
                config.InferType = false;
                config.IsLazy = false;
                config.PropertyInfo = typeof(Stub).GetProperty("Children");

                var service = Substitute.For<ISitecoreService>();
                var predicate = Arg.Is<Item>(x => item.Children.Any(y => x.ID == y.ID));

                //ME - Although this looks correct I am not sure it is
                service.CreateType(typeof(StubChild), predicate, false, false, null)
                    .ReturnsForAnyArgs(info => new StubChild()
                    {
                        Id = info.Arg<Item>().ID
                    });
                service.Config.Returns(new Config());

                var context = new SitecoreDataMappingContext(null, item, service);

                mapper.Setup(new DataMapperResolverArgs(null, config));

                //Act
                var result = mapper.MapToProperty(context) as IEnumerable<StubChild>;

                //Assert

                Assert.AreEqual(item.Children.Count, result.Count());

                foreach (Item child in item.Children)
                {
                    Assert.IsTrue(result.Any(x => x.Id == child.ID));
                }
            }

        }
        /// <summary>
        /// Called to map each property automatically
        /// </summary>
        /// <param name="property"></param>
        /// <returns></returns>
        protected override AbstractPropertyConfiguration AutoMapProperty(System.Reflection.PropertyInfo property)
        {
            string           name = property.Name;
            SitecoreInfoType infoType;

            if (name.Contains(".")) //explicit interface implementation
            {
                name = name.Split('.', StringSplitOptions.RemoveEmptyEntries).Last();
            }

            if (name.ToLowerInvariant() == "id")
            {
                var idConfig = new SitecoreIdConfiguration();
                idConfig.PropertyInfo = property;
                return(idConfig);
            }

            if (name.ToLowerInvariant() == "parent")
            {
                var parentConfig = new SitecoreParentConfiguration();
                parentConfig.PropertyInfo = property;
                return(parentConfig);
            }
            if (name.ToLowerInvariant() == "children")
            {
                var childrenConfig = new SitecoreChildrenConfiguration();
                childrenConfig.PropertyInfo = property;
                return(childrenConfig);
            }
            if (name.ToLowerInvariant() == "item" && property.PropertyType == typeof(Item))
            {
                var itemConfig = new SitecoreItemConfiguration();
                itemConfig.PropertyInfo = property;
                return(itemConfig);
            }

            if (Enum.TryParse(name, true, out infoType))
            {
                SitecoreInfoConfiguration infoConfig = new SitecoreInfoConfiguration();
                infoConfig.PropertyInfo = property;
                infoConfig.Type         = infoType;
                return(infoConfig);
            }

            SitecoreFieldConfiguration fieldConfig = new SitecoreFieldConfiguration();

            fieldConfig.FieldName    = name;
            fieldConfig.PropertyInfo = property;
            return(fieldConfig);
        }
示例#3
0
        /// <summary>
        /// Called to map each property automatically
        /// </summary>
        /// <param name="property"></param>
        /// <returns></returns>
        protected override AbstractPropertyConfiguration AutoMapProperty(System.Reflection.PropertyInfo property)
        {
            string           name = property.Name;
            SitecoreInfoType infoType;

            if (name.ToLowerInvariant() == "id")
            {
                var idConfig = new SitecoreIdConfiguration();
                idConfig.PropertyInfo = property;
                return(idConfig);
            }

            if (name.ToLowerInvariant() == "parent")
            {
                var parentConfig = new SitecoreParentConfiguration();
                parentConfig.PropertyInfo = property;
                return(parentConfig);
            }
            if (name.ToLowerInvariant() == "children")
            {
                var childrenConfig = new SitecoreChildrenConfiguration();
                childrenConfig.PropertyInfo = property;
                return(childrenConfig);
            }
            if (name.ToLowerInvariant() == "item" && property.PropertyType == typeof(Item))
            {
                var itemConfig = new SitecoreItemConfiguration();
                itemConfig.PropertyInfo = property;
                return(itemConfig);
            }

            if (Enum.TryParse(name, true, out infoType))
            {
                SitecoreInfoConfiguration infoConfig = new SitecoreInfoConfiguration();
                infoConfig.PropertyInfo = property;
                infoConfig.Type         = infoType;
                return(infoConfig);
            }

            SitecoreFieldConfiguration fieldConfig = new SitecoreFieldConfiguration();

            fieldConfig.FieldName    = name;
            fieldConfig.PropertyInfo = property;
            return(fieldConfig);
        }
        public void MapToProperty_ItemHasNoChildren_NoObjectsCreated()
        {
            //Assign
            var database = Sitecore.Configuration.Factory.GetDatabase("master");

            var item = database.GetItem("/sitecore/content/Tests/DataMappers/SitecoreChildrenMapper/Parent/Child1");
            var mapper = new SitecoreChildrenMapper();

            var config = new SitecoreChildrenConfiguration();
            config.InferType = false;
            config.IsLazy = false;
            config.PropertyInfo = typeof(Stub).GetProperty("Children");

            var service = Substitute.For<ISitecoreService>();
            var predicate = Arg.Is<Item>(x => item.Children.Any(y => x.ID == y.ID));

            //ME - Although this looks correct I am not sure it is
            service.CreateType(typeof(StubChild), predicate, false, false, null).ReturnsForAnyArgs(info => new StubChild()
            {
                Id = info.Arg<Item>().ID
            });
            service.Config = new Config();

            var context = new SitecoreDataMappingContext(null, item, service);

            mapper.Setup(new DataMapperResolverArgs(null,config));

            //Act
            var result = mapper.MapToProperty(context) as IEnumerable<StubChild>;

            //Assert

            Assert.AreEqual(0, result.Count());

          

        }
        public void CanHandle_ConfigIsChildren_ReturnsTrue()
        {
            //Assign
            var mapper = new SitecoreChildrenMapper();
            var config = new SitecoreChildrenConfiguration();

            //Act
            var result = mapper.CanHandle(config, null);

            //Assert
            Assert.IsTrue(result);
        }
        protected override AbstractPropertyConfiguration AutoMapProperty(System.Reflection.PropertyInfo property)
        {
            string name = property.Name;
            SitecoreInfoType infoType;

            if (name.ToLowerInvariant() == "id")
            {
                SitecoreIdConfiguration idConfig = new SitecoreIdConfiguration();
                idConfig.PropertyInfo = property;
                return idConfig;
            }

            if (name.ToLowerInvariant() == "parent")
            {
                SitecoreParentConfiguration parentConfig = new SitecoreParentConfiguration();
                parentConfig.PropertyInfo = property;
                return parentConfig;
            }
            if (name.ToLowerInvariant() == "children")
            {
                SitecoreChildrenConfiguration childrenConfig = new SitecoreChildrenConfiguration();
                childrenConfig.PropertyInfo = property;
                return childrenConfig;
            }

            if (Enum.TryParse(name, true, out infoType))
            {
                SitecoreInfoConfiguration infoConfig = new SitecoreInfoConfiguration();
                infoConfig.PropertyInfo = property;
                infoConfig.Type = infoType;
                return infoConfig;
            }

            SitecoreFieldConfiguration fieldConfig = new SitecoreFieldConfiguration();
            fieldConfig.FieldName = name;
            fieldConfig.PropertyInfo = property;
            return fieldConfig;
        }