Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@
namespace Umbraco.Cms.Infrastructure.Examine.DependencyInjection;

/// <summary>
/// Configures the index options to construct the Examine indexes
/// Configures the index options to construct the Examine indexes.
/// </summary>
public sealed class ConfigureIndexOptions : IConfigureNamedOptions<LuceneDirectoryIndexOptions>
{
private readonly IndexCreatorSettings _settings;
private readonly IUmbracoIndexConfig _umbracoIndexConfig;
private readonly IDeliveryApiContentIndexFieldDefinitionBuilder _deliveryApiContentIndexFieldDefinitionBuilder;

/// <summary>
/// Initializes a new instance of the <see cref="ConfigureIndexOptions"/> class.
/// </summary>
public ConfigureIndexOptions(
IUmbracoIndexConfig umbracoIndexConfig,
IOptions<IndexCreatorSettings> settings,
Expand All @@ -28,24 +31,27 @@ public ConfigureIndexOptions(
_deliveryApiContentIndexFieldDefinitionBuilder = deliveryApiContentIndexFieldDefinitionBuilder;
}

/// <inheritdoc/>
public void Configure(string? name, LuceneDirectoryIndexOptions options)
{
// When creating FieldDefinitions with Umbraco defaults, pass in any already defined to avoid overwriting
// those added via a package or custom code.
switch (name)
{
case Constants.UmbracoIndexes.InternalIndexName:
options.Analyzer = new CultureInvariantWhitespaceAnalyzer();
options.Validator = _umbracoIndexConfig.GetContentValueSetValidator();
options.FieldDefinitions = new UmbracoFieldDefinitionCollection();
options.FieldDefinitions = new UmbracoFieldDefinitionCollection(options.FieldDefinitions);
break;
case Constants.UmbracoIndexes.ExternalIndexName:
options.Analyzer = new StandardAnalyzer(LuceneInfo.CurrentVersion);
options.Validator = _umbracoIndexConfig.GetPublishedContentValueSetValidator();
options.FieldDefinitions = new UmbracoFieldDefinitionCollection();
options.FieldDefinitions = new UmbracoFieldDefinitionCollection(options.FieldDefinitions);
break;
case Constants.UmbracoIndexes.MembersIndexName:
options.Analyzer = new CultureInvariantWhitespaceAnalyzer();
options.Validator = _umbracoIndexConfig.GetMemberValueSetValidator();
options.FieldDefinitions = new UmbracoFieldDefinitionCollection();
options.FieldDefinitions = new UmbracoFieldDefinitionCollection(options.FieldDefinitions);
break;
case Constants.UmbracoIndexes.DeliveryApiContentIndexName:
options.Analyzer = new StandardAnalyzer(LuceneInfo.CurrentVersion);
Expand All @@ -64,6 +70,7 @@ public void Configure(string? name, LuceneDirectoryIndexOptions options)
}
}

/// <inheritdoc/>
public void Configure(LuceneDirectoryIndexOptions options)
=> throw new NotImplementedException("This is never called and is just part of the interface");
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,29 @@ public class UmbracoFieldDefinitionCollection : FieldDefinitionCollection
new(UmbracoExamineFieldNames.VariesByCultureFieldName, FieldDefinitionTypes.Raw),
};

/// <summary>
/// Initializes a new instance of the <see cref="UmbracoFieldDefinitionCollection"/> class containing
/// the default Umbraco field definitions.
/// </summary>
public UmbracoFieldDefinitionCollection()
: base(UmbracoIndexFieldDefinitions)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="UmbracoFieldDefinitionCollection"/> class containing the containing
/// the default Umbraco field definitions, augmented or overridden by the provided definitions.
/// </summary>
/// <param name="definitions">Existing collection of field definitions.</param>
public UmbracoFieldDefinitionCollection(FieldDefinitionCollection definitions)
: base(UmbracoIndexFieldDefinitions)
{
foreach (FieldDefinition definition in definitions)
{
AddOrUpdate(definition);
}
}

/// <summary>
/// Overridden to dynamically add field definitions for culture variations
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using Examine;
using NUnit.Framework;
using Umbraco.Cms.Infrastructure.Examine;

namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Infrastructure.Examine;

[TestFixture]
internal class UmbracoFieldDefinitionCollectionTests
{
[Test]
public void Create_Contains_Expected_Fields()
{
var collection = new UmbracoFieldDefinitionCollection();
AssertDefaultField(collection);
}

[Test]
public void Create_New_Contains_Expected_Fields()
{
var collection = new UmbracoFieldDefinitionCollection();
collection.AddOrUpdate(new FieldDefinition("customField", "string"));
var collectionCount = collection.Count;

collection = new UmbracoFieldDefinitionCollection();
Assert.AreEqual(collectionCount - 1, collection.Count);
AssertDefaultField(collection);
AssertCustomField(collection, expectExists: false);
}

[Test]
public void Create_With_Existing_Contains_Expected_Fields()
{
var collection = new UmbracoFieldDefinitionCollection();
collection.AddOrUpdate(new FieldDefinition("customField", "string"));
var collectionCount = collection.Count;

collection = new UmbracoFieldDefinitionCollection(collection);
Assert.AreEqual(collectionCount, collection.Count);
AssertDefaultField(collection);
AssertCustomField(collection, expectExists: true);
}

[Test]
public void Create_With_Existing_Retains_Override_Of_DefaultField()
{
var collection = new UmbracoFieldDefinitionCollection();
collection.AddOrUpdate(new FieldDefinition("parentID", "string"));

collection = new UmbracoFieldDefinitionCollection(collection);
AssertDefaultField(collection, "string");
}

private static void AssertDefaultField(UmbracoFieldDefinitionCollection collection, string expectedType = "int")
{
var field = collection.SingleOrDefault(x => x.Name == "parentID");
Assert.IsNotNull(field);
Assert.AreEqual("parentID", field.Name);
Assert.AreEqual(expectedType, field.Type);
}

private static void AssertCustomField(UmbracoFieldDefinitionCollection collection, bool expectExists)
{
var field = collection.SingleOrDefault(x => x.Name == "customField");
if (expectExists is false)
{
Assert.IsNull(field.Name);
Assert.IsNull(field.Type);
return;
}

Assert.IsNotNull(field);
Assert.AreEqual("customField", field.Name);
Assert.AreEqual("string", field.Type);
}
}
Loading