Files
Umbraco-CMS/src/Umbraco.Tests/UmbracoExamine/IndexInitializer.cs

212 lines
9.3 KiB
C#
Raw Normal View History

2018-06-29 19:52:40 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using Examine;
using Examine.LuceneEngine.Providers;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Store;
2020-09-15 15:14:44 +02:00
using Microsoft.Extensions.Logging;
2018-06-29 19:52:40 +02:00
using Moq;
using Umbraco.Cms.Core.Hosting;
using Umbraco.Cms.Core.Logging;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.Membership;
using Umbraco.Cms.Core.Persistence.Querying;
using Umbraco.Cms.Core.PropertyEditors;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Strings;
2018-06-29 19:52:40 +02:00
using Umbraco.Core.Scoping;
using Umbraco.Examine;
using Umbraco.Tests.TestHelpers;
using IContentService = Umbraco.Cms.Core.Services.IContentService;
using IMediaService = Umbraco.Cms.Core.Services.IMediaService;
2018-06-29 19:52:40 +02:00
using Version = Lucene.Net.Util.Version;
namespace Umbraco.Tests.UmbracoExamine
{
/// <summary>
/// Used internally by test classes to initialize a new index from the template
/// </summary>
internal static class IndexInitializer
{
public static ContentValueSetBuilder GetContentValueSetBuilder(PropertyEditorCollection propertyEditors, IScopeProvider scopeProvider, bool publishedValuesOnly)
{
var contentValueSetBuilder = new ContentValueSetBuilder(
propertyEditors,
new UrlSegmentProviderCollection(new[] { new DefaultUrlSegmentProvider(TestHelper.ShortStringHelper) }),
GetMockUserService(),
TestHelper.ShortStringHelper,
scopeProvider,
publishedValuesOnly);
return contentValueSetBuilder;
}
public static ContentIndexPopulator GetContentIndexRebuilder(PropertyEditorCollection propertyEditors, IContentService contentService, IScopeProvider scopeProvider, bool publishedValuesOnly)
{
var contentValueSetBuilder = GetContentValueSetBuilder(propertyEditors, scopeProvider, publishedValuesOnly);
var contentIndexDataSource = new ContentIndexPopulator(publishedValuesOnly, null, contentService, scopeProvider.SqlContext, contentValueSetBuilder);
return contentIndexDataSource;
}
public static MediaIndexPopulator GetMediaIndexRebuilder(PropertyEditorCollection propertyEditors, IMediaService mediaService)
{
2020-09-28 08:26:21 +02:00
var mediaValueSetBuilder = new MediaValueSetBuilder(propertyEditors, new UrlSegmentProviderCollection(new[] { new DefaultUrlSegmentProvider(TestHelper.ShortStringHelper) }), GetMockUserService(), Mock.Of<ILogger<MediaValueSetBuilder>>(), TestHelper.ShortStringHelper, TestHelper.JsonSerializer);
var mediaIndexDataSource = new MediaIndexPopulator(null, mediaService, mediaValueSetBuilder);
return mediaIndexDataSource;
}
public static IContentService GetMockContentService()
{
long longTotalRecs;
var demoData = new ExamineDemoDataContentService();
var allRecs = demoData.GetLatestContentByXPath("//*[@isDoc]")
.Root
.Elements()
.Select(x => Mock.Of<IContent>(
m =>
m.Id == (int)x.Attribute("id") &&
m.ParentId == (int)x.Attribute("parentID") &&
m.Level == (int)x.Attribute("level") &&
m.CreatorId == 0 &&
m.SortOrder == (int)x.Attribute("sortOrder") &&
m.CreateDate == (DateTime)x.Attribute("createDate") &&
m.UpdateDate == (DateTime)x.Attribute("updateDate") &&
2020-01-28 17:07:06 +11:00
m.Name == (string)x.Attribute(UmbracoExamineFieldNames.NodeNameFieldName) &&
m.GetCultureName(It.IsAny<string>()) == (string)x.Attribute(UmbracoExamineFieldNames.NodeNameFieldName) &&
m.Path == (string)x.Attribute("path") &&
m.Properties == new PropertyCollection() &&
m.ContentType == Mock.Of<ISimpleContentType>(mt =>
mt.Icon == "test" &&
mt.Alias == x.Name.LocalName &&
mt.Id == (int)x.Attribute("nodeType"))))
.ToArray();
return Mock.Of<IContentService>(
x => x.GetPagedDescendants(
It.IsAny<int>(), It.IsAny<long>(), It.IsAny<int>(), out longTotalRecs, It.IsAny<IQuery<IContent>>(), It.IsAny<Ordering>())
== allRecs);
}
public static IUserService GetMockUserService()
{
return Mock.Of<IUserService>(x => x.GetProfileById(It.IsAny<int>()) == Mock.Of<IProfile>(p => p.Id == 0 && p.Name == "admin"));
}
public static IMediaService GetMockMediaService()
{
long totalRecs;
var demoData = new ExamineDemoDataMediaService();
var allRecs = demoData.GetLatestMediaByXpath("//node")
.Root
.Elements()
.Select(x => Mock.Of<IMedia>(
m =>
m.Id == (int)x.Attribute("id") &&
m.ParentId == (int)x.Attribute("parentID") &&
m.Level == (int)x.Attribute("level") &&
m.CreatorId == 0 &&
m.SortOrder == (int)x.Attribute("sortOrder") &&
m.CreateDate == (DateTime)x.Attribute("createDate") &&
m.UpdateDate == (DateTime)x.Attribute("updateDate") &&
2020-01-28 17:07:06 +11:00
m.Name == (string)x.Attribute(UmbracoExamineFieldNames.NodeNameFieldName) &&
m.GetCultureName(It.IsAny<string>()) == (string)x.Attribute(UmbracoExamineFieldNames.NodeNameFieldName) &&
m.Path == (string)x.Attribute("path") &&
m.Properties == new PropertyCollection() &&
2019-02-05 19:58:33 +01:00
m.ContentType == Mock.Of<ISimpleContentType>(mt =>
mt.Alias == (string)x.Attribute("nodeTypeAlias") &&
mt.Id == (int)x.Attribute("nodeType"))))
.ToArray();
// MOCK!
var mediaServiceMock = new Mock<IMediaService>();
mediaServiceMock
.Setup(x => x.GetPagedDescendants(
It.IsAny<int>(), It.IsAny<long>(), It.IsAny<int>(), out totalRecs, It.IsAny<IQuery<IMedia>>(), It.IsAny<Ordering>())
).Returns(() => allRecs);
//mediaServiceMock.Setup(service => service.GetPagedXmlEntries(It.IsAny<string>(), It.IsAny<long>(), It.IsAny<int>(), out longTotalRecs))
// .Returns(() => allRecs.Select(x => x.ToXml()));
return mediaServiceMock.Object;
}
public static ILocalizationService GetMockLocalizationService()
{
return Mock.Of<ILocalizationService>(x => x.GetAllLanguages() == Array.Empty<ILanguage>());
}
public static IMediaTypeService GetMockMediaTypeService()
{
var mediaTypeServiceMock = new Mock<IMediaTypeService>();
mediaTypeServiceMock.Setup(x => x.GetAll())
.Returns(new List<IMediaType>
{
new MediaType(TestHelper.ShortStringHelper, -1) {Alias = "Folder", Name = "Folder", Id = 1031, Icon = "icon-folder"},
new MediaType(TestHelper.ShortStringHelper, -1) {Alias = "Image", Name = "Image", Id = 1032, Icon = "icon-picture"}
});
return mediaTypeServiceMock.Object;
}
2020-09-15 12:40:35 +02:00
public static IProfilingLogger GetMockProfilingLogger()
{
2020-11-20 12:24:16 +00:00
return new ProfilingLogger(Mock.Of<ILogger<ProfilingLogger>>(), Mock.Of<IProfiler>());
}
public static UmbracoContentIndex GetUmbracoIndexer(
2018-11-27 13:46:43 +01:00
IProfilingLogger profilingLogger,
IHostingEnvironment hostingEnvironment,
IRuntimeState runtimeState,
2018-06-29 19:52:40 +02:00
Directory luceneDir,
Analyzer analyzer = null,
ILocalizationService languageService = null,
IContentValueSetValidator validator = null)
2018-06-29 19:52:40 +02:00
{
if (languageService == null)
languageService = GetMockLocalizationService();
2018-06-29 19:52:40 +02:00
if (analyzer == null)
analyzer = new StandardAnalyzer(Version.LUCENE_30);
if (validator == null)
2018-12-05 17:20:48 +11:00
validator = new ContentValueSetValidator(true);
var i = new UmbracoContentIndex(
2018-06-29 19:52:40 +02:00
"testIndexer",
luceneDir,
2018-12-18 17:55:30 +11:00
new UmbracoFieldDefinitionCollection(),
2018-06-29 19:52:40 +02:00
analyzer,
profilingLogger,
Mock.Of<ILogger<UmbracoContentIndex>>(),
2020-09-15 15:14:44 +02:00
Mock.Of<ILoggerFactory>(),
hostingEnvironment,
runtimeState,
languageService,
validator);
2018-06-29 19:52:40 +02:00
i.IndexingError += IndexingError;
return i;
}
//public static MultiIndexSearcher GetMultiSearcher(Directory pdfDir, Directory simpleDir, Directory conventionDir, Directory cwsDir)
//{
// var i = new MultiIndexSearcher("testSearcher", new[] { pdfDir, simpleDir, conventionDir, cwsDir }, new StandardAnalyzer(Version.LUCENE_29));
// return i;
//}
2018-06-29 19:52:40 +02:00
internal static void IndexingError(object sender, IndexingErrorEventArgs e)
{
throw new ApplicationException(e.Message, e.InnerException);
}
}
}