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

217 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;
using Moq;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.Models.Membership;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.DatabaseModelDefinitions;
using Umbraco.Core.Persistence.Querying;
2018-11-26 14:43:08 +11:00
using Umbraco.Core.PropertyEditors;
2018-06-29 19:52:40 +02:00
using Umbraco.Core.Scoping;
using Umbraco.Core.Services;
using Umbraco.Core.Strings;
using Umbraco.Examine;
using IContentService = Umbraco.Core.Services.IContentService;
using IMediaService = Umbraco.Core.Services.IMediaService;
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 UmbracoContentIndexer GetUmbracoIndexer(
ProfilingLogger profilingLogger,
Directory luceneDir,
ISqlContext sqlContext,
2018-11-26 14:43:08 +11:00
PropertyEditorCollection propertyEditors,
2018-06-29 19:52:40 +02:00
Analyzer analyzer = null,
IContentService contentService = null,
IMediaService mediaService = null,
IMemberService memberService = null,
IUserService userService = null,
ILocalizationService languageService = null,
2018-06-29 19:52:40 +02:00
IContentTypeService contentTypeService = null,
IMediaTypeService mediaTypeService = null,
UmbracoContentIndexerOptions options = null)
{
if (languageService == null)
{
languageService = Mock.Of<ILocalizationService>(
x => x.GetAllLanguages() == Array.Empty<ILanguage>());
}
2018-06-29 19:52:40 +02:00
if (contentService == null)
{
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") &&
m.Name == (string)x.Attribute("nodeName") &&
2018-06-20 14:18:57 +02:00
m.GetCultureName(It.IsAny<string>()) == (string)x.Attribute("nodeName") &&
2018-06-29 19:52:40 +02:00
m.Path == (string)x.Attribute("path") &&
m.Properties == new PropertyCollection() &&
m.ContentType == Mock.Of<IContentType>(mt =>
mt.Icon == "test" &&
mt.Alias == x.Name.LocalName &&
mt.Id == (int)x.Attribute("nodeType"))))
.ToArray();
contentService = Mock.Of<IContentService>(
x => x.GetPagedDescendants(
It.IsAny<int>(), It.IsAny<long>(), It.IsAny<int>(), out longTotalRecs, It.IsAny<IQuery<IContent>>(), It.IsAny<Ordering>())
2018-06-29 19:52:40 +02:00
==
allRecs);
}
if (userService == null)
{
userService = Mock.Of<IUserService>(x => x.GetProfileById(It.IsAny<int>()) == Mock.Of<IProfile>(p => p.Id == 0 && p.Name == "admin"));
}
if (mediaService == null)
{
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") &&
m.Name == (string) x.Attribute("nodeName") &&
2018-06-20 14:18:57 +02:00
m.GetCultureName(It.IsAny<string>()) == (string)x.Attribute("nodeName") &&
2018-06-29 19:52:40 +02:00
m.Path == (string) x.Attribute("path") &&
m.Properties == new PropertyCollection() &&
m.ContentType == Mock.Of<IMediaType>(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>())
2018-06-29 19:52:40 +02:00
).Returns(() => allRecs);
//mediaServiceMock.Setup(service => service.GetPagedXmlEntries(It.IsAny<string>(), It.IsAny<long>(), It.IsAny<int>(), out longTotalRecs))
// .Returns(() => allRecs.Select(x => x.ToXml()));
mediaService = mediaServiceMock.Object;
}
if (analyzer == null)
{
analyzer = new StandardAnalyzer(Version.LUCENE_30);
}
//var indexSet = new IndexSet();
// var indexCriteria = indexSet.ToIndexCriteria(dataService, UmbracoContentIndexer.IndexFieldPolicies);
//var i = new UmbracoContentIndexer(indexCriteria,
// luceneDir, //custom lucene directory
// dataService,
// contentService,
// mediaService,
// dataTypeService,
// userService,
// new[] { new DefaultUrlSegmentProvider() },
// analyzer,
// false);
//i.IndexSecondsInterval = 1;
if (options == null)
{
options = new UmbracoContentIndexerOptions(false, false, null);
}
if (mediaTypeService == null)
{
var mediaTypeServiceMock = new Mock<IMediaTypeService>();
mediaTypeServiceMock.Setup(x => x.GetAll())
.Returns(new List<IMediaType>
{
new MediaType(-1) {Alias = "Folder", Name = "Folder", Id = 1031, Icon = "icon-folder"},
new MediaType(-1) {Alias = "Image", Name = "Image", Id = 1032, Icon = "icon-picture"}
});
mediaTypeService = mediaTypeServiceMock.Object;
}
// fixme oops?!
//var query = new Mock<IQuery<IContent>>();
//query
// .Setup(x => x.GetWhereClauses())
// .Returns(new List<Tuple<string, object[]>> { new Tuple<string, object[]>($"{Constants.DatabaseSchema.Tables.Document}.published", new object[] { 1 }) });
//scopeProvider
// .Setup(x => x.Query<IContent>())
// .Returns(query.Object);
var i = new UmbracoContentIndexer(
"testIndexer",
2018-11-26 14:43:08 +11:00
UmbracoExamineIndexer.UmbracoIndexFieldDefinitions,
2018-06-29 19:52:40 +02:00
luceneDir,
analyzer,
profilingLogger,
2018-11-26 14:43:08 +11:00
new ContentValueSetBuilder(propertyEditors, new[] { new DefaultUrlSegmentProvider() }, userService),
new MediaValueSetBuilder(propertyEditors, new[] { new DefaultUrlSegmentProvider() }, userService),
2018-06-29 19:52:40 +02:00
contentService,
mediaService,
languageService,
2018-06-29 19:52:40 +02:00
sqlContext,
new UmbracoContentValueSetValidator(options, Mock.Of<IPublicAccessService>()),
options);
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);
}
}
}