diff --git a/src/Umbraco.Tests/Mvc/UmbracoViewPageTests.cs b/src/Umbraco.Tests/Mvc/UmbracoViewPageTests.cs index c9ed53ef2a..f5ffee1ce3 100644 --- a/src/Umbraco.Tests/Mvc/UmbracoViewPageTests.cs +++ b/src/Umbraco.Tests/Mvc/UmbracoViewPageTests.cs @@ -408,7 +408,7 @@ namespace Umbraco.Tests.Mvc var ctx = new UmbracoContext( GetHttpContextFactory(url, routeData).HttpContext, appCtx, - new PublishedCaches(cache, new PublishedMediaCache()), + new PublishedCaches(cache, new PublishedMediaCache(appCtx)), new WebSecurity(http, appCtx)); //if (setSingleton) diff --git a/src/Umbraco.Tests/PublishedCache/PublishedContentCacheTests.cs b/src/Umbraco.Tests/PublishedCache/PublishedContentCacheTests.cs index b94dc9892d..03e13acdd5 100644 --- a/src/Umbraco.Tests/PublishedCache/PublishedContentCacheTests.cs +++ b/src/Umbraco.Tests/PublishedCache/PublishedContentCacheTests.cs @@ -92,9 +92,9 @@ namespace Umbraco.Tests.PublishedCache _umbracoContext = new UmbracoContext( _httpContextFactory.HttpContext, - ApplicationContext.Current, - new PublishedCaches(cache, new PublishedMediaCache()), - new WebSecurity(_httpContextFactory.HttpContext, ApplicationContext.Current)); + ApplicationContext, + new PublishedCaches(cache, new PublishedMediaCache(ApplicationContext)), + new WebSecurity(_httpContextFactory.HttpContext, ApplicationContext)); _cache = _umbracoContext.ContentCache; } diff --git a/src/Umbraco.Tests/PublishedCache/PublishedMediaCacheTests.cs b/src/Umbraco.Tests/PublishedCache/PublishedMediaCacheTests.cs index 53a8b5b658..6d0b5b18b7 100644 --- a/src/Umbraco.Tests/PublishedCache/PublishedMediaCacheTests.cs +++ b/src/Umbraco.Tests/PublishedCache/PublishedMediaCacheTests.cs @@ -37,7 +37,7 @@ namespace Umbraco.Tests.PublishedCache var mChild2 = global::umbraco.cms.businesslogic.media.Media.MakeNew("Child2", mType, user, mRoot2.Id); var ctx = GetUmbracoContext("/test", 1234); - var cache = new ContextualPublishedMediaCache(new PublishedMediaCache(), ctx); + var cache = new ContextualPublishedMediaCache(new PublishedMediaCache(ctx.Application), ctx); var roots = cache.GetAtRoot(); Assert.AreEqual(2, roots.Count()); Assert.IsTrue(roots.Select(x => x.Id).ContainsAll(new[] {mRoot1.Id, mRoot2.Id})); @@ -126,6 +126,8 @@ namespace Umbraco.Tests.PublishedCache [Test] public void Convert_From_Search_Result() { + var ctx = GetUmbracoContext("/test", 1234); + var result = new SearchResult() { Id = 1234, @@ -144,7 +146,7 @@ namespace Umbraco.Tests.PublishedCache result.Fields.Add("updateDate", "2012-07-16T10:34:09"); result.Fields.Add("writerName", "Shannon"); - var store = new PublishedMediaCache(); + var store = new PublishedMediaCache(ctx.Application); var doc = store.ConvertFromSearchResult(result); DoAssert(doc, 1234, 0, 0, "", "Image", 0, "Shannon", "", 0, 0, "-1,1234", default(DateTime), DateTime.Parse("2012-07-16T10:34:09"), 2); @@ -154,9 +156,11 @@ namespace Umbraco.Tests.PublishedCache [Test] public void Convert_From_XPath_Navigator() { + var ctx = GetUmbracoContext("/test", 1234); + var xmlDoc = GetMediaXml(); var navigator = xmlDoc.SelectSingleNode("/root/Image").CreateNavigator(); - var cache = new PublishedMediaCache(); + var cache = new PublishedMediaCache(ctx.Application); var doc = cache.ConvertFromXPathNavigator(navigator); DoAssert(doc, 2000, 0, 2, "image1", "Image", 2044, "Shannon", "Shannon2", 22, 33, "-1,2000", DateTime.Parse("2012-06-12T14:13:17"), DateTime.Parse("2012-07-20T18:50:43"), 1); diff --git a/src/Umbraco.Tests/PublishedContent/PublishedContentTestBase.cs b/src/Umbraco.Tests/PublishedContent/PublishedContentTestBase.cs index d776e61776..478335af15 100644 --- a/src/Umbraco.Tests/PublishedContent/PublishedContentTestBase.cs +++ b/src/Umbraco.Tests/PublishedContent/PublishedContentTestBase.cs @@ -49,7 +49,7 @@ namespace Umbraco.Tests.PublishedContent }); PublishedCachesResolver.Current = new PublishedCachesResolver(new PublishedCaches( - new PublishedContentCache(), new PublishedMediaCache())); + new PublishedContentCache(), new PublishedMediaCache(ApplicationContext))); if (PublishedContentModelFactoryResolver.HasCurrent == false) PublishedContentModelFactoryResolver.Current = new PublishedContentModelFactoryResolver(); diff --git a/src/Umbraco.Tests/PublishedContent/PublishedMediaTests.cs b/src/Umbraco.Tests/PublishedContent/PublishedMediaTests.cs index 29698cc9d4..425da6ac2e 100644 --- a/src/Umbraco.Tests/PublishedContent/PublishedMediaTests.cs +++ b/src/Umbraco.Tests/PublishedContent/PublishedMediaTests.cs @@ -61,7 +61,7 @@ namespace Umbraco.Tests.PublishedContent internal static IPublishedContent GetNode(int id, UmbracoContext umbracoContext) { var ctx = umbracoContext; - var cache = new ContextualPublishedMediaCache(new PublishedMediaCache(), ctx); + var cache = new ContextualPublishedMediaCache(new PublishedMediaCache(ctx.Application), ctx); var doc = cache.GetById(id); Assert.IsNotNull(doc); return doc; @@ -113,7 +113,7 @@ namespace Umbraco.Tests.PublishedContent indexer.RebuildIndex(); var searcher = IndexInitializer.GetUmbracoSearcher(luceneDir); var ctx = GetUmbracoContext("/test", 1234); - var cache = new ContextualPublishedMediaCache(new PublishedMediaCache(searcher, indexer), ctx); + var cache = new ContextualPublishedMediaCache(new PublishedMediaCache(ctx.Application, searcher, indexer), ctx); //we are using the media.xml media to test the examine results implementation, see the media.xml file in the ExamineHelpers namespace var publishedMedia = cache.GetById(1111); @@ -142,7 +142,7 @@ namespace Umbraco.Tests.PublishedContent indexer.RebuildIndex(); var searcher = IndexInitializer.GetUmbracoSearcher(luceneDir); var ctx = GetUmbracoContext("/test", 1234); - var cache = new ContextualPublishedMediaCache(new PublishedMediaCache(searcher, indexer), ctx); + var cache = new ContextualPublishedMediaCache(new PublishedMediaCache(ctx.Application, searcher, indexer), ctx); //ensure it is found var publishedMedia = cache.GetById(3113); @@ -182,7 +182,7 @@ namespace Umbraco.Tests.PublishedContent indexer.RebuildIndex(); var searcher = IndexInitializer.GetUmbracoSearcher(luceneDir); var ctx = GetUmbracoContext("/test", 1234); - var cache = new ContextualPublishedMediaCache(new PublishedMediaCache(searcher, indexer), ctx); + var cache = new ContextualPublishedMediaCache(new PublishedMediaCache(ctx.Application, searcher, indexer), ctx); //we are using the media.xml media to test the examine results implementation, see the media.xml file in the ExamineHelpers namespace var publishedMedia = cache.GetById(1111); @@ -204,7 +204,7 @@ namespace Umbraco.Tests.PublishedContent indexer.RebuildIndex(); var searcher = IndexInitializer.GetUmbracoSearcher(luceneDir); var ctx = GetUmbracoContext("/test", 1234); - var cache = new ContextualPublishedMediaCache(new PublishedMediaCache(searcher, indexer), ctx); + var cache = new ContextualPublishedMediaCache(new PublishedMediaCache(ctx.Application, searcher, indexer), ctx); //we are using the media.xml media to test the examine results implementation, see the media.xml file in the ExamineHelpers namespace var publishedMedia = cache.GetById(1111); @@ -226,7 +226,7 @@ namespace Umbraco.Tests.PublishedContent indexer.RebuildIndex(); var searcher = IndexInitializer.GetUmbracoSearcher(luceneDir); var ctx = GetUmbracoContext("/test", 1234); - var cache = new ContextualPublishedMediaCache(new PublishedMediaCache(searcher, indexer), ctx); + var cache = new ContextualPublishedMediaCache(new PublishedMediaCache(ctx.Application, searcher, indexer), ctx); //we are using the media.xml media to test the examine results implementation, see the media.xml file in the ExamineHelpers namespace var publishedMedia = cache.GetById(1111); @@ -248,7 +248,7 @@ namespace Umbraco.Tests.PublishedContent indexer.RebuildIndex(); var ctx = GetUmbracoContext("/test", 1234); var searcher = IndexInitializer.GetUmbracoSearcher(luceneDir); - var cache = new ContextualPublishedMediaCache(new PublishedMediaCache(searcher, indexer), ctx); + var cache = new ContextualPublishedMediaCache(new PublishedMediaCache(ctx.Application, searcher, indexer), ctx); //we are using the media.xml media to test the examine results implementation, see the media.xml file in the ExamineHelpers namespace var publishedMedia = cache.GetById(3113); @@ -267,7 +267,7 @@ namespace Umbraco.Tests.PublishedContent indexer.RebuildIndex(); var ctx = GetUmbracoContext("/test", 1234); var searcher = IndexInitializer.GetUmbracoSearcher(luceneDir); - var cache = new ContextualPublishedMediaCache(new PublishedMediaCache(searcher, indexer), ctx); + var cache = new ContextualPublishedMediaCache(new PublishedMediaCache(ctx.Application, searcher, indexer), ctx); //we are using the media.xml media to test the examine results implementation, see the media.xml file in the ExamineHelpers namespace var publishedMedia = cache.GetById(3113); @@ -442,7 +442,7 @@ namespace Umbraco.Tests.PublishedContent "); var node = xml.DescendantsAndSelf("node").Single(x => (int) x.Attribute("id") == nodeId); - var publishedMedia = new PublishedMediaCache(); + var publishedMedia = new PublishedMediaCache(ApplicationContext); var nav = node.CreateNavigator(); @@ -483,7 +483,7 @@ namespace Umbraco.Tests.PublishedContent "); var node = xml.DescendantsAndSelf("Image").Single(x => (int)x.Attribute("id") == nodeId); - var publishedMedia = new PublishedMediaCache(); + var publishedMedia = new PublishedMediaCache(ApplicationContext); var nav = node.CreateNavigator(); @@ -502,7 +502,7 @@ namespace Umbraco.Tests.PublishedContent var errorXml = new XElement("error", string.Format("No media is maching '{0}'", 1234)); var nav = errorXml.CreateNavigator(); - var publishedMedia = new PublishedMediaCache(); + var publishedMedia = new PublishedMediaCache(ApplicationContext); var converted = publishedMedia.ConvertFromXPathNodeIterator(nav.Select("/"), 1234); Assert.IsNull(converted); diff --git a/src/Umbraco.Tests/TestHelpers/BaseDatabaseFactoryTest.cs b/src/Umbraco.Tests/TestHelpers/BaseDatabaseFactoryTest.cs index ec1148e1c3..3af0c34a6a 100644 --- a/src/Umbraco.Tests/TestHelpers/BaseDatabaseFactoryTest.cs +++ b/src/Umbraco.Tests/TestHelpers/BaseDatabaseFactoryTest.cs @@ -356,7 +356,7 @@ namespace Umbraco.Tests.TestHelpers var ctx = new UmbracoContext( httpContext, ApplicationContext, - new PublishedCaches(cache, new PublishedMediaCache()), + new PublishedCaches(cache, new PublishedMediaCache(ApplicationContext)), new WebSecurity(httpContext, ApplicationContext)); if (setSingleton) diff --git a/src/Umbraco.Web/PublishedCache/XmlPublishedCache/PublishedMediaCache.cs b/src/Umbraco.Web/PublishedCache/XmlPublishedCache/PublishedMediaCache.cs index f9c2d5a710..94ba9d0537 100644 --- a/src/Umbraco.Web/PublishedCache/XmlPublishedCache/PublishedMediaCache.cs +++ b/src/Umbraco.Web/PublishedCache/XmlPublishedCache/PublishedMediaCache.cs @@ -18,8 +18,6 @@ using Umbraco.Core.Xml; using Umbraco.Web.Models; using UmbracoExamine; using umbraco; -using umbraco.cms.businesslogic; -using ContentType = umbraco.cms.businesslogic.ContentType; namespace Umbraco.Web.PublishedCache.XmlPublishedCache { @@ -31,21 +29,30 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache /// internal class PublishedMediaCache : IPublishedMediaCache { - public PublishedMediaCache() - { - } + public PublishedMediaCache(ApplicationContext applicationContext) + { + if (applicationContext == null) throw new ArgumentNullException("applicationContext"); + _applicationContext = applicationContext; + } /// /// Generally used for unit testing to use an explicit examine searcher /// + /// /// /// - internal PublishedMediaCache(BaseSearchProvider searchProvider, BaseIndexProvider indexProvider) + internal PublishedMediaCache(ApplicationContext applicationContext, BaseSearchProvider searchProvider, BaseIndexProvider indexProvider) { - _searchProvider = searchProvider; + if (applicationContext == null) throw new ArgumentNullException("applicationContext"); + if (searchProvider == null) throw new ArgumentNullException("searchProvider"); + if (indexProvider == null) throw new ArgumentNullException("indexProvider"); + + _applicationContext = applicationContext; + _searchProvider = searchProvider; _indexProvider = indexProvider; } + private readonly ApplicationContext _applicationContext; private readonly BaseSearchProvider _searchProvider; private readonly BaseIndexProvider _indexProvider; @@ -56,17 +63,10 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache public virtual IEnumerable GetAtRoot(UmbracoContext umbracoContext, bool preview) { - var rootMedia = global::umbraco.cms.businesslogic.media.Media.GetRootMedias(); - var result = new List(); - //TODO: need to get a ConvertFromMedia method but we'll just use this for now. - foreach (var media in rootMedia - .Select(m => global::umbraco.library.GetMedia(m.Id, true)) - .Where(media => media != null && media.Current != null)) - { - media.MoveNext(); - result.Add(ConvertFromXPathNavigator(media.Current)); - } - return result; + //TODO: We should be able to look these ids first in Examine! + + var rootMedia = _applicationContext.Services.MediaService.GetRootMedia(); + return rootMedia.Select(m => GetUmbracoMedia(m.Id)); } public virtual IPublishedContent GetSingleByXPath(UmbracoContext umbracoContext, bool preview, string xpath, XPathVariable[] vars) @@ -96,7 +96,7 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache public bool XPathNavigatorIsNavigable { get { return false; } } - public virtual bool HasContent(UmbracoContext context, bool preview) { throw new NotImplementedException(); } + public virtual bool HasContent(UmbracoContext context, bool preview) { throw new NotImplementedException(); } private ExamineManager GetExamineManagerSafe() { diff --git a/src/Umbraco.Web/Standalone/StandaloneBootManager.cs b/src/Umbraco.Web/Standalone/StandaloneBootManager.cs index 09d5d5ca10..2e8d23dd1d 100644 --- a/src/Umbraco.Web/Standalone/StandaloneBootManager.cs +++ b/src/Umbraco.Web/Standalone/StandaloneBootManager.cs @@ -57,7 +57,7 @@ namespace Umbraco.Web.Standalone var caches = new PublishedCaches( new PublishedCache.XmlPublishedCache.PublishedContentCache(), - new PublishedCache.XmlPublishedCache.PublishedMediaCache()); + new PublishedCache.XmlPublishedCache.PublishedMediaCache(ApplicationContext.Current)); PublishedCachesResolver.Current = new PublishedCachesResolver(caches); diff --git a/src/Umbraco.Web/WebBootManager.cs b/src/Umbraco.Web/WebBootManager.cs index 88a44242dc..eb9703ba7f 100644 --- a/src/Umbraco.Web/WebBootManager.cs +++ b/src/Umbraco.Web/WebBootManager.cs @@ -338,7 +338,7 @@ namespace Umbraco.Web PublishedCachesResolver.Current = new PublishedCachesResolver(new PublishedCaches( new PublishedCache.XmlPublishedCache.PublishedContentCache(), - new PublishedCache.XmlPublishedCache.PublishedMediaCache())); + new PublishedCache.XmlPublishedCache.PublishedMediaCache(ApplicationContext))); GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerSelector), new NamespaceHttpControllerSelector(GlobalConfiguration.Configuration)); diff --git a/src/Umbraco.Web/umbraco.presentation/library.cs b/src/Umbraco.Web/umbraco.presentation/library.cs index d4042f75aa..4b97dffe5f 100644 --- a/src/Umbraco.Web/umbraco.presentation/library.cs +++ b/src/Umbraco.Web/umbraco.presentation/library.cs @@ -528,8 +528,10 @@ namespace umbraco if (media == null) return null; var serializer = new EntityXmlSerializer(); var serialized = serializer.Serialize( - ApplicationContext.Current.Services.MediaService, ApplicationContext.Current.Services.DataTypeService, - ApplicationContext.Current.Services.UserService, media, deep); + ApplicationContext.Current.Services.MediaService, + ApplicationContext.Current.Services.DataTypeService, + media, + deep); return serialized; }