diff --git a/src/Umbraco.Tests/ContentStores/PublishMediaStoreTests.cs b/src/Umbraco.Tests/ContentStores/PublishMediaStoreTests.cs index a60c52d966..ec1bc3b35e 100644 --- a/src/Umbraco.Tests/ContentStores/PublishMediaStoreTests.cs +++ b/src/Umbraco.Tests/ContentStores/PublishMediaStoreTests.cs @@ -21,11 +21,12 @@ namespace Umbraco.Tests.ContentStores base.Initialize(); } - protected override void OnFreezing() + protected override void FreezeResolution() { - base.OnFreezing(); //we're going to use the same initialization as the PublishedMediaTests PublishedMediaTests.DoInitialization(GetUmbracoContext("/test", 1234)); + + base.FreezeResolution(); } public override void TearDown() diff --git a/src/Umbraco.Tests/PublishedContent/PublishedContentTests.cs b/src/Umbraco.Tests/PublishedContent/PublishedContentTests.cs index 72da9dc875..4f7f6d3578 100644 --- a/src/Umbraco.Tests/PublishedContent/PublishedContentTests.cs +++ b/src/Umbraco.Tests/PublishedContent/PublishedContentTests.cs @@ -85,10 +85,8 @@ namespace Umbraco.Tests.PublishedContent /// /// Setup any resolvers before freezing /// - protected override void OnFreezing() + protected override void FreezeResolution() { - base.OnFreezing(); - PublishedContentStoreResolver.Current = new PublishedContentStoreResolver(new DefaultPublishedContentStore()); PropertyEditorValueConvertersResolver.Current = new PropertyEditorValueConvertersResolver( @@ -98,6 +96,8 @@ namespace Umbraco.Tests.PublishedContent typeof(TinyMcePropertyEditorValueConverter), typeof(YesNoPropertyEditorValueConverter) }); + + base.FreezeResolution(); } public override void TearDown() diff --git a/src/Umbraco.Tests/PublishedContent/PublishedMediaTests.cs b/src/Umbraco.Tests/PublishedContent/PublishedMediaTests.cs index 1069c2d2c3..8456e40d37 100644 --- a/src/Umbraco.Tests/PublishedContent/PublishedMediaTests.cs +++ b/src/Umbraco.Tests/PublishedContent/PublishedMediaTests.cs @@ -35,13 +35,14 @@ namespace Umbraco.Tests.PublishedContent public override void Initialize() { base.Initialize(); - + UmbracoExamineSearcher.DisableInitializationCheck = true; + BaseUmbracoIndexer.DisableInitializationCheck = true; } - protected override void OnFreezing() - { - base.OnFreezing(); - DoInitialization(GetUmbracoContext("/test", 1234)); + protected override void FreezeResolution() + { + DoInitialization(GetUmbracoContext("/test", 1234)); + base.FreezeResolution(); } @@ -89,6 +90,8 @@ namespace Umbraco.Tests.PublishedContent /// internal static void DoTearDown() { + UmbracoExamineSearcher.DisableInitializationCheck = null; + BaseUmbracoIndexer.DisableInitializationCheck = null; PropertyEditorValueConvertersResolver.Reset(); UmbracoContext.Current = null; PublishedMediaStoreResolver.Reset(); @@ -117,38 +120,41 @@ namespace Umbraco.Tests.PublishedContent [Test] public void Do_Not_Find_In_Recycle_Bin() { - var newIndexFolder = new DirectoryInfo(Path.Combine("App_Data\\CWSIndexSetTest", Guid.NewGuid().ToString())); - var indexInit = new IndexInitializer(); - var indexer = indexInit.GetUmbracoIndexer(newIndexFolder); - indexer.RebuildIndex(); - var searcher = indexInit.GetUmbracoSearcher(newIndexFolder); - var store = new DefaultPublishedMediaStore(searcher); - var ctx = GetUmbracoContext("/test", 1234); + using (var luceneDir = new RAMDirectory()) + { + var indexer = IndexInitializer.GetUmbracoIndexer(luceneDir); + indexer.RebuildIndex(); + var searcher = IndexInitializer.GetUmbracoSearcher(luceneDir); + var store = new DefaultPublishedMediaStore(searcher); + var ctx = GetUmbracoContext("/test", 1234); - //ensure it is found - var publishedMedia = store.GetDocumentById(ctx, 3113); - Assert.IsNotNull(publishedMedia); + //ensure it is found + var publishedMedia = store.GetDocumentById(ctx, 3113); + Assert.IsNotNull(publishedMedia); - //move item to recycle bin - var newXml = XElement.Parse(@" + //move item to recycle bin + var newXml = XElement.Parse(@" 115 268 10726 jpg "); - indexer.ReIndexNode(newXml, "media"); + indexer.ReIndexNode(newXml, "media"); - //ensure it still exists in the index (raw examine search) - var criteria = searcher.CreateSearchCriteria(); - var filter = criteria.Id(3113); - var found = searcher.Search(filter.Compile()); - Assert.IsNotNull(found); - Assert.AreEqual(1, found.TotalItemCount); + //ensure it still exists in the index (raw examine search) + var criteria = searcher.CreateSearchCriteria(); + var filter = criteria.Id(3113); + var found = searcher.Search(filter.Compile()); + Assert.IsNotNull(found); + Assert.AreEqual(1, found.TotalItemCount); - //ensure it does not show up in the published media store - var recycledMedia = store.GetDocumentById(ctx, 3113); - Assert.IsNull(recycledMedia); + //ensure it does not show up in the published media store + var recycledMedia = store.GetDocumentById(ctx, 3113); + Assert.IsNull(recycledMedia); + + } + } [Test] diff --git a/src/Umbraco.Tests/PublishedContent/StronglyTypedQueryTests.cs b/src/Umbraco.Tests/PublishedContent/StronglyTypedQueryTests.cs index 8cb05f77a8..2ce0e89ed9 100644 --- a/src/Umbraco.Tests/PublishedContent/StronglyTypedQueryTests.cs +++ b/src/Umbraco.Tests/PublishedContent/StronglyTypedQueryTests.cs @@ -22,11 +22,12 @@ namespace Umbraco.Tests.PublishedContent base.Initialize(); } - protected override void OnFreezing() - { + protected override void FreezeResolution() + { var routingCtx = GetRoutingContext("/test", 1234); UmbracoContext.Current = routingCtx.UmbracoContext; PropertyEditorValueConvertersResolver.Current = new PropertyEditorValueConvertersResolver(Enumerable.Empty()); + base.FreezeResolution(); } public override void TearDown() diff --git a/src/Umbraco.Tests/TestHelpers/BaseDatabaseFactoryTest.cs b/src/Umbraco.Tests/TestHelpers/BaseDatabaseFactoryTest.cs index 3e1bde3604..4265575e9a 100644 --- a/src/Umbraco.Tests/TestHelpers/BaseDatabaseFactoryTest.cs +++ b/src/Umbraco.Tests/TestHelpers/BaseDatabaseFactoryTest.cs @@ -63,17 +63,23 @@ namespace Umbraco.Tests.TestHelpers //Create the Sql CE database var engine = new SqlCeEngine(settings.ConnectionString); engine.CreateDatabase(); - - Resolution.Freeze(); + ApplicationContext.Current = new ApplicationContext( //assign the db context new DatabaseContext(new DefaultDatabaseFactory()), //assign the service context new ServiceContext(new PetaPocoUnitOfWorkProvider(), new FileUnitOfWorkProvider(), new PublishingStrategy())) { IsReady = true }; + FreezeResolution(); + InitializeDatabase(); } + protected virtual void FreezeResolution() + { + Resolution.Freeze(); + } + protected virtual void InitializeDatabase() { //Configure the Database and Sql Syntax based on connection string set in config @@ -95,8 +101,9 @@ namespace Umbraco.Tests.TestHelpers SqlCeContextGuardian.CloseBackgroundConnection(); ApplicationContext.Current = null; - Resolution.IsFrozen = false; + RepositoryResolver.Reset(); + Resolution.Reset(); TestHelper.CleanContentDirectories(); diff --git a/src/Umbraco.Tests/UmbracoExamine/IndexInitializer.cs b/src/Umbraco.Tests/UmbracoExamine/IndexInitializer.cs index a61a60fdd2..89f9f86a90 100644 --- a/src/Umbraco.Tests/UmbracoExamine/IndexInitializer.cs +++ b/src/Umbraco.Tests/UmbracoExamine/IndexInitializer.cs @@ -1,10 +1,12 @@ using System; using System.Linq; using Examine; +using Examine.LuceneEngine.Config; using Examine.LuceneEngine.Providers; using Lucene.Net.Analysis; using Lucene.Net.Analysis.Standard; using UmbracoExamine; +using UmbracoExamine.Config; using UmbracoExamine.DataServices; using UmbracoExamine.PDF; @@ -30,22 +32,10 @@ namespace Umbraco.Tests.UmbracoExamine analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29); } - var i = new UmbracoContentIndexer(new IndexCriteria( - //new[] - // { - // new TestIndexField {Name = "id", EnableSorting = true, Type = "Number"}, - // new TestIndexField {Name = "nodeName", EnableSorting = true}, - // new TestIndexField {Name = "updateDate", EnableSorting = true, Type = "DateTime"}, - // new TestIndexField {Name = "writerName"}, - // new TestIndexField {Name = "path"}, - // new TestIndexField {Name = "nodeTypeAlias"}, - // new TestIndexField {Name = "parentID"} - // }, - Enumerable.Empty(), - Enumerable.Empty(), - Enumerable.Empty(), - new string[] {}, - -1), + var indexSet = new IndexSet(); + var indexCriteria = indexSet.ToIndexCriteria(dataService); + + var i = new UmbracoContentIndexer(indexCriteria, luceneDir, //custom lucene directory dataService, analyzer, diff --git a/src/Umbraco.Tests/UmbracoExamine/TestContentService.cs b/src/Umbraco.Tests/UmbracoExamine/TestContentService.cs index 18f8cc82b4..888b05d1a7 100644 --- a/src/Umbraco.Tests/UmbracoExamine/TestContentService.cs +++ b/src/Umbraco.Tests/UmbracoExamine/TestContentService.cs @@ -17,13 +17,18 @@ namespace Umbraco.Tests.UmbracoExamine { public const int ProtectedNode = 1142; - public TestContentService(string xml = null) + public TestContentService(string contentXml = null, string mediaXml = null) { - if (xml == null) + if (contentXml == null) { - xml = TestFiles.umbraco; + contentXml = TestFiles.umbraco; } - _xDoc = XDocument.Parse(xml); + if (mediaXml == null) + { + mediaXml = TestFiles.media; + } + _xContent = XDocument.Parse(contentXml); + _xMedia = XDocument.Parse(mediaXml); } #region IContentService Members @@ -39,7 +44,7 @@ namespace Umbraco.Tests.UmbracoExamine public XDocument GetLatestContentByXPath(string xpath) { var xdoc = XDocument.Parse(""); - xdoc.Root.Add(_xDoc.XPathSelectElements(xpath)); + xdoc.Root.Add(_xContent.XPathSelectElements(xpath)); return xdoc; } @@ -51,12 +56,17 @@ namespace Umbraco.Tests.UmbracoExamine /// public XDocument GetPublishedContentByXPath(string xpath) { - var xdoc = XDocument.Parse(""); - xdoc.Root.Add(_xDoc.XPathSelectElements(xpath)); - - return xdoc; + return GetContentByXPath(xpath, _xContent); } + private XDocument GetContentByXPath(string xpath, XDocument content) + { + var xdoc = XDocument.Parse(""); + xdoc.Root.Add(content.XPathSelectElements(xpath)); + + return xdoc; + } + public string StripHtml(string value) { const string pattern = @"<(.|\n)*?>"; @@ -70,24 +80,42 @@ namespace Umbraco.Tests.UmbracoExamine return nodeId == ProtectedNode; } + private List _userPropNames; public IEnumerable GetAllUserPropertyNames() { - return GetPublishedContentByXPath("//*[count(@id)>0]") - .Root - .Elements() - .Select(x => x.Name.LocalName) - .ToList(); + if (_userPropNames == null) + { + var xpath = "//*[count(@id)>0 and @id != -1]"; + _userPropNames = GetPublishedContentByXPath(xpath) + .Root + .Elements() //each page + .SelectMany(x => x.Elements().Where(e => e.Attribute("id") == null)) //each page property (no @id) + .Select(x => x.Name.LocalName) //the name of the property + .Distinct() + .Union(GetContentByXPath(xpath, _xMedia) + .Root + .Elements() //each page + .SelectMany(x => x.Elements().Where(e => e.Attribute("id") == null)) //each page property (no @id) + .Select(x => (string)x.Attribute("alias")) //the name of the property NOTE: We are using the legacy XML here. + .Distinct()).ToList(); + } + return _userPropNames; } + private List _sysPropNames; public IEnumerable GetAllSystemPropertyNames() { - return UmbracoContentIndexer.IndexFieldPolicies.Select(x => x.Name); + if (_sysPropNames == null) + { + _sysPropNames = UmbracoContentIndexer.IndexFieldPolicies.Select(x => x.Name).ToList(); + } + return _sysPropNames; } #endregion - private readonly XDocument _xDoc; - + private readonly XDocument _xContent; + private readonly XDocument _xMedia; diff --git a/src/Umbraco.Tests/UmbracoExamine/TestFiles/media.xml b/src/Umbraco.Tests/UmbracoExamine/TestFiles/media.xml index ec364ddac7..986ef4c8a1 100644 --- a/src/Umbraco.Tests/UmbracoExamine/TestFiles/media.xml +++ b/src/Umbraco.Tests/UmbracoExamine/TestFiles/media.xml @@ -2,15 +2,15 @@ - + - + 115 268 10726 jpg - + 115 268 diff --git a/src/UmbracoExamine/BaseUmbracoIndexer.cs b/src/UmbracoExamine/BaseUmbracoIndexer.cs index 5a1a0d1a5e..217eac73ec 100644 --- a/src/UmbracoExamine/BaseUmbracoIndexer.cs +++ b/src/UmbracoExamine/BaseUmbracoIndexer.cs @@ -58,6 +58,11 @@ namespace UmbracoExamine #endregion + /// + /// Used for unit tests + /// + internal static bool? DisableInitializationCheck = null; + #region Properties /// @@ -214,13 +219,18 @@ namespace UmbracoExamine [SecuritySafeCritical] protected bool CanInitialize() { - //We need to check if we actually can initialize, if not then don't continue - if (ApplicationContext.Current == null - || !ApplicationContext.Current.IsConfigured - || !ApplicationContext.Current.DatabaseContext.IsDatabaseConfigured) + //check the DisableInitializationCheck and ensure that it is not set to true + if (!DisableInitializationCheck.HasValue || !DisableInitializationCheck.Value) { - return false; + //We need to check if we actually can initialize, if not then don't continue + if (ApplicationContext.Current == null + || !ApplicationContext.Current.IsConfigured + || !ApplicationContext.Current.DatabaseContext.IsDatabaseConfigured) + { + return false; + } } + return true; } diff --git a/src/UmbracoExamine/UmbracoExamineSearcher.cs b/src/UmbracoExamine/UmbracoExamineSearcher.cs index 3f546cb13f..68f07f45e1 100644 --- a/src/UmbracoExamine/UmbracoExamineSearcher.cs +++ b/src/UmbracoExamine/UmbracoExamineSearcher.cs @@ -85,6 +85,11 @@ namespace UmbracoExamine #endregion + /// + /// Used for unit tests + /// + internal static bool? DisableInitializationCheck = null; + /// /// Returns true if the Umbraco application is in a state that we can initialize the examine indexes /// @@ -92,13 +97,17 @@ namespace UmbracoExamine [SecuritySafeCritical] protected bool CanInitialize() { - //We need to check if we actually can initialize, if not then don't continue - if (ApplicationContext.Current == null - || !ApplicationContext.Current.IsConfigured - || !ApplicationContext.Current.DatabaseContext.IsDatabaseConfigured) + //check the DisableInitializationCheck and ensure that it is not set to true + if (!DisableInitializationCheck.HasValue || !DisableInitializationCheck.Value) { - return false; - } + //We need to check if we actually can initialize, if not then don't continue + if (ApplicationContext.Current == null + || !ApplicationContext.Current.IsConfigured + || !ApplicationContext.Current.DatabaseContext.IsDatabaseConfigured) + { + return false; + } + } return true; }