public void HowToMockAuthorizationProvider()
    {
      // create sample user
      var user = Sitecore.Security.Accounts.User.FromName(@"extranet\John", true);

      using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
        {
          new Sitecore.FakeDb.DbItem("home")
        })
      {
        Sitecore.Data.Items.Item home = db.GetItem("/sitecore/content/home");

        // configure authorization provider mock to deny item read for the user
        var provider =
          Substitute.For<Sitecore.Security.AccessControl.AuthorizationProvider>();

        provider
          .GetAccess(home, user, Sitecore.Security.AccessControl.AccessRight.ItemRead)
          .Returns(new Sitecore.FakeDb.Security.AccessControl.DenyAccessResult());

        // switch the authorization provider
        using (new Sitecore.FakeDb.Security.AccessControl.AuthorizationSwitcher(provider))
        {
          // check the user cannot read the item
          bool canRead =
            Sitecore.Security.AccessControl.AuthorizationManager.IsAllowed(
              home,
              Sitecore.Security.AccessControl.AccessRight.ItemRead,
              user);

          Xunit.Assert.False(canRead);
        }
      }
    }
        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.Empty(Sitecore.Globals.LinkDatabase.GetReferrers(source));

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

                    const int expected = 2;
                    Xunit.Assert.Equal(referrers.Count(), expected);
                    Xunit.Assert.Single(referrers.Where(r => r.SourceItemID == clone.ID &&
                                                        r.TargetItemID == source.ID));
                    Xunit.Assert.Single(referrers.Where(r => r.SourceItemID == alias.ID &&
                                                        r.TargetItemID == source.ID));
                }

                // link database is clean again
                Xunit.Assert.Empty(Sitecore.Globals.LinkDatabase.GetReferrers(source));
            }
        }
        public void HowToMockAuthorizationProvider()
        {
            // create sample user
            var user = Sitecore.Security.Accounts.User.FromName(@"extranet\John", true);

            using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
            {
                new Sitecore.FakeDb.DbItem("home")
            })
            {
                Sitecore.Data.Items.Item home = db.GetItem("/sitecore/content/home");

                // configure authorization provider mock to deny item read for the user
                var provider =
                    Substitute.For <Sitecore.Security.AccessControl.AuthorizationProvider>();

                provider
                .GetAccess(home, user, Sitecore.Security.AccessControl.AccessRight.ItemRead)
                .Returns(new Sitecore.FakeDb.Security.AccessControl.DenyAccessResult());

                // switch the authorization provider
                using (new Sitecore.FakeDb.Security.AccessControl.AuthorizationSwitcher(provider))
                {
                    // check the user cannot read the item
                    bool canRead =
                        Sitecore.Security.AccessControl.AuthorizationManager.IsAllowed(
                            home,
                            Sitecore.Security.AccessControl.AccessRight.ItemRead,
                            user);

                    Xunit.Assert.False(canRead);
                }
            }
        }
        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);
                }
            }
        }
        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);
                }
            }
        }
        public void HowToSetAndGetBlobStream()
        {
            // arrange
            var stream = new System.IO.MemoryStream();

            using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
            {
                new Sitecore.FakeDb.DbItem("home")
                {
                    new Sitecore.FakeDb.DbField("field")
                }
            })
            {
                Sitecore.Data.Items.Item   item  = db.GetItem("/sitecore/content/home");
                Sitecore.Data.Fields.Field field = item.Fields["field"];

                using (new Sitecore.Data.Items.EditContext(item))
                {
                    // act
                    field.SetBlobStream(stream);
                }

                // assert
                Xunit.Assert.Equal(stream.ToArray(),
                                   ((System.IO.MemoryStream)field.GetBlobStream()).ToArray());
            }
        }
        public void HowToUnitTestItemSecurityWithFakeProvider()
        {
            // create sample item
            using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
            {
                new Sitecore.FakeDb.DbItem("home")
            })
            {
                Sitecore.Data.Items.Item home = db.GetItem("/sitecore/content/home");

                // call your business logic that changes the item security, e.g. denies Read
                // for Editors
                var account         = Sitecore.Security.Accounts.Role.FromName(@"sitecore\Editors");
                var accessRight     = Sitecore.Security.AccessControl.AccessRight.ItemRead;
                var propagationType = Sitecore.Security.AccessControl.PropagationType.Entity;
                var permission      = Sitecore.Security.AccessControl.AccessPermission.Deny;

                Sitecore.Security.AccessControl.AccessRuleCollection rules =
                    new Sitecore.Security.AccessControl.AccessRuleCollection
                {
                    Sitecore.Security.AccessControl.AccessRule.Create
                        (account, accessRight, propagationType, permission)
                };
                Sitecore.Security.AccessControl.AuthorizationManager.SetAccessRules(home, rules);

                // check the account cannot read the item
                Xunit.Assert.False(home.Security.CanRead(account));
            }
        }
        public void HowToMockContentSearchLogic()
        {
            var index = Substitute.For <Sitecore.ContentSearch.ISearchIndex>();

            // don't forget to clean up.
            Sitecore.ContentSearch
            .ContentSearchManager.SearchConfiguration.Indexes["my_index"] = index;

            using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
            {
                new Sitecore.FakeDb.DbItem("home")
            })
            {
                // configure a search result item behavior.
                var searchResultItem =
                    Substitute.For <Sitecore.ContentSearch.SearchTypes.SearchResultItem>();

                var expectedItem = db.GetItem("/sitecore/content/home");
                searchResultItem.GetItem().Returns(expectedItem);

                // configure a search ndex behavior.
                index.CreateSearchContext()
                .GetQueryable <Sitecore.ContentSearch.SearchTypes.SearchResultItem>()
                .Returns((new[] { searchResultItem }).AsQueryable());

                // get the item from the search index and check the expectations.
                Sitecore.Data.Items.Item actualItem =
                    index.CreateSearchContext()
                    .GetQueryable <Sitecore.ContentSearch.SearchTypes.SearchResultItem>()
                    .Single()
                    .GetItem();

                Xunit.Assert.Equal(expectedItem, actualItem);
            }
        }
    public void HowToCreateMultilingualItem()
    {
      using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
        {
          new Sitecore.FakeDb.DbItem("home")
            {
              new Sitecore.FakeDb.DbField("Title") { { "en", "Hello!" }, { "da", "Hej!" } }
            }
        })
      {
        Sitecore.Data.Items.Item homeEn = db.GetItem("/sitecore/content/home", "en");
        Xunit.Assert.Equal("Hello!", homeEn["Title"]);

        Sitecore.Data.Items.Item homeDa = db.GetItem("/sitecore/content/home", "da");
        Xunit.Assert.Equal("Hej!", homeDa["Title"]);
      }
    }
    public void HowToCreateMultilingualItem()
    {
      using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
        {
          new Sitecore.FakeDb.DbItem("home")
            {
              new Sitecore.FakeDb.DbField("Title") { { "en", "Hello!" }, { "da", "Hej!" } }
            }
        })
      {
        Sitecore.Data.Items.Item homeEn = db.GetItem("/sitecore/content/home", "en");
        Xunit.Assert.Equal("Hello!", homeEn["Title"]);

        Sitecore.Data.Items.Item homeDa = db.GetItem("/sitecore/content/home", "da");
        Xunit.Assert.Equal("Hej!", homeDa["Title"]);
      }
    }
 public void HowToCreateSimpleItem()
 {
   using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
     {
       new Sitecore.FakeDb.DbItem("Home") { { "Title", "Welcome!" } }
     })
   {
     Sitecore.Data.Items.Item home = db.GetItem("/sitecore/content/home");
     Xunit.Assert.Equal("Welcome!", home["Title"]);
   }
 }
 public void HowToCreateItemUnderSystem()
 {
   using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
     {
       new Sitecore.FakeDb.DbItem("Home") { ParentID = Sitecore.ItemIDs.SystemRoot }
     })
   {
     Sitecore.Data.Items.Item home = db.GetItem("/sitecore/system/home");
     Xunit.Assert.Equal("home", home.Key);
   }
 }
 public void HowToCreateSimpleItem()
 {
   using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
     {
       new Sitecore.FakeDb.DbItem("Home") { { "Title", "Welcome!" } }
     })
   {
     Sitecore.Data.Items.Item home = db.GetItem("/sitecore/content/home");
     Xunit.Assert.Equal("Welcome!", home["Title"]);
   }
 }
 public void HowToCreateItemUnderSystem()
 {
   using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
     {
       new Sitecore.FakeDb.DbItem("Home") { ParentID = Sitecore.ItemIDs.SystemRoot }
     })
   {
     Sitecore.Data.Items.Item home = db.GetItem("/sitecore/system/home");
     Xunit.Assert.Equal("home", home.Key);
   }
 }
        public void HowToCreateVersionedItem()
        {
            using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
            {
                new Sitecore.FakeDb.DbItem("home")
                {
                    new Sitecore.FakeDb.DbField("Title")
                    {
                        { "en", 1, "Hello!" },
                        { "en", 2, "Welcome!" }
                    }
                }
            })
            {
                Sitecore.Data.Items.Item home1 = db.GetItem("/sitecore/content/home", "en", 1);
                Xunit.Assert.Equal("Hello!", home1["Title"]);

                Sitecore.Data.Items.Item home2 = db.GetItem("/sitecore/content/home", "en", 2);
                Xunit.Assert.Equal("Welcome!", home2["Title"]);
            }
        }
    public void HowToConfigureItemAccess()
    {
      using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
        {
          // set Access.CanRead to False
          new Sitecore.FakeDb.DbItem("home") { Access = { CanRead = false } }
        })
      {
        Sitecore.Data.Items.Item item = db.GetItem("/sitecore/content/home");

        // item is null because read is denied
        Xunit.Assert.Null(item);
      }
    }
 public void HowToDeserializeItem()
 {
   using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
     {
       new Sitecore.FakeDb.Serialization.DsDbTemplate(
         "/sitecore/templates/Sample/Sample Item"),
       new Sitecore.FakeDb.Serialization.DsDbItem(
         "/sitecore/content/home", true)
     })
   {
     var home = db.GetItem("/sitecore/content/home");
     Assert.Equal("Sitecore", home["Title"]);
   }
 }
 public void HowToDeserializeItem()
 {
     using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
     {
         new Sitecore.FakeDb.Serialization.DsDbTemplate(
             "/sitecore/templates/Sample/Sample Item"),
         new Sitecore.FakeDb.Serialization.DsDbItem(
             "/sitecore/content/home", true)
     })
     {
         var home = db.GetItem("/sitecore/content/home");
         Assert.Equal("Sitecore", home["Title"]);
     }
 }
    public void HowDoICreateAnItemOfSpecificTemplate()
    {
      Sitecore.Data.ID templateId = Sitecore.Data.ID.NewID;

      using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
        {
          new Sitecore.FakeDb.DbTemplate("products", templateId) { "Name" },
          new Sitecore.FakeDb.DbItem("Apple") { TemplateID = templateId }
        })
      {
        Sitecore.Data.Items.Item item = db.GetItem("/sitecore/content/apple");
        Assert.Equal(templateId, item.TemplateID);
        Assert.NotNull(item.Fields["Name"]);
      }
    }
    public void HowToCreateItemWithSpecificTemplate()
    {
      Sitecore.Data.ID templateId = Sitecore.Data.ID.NewID;

      using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
        {
          new Sitecore.FakeDb.DbTemplate("products", templateId) { "Name" },
          new Sitecore.FakeDb.DbItem("Apple") { TemplateID = templateId }
        })
      {
        Sitecore.Data.Items.Item item = db.GetItem("/sitecore/content/apple");

        Xunit.Assert.Equal(templateId, item.TemplateID);
        Xunit.Assert.NotNull(item.Fields["Name"]);
      }
    }
 public void HowDoICreateAnItemHierarchy()
 {
   using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
     {
       new Sitecore.FakeDb.DbItem("Articles")
         {
           new Sitecore.FakeDb.DbItem("Getting Started"),
           new Sitecore.FakeDb.DbItem("Troubleshooting")
         }
     })
   {
     Sitecore.Data.Items.Item articles = db.GetItem("/sitecore/content/Articles");
     Assert.NotNull(articles["Getting Started"]);
     Assert.NotNull(articles["Troubleshooting"]);
   }
 }
