GetField() 공개 메소드

Gets the field.
public GetField ( Sitecore field, SitecoreFieldConfiguration config, SitecoreDataMappingContext context ) : object
field Sitecore The field.
config Glass.Mapper.Sc.Configuration.SitecoreFieldConfiguration The config.
context SitecoreDataMappingContext The context.
리턴 object
        public void GetField_FieldContainsExternal_ReturnsExternalLink()
        {
            //Assign
            var mapper = new SitecoreFieldLinkMapper();
            var fieldValue =
                "<link text=\"Test description\" linktype=\"external\" url=\"http://www.google.com\" anchor=\"\" title=\"test alternative\" class=\"testClass\" target=\"_blank\" />";

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

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

            //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(Guid.Empty, result.TargetId);
            Assert.AreEqual("Test description", result.Text);
            Assert.AreEqual("test alternative", result.Title);
            Assert.AreEqual(LinkType.External, result.Type);
            Assert.AreEqual("http://www.google.com", result.Url);
        }
        public void GetField_FieldContainsAnchor_ReturnsAnchorLink()
        {
            //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();
                var fieldValue =
                    "<link text=\"Test description\" linktype=\"anchor\" url=\"testAnchor\" anchor=\"testAnchor\" title=\"test alternate\" class=\"testClass\" />";

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

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

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

                //Assert
                Assert.AreEqual("testAnchor", result.Anchor);
                Assert.AreEqual("testClass", result.Class);
                Assert.AreEqual("", result.Query);
                Assert.AreEqual("", result.Target);
                Assert.AreEqual(Guid.Empty, result.TargetId);
                Assert.AreEqual("Test description", result.Text);
                Assert.AreEqual("test alternate", result.Title);
                Assert.AreEqual(LinkType.Anchor, result.Type);
                Assert.AreEqual("testAnchor", result.Url);
            }
        }
        public void GetField_FieldContainsJavaScript_ReturnsJavascriptLink()
        {
            //Assign
            var mapper = new SitecoreFieldLinkMapper();
            var fieldValue =
                "<link text=\"Test description\" linktype=\"javascript\" url=\"javascript:alert('hello world');\" anchor=\"\" title=\"test alternate\" class=\"testClass\" />";

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

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

            //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("", result.Target);
            Assert.AreEqual(Guid.Empty, result.TargetId);
            Assert.AreEqual("Test description", result.Text);
            Assert.AreEqual("test alternate", result.Title);
            Assert.AreEqual(LinkType.JavaScript, result.Type);
            Assert.AreEqual("javascript:alert('hello world');", result.Url);
        }
        public void GetField_FieldContainsMediaLinkMissingItem_ReturnsNotSetLink()
        {
            //Assign
            var mapper = new SitecoreFieldLinkMapper();
            var fieldValue =
                "<link text=\"Test description\" linktype=\"media\" url=\"/Files/20121222_001405\" title=\"test alternative\" class=\"testClass\" target=\"_blank\" id=\"{11111111-CF15-4067-A3F4-85148606F9CD}\" />";

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

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

            //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(new Guid("{11111111-CF15-4067-A3F4-85148606F9CD}"), result.TargetId);
            Assert.AreEqual("Test description", result.Text);
            Assert.AreEqual("test alternative", result.Title);
            Assert.AreEqual(LinkType.Media, result.Type);
            Assert.AreEqual("", result.Url);
        }
        public void GetField_FieldContainsInternalMissingItem_ReturnsNotSetLink()
        {
            //Assign
            var mapper = new SitecoreFieldLinkMapper();
            var fieldValue =
                "<link text=\"Test description\" linktype=\"internal\" url=\"/Tests/DataMappers/SitecoreFieldLinkMapper/Target.aspx\" anchor=\"testAnchor\" querystring=\"q=s\" title=\"test alternative\" class=\"testClass\" target=\"testTarget\" id=\"{11111111-3B6F-4F5F-A5C2-FD2B9D5A47A0}\" />";

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

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

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

            //Assert
            Assert.AreEqual("testAnchor", result.Anchor);
            Assert.AreEqual("testClass", result.Class);
            Assert.AreEqual("q=s", result.Query);
            Assert.AreEqual("testTarget", result.Target);
            Assert.AreEqual(new Guid("{11111111-3B6F-4F5F-A5C2-FD2B9D5A47A0}"), result.TargetId);
            Assert.AreEqual("Test description", result.Text);
            Assert.AreEqual("test alternative", result.Title);
            Assert.AreEqual(LinkType.Internal, result.Type);
            Assert.AreEqual("", result.Url);
        }
        public void GetField_FieldContainsInternalAbsoluteUrl_ReturnsInternalLink()
        {
            //Assign
            var mapper = new SitecoreFieldLinkMapper();
            var fieldValue =
                "<link text=\"Test description\" linktype=\"internal\" url=\"/Tests/DataMappers/SitecoreFieldLinkMapper/Target.aspx\" anchor=\"testAnchor\" querystring=\"q=s\" title=\"test alternative\" class=\"testClass\" target=\"testTarget\" id=\"{1AE37F34-3B6F-4F5F-A5C2-FD2B9D5A47A0}\" />";

            Sitecore.Context.Site = null;


            var item = Database.GetItem("/sitecore/content/Tests/DataMappers/SitecoreFieldLinkMapper/GetField");
            var field = item.Fields[FieldName];
            var config = new SitecoreFieldConfiguration();
            config.UrlOptions = SitecoreInfoUrlOptions.LanguageEmbeddingNever;
            
            using (new ItemEditing(item, true))
            {
                field.Value = fieldValue;
            }

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

            //Assert
            Assert.AreEqual("testAnchor", result.Anchor);
            Assert.AreEqual("testClass", result.Class);
            Assert.AreEqual("q=s", result.Query);
            Assert.AreEqual("testTarget", result.Target);
            Assert.AreEqual(new Guid("{1AE37F34-3B6F-4F5F-A5C2-FD2B9D5A47A0}"), result.TargetId);
            Assert.AreEqual("Test description", result.Text);
            Assert.AreEqual("test alternative", result.Title);
            Assert.AreEqual(LinkType.Internal, result.Type);
            Assert.AreEqual("/sitecore/content/Tests/DataMappers/SitecoreFieldLinkMapper/Target.aspx", result.Url);
        }
        public void GetField_FieldContainsMediaLinkMissingItem_ReturnsNotSetLink()
        {
            //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=\"{11111111-CF15-4067-A3F4-85148606F9CD}\" />";

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

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

                //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(new Guid("{11111111-CF15-4067-A3F4-85148606F9CD}"), result.TargetId);
                Assert.AreEqual("Test description", result.Text);
                Assert.AreEqual("test alternative", result.Title);
                Assert.AreEqual(LinkType.Media, result.Type);
                Assert.AreEqual("", result.Url);
            }
        }
        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 GetField_FieldContainsInternalMissingItem_ReturnsNotSetLink()
        {
            //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=\"internal\" url=\"/Target.aspx\" anchor=\"testAnchor\" querystring=\"q=s\" title=\"test alternative\" class=\"testClass\" target=\"testTarget\" id=\"{11111111-3B6F-4F5F-A5C2-FD2B9D5A47A0}\" />";

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

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

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

                //Assert
                Assert.AreEqual("testAnchor", result.Anchor);
                Assert.AreEqual("testClass", result.Class);
                Assert.AreEqual("q=s", result.Query);
                Assert.AreEqual("testTarget", result.Target);
                Assert.AreEqual(new Guid("{11111111-3B6F-4F5F-A5C2-FD2B9D5A47A0}"), result.TargetId);
                Assert.AreEqual("Test description", result.Text);
                Assert.AreEqual("test alternative", result.Title);
                Assert.AreEqual(LinkType.Internal, result.Type);
                Assert.AreEqual("", result.Url);
            }
        }
        [Category("Exclude82")] //Required FakeDB fix
        public void GetField_FieldContainsInternalAbsoluteUrl_ReturnsInternalLink()
        {
            //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=\"internal\" url=\"/Tests/DataMappers/SitecoreFieldLinkMapper/Target.aspx\" anchor=\"testAnchor\" querystring=\"q=s\" title=\"test alternative\" class=\"testClass\" target=\"testTarget\" id=\"{0}\" />"
                    .Formatted(targetId);

                Sitecore.Context.Site = null;


                var item = database.GetItem("/sitecore/content/Target");
                var field = item.Fields[fieldName];
                var config = new SitecoreFieldConfiguration();
                config.UrlOptions = SitecoreInfoUrlOptions.LanguageEmbeddingNever;

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

                Sitecore.Links.LinkProvider provider =
                   Substitute.For<Sitecore.Links.LinkProvider>();
                provider
                  .GetItemUrl(item, Arg.Is<Sitecore.Links.UrlOptions>(x=>x.LanguageEmbedding == LanguageEmbedding.Never))
                  .Returns("/target.aspx");


                using (new Sitecore.FakeDb.Links.LinkProviderSwitcher(provider))
                {

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

                    //Assert
                    Assert.AreEqual("testAnchor", result.Anchor);
                    Assert.AreEqual("testClass", result.Class);
                    Assert.AreEqual("q=s", result.Query);
                    Assert.AreEqual("testTarget", result.Target);
                    Assert.AreEqual(targetId.Guid, result.TargetId);
                    Assert.AreEqual("Test description", result.Text);
                    Assert.AreEqual("test alternative", result.Title);
                    Assert.AreEqual(LinkType.Internal, result.Type);
                    Assert.AreEqual("/target.aspx",
                        result.Url);
                }
            }
        }