Skip to content
sergeyshushlyapin edited this page Sep 23, 2015 · 4 revisions

Switching the LinkDatabase

[Fact]
public void HowToWorkWithLinkDatabase()
{
  // arrange your database and items
  Sitecore.Data.ID sourceId = Sitecore.Data.ID.NewID;
  Sitecore.Data.ID aliasId = Sitecore.Data.ID.NewID;
  Sitecore.Data.ID linkedItemId = Sitecore.Data.ID.NewID;

  using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
  {
    new Sitecore.FakeDb.DbItem("source", sourceId),
    new Sitecore.FakeDb.DbItem("clone"),
    new Sitecore.FakeDb.DbItem("alias", aliasId, Sitecore.TemplateIDs.Alias)
    {
      new Sitecore.FakeDb.DbField("Linked item", linkedItemId)
    }
  })
  {
    // arrange desired LinkDatabase behavior
    var behavior = Substitute.For<Sitecore.Links.LinkDatabase>();

    Sitecore.Data.Items.Item source = db.GetItem("/sitecore/content/source");
    Sitecore.Data.Items.Item alias = db.GetItem("/sitecore/content/alias");
    Sitecore.Data.Items.Item clone = db.GetItem("/sitecore/content/clone");

    string sourcePath = source.Paths.FullPath;
    behavior.GetReferrers(source).Returns(new[]
    {
      new Sitecore.Links.ItemLink(alias, linkedItemId, source, sourcePath),
      new Sitecore.Links.ItemLink(clone, Sitecore.FieldIDs.Source, source, sourcePath)
    });

    // link database is clean
    Xunit.Assert.Equal(Sitecore.Globals.LinkDatabase.GetReferrers(source).Count(), 0);

    using (new Sitecore.FakeDb.Links.LinkDatabaseSwitcher(behavior))
    {
      Sitecore.Links.ItemLink[] referrers =
        Sitecore.Globals.LinkDatabase.GetReferrers(source);

      Xunit.Assert.Equal(referrers.Count(), 2);
      Xunit.Assert.Equal(referrers.Count(r => r.SourceItemID == clone.ID
        && r.TargetItemID == source.ID), 1);
      Xunit.Assert.Equal(referrers.Count(r => r.SourceItemID == alias.ID
        && r.TargetItemID == source.ID), 1);
    }

    // link database is clean again
    Xunit.Assert.Equal(Sitecore.Globals.LinkDatabase.GetReferrers(source).Count(), 0);
  }
}

Mocking the LinkProvider

[Fact]
public void HowToSwitchLinkProvider()
{
  // arrange
  using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
    {
      new Sitecore.FakeDb.DbItem("home")
    })
  {
    Sitecore.Data.Items.Item home = db.GetItem("/sitecore/content/home");

    Sitecore.Links.LinkProvider provider = 
      Substitute.For<Sitecore.Links.LinkProvider>();
    provider
      .GetItemUrl(home, Arg.Is<Sitecore.Links.UrlOptions>(
        x => x.AlwaysIncludeServerUrl))
      .Returns("http://myawesomeurl.com");

    using (new Sitecore.FakeDb.Links.LinkProviderSwitcher(provider))
    {
      // act
      var result = Sitecore.Links.LinkManager.GetItemUrl(home,
        new Sitecore.Links.UrlOptions { AlwaysIncludeServerUrl = true });

      // assert
      Xunit.Assert.Equal("http://myawesomeurl.com", result);
    }
  }
}

Important:

For Sitecore 7.2 you should change the default link provider to "switcher":
<linkManager defaultProvider="switcher">

Clone this wiki locally