2013-01-05 03:28:47 +03:00
|
|
|
|
using System;
|
2016-05-02 18:29:35 +02:00
|
|
|
|
using System.Collections.Generic;
|
2013-01-05 23:36:38 +03:00
|
|
|
|
using System.Linq;
|
2013-01-05 03:28:47 +03:00
|
|
|
|
using Examine;
|
|
|
|
|
|
using Examine.LuceneEngine.Providers;
|
2013-02-14 06:33:48 +06:00
|
|
|
|
using Lucene.Net.Analysis;
|
2013-01-05 03:28:47 +03:00
|
|
|
|
using Lucene.Net.Analysis.Standard;
|
2015-03-31 17:04:32 +11:00
|
|
|
|
using Lucene.Net.Store;
|
2014-09-30 15:13:10 +10:00
|
|
|
|
using Moq;
|
2016-04-23 18:57:13 +02:00
|
|
|
|
using Umbraco.Core.Logging;
|
2014-09-30 18:46:02 +10:00
|
|
|
|
using Umbraco.Core.Models;
|
|
|
|
|
|
using Umbraco.Core.Models.Membership;
|
2018-03-27 18:14:21 +11:00
|
|
|
|
using Umbraco.Core.Persistence;
|
2014-09-30 18:46:02 +10:00
|
|
|
|
using Umbraco.Core.Persistence.DatabaseModelDefinitions;
|
2016-05-02 15:38:45 +02:00
|
|
|
|
using Umbraco.Core.Persistence.Querying;
|
2017-05-12 14:49:44 +02:00
|
|
|
|
using Umbraco.Core.Scoping;
|
2014-09-30 15:13:10 +10:00
|
|
|
|
using Umbraco.Core.Services;
|
2015-01-27 14:58:33 +11:00
|
|
|
|
using Umbraco.Core.Strings;
|
2017-07-27 12:01:38 +02:00
|
|
|
|
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;
|
2013-01-05 03:28:47 +03:00
|
|
|
|
|
|
|
|
|
|
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(
|
2016-04-23 18:57:13 +02:00
|
|
|
|
ProfilingLogger profilingLogger,
|
2017-07-20 11:21:28 +02:00
|
|
|
|
Directory luceneDir,
|
2018-03-27 18:35:16 +11:00
|
|
|
|
ISqlContext sqlContext,
|
2013-02-14 23:06:28 +06:00
|
|
|
|
Analyzer analyzer = null,
|
2015-03-31 17:04:32 +11:00
|
|
|
|
IContentService contentService = null,
|
|
|
|
|
|
IMediaService mediaService = null,
|
2014-09-30 18:46:02 +10:00
|
|
|
|
IMemberService memberService = null,
|
2016-04-26 15:48:25 +02:00
|
|
|
|
IUserService userService = null,
|
2016-11-23 10:33:12 +01:00
|
|
|
|
IContentTypeService contentTypeService = null,
|
|
|
|
|
|
IMediaTypeService mediaTypeService = null,
|
2016-04-26 15:48:25 +02:00
|
|
|
|
UmbracoContentIndexerOptions options = null)
|
2017-07-20 11:21:28 +02:00
|
|
|
|
{
|
|
|
|
|
|
if (contentService == null)
|
|
|
|
|
|
{
|
2016-05-26 15:30:40 +02:00
|
|
|
|
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") &&
|
2018-04-30 21:03:43 +02:00
|
|
|
|
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(
|
2016-05-26 15:30:40 +02:00
|
|
|
|
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>>())
|
2016-04-29 10:50:03 +02:00
|
|
|
|
==
|
2016-05-26 15:30:40 +02:00
|
|
|
|
allRecs);
|
2016-04-29 10:50:03 +02:00
|
|
|
|
}
|
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
|
|
|
|
}
|
2018-03-27 18:14:21 +11:00
|
|
|
|
|
2014-09-30 15:13:10 +10:00
|
|
|
|
if (mediaService == null)
|
|
|
|
|
|
{
|
2015-07-24 12:54:59 +02:00
|
|
|
|
long totalRecs;
|
2014-09-30 18:46:02 +10:00
|
|
|
|
|
2016-04-23 18:57:13 +02:00
|
|
|
|
var demoData = new ExamineDemoDataMediaService();
|
|
|
|
|
|
|
|
|
|
|
|
var allRecs = demoData.GetLatestMediaByXpath("//node")
|
2014-09-30 18:46:02 +10:00
|
|
|
|
.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-04-30 21:03:43 +02:00
|
|
|
|
m.GetName(It.IsAny<string>()) == (string)x.Attribute("nodeName") &&
|
2014-09-30 18:46:02 +10: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();
|
2017-07-20 11:21:28 +02:00
|
|
|
|
|
2016-10-27 10:47:32 +02:00
|
|
|
|
// MOCK!
|
|
|
|
|
|
var mediaServiceMock = new Mock<IMediaService>();
|
2014-09-30 18:46:02 +10:00
|
|
|
|
|
2016-10-27 10:47:32 +02:00
|
|
|
|
mediaServiceMock
|
|
|
|
|
|
.Setup(x => x.GetPagedDescendants(
|
2016-11-23 10:33:12 +01:00
|
|
|
|
It.IsAny<int>(), It.IsAny<long>(), It.IsAny<int>(), out totalRecs, It.IsAny<string>(), It.IsAny<Direction>(), It.IsAny<string>())
|
2016-10-27 10:47:32 +02:00
|
|
|
|
).Returns(() => allRecs);
|
2016-04-29 10:50:03 +02:00
|
|
|
|
|
2016-10-27 10:47:32 +02:00
|
|
|
|
mediaServiceMock
|
|
|
|
|
|
.Setup(x => x.GetPagedDescendants(
|
2016-11-23 10:33:12 +01:00
|
|
|
|
It.IsAny<int>(), It.IsAny<long>(), It.IsAny<int>(), out totalRecs, It.IsAny<string>(), It.IsAny<Direction>(), It.IsAny<bool>(), It.IsAny<IQuery<IMedia>>())
|
2016-10-27 10:47:32 +02:00
|
|
|
|
).Returns(() => allRecs);
|
|
|
|
|
|
|
2016-11-23 10:33:12 +01:00
|
|
|
|
//mediaServiceMock.Setup(service => service.GetPagedXmlEntries(It.IsAny<string>(), It.IsAny<long>(), It.IsAny<int>(), out longTotalRecs))
|
|
|
|
|
|
// .Returns(() => allRecs.Select(x => x.ToXml()));
|
2016-10-27 10:47:32 +02:00
|
|
|
|
|
|
|
|
|
|
mediaService = mediaServiceMock.Object;
|
2016-04-29 10:50:03 +02:00
|
|
|
|
|
2014-09-30 15:13:10 +10:00
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
|
2013-02-14 06:33:48 +06:00
|
|
|
|
if (analyzer == null)
|
|
|
|
|
|
{
|
2016-03-30 15:12:30 +02:00
|
|
|
|
analyzer = new StandardAnalyzer(Version.LUCENE_30);
|
2013-02-14 06:33:48 +06:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-07-20 11:21:28 +02:00
|
|
|
|
//var indexSet = new IndexSet();
|
2016-03-30 15:12:30 +02:00
|
|
|
|
// var indexCriteria = indexSet.ToIndexCriteria(dataService, UmbracoContentIndexer.IndexFieldPolicies);
|
2017-07-20 11:21:28 +02:00
|
|
|
|
|
|
|
|
|
|
//var i = new UmbracoContentIndexer(indexCriteria,
|
|
|
|
|
|
// luceneDir, //custom lucene directory
|
2016-03-30 15:12:30 +02:00
|
|
|
|
// dataService,
|
|
|
|
|
|
// contentService,
|
|
|
|
|
|
// mediaService,
|
|
|
|
|
|
// dataTypeService,
|
|
|
|
|
|
// userService,
|
|
|
|
|
|
// new[] { new DefaultUrlSegmentProvider() },
|
2017-07-20 11:21:28 +02:00
|
|
|
|
// analyzer,
|
|
|
|
|
|
// false);
|
2013-01-05 03:28:47 +03:00
|
|
|
|
|
2017-07-20 11:21:28 +02:00
|
|
|
|
//i.IndexSecondsInterval = 1;
|
2013-01-05 03:28:47 +03:00
|
|
|
|
|
2017-07-20 11:21:28 +02:00
|
|
|
|
if (options == null)
|
|
|
|
|
|
{
|
2016-04-26 15:48:25 +02:00
|
|
|
|
options = new UmbracoContentIndexerOptions(false, false, null);
|
|
|
|
|
|
}
|
2016-04-23 18:57:13 +02:00
|
|
|
|
|
2016-11-23 10:33:12 +01:00
|
|
|
|
if (mediaTypeService == null)
|
2016-10-27 10:47:32 +02:00
|
|
|
|
{
|
2016-11-23 10:33:12 +01:00
|
|
|
|
var mediaTypeServiceMock = new Mock<IMediaTypeService>();
|
|
|
|
|
|
mediaTypeServiceMock.Setup(x => x.GetAll())
|
|
|
|
|
|
.Returns(new List<IMediaType>
|
2016-10-27 10:47:32 +02:00
|
|
|
|
{
|
2016-11-14 15:38:50 +01:00
|
|
|
|
new MediaType(-1) {Alias = "Folder", Name = "Folder", Id = 1031, Icon = "icon-folder"},
|
|
|
|
|
|
new MediaType(-1) {Alias = "Image", Name = "Image", Id = 1032, Icon = "icon-picture"}
|
2016-10-27 10:47:32 +02:00
|
|
|
|
});
|
2016-11-23 10:33:12 +01:00
|
|
|
|
mediaTypeService = mediaTypeServiceMock.Object;
|
2013-02-14 06:33:48 +06:00
|
|
|
|
}
|
2016-02-26 14:30:32 +00:00
|
|
|
|
|
2017-05-12 14:49:44 +02:00
|
|
|
|
// fixme oops?!
|
|
|
|
|
|
//var query = new Mock<IQuery<IContent>>();
|
|
|
|
|
|
//query
|
|
|
|
|
|
// .Setup(x => x.GetWhereClauses())
|
2018-03-22 17:17:01 +11:00
|
|
|
|
// .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(
|
2018-03-28 16:12:43 +11:00
|
|
|
|
"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,
|
2018-03-27 18:14:21 +11:00
|
|
|
|
sqlContext,
|
2017-07-20 11:21:28 +02:00
|
|
|
|
new[] {new DefaultUrlSegmentProvider()},
|
|
|
|
|
|
new UmbracoContentValueSetValidator(options, Mock.Of<IPublicAccessService>()),
|
2018-03-27 18:14:21 +11:00
|
|
|
|
options);
|
2016-03-30 15:12:30 +02:00
|
|
|
|
|
2017-07-20 11:21:28 +02:00
|
|
|
|
i.IndexingError += IndexingError;
|
|
|
|
|
|
|
|
|
|
|
|
return i;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static LuceneSearcher GetLuceneSearcher(Directory luceneDir)
|
|
|
|
|
|
{
|
2018-03-28 16:12:43 +11:00
|
|
|
|
return new LuceneSearcher("testSearcher", luceneDir, new StandardAnalyzer(Version.LUCENE_29));
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|
2013-01-05 03:28:47 +03:00
|
|
|
|
|
2017-07-20 11:21:28 +02:00
|
|
|
|
public static MultiIndexSearcher GetMultiSearcher(Directory pdfDir, Directory simpleDir, Directory conventionDir, Directory cwsDir)
|
|
|
|
|
|
{
|
2018-03-28 16:12:43 +11:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
2013-01-05 03:28:47 +03:00
|
|
|
|
|
|
|
|
|
|
|
2017-07-20 11:21:28 +02:00
|
|
|
|
internal static void IndexingError(object sender, IndexingErrorEventArgs e)
|
|
|
|
|
|
{
|
2018-03-27 18:14:21 +11:00
|
|
|
|
throw new ApplicationException(e.Message, e.InnerException);
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|
2013-01-05 03:28:47 +03:00
|
|
|
|
|
|
|
|
|
|
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|