public void GetField_ImageInField_MissingMediaItem_ReturnsImageObjectWithSrc()
        {
            //Assign
            var fieldValue =
                "<image mediaid=\"{D897833C-1F53-4FAE-B54B-BB5B11B8F851}\" mediapath=\"/Files/20121222_001405\" src=\"~/media/D897833C1F534FAEB54BBB5B11B8F851.ashx\" hspace=\"15\" vspace=\"20\" />";
            var mediaId = new ID("{D897833C-1F53-4FAE-B54B-BB5B11B8F851}");

            using (Db database = new Db
            {
                new Sitecore.FakeDb.DbItem("TestItem")
                {
                    new DbField(FieldName)
                    {
                        Value = fieldValue
                    }
                }
            })
            {
#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 == mediaId), 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 == mediaId), Arg.Any <MediaUrlOptions>())
                .Returns("/~/media/Test.ashx");

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



                var item   = database.GetItem("/sitecore/content/TestItem");
                var field  = item.Fields[FieldName];
                var mapper = new SitecoreFieldImageMapper();

                //Act
                var result = mapper.GetField(field, new SitecoreFieldConfiguration(), null) as Image;

                //Assert
                Assert.IsEmpty(result.Alt);
                // Assert.Equals(null, result.Border);
                Assert.AreEqual(string.Empty, result.Class);
                Assert.AreEqual(15, result.HSpace);
                Assert.AreEqual(0, result.Height);
                Assert.AreEqual(new Guid("{D897833C-1F53-4FAE-B54B-BB5B11B8F851}"), result.MediaId);
                Assert.IsTrue(string.IsNullOrEmpty(result.Src));
                Assert.AreEqual(20, result.VSpace);
                Assert.AreEqual(0, result.Width);
                Assert.AreEqual(false, result.MediaExists);
            }
        }
Esempio n. 2
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);
            }
        }
Esempio n. 3
0
        public void SetField_FileObjectPass_FieldPopulated()
        {
            //Assign
            var templateId = ID.NewID;
            var fieldId    = ID.NewID;
            var targetId   = ID.NewID;
            var mediaId    = new ID("{C10794CE-624F-4F72-A2B9-14336F3FB582}");

            using (Db database = new Db
            {
                new DbTemplate(templateId)
                {
                    { "Field", "" }
                },
                new Sitecore.FakeDb.DbItem("Target", targetId, templateId),
                new DbItem("Media", mediaId)
            })
            {
                var expected =
                    "<file mediaid=\"{C10794CE-624F-4F72-A2B9-14336F3FB582}\" src=\"~/media/Test.ashx\" />";

                var item   = database.GetItem("/sitecore/content/Target");
                var field  = item.Fields["Field"];
                var mapper = new SitecoreFieldFileMapper();
                var file   = new File()
                {
                    Id = new Guid("{C10794CE-624F-4F72-A2B9-14336F3FB582}")
                };


                using (new ItemEditing(item, true))
                {
                    field.Value = string.Empty;
                }

                Sitecore.Resources.Media.MediaProvider mediaProvider = Substitute.For <Sitecore.Resources.Media.MediaProvider>();
                mediaProvider
                .GetMediaUrl(Arg.Is <Sitecore.Data.Items.MediaItem>(i => i.ID == mediaId), Arg.Any <MediaUrlOptions>())
                .Returns("~/media/Test.ashx");

                using (new Sitecore.FakeDb.Resources.Media.MediaProviderSwitcher(mediaProvider))
                {
                    //Act
                    using (new ItemEditing(item, true))
                    {
                        mapper.SetField(field, file, null, null);
                    }
                    //Assert
                }
                AssertHtml.AreHtmlElementsEqual(expected, item["Field"], "file");
            }
        }
Esempio n. 4
0
        public void GetViewValue_FieldPointsAtFile_ReturnFileObject()
        {
            //Assign
            var templateId = ID.NewID;
            var fieldId    = ID.NewID;
            var targetId   = ID.NewID;
            var expected   = "/~/media/C10794CE624F4F72A2B914336F3FB582.ashx";
            var mediaId    = new ID("{C10794CE-624F-4F72-A2B9-14336F3FB582}");

            using (Db database = new Db
            {
                new DbTemplate(templateId)
                {
                    { "Field", "" }
                },
                new Sitecore.FakeDb.DbItem("Target", targetId, templateId),
                new DbItem("Media", mediaId)
            })
            {
                Sitecore.Resources.Media.MediaProvider mediaProvider =
                    Substitute.For <Sitecore.Resources.Media.MediaProvider>();
                mediaProvider
                .GetMediaUrl(
                    Arg.Is <Sitecore.Data.Items.MediaItem>(
                        i => i.ID == mediaId)
                    )
                .Returns(expected);

                using (new Sitecore.FakeDb.Resources.Media.MediaProviderSwitcher(mediaProvider))
                {
                    var fieldValue =
                        "<file mediaid=\"{C10794CE-624F-4F72-A2B9-14336F3FB582}\" src=\"~/media/C10794CE624F4F72A2B914336F3FB582.ashx\" />";

                    var item   = database.GetItem("/sitecore/content/Target");
                    var field  = item.Fields["Field"];
                    var mapper = new SitecoreFieldFileMapper();

                    using (new ItemEditing(item, true))
                    {
                        field.Value = fieldValue;
                    }

                    //Act
                    var result = mapper.GetField(field, null, null) as File;

                    //Assert
                    Assert.AreEqual(mediaId.Guid, result.Id);
                    Assert.AreEqual(expected, result.Src);
                }
            }
        }
Esempio n. 5
0
        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);
                    }
                };


#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.Is <MediaUrlOptions>(x => pred(x))
                    )
                .Returns(expected);
#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.Is <MediaUrlOptions>(x => pred(x))
                    )
                .Returns(expected);

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

                var options = new GetItemOptionsParams();

                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);
            }
        }
        public void GetField_FieldNotNull_ReturnsNullImageObject(
            SitecoreMediaUrlOptions option

            )
        {
            //Assign
            var config = new SitecoreFieldConfiguration();

            config.MediaUrlOptions = option;
            string expected   = "/~/media/Test.ashx";
            var    fieldValue =
                "<image mediaid=\"{D897833C-1F53-4FAE-B54B-BB5B11B8F851}\" mediapath=\"/Files/20121222_001405\" src=\"~/media/D897833C1F534FAEB54BBB5B11B8F851.ashx\" hspace=\"15\" vspace=\"20\" />";
            var mediaId = new ID("{D897833C-1F53-4FAE-B54B-BB5B11B8F851}");


            using (Db database = new Db
            {
                new Sitecore.FakeDb.DbItem("TestItem")
                {
                    new DbField(FieldName)
                    {
                        Value = fieldValue
                    }
                },
                new Sitecore.FakeDb.DbItem("MediaItem", mediaId)
                {
                    new DbField("alt")
                    {
                        Value = "test alt"
                    },
                    new DbField("height")
                    {
                        Value = "480"
                    },
                    new DbField("width")
                    {
                        Value = "640"
                    },
                }
            })
            {
                Func <MediaUrlOptions, bool> pred = x =>
                {
                    switch (option)
                    {
                    case SitecoreMediaUrlOptions.Default:
                        return(true);

                    case SitecoreMediaUrlOptions.RemoveExtension:
                        return(x.IncludeExtension == false);

                    case SitecoreMediaUrlOptions.LowercaseUrls:
                        return(x.LowercaseUrls == true);

                    default:
                        return(false);
                    }
                };



#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 == mediaId),
                    Arg.Is <MediaUrlOptions>(x => pred(x))
                    )
                .Returns(expected);
#else
                Sitecore.Resources.Media.MediaProvider mediaProvider =
                    Substitute.For <Sitecore.Resources.Media.MediaProvider>();
                mediaProvider
                .GetMediaUrl(
                    Arg.Is <Sitecore.Data.Items.MediaItem>(i => i.ID == mediaId),
                    Arg.Is <MediaUrlOptions>(x => pred(x))
                    )
                .Returns(expected);

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


                var item   = database.GetItem("/sitecore/content/TestItem");
                var field  = item.Fields[FieldName];
                var mapper = new SitecoreFieldImageMapper();
                mapper.Setup(new DataMapperResolverArgs(null, config));
                //Act
                var result = mapper.GetField(field, config, null) as Image;

                //Assert
                Assert.AreEqual("test alt", result.Alt);
                // Assert.Equals(null, result.Border);
                Assert.AreEqual(string.Empty, result.Class);
                Assert.AreEqual(15, result.HSpace);
                Assert.AreEqual(480, result.Height);
                Assert.AreEqual(new Guid("{D897833C-1F53-4FAE-B54B-BB5B11B8F851}"), result.MediaId);
                Assert.IsTrue(result.Src.EndsWith(expected));
                Assert.AreEqual(20, result.VSpace);
                Assert.AreEqual(640, result.Width);
            }
        }
Esempio n. 7
0
        public void GetField_ImageInField_ReturnsImageObject()
        {
            //Assign
            var fieldValue =
                "<image mediaid=\"{D897833C-1F53-4FAE-B54B-BB5B11B8F851}\" mediapath=\"/Files/20121222_001405\" src=\"~/media/D897833C1F534FAEB54BBB5B11B8F851.ashx\" hspace=\"15\" vspace=\"20\" />";
            var mediaId = new ID("{D897833C-1F53-4FAE-B54B-BB5B11B8F851}");

            using (Db database = new Db
            {
                new Sitecore.FakeDb.DbItem("TestItem")
                {
                    new DbField(FieldName)
                    {
                        Value = fieldValue
                    }
                },
                new Sitecore.FakeDb.DbItem("MediaItem", mediaId)
                {
                    new DbField("alt")
                    {
                        Value = "test alt"
                    },
                    new DbField("height")
                    {
                        Value = "480"
                    },
                    new DbField("width")
                    {
                        Value = "640"
                    },
                }
            })
            {
                Sitecore.Resources.Media.MediaProvider mediaProvider = Substitute.For <Sitecore.Resources.Media.MediaProvider>();
                mediaProvider
                .GetMediaUrl(Arg.Is <Sitecore.Data.Items.MediaItem>(i => i.ID == mediaId), Arg.Any <MediaUrlOptions>())
                .Returns("/~/media/Test.ashx");

                using (new Sitecore.FakeDb.Resources.Media.MediaProviderSwitcher(mediaProvider))
                {
                    var item   = database.GetItem("/sitecore/content/TestItem");
                    var field  = item.Fields[FieldName];
                    var mapper = new SitecoreFieldImageMapper();

                    //Act
                    var result = mapper.GetField(field, new SitecoreFieldConfiguration(), null) as Image;

                    //Assert
                    Assert.AreEqual("test alt", result.Alt);
                    // Assert.Equals(null, result.Border);
                    Assert.AreEqual(string.Empty, result.Class);
                    Assert.AreEqual(15, result.HSpace);
                    Assert.AreEqual(480, result.Height);
                    Assert.AreEqual(new Guid("{D897833C-1F53-4FAE-B54B-BB5B11B8F851}"), result.MediaId);
                    Assert.IsTrue(result.Src.EndsWith("/~/media/Test.ashx"));
                    Assert.AreEqual(20, result.VSpace);
                    Assert.AreEqual(640, result.Width);
                    Assert.AreEqual(true, result.MediaExists);
                }
            }
        }