using System; using System.Linq; using System.Threading; using NUnit.Framework; using Umbraco.Cms.Core.Cache; using Umbraco.Cms.Core.Events; using Umbraco.Cms.Core.Models; using Umbraco.Cms.Core.Models.Membership; using Umbraco.Cms.Core.Routing; using Umbraco.Cms.Core.Services; using Umbraco.Cms.Core.Web; using Umbraco.Cms.Tests.Common.Testing; using Umbraco.Cms.Tests.Integration.Testing; namespace Umbraco.Cms.Tests.Integration.Cache { [TestFixture] [UmbracoTest(Boot = true)] public class DistributedCacheBinderTests : UmbracoIntegrationTest { private IUserService UserService => GetRequiredService(); private ILocalizationService LocalizationService => GetRequiredService(); private IDataTypeService DataTypeService => GetRequiredService(); private IFileService FileService => GetRequiredService(); private IContentTypeService ContentTypeService => GetRequiredService(); private IMediaTypeService MediaTypeService => GetRequiredService(); private IDomainService DomainService => GetRequiredService(); private IMemberTypeService MemberTypeService => GetRequiredService(); private IMacroService MacroService => GetRequiredService(); private IMemberService MemberService => GetRequiredService(); private IMemberGroupService MemberGroupService => GetRequiredService(); private IMediaService MediaService => GetRequiredService(); private IContentService ContentService => GetRequiredService(); private IPublicAccessService PublicAccessService => GetRequiredService(); private IRelationService RelationService => GetRequiredService(); private UriUtility UriUtility => GetRequiredService(); private IUmbracoContextFactory UmbracoContextFactory => GetRequiredService(); [Test] public void Can_Find_All_Event_Handlers() { var definitions = new IEventDefinition[] { new EventDefinition>(null, FileService, new SaveEventArgs(Enumerable.Empty())), new EventDefinition>(null, FileService, new DeleteEventArgs(Enumerable.Empty())), new EventDefinition>(null, ContentTypeService, new SaveEventArgs(Enumerable.Empty())), new EventDefinition>(null, ContentTypeService, new DeleteEventArgs(Enumerable.Empty())), new EventDefinition>(null, MediaTypeService, new SaveEventArgs(Enumerable.Empty())), new EventDefinition>(null, MediaTypeService, new DeleteEventArgs(Enumerable.Empty())), new EventDefinition>(null, MemberTypeService, new SaveEventArgs(Enumerable.Empty())), new EventDefinition>(null, MemberTypeService, new DeleteEventArgs(Enumerable.Empty())), new EventDefinition>(null, FileService, new SaveEventArgs(Enumerable.Empty())), new EventDefinition>(null, FileService, new DeleteEventArgs(Enumerable.Empty())), // not managed //new EventDefinition>(null, ContentService, new SaveEventArgs(Enumerable.Empty()), "SavedBlueprint"), //new EventDefinition>(null, ContentService, new DeleteEventArgs(Enumerable.Empty()), "DeletedBlueprint"), new EventDefinition>(null, RelationService, new SaveEventArgs(Enumerable.Empty())), new EventDefinition>(null, RelationService, new DeleteEventArgs(Enumerable.Empty())), new EventDefinition>(null, RelationService, new SaveEventArgs(Enumerable.Empty())), new EventDefinition>(null, 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, ContentTypeService, new SaveEventArgs(Enumerable.Empty()), "Saved"), }; Assert.DoesNotThrow(() => { var refreshers = new DistributedCacheBinder(null, UmbracoContextFactory, null); refreshers.HandleEvents(definitions); }); } } }