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 <IContentTypeService, SaveEventArgs <IContentType> >(null, Current.Services.ContentTypeService, new SaveEventArgs <IContentType>(Enumerable.Empty <IContentType>()), "Saved"),
            };

            // just assert it does not throw
            var refreshers = new DistributedCacheBinder(null, null);

            refreshers.HandleEvents(definitions);
        }
Esempio n. 2
0
        public void Teardown()
        {
            _distributedCacheBinder?.UnbindEvents();
            _distributedCacheBinder = null;

            _onPublishedAssertAction    = null;
            ContentService.Published   -= OnPublishedAssert;
            SafeXmlReaderWriter.Cloning = null;
        }
Esempio n. 3
0
        public override void TearDown()
        {
            base.TearDown();

            _distributedCacheBinder?.UnbindEvents();
            _distributedCacheBinder = null;

            _onPublishedAssertAction  = null;
            ContentService.Published -= OnPublishedAssert;
        }
Esempio n. 4
0
        public void OnlyHandlesOnContentTypeEvent()
        {
            var definitions = new IEventDefinition[]
            {
                new EventDefinition <IContentTypeService, ContentTypeChange <IContentType> .EventArgs>(null, Current.Services.ContentTypeService, new ContentTypeChange <IContentType> .EventArgs(Enumerable.Empty <ContentTypeChange <IContentType> >()), "Changed"),
                new EventDefinition <IContentTypeService, SaveEventArgs <IContentType> >(null, Current.Services.ContentTypeService, new SaveEventArgs <IContentType>(Enumerable.Empty <IContentType>()), "Saved"),
                new EventDefinition <IContentTypeService, ContentTypeChange <IContentType> .EventArgs>(null, Current.Services.ContentTypeService, new ContentTypeChange <IContentType> .EventArgs(Enumerable.Empty <ContentTypeChange <IContentType> >()), "Changed"),
                new EventDefinition <IContentTypeService, SaveEventArgs <IContentType> >(null, Current.Services.ContentTypeService, new SaveEventArgs <IContentType>(Enumerable.Empty <IContentType>()), "Saved"),
            };
            var result = DistributedCacheBinder.GetReducedEventList(definitions);

            Assert.AreEqual(1, result.Count());
        }
        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 <IContentTypeService, SaveEventArgs <IContentType> >(null, Current.Services.ContentTypeService, new SaveEventArgs <IContentType>(Enumerable.Empty <IContentType>()), "Saved"),
            };

            var umbracoContextFactory = new UmbracoContextFactory(
                new TestUmbracoContextAccessor(),
                Mock.Of <IPublishedSnapshotService>(),
                new TestVariationContextAccessor(),
                new TestDefaultCultureAccessor(),
                TestObjects.GetUmbracoSettings(),
                TestObjects.GetGlobalSettings(),
                new UrlProviderCollection(Enumerable.Empty <IUrlProvider>()),
                new MediaUrlProviderCollection(Enumerable.Empty <IMediaUrlProvider>()),
                Mock.Of <IUserService>());

            // just assert it does not throw
            var refreshers = new DistributedCacheBinder(null, umbracoContextFactory, null);

            refreshers.HandleEvents(definitions);
        }
Esempio n. 6
0
        public void TestScope(bool complete)
        {
            var umbracoContext = GetUmbracoContextNu("http://example.com/", setSingleton: true);

            // wire cache refresher
            _distributedCacheBinder = new DistributedCacheBinder(new DistributedCache(), Mock.Of <IUmbracoContextFactory>(), Mock.Of <ILogger>());
            _distributedCacheBinder.BindEvents(true);

            // create document type, document
            var contentType = new ContentType(-1)
            {
                Alias = "CustomDocument", Name = "Custom Document"
            };

            Current.Services.ContentTypeService.Save(contentType);
            var item = new Content("name", -1, contentType);

            // event handler
            var evented = 0;

            _onPublishedAssertAction = () =>
            {
                evented++;

                var e = umbracoContext.Content.GetById(item.Id);

                // during events, due to LiveSnapshot, we see the changes
                Assert.IsNotNull(e);
                Assert.AreEqual("changed", e.Name());
            };

            using (var scope = ScopeProvider.CreateScope())
            {
                Current.Services.ContentService.SaveAndPublish(item);
                scope.Complete();
            }

            // been created
            var x = umbracoContext.Content.GetById(item.Id);

            Assert.IsNotNull(x);
            Assert.AreEqual("name", x.Name());

            ContentService.Published += OnPublishedAssert;

            using (var scope = ScopeProvider.CreateScope())
            {
                item.Name = "changed";
                Current.Services.ContentService.SaveAndPublish(item);

                if (complete)
                {
                    scope.Complete();
                }
            }

            // only 1 event occuring because we are publishing twice for the same event for
            // the same object and the scope deduplicates the events (uses the latest)
            Assert.AreEqual(complete ? 1 : 0, evented);

            // after the scope,
            // if completed, we see the changes
            // else changes have been rolled back
            x = umbracoContext.Content.GetById(item.Id);
            Assert.IsNotNull(x);
            Assert.AreEqual(complete ? "changed" : "name", x.Name());
        }
Esempio n. 7
0
        public void TestScope(bool complete)
        {
            var umbracoContext = GetUmbracoContext("http://example.com/", setSingleton: true);

            // sanity checks
            Assert.AreSame(umbracoContext, Umbraco.Web.Composing.Current.UmbracoContext);
            Assert.AreSame(XmlStore, ((PublishedContentCache)umbracoContext.ContentCache).XmlStore);

            // settings
            var contentMock = Mock.Get(Factory.GetInstance <IUmbracoSettingsSection>().Content);

            contentMock.Setup(x => x.XmlCacheEnabled).Returns(false);

            // create document type, document
            var contentType = new ContentType(-1)
            {
                Alias = "CustomDocument", Name = "Custom Document"
            };

            Current.Services.ContentTypeService.Save(contentType);
            var item = new Content("name", -1, contentType);

            // wire cache refresher
            _distributedCacheBinder = new DistributedCacheBinder(new DistributedCache(), Mock.Of <ILogger>());
            _distributedCacheBinder.BindEvents(true);

            // check xml in context = "before"
            var xml            = XmlInContext;
            var beforeXml      = xml;
            var beforeOuterXml = beforeXml.OuterXml;

            Console.WriteLine("Xml Before:");
            Console.WriteLine(xml.OuterXml);

            // event handler
            var evented = 0;

            _onPublishedAssertAction = () =>
            {
                evented++;

                // should see the changes in context, not in master
                xml = XmlInContext;
                Assert.AreNotSame(beforeXml, xml);
                Console.WriteLine("Xml Event:");
                Console.WriteLine(xml.OuterXml);
                var node = xml.GetElementById(item.Id.ToString());
                Assert.IsNotNull(node);

                xml = XmlMaster;
                Assert.AreSame(beforeXml, xml);
                Assert.AreEqual(beforeOuterXml, xml.OuterXml);
            };

            ContentService.Published += OnPublishedAssert;

            using (var scope = ScopeProvider.CreateScope())
            {
                Current.Services.ContentService.SaveAndPublish(item); // should create an xml clone
                item.Name = "changed";
                Current.Services.ContentService.SaveAndPublish(item); // should re-use the xml clone

                // this should never change
                Assert.AreEqual(beforeOuterXml, beforeXml.OuterXml);

                // this does not change, other thread don't see the changes
                xml = XmlMaster;
                Assert.AreSame(beforeXml, xml);
                Console.WriteLine("XmlInternal During:");
                Console.WriteLine(xml.OuterXml);
                Assert.AreEqual(beforeOuterXml, xml.OuterXml);

                // this does not change during the scope (only in events)
                // because it is the events that trigger the changes
                xml = XmlInContext;
                Assert.IsNotNull(xml);
                Assert.AreSame(beforeXml, xml);
                Assert.AreEqual(beforeOuterXml, xml.OuterXml);

                // note
                // this means that, as long as ppl don't create scopes, they'll see their
                // changes right after SaveAndPublish, but if they create scopes,
                // they will have to wail until the scope is completed, ie wait until the
                // events trigger, to use eg GetUrl etc

                if (complete)
                {
                    scope.Complete();
                }
            }

            //The reason why there is only 1 event occuring is because we are publishing twice for the same event for the same
            //object and the scope deduplicates the events(uses the latest)
            Assert.AreEqual(complete? 1 : 0, evented);

            // this should never change
            Assert.AreEqual(beforeOuterXml, beforeXml.OuterXml);

            xml = XmlInContext;
            Console.WriteLine("Xml After:");
            Console.WriteLine(xml.OuterXml);
            if (complete)
            {
                var node = xml.GetElementById(item.Id.ToString());
                Assert.IsNotNull(node);
            }
            else
            {
                Assert.AreSame(beforeXml, xml);
                Assert.AreEqual(beforeOuterXml, xml.OuterXml); // nothing has changed!
            }

            xml = XmlMaster;
            if (complete)
            {
                var node = xml.GetElementById(item.Id.ToString());
                Assert.IsNotNull(node);
            }
            else
            {
                Assert.AreSame(beforeXml, xml);
                Assert.AreEqual(beforeOuterXml, xml.OuterXml); // nothing has changed!
            }
        }
Esempio n. 8
0
        public void TestScopeMany(bool complete)
        {
            var umbracoContext = GetUmbracoContext("http://example.com/", setSingleton: true);

            // sanity checks
            Assert.AreSame(umbracoContext, Umbraco.Web.Composing.Current.UmbracoContext);
            Assert.AreSame(XmlStore, ((PublishedContentCache)umbracoContext.ContentCache).XmlStore);

            // settings
            var settings    = SettingsForTests.GenerateMockUmbracoSettings();
            var contentMock = Mock.Get(Factory.GetInstance <IUmbracoSettingsSection>().Content);

            contentMock.Setup(x => x.XmlCacheEnabled).Returns(false);

            // create document type
            var contentType = new ContentType(-1)
            {
                Alias = "CustomDocument", Name = "Custom Document"
            };

            Current.Services.ContentTypeService.Save(contentType);

            // wire cache refresher
            _distributedCacheBinder = new DistributedCacheBinder(new DistributedCache(), Mock.Of <ILogger>());
            _distributedCacheBinder.BindEvents(true);

            // check xml in context = "before"
            var       xml            = XmlInContext;
            var       beforeXml      = xml;
            var       beforeOuterXml = beforeXml.OuterXml;
            var       item           = new Content("name", -1, contentType);
            const int count          = 10;
            var       ids            = new int[count];
            var       clones         = 0;

            SafeXmlReaderWriter.Cloning = () => { clones++; };

            Console.WriteLine("Xml Before:");
            Console.WriteLine(xml.OuterXml);

            using (var scope = ScopeProvider.CreateScope())
            {
                Current.Services.ContentService.SaveAndPublish(item);

                for (var i = 0; i < count; i++)
                {
                    var temp = new Content("content_" + i, -1, contentType);
                    Current.Services.ContentService.SaveAndPublish(temp);
                    ids[i] = temp.Id;
                }

                // this should never change
                Assert.AreEqual(beforeOuterXml, beforeXml.OuterXml);

                xml = XmlMaster;
                Assert.IsNotNull(xml);
                Console.WriteLine("Xml InScope (before complete):");
                Console.WriteLine(xml.OuterXml);
                Assert.AreEqual(beforeOuterXml, xml.OuterXml);

                if (complete)
                {
                    scope.Complete();
                }

                xml = XmlMaster;
                Assert.IsNotNull(xml);
                Console.WriteLine("Xml InScope (after complete):");
                Console.WriteLine(xml.OuterXml);
                Assert.AreEqual(beforeOuterXml, xml.OuterXml);
            }

            var scopeProvider = ScopeProvider;

            Assert.IsNotNull(scopeProvider);
            // ambient scope may be null, or maybe not, depending on whether the code that
            // was called did proper scoped work, or some direct (NoScope) use of the database
            Assert.IsNull(scopeProvider.AmbientContext);

            // limited number of clones!
            Assert.AreEqual(complete ? 1 : 0, clones);

            // this should never change
            Assert.AreEqual(beforeOuterXml, beforeXml.OuterXml);

            xml = XmlMaster;
            Assert.IsNotNull(xml);
            Console.WriteLine("Xml After:");
            Console.WriteLine(xml.OuterXml);
            if (complete)
            {
                var node = xml.GetElementById(item.Id.ToString());
                Assert.IsNotNull(node);

                for (var i = 0; i < 10; i++)
                {
                    node = xml.GetElementById(ids[i].ToString());
                    Assert.IsNotNull(node);
                }
            }
            else
            {
                Assert.AreEqual(beforeOuterXml, xml.OuterXml); // nothing has changed!
            }
        }
        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<IContent>.AssignedPermissions += CacheRefresherEventHandler_AssignedPermissions;

                new EventDefinition <IUserService, SaveEventArgs <IUser> >(null, serviceContext.UserService, new SaveEventArgs <IUser>(Enumerable.Empty <IUser>())),
                new EventDefinition <IUserService, DeleteEventArgs <IUser> >(null, serviceContext.UserService, new DeleteEventArgs <IUser>(Enumerable.Empty <IUser>())),
                new EventDefinition <IUserService, SaveEventArgs <UserGroupWithUsers> >(null, serviceContext.UserService, new SaveEventArgs <UserGroupWithUsers>(Enumerable.Empty <UserGroupWithUsers>())),
                new EventDefinition <IUserService, DeleteEventArgs <IUserGroup> >(null, serviceContext.UserService, new DeleteEventArgs <IUserGroup>(Enumerable.Empty <IUserGroup>())),

                new EventDefinition <ILocalizationService, SaveEventArgs <IDictionaryItem> >(null, serviceContext.LocalizationService, new SaveEventArgs <IDictionaryItem>(Enumerable.Empty <IDictionaryItem>())),
                new EventDefinition <ILocalizationService, DeleteEventArgs <IDictionaryItem> >(null, serviceContext.LocalizationService, new DeleteEventArgs <IDictionaryItem>(Enumerable.Empty <IDictionaryItem>())),

                new EventDefinition <IDataTypeService, SaveEventArgs <IDataType> >(null, serviceContext.DataTypeService, new SaveEventArgs <IDataType>(Enumerable.Empty <IDataType>())),
                new EventDefinition <IDataTypeService, DeleteEventArgs <IDataType> >(null, serviceContext.DataTypeService, new DeleteEventArgs <IDataType>(Enumerable.Empty <IDataType>())),

                new EventDefinition <IFileService, SaveEventArgs <Stylesheet> >(null, serviceContext.FileService, new SaveEventArgs <Stylesheet>(Enumerable.Empty <Stylesheet>())),
                new EventDefinition <IFileService, DeleteEventArgs <Stylesheet> >(null, serviceContext.FileService, new DeleteEventArgs <Stylesheet>(Enumerable.Empty <Stylesheet>())),

                new EventDefinition <IDomainService, SaveEventArgs <IDomain> >(null, serviceContext.DomainService, new SaveEventArgs <IDomain>(Enumerable.Empty <IDomain>())),
                new EventDefinition <IDomainService, DeleteEventArgs <IDomain> >(null, serviceContext.DomainService, new DeleteEventArgs <IDomain>(Enumerable.Empty <IDomain>())),

                new EventDefinition <ILocalizationService, SaveEventArgs <ILanguage> >(null, serviceContext.LocalizationService, new SaveEventArgs <ILanguage>(Enumerable.Empty <ILanguage>())),
                new EventDefinition <ILocalizationService, DeleteEventArgs <ILanguage> >(null, serviceContext.LocalizationService, new DeleteEventArgs <ILanguage>(Enumerable.Empty <ILanguage>())),

                new EventDefinition <IContentTypeService, SaveEventArgs <IContentType> >(null, serviceContext.ContentTypeService, new SaveEventArgs <IContentType>(Enumerable.Empty <IContentType>())),
                new EventDefinition <IContentTypeService, DeleteEventArgs <IContentType> >(null, serviceContext.ContentTypeService, new DeleteEventArgs <IContentType>(Enumerable.Empty <IContentType>())),
                new EventDefinition <IMediaTypeService, SaveEventArgs <IMediaType> >(null, serviceContext.MediaTypeService, new SaveEventArgs <IMediaType>(Enumerable.Empty <IMediaType>())),
                new EventDefinition <IMediaTypeService, DeleteEventArgs <IMediaType> >(null, serviceContext.MediaTypeService, new DeleteEventArgs <IMediaType>(Enumerable.Empty <IMediaType>())),

                new EventDefinition <IMemberTypeService, SaveEventArgs <IMemberType> >(null, serviceContext.MemberTypeService, new SaveEventArgs <IMemberType>(Enumerable.Empty <IMemberType>())),
                new EventDefinition <IMemberTypeService, DeleteEventArgs <IMemberType> >(null, serviceContext.MemberTypeService, new DeleteEventArgs <IMemberType>(Enumerable.Empty <IMemberType>())),

                new EventDefinition <IFileService, SaveEventArgs <ITemplate> >(null, serviceContext.FileService, new SaveEventArgs <ITemplate>(Enumerable.Empty <ITemplate>())),
                new EventDefinition <IFileService, DeleteEventArgs <ITemplate> >(null, serviceContext.FileService, new DeleteEventArgs <ITemplate>(Enumerable.Empty <ITemplate>())),

                new EventDefinition <IMacroService, SaveEventArgs <IMacro> >(null, serviceContext.MacroService, new SaveEventArgs <IMacro>(Enumerable.Empty <IMacro>())),
                new EventDefinition <IMacroService, DeleteEventArgs <IMacro> >(null, serviceContext.MacroService, new DeleteEventArgs <IMacro>(Enumerable.Empty <IMacro>())),

                new EventDefinition <IMemberService, SaveEventArgs <IMember> >(null, serviceContext.MemberService, new SaveEventArgs <IMember>(Enumerable.Empty <IMember>())),
                new EventDefinition <IMemberService, DeleteEventArgs <IMember> >(null, serviceContext.MemberService, new DeleteEventArgs <IMember>(Enumerable.Empty <IMember>())),

                new EventDefinition <IMemberGroupService, SaveEventArgs <IMemberGroup> >(null, serviceContext.MemberGroupService, new SaveEventArgs <IMemberGroup>(Enumerable.Empty <IMemberGroup>())),
                new EventDefinition <IMemberGroupService, DeleteEventArgs <IMemberGroup> >(null, serviceContext.MemberGroupService, new DeleteEventArgs <IMemberGroup>(Enumerable.Empty <IMemberGroup>())),

                new EventDefinition <IMediaService, SaveEventArgs <IMedia> >(null, serviceContext.MediaService, new SaveEventArgs <IMedia>(Enumerable.Empty <IMedia>())),
                new EventDefinition <IMediaService, DeleteEventArgs <IMedia> >(null, serviceContext.MediaService, new DeleteEventArgs <IMedia>(Enumerable.Empty <IMedia>())),
                new EventDefinition <IMediaService, MoveEventArgs <IMedia> >(null, serviceContext.MediaService, new MoveEventArgs <IMedia>(new MoveEventInfo <IMedia>(null, "", -1)), "Moved"),
                new EventDefinition <IMediaService, MoveEventArgs <IMedia> >(null, serviceContext.MediaService, new MoveEventArgs <IMedia>(new MoveEventInfo <IMedia>(null, "", -1)), "Trashed"),
                new EventDefinition <IMediaService, RecycleBinEventArgs>(null, serviceContext.MediaService, new RecycleBinEventArgs(Guid.NewGuid())),

                new EventDefinition <IContentService, SaveEventArgs <IContent> >(null, serviceContext.ContentService, new SaveEventArgs <IContent>(Enumerable.Empty <IContent>()), "Saved"),
                new EventDefinition <IContentService, DeleteEventArgs <IContent> >(null, serviceContext.ContentService, new DeleteEventArgs <IContent>(Enumerable.Empty <IContent>()), "Deleted"),

                // not managed
                //new EventDefinition<IContentService, SaveEventArgs<IContent>>(null, serviceContext.ContentService, new SaveEventArgs<IContent>(Enumerable.Empty<IContent>()), "SavedBlueprint"),
                //new EventDefinition<IContentService, DeleteEventArgs<IContent>>(null, serviceContext.ContentService, new DeleteEventArgs<IContent>(Enumerable.Empty<IContent>()), "DeletedBlueprint"),

                new EventDefinition <IContentService, CopyEventArgs <IContent> >(null, serviceContext.ContentService, new CopyEventArgs <IContent>(null, null, -1)),
                new EventDefinition <IContentService, MoveEventArgs <IContent> >(null, serviceContext.ContentService, new MoveEventArgs <IContent>(new MoveEventInfo <IContent>(null, "", -1)), "Trashed"),
                new EventDefinition <IContentService, RecycleBinEventArgs>(null, serviceContext.ContentService, new RecycleBinEventArgs(Guid.NewGuid())),
                new EventDefinition <IContentService, PublishEventArgs <IContent> >(null, serviceContext.ContentService, new PublishEventArgs <IContent>(Enumerable.Empty <IContent>()), "Published"),
                new EventDefinition <IContentService, PublishEventArgs <IContent> >(null, serviceContext.ContentService, new PublishEventArgs <IContent>(Enumerable.Empty <IContent>()), "Unpublished"),

                new EventDefinition <IPublicAccessService, SaveEventArgs <PublicAccessEntry> >(null, serviceContext.PublicAccessService, new SaveEventArgs <PublicAccessEntry>(Enumerable.Empty <PublicAccessEntry>())),
                new EventDefinition <IPublicAccessService, DeleteEventArgs <PublicAccessEntry> >(null, serviceContext.PublicAccessService, new DeleteEventArgs <PublicAccessEntry>(Enumerable.Empty <PublicAccessEntry>())),

                new EventDefinition <IRelationService, SaveEventArgs <IRelationType> >(null, serviceContext.RelationService, new SaveEventArgs <IRelationType>(Enumerable.Empty <IRelationType>())),
                new EventDefinition <IRelationService, DeleteEventArgs <IRelationType> >(null, serviceContext.RelationService, new DeleteEventArgs <IRelationType>(Enumerable.Empty <IRelationType>())),

                new EventDefinition <IRelationService, SaveEventArgs <IRelationType> >(null, serviceContext.RelationService, new SaveEventArgs <IRelationType>(Enumerable.Empty <IRelationType>())),
                new EventDefinition <IRelationService, DeleteEventArgs <IRelationType> >(null, serviceContext.RelationService, new DeleteEventArgs <IRelationType>(Enumerable.Empty <IRelationType>())),
            };

            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");
        }
        public void GroupsContentTypeEvents()
        {
            var num                = 30;
            var contentTypes       = Enumerable.Repeat(MockedContentTypes.CreateBasicContentType(), num);
            var mediaTypes         = Enumerable.Repeat(MockedContentTypes.CreateImageMediaType(), num);
            var memberTypes        = Enumerable.Repeat(MockedContentTypes.CreateSimpleMemberType(), num);
            var definitionsContent = contentTypes.SelectMany(x => new IEventDefinition[]
            {
                new EventDefinition <IContentTypeService, ContentTypeChange <IContentType> .EventArgs>(null, Current.Services.ContentTypeService, new ContentTypeChange <IContentType> .EventArgs(new ContentTypeChange <IContentType>(x, ContentTypeChangeTypes.Create)), "Changed"),
                new EventDefinition <IContentTypeService, SaveEventArgs <IContentType> >(null, Current.Services.ContentTypeService, new SaveEventArgs <IContentType>(x), "Saved"),
            });

            var definitionsMedia = mediaTypes.SelectMany(x => new IEventDefinition[]
            {
                new EventDefinition <IMediaTypeService, ContentTypeChange <IMediaType> .EventArgs>(null, Current.Services.MediaTypeService, new ContentTypeChange <IMediaType> .EventArgs(new ContentTypeChange <IMediaType>(x, ContentTypeChangeTypes.Create)), "Changed"),
                new EventDefinition <IMediaTypeService, SaveEventArgs <IMediaType> >(null, Current.Services.MediaTypeService, new SaveEventArgs <IMediaType>(x), "Saved"),
            });
            var definitionsMember = memberTypes.SelectMany(x => new IEventDefinition[]
            {
                new EventDefinition <IMemberTypeService, ContentTypeChange <IMemberType> .EventArgs>(null, Current.Services.MemberTypeService, new ContentTypeChange <IMemberType> .EventArgs(new ContentTypeChange <IMemberType>(x, ContentTypeChangeTypes.Create)), "Changed"),
                new EventDefinition <IMemberTypeService, SaveEventArgs <IMemberType> >(null, Current.Services.MemberTypeService, new SaveEventArgs <IMemberType>(x), "Saved"),
            });

            var definitions = new List <IEventDefinition>();

            definitions.AddRange(definitionsContent);
            definitions.AddRange(definitionsMedia);
            definitions.AddRange(definitionsMember);

            var result = DistributedCacheBinder.GetGroupedEventList(definitions);

            Assert.Multiple(() =>
            {
                Assert.AreEqual(num * 6, definitions.Count(), "Precondition is we have many definitions");
                Assert.AreEqual(6, result.Count(), "Unexpected number of reduced definitions");
                foreach (var eventDefinition in result)
                {
                    if (eventDefinition.Args is SaveEventArgs <IContentType> saveContentEventArgs)
                    {
                        Assert.AreEqual(num, saveContentEventArgs.SavedEntities.Count());
                    }

                    if (eventDefinition.Args is ContentTypeChange <IContentType> .EventArgs changeContentEventArgs)
                    {
                        Assert.AreEqual(num, changeContentEventArgs.Changes.Count());
                    }

                    if (eventDefinition.Args is SaveEventArgs <IMediaType> saveMediaEventArgs)
                    {
                        Assert.AreEqual(num, saveMediaEventArgs.SavedEntities.Count());
                    }

                    if (eventDefinition.Args is ContentTypeChange <IMediaType> .EventArgs changeMediaEventArgs)
                    {
                        Assert.AreEqual(num, changeMediaEventArgs.Changes.Count());
                    }

                    if (eventDefinition.Args is SaveEventArgs <IMemberType> saveMemberEventArgs)
                    {
                        Assert.AreEqual(num, saveMemberEventArgs.SavedEntities.Count());
                    }

                    if (eventDefinition.Args is ContentTypeChange <IMemberType> .EventArgs changeMemberEventArgs)
                    {
                        Assert.AreEqual(num, changeMemberEventArgs.Changes.Count());
                    }
                }
            });
        }
Esempio n. 11
0
        public void DefaultRepositoryCachePolicy(bool complete)
        {
            var scopeProvider = ScopeProvider;
            var service       = Current.Services.UserService;
            var globalCache   = Current.AppCaches.IsolatedCaches.GetOrCreate(typeof(IUser));

            var user = (IUser) new User("name", "email", "username", "rawPassword");

            service.Save(user);

            // global cache contains the entity
            var globalCached = (IUser)globalCache.Get(GetCacheIdKey <IUser>(user.Id), () => null);

            Assert.IsNotNull(globalCached);
            Assert.AreEqual(user.Id, globalCached.Id);
            Assert.AreEqual("name", globalCached.Name);

            // get user again - else we'd modify the one that's in the cache
            user = service.GetUserById(user.Id);

            _distributedCacheBinder = new DistributedCacheBinder(new DistributedCache(), Mock.Of <IUmbracoContextFactory>(), Mock.Of <ILogger>());
            _distributedCacheBinder.BindEvents(true);

            Assert.IsNull(scopeProvider.AmbientScope);
            using (var scope = scopeProvider.CreateScope(repositoryCacheMode: RepositoryCacheMode.Scoped))
            {
                Assert.IsInstanceOf <Core.Scoping.Scope>(scope);
                Assert.IsNotNull(scopeProvider.AmbientScope);
                Assert.AreSame(scope, scopeProvider.AmbientScope);

                // scope has its own isolated cache
                var scopedCache = scope.IsolatedCaches.GetOrCreate(typeof(IUser));
                Assert.AreNotSame(globalCache, scopedCache);

                user.Name = "changed";
                service.Save(user);

                // scoped cache contains the "new" entity
                var scopeCached = (IUser)scopedCache.Get(GetCacheIdKey <IUser>(user.Id), () => null);
                Assert.IsNotNull(scopeCached);
                Assert.AreEqual(user.Id, scopeCached.Id);
                Assert.AreEqual("changed", scopeCached.Name);

                // global cache is unchanged
                globalCached = (IUser)globalCache.Get(GetCacheIdKey <IUser>(user.Id), () => null);
                Assert.IsNotNull(globalCached);
                Assert.AreEqual(user.Id, globalCached.Id);
                Assert.AreEqual("name", globalCached.Name);

                if (complete)
                {
                    scope.Complete();
                }
            }
            Assert.IsNull(scopeProvider.AmbientScope);

            globalCached = (IUser)globalCache.Get(GetCacheIdKey <IUser>(user.Id), () => null);
            if (complete)
            {
                // global cache has been cleared
                Assert.IsNull(globalCached);
            }
            else
            {
                // global cache has *not* been cleared
                Assert.IsNotNull(globalCached);
            }

            // get again, updated if completed
            user = service.GetUserById(user.Id);
            Assert.AreEqual(complete ? "changed" : "name", user.Name);

            // global cache contains the entity again
            globalCached = (IUser)globalCache.Get(GetCacheIdKey <IUser>(user.Id), () => null);
            Assert.IsNotNull(globalCached);
            Assert.AreEqual(user.Id, globalCached.Id);
            Assert.AreEqual(complete ? "changed" : "name", globalCached.Name);
        }
Esempio n. 12
0
 public void Teardown()
 {
     _distributedCacheBinder?.UnbindEvents();
     _distributedCacheBinder = null;
 }
Esempio n. 13
0
        public void SingleItemsOnlyRepositoryCachePolicy(bool complete)
        {
            var scopeProvider = ScopeProvider;
            var service       = Current.Services.LocalizationService;
            var globalCache   = Current.AppCaches.IsolatedCaches.GetOrCreate(typeof(IDictionaryItem));

            var lang = (ILanguage) new Language("fr-FR");

            service.Save(lang);

            var item = (IDictionaryItem) new DictionaryItem("item-key");

            item.Translations = new IDictionaryTranslation[]
            {
                new DictionaryTranslation(lang.Id, "item-value"),
            };
            service.Save(item);

            // global cache contains the entity
            var globalCached = (IDictionaryItem)globalCache.Get(GetCacheIdKey <IDictionaryItem>(item.Id), () => null);

            Assert.IsNotNull(globalCached);
            Assert.AreEqual(item.Id, globalCached.Id);
            Assert.AreEqual("item-key", globalCached.ItemKey);

            _distributedCacheBinder = new DistributedCacheBinder(new DistributedCache(), Mock.Of <IUmbracoContextFactory>(), Mock.Of <ILogger>());
            _distributedCacheBinder.BindEvents(true);

            Assert.IsNull(scopeProvider.AmbientScope);
            using (var scope = scopeProvider.CreateScope(repositoryCacheMode: RepositoryCacheMode.Scoped))
            {
                Assert.IsInstanceOf <Core.Scoping.Scope>(scope);
                Assert.IsNotNull(scopeProvider.AmbientScope);
                Assert.AreSame(scope, scopeProvider.AmbientScope);

                // scope has its own isolated cache
                var scopedCache = scope.IsolatedCaches.GetOrCreate(typeof(IDictionaryItem));
                Assert.AreNotSame(globalCache, scopedCache);

                item.ItemKey = "item-changed";
                service.Save(item);

                // scoped cache contains the "new" entity
                var scopeCached = (IDictionaryItem)scopedCache.Get(GetCacheIdKey <IDictionaryItem>(item.Id), () => null);
                Assert.IsNotNull(scopeCached);
                Assert.AreEqual(item.Id, scopeCached.Id);
                Assert.AreEqual("item-changed", scopeCached.ItemKey);

                // global cache is unchanged
                globalCached = (IDictionaryItem)globalCache.Get(GetCacheIdKey <IDictionaryItem>(item.Id), () => null);
                Assert.IsNotNull(globalCached);
                Assert.AreEqual(item.Id, globalCached.Id);
                Assert.AreEqual("item-key", globalCached.ItemKey);

                if (complete)
                {
                    scope.Complete();
                }
            }
            Assert.IsNull(scopeProvider.AmbientScope);

            globalCached = (IDictionaryItem)globalCache.Get(GetCacheIdKey <IDictionaryItem>(item.Id), () => null);
            if (complete)
            {
                // global cache has been cleared
                Assert.IsNull(globalCached);
            }
            else
            {
                // global cache has *not* been cleared
                Assert.IsNotNull(globalCached);
            }

            // get again, updated if completed
            item = service.GetDictionaryItemById(item.Id);
            Assert.AreEqual(complete ? "item-changed" : "item-key", item.ItemKey);

            // global cache contains the entity again
            globalCached = (IDictionaryItem)globalCache.Get(GetCacheIdKey <IDictionaryItem>(item.Id), () => null);
            Assert.IsNotNull(globalCached);
            Assert.AreEqual(item.Id, globalCached.Id);
            Assert.AreEqual(complete ? "item-changed" : "item-key", globalCached.ItemKey);
        }
Esempio n. 14
0
        public void FullDataSetRepositoryCachePolicy(bool complete)
        {
            var scopeProvider = ScopeProvider;
            var service       = Current.Services.LocalizationService;
            var globalCache   = Current.AppCaches.IsolatedCaches.GetOrCreate(typeof(ILanguage));

            var lang = (ILanguage) new Language("fr-FR");

            service.Save(lang);

            // global cache has been flushed, reload
            var globalFullCached = (IEnumerable <ILanguage>)globalCache.Get(GetCacheTypeKey <ILanguage>(), () => null);

            Assert.IsNull(globalFullCached);
            var reload = service.GetLanguageById(lang.Id);

            // global cache contains the entity
            globalFullCached = (IEnumerable <ILanguage>)globalCache.Get(GetCacheTypeKey <ILanguage>(), () => null);
            Assert.IsNotNull(globalFullCached);
            var globalCached = globalFullCached.First(x => x.Id == lang.Id);

            Assert.IsNotNull(globalCached);
            Assert.AreEqual(lang.Id, globalCached.Id);
            Assert.AreEqual("fr-FR", globalCached.IsoCode);

            _distributedCacheBinder = new DistributedCacheBinder(new DistributedCache(), Mock.Of <IUmbracoContextFactory>(), Mock.Of <ILogger>());
            _distributedCacheBinder.BindEvents(true);

            Assert.IsNull(scopeProvider.AmbientScope);
            using (var scope = scopeProvider.CreateScope(repositoryCacheMode: RepositoryCacheMode.Scoped))
            {
                Assert.IsInstanceOf <Core.Scoping.Scope>(scope);
                Assert.IsNotNull(scopeProvider.AmbientScope);
                Assert.AreSame(scope, scopeProvider.AmbientScope);

                // scope has its own isolated cache
                var scopedCache = scope.IsolatedCaches.GetOrCreate(typeof(ILanguage));
                Assert.AreNotSame(globalCache, scopedCache);

                lang.IsoCode = "de-DE";
                service.Save(lang);

                // scoped cache has been flushed, reload
                var scopeFullCached = (IEnumerable <ILanguage>)scopedCache.Get(GetCacheTypeKey <ILanguage>(), () => null);
                Assert.IsNull(scopeFullCached);
                reload = service.GetLanguageById(lang.Id);

                // scoped cache contains the "new" entity
                scopeFullCached = (IEnumerable <ILanguage>)scopedCache.Get(GetCacheTypeKey <ILanguage>(), () => null);
                Assert.IsNotNull(scopeFullCached);
                var scopeCached = scopeFullCached.First(x => x.Id == lang.Id);
                Assert.IsNotNull(scopeCached);
                Assert.AreEqual(lang.Id, scopeCached.Id);
                Assert.AreEqual("de-DE", scopeCached.IsoCode);

                // global cache is unchanged
                globalFullCached = (IEnumerable <ILanguage>)globalCache.Get(GetCacheTypeKey <ILanguage>(), () => null);
                Assert.IsNotNull(globalFullCached);
                globalCached = globalFullCached.First(x => x.Id == lang.Id);
                Assert.IsNotNull(globalCached);
                Assert.AreEqual(lang.Id, globalCached.Id);
                Assert.AreEqual("fr-FR", globalCached.IsoCode);

                if (complete)
                {
                    scope.Complete();
                }
            }
            Assert.IsNull(scopeProvider.AmbientScope);

            globalFullCached = (IEnumerable <ILanguage>)globalCache.Get(GetCacheTypeKey <ILanguage>(), () => null);
            if (complete)
            {
                // global cache has been cleared
                Assert.IsNull(globalFullCached);
            }
            else
            {
                // global cache has *not* been cleared
                Assert.IsNotNull(globalFullCached);
            }

            // get again, updated if completed
            lang = service.GetLanguageById(lang.Id);
            Assert.AreEqual(complete ? "de-DE" : "fr-FR", lang.IsoCode);

            // global cache contains the entity again
            globalFullCached = (IEnumerable <ILanguage>)globalCache.Get(GetCacheTypeKey <ILanguage>(), () => null);
            Assert.IsNotNull(globalFullCached);
            globalCached = globalFullCached.First(x => x.Id == lang.Id);
            Assert.IsNotNull(globalCached);
            Assert.AreEqual(lang.Id, globalCached.Id);
            Assert.AreEqual(complete ? "de-DE" : "fr-FR", lang.IsoCode);
        }