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)) .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, null, 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); } } }
public void HowToMockMediaItemProvider() { const string MyImageUrl = "~/media/myimage.ashx"; Sitecore.Data.ID mediaItemId = Sitecore.Data.ID.NewID; // create some media item. Location, fields and template are not important using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db { new Sitecore.FakeDb.DbItem("my-image", mediaItemId) }) { Sitecore.Data.Items.Item mediaItem = db.GetItem(mediaItemId); // create media provider mock and configure behaviour Sitecore.Resources.Media.MediaProvider mediaProvider = NSubstitute.Substitute.For <Sitecore.Resources.Media.MediaProvider>(); mediaProvider .GetMediaUrl(Arg.Is <Sitecore.Data.Items.MediaItem>(i => i.ID == mediaItemId)) .Returns(MyImageUrl); // substitute the original provider with the mocked one using (new Sitecore.FakeDb.Resources.Media.MediaProviderSwitcher(mediaProvider)) { string mediaUrl = Sitecore.Resources.Media.MediaManager.GetMediaUrl(mediaItem); Xunit.Assert.Equal(MyImageUrl, mediaUrl); } } }
public void GetField_FieldContainsMediaLink_ReturnsMediaLink() { //Assign var templateId = ID.NewID; var targetId = ID.NewID; var fieldName = "Field"; using (Db database = new Db { new DbTemplate(templateId) { { fieldName, "" } }, new Sitecore.FakeDb.DbItem("Target", targetId, templateId), }) { var mapper = new SitecoreFieldLinkMapper(new FakeUrlOptionsResolver()); var fieldValue = "<link text=\"Test description\" linktype=\"media\" url=\"/Files/20121222_001405\" title=\"test alternative\" class=\"testClass\" target=\"_blank\" id=\"{0}\" />" .Formatted(targetId); var item = database.GetItem("/sitecore/content/Target"); var field = item.Fields[fieldName]; using (new ItemEditing(item, true)) { field.Value = fieldValue; } Sitecore.Resources.Media.MediaProvider mediaProvider = NSubstitute.Substitute.For <Sitecore.Resources.Media.MediaProvider>(); mediaProvider .GetMediaUrl(Arg.Is <Sitecore.Data.Items.MediaItem>(i => i.ID == targetId)) .Returns("Media.Aspx"); // substitute the original provider with the mocked one using (new Sitecore.FakeDb.Resources.Media.MediaProviderSwitcher(mediaProvider)) { //Act var result = mapper.GetField(field, null, null) as Link; //Assert Assert.AreEqual("", result.Anchor); Assert.AreEqual("testClass", result.Class); Assert.AreEqual("", result.Query); Assert.AreEqual("_blank", result.Target); Assert.AreEqual(targetId.Guid, result.TargetId); Assert.AreEqual("Test description", result.Text); Assert.AreEqual("test alternative", result.Title); Assert.AreEqual(LinkType.Media, result.Type); Assert.AreEqual("Media.Aspx", result.Url); } } }
public void MediaUrlShouldReturnEmptyStringWhenLinkIsBroken([Content] Db db, [Content] Item target, [Content] MediaTemplate template, string expectedUri) { var newID = ID.NewID; db.Add(new Sitecore.FakeDb.DbItem("home", newID, template.ID) { new Sitecore.FakeDb.DbLinkField("medialink", template.FieldId) { LinkType = "media", } }); Sitecore.Resources.Media.MediaProvider mediaProvider = NSubstitute.Substitute.For <Sitecore.Resources.Media.MediaProvider>(); mediaProvider .GetMediaUrl(Arg.Is <Sitecore.Data.Items.MediaItem>(i => i.ID == target.ID)) .Returns(expectedUri); // substitute the original provider with the mocked one using (new Sitecore.FakeDb.Resources.Media.MediaProviderSwitcher(mediaProvider)) ItemExtensions.MediaUrl(Database.GetDatabase("master").GetItem(newID), template.FieldId).Should().Be(""); }
public void SetField_MedialLink_MediaLinkSetOnField() { //Assign var templateId = ID.NewID; var targetId = ID.NewID; var fieldName = "Field"; using (Db database = new Db { new DbTemplate(templateId) { { fieldName, "" } }, new Sitecore.FakeDb.DbItem("Target", targetId, templateId), }) { var mapper = new SitecoreFieldLinkMapper(new FakeUrlOptionsResolver()); var expected = "<link target=\"_blank\" title=\"test alternative\" linktype=\"media\" id=\"{0}\" url=\"Media.Aspx\" class=\"testClass\" text=\"Test description\" />" .Formatted(targetId.Guid.ToString("B").ToUpperInvariant()); var item = database.GetItem("/sitecore/content/Target"); var field = item.Fields[fieldName]; var value = new Link() { Anchor = "", Class = "testClass", Query = "", Target = "_blank", TargetId = targetId.Guid, Text = "Test description", Title = "test alternative", Type = LinkType.Media, Url = "" }; using (new ItemEditing(item, true)) { field.Value = string.Empty; } #if SC90 || SC91 var mediaUrlProvider = Substitute.For <BaseMediaManager>(); SitecoreVersionAbstractions.MediaManager = new LazyResetable <BaseMediaManager>(() => mediaUrlProvider); mediaUrlProvider .GetMediaUrl(Arg.Is <Sitecore.Data.Items.MediaItem>(i => i.ID == targetId)) .Returns("Media.Aspx"); #else Sitecore.Resources.Media.MediaProvider mediaProvider = Substitute.For <Sitecore.Resources.Media.MediaProvider>(); mediaProvider .GetMediaUrl(Arg.Is <Sitecore.Data.Items.MediaItem>(i => i.ID == targetId)) .Returns("Media.Aspx"); new Sitecore.FakeDb.Resources.Media.MediaProviderSwitcher(mediaProvider); #endif //Act using (new ItemEditing(item, true)) { mapper.SetField(field, value, null, null); } //Assert AssertHtml.AreHtmlElementsEqual(expected, field.Value, "link"); } }
public void GetField_FieldContainsMediaLink_ReturnsMediaLink() { //Assign var templateId = ID.NewID; var targetId = ID.NewID; var fieldName = "Field"; using (Db database = new Db { new DbTemplate(templateId) { { fieldName, "" } }, new Sitecore.FakeDb.DbItem("Target", targetId, templateId), }) { var mapper = new SitecoreFieldLinkMapper(new FakeUrlOptionsResolver()); var fieldValue = "<link text=\"Test description\" linktype=\"media\" url=\"/Files/20121222_001405\" title=\"test alternative\" class=\"testClass\" target=\"_blank\" id=\"{0}\" />" .Formatted(targetId); var item = database.GetItem("/sitecore/content/Target"); var field = item.Fields[fieldName]; using (new ItemEditing(item, true)) { field.Value = fieldValue; } #if SC90 || SC91 || SC92 || SC93 || SC100 var mediaUrlProvider = Substitute.For <BaseMediaManager>(); SitecoreVersionAbstractions.MediaManager = new LazyResetable <BaseMediaManager>(() => mediaUrlProvider); mediaUrlProvider .GetMediaUrl(Arg.Is <Sitecore.Data.Items.MediaItem>(i => i.ID == targetId)) .Returns("Media.Aspx"); #else Sitecore.Resources.Media.MediaProvider mediaProvider = Substitute.For <Sitecore.Resources.Media.MediaProvider>(); mediaProvider .GetMediaUrl(Arg.Is <Sitecore.Data.Items.MediaItem>(i => i.ID == targetId)) .Returns("Media.Aspx"); new Sitecore.FakeDb.Resources.Media.MediaProviderSwitcher(mediaProvider); #endif var context = new SitecoreDataMappingContext(null, item, null, new GetItemByPathOptions()); //Act var result = mapper.GetField(field, null, context) as Link; //Assert Assert.AreEqual("", result.Anchor); Assert.AreEqual("testClass", result.Class); Assert.AreEqual("", result.Query); Assert.AreEqual("_blank", result.Target); Assert.AreEqual(targetId.Guid, result.TargetId); Assert.AreEqual("Test description", result.Text); Assert.AreEqual("test alternative", result.Title); Assert.AreEqual(LinkType.Media, result.Type); Assert.AreEqual("Media.Aspx", result.Url); } }