From a2ea20de703bef52da8d688f99013c946acdb879 Mon Sep 17 00:00:00 2001 From: Shannon Deminick Date: Tue, 19 Feb 2013 22:46:44 +0600 Subject: [PATCH] un-ignores PublishedMedia tests. Streamlines BaseWebTest so now we only have one place for database init logic (will enhance this more today). Changed ExamineEvents to not delete indexes on trashed and changed PublishedMediaStore to ignore nodes in the recycle bin path... and now to write unit tests for that. --- .../PublishedContent/PublishedMediaTests.cs | 6 - .../TestHelpers/BaseDatabaseFactoryTest.cs | 10 +- src/Umbraco.Tests/TestHelpers/BaseWebTest.cs | 131 ++---------------- src/Umbraco.Web/DefaultPublishedMediaStore.cs | 3 +- .../umbraco/Search/ExamineEvents.cs | 44 ++++-- 5 files changed, 53 insertions(+), 141 deletions(-) diff --git a/src/Umbraco.Tests/PublishedContent/PublishedMediaTests.cs b/src/Umbraco.Tests/PublishedContent/PublishedMediaTests.cs index 669e8a76b3..8a8f693bd7 100644 --- a/src/Umbraco.Tests/PublishedContent/PublishedMediaTests.cs +++ b/src/Umbraco.Tests/PublishedContent/PublishedMediaTests.cs @@ -200,7 +200,6 @@ namespace Umbraco.Tests.PublishedContent Assert.IsTrue(ancestors.Select(x => x.Id).ContainsAll(new[] { 3113, 2112, 2222, 1111 })); } - [Ignore] [Test] public void Children_Without_Examine() { @@ -225,7 +224,6 @@ namespace Umbraco.Tests.PublishedContent Assert.IsTrue(subChildren.Select(x => x.Id).ContainsAll(new[] { mSubChild1.Id, mSubChild2.Id, mSubChild3.Id })); } - [Ignore] [Test] public void Descendants_Without_Examine() { @@ -250,7 +248,6 @@ namespace Umbraco.Tests.PublishedContent Assert.IsTrue(subDescendants.Select(x => x.Id).ContainsAll(new[] { mSubChild1.Id, mSubChild2.Id, mSubChild3.Id })); } - [Ignore] [Test] public void DescendantsOrSelf_Without_Examine() { @@ -277,7 +274,6 @@ namespace Umbraco.Tests.PublishedContent new[] { mChild1.Id, mSubChild1.Id, mSubChild2.Id, mSubChild3.Id })); } - [Ignore] [Test] public void Parent_Without_Examine() { @@ -303,7 +299,6 @@ namespace Umbraco.Tests.PublishedContent Assert.AreEqual(mChild1.Id, publishedSubChild1.Parent.Id); } - [Ignore] [Test] public void Ancestors_Without_Examine() { @@ -323,7 +318,6 @@ namespace Umbraco.Tests.PublishedContent Assert.IsTrue(publishedSubChild1.Ancestors().Select(x => x.Id).ContainsAll(new[] {mChild1.Id, mRoot.Id})); } - [Ignore] [Test] public void AncestorsOrSelf_Without_Examine() { diff --git a/src/Umbraco.Tests/TestHelpers/BaseDatabaseFactoryTest.cs b/src/Umbraco.Tests/TestHelpers/BaseDatabaseFactoryTest.cs index 3b2d772072..66457fffe1 100644 --- a/src/Umbraco.Tests/TestHelpers/BaseDatabaseFactoryTest.cs +++ b/src/Umbraco.Tests/TestHelpers/BaseDatabaseFactoryTest.cs @@ -69,14 +69,18 @@ namespace Umbraco.Tests.TestHelpers new DatabaseContext(new DefaultDatabaseFactory()), //assign the service context new ServiceContext(new PetaPocoUnitOfWorkProvider(), new FileUnitOfWorkProvider(), new PublishingStrategy())) { IsReady = true }; - + + InitializeDatabase(); + } + + protected virtual void InitializeDatabase() + { //Configure the Database and Sql Syntax based on connection string set in config DatabaseContext.Initialize(); //Create the umbraco database and its base data DatabaseContext.Database.CreateDatabaseSchema(false); } - [TearDown] public virtual void TearDown() { @@ -103,6 +107,8 @@ namespace Umbraco.Tests.TestHelpers { File.Delete(filePath); } + + UmbracoSettings.ResetSetters(); } protected ApplicationContext ApplicationContext diff --git a/src/Umbraco.Tests/TestHelpers/BaseWebTest.cs b/src/Umbraco.Tests/TestHelpers/BaseWebTest.cs index 4f1e3c158e..ae5eb1cc1f 100644 --- a/src/Umbraco.Tests/TestHelpers/BaseWebTest.cs +++ b/src/Umbraco.Tests/TestHelpers/BaseWebTest.cs @@ -9,6 +9,9 @@ using Umbraco.Core.Configuration; using Umbraco.Core.IO; using Umbraco.Core.ObjectResolution; using Umbraco.Core.Persistence; +using Umbraco.Core.Persistence.UnitOfWork; +using Umbraco.Core.Publishing; +using Umbraco.Core.Services; using Umbraco.Tests.Stubs; using Umbraco.Web; using Umbraco.Web.Routing; @@ -18,77 +21,27 @@ using umbraco.cms.businesslogic.cache; namespace Umbraco.Tests.TestHelpers { [TestFixture, RequiresSTA] - public abstract class BaseWebTest + public abstract class BaseWebTest : BaseDatabaseFactoryTest { [SetUp] - public virtual void Initialize() + public override void Initialize() { - TestHelper.SetupLog4NetForTests(); - TestHelper.InitializeContentDirectories(); - - AppDomain.CurrentDomain.SetData("DataDirectory", TestHelper.CurrentAssemblyDirectory); + base.Initialize(); + } + protected override void InitializeDatabase() + { if (RequiresDbSetup) - TestHelper.InitializeDatabase(); - - Resolution.Freeze(); - - //NOTE: We are not constructing with the service context here because it is not required for these tests (currently) - // if we do, this means that we have to initialized the RepositoryResolver too. - ApplicationContext.Current = new ApplicationContext - { - IsReady = true, - //assign the db context - DatabaseContext = new DatabaseContext(new DefaultDatabaseFactory()) - }; - + base.InitializeDatabase(); } [TearDown] - public virtual void TearDown() + public override void TearDown() { - TestHelper.CleanContentDirectories(); - - //reset the app context - DatabaseContext.Database.Dispose(); - ApplicationContext.ApplicationCache.ClearAllCache(); - ApplicationContext.Current = null; - Resolution.IsFrozen = false; - - if (RequiresDbSetup) - { - TestHelper.ClearDatabase(); - SqlCeContextGuardian.CloseBackgroundConnection(); - } - - AppDomain.CurrentDomain.SetData("DataDirectory", null); - - Cache.ClearAllCache(); - - UmbracoSettings.ResetSetters(); - } - - protected virtual void CreateDirectories(string[] directories) - { - foreach (var directory in directories) - { - var directoryInfo = new DirectoryInfo(IOHelper.MapPath(directory)); - if (directoryInfo.Exists == false) - Directory.CreateDirectory(IOHelper.MapPath(directory)); - } + base.TearDown(); } - protected virtual void CleanDirectories(string[] directories) - { - foreach (var directory in directories) - { - var directoryInfo = new DirectoryInfo(IOHelper.MapPath(directory)); - if (directoryInfo.Exists) - directoryInfo.GetFiles().ForEach(x => x.Delete()); - } - } - /// /// By default this unit test will create and initialize an umbraco database /// @@ -96,46 +49,8 @@ namespace Umbraco.Tests.TestHelpers { get { return true; } } - - protected FakeHttpContextFactory GetHttpContextFactory(string url, RouteData routeData = null) - { - var factory = routeData != null - ? new FakeHttpContextFactory(url, routeData) - : new FakeHttpContextFactory(url); - - - //set the state helper - StateHelper.HttpContext = factory.HttpContext; - - return factory; - } - - protected ApplicationContext ApplicationContext - { - get { return ApplicationContext.Current; } - } - - protected DatabaseContext DatabaseContext - { - get { return ApplicationContext.DatabaseContext; } - } - - internal virtual IRoutesCache GetRoutesCache() - { - return new FakeRoutesCache(); - } - - protected UmbracoContext GetUmbracoContext(string url, int templateId, RouteData routeData = null) - { - var ctx = new UmbracoContext( - GetHttpContextFactory(url, routeData).HttpContext, - ApplicationContext, - GetRoutesCache()); - SetupUmbracoContextForTest(ctx, templateId); - return ctx; - } - - protected virtual string GetXmlContent(int templateId) + + protected override string GetXmlContent(int templateId) { return @" "; } - /// - /// Initlializes the UmbracoContext with specific XML - /// - /// - /// - protected void SetupUmbracoContextForTest(UmbracoContext umbracoContext, int templateId) - { - umbracoContext.GetXmlDelegate = () => - { - var xDoc = new XmlDocument(); - - //create a custom xml structure to return - - xDoc.LoadXml(GetXmlContent(templateId)); - //return the custom x doc - return xDoc; - }; - } } } \ No newline at end of file diff --git a/src/Umbraco.Web/DefaultPublishedMediaStore.cs b/src/Umbraco.Web/DefaultPublishedMediaStore.cs index afe1aa24d7..2a35ca3619 100644 --- a/src/Umbraco.Web/DefaultPublishedMediaStore.cs +++ b/src/Umbraco.Web/DefaultPublishedMediaStore.cs @@ -11,6 +11,7 @@ using Umbraco.Core; using Umbraco.Core.Dynamics; using Umbraco.Core.Models; using Umbraco.Web.Models; +using UmbracoExamine; using umbraco; using umbraco.cms.businesslogic; using ContentType = umbraco.cms.businesslogic.ContentType; @@ -107,7 +108,7 @@ namespace Umbraco.Web { //first check in Examine as this is WAY faster var criteria = searchProvider.CreateSearchCriteria("media"); - var filter = criteria.Id(id); + var filter = criteria.Id(id).Not().Field(UmbracoContentIndexer.IndexPathFieldName, "-21"); var results = searchProvider.Search(filter.Compile()); if (results.Any()) { diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/Search/ExamineEvents.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/Search/ExamineEvents.cs index f04cb9ede1..45abc5be22 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/Search/ExamineEvents.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/Search/ExamineEvents.cs @@ -38,11 +38,14 @@ namespace umbraco.presentation.umbraco.Search // in the core. This is only temporary to get this task completed for 6.0: // http://issues.umbraco.org/issue/U4-1530 MediaService.Saved += MediaService_Saved; - MediaService.Trashed += MediaService_Trashed; + MediaService.Deleted += MediaService_Deleted; MediaService.Moved += MediaService_Moved; + MediaService.Trashed += MediaService_Trashed; + ContentService.Saved += ContentService_Saved; - ContentService.Trashed += ContentService_Trashed; + ContentService.Deleted += ContentService_Deleted; ContentService.Moved += ContentService_Moved; + ContentService.Trashed += ContentService_Trashed; //bind to examine events var contentIndexer = ExamineManager.Instance.IndexProviderCollection["InternalIndexer"] as UmbracoContentIndexer; @@ -59,36 +62,47 @@ namespace umbraco.presentation.umbraco.Search void ContentService_Trashed(IContentService sender, Umbraco.Core.Events.MoveEventArgs e) { - ExamineManager.Instance.DeleteFromIndex(e.Entity.Id.ToString(), - ExamineManager.Instance.IndexProviderCollection - .OfType() - .Where(x => x.EnableDefaultEventHandler)); - + IndexContent(e.Entity); } void MediaService_Trashed(IMediaService sender, Umbraco.Core.Events.MoveEventArgs e) { - ExamineManager.Instance.DeleteFromIndex(e.Entity.Id.ToString(), - ExamineManager.Instance.IndexProviderCollection - .OfType() - .Where(x => x.EnableDefaultEventHandler)); + IndexMedia(e.Entity); } void ContentService_Moved(IContentService sender, Umbraco.Core.Events.MoveEventArgs e) { - IndexConent(e.Entity); + IndexContent(e.Entity); + } + + void ContentService_Deleted(IContentService sender, Umbraco.Core.Events.DeleteEventArgs e) + { + e.DeletedEntities.ForEach( + content => + ExamineManager.Instance.DeleteFromIndex( + content.Id.ToString(), + ExamineManager.Instance.IndexProviderCollection.OfType().Where(x => x.EnableDefaultEventHandler))); } void ContentService_Saved(IContentService sender, Umbraco.Core.Events.SaveEventArgs e) { - e.SavedEntities.ForEach(IndexConent); + e.SavedEntities.ForEach(IndexContent); } void MediaService_Moved(IMediaService sender, Umbraco.Core.Events.MoveEventArgs e) { IndexMedia(e.Entity); } - + + void MediaService_Deleted(IMediaService sender, Umbraco.Core.Events.DeleteEventArgs e) + { + e.DeletedEntities.ForEach( + media => + ExamineManager.Instance.DeleteFromIndex( + media.Id.ToString(), + ExamineManager.Instance.IndexProviderCollection.OfType().Where(x => x.EnableDefaultEventHandler))); + } + void MediaService_Saved(IMediaService sender, Umbraco.Core.Events.SaveEventArgs e) { e.SavedEntities.ForEach(IndexMedia); @@ -101,7 +115,7 @@ namespace umbraco.presentation.umbraco.Search ExamineManager.Instance.IndexProviderCollection.OfType().Where(x => x.EnableDefaultEventHandler)); } - private void IndexConent(IContent sender) + private void IndexContent(IContent sender) { ExamineManager.Instance.ReIndexNode( sender.ToXml(), "content",