using System; using System.Linq; using System.Xml; using NUnit.Framework; using Umbraco.Core; using Umbraco.Core.Configuration; using Umbraco.Tests.PublishedContent; using Umbraco.Tests.TestHelpers; using Umbraco.Web; using Umbraco.Web.PublishedCache; using Umbraco.Web.PublishedCache.XmlPublishedCache; using Umbraco.Web.Routing; using umbraco.BusinessLogic; namespace Umbraco.Tests.PublishedCache { [TestFixture] public class PublishContentCacheTests { private FakeHttpContextFactory _httpContextFactory; private UmbracoContext _umbracoContext; private ContextualPublishedContentCache _cache; private string GetLegacyXml() { return @" ]> "; } private string GetXml() { return @" ]> "; } [SetUp] public void SetUp() { TestHelper.SetupLog4NetForTests(); //create the app context ApplicationContext.Current = new ApplicationContext(false); _httpContextFactory = new FakeHttpContextFactory("~/Home"); //ensure the StateHelper is using our custom context StateHelper.HttpContext = _httpContextFactory.HttpContext; UmbracoSettings.UseLegacyXmlSchema = false; var cache = new PublishedContentCache { GetXmlDelegate = (context, preview) => { var doc = new XmlDocument(); doc.LoadXml(GetXml()); return doc; } }; _umbracoContext = new UmbracoContext( _httpContextFactory.HttpContext, ApplicationContext.Current, new PublishedCaches(cache, new PublishedMediaCache())); _cache = _umbracoContext.ContentCache; } private void SetupForLegacy() { Umbraco.Core.Configuration.UmbracoSettings.UseLegacyXmlSchema = true; var cache = _umbracoContext.ContentCache.InnerCache as PublishedContentCache; if (cache == null) throw new Exception("Unsupported IPublishedContentCache, only the Xml one is supported."); cache.GetXmlDelegate = (context, preview) => { var doc = new XmlDocument(); doc.LoadXml(GetLegacyXml()); return doc; }; } [TearDown] public void TearDown() { UmbracoSettings.Reset(); } [Test] public void Has_Content_LegacySchema() { SetupForLegacy(); Has_Content(); } [Test] public void Has_Content() { Assert.IsTrue(_cache.HasContent()); } [Test] public void Get_Root_Docs_LegacySchema() { SetupForLegacy(); Get_Root_Docs(); } [Test] public void Get_Root_Docs() { var result = _cache.GetAtRoot(); Assert.AreEqual(2, result.Count()); Assert.AreEqual(1046, result.ElementAt(0).Id); Assert.AreEqual(1172, result.ElementAt(1).Id); } [TestCase("/", 1046)] [TestCase("/home", 1046)] [TestCase("/Home", 1046)] //test different cases [TestCase("/home/sub1", 1173)] [TestCase("/Home/sub1", 1173)] [TestCase("/home/Sub1", 1173)] //test different cases public void Get_Node_By_Route_LegacySchema(string route, int nodeId) { SetupForLegacy(); Get_Node_By_Route(route, nodeId); } [TestCase("/", 1046)] [TestCase("/home", 1046)] [TestCase("/Home", 1046)] //test different cases [TestCase("/home/sub1", 1173)] [TestCase("/Home/sub1", 1173)] [TestCase("/home/Sub1", 1173)] //test different cases [TestCase("/home/Sub'Apostrophe", 1177)] public void Get_Node_By_Route(string route, int nodeId) { var result = _cache.GetByRoute(route, false); Assert.IsNotNull(result); Assert.AreEqual(nodeId, result.Id); } [TestCase("/", 1046)] [TestCase("/sub1", 1173)] [TestCase("/Sub1", 1173)] public void Get_Node_By_Route_Hiding_Top_Level_Nodes_LegacySchema(string route, int nodeId) { SetupForLegacy(); Get_Node_By_Route_Hiding_Top_Level_Nodes(route, nodeId); } [TestCase("/", 1046)] [TestCase("/sub1", 1173)] [TestCase("/Sub1", 1173)] public void Get_Node_By_Route_Hiding_Top_Level_Nodes(string route, int nodeId) { var result = _cache.GetByRoute(route, true); Assert.IsNotNull(result); Assert.AreEqual(nodeId, result.Id); } } }