using System; using System.Linq; using System.Threading; using Moq; using NUnit.Framework; using Umbraco.Core.Composing; using Umbraco.Core.Configuration.UmbracoSettings; using Umbraco.Core.Events; using Umbraco.Core.Models; using Umbraco.Core.Models.Membership; using Umbraco.Core.Services; using Umbraco.Tests.Testing; using Umbraco.Tests.Testing.Objects.Accessors; using Umbraco.Web; using Umbraco.Web.Cache; using Umbraco.Web.PublishedCache; using Umbraco.Web.Routing; namespace Umbraco.Tests.Cache { [TestFixture] [UmbracoTest(WithApplication = true)] public class DistributedCacheBinderTests : UmbracoTestBase { protected override void Compose(Composition composition) { base.Compose(composition); // refreshers.HandleEvents wants a UmbracoContext // which wants these composition.RegisterUnique(_ => Mock.Of()); composition.WithCollectionBuilder(); } [Test] public void Can_Find_All_Event_Handlers() { // FIXME: cannot work with mocks // because the events are defined on actual static classes, not on the interfaces, so name matching fails // we should really refactor events entirely - in the meantime, let it be an UmbracoTestBase ;( //var testObjects = new TestObjects(null); //var serviceContext = testObjects.GetServiceContextMock(); var serviceContext = Current.Services; var definitions = new IEventDefinition[] { //I would test these but they are legacy events and we don't need them for deploy, when we migrate to new/better events we can wire up the check //Permission.New += PermissionNew; //Permission.Updated += PermissionUpdated; //Permission.Deleted += PermissionDeleted; //PermissionRepository.AssignedPermissions += CacheRefresherEventHandler_AssignedPermissions; new EventDefinition>(null, serviceContext.UserService, new SaveEventArgs(Enumerable.Empty())), new EventDefinition>(null, serviceContext.UserService, new DeleteEventArgs(Enumerable.Empty())), new EventDefinition>(null, serviceContext.UserService, new SaveEventArgs(Enumerable.Empty())), new EventDefinition>(null, serviceContext.UserService, new DeleteEventArgs(Enumerable.Empty())), new EventDefinition>(null, serviceContext.LocalizationService, new SaveEventArgs(Enumerable.Empty())), new EventDefinition>(null, serviceContext.LocalizationService, new DeleteEventArgs(Enumerable.Empty())), new EventDefinition>(null, serviceContext.DataTypeService, new SaveEventArgs(Enumerable.Empty())), new EventDefinition>(null, serviceContext.DataTypeService, new DeleteEventArgs(Enumerable.Empty())), new EventDefinition>(null, serviceContext.FileService, new SaveEventArgs(Enumerable.Empty())), new EventDefinition>(null, serviceContext.FileService, new DeleteEventArgs(Enumerable.Empty())), new EventDefinition>(null, serviceContext.DomainService, new SaveEventArgs(Enumerable.Empty())), new EventDefinition>(null, serviceContext.DomainService, new DeleteEventArgs(Enumerable.Empty())), new EventDefinition>(null, serviceContext.LocalizationService, new SaveEventArgs(Enumerable.Empty())), new EventDefinition>(null, serviceContext.LocalizationService, new DeleteEventArgs(Enumerable.Empty())), new EventDefinition>(null, serviceContext.ContentTypeService, new SaveEventArgs(Enumerable.Empty())), new EventDefinition>(null, serviceContext.ContentTypeService, new DeleteEventArgs(Enumerable.Empty())), new EventDefinition>(null, serviceContext.MediaTypeService, new SaveEventArgs(Enumerable.Empty())), new EventDefinition>(null, serviceContext.MediaTypeService, new DeleteEventArgs(Enumerable.Empty())), new EventDefinition>(null, serviceContext.MemberTypeService, new SaveEventArgs(Enumerable.Empty())), new EventDefinition>(null, serviceContext.MemberTypeService, new DeleteEventArgs(Enumerable.Empty())), new EventDefinition>(null, serviceContext.FileService, new SaveEventArgs(Enumerable.Empty())), new EventDefinition>(null, serviceContext.FileService, new DeleteEventArgs(Enumerable.Empty())), new EventDefinition>(null, serviceContext.MacroService, new SaveEventArgs(Enumerable.Empty())), new EventDefinition>(null, serviceContext.MacroService, new DeleteEventArgs(Enumerable.Empty())), new EventDefinition>(null, serviceContext.MemberService, new SaveEventArgs(Enumerable.Empty())), new EventDefinition>(null, serviceContext.MemberService, new DeleteEventArgs(Enumerable.Empty())), new EventDefinition>(null, serviceContext.MemberGroupService, new SaveEventArgs(Enumerable.Empty())), new EventDefinition>(null, serviceContext.MemberGroupService, new DeleteEventArgs(Enumerable.Empty())), new EventDefinition>(null, serviceContext.MediaService, new SaveEventArgs(Enumerable.Empty())), new EventDefinition>(null, serviceContext.MediaService, new DeleteEventArgs(Enumerable.Empty())), new EventDefinition>(null, serviceContext.MediaService, new MoveEventArgs(new MoveEventInfo(null, "", -1)), "Moved"), new EventDefinition>(null, serviceContext.MediaService, new MoveEventArgs(new MoveEventInfo(null, "", -1)), "Trashed"), new EventDefinition(null, serviceContext.MediaService, new RecycleBinEventArgs(Guid.NewGuid())), new EventDefinition>(null, serviceContext.ContentService, new SaveEventArgs(Enumerable.Empty()), "Saved"), new EventDefinition>(null, serviceContext.ContentService, new DeleteEventArgs(Enumerable.Empty()), "Deleted"), // not managed //new EventDefinition>(null, serviceContext.ContentService, new SaveEventArgs(Enumerable.Empty()), "SavedBlueprint"), //new EventDefinition>(null, serviceContext.ContentService, new DeleteEventArgs(Enumerable.Empty()), "DeletedBlueprint"), new EventDefinition>(null, serviceContext.ContentService, new CopyEventArgs(null, null, -1)), new EventDefinition>(null, serviceContext.ContentService, new MoveEventArgs(new MoveEventInfo(null, "", -1)), "Trashed"), new EventDefinition(null, serviceContext.ContentService, new RecycleBinEventArgs(Guid.NewGuid())), new EventDefinition>(null, serviceContext.ContentService, new PublishEventArgs(Enumerable.Empty()), "Published"), new EventDefinition>(null, serviceContext.ContentService, new PublishEventArgs(Enumerable.Empty()), "Unpublished"), new EventDefinition>(null, serviceContext.PublicAccessService, new SaveEventArgs(Enumerable.Empty())), new EventDefinition>(null, serviceContext.PublicAccessService, new DeleteEventArgs(Enumerable.Empty())), new EventDefinition>(null, serviceContext.RelationService, new SaveEventArgs(Enumerable.Empty())), new EventDefinition>(null, serviceContext.RelationService, new DeleteEventArgs(Enumerable.Empty())), new EventDefinition>(null, serviceContext.RelationService, new SaveEventArgs(Enumerable.Empty())), new EventDefinition>(null, serviceContext.RelationService, new DeleteEventArgs(Enumerable.Empty())), }; var ok = true; foreach (var definition in definitions) { var found = DistributedCacheBinder.FindHandler(definition); if (found == null) { Console.WriteLine("Couldn't find method for " + definition.EventName + " on " + definition.Sender.GetType()); ok = false; } } Assert.IsTrue(ok, "see log for details"); } [Test] public void CanHandleEvent() { // refreshers.HandleEvents wants a UmbracoContext // which wants an HttpContext, which we build using a SimpleWorkerRequest // which requires these to be non-null var domain = Thread.GetDomain(); if (domain.GetData(".appPath") == null) domain.SetData(".appPath", ""); if (domain.GetData(".appVPath") == null) domain.SetData(".appVPath", ""); // create some event definitions var definitions = new IEventDefinition[] { // works because that event definition maps to an empty handler new EventDefinition>(null, Current.Services.ContentTypeService, new SaveEventArgs(Enumerable.Empty()), "Saved"), }; var umbracoContextFactory = new UmbracoContextFactory( new TestUmbracoContextAccessor(), Mock.Of(), new TestVariationContextAccessor(), new TestDefaultCultureAccessor(), TestObjects.GetUmbracoSettings(), TestObjects.GetGlobalSettings(), new UrlProviderCollection(Enumerable.Empty()), new MediaUrlProviderCollection(Enumerable.Empty()), Mock.Of()); // just assert it does not throw var refreshers = new DistributedCacheBinder(null, umbracoContextFactory, null); refreshers.HandleEvents(definitions); } } }