コード例 #1
0
        public void MapToProperty_SitecoreInfoTypeNotSet_ThrowsException()
        {
            //Assign
            using (Db database = new Db
            {
                new Sitecore.FakeDb.DbItem("TestItem")
            })
            {
                SitecoreInfoType type = SitecoreInfoType.NotSet;

                var mapper = new SitecoreInfoMapper();
                var config = new SitecoreInfoConfiguration();
                config.Type = type;

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

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

                //Act
                Assert.Throws <MapperException>(() => mapper.Setup(new DataMapperResolverArgs(null, config)));

                //Assert
                //No asserts expect exception
            }
        }
コード例 #2
0
        /// <summary>
        /// Map item information  to a class property
        /// </summary>
        /// <param name="ex">The ex.</param>
        /// <param name="infoType">The type of info to map</param>
        /// <returns>SitecoreInfo{`0}.</returns>
        public SitecoreInfo <T> Info(Expression <Func <T, object> > ex, SitecoreInfoType infoType)
        {
            SitecoreInfo <T> builder = new SitecoreInfo <T>(ex, Configuration);

            builder.InfoType(infoType);
            Configuration.AddProperty(builder.Configuration);
            return(builder);
        }
コード例 #3
0
        public static object GetItemInfo(SitecoreInfoType infoType, Item item, UrlOptions urlOptions)
        {
            if (urlOptions == null)
            {
                urlOptions = new UrlOptions();
            }

            switch (infoType)
            {
            case SitecoreInfoType.ContentPath:
                return(item.Paths.ContentPath);

            case SitecoreInfoType.DisplayName:
                return(item.DisplayName);

            case SitecoreInfoType.FullPath:
                return(item.Paths.FullPath);

            case SitecoreInfoType.Name:
                return(item.Name);

            case SitecoreInfoType.Key:
                return(item.Key);

            case SitecoreInfoType.MediaUrl:
                global::Sitecore.Data.Items.MediaItem media = new global::Sitecore.Data.Items.MediaItem(item);
                return(global::Sitecore.Resources.Media.MediaManager.GetMediaUrl(media));

                break;

            case SitecoreInfoType.Path:
                return(item.Paths.Path);

            case SitecoreInfoType.TemplateId:
                return(item.TemplateID.Guid);

            case SitecoreInfoType.TemplateName:
                return(item.TemplateName);

            case SitecoreInfoType.Url:
                return(LinkManager.GetItemUrl(item, urlOptions));

            case SitecoreInfoType.FullUrl:
                return(LinkManager.GetItemUrl(item, new UrlOptions()
                {
                    AlwaysIncludeServerUrl = true
                }));

            case SitecoreInfoType.Version:
                return(item.Version.Number);

            case SitecoreInfoType.Language:
                return(item.Language);

            default:
                throw new NotSupportedException("Value {0} not supported".Formatted(infoType.ToString()));
            }
        }
コード例 #4
0
        private T GetInfo <T>(SitecoreInfoType infoType)
        {
            var mapper = new SitecoreInfoMapper();
            var config = new SitecoreInfoConfiguration();

            config.Type = infoType;
            //  config.PropertyInfo = new FakePropertyInfo(typeof(T),typeof(DynamicItem));
            mapper.Setup(new DataMapperResolverArgs(null, config));
            return((T)mapper.MapToProperty(new SitecoreDataMappingContext(null, _item, null)));
        }
コード例 #5
0
        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");
            var options    = new GetItemOptionsParams();

            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,
                }
            })
            {
#if SC90 || SC91 || SC92
                var mediaUrlProvider = Substitute.For <BaseMediaManager>();

                SitecoreVersionAbstractions.MediaManager = new LazyResetable <BaseMediaManager>(() => mediaUrlProvider);

                mediaUrlProvider
                .GetMediaUrl(Arg.Is <Sitecore.Data.Items.MediaItem>(i => i.ID == itemId), Arg.Any <MediaUrlOptions>())
                .Returns("/~/media/Test.ashx");
#else
                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");

                new Sitecore.FakeDb.Resources.Media.MediaProviderSwitcher(mediaProvider);
#endif

                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, options);

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

                //Assert
                Assert.AreEqual(expected, value);
            }
        }
コード例 #6
0
 /// <summary>
 /// The type of information that should populate the property
 /// </summary>
 /// <param name="type">The type.</param>
 /// <returns>SitecoreInfo{`0}.</returns>
 public SitecoreInfo <T> InfoType(SitecoreInfoType type)
 {
     Configuration.Type = type;
     if (type == SitecoreInfoType.Language)
     {
         _owner.LanguageConfig = Configuration;
     }
     else if (type == SitecoreInfoType.Version)
     {
         _owner.VersionConfig = Configuration;
     }
     return(this);
 }
コード例 #7
0
        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);
        }
コード例 #8
0
        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);
                }
            }
        }
コード例 #9
0
        public static object GetItemInfo(SitecoreInfoType infoType, Item item, global::Sitecore.Links.UrlOptions urlOptions)
        {
            
            if (urlOptions == null) urlOptions = new UrlOptions();

            if(urlOptions.Language == null)
                urlOptions.Language = item.Language;

            switch (infoType)
            {
                case SitecoreInfoType.ContentPath:
                    return item.Paths.ContentPath;
                case SitecoreInfoType.DisplayName:
                    return item.DisplayName;
                case SitecoreInfoType.FullPath:
                    return item.Paths.FullPath;
                case SitecoreInfoType.Name:
                    return item.Name;
                case SitecoreInfoType.Key:
                    return item.Key;
                case SitecoreInfoType.MediaUrl:
                    global::Sitecore.Data.Items.MediaItem media = new global::Sitecore.Data.Items.MediaItem(item);
                    return global::Sitecore.Resources.Media.MediaManager.GetMediaUrl(media);
                    break;
                case SitecoreInfoType.Path:
                    return item.Paths.Path;
                case SitecoreInfoType.TemplateId:
                    return item.TemplateID.Guid;
                case SitecoreInfoType.TemplateName:
                    return item.TemplateName;
                case SitecoreInfoType.Url:
                    return LinkManager.GetItemUrl(item, urlOptions);
                case SitecoreInfoType.FullUrl:
                    return LinkManager.GetItemUrl(item, new UrlOptions() { AlwaysIncludeServerUrl = true });
                case SitecoreInfoType.Version:
                    return item.Version.Number;
                case SitecoreInfoType.Language:
                    return item.Language;
                default:
                    throw new NotSupportedException("Value {0} not supported".Formatted(infoType.ToString()));
            }
        }
コード例 #10
0
        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
        }
コード例 #11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SitecoreInfoAttribute"/> class.
 /// </summary>
 /// <param name="infoType">Type of the info.</param>
 public SitecoreInfoAttribute(SitecoreInfoType infoType)
 {
     Type = infoType;
 }
コード例 #12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SitecoreInfoAttribute"/> class.
 /// </summary>
 /// <param name="infoType">Type of the info.</param>
 public SitecoreInfoAttribute(SitecoreInfoType infoType)
 {
     Type = infoType;
 }
コード例 #13
0
ファイル: SitecoreInfo.cs プロジェクト: dsolovay/Glass.Mapper
 /// <summary>
 /// The type of information that should populate the property
 /// </summary>
 /// <param name="type">The type.</param>
 /// <returns>SitecoreInfo{`0}.</returns>
 public SitecoreInfo <T> InfoType(SitecoreInfoType type)
 {
     Configuration.Type = type;
     return(this);
 }
コード例 #14
0
 internal SitecoreInfoAttribute()
 {
     Type = SitecoreInfoType.NotSet;
 }
コード例 #15
0
 /// <summary>
 /// Indicates that the property should be populated with data about the item such as URL, ID, etc.
 /// </summary>
 /// <param name="type">The type of information that should populate the property</param>
 public SitecoreInfoAttribute(SitecoreInfoType type)
 {
     Type = type;
 }
コード例 #16
0
 public SitecoreInfo <T> InfoType(SitecoreInfoType type)
 {
     _attr.Type = type;
     return(this);
 }
コード例 #17
0
 /// <summary>
 /// Indicates that the property should be populated with data about the item such as URL, ID, etc.
 /// </summary>
 /// <param name="type">The type of information that should populate the property</param>
 public SitecoreInfoAttribute(SitecoreInfoType type)
 {
     Type = type;
 }
コード例 #18
0
 /// <summary>
 /// The type of information that should populate the property
 /// </summary>
 /// <param name="type">The type.</param>
 /// <returns>SitecoreInfo{`0}.</returns>
 public SitecoreInfo <T> InfoType(SitecoreInfoType type)
 {
     Configuration.Type = type;
     SitecoreTypeConfiguration.AssignInfoToKnownProperty(_owner, Configuration);
     return(this);
 }