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
This commit is contained in:
Mole
2024-01-11 11:12:47 +01:00
committed by GitHub
parent 8516a7b2e5
commit 416c19854d
4 changed files with 180 additions and 44 deletions

View File

@@ -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<IContentTypeService>();
private ILocalizationService LocalizationService => GetRequiredService<ILocalizationService>();
private ILanguageService LanguageService => GetRequiredService<ILanguageService>();
private ContentService ContentService => (ContentService)GetRequiredService<IContentService>();
@@ -75,8 +74,13 @@ public class BackOfficeExamineSearcherTests : ExamineBaseTest
}
private IEnumerable<ISearchResult> 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<string>());
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<string>());
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<string>());
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<string>());
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<string>());
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<string>());
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<string>());
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<string>());
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<string>());
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<string>());
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<string>());
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<string>());
scope.Complete();
return result;
},
Constants.UmbracoIndexes.InternalIndexName);
string query = contentName;

View File

@@ -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<IndexInitializer>();
protected IHostingEnvironment HostingEnvironment => Services.GetRequiredService<IHostingEnvironment>();
@@ -124,7 +126,8 @@ public abstract class ExamineBaseTest : UmbracoIntegrationTest
{
indexUpdatingAction();
return null;
}, indexName);
},
indexName);
/// <summary>
/// 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;
}

View File

@@ -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<ISearchResult> 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<ISearchResult>();
@@ -159,14 +153,14 @@ public class ExamineExternalIndexSearcherTest : IExamineExternalIndexSearcherTes
return result;
}
private bool BuildQuery(StringBuilder sb, string query, string? searchFrom, List<string> fields, string type)
private async Task<bool> BuildQueryAsync(StringBuilder sb, string query, string? searchFrom, List<string> 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)
{

View File

@@ -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<IBackOfficeSignInManager>();
private ICoreScopeProvider CoreScopeProvider => GetRequiredService<ICoreScopeProvider>();
protected override void CustomTestSetup(IUmbracoBuilder builder)
{
builder.Services.AddUnique<IExamineExternalIndexSearcherTest, ExamineExternalIndexSearcherTest>();
@@ -74,8 +76,13 @@ public class ExamineExternalIndexTests : ExamineBaseTest
}
private IEnumerable<ISearchResult> 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<string>());
scope.Complete();
return published;
},
Constants.UmbracoIndexes.ExternalIndexName);
// Act
IEnumerable<ISearchResult> 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<ISearchResult> actual = ExamineExternalIndexSearch(ContentName);