Example #22
0
        public void HowToUnitTestItemSecurityWithMockedProvider()
        {
            // create sample item
            using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
            {
                new Sitecore.FakeDb.DbItem("home")
            })
            {
                Sitecore.Data.Items.Item home = db.GetItem("/sitecore/content/home");

                // substitute the authorization provider
                var provider =
                    Substitute.For <Sitecore.Security.AccessControl.AuthorizationProvider>();

#pragma warning disable 618
                using (new Sitecore.FakeDb.Security.AccessControl.AuthorizationSwitcher(provider))
#pragma warning restore 618
                {
                    // call your business logic that changes the item security, e.g. denies Read
                    // for Editors
                    var account         = Sitecore.Security.Accounts.Role.FromName(@"sitecore\Editors");
                    var accessRight     = Sitecore.Security.AccessControl.AccessRight.ItemRead;
                    var propagationType = Sitecore.Security.AccessControl.PropagationType.Entity;
                    var permission      = Sitecore.Security.AccessControl.AccessPermission.Deny;

                    Sitecore.Security.AccessControl.AccessRuleCollection rules =
                        new Sitecore.Security.AccessControl.AccessRuleCollection
                    {
                        Sitecore.Security.AccessControl.AccessRule.Create
                            (account, accessRight, propagationType, permission)
                    };
                    Sitecore.Security.AccessControl.AuthorizationManager.SetAccessRules(home, rules);

                    // check the provider is called with proper arguments
                    provider
                    .Received()
                    .SetAccessRules(
                        home,
                        NSubstitute.Arg.Is <Sitecore.Security.AccessControl.AccessRuleCollection>(
                            r => r[0].Account.Name == @"sitecore\Editors" &&
                            r[0].AccessRight.Name == "item:read" &&
                            r[0].PropagationType.ToString() == "Entity" &&
                            r[0].SecurityPermission.ToString() == "DenyAccess"));
                }
            }
        }
        public void HowToCreateItemHierarchy()
        {
            using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
            {
                new Sitecore.FakeDb.DbItem("Articles")
                {
                    new Sitecore.FakeDb.DbItem("Getting Started"),
                    new Sitecore.FakeDb.DbItem("Troubleshooting")
                }
            })
            {
                Sitecore.Data.Items.Item articles = db.GetItem("/sitecore/content/Articles");

                Xunit.Assert.NotNull(articles.Children["Getting Started"]);
                Xunit.Assert.NotNull(articles.Children["Troubleshooting"]);
            }
        }
        public void HowToCreateLinkFieldUsingRawValue()
        {
            using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
            {
                new Sitecore.FakeDb.DbItem("home")
                {
                    { "link", "<link linktype=\"external\" url=\"http://google.com\" />" }
                }
            })
            {
                var item = db.GetItem("/sitecore/content/home");

                var linkField = (Sitecore.Data.Fields.LinkField)item.Fields["link"];

                Xunit.Assert.Equal("external", linkField.LinkType);
                Xunit.Assert.Equal("http://google.com", linkField.Url);
            }
        }
    public void HowToCreateTemplateWithStandardValues()
    {
      var templateId = new Sitecore.Data.TemplateID(Sitecore.Data.ID.NewID);

      using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
        {
          // create template with field Title and standard value $name
          new Sitecore.FakeDb.DbTemplate("Sample", templateId) { { "Title", "$name" } }
        })
      {
        // add item based on the template to the content root
        Sitecore.Data.Items.Item contentRoot = db.GetItem(Sitecore.ItemIDs.ContentRoot);
        Sitecore.Data.Items.Item item = contentRoot.Add("Home", templateId);

        // the Title field is set to 'Home'
        Xunit.Assert.Equal("Home", item["Title"]);
      }
    }
        public void HowToCreateLinkFieldUsingDbLinkField()
        {
            using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
            {
                new Sitecore.FakeDb.DbItem("home")
                {
                    new Sitecore.FakeDb.DbLinkField("link")
                    {
                        LinkType = "external", Url = "http://google.com"
                    }
                }
            })
            {
                var item = db.GetItem("/sitecore/content/home");

                var linkField = (Sitecore.Data.Fields.LinkField)item.Fields["link"];

                Xunit.Assert.Equal("external", linkField.LinkType);
                Xunit.Assert.Equal("http://google.com", linkField.Url);
            }
        }
    public void HowToSetAndGetBlobStream()
    {
      // arrange
      var stream = new System.IO.MemoryStream();

      using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
        {
          new Sitecore.FakeDb.DbItem("home")
            {
              new Sitecore.FakeDb.DbField("field")
            }
        })
      {
        Sitecore.Data.Items.Item item = db.GetItem("/sitecore/content/home");
        Sitecore.Data.Fields.Field field = item.Fields["field"];

        using (new Sitecore.Data.Items.EditContext(item))
        {
          // act
          field.SetBlobStream(stream);
        }

        // assert
        Xunit.Assert.Same(stream, field.GetBlobStream());
      }
    }
    public void HowToMockContentSearchLogic()
    {
      var index = Substitute.For<Sitecore.ContentSearch.ISearchIndex>();

      // don't forget to clean up.
      Sitecore.ContentSearch
        .ContentSearchManager.SearchConfiguration.Indexes["my_index"] = index;

      using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
        {
          new Sitecore.FakeDb.DbItem("home")
        })
      {
        // configure a search result item behavior.
        var searchResultItem =
          Substitute.For<Sitecore.ContentSearch.SearchTypes.SearchResultItem>();

        var expectedItem = db.GetItem("/sitecore/content/home");
        searchResultItem.GetItem().Returns(expectedItem);

        // configure a search ndex behavior.
        index.CreateSearchContext()
          .GetQueryable<Sitecore.ContentSearch.SearchTypes.SearchResultItem>()
          .Returns((new[] { searchResultItem }).AsQueryable());

        // get the item from the search index and check the expectations.
        Sitecore.Data.Items.Item actualItem =
          index.CreateSearchContext()
            .GetQueryable<Sitecore.ContentSearch.SearchTypes.SearchResultItem>()
            .Single()
            .GetItem();

        Xunit.Assert.Equal(expectedItem, actualItem);
      }
    }
    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);
        }
      }
    }
    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);
        }
      }
    }
    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);
      }
    }
    public void HowDoIMockContentSearchLogic()
    {
      try
      {
        var index = Substitute.For<Sitecore.ContentSearch.ISearchIndex>();
        Sitecore.ContentSearch.ContentSearchManager.SearchConfiguration.Indexes.Add("my_index", index);

        using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db { new Sitecore.FakeDb.DbItem("home") })
        {
          var searchResultItem = Substitute.For<Sitecore.ContentSearch.SearchTypes.SearchResultItem>();
          searchResultItem.GetItem().Returns(db.GetItem("/sitecore/content/home"));
          index.CreateSearchContext().GetQueryable<Sitecore.ContentSearch.SearchTypes.SearchResultItem>().Returns((new[] { searchResultItem }).AsQueryable());

          Sitecore.Data.Items.Item result = index.CreateSearchContext().GetQueryable<Sitecore.ContentSearch.SearchTypes.SearchResultItem>().Single().GetItem();

          Assert.Equal("/sitecore/content/home", result.Paths.FullPath);
        }
      }
      finally
      {
        Sitecore.ContentSearch.ContentSearchManager.SearchConfiguration.Indexes.Remove("my_index");
      }
    }
    public void HowToUnitTestItemSecurityWithFakeProvider()
    {
      // create sample item
      using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
        {
          new Sitecore.FakeDb.DbItem("home")
        })
      {
        Sitecore.Data.Items.Item home = db.GetItem("/sitecore/content/home");

        // call your business logic that changes the item security, e.g. denies Read
        // for Editors
        var account = Sitecore.Security.Accounts.Role.FromName(@"sitecore\Editors");
        var accessRight = Sitecore.Security.AccessControl.AccessRight.ItemRead;
        var propagationType = Sitecore.Security.AccessControl.PropagationType.Entity;
        var permission = Sitecore.Security.AccessControl.AccessPermission.Deny;

        Sitecore.Security.AccessControl.AccessRuleCollection rules =
          new Sitecore.Security.AccessControl.AccessRuleCollection
              {
                Sitecore.Security.AccessControl.AccessRule.Create
                  (account, accessRight, propagationType, permission)
              };
        Sitecore.Security.AccessControl.AuthorizationManager.SetAccessRules(home, rules);

        // check the account cannot read the item
        Xunit.Assert.False(home.Security.CanRead(account));
      }
    }
    public void HowToUnitTestItemSecurityWithMockedProvider()
    {
      // create sample item
      using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
        {
          new Sitecore.FakeDb.DbItem("home")
        })
      {
        Sitecore.Data.Items.Item home = db.GetItem("/sitecore/content/home");

        // substitute the authorization provider
        var provider =
          Substitute.For<Sitecore.Security.AccessControl.AuthorizationProvider>();

        using (new Sitecore.FakeDb.Security.AccessControl.AuthorizationSwitcher(provider))
        {
          // call your business logic that changes the item security, e.g. denies Read
          // for Editors
          var account = Sitecore.Security.Accounts.Role.FromName(@"sitecore\Editors");
          var accessRight = Sitecore.Security.AccessControl.AccessRight.ItemRead;
          var propagationType = Sitecore.Security.AccessControl.PropagationType.Entity;
          var permission = Sitecore.Security.AccessControl.AccessPermission.Deny;

          Sitecore.Security.AccessControl.AccessRuleCollection rules =
            new Sitecore.Security.AccessControl.AccessRuleCollection
              {
                Sitecore.Security.AccessControl.AccessRule.Create
                  (account, accessRight, propagationType, permission)
              };
          Sitecore.Security.AccessControl.AuthorizationManager.SetAccessRules(home, rules);

          // check the provider is called with proper arguments
          provider
            .Received()
            .SetAccessRules(
              home,
              NSubstitute.Arg.Is<Sitecore.Security.AccessControl.AccessRuleCollection>(
                r => r[0].Account.Name == @"sitecore\Editors"
                  && r[0].AccessRight.Name == "item:read"
                  && r[0].PropagationType.ToString() == "Entity"
                  && r[0].SecurityPermission.ToString() == "DenyAccess"));
        }
      }
    }
    public void HowToCreateLinkFieldUsingDbLinkField()
    {
      using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
        {
          new Sitecore.FakeDb.DbItem("home")
            {
              new Sitecore.FakeDb.DbLinkField("link")
                {
                  LinkType = "external", Url = "http://google.com"
                }
            }
        })
      {
        var item = db.GetItem("/sitecore/content/home");

        var linkField = (Sitecore.Data.Fields.LinkField)item.Fields["link"];

        Xunit.Assert.Equal("external", linkField.LinkType);
        Xunit.Assert.Equal("http://google.com", linkField.Url);
      }
    }
    public void HowToCreateLinkFieldUsingRawValue()
    {
      using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
        {
          new Sitecore.FakeDb.DbItem("home")
            {
              { "link", "<link linktype=\"external\" url=\"http://google.com\" />" }
            }
        })
      {
        var item = db.GetItem("/sitecore/content/home");

        var linkField = (Sitecore.Data.Fields.LinkField)item.Fields["link"];

        Xunit.Assert.Equal("external", linkField.LinkType);
        Xunit.Assert.Equal("http://google.com", linkField.Url);
      }
    }
    public void HowToCreateVersionedItem()
    {
      using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
        {
          new Sitecore.FakeDb.DbItem("home")
            {
              new Sitecore.FakeDb.DbField("Title")
                {
                  { "en", 1, "Hello!" },
                  { "en", 2, "Welcome!" }
                }
            }
        })
      {
        Sitecore.Data.Items.Item home1 = db.GetItem("/sitecore/content/home", "en", 1);
        Xunit.Assert.Equal("Hello!", home1["Title"]);

        Sitecore.Data.Items.Item home2 = db.GetItem("/sitecore/content/home", "en", 2);
        Xunit.Assert.Equal("Welcome!", home2["Title"]);
      }
    }
    public void HowToConfigureItemAccess()
    {
      using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
        {
          // set Access.CanRead to False
          new Sitecore.FakeDb.DbItem("home") { Access = { CanRead = false } }
        })
      {
        Sitecore.Data.Items.Item item = db.GetItem("/sitecore/content/home");

        // item is null because read is denied
        Xunit.Assert.Null(item);
      }
    }
    public void HowDoICreateATemplateWithStandardValues()
    {
      var templateId = Sitecore.Data.ID.NewID;

      using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
        {
          new Sitecore.FakeDb.DbTemplate("sample", templateId) { { "Title", "$name"} }
        })
      {
        var root = db.GetItem(Sitecore.ItemIDs.ContentRoot);
        var item = Sitecore.Data.Managers.ItemManager.CreateItem("Home", root, templateId);
        Assert.Equal("Home", item["Title"]);
      }
    }
    public void HowToCreateTemplateWithStandardValues()
    {
      var templateId = new Sitecore.Data.TemplateID(Sitecore.Data.ID.NewID);

      using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
        {
          // create template with field Title and standard value $name
          new Sitecore.FakeDb.DbTemplate("Sample", templateId) { { "Title", "$name" } }
        })
      {
        // add item based on the template to the content root
        Sitecore.Data.Items.Item contentRoot = db.GetItem(Sitecore.ItemIDs.ContentRoot);
        Sitecore.Data.Items.Item item = contentRoot.Add("Home", templateId);

        // the Title field is set to 'Home'
        Xunit.Assert.Equal("Home", item["Title"]);
      }
    }