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;
|
2021-02-09 10:22:42 +01:00
|
|
|
|
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;
|
2019-12-09 14:12:06 +01:00
|
|
|
|
using Umbraco.Tests.TestHelpers;
|
2021-02-09 10:22:42 +01:00
|
|
|
|
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
|
|
|
|
|
|
{
|
2020-04-22 10:37:23 +10:00
|
|
|
|
public static ContentValueSetBuilder GetContentValueSetBuilder(PropertyEditorCollection propertyEditors, IScopeProvider scopeProvider, bool publishedValuesOnly)
|
2018-11-28 01:23:02 +11:00
|
|
|
|
{
|
2020-04-22 10:37:23 +10:00
|
|
|
|
var contentValueSetBuilder = new ContentValueSetBuilder(
|
|
|
|
|
|
propertyEditors,
|
2020-04-24 08:33:17 +02:00
|
|
|
|
new UrlSegmentProviderCollection(new[] { new DefaultUrlSegmentProvider(TestHelper.ShortStringHelper) }),
|
2020-04-22 10:37:23 +10:00
|
|
|
|
GetMockUserService(),
|
2020-04-24 08:33:17 +02:00
|
|
|
|
TestHelper.ShortStringHelper,
|
2020-04-22 10:37:23 +10:00
|
|
|
|
scopeProvider,
|
|
|
|
|
|
publishedValuesOnly);
|
|
|
|
|
|
|
2018-11-28 01:23:02 +11:00
|
|
|
|
return contentValueSetBuilder;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-04-22 10:37:23 +10:00
|
|
|
|
public static ContentIndexPopulator GetContentIndexRebuilder(PropertyEditorCollection propertyEditors, IContentService contentService, IScopeProvider scopeProvider, bool publishedValuesOnly)
|
2018-11-28 01:23:02 +11:00
|
|
|
|
{
|
2020-04-22 10:37:23 +10:00
|
|
|
|
var contentValueSetBuilder = GetContentValueSetBuilder(propertyEditors, scopeProvider, publishedValuesOnly);
|
2020-10-22 17:16:05 +11:00
|
|
|
|
var contentIndexDataSource = new ContentIndexPopulator(publishedValuesOnly, null, contentService, scopeProvider.SqlContext, contentValueSetBuilder);
|
2018-11-28 01:23:02 +11:00
|
|
|
|
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);
|
2018-11-28 01:23:02 +11:00
|
|
|
|
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) &&
|
2018-11-28 01:23:02 +11:00
|
|
|
|
m.Path == (string)x.Attribute("path") &&
|
|
|
|
|
|
m.Properties == new PropertyCollection() &&
|
2019-01-03 09:27:52 +01:00
|
|
|
|
m.ContentType == Mock.Of<ISimpleContentType>(mt =>
|
2018-11-28 01:23:02 +11:00
|
|
|
|
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) &&
|
2018-11-28 01:23:02 +11:00
|
|
|
|
m.Path == (string)x.Attribute("path") &&
|
|
|
|
|
|
m.Properties == new PropertyCollection() &&
|
2019-02-05 19:58:33 +01:00
|
|
|
|
m.ContentType == Mock.Of<ISimpleContentType>(mt =>
|
2018-11-28 01:23:02 +11:00
|
|
|
|
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>
|
|
|
|
|
|
{
|
2019-12-09 14:12:06 +01:00
|
|
|
|
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"}
|
2018-11-28 01:23:02 +11:00
|
|
|
|
});
|
|
|
|
|
|
return mediaTypeServiceMock.Object;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-15 12:40:35 +02:00
|
|
|
|
public static IProfilingLogger GetMockProfilingLogger()
|
2019-11-13 12:07:11 +00:00
|
|
|
|
{
|
2020-11-20 12:24:16 +00:00
|
|
|
|
return new ProfilingLogger(Mock.Of<ILogger<ProfilingLogger>>(), Mock.Of<IProfiler>());
|
2019-11-13 12:07:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-10 13:06:39 +11:00
|
|
|
|
public static UmbracoContentIndex GetUmbracoIndexer(
|
2018-11-27 13:46:43 +01:00
|
|
|
|
IProfilingLogger profilingLogger,
|
2020-04-03 15:36:57 +11:00
|
|
|
|
IHostingEnvironment hostingEnvironment,
|
2019-11-19 11:44:34 +01:00
|
|
|
|
IRuntimeState runtimeState,
|
2018-06-29 19:52:40 +02:00
|
|
|
|
Directory luceneDir,
|
|
|
|
|
|
Analyzer analyzer = null,
|
2018-11-21 20:36:15 +11:00
|
|
|
|
ILocalizationService languageService = null,
|
2018-12-03 22:10:56 +11:00
|
|
|
|
IContentValueSetValidator validator = null)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
2018-11-21 20:36:15 +11:00
|
|
|
|
if (languageService == null)
|
2018-11-28 01:23:02 +11:00
|
|
|
|
languageService = GetMockLocalizationService();
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
|
|
if (analyzer == null)
|
|
|
|
|
|
analyzer = new StandardAnalyzer(Version.LUCENE_30);
|
|
|
|
|
|
|
2018-11-28 18:39:03 +11:00
|
|
|
|
if (validator == null)
|
2018-12-05 17:20:48 +11:00
|
|
|
|
validator = new ContentValueSetValidator(true);
|
2019-01-03 09:27:52 +01:00
|
|
|
|
|
2018-12-10 13:06:39 +11:00
|
|
|
|
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,
|
2020-09-28 14:59:17 +02:00
|
|
|
|
Mock.Of<ILogger<UmbracoContentIndex>>(),
|
2020-09-15 15:14:44 +02:00
|
|
|
|
Mock.Of<ILoggerFactory>(),
|
2020-09-28 14:59:17 +02:00
|
|
|
|
hostingEnvironment,
|
2019-11-19 11:44:34 +01:00
|
|
|
|
runtimeState,
|
2018-11-21 20:36:15 +11:00
|
|
|
|
languageService,
|
2018-11-28 18:39:03 +11:00
|
|
|
|
validator);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
|
|
i.IndexingError += IndexingError;
|
|
|
|
|
|
|
|
|
|
|
|
return i;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-11-22 14:05:36 +11:00
|
|
|
|
//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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|