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);
        }
      }
    }
Пример #2
0
        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);
                }
            }
        }
Пример #3
0
        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);
            }
        }