SetField() public method

Sets the field.
/// No item with ID {0}. Can not update Link linkField.Formatted(newId) /// or /// No item with ID {0}. Can not update Link linkField.Formatted(newId) ///
public SetField ( Field field, object value, SitecoreFieldConfiguration config, SitecoreDataMappingContext context ) : void
field Field The field.
value object The value.
config Glass.Mapper.Sc.Configuration.SitecoreFieldConfiguration The config.
context SitecoreDataMappingContext The context.
return void
        public void SetField_MedialLink_MediaLinkSetOnField()
        {
            //Assign
            var mapper = new SitecoreFieldLinkMapper();
            var expected =
                  "<link target=\"_blank\" title=\"test alternative\" linktype=\"media\" id=\"{8E1C32F3-CF15-4067-A3F4-85148606F9CD}\" url=\"/~/media/8E1C32F3CF154067A3F485148606F9CD.ashx\" class=\"testClass\" text=\"Test description\" />";


            var item = Database.GetItem("/sitecore/content/Tests/DataMappers/SitecoreFieldLinkMapper/SetField");
            var field = item.Fields[FieldName];

            var value = new Link()
            {
                Anchor = "",
                Class = "testClass",
                Query = "",
                Target = "_blank",
                TargetId = new Guid("{8E1C32F3-CF15-4067-A3F4-85148606F9CD}"),
                Text = "Test description",
                Title = "test alternative",
                Type = LinkType.Media,
                Url = ""
            };


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

            //Act
            using (new ItemEditing(item, true))
            {
                mapper.SetField(field, value, null, null);
            }

            //Assert
            Assert.AreEqual(expected, field.Value);
        }
        public void SetField_MedialLinkMissingItem_ThrowsException()
        {
            //Assign
            var mapper = new SitecoreFieldLinkMapper();


            var item = Database.GetItem("/sitecore/content/Tests/DataMappers/SitecoreFieldLinkMapper/SetField");
            var field = item.Fields[FieldName];

            var value = new Link()
            {
                Anchor = "",
                Class = "testClass",
                Query = "",
                Target = "_blank",
                TargetId = new Guid("{11111111-CF15-4067-A3F4-85148606F9CD}"),
                Text = "Test description",
                Title = "test alternative",
                Type = LinkType.Media,
                Url = ""
            };


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

            //Act
            using (new ItemEditing(item, true))
            {
                mapper.SetField(field, value, null, null);
            }

            //Assert
        }
        public void SetField_InternalLink_InternalLinkSetOnField()
        {
            //Assign
            var mapper = new SitecoreFieldLinkMapper();
            var expected =
                "<link target=\"testTarget\" title=\"test alternative\" querystring=\"q=s\" linktype=\"internal\" id=\"{1AE37F34-3B6F-4F5F-A5C2-FD2B9D5A47A0}\" anchor=\"testAnchor\" url=\"/en/sitecore/content/Tests/DataMappers/SitecoreFieldLinkMapper/Target.aspx\" class=\"testClass\" text=\"Test description\" />";
            
            var item = Database.GetItem("/sitecore/content/Tests/DataMappers/SitecoreFieldLinkMapper/SetField");
            var field = item.Fields[FieldName];

            var value = new Link()
            {
                Anchor = "testAnchor",
                Class = "testClass",
                Query = "q=s",
                Target = "testTarget",
                TargetId = new Guid("{1AE37F34-3B6F-4F5F-A5C2-FD2B9D5A47A0}"),
                Text = "Test description",
                Title = "test alternative",
                Type = LinkType.Internal,
                Url = ""
            };


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

            //Act
            using (new ItemEditing(item, true))
            {
                mapper.SetField(field, value, null, null);
            }

            //Assert
            Assert.AreEqual(expected, field.Value);
        }
        public void SetField_InternalLinkMissingTarget_ThorwsException()
        {
            //Assign
            var mapper = new SitecoreFieldLinkMapper();

            var item = Database.GetItem("/sitecore/content/Tests/DataMappers/SitecoreFieldLinkMapper/SetField");
            var field = item.Fields[FieldName];

            var value = new Link()
            {
                Anchor = "testAnchor",
                Class = "testClass",
                Query = "q=s",
                Target = "testTarget",
                TargetId = new Guid("{11111111-3B6F-4F5F-A5C2-FD2B9D5A47A0}"),
                Text = "Test description",
                Title = "test alternative",
                Type = LinkType.Internal,
                Url = ""
            };


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

            //Act
            using (new ItemEditing(item, true))
            {
                mapper.SetField(field, value, null, null);
            }

            //Assert
        }
        public void SetField_MailToLink_MailToLinkSetOnField()
        {
            //Assign
            var mapper = new SitecoreFieldLinkMapper();
            var expected =
                "<link url=\"mailto:[email protected]\" text=\"Test description\" class=\"testClass\" title=\"test alternative\" linktype=\"mailto\" />";

            var item = Database.GetItem("/sitecore/content/Tests/DataMappers/SitecoreFieldLinkMapper/SetField");
            var field = item.Fields[FieldName];

            var value = new Link()
            {
                Anchor = "",
                Class = "testClass",
                Query = "",
                Target = "",
                TargetId = Guid.Empty,
                Text = "Test description",
                Title = "test alternative",
                Type = LinkType.MailTo,
                Url = "mailto:[email protected]"
            };


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

            //Act
            using (new ItemEditing(item, true))
            {
                mapper.SetField(field, value, null, null);
            }

            //Assert
            Assert.AreEqual(expected, field.Value);
        }
        public void SetField_InternalLinkMissingTarget_ThorwsException()
        {
            //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 item = database.GetItem("/sitecore/content/Target");
                var field = item.Fields[fieldName];

                var value = new Link()
                {
                    Anchor = "testAnchor",
                    Class = "testClass",
                    Query = "q=s",
                    Target = "testTarget",
                    TargetId = new Guid("{11111111-3B6F-4F5F-A5C2-FD2B9D5A47A0}"),
                    Text = "Test description",
                    Title = "test alternative",
                    Type = LinkType.Internal,
                    Url = ""
                };


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

                //Act
                using (new ItemEditing(item, true))
                {
                    Assert.Throws<MapperException>(() =>
                    {
                        mapper.SetField(field, value, null, null);

                    });
                }

                //Assert
            }
        }
        public void SetField_InternalLinkWithSpecialCharacters_InternalLinkSetOnField()
        {
            //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=\"testTarget\" title=\"test alternative\" querystring=\"q%3ds%253d\" linktype=\"internal\" id=\"{0}\" anchor=\"testAnchor\" url=\"/en/sitecore/content/Target.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 = "testAnchor",
                    Class = "testClass",
                    Query = "q=s%3d",
                    Target = "testTarget",
                    TargetId = targetId.Guid,
                    Text = "Test description",
                    Title = "test alternative",
                    Type = LinkType.Internal,
                    Url = ""
                };


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

                //Act
                using (new ItemEditing(item, true))
                {
                    mapper.SetField(field, value, null, null);
                }

                //Assert
                AssertHtml.AreHtmlElementsEqual(expected, field.Value, "link");
            }
        }
        public void SetField_MailToLink_MailToLinkSetOnField()
        {
            //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 url=\"mailto:[email protected]\" text=\"Test description\" class=\"testClass\" title=\"test alternative\" linktype=\"mailto\" />";

                var item = database.GetItem("/sitecore/content/Target");
                var field = item.Fields[fieldName];

                var value = new Link()
                {
                    Anchor = "",
                    Class = "testClass",
                    Query = "",
                    Target = "",
                    TargetId = Guid.Empty,
                    Text = "Test description",
                    Title = "test alternative",
                    Type = LinkType.MailTo,
                    Url = "mailto:[email protected]"
                };


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

                //Act
                using (new ItemEditing(item, true))
                {
                    mapper.SetField(field, value, null, null);
                }

                //Assert
                AssertHtml.AreHtmlElementsEqual(expected, field.Value, "link");
            }
        }
        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;
                }

                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
                    using (new ItemEditing(item, true))
                    {
                        mapper.SetField(field, value, null, null);
                    }

                    //Assert
                    AssertHtml.AreHtmlElementsEqual(expected, field.Value, "link");
                }
            }
        }