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

221 lines
9.8 KiB
C#
Raw Normal View History

using System;
2016-05-02 18:29:35 +02:00
using System.Collections.Generic;
using System.Linq;
using Examine;
using Examine.LuceneEngine.Providers;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
2015-03-31 17:04:32 +11:00
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;
2017-05-12 14:49:44 +02:00
using Umbraco.Core.Scoping;
using Umbraco.Core.Services;
using Umbraco.Core.Strings;
using Umbraco.Examine;
2015-03-31 17:04:32 +11:00
using IContentService = Umbraco.Core.Services.IContentService;
using IMediaService = Umbraco.Core.Services.IMediaService;
using Version = Lucene.Net.Util.Version;
namespace Umbraco.Tests.UmbracoExamine
{
2017-07-20 11:21:28 +02:00
/// <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,
2017-07-20 11:21:28 +02:00
Directory luceneDir,
2018-03-27 18:35:16 +11:00
ISqlContext sqlContext,
Analyzer analyzer = null,
2015-03-31 17:04:32 +11:00
IContentService contentService = null,
IMediaService mediaService = null,
IMemberService memberService = null,
IUserService userService = null,
IContentTypeService contentTypeService = null,
IMediaTypeService mediaTypeService = null,
UmbracoContentIndexerOptions options = null)
2017-07-20 11:21:28 +02:00
{
if (contentService == null)
{
long longTotalRecs;
2017-07-20 11:21:28 +02:00
var demoData = new ExamineDemoDataContentService();
2016-04-26 17:29:40 +02:00
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") &&
m.GetName(It.IsAny<string>()) == (string)x.Attribute("nodeName") &&
2016-04-26 17:29: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<string>(), It.IsAny<Direction>(), It.IsAny<string>())
==
allRecs
&& x.GetPagedDescendants(
It.IsAny<int>(), It.IsAny<long>(), It.IsAny<int>(), out longTotalRecs, It.IsAny<string>(), It.IsAny<Direction>(), It.IsAny<bool>(), It.IsAny<IQuery<IContent>>())
==
allRecs);
}
2017-07-20 11:21:28 +02:00
if (userService == null)
{
2017-09-14 19:29:12 +02:00
userService = Mock.Of<IUserService>(x => x.GetProfileById(It.IsAny<int>()) == Mock.Of<IProfile>(p => p.Id == 0 && p.Name == "admin"));
2017-07-20 11:21:28 +02:00
}
if (mediaService == null)
{
2015-07-24 12:54:59 +02:00
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") &&
m.GetName(It.IsAny<string>()) == (string)x.Attribute("nodeName") &&
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();
2017-07-20 11:21:28 +02:00
// MOCK!
var mediaServiceMock = new Mock<IMediaService>();
mediaServiceMock
.Setup(x => x.GetPagedDescendants(
It.IsAny<int>(), It.IsAny<long>(), It.IsAny<int>(), out totalRecs, It.IsAny<string>(), It.IsAny<Direction>(), It.IsAny<string>())
).Returns(() => allRecs);
mediaServiceMock
.Setup(x => x.GetPagedDescendants(
It.IsAny<int>(), It.IsAny<long>(), It.IsAny<int>(), out totalRecs, It.IsAny<string>(), It.IsAny<Direction>(), It.IsAny<bool>(), It.IsAny<IQuery<IMedia>>())
).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;
}
2017-07-20 11:21:28 +02:00
if (analyzer == null)
{
analyzer = new StandardAnalyzer(Version.LUCENE_30);
}
2017-07-20 11:21:28 +02:00
//var indexSet = new IndexSet();
// var indexCriteria = indexSet.ToIndexCriteria(dataService, UmbracoContentIndexer.IndexFieldPolicies);
2017-07-20 11:21:28 +02:00
//var i = new UmbracoContentIndexer(indexCriteria,
// luceneDir, //custom lucene directory
// dataService,
// contentService,
// mediaService,
// dataTypeService,
// userService,
// new[] { new DefaultUrlSegmentProvider() },
2017-07-20 11:21:28 +02:00
// analyzer,
// false);
2017-07-20 11:21:28 +02:00
//i.IndexSecondsInterval = 1;
2017-07-20 11:21:28 +02:00
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;
}
2017-05-12 14:49:44 +02:00
// 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 }) });
2018-03-27 18:35:16 +11:00
2017-07-20 11:21:28 +02:00
//scopeProvider
// .Setup(x => x.Query<IContent>())
// .Returns(query.Object);
var i = new UmbracoContentIndexer(
"testIndexer",
2018-03-27 18:35:16 +11:00
Enumerable.Empty<FieldDefinition>(),
2017-07-20 11:21:28 +02:00
luceneDir,
analyzer,
profilingLogger,
contentService,
mediaService,
userService,
sqlContext,
2017-07-20 11:21:28 +02:00
new[] {new DefaultUrlSegmentProvider()},
new UmbracoContentValueSetValidator(options, Mock.Of<IPublicAccessService>()),
options);
2017-07-20 11:21:28 +02:00
i.IndexingError += IndexingError;
return i;
}
public static LuceneSearcher GetLuceneSearcher(Directory luceneDir)
{
return new LuceneSearcher("testSearcher", luceneDir, new StandardAnalyzer(Version.LUCENE_29));
2017-07-20 11:21:28 +02:00
}
2017-07-20 11:21:28 +02: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));
2017-07-20 11:21:28 +02:00
return i;
}
2017-07-20 11:21:28 +02:00
internal static void IndexingError(object sender, IndexingErrorEventArgs e)
{
throw new ApplicationException(e.Message, e.InnerException);
2017-07-20 11:21:28 +02:00
}
2017-07-20 11:21:28 +02:00
}
}