MapToProperty() public method

Maps data from the CMS value to the .Net property value
SitecoreInfoType {0} not supported.Formatted(scConfig.Type)
public MapToProperty ( AbstractDataMappingContext mappingContext ) : object
mappingContext AbstractDataMappingContext The mapping context.
return object
        public void MapToProperty_SitecoreInfoTypeBaseTemplateIds_ReturnsBaseTemplateIds()
        {
            //Assign
            ID templateId1 = ID.NewID;
            ID templateId2 = ID.NewID;

            using (Db database = new Db
            {
                new Sitecore.FakeDb.DbTemplate("TestTemplate1", templateId1),
                new Sitecore.FakeDb.DbTemplate("TestTemplate2", templateId2)
                {
                    new FakeDbField(new ID("{12C33F3F-86C5-43A5-AEB4-5598CEC45116}"), templateId1.ToString())
                },
                new Sitecore.FakeDb.DbItem("TestItem",ID.NewID, templateId2)
            })
            {
                var type = SitecoreInfoType.BaseTemplateIds;

                var mapper = new SitecoreInfoMapper();
                var config = new SitecoreInfoConfiguration();
                config.Type = type;
                config.PropertyInfo = typeof(Stub).GetProperty("BaseTemplateIds");

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

                var item = database.GetItem("/sitecore/Content/TestItem");

                Assert.IsNotNull(item, "Item is null, check in Sitecore that item exists");
                var dataContext = new SitecoreDataMappingContext(null, item, null);

                //Act
                IEnumerable<ID> results;
                using (new SecurityDisabler())
                {

                    results = mapper.MapToProperty(dataContext) as IEnumerable<ID>;
                }

                //Assert
                Assert.Greater(results.Count(), 1);
                Assert.IsTrue(results.All(x => x != item.TemplateID));
            }
        }
        public void MapToProperty_SitecoreInfoType_GetsExpectedValueFromSitecore(
            SitecoreInfoType type, object expected
            )
        {
            //Assign
            var mapper = new SitecoreInfoMapper();
            var config = new SitecoreInfoConfiguration();
            config.Type = type;
            mapper.Setup(new DataMapperResolverArgs(null, config));

            Sitecore.Context.Site = null;
            var templateId = ID.NewID;
            var itemId = new ID("031501A9C7F24596BD659276DA3A627A");

            using (Db database = new Db
            {
                new Sitecore.FakeDb.DbTemplate("TestTemplate", templateId),
                new Sitecore.FakeDb.DbItem("TestItem", itemId)
                {
                    Fields = { new FakeDbField(new ID("{B5E02AD9-D56F-4C41-A065-A133DB87BDEB}"), "DataMappersEmptyItem DisplayName")},
                    TemplateID = templateId,
                }
            })
            {
                Sitecore.Resources.Media.MediaProvider mediaProvider = Substitute.For<Sitecore.Resources.Media.MediaProvider>();
                mediaProvider
                      .GetMediaUrl(Arg.Is<Sitecore.Data.Items.MediaItem>(i => i.ID == itemId), Arg.Any<MediaUrlOptions>())
                      .Returns("/~/media/Test.ashx");

                using (new Sitecore.FakeDb.Resources.Media.MediaProviderSwitcher(mediaProvider))
                {
                    var item = database.GetItem("/sitecore/content/TestItem");
                    Assert.IsNotNull(item, "Item is null, check in Sitecore that item exists");
                    var dataContext = new SitecoreDataMappingContext(null, item, null);

                    //Act
                    var value = mapper.MapToProperty(dataContext);

                    //Assert
                    Assert.AreEqual(expected, value);
                }
            }
        }
        public void MapToProperty_SitecoreInfoTypeItemuri_ReturnsFullItemUri()
        {
            //Assign
            using (Db database = new Db
            {
                new Sitecore.FakeDb.DbItem("TestItem")
            })
            {
                var type = SitecoreInfoType.ItemUri;

                var mapper = new SitecoreInfoMapper();
                var config = new SitecoreInfoConfiguration();
                config.Type = type;
                config.PropertyInfo = new FakePropertyInfo(typeof(string), "StringField", typeof(Stub));
                mapper.Setup(new DataMapperResolverArgs(null, config));

                var item = database.GetItem("/sitecore/Content/TestItem");

                Assert.IsNotNull(item, "Item is null, check in Sitecore that item exists");
                var dataContext = new SitecoreDataMappingContext(null, item, null);

                //Act
                var value = mapper.MapToProperty(dataContext) as ItemUri;

                //Assert
                Assert.AreEqual(item.ID, value.ItemID);
                Assert.AreEqual(item.Language, value.Language);
                Assert.AreEqual(item.Database.Name, value.DatabaseName);
                Assert.AreEqual(item.Version, value.Version);
            }
        }
        public void MapToProperty_SitecoreInfoTypeTemplateId_ReturnsTemplateIdAsID()
        {
            //Assign

            using (Db database = new Db
            {
                new Sitecore.FakeDb.DbItem("TestItem")
            })
            {
                var type = SitecoreInfoType.TemplateId;

                var mapper = new SitecoreInfoMapper();
                var config = new SitecoreInfoConfiguration();
                config.Type = type;
                config.PropertyInfo = typeof(Stub).GetProperty("TemplateId");

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

                var item = database.GetItem("/sitecore/Content/TestItem");
                var expected = item.TemplateID;

                Assert.IsNotNull(item, "Item is null, check in Sitecore that item exists");
                var dataContext = new SitecoreDataMappingContext(null, item, null);

                //Act
                var value = mapper.MapToProperty(dataContext);

                //Assert
                Assert.AreEqual(expected, value);
            }
        }
        public void MapToProperty_GetUrlInItemLanguage_ReturnsUrlWithFr()
        {
            //Assign
            var type = SitecoreInfoType.Url;
            string expected = "/fr-FR/sitecore/content/TestItem.aspx";

            var mapper = new SitecoreInfoMapper();
            var config = new SitecoreInfoConfiguration();
            config.Type = type;
            config.UrlOptions = SitecoreInfoUrlOptions.UseItemLanguage;
            mapper.Setup(new DataMapperResolverArgs(null, config));

            Sitecore.Context.Site = null;
            var templateId = ID.NewID;
            var itemId = new ID("031501A9C7F24596BD659276DA3A627A");

            using (Db database = new Db
            {
                new Sitecore.FakeDb.DbItem("TestItem", itemId)
                {
                    Fields =
                    {
                        new DbField("title")
                        {
                            {"fr-fr", 1, "test"}
                        }
                    },
                }
            })
            {

                var item = database.GetItem("/sitecore/content/TestItem", "fr-fr");
                Assert.IsNotNull(item, "Item is null, check in Sitecore that item exists");
                var dataContext = new SitecoreDataMappingContext(null, item, null);

                //Act
                var value = mapper.MapToProperty(dataContext);

                //Assert
                Assert.AreEqual(expected, value);
            }
        }
        public void MapToProperty_MediaUrlWithFlag_ReturnsModifiedUrl(
            SitecoreInfoMediaUrlOptions option
            
            )
        {
            //Assign
            var mapper = new SitecoreInfoMapper();
            var config = new SitecoreInfoConfiguration();
            config.Type = SitecoreInfoType.MediaUrl;
            config.MediaUrlOptions = option;
            mapper.Setup(new DataMapperResolverArgs(null, config));
            var itemId = new ID("031501A9C7F24596BD659276DA3A627A");
            string expected = "media url";

            Sitecore.Context.Site = null;

            using (Db database = new Db
            {
                new Sitecore.FakeDb.DbItem("TestItem", itemId)
                {
                    Fields =
                    {
                        new FakeDbField(new ID("{B5E02AD9-D56F-4C41-A065-A133DB87BDEB}"),
                            "DataMappersEmptyItem DisplayName")
                    },
                }
            })
            {

                Func<MediaUrlOptions, bool> pred = x =>
                {
                    switch (option)
                    {
                        case SitecoreInfoMediaUrlOptions.Default:
                            return true;
                        case SitecoreInfoMediaUrlOptions.RemoveExtension:
                            return x.IncludeExtension == false;
                        case SitecoreInfoMediaUrlOptions.LowercaseUrls:
                            return x.LowercaseUrls == true;
                        default:
                            return false;
                    }
                };

                Sitecore.Resources.Media.MediaProvider mediaProvider =
                    Substitute.For<Sitecore.Resources.Media.MediaProvider>();
                mediaProvider
                    .GetMediaUrl(
                        Arg.Is<Sitecore.Data.Items.MediaItem>(i => i.ID == itemId),
                        Arg.Is<MediaUrlOptions>(x => pred(x))
                    )
                    .Returns(expected);

                using (new Sitecore.FakeDb.Resources.Media.MediaProviderSwitcher(mediaProvider))
                {
                    var item = database.GetItem("/sitecore/content/TestItem");
                    Assert.IsNotNull(item, "Item is null, check in Sitecore that item exists");
                    var dataContext = new SitecoreDataMappingContext(null, item, null);

                    //Act
                    var value = mapper.MapToProperty(dataContext);

                    //Assert
                    Assert.AreEqual(expected, value);
                }
            }
        }
        public void MapToProperty_SitecoreInfoTypeNotSet_ThrowsException()
        {
            //Assign
            SitecoreInfoType type = SitecoreInfoType.NotSet;

            var mapper = new SitecoreInfoMapper();
            var config = new SitecoreInfoConfiguration();
            config.Type = type;
            mapper.Setup(new DataMapperResolverArgs(null,config));

            var item = _db.GetItem("/sitecore/Content/Tests/DataMappers/SitecoreInfoMapper/DataMappersEmptyItem");

            Assert.IsNotNull(item, "Item is null, check in Sitecore that item exists");
            var dataContext = new SitecoreDataMappingContext(null, item, null);

            //Act
            var value = mapper.MapToProperty(dataContext);

            //Assert
            //No asserts expect exception
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Provides the implementation for operations that get member values. Classes derived from the <see cref="T:System.Dynamic.DynamicObject" /> class can override this method to specify dynamic behavior for operations such as getting a value for a property.
        /// </summary>
        /// <param name="binder">Provides information about the object that called the dynamic operation. The binder.Name property provides the name of the member on which the dynamic operation is performed. For example, for the Console.WriteLine(sampleObject.SampleProperty) statement, where sampleObject is an instance of the class derived from the <see cref="T:System.Dynamic.DynamicObject" /> class, binder.Name returns "SampleProperty". The binder.IgnoreCase property specifies whether the member name is case-sensitive.</param>
        /// <param name="result">The result of the get operation. For example, if the method is called for a property, you can assign the property value to <paramref name="result" />.</param>
        /// <returns>true if the operation is successful; otherwise, false. If this method returns false, the run-time binder of the language determines the behavior. (In most cases, a run-time exception is thrown.)</returns>
        /// <exception cref="System.NotSupportedException">No field of Sitecore info matches the name {0} for item {1}.Formatted(name, _item.Paths.FullPath)</exception>
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            result = null;

            string name = binder.Name;

            if (name == "Id")
            {
                result = _item.ID;
                return true;
            }


            if (_item.Fields[name] != null)
            {
                result = GetField(name, _item);
               return true;
            }

            SitecoreInfoType infoType;

            if (Enum.TryParse<SitecoreInfoType>(name, out infoType))
            {
                var mapper = new SitecoreInfoMapper();
                var config = new SitecoreInfoConfiguration();
                config.Type = infoType;

                mapper.Setup(new DataMapperResolverArgs(null, config ));
                result = mapper.MapToProperty(new SitecoreDataMappingContext(null, _item, null));
                return true;
            }

           

            switch (name)
            {
                case "Parent":
                    result = CreateNew(_item.Parent);
                    break;
                case "Children":
                    result = new DynamicCollection<DynamicItem>(_item.Children.Select(x => CreateNew(x)).ToArray());
                    break;
            }
            if (result != null) return true;
            
            throw new NotSupportedException("No field of Sitecore info matches the name {0} for item {1}".Formatted(name, _item.Paths.FullPath));

        }
        public void MapToProperty_SitecoreInfoType_GetsExpectedValueFromSitecore(
            [Values(
                SitecoreInfoType.ContentPath,
                SitecoreInfoType.DisplayName,
                SitecoreInfoType.FullPath,
                SitecoreInfoType.Key,
                SitecoreInfoType.MediaUrl,
                SitecoreInfoType.Name,
                SitecoreInfoType.Path,
                SitecoreInfoType.TemplateName,
                SitecoreInfoType.Url,
                SitecoreInfoType.Version
                )] SitecoreInfoType type,
            [Values(
                "/Tests/DataMappers/SitecoreInfoMapper/DataMappersEmptyItem", //content path
                "DataMappersEmptyItem DisplayName", //DisplayName
                "/sitecore/content/Tests/DataMappers/SitecoreInfoMapper/DataMappersEmptyItem", //FullPath
                "datamappersemptyitem", //Key
                "/~/media/031501A9C7F24596BD659276DA3A627A.ashx", //MediaUrl
                "DataMappersEmptyItem", //Name
                "/sitecore/content/Tests/DataMappers/SitecoreInfoMapper/DataMappersEmptyItem", //Path
                "DataMappersEmptyItem", //TemplateName
                "/en/sitecore/content/Tests/DataMappers/SitecoreInfoMapper/DataMappersEmptyItem.aspx", //Url
                1 //version
                )] object expected
            )
        {
            //Assign
            var mapper = new SitecoreInfoMapper();
            var config = new SitecoreInfoConfiguration();
            config.Type = type;
            mapper.Setup(new DataMapperResolverArgs(null,config));

            Sitecore.Context.Site = null;

            var item = _db.GetItem("/sitecore/Content/Tests/DataMappers/SitecoreInfoMapper/DataMappersEmptyItem");

            Assert.IsNotNull(item, "Item is null, check in Sitecore that item exists");
            var dataContext = new SitecoreDataMappingContext(null, item, null);

            //Act
            var value = mapper.MapToProperty(dataContext);

            //Assert
            Assert.AreEqual(expected, value);
        }
        public void MapToProperty_SitecoreInfoTypeTemplateId_ReturnsTemplateIdAsGuid()
        {
            //Assign
            var type = SitecoreInfoType.TemplateId;

            var mapper = new SitecoreInfoMapper();
            var config = new SitecoreInfoConfiguration();
            config.Type = type;
            mapper.Setup(new DataMapperResolverArgs(null,config));

            var item = _db.GetItem("/sitecore/Content/Tests/DataMappers/SitecoreInfoMapper/DataMappersEmptyItem");
            var expected = item.TemplateID.Guid;

            Assert.IsNotNull(item, "Item is null, check in Sitecore that item exists");
            var dataContext = new SitecoreDataMappingContext(null, item, null);

            //Act
            var value = mapper.MapToProperty(dataContext);

            //Assert
            Assert.AreEqual(expected, value);
        }
        public void MapToProperty_SitecoreInfoTypeBaseTemplateIds_ReturnsBaseTemplateIds()
        {
            //Assign
            var type = SitecoreInfoType.BaseTemplateIds;

            var mapper = new SitecoreInfoMapper();
            var config = new SitecoreInfoConfiguration();
            config.Type = type;
            config.PropertyInfo = typeof(Stub).GetProperty("BaseTemplateIds");

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

            var item = _db.GetItem("/sitecore/Content/Tests/DataMappers/SitecoreInfoMapper/DataMappersEmptyItem");
          
            Assert.IsNotNull(item, "Item is null, check in Sitecore that item exists");
            var dataContext = new SitecoreDataMappingContext(null, item, null);

            //Act
            IEnumerable<ID> results;
            using (new SecurityDisabler())
            {
                
                results = mapper.MapToProperty(dataContext) as IEnumerable<ID>;
            }

            //Assert
            Assert.Greater( results.Count(), 10);
            Assert.IsTrue(results.All(x=>x!= item.TemplateID));
        }
        public void MapToProperty_SitecoreInfoTypeLanguage_ReturnsEnStringType()
        {

            //Assign
            var type = SitecoreInfoType.Language;

            var mapper = new SitecoreInfoMapper();
            var config = new SitecoreInfoConfiguration();
            config.Type = type;
            config.PropertyInfo = new FakePropertyInfo(typeof(string), "StringField", typeof(Stub));
            mapper.Setup(new DataMapperResolverArgs(null, config));

            var item = _db.GetItem("/sitecore/Content/Tests/DataMappers/SitecoreInfoMapper/DataMappersEmptyItem");

            var expected = item.Language.Name;


            Assert.IsNotNull(item, "Item is null, check in Sitecore that item exists");
            var dataContext = new SitecoreDataMappingContext(null, item, null);

            //Act
            var value = mapper.MapToProperty(dataContext);

            //Assert
            Assert.AreEqual(expected, value);
        }
        public void MapToProperty_MediaUrlWithFlag_ReturnsModifiedUrl(
            SitecoreInfoMediaUrlOptions option,
            string expected
            )
        {
            //Assign
            var mapper = new SitecoreInfoMapper();
            var config = new SitecoreInfoConfiguration();
            config.Type = SitecoreInfoType.MediaUrl;
            config.MediaUrlOptions = option;
            mapper.Setup(new DataMapperResolverArgs(null, config));

            Sitecore.Context.Site = null;

            var item = _db.GetItem("/sitecore/Content/Tests/DataMappers/SitecoreInfoMapper/DataMappersEmptyItem");

            Assert.IsNotNull(item, "Item is null, check in Sitecore that item exists");
            var dataContext = new SitecoreDataMappingContext(null, item, null);

            //Act
            var value = mapper.MapToProperty(dataContext);

            //Assert
            Assert.AreEqual(expected, value);
        }