using System.Linq;
using System.Xml;
using Moq;
using NUnit.Framework;
using umbraco.BusinessLogic;
using Umbraco.Core.Cache;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Tests.TestHelpers;
using Umbraco.Web;
using Umbraco.Web.PublishedCache;
using Umbraco.Web.PublishedCache.XmlPublishedCache;
using Umbraco.Web.Routing;
using Umbraco.Web.Security;
namespace Umbraco.Tests.Cache.PublishedCache
{
[DatabaseTestBehavior(DatabaseBehavior.NewDbFileAndSchemaPerFixture)]
[TestFixture]
public class PublishContentCacheTests : BaseWebTest
{
private FakeHttpContextFactory _httpContextFactory;
private UmbracoContext _umbracoContext;
private IPublishedContentCache _cache;
private XmlDocument _xml;
private string GetXml()
{
return @"
]>
";
}
[SetUp]
public override void Initialize()
{
base.Initialize();
_httpContextFactory = new FakeHttpContextFactory("~/Home");
var settings = SettingsForTests.GenerateMockSettings();
SettingsForTests.ConfigureSettings(settings);
_xml = new XmlDocument();
_xml.LoadXml(GetXml());
var xmlStore = new XmlStore(() => _xml);
var cacheProvider = new StaticCacheProvider();
var domainCache = new DomainCache(ServiceContext.DomainService);
var facade = new Facade(
new PublishedContentCache(xmlStore, domainCache, cacheProvider, ContentTypesCache, null, null),
new PublishedMediaCache(xmlStore, ServiceContext.MediaService, cacheProvider, ContentTypesCache),
new PublishedMemberCache(null, cacheProvider, ApplicationContext.Services.MemberService, ContentTypesCache),
domainCache);
var facadeService = new Mock();
facadeService.Setup(x => x.CreateFacade(It.IsAny())).Returns(facade);
_umbracoContext = UmbracoContext.CreateContext(
_httpContextFactory.HttpContext, ApplicationContext,
facadeService.Object,
new WebSecurity(_httpContextFactory.HttpContext, ApplicationContext),
settings,
Enumerable.Empty(),
null);
_cache = _umbracoContext.ContentCache;
}
protected override void FreezeResolution()
{
PublishedContentModelFactoryResolver.Current = new PublishedContentModelFactoryResolver();
base.FreezeResolution();
}
[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);
}
}
}