Class SitecoreLinkedMapper
Наследование: AbstractDataMapper
        public void ReadOnly_ReturnsTrue()
        {
            //Assign
            var mapper = new SitecoreLinkedMapper();

            //Act
            var result = mapper.ReadOnly;

            //Assert
            Assert.IsTrue(result);

        }
        public void CanHandle_IsEnumerableOfNotMappedClassWithLinkedConfig_ReturnsTrueOnDemand()
        {
            //Assign
            var config = new SitecoreLinkedConfiguration();
            config.PropertyInfo = new FakePropertyInfo(typeof(IEnumerable<StubNotMapped>));
            var mapper = new SitecoreLinkedMapper();
            var context = Context.Create(Utilities.CreateStandardResolver());
            context.Load(new SitecoreAttributeConfigurationLoader("Glass.Mapper.Sc.Integration"));


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

            //Assert
            Assert.IsTrue(result);

        }
        public void CanHandle_IsEnumerableOfMappedClassWithLinkedConfig_ReturnsTrue()
        {
            //Assign
            var config = new SitecoreLinkedConfiguration();
            config.PropertyInfo = typeof(StubClass).GetProperty("StubMappeds");

            var mapper = new SitecoreLinkedMapper();
            var context = Context.Create(Utilities.CreateStandardResolver());
            context.Load(new OnDemandLoader<SitecoreTypeConfiguration>(typeof(StubClass)));



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

            //Assert
            Assert.IsTrue(result);

        }
        public void CanHandle_IsListOfMappedClassWithLinkedConfig_ReturnsFalse()
        {
            //Assign
            var config = new SitecoreLinkedConfiguration();
            config.PropertyInfo = typeof (StubClass).GetProperty("StubMappedsList");

            var mapper = new SitecoreLinkedMapper();
            var context = Context.Create(Utilities.CreateStandardResolver());
            context.Load(new SitecoreAttributeConfigurationLoader("Glass.Mapper.Sc.Integration"));


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

            //Assert
            Assert.IsFalse(result);

        }
        public void MapToProperty_GetAll_ReferrersListReturned()
        {
            //Assign
            var item = Database.GetItem("/sitecore/content/Tests/DataMappers/SitecoreLinkedMapper/Target");
            var language = LanguageManager.GetLanguage("af-ZA");

            var source = Database.GetItem("/sitecore/content/Tests/DataMappers/SitecoreLinkedMapper/Source",
                                             language);

            using (new ItemEditing(source, true))
            {
                source[FieldName] = "<a href=\"~/link.aspx?_id=216C0015-8626-4951-9730-85BCA34EC2A3&amp;_z=z\">Source</a>";
            }
        

        //ME - when getting templates you have to disable the role manager
            using (new SecurityDisabler())
            {

                var template = Database.GetItem("/sitecore/templates/Tests/DataMappers/DataMappersSingleField");
               

                var config = new SitecoreLinkedConfiguration();
                config.PropertyInfo = typeof (StubClass).GetProperty("StubMappeds");
                config.Option = SitecoreLinkedOptions.All;

                var context = Context.Create(Utilities.CreateStandardResolver());
                context.Load(new SitecoreAttributeConfigurationLoader("Glass.Mapper.Sc.Integration"));

                var mapper = new SitecoreLinkedMapper();
                mapper.Setup(new DataMapperResolverArgs(context, config));

                var service = new SitecoreService(Database, context);

                //Act
                var result =
                    mapper.MapToProperty(new SitecoreDataMappingContext(null, item, service)) as IEnumerable<StubMapped>;

                //Assert
                Assert.AreEqual(2, result.Count());
                Assert.AreEqual(template.ID.Guid, result.First().Id);
                Assert.AreEqual(source.ID.Guid, result.Skip(1).First().Id);
                Assert.AreEqual(source.Language, result.Skip(1).First().Language);
            }
        }
        public void MapToProperty_GetAllReferences_ReferrersListReturned()
        {
            //Act
            var item = Database.GetItem("/sitecore/content/Tests/DataMappers/SitecoreLinkedMapper/Target");
           
            //ME - when getting templates you have to disable the role manager
            using (new SecurityDisabler())
            {
                var template = Database.GetItem("/sitecore/templates/Tests/DataMappers/DataMappersSingleField");
         
            var config = new SitecoreLinkedConfiguration();
                config.PropertyInfo = typeof (StubClass).GetProperty("StubMappeds");
            config.Option = SitecoreLinkedOptions.References;

            var context = Context.Create(Utilities.CreateStandardResolver());
            context.Load(new SitecoreAttributeConfigurationLoader("Glass.Mapper.Sc.Integration"));

            var mapper = new SitecoreLinkedMapper();
            mapper.Setup(new DataMapperResolverArgs(context, config));

            var service = new SitecoreService(Database, context);

            //Act
            var result =
                mapper.MapToProperty(new SitecoreDataMappingContext(null, item, service)) as IEnumerable<StubMapped>;

            //Assert
            Assert.AreEqual(1, result.Count());
            Assert.AreEqual(template.ID.Guid, result.First().Id);
            }
        }
        public void MapToProperty_GetAll_ReferrersListReturned()
        {
            //Assign

            var templateId = ID.NewID;
            var fieldId = ID.NewID;
            var targetId = ID.NewID;
            var language = LanguageManager.GetLanguage("af-ZA");

            using (Db database = new Db
            {
                new DbTemplate(templateId),
                new Sitecore.FakeDb.DbItem("Target", targetId, templateId),
                new Sitecore.FakeDb.DbItem("Source", ID.NewID, templateId)
                {
                    new DbField("Field", fieldId)
                    {
                        {language.Name, 1, targetId.ToString()}
                    }
                }
            })
            {


                var target = database.GetItem("/sitecore/content/Target");
                var source = database.Database.GetItem("/sitecore/content/Source", language);
                var template = database.Database.GetTemplate(templateId);

                var behavior = Substitute.For<Sitecore.Links.LinkDatabase>();

                behavior.GetReferences(target).Returns(new[]
                {
                    new Sitecore.Links.ItemLink(target, fieldId, template, template.InnerItem.Paths.FullPath),
                });
                behavior.GetReferrers(target).Returns(new[]
                {
                    new Sitecore.Links.ItemLink(source, fieldId, target, target.Paths.FullPath),
                });


                using (new Sitecore.FakeDb.Links.LinkDatabaseSwitcher(behavior))
                {
                    //ME - when getting templates you have to disable the role manager
                    using (new SecurityDisabler())
                    {

                        var config = new SitecoreLinkedConfiguration();
                        config.PropertyInfo = typeof(StubClass).GetProperty("StubMappeds");
                        config.Option = SitecoreLinkedOptions.All;

                        var context = Context.Create(Utilities.CreateStandardResolver());
                        context.Load(new OnDemandLoader<SitecoreTypeConfiguration>(typeof(StubMapped)));

                        var mapper = new SitecoreLinkedMapper();
                        mapper.Setup(new DataMapperResolverArgs(context, config));

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

                        //Act
                        var result =
                            mapper.MapToProperty(new SitecoreDataMappingContext(null, target, service)) as
                                IEnumerable<StubMapped>;

                        //Assert
                        Assert.AreEqual(2, result.Count());
                        Assert.AreEqual(template.ID.Guid, result.First().Id);
                        Assert.AreEqual(source.ID.Guid, result.Skip(1).First().Id);
                        Assert.AreEqual(source.Language, result.Skip(1).First().Language);
                    }
                }
            }
        }