public void SetField_FileEmptyGuid_FieldLinkRemoved() { //Assign var templateId = ID.NewID; var fieldId = ID.NewID; var targetId = ID.NewID; using (Db database = new Db { new DbTemplate(templateId) { {"Field", ""} }, new Sitecore.FakeDb.DbItem("Target", targetId, templateId), new Sitecore.FakeDb.DbItem("File", new ID("{C10794CE-624F-4F72-A2B9-14336F3FB582}"), templateId), }) { var fieldValue = "<file mediaid=\"{C10794CE-624F-4F72-A2B9-14336F3FB582}\" src=\"~/media/C10794CE624F4F72A2B914336F3FB582.ashx\" />"; var expected = string.Empty; var item = database.GetItem("/sitecore/content/Target"); var field = item.Fields["Field"]; var mapper = new SitecoreFieldFileMapper(); var file = new File() { Id = Guid.Empty }; using (new ItemEditing(item, true)) { field.Value = fieldValue; } //Act using (new ItemEditing(item, true)) { mapper.SetField(field, file, null, null); } //Assert Assert.AreEqual(expected, item["Field"]); } }
/// <summary> /// Gets the field. /// </summary> /// <param name="field">The field.</param> /// <param name="config">The config.</param> /// <param name="context">The context.</param> /// <returns>System.Object.</returns> public override object GetField(Field field, SitecoreFieldConfiguration config, SitecoreDataMappingContext context) { FileField fileField = new FileField(field); File file = new File(); if (fileField.MediaItem != null) file.Src = MediaManager.GetMediaUrl(fileField.MediaItem); file.Id = fileField.MediaID.Guid; return file; }
public void SetField_FileObjectPass_FieldPopulated() { //Assign var expected = "<file mediaid=\"{C10794CE-624F-4F72-A2B9-14336F3FB582}\" src=\"~/media/C10794CE624F4F72A2B914336F3FB582.ashx\" />"; var item = Database.GetItem("/sitecore/content/Tests/DataMappers/SitecoreFieldFileMapper/SetField"); var field = item.Fields[FieldName]; 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; } //Act using (new ItemEditing(item, true)) { mapper.SetField(field, file, null, null); } //Assert Assert.AreEqual(expected, item[FieldName]); }
public void SetField_FileContainsMissinfMedia_ExpectionThrown() { //Assign var expected = string.Empty; var item = Database.GetItem("/sitecore/content/Tests/DataMappers/SitecoreFieldFileMapper/SetField"); var field = item.Fields[FieldName]; var mapper = new SitecoreFieldFileMapper(); var file = new File() { Id = Guid.NewGuid() }; using (new ItemEditing(item, true)) { field.Value = string.Empty; } //Act using (new ItemEditing(item, true)) { mapper.SetField(field, file, null, null); } //Assert }
public void SetField_FileContainsMissinfMedia_ExpectionThrown() { //Assign var templateId = ID.NewID; var fieldId = ID.NewID; var targetId = ID.NewID; using (Db database = new Db { new DbTemplate(templateId) { {"Field", ""} }, new Sitecore.FakeDb.DbItem("Target", targetId, templateId), }) { var expected = string.Empty; var item = database.GetItem("/sitecore/content/Target"); var field = item.Fields["Field"]; var mapper = new SitecoreFieldFileMapper(); var file = new File() { Id = Guid.NewGuid() }; using (new ItemEditing(item, true)) { field.Value = string.Empty; } //Act using (new ItemEditing(item, true)) { Assert.Throws<MapperException>(() => { mapper.SetField(field, file, null, null); }); } //Assert } }
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"); } }