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
            });

            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());
        }
Exemplo n.º 2
0
        public void MapToProperty_EnforceTemplate_OneObjectAreCreated()
        {
            //Assign


            ID templateId = ID.NewID;
            ID childId    = ID.NewID;

            using (Db database = new Db
            {
                new DbTemplate(templateId)
                {
                },
                new Sitecore.FakeDb.DbItem("TestItem")
                {
                    new Sitecore.FakeDb.DbItem("Child1", childId, templateId),
                    new Sitecore.FakeDb.DbItem("Child2"),
                    new Sitecore.FakeDb.DbItem("Child3")
                }
            })
            {
                var item    = database.GetItem("/sitecore/content/TestItem");
                var mapper  = new SitecoreChildrenMapper();
                var options = new GetItemOptionsParams();

                var config = new SitecoreChildrenConfiguration();
                config.InferType       = false;
                config.PropertyInfo    = typeof(Stub).GetProperty("Children");
                config.TemplateId      = templateId;
                config.EnforceTemplate = SitecoreEnforceTemplate.TemplateAndBase;

                var scContext = Context.Create(Utilities.CreateStandardResolver());

                var service = new SitecoreService(database.Database, scContext);


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

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

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

                //Assert

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

                Assert.AreEqual(childId, result.First().Id);
            }
        }
        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));
                }
            }
        }
Exemplo n.º 4
0
        public void MapToProperty_ItemHasThreeChildren_ThreeObjectAreCreated()
        {
            //Assign
            using (new EventDisabler())
                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 options = new GetItemOptionsParams();

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

                    var service = Substitute.For <ISitecoreService>();

                    service.GetItems(Arg.Any <GetItemsByFuncOptions>()).Returns(info => ((GetItemsByFuncOptions)info.Args()[0]).ItemsFunc(item.Database).Select(child => new StubChild {
                        Id = child.ID
                    }));

                    service.Config.Returns(new Config());

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

                    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));
                    }
                }
        }
Exemplo n.º 5
0
        public void MapToProperty_ItemHasNoChildren_NoObjectsCreated()
        {
            //Assign
            using (Db database = new Db
            {
                new Sitecore.FakeDb.DbItem("TestItem")
                {
                }
            })
            {
                var item    = database.GetItem("/sitecore/content/TestItem");
                var mapper  = new SitecoreChildrenMapper();
                var options = new GetItemOptionsParams();

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

                var scContext = Context.Create(Utilities.CreateStandardResolver());

                var service = new SitecoreService(database.Database, scContext);

                service.Config = new Config();

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

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

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

                //Assert

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