Ejemplo n.º 1
0
        public void FindItemsByTemplateOrDerivedTemplate_ItemsBasedOnTemplate()
        {
            var templateId = ID.NewID;

            using (var db = new Db()
            {
                new DbItem("blog1", ID.NewID, templateId),
                new DbItem("blog2", ID.NewID, templateId),
                new DbItem("normal item")
            })
            {
                var root     = db.GetItem("/sitecore/content");
                var template = root.Database.GetTemplate(templateId);

                var blog1 = db.GetItem("/sitecore/content/blog1");
                var blog2 = db.GetItem("/sitecore/content/blog2");

                // Setup LinkDatabase
                var linkDb = Mock.Of <Sitecore.Links.LinkDatabase>(x =>
                                                                   x.GetReferrers(template) == new[]
                {
                    new ItemLink("master", blog1.ID, ID.Null, "master", templateId, templateId.ToString()),
                    new ItemLink("master", blog2.ID, ID.Null, "master", templateId, templateId.ToString()),
                });

                using (new LinkDatabaseSwitcher(linkDb))
                {
                    var result = ItemExtensions.FindItemsByTemplateOrDerivedTemplate(root, template);

                    Assert.That(result.Length, Is.EqualTo(2));
                    Assert.That(result[0].Name, Is.EqualTo("blog1"));
                    Assert.That(result[1].Name, Is.EqualTo("blog2"));
                }
            }
        }