using System.Linq;
using System.Xml;
using Moq;
using NUnit.Framework;
using Umbraco.Core;
using Umbraco.Core.Cache;
using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.UmbracoSettings;
using Umbraco.Core.Services;
using Umbraco.Tests.LegacyXmlPublishedCache;
using Umbraco.Tests.TestHelpers;
using Umbraco.Tests.Testing;
using Umbraco.Tests.Testing.Objects.Accessors;
using Umbraco.Web;
using Umbraco.Web.PublishedCache;
using Umbraco.Web.Routing;
using Umbraco.Web.Security;
namespace Umbraco.Tests.Cache.PublishedCache
{
[TestFixture]
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerFixture)]
public class PublishContentCacheTests : BaseWebTest
{
private FakeHttpContextFactory _httpContextFactory;
private IUmbracoContext _umbracoContext;
private IPublishedContentCache _cache;
private XmlDocument _xml;
private string GetXml()
{
return @"
]>
";
}
protected override void Initialize()
{
base.Initialize();
_httpContextFactory = new FakeHttpContextFactory("~/Home");
var umbracoSettings = Factory.GetInstance();
var globalSettings = Factory.GetInstance();
var umbracoContextAccessor = Factory.GetInstance();
_xml = new XmlDocument();
_xml.LoadXml(GetXml());
var xmlStore = new XmlStore(() => _xml, null, null, null, HostingEnvironment);
var appCache = new DictionaryAppCache();
var domainCache = new DomainCache(Mock.Of(), DefaultCultureAccessor);
var publishedShapshot = new PublishedSnapshot(
new PublishedContentCache(xmlStore, domainCache, appCache, globalSettings, ContentTypesCache, null, VariationContextAccessor, null),
new PublishedMediaCache(xmlStore, Mock.Of(), Mock.Of(), appCache, ContentTypesCache, Factory.GetInstance(), umbracoContextAccessor, VariationContextAccessor),
new PublishedMemberCache(null, appCache, Mock.Of(), ContentTypesCache, Mock.Of(), VariationContextAccessor),
domainCache);
var publishedSnapshotService = new Mock();
publishedSnapshotService.Setup(x => x.CreatePublishedSnapshot(It.IsAny())).Returns(publishedShapshot);
var httpContext = _httpContextFactory.HttpContext;
var httpContextAccessor = TestObjects.GetHttpContextAccessor(httpContext);
_umbracoContext = new UmbracoContext(
httpContext,
publishedSnapshotService.Object,
new WebSecurity(httpContextAccessor, Mock.Of(), globalSettings, IOHelper),
umbracoSettings,
Enumerable.Empty(),
Enumerable.Empty(),
globalSettings,
new TestVariationContextAccessor(),
IOHelper);
_cache = _umbracoContext.Content;
}
[Test]
public void Has_Content()
{
Assert.IsTrue(_cache.HasContent());
}
[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
[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(string route, int nodeId)
{
var result = _cache.GetByRoute(route, true);
Assert.IsNotNull(result);
Assert.AreEqual(nodeId, result.Id);
}
}
}