Files
Umbraco-CMS/tests/Umbraco.Tests.Integration/Umbraco.Examine.Lucene/UmbracoExamine/ExamineExternalIndexTests.cs
Bjarke Berg 935c3b8e42 Initialize important services before unattended installs (#17366)
* Added new notification to hook in after the premigrations and use this to init different services.

* Force MaxDegreeOfParallelism to 1, while investigating scopes

* Tried some more workarounds

* Updated scopes and changed parallel to non parallel to ensure migration works

* Missing scope

* Make it parallel again - The secret is, the SuppressFlow needs to be when you create the task, but not on the await!.

* Fixed issue when DEBUG_SCOPES is not added to tests.

* Remove test exception

* Try build on ubuntu again, even that we know it can be stuck. Just a test to see if all tests pass

* Updated comment

---------

Co-authored-by: kjac <kja@umbraco.dk>
2024-10-28 12:10:38 +01:00

167 lines
6.2 KiB
C#

using Examine;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using NUnit.Framework;
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;
using Umbraco.Cms.Infrastructure.Examine.DependencyInjection;
using Umbraco.Cms.Infrastructure.HostedServices;
using Umbraco.Cms.Infrastructure.Search;
using Umbraco.Cms.Tests.Common.Builders;
using Umbraco.Cms.Tests.Common.Builders.Extensions;
using Umbraco.Cms.Tests.Common.Testing;
using Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Services;
using Umbraco.Cms.Web.Common.Security;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Examine.Lucene.UmbracoExamine;
[TestFixture]
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
public class ExamineExternalIndexTests : ExamineBaseTest
{
private const string ContentName = "TestContent";
[SetUp]
public void Setup()
{
TestHelper.DeleteDirectory(GetIndexPath(Constants.UmbracoIndexes.InternalIndexName));
TestHelper.DeleteDirectory(GetIndexPath(Constants.UmbracoIndexes.ExternalIndexName));
var httpContext = new DefaultHttpContext();
httpContext.RequestServices = Services;
Mock.Get(TestHelper.GetHttpContextAccessor()).Setup(x => x.HttpContext).Returns(httpContext);
DocumentUrlService.InitAsync(false, CancellationToken.None).GetAwaiter().GetResult();
}
[TearDown]
public void TearDown()
{
// When disposing examine, it does a final write, which ends up locking the file if the indexing is not done yet. So we have this wait to circumvent that.
Thread.Sleep(1500);
// Sometimes we do not dispose all services in time and the test fails because the log file is locked. Resulting in all other tests failing as well
Services.DisposeIfDisposable();
}
private IExamineExternalIndexSearcherTest ExamineExternalIndexSearcher =>
GetRequiredService<IExamineExternalIndexSearcherTest>();
private IDocumentUrlService DocumentUrlService => GetRequiredService<IDocumentUrlService>();
private IContentTypeService ContentTypeService => GetRequiredService<IContentTypeService>();
private ContentService ContentService => (ContentService)GetRequiredService<IContentService>();
private IUserStore<BackOfficeIdentityUser> BackOfficeUserStore =>
GetRequiredService<IUserStore<BackOfficeIdentityUser>>();
private IBackOfficeSignInManager BackOfficeSignInManager => GetRequiredService<IBackOfficeSignInManager>();
private ICoreScopeProvider CoreScopeProvider => GetRequiredService<ICoreScopeProvider>();
private IHttpContextAccessor HttpContextAccessor => GetRequiredService<IHttpContextAccessor>();
protected override void CustomTestSetup(IUmbracoBuilder builder)
{
base.CustomTestSetup(builder);
builder.Services.AddUnique<IExamineExternalIndexSearcherTest, ExamineExternalIndexSearcherTest>();
builder.AddNotificationHandler<ContentCacheRefresherNotification, ContentIndexingNotificationHandler>();
builder.AddExamineIndexes();
builder.Services.AddHostedService<QueuedHostedService>();
}
private IEnumerable<ISearchResult> ExamineExternalIndexSearch(string query, int pageSize = 20, int pageIndex = 0) =>
ExamineExternalIndexSearcher.Search(
query,
UmbracoEntityTypes.Document,
pageSize,
pageIndex,
out _,
ignoreUserStartNodes: true);
private async Task SetupUserIdentity(string userId)
{
var identity =
await BackOfficeUserStore.FindByIdAsync(userId, CancellationToken.None);
await BackOfficeSignInManager.SignInAsync(identity, false);
var principal = await BackOfficeSignInManager.CreateUserPrincipalAsync(identity);
HttpContextAccessor.HttpContext.SetPrincipalForRequest(principal);
}
[Test]
public async Task Search_Published_Content_With_Query_By_Content_Name()
{
// Arrange
await SetupUserIdentity(Constants.Security.SuperUserIdAsString);
var contentType = new ContentTypeBuilder()
.WithId(0)
.Build();
ContentTypeService.Save(contentType);
var content = new ContentBuilder()
.WithId(0)
.WithName(ContentName)
.WithContentType(contentType)
.Build();
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);
// Assert
IEnumerable<ISearchResult> searchResults = actual.ToArray();
Assert.AreEqual(1, searchResults.Count());
Assert.AreEqual(searchResults.First().Values["nodeName"], ContentName);
}
[Test]
public async Task Search_Unpublished_Content_With_Query_By_Content_Name()
{
// Arrange
await SetupUserIdentity(Constants.Security.SuperUserIdAsString);
var contentType = new ContentTypeBuilder()
.WithId(0)
.Build();
ContentTypeService.Save(contentType);
var content = new ContentBuilder()
.WithId(0)
.WithName(ContentName)
.WithContentType(contentType)
.Build();
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);
// Assert
Assert.AreEqual(0, actual.Count());
}
}