protected override void Initialize() { base.Initialize(); if (!(PublishedSnapshotService is PublishedSnapshotService)) { var options = new PublishedSnapshotServiceOptions { IgnoreLocalDb = true }; var runtime = Mock.Of <IRuntimeState>(); PublishedSnapshotService = new PublishedSnapshotService( options, null, runtime, ServiceContext, Factory.GetInstance <IPublishedContentTypeFactory>(), null, new TestPublishedSnapshotAccessor(), new TestVariationContextAccessor(), Mock.Of <IProfilingLogger>(), ScopeProvider, Factory.GetInstance <IDocumentRepository>(), Factory.GetInstance <IMediaRepository>(), Factory.GetInstance <IMemberRepository>(), DefaultCultureAccessor, new DatabaseDataSource(new JsonContentNestedDataSerializerFactory()), new GlobalSettings(), Factory.GetInstance <IEntityXmlSerializer>(), Factory.GetInstance <IPublishedModelFactory>(), new UrlSegmentProviderCollection(new[] { new DefaultUrlSegmentProvider() }), new TestSyncBootStateAccessor(SyncBootState.WarmBoot), new JsonContentNestedDataSerializerFactory() ); } }
public override void TearDown() { var profilingLogger = Factory.GetService <IProfilingLogger>(); var timer = profilingLogger?.TraceDuration <TestWithDatabaseBase>("teardown"); // FIXME: move that one up try { // FIXME: should we first kill all scopes? if (Options.Database == UmbracoTestOptions.Database.NewSchemaPerTest) { RemoveDatabaseFile(); } AppDomain.CurrentDomain.SetData("DataDirectory", null); // make sure we dispose of the service to unbind events PublishedSnapshotService?.Dispose(); PublishedSnapshotService = null; } finally { timer?.Dispose(); } base.TearDown(); }
public void UnpublishedNameChanges() { var urlSegmentProvider = new DefaultUrlSegmentProvider(); var contentType = MockedContentTypes.CreateTextPageContentType(); ServiceContext.FileService.SaveTemplate(contentType.DefaultTemplate); ServiceContext.ContentTypeService.Save(contentType); var content = MockedContent.CreateTextpageContent(contentType, "hello", Constants.System.Root); ServiceContext.ContentService.SaveAndPublish(content); var cachedContent = ServiceContext.ContentService.GetById(content.Id); var segment = urlSegmentProvider.GetUrlSegment(cachedContent); // Does a new node work? Assert.AreEqual("hello", segment); content.Name = "goodbye"; cachedContent = ServiceContext.ContentService.GetById(content.Id); segment = urlSegmentProvider.GetUrlSegment(cachedContent); // We didn't save anything, so all should still be the same Assert.AreEqual("hello", segment); ServiceContext.ContentService.Save(content); cachedContent = ServiceContext.ContentService.GetById(content.Id); segment = urlSegmentProvider.GetUrlSegment(cachedContent); // At this point we have saved the new name, but not published. The url should still be the previous name Assert.AreEqual("hello", segment); PublishedSnapshotService.Rebuild(); cachedContent = ServiceContext.ContentService.GetById(content.Id); segment = urlSegmentProvider.GetUrlSegment(cachedContent); // After a rebuild, the unpublished name should still not be the url. // This was previously incorrect, per #11074 Assert.AreEqual("hello", segment); ServiceContext.ContentService.SaveAndPublish(content); cachedContent = ServiceContext.ContentService.GetById(content.Id); segment = urlSegmentProvider.GetUrlSegment(cachedContent); // The page has now been published, so we should see the new url segment Assert.AreEqual("goodbye", segment); PublishedSnapshotService.Rebuild(); cachedContent = ServiceContext.ContentService.GetById(content.Id); segment = urlSegmentProvider.GetUrlSegment(cachedContent); // Just double checking that things remain after a rebuild Assert.AreEqual("goodbye", segment); }
public override void TearDown() { if (_service == null) { return; } _service.Dispose(); _service = null; }
public XmlDataIntegrityController(IPublishedSnapshotService publishedSnapshotService) { if (publishedSnapshotService == null) { throw new ArgumentNullException(nameof(publishedSnapshotService)); } _publishedSnapshotService = publishedSnapshotService as PublishedSnapshotService; if (_publishedSnapshotService == null) { throw new NotSupportedException("Unsupported IPublishedSnapshotService, only the Xml one is supported."); } }
protected UmbracoContext GetUmbracoContext(ILogger logger, IUmbracoSettingsSection umbracoSettings, string url, int templateId, RouteData routeData = null, bool setSingleton = false) { var svcCtx = GetServiceContext(); var databaseFactory = TestObjects.GetDatabaseFactoryMock(); //var appCtx = new ApplicationContext( // new DatabaseContext(databaseFactory, logger, Mock.Of<IRuntimeState>(), Mock.Of<IMigrationEntryService>()), // svcCtx, // CacheHelper.CreateDisabledCacheHelper(), // new ProfilingLogger(logger, Mock.Of<IProfiler>())) { /*IsReady = true*/ }; var cache = NoAppCache.Instance; //var provider = new ScopeUnitOfWorkProvider(databaseFactory, new RepositoryFactory(Mock.Of<IServiceContainer>())); var scopeProvider = TestObjects.GetScopeProvider(Mock.Of <ILogger>()); var factory = Mock.Of <IPublishedContentTypeFactory>(); var umbracoContextAccessor = Mock.Of <IUmbracoContextAccessor>(); _service = new PublishedSnapshotService(svcCtx, factory, scopeProvider, cache, null, null, umbracoContextAccessor, null, null, null, new TestDefaultCultureAccessor(), Current.Logger, TestObjects.GetGlobalSettings(), new SiteDomainHelper(), Factory.GetInstance <IEntityXmlSerializer>(), null, true, false ); // no events var http = GetHttpContextFactory(url, routeData).HttpContext; var globalSettings = TestObjects.GetGlobalSettings(); var ctx = new UmbracoContext( http, _service, new WebSecurity(http, Current.Services.UserService, globalSettings), TestObjects.GetUmbracoSettings(), Enumerable.Empty <IUrlProvider>(), Enumerable.Empty <IMediaUrlProvider>(), globalSettings, new TestVariationContextAccessor()); //if (setSingleton) //{ // UmbracoContext.Current = ctx; //} return(ctx); }
protected override IPublishedSnapshotService CreatePublishedSnapshotService(GlobalSettings globalSettings = null) { var options = new PublishedSnapshotServiceOptions { IgnoreLocalDb = true }; var publishedSnapshotAccessor = new UmbracoContextPublishedSnapshotAccessor(Current.UmbracoContextAccessor); var runtimeStateMock = new Mock <IRuntimeState>(); runtimeStateMock.Setup(x => x.Level).Returns(() => RuntimeLevel.Run); var contentTypeFactory = Factory.GetRequiredService <IPublishedContentTypeFactory>(); var documentRepository = Mock.Of <IDocumentRepository>(); var mediaRepository = Mock.Of <IMediaRepository>(); var memberRepository = Mock.Of <IMemberRepository>(); var hostingEnvironment = TestHelper.GetHostingEnvironment(); var typeFinder = TestHelper.GetTypeFinder(); var nuCacheSettings = new NuCacheSettings(); var lifetime = new Mock <IUmbracoApplicationLifetime>(); var repository = new NuCacheContentRepository(ScopeProvider, AppCaches.Disabled, Mock.Of <ILogger <NuCacheContentRepository> >(), memberRepository, documentRepository, mediaRepository, Mock.Of <IShortStringHelper>(), new UrlSegmentProviderCollection(new[] { new DefaultUrlSegmentProvider(ShortStringHelper) })); var snapshotService = new PublishedSnapshotService( options, null, ServiceContext, contentTypeFactory, publishedSnapshotAccessor, Mock.Of <IVariationContextAccessor>(), base.ProfilingLogger, NullLoggerFactory.Instance, ScopeProvider, new NuCacheContentService(repository, ScopeProvider, NullLoggerFactory.Instance, Mock.Of <IEventMessagesFactory>()), DefaultCultureAccessor, Microsoft.Extensions.Options.Options.Create(globalSettings ?? new GlobalSettings()), Factory.GetRequiredService <IEntityXmlSerializer>(), new NoopPublishedModelFactory(), hostingEnvironment, Microsoft.Extensions.Options.Options.Create(nuCacheSettings)); return(snapshotService); }
protected virtual IPublishedSnapshotService CreatePublishedSnapshotService() { var cache = NoAppCache.Instance; ContentTypesCache = new PublishedContentTypeCache( Factory.GetInstance <IContentTypeService>(), Factory.GetInstance <IMediaTypeService>(), Factory.GetInstance <IMemberTypeService>(), Factory.GetInstance <IPublishedContentTypeFactory>(), Logger); // testing=true so XmlStore will not use the file nor the database var publishedSnapshotAccessor = new UmbracoContextPublishedSnapshotAccessor(Umbraco.Web.Composing.Current.UmbracoContextAccessor); var variationContextAccessor = new TestVariationContextAccessor(); var service = new PublishedSnapshotService( ServiceContext, Factory.GetInstance <IPublishedContentTypeFactory>(), ScopeProvider, cache, publishedSnapshotAccessor, variationContextAccessor, Factory.GetInstance <IUmbracoContextAccessor>(), Factory.GetInstance <IDocumentRepository>(), Factory.GetInstance <IMediaRepository>(), Factory.GetInstance <IMemberRepository>(), DefaultCultureAccessor, Logger, Factory.GetInstance <IGlobalSettings>(), new SiteDomainHelper(), Factory.GetInstance <IEntityXmlSerializer>(), ContentTypesCache, null, true, Options.PublishedRepositoryEvents); // initialize PublishedCacheService content with an Xml source service.XmlStore.GetXmlDocument = () => { var doc = new XmlDocument(); doc.LoadXml(GetXmlContent(0)); return(doc); }; return(service); }
public void StandaloneVariations() { // this test implements a full standalone NuCache (based upon a test IDataSource, does not // use any local db files, does not rely on any database) - and tests variations Current.Reset(); Current.UnlockConfigs(); Current.Configs.Add(SettingsForTests.GenerateMockUmbracoSettings); Current.Configs.Add <IGlobalSettings>(() => new GlobalSettings()); var globalSettings = Current.Configs.Global(); // create a content node kit var kit = new ContentNodeKit { ContentTypeId = 2, Node = new ContentNode(1, Guid.NewGuid(), 0, "-1,1", 0, -1, DateTime.Now, 0), DraftData = new ContentData { Name = "It Works2!", Published = false, TemplateId = 0, VersionId = 2, VersionDate = DateTime.Now, WriterId = 0, Properties = new Dictionary <string, PropertyData[]> { { "prop", new[] { new PropertyData { Culture = "", Segment = "", Value = "val2" }, new PropertyData { Culture = "fr-FR", Segment = "", Value = "val-fr2" }, new PropertyData { Culture = "en-UK", Segment = "", Value = "val-uk2" } } } }, CultureInfos = new Dictionary <string, CultureVariation> { { "fr-FR", new CultureVariation { Name = "name-fr2", Date = new DateTime(2018, 01, 03, 01, 00, 00) } }, { "en-UK", new CultureVariation { Name = "name-uk2", Date = new DateTime(2018, 01, 04, 01, 00, 00) } } } }, PublishedData = new ContentData { Name = "It Works1!", Published = true, TemplateId = 0, VersionId = 1, VersionDate = DateTime.Now, WriterId = 0, Properties = new Dictionary <string, PropertyData[]> { { "prop", new[] { new PropertyData { Culture = "", Segment = "", Value = "val1" }, new PropertyData { Culture = "fr-FR", Segment = "", Value = "val-fr1" }, new PropertyData { Culture = "en-UK", Segment = "", Value = "val-uk1" } } } }, CultureInfos = new Dictionary <string, CultureVariation> { { "fr-FR", new CultureVariation { Name = "name-fr1", Date = new DateTime(2018, 01, 01, 01, 00, 00) } }, { "en-UK", new CultureVariation { Name = "name-uk1", Date = new DateTime(2018, 01, 02, 01, 00, 00) } } } } }; // create a data source for NuCache var dataSource = new TestDataSource(kit); var runtime = Mock.Of <IRuntimeState>(); Mock.Get(runtime).Setup(x => x.Level).Returns(RuntimeLevel.Run); // create data types, property types and content types var dataType = new DataType(new VoidEditor("Editor", Mock.Of <ILogger>())) { Id = 3 }; var dataTypes = new[] { dataType }; var propertyType = new PropertyType("Umbraco.Void.Editor", ValueStorageType.Nvarchar) { Alias = "prop", DataTypeId = 3, Variations = ContentVariation.Culture }; var contentType = new ContentType(-1) { Id = 2, Alias = "alias-ct", Variations = ContentVariation.Culture }; contentType.AddPropertyType(propertyType); var contentTypes = new[] { contentType }; var contentTypeService = Mock.Of <IContentTypeService>(); Mock.Get(contentTypeService).Setup(x => x.GetAll()).Returns(contentTypes); Mock.Get(contentTypeService).Setup(x => x.GetAll(It.IsAny <int[]>())).Returns(contentTypes); var dataTypeService = Mock.Of <IDataTypeService>(); Mock.Get(dataTypeService).Setup(x => x.GetAll()).Returns(dataTypes); // create a service context var serviceContext = ServiceContext.CreatePartial( dataTypeService: dataTypeService, memberTypeService: Mock.Of <IMemberTypeService>(), memberService: Mock.Of <IMemberService>(), contentTypeService: contentTypeService, localizationService: Mock.Of <ILocalizationService>() ); // create a scope provider var scopeProvider = Mock.Of <IScopeProvider>(); Mock.Get(scopeProvider) .Setup(x => x.CreateScope( It.IsAny <IsolationLevel>(), It.IsAny <RepositoryCacheMode>(), It.IsAny <IEventDispatcher>(), It.IsAny <bool?>(), It.IsAny <bool>(), It.IsAny <bool>())) .Returns(Mock.Of <IScope>); // create a published content type factory var contentTypeFactory = new PublishedContentTypeFactory( Mock.Of <IPublishedModelFactory>(), new PropertyValueConverterCollection(Array.Empty <IPropertyValueConverter>()), dataTypeService); // create a variation accessor var variationAccessor = new TestVariationContextAccessor(); // at last, create the complete NuCache snapshot service! var options = new PublishedSnapshotService.Options { IgnoreLocalDb = true }; var snapshotService = new PublishedSnapshotService(options, null, runtime, serviceContext, contentTypeFactory, null, new TestPublishedSnapshotAccessor(), variationAccessor, Mock.Of <ILogger>(), scopeProvider, Mock.Of <IDocumentRepository>(), Mock.Of <IMediaRepository>(), Mock.Of <IMemberRepository>(), new TestDefaultCultureAccessor(), dataSource, globalSettings, new SiteDomainHelper()); // get a snapshot, get a published content var snapshot = snapshotService.CreatePublishedSnapshot(previewToken: null); var publishedContent = snapshot.Content.GetById(1); // invariant is the current default variationAccessor.VariationContext = new VariationContext(); Assert.IsNotNull(publishedContent); Assert.AreEqual("It Works1!", publishedContent.Name); Assert.AreEqual("val1", publishedContent.Value <string>("prop")); Assert.AreEqual("val-fr1", publishedContent.Value <string>("prop", "fr-FR")); Assert.AreEqual("val-uk1", publishedContent.Value <string>("prop", "en-UK")); Assert.AreEqual("name-fr1", publishedContent.GetCulture("fr-FR").Name); Assert.AreEqual("name-uk1", publishedContent.GetCulture("en-UK").Name); var draftContent = snapshot.Content.GetById(true, 1); Assert.AreEqual("It Works2!", draftContent.Name); Assert.AreEqual("val2", draftContent.Value <string>("prop")); Assert.AreEqual("val-fr2", draftContent.Value <string>("prop", "fr-FR")); Assert.AreEqual("val-uk2", draftContent.Value <string>("prop", "en-UK")); Assert.AreEqual("name-fr2", draftContent.GetCulture("fr-FR").Name); Assert.AreEqual("name-uk2", draftContent.GetCulture("en-UK").Name); // now french is default variationAccessor.VariationContext = new VariationContext("fr-FR"); Assert.AreEqual("val-fr1", publishedContent.Value <string>("prop")); Assert.AreEqual("name-fr1", publishedContent.GetCulture().Name); Assert.AreEqual("name-fr1", publishedContent.Name); Assert.AreEqual(new DateTime(2018, 01, 01, 01, 00, 00), publishedContent.GetCulture().Date); // now uk is default variationAccessor.VariationContext = new VariationContext("en-UK"); Assert.AreEqual("val-uk1", publishedContent.Value <string>("prop")); Assert.AreEqual("name-uk1", publishedContent.GetCulture().Name); Assert.AreEqual("name-uk1", publishedContent.Name); Assert.AreEqual(new DateTime(2018, 01, 02, 01, 00, 00), publishedContent.GetCulture().Date); // invariant needs to be retrieved explicitely, when it's not default Assert.AreEqual("val1", publishedContent.Value <string>("prop", culture: "")); // but, // if the content type / property type does not vary, then it's all invariant again // modify the content type and property type, notify the snapshot service contentType.Variations = ContentVariation.Nothing; propertyType.Variations = ContentVariation.Nothing; snapshotService.Notify(new[] { new ContentTypeCacheRefresher.JsonPayload("IContentType", publishedContent.ContentType.Id, ContentTypeChangeTypes.RefreshMain) }); // get a new snapshot (nothing changed in the old one), get the published content again var anotherSnapshot = snapshotService.CreatePublishedSnapshot(previewToken: null); var againContent = anotherSnapshot.Content.GetById(1); Assert.AreEqual(ContentVariation.Nothing, againContent.ContentType.Variations); Assert.AreEqual(ContentVariation.Nothing, againContent.ContentType.GetPropertyType("prop").Variations); // now, "no culture" means "invariant" Assert.AreEqual("It Works1!", againContent.Name); Assert.AreEqual("val1", againContent.Value <string>("prop")); }
public PublishedSnapshot(PublishedSnapshotService service, bool defaultPreview) { _service = service; _defaultPreview = defaultPreview; }