From 416c19854d7217b1971e25e9a3fb1d791ec18624 Mon Sep 17 00:00:00 2001 From: Mole Date: Thu, 11 Jan 2024 11:12:47 +0100 Subject: [PATCH] V14: Migrate examine integration tests to V14 (#15536) * Begin migrating test to use new service calls * Fix formatting * Add todo to add timeout so our tests dont stall entirely * Add timeout so tests can run Still need to figure out why the index only seems to update sometimes * Migrate BackOfficeExamineSearcherTests to use new service calls * Remove unused imports * Migrate to languageservice in external index searcher test * Use seperate save and publish calls * Use scopes in ExamineExternalIndexTests * Use scopes in BackOfficeExamineSearcherTests --- .../BackOfficeExamineSearcherTests.cs | 158 +++++++++++++++--- .../UmbracoExamine/ExamineBaseTest.cs | 8 +- .../ExamineExternalIndexSearcherTest.cs | 24 +-- .../ExamineExternalIndexTests.cs | 34 +++- 4 files changed, 180 insertions(+), 44 deletions(-) diff --git a/tests/Umbraco.Tests.Integration/Umbraco.Examine.Lucene/UmbracoExamine/BackOfficeExamineSearcherTests.cs b/tests/Umbraco.Tests.Integration/Umbraco.Examine.Lucene/UmbracoExamine/BackOfficeExamineSearcherTests.cs index dc758ca888..9d77c42bd1 100644 --- a/tests/Umbraco.Tests.Integration/Umbraco.Examine.Lucene/UmbracoExamine/BackOfficeExamineSearcherTests.cs +++ b/tests/Umbraco.Tests.Integration/Umbraco.Examine.Lucene/UmbracoExamine/BackOfficeExamineSearcherTests.cs @@ -24,7 +24,6 @@ using Umbraco.Cms.Web.BackOffice.Security; namespace Umbraco.Cms.Tests.Integration.Umbraco.Examine.Lucene.UmbracoExamine; -[Ignore("Need rework after save and publish have been splitted")] [TestFixture] [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)] public class BackOfficeExamineSearcherTests : ExamineBaseTest @@ -52,7 +51,7 @@ public class BackOfficeExamineSearcherTests : ExamineBaseTest private IContentTypeService ContentTypeService => GetRequiredService(); - private ILocalizationService LocalizationService => GetRequiredService(); + private ILanguageService LanguageService => GetRequiredService(); private ContentService ContentService => (ContentService)GetRequiredService(); @@ -75,8 +74,13 @@ public class BackOfficeExamineSearcherTests : ExamineBaseTest } private IEnumerable BackOfficeExamineSearch(string query, int pageSize = 20, int pageIndex = 0) => - BackOfficeExamineSearcher.Search(query, UmbracoEntityTypes.Document, - pageSize, pageIndex, out _, ignoreUserStartNodes: true); + BackOfficeExamineSearcher.Search( + query, + UmbracoEntityTypes.Document, + pageSize, + pageIndex, + out _, + ignoreUserStartNodes: true); private async Task SetupUserIdentity(string userId) { @@ -90,7 +94,9 @@ public class BackOfficeExamineSearcherTests : ExamineBaseTest var contentType = new ContentTypeBuilder() .WithId(0) .Build(); - await ExecuteAndWaitForIndexing(() => ContentTypeService.Save(contentType), Constants.UmbracoIndexes.InternalIndexName); + await ExecuteAndWaitForIndexing( + () => ContentTypeService.Save(contentType), + Constants.UmbracoIndexes.InternalIndexName); var content = new ContentBuilder() .WithId(0) @@ -98,7 +104,16 @@ public class BackOfficeExamineSearcherTests : ExamineBaseTest .WithContentType(contentType) .Build(); - var createdContent = await ExecuteAndWaitForIndexing(() => ContentService.SaveAndPublish(content), Constants.UmbracoIndexes.InternalIndexName); + var createdContent = await ExecuteAndWaitForIndexing( + () => + { + using var scope = ScopeProvider.CreateScope(); + ContentService.Save(content); + var result = ContentService.Publish(content, Array.Empty()); + scope.Complete(); + return result; + }, + Constants.UmbracoIndexes.InternalIndexName); return createdContent; } @@ -111,13 +126,15 @@ public class BackOfficeExamineSearcherTests : ExamineBaseTest var langDa = new LanguageBuilder() .WithCultureInfo(dkIso) .Build(); - LocalizationService.Save(langDa); + await LanguageService.CreateAsync(langDa, Constants.Security.SuperUserKey); var contentType = new ContentTypeBuilder() .WithId(0) .WithContentVariation(ContentVariation.Culture) .Build(); - await ExecuteAndWaitForIndexing(() => ContentTypeService.Save(contentType), Constants.UmbracoIndexes.InternalIndexName); + await ExecuteAndWaitForIndexing( + () => ContentTypeService.Save(contentType), + Constants.UmbracoIndexes.InternalIndexName); var content = new ContentBuilder() .WithId(0) @@ -125,7 +142,16 @@ public class BackOfficeExamineSearcherTests : ExamineBaseTest .WithCultureName(dkIso, danishNodeName) .WithContentType(contentType) .Build(); - var createdContent = await ExecuteAndWaitForIndexing(() => ContentService.SaveAndPublish(content), Constants.UmbracoIndexes.InternalIndexName); + var createdContent = await ExecuteAndWaitForIndexing( + () => + { + using var scope = ScopeProvider.CreateScope(); + ContentService.Save(content); + var result = ContentService.Publish(content, Array.Empty()); + scope.Complete(); + return result; + }, + Constants.UmbracoIndexes.InternalIndexName); return createdContent; } @@ -227,14 +253,32 @@ public class BackOfficeExamineSearcherTests : ExamineBaseTest .WithName(contentName) .WithContentType(contentType) .Build(); - await ExecuteAndWaitForIndexing(() => ContentService.SaveAndPublish(firstContent), Constants.UmbracoIndexes.InternalIndexName); + await ExecuteAndWaitForIndexing( + () => + { + using var scope = ScopeProvider.CreateCoreScope(); + ContentService.Save(firstContent); + var result = ContentService.Publish(firstContent, Array.Empty()); + scope.Complete(); + return result; + }, + Constants.UmbracoIndexes.InternalIndexName); var secondContent = new ContentBuilder() .WithId(0) .WithName(secondContentName) .WithContentType(contentType) .Build(); - await ExecuteAndWaitForIndexing(() => ContentService.SaveAndPublish(secondContent), Constants.UmbracoIndexes.InternalIndexName); + await ExecuteAndWaitForIndexing( + () => + { + using var scope = ScopeProvider.CreateCoreScope(); + ContentService.Save(secondContent); + var content = ContentService.Publish(secondContent, Array.Empty()); + scope.Complete(); + return content; + }, + Constants.UmbracoIndexes.InternalIndexName); string query = contentName; @@ -268,14 +312,32 @@ public class BackOfficeExamineSearcherTests : ExamineBaseTest .WithName(contentName) .WithContentType(contentType) .Build(); - await ExecuteAndWaitForIndexing(() => ContentService.SaveAndPublish(content), Constants.UmbracoIndexes.InternalIndexName); + await ExecuteAndWaitForIndexing( + () => + { + using var scope = ScopeProvider.CreateCoreScope(); + ContentService.Save(content); + var result = ContentService.Publish(content, Array.Empty()); + scope.Complete(); + return result; + }, + Constants.UmbracoIndexes.InternalIndexName); var childContent = new ContentBuilder() .WithName(childContentName) .WithContentType(contentType) .WithParentId(content.Id) .Build(); - await ExecuteAndWaitForIndexing(() => ContentService.SaveAndPublish(childContent), Constants.UmbracoIndexes.InternalIndexName); + await ExecuteAndWaitForIndexing( + () => + { + using var scope = ScopeProvider.CreateCoreScope(); + ContentService.Save(childContent); + var result = ContentService.Publish(childContent, Array.Empty()); + scope.Complete(); + return result; + }, + Constants.UmbracoIndexes.InternalIndexName); string parentQuery = content.Id.ToString(); @@ -313,21 +375,48 @@ public class BackOfficeExamineSearcherTests : ExamineBaseTest .WithName(contentName) .WithContentType(contentType) .Build(); - await ExecuteAndWaitForIndexing(() => ContentService.SaveAndPublish(content), Constants.UmbracoIndexes.InternalIndexName); + await ExecuteAndWaitForIndexing( + () => + { + using var scope = ScopeProvider.CreateCoreScope(); + ContentService.Save(content); + var result = ContentService.Publish(content, Array.Empty()); + scope.Complete(); + return result; + }, + Constants.UmbracoIndexes.InternalIndexName); var childContent = new ContentBuilder() .WithName(childContentName) .WithContentType(contentType) .WithParentId(content.Id) .Build(); - await ExecuteAndWaitForIndexing(() => ContentService.SaveAndPublish(childContent), Constants.UmbracoIndexes.InternalIndexName); + await ExecuteAndWaitForIndexing( + () => + { + using var scope = ScopeProvider.CreateCoreScope(); + ContentService.Save(childContent); + var result = ContentService.Publish(childContent, Array.Empty()); + scope.Complete(); + return result; + }, + Constants.UmbracoIndexes.InternalIndexName); var childChildContent = new ContentBuilder() .WithName(childChildContentName) .WithContentType(contentType) .WithParentId(childContent.Id) .Build(); - await ExecuteAndWaitForIndexing(() => ContentService.SaveAndPublish(childChildContent), Constants.UmbracoIndexes.InternalIndexName); + await ExecuteAndWaitForIndexing( + () => + { + using var scope = ScopeProvider.CreateCoreScope(); + ContentService.Save(childChildContent); + var result = ContentService.Publish(childChildContent, Array.Empty()); + scope.Complete(); + return result; + }, + Constants.UmbracoIndexes.InternalIndexName); string parentQuery = content.Id.ToString(); string childQuery = childContent.Id.ToString(); @@ -383,7 +472,7 @@ public class BackOfficeExamineSearcherTests : ExamineBaseTest var langDa = new LanguageBuilder() .WithCultureInfo(dkIso) .Build(); - LocalizationService.Save(langDa); + await LanguageService.CreateAsync(langDa, Constants.Security.SuperUserKey); var contentType = new ContentTypeBuilder() .WithId(0) @@ -397,7 +486,16 @@ public class BackOfficeExamineSearcherTests : ExamineBaseTest .WithCultureName(dkIso, danishNodeName) .WithContentType(contentType) .Build(); - PublishResult createdContent = await ExecuteAndWaitForIndexing(() => ContentService.SaveAndPublish(content), Constants.UmbracoIndexes.InternalIndexName); + PublishResult createdContent = await ExecuteAndWaitForIndexing( + () => + { + using var scope = ScopeProvider.CreateCoreScope(); + ContentService.Save(content); + var result = ContentService.Publish(content, Array.Empty()); + scope.Complete(); + return result; + }, + Constants.UmbracoIndexes.InternalIndexName); string query = createdContent.Content.Id.ToString(); @@ -507,7 +605,16 @@ public class BackOfficeExamineSearcherTests : ExamineBaseTest .WithName(contentName) .WithContentType(contentType) .Build(); - await ExecuteAndWaitForIndexing(() => ContentService.SaveAndPublish(contentNode), Constants.UmbracoIndexes.InternalIndexName); + await ExecuteAndWaitForIndexing( + () => + { + using var scope = ScopeProvider.CreateCoreScope(); + ContentService.Save(contentNode); + var result = ContentService.Publish(contentNode, Array.Empty()); + scope.Complete(); + return result; + }, + Constants.UmbracoIndexes.InternalIndexName); string query = contentName; @@ -563,7 +670,7 @@ public class BackOfficeExamineSearcherTests : ExamineBaseTest .WithAlias("testBox") .Done() .Build(); - ContentTypeService.Save(contentType); + await ExecuteAndWaitForIndexing(() => ContentTypeService.Save(contentType), Constants.UmbracoIndexes.InternalIndexName); var contentNode = new ContentBuilder() .WithId(0) @@ -571,7 +678,16 @@ public class BackOfficeExamineSearcherTests : ExamineBaseTest .WithContentType(contentType) .WithPropertyValues(new { testBox = "TestValue" }) .Build(); - await ExecuteAndWaitForIndexing(() => ContentService.SaveAndPublish(contentNode), Constants.UmbracoIndexes.InternalIndexName); + await ExecuteAndWaitForIndexing( + () => + { + using var scope = ScopeProvider.CreateCoreScope(); + ContentService.Save(contentNode); + var result = ContentService.Publish(contentNode, Array.Empty()); + scope.Complete(); + return result; + }, + Constants.UmbracoIndexes.InternalIndexName); string query = contentName; diff --git a/tests/Umbraco.Tests.Integration/Umbraco.Examine.Lucene/UmbracoExamine/ExamineBaseTest.cs b/tests/Umbraco.Tests.Integration/Umbraco.Examine.Lucene/UmbracoExamine/ExamineBaseTest.cs index 3d97f4ede0..fa14bcf18f 100644 --- a/tests/Umbraco.Tests.Integration/Umbraco.Examine.Lucene/UmbracoExamine/ExamineBaseTest.cs +++ b/tests/Umbraco.Tests.Integration/Umbraco.Examine.Lucene/UmbracoExamine/ExamineBaseTest.cs @@ -20,6 +20,8 @@ namespace Umbraco.Cms.Tests.Integration.Umbraco.Examine.Lucene.UmbracoExamine; [TestFixture] public abstract class ExamineBaseTest : UmbracoIntegrationTest { + private const int IndexingTimoutInMilliseconds = 3000; + protected IndexInitializer IndexInitializer => Services.GetRequiredService(); protected IHostingEnvironment HostingEnvironment => Services.GetRequiredService(); @@ -124,7 +126,8 @@ public abstract class ExamineBaseTest : UmbracoIntegrationTest { indexUpdatingAction(); return null; - }, indexName); + }, + indexName); /// /// Performs and action and waits for the specified index to be done indexing. @@ -148,8 +151,7 @@ public abstract class ExamineBaseTest : UmbracoIntegrationTest // Perform the action, and wait for the handle to be freed, meaning the index is done populating. var result = indexUpdatingAction(); - await indexingHandle.WaitOneAsync(); - + await indexingHandle.WaitOneAsync(millisecondsTimeout: IndexingTimoutInMilliseconds); return result; } diff --git a/tests/Umbraco.Tests.Integration/Umbraco.Examine.Lucene/UmbracoExamine/ExamineExternalIndexSearcherTest.cs b/tests/Umbraco.Tests.Integration/Umbraco.Examine.Lucene/UmbracoExamine/ExamineExternalIndexSearcherTest.cs index 38eb270b3c..a6e2891447 100644 --- a/tests/Umbraco.Tests.Integration/Umbraco.Examine.Lucene/UmbracoExamine/ExamineExternalIndexSearcherTest.cs +++ b/tests/Umbraco.Tests.Integration/Umbraco.Examine.Lucene/UmbracoExamine/ExamineExternalIndexSearcherTest.cs @@ -24,32 +24,26 @@ namespace Umbraco.Cms.Tests.Integration.Umbraco.Examine.Lucene.UmbracoExamine; public class ExamineExternalIndexSearcherTest : IExamineExternalIndexSearcherTest { private readonly AppCaches _appCaches; + private readonly ILanguageService _languageService; private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor; private readonly IEntityService _entityService; private readonly IExamineManager _examineManager; - private readonly ILocalizationService _languageService; - private readonly IPublishedUrlProvider _publishedUrlProvider; private readonly IUmbracoTreeSearcherFields _treeSearcherFields; - private readonly IUmbracoMapper _umbracoMapper; public ExamineExternalIndexSearcherTest( IExamineManager examineManager, - ILocalizationService languageService, IBackOfficeSecurityAccessor backOfficeSecurityAccessor, IEntityService entityService, IUmbracoTreeSearcherFields treeSearcherFields, AppCaches appCaches, - IUmbracoMapper umbracoMapper, - IPublishedUrlProvider publishedUrlProvider) + ILanguageService languageService) { _examineManager = examineManager; - _languageService = languageService; _backOfficeSecurityAccessor = backOfficeSecurityAccessor; _entityService = entityService; _treeSearcherFields = treeSearcherFields; _appCaches = appCaches; - _umbracoMapper = umbracoMapper; - _publishedUrlProvider = publishedUrlProvider; + _languageService = languageService; } public IEnumerable Search( @@ -141,7 +135,7 @@ public class ExamineExternalIndexSearcherTest : IExamineExternalIndexSearcherTes throw new InvalidOperationException("No index found by name " + indexName); } - if (!BuildQuery(sb, query, searchFrom, fields, type)) + if (!BuildQueryAsync(sb, query, searchFrom, fields, type).GetAwaiter().GetResult()) { totalFound = 0; return Enumerable.Empty(); @@ -159,14 +153,14 @@ public class ExamineExternalIndexSearcherTest : IExamineExternalIndexSearcherTes return result; } - private bool BuildQuery(StringBuilder sb, string query, string? searchFrom, List fields, string type) + private async Task BuildQueryAsync(StringBuilder sb, string query, string? searchFrom, List fields, string type) { //build a lucene query: // the nodeName will be boosted 10x without wildcards // then nodeName will be matched normally with wildcards // the rest will be normal without wildcards - var allLangs = _languageService.GetAllLanguages().Select(x => x.IsoCode.ToLowerInvariant()).ToList(); + var allLanguages = (await _languageService.GetAllAsync()).Select(x => x.IsoCode.ToLowerInvariant()).ToList(); // the chars [*-_] in the query will mess everything up so let's remove those // However we cannot just remove - and _ since these signify a space, so we instead replace them with that. @@ -199,7 +193,7 @@ public class ExamineExternalIndexSearcherTest : IExamineExternalIndexSearcherTes sb.Append("+("); - AppendNodeNamePhraseWithBoost(sb, query, allLangs); + AppendNodeNamePhraseWithBoost(sb, query, allLanguages); foreach (var f in fields) { @@ -232,9 +226,9 @@ public class ExamineExternalIndexSearcherTest : IExamineExternalIndexSearcherTes sb.Append("+("); - AppendNodeNameExactWithBoost(sb, query, allLangs); + AppendNodeNameExactWithBoost(sb, query, allLanguages); - AppendNodeNameWithWildcards(sb, querywords, allLangs); + AppendNodeNameWithWildcards(sb, querywords, allLanguages); foreach (var f in fields) { diff --git a/tests/Umbraco.Tests.Integration/Umbraco.Examine.Lucene/UmbracoExamine/ExamineExternalIndexTests.cs b/tests/Umbraco.Tests.Integration/Umbraco.Examine.Lucene/UmbracoExamine/ExamineExternalIndexTests.cs index 602c53cf26..10f324183d 100644 --- a/tests/Umbraco.Tests.Integration/Umbraco.Examine.Lucene/UmbracoExamine/ExamineExternalIndexTests.cs +++ b/tests/Umbraco.Tests.Integration/Umbraco.Examine.Lucene/UmbracoExamine/ExamineExternalIndexTests.cs @@ -8,6 +8,7 @@ using Umbraco.Cms.Core; using Umbraco.Cms.Core.Cache; using Umbraco.Cms.Core.Models.ContentEditing; using Umbraco.Cms.Core.Notifications; +using Umbraco.Cms.Core.Scoping; using Umbraco.Cms.Core.Security; using Umbraco.Cms.Core.Services; using Umbraco.Cms.Core.Sync; @@ -22,7 +23,6 @@ using Umbraco.Cms.Web.BackOffice.Security; namespace Umbraco.Cms.Tests.Integration.Umbraco.Examine.Lucene.UmbracoExamine; -[Ignore("Need rework after save and publish have been splitted")] [TestFixture] [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)] public class ExamineExternalIndexTests : ExamineBaseTest @@ -60,6 +60,8 @@ public class ExamineExternalIndexTests : ExamineBaseTest private IBackOfficeSignInManager BackOfficeSignInManager => GetRequiredService(); + private ICoreScopeProvider CoreScopeProvider => GetRequiredService(); + protected override void CustomTestSetup(IUmbracoBuilder builder) { builder.Services.AddUnique(); @@ -74,8 +76,13 @@ public class ExamineExternalIndexTests : ExamineBaseTest } private IEnumerable ExamineExternalIndexSearch(string query, int pageSize = 20, int pageIndex = 0) => - ExamineExternalIndexSearcher.Search(query, UmbracoEntityTypes.Document, - pageSize, pageIndex, out _, ignoreUserStartNodes: true); + ExamineExternalIndexSearcher.Search( + query, + UmbracoEntityTypes.Document, + pageSize, + pageIndex, + out _, + ignoreUserStartNodes: true); private async Task SetupUserIdentity(string userId) { @@ -100,7 +107,16 @@ public class ExamineExternalIndexTests : ExamineBaseTest .WithName(ContentName) .WithContentType(contentType) .Build(); - await ExecuteAndWaitForIndexing(() => ContentService.SaveAndPublish(content), Constants.UmbracoIndexes.ExternalIndexName); + await ExecuteAndWaitForIndexing( + () => + { + using ICoreScope scope = CoreScopeProvider.CreateCoreScope(); + ContentService.Save(content); + var published = ContentService.Publish(content, Array.Empty()); + scope.Complete(); + return published; + }, + Constants.UmbracoIndexes.ExternalIndexName); // Act IEnumerable actual = ExamineExternalIndexSearch(ContentName); @@ -127,7 +143,15 @@ public class ExamineExternalIndexTests : ExamineBaseTest .WithName(ContentName) .WithContentType(contentType) .Build(); - await ExecuteAndWaitForIndexing(() => ContentService.Save(content), Constants.UmbracoIndexes.ExternalIndexName); + await ExecuteAndWaitForIndexing( + () => + { + using ICoreScope scope = CoreScopeProvider.CreateCoreScope(); + var result = ContentService.Save(content); + scope.Complete(); + return result; + }, + Constants.UmbracoIndexes.ExternalIndexName); // Act IEnumerable actual = ExamineExternalIndexSearch(ContentName);