2

When using FakeDb, and I set the full path of a DbItem to go under the media library, the path gets changed to the /sitecore/content/home/image-item.

    //Method starts here
    public void MediaItemPath()
    {
        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())
        {
            var mediaFakeItem = new Sitecore.FakeDb.DbItem("my-image", mediaItemId);
            mediaFakeItem.FullPath = "/sitecore/media library/my-image";
            db.Add(mediaFakeItem);
            Sitecore.Data.Items.Item mediaItem = db.GetItem(mediaItemId);

            Assert.AreEqual("/sitecore/media library/my-image", mediaFakeItem.FullPath);

        }
    }

How can I add an item to the media library?

  • Do you have these entries in your APP_Config '<mediaLibrary> <mediaProvider type="Sitecore.Fakes.FakeMediaProvider, Sitecore.Fakes"/> </mediaLibrary>' – Balaji Kuppuswamy Feb 28 '17 at 2:45
  • It's a bug. – Serhii Shushliapin Feb 28 '17 at 7:15
4

The API is not quite obvious here. By default, all the items are added to the content. If you need to add an item to the Media Library, you should set the fakeItem.ParentID property:

    var mediaFakeItem = new DbItem("my-image")
    {
      ParentID = Sitecore.ItemIDs.MediaLibraryRoot
    };

Since DbItem has got mutable ParentID and FullPath properties, it's quite challenging to keep them in sync (e.g. when you change the FullPath only). I created a FakeDb issue to address it (if possible without introducing breaking changes).

| improve this answer | |
0

Please make sure you have media config in App.config

<mediaLibrary>
      <!-- MEDIA PROVIDER -->
      <mediaProvider type="Sitecore.FakeDb.Resources.Media.FakeMediaProvider, Sitecore.FakeDb" />
    </mediaLibrary>

Also try this below code :

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);
        }
      }
    }

Source : https://github.com/sergeyshushlyapin/Sitecore.FakeDb/wiki/Media

| improve this answer | |
  • 1
    I did try that method; however, the path of the mediaItem is still under "/sitecore/content/my-image." For my purposes, I cared more about the actual location of the mediaItem rather than the Url of the media item. I had a Sitecore Query that was checking under the media library. – user1619 Feb 28 '17 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.