diff --git a/src/OrchardCore.Modules/OrchardCore.ContentFields/Settings/BooleanFieldSettingsDriver.cs b/src/OrchardCore.Modules/OrchardCore.ContentFields/Settings/BooleanFieldSettingsDriver.cs index ddf595f35a9..918535ff588 100644 --- a/src/OrchardCore.Modules/OrchardCore.ContentFields/Settings/BooleanFieldSettingsDriver.cs +++ b/src/OrchardCore.Modules/OrchardCore.ContentFields/Settings/BooleanFieldSettingsDriver.cs @@ -13,7 +13,7 @@ public override IDisplayResult Edit(ContentPartFieldDefinition partFieldDefiniti { return Initialize("BooleanFieldSettings_Edit", model => { - var settings = partFieldDefinition.Settings.ToObject(); + var settings = partFieldDefinition.GetSettings(); model.Hint = settings.Hint; model.Label = settings.Label; diff --git a/src/OrchardCore.Modules/OrchardCore.ContentFields/Settings/DateFieldSettingsDriver.cs b/src/OrchardCore.Modules/OrchardCore.ContentFields/Settings/DateFieldSettingsDriver.cs index 76c91f54e7b..7105472b225 100644 --- a/src/OrchardCore.Modules/OrchardCore.ContentFields/Settings/DateFieldSettingsDriver.cs +++ b/src/OrchardCore.Modules/OrchardCore.ContentFields/Settings/DateFieldSettingsDriver.cs @@ -13,7 +13,7 @@ public override IDisplayResult Edit(ContentPartFieldDefinition partFieldDefiniti { return Initialize("DateFieldSettings_Edit", model => { - var settings = partFieldDefinition.Settings.ToObject(); + var settings = partFieldDefinition.GetSettings(); model.Hint = settings.Hint; model.Required = settings.Required; diff --git a/src/OrchardCore.Modules/OrchardCore.ContentFields/Settings/DateTimeFieldSettingsDriver.cs b/src/OrchardCore.Modules/OrchardCore.ContentFields/Settings/DateTimeFieldSettingsDriver.cs index 4637b788cc2..710a689505e 100644 --- a/src/OrchardCore.Modules/OrchardCore.ContentFields/Settings/DateTimeFieldSettingsDriver.cs +++ b/src/OrchardCore.Modules/OrchardCore.ContentFields/Settings/DateTimeFieldSettingsDriver.cs @@ -13,7 +13,7 @@ public override IDisplayResult Edit(ContentPartFieldDefinition partFieldDefiniti { return Initialize("DateTimeFieldSettings_Edit", model => { - var settings = partFieldDefinition.Settings.ToObject(); + var settings = partFieldDefinition.GetSettings(); model.Hint = settings.Hint; model.Required = settings.Required; diff --git a/src/OrchardCore.Modules/OrchardCore.ContentFields/Settings/LinkFieldSettingsDriver.cs b/src/OrchardCore.Modules/OrchardCore.ContentFields/Settings/LinkFieldSettingsDriver.cs index fae978770ef..af739c17dda 100644 --- a/src/OrchardCore.Modules/OrchardCore.ContentFields/Settings/LinkFieldSettingsDriver.cs +++ b/src/OrchardCore.Modules/OrchardCore.ContentFields/Settings/LinkFieldSettingsDriver.cs @@ -13,7 +13,7 @@ public override IDisplayResult Edit(ContentPartFieldDefinition partFieldDefiniti { return Initialize("LinkFieldSettings_Edit", model => { - var settings = partFieldDefinition.Settings.ToObject(); + var settings = partFieldDefinition.GetSettings(); model.Hint = settings.Hint; model.HintLinkText = settings.HintLinkText; diff --git a/src/OrchardCore.Modules/OrchardCore.ContentFields/Settings/LocalizationSetContentPickerFieldSettingsDriver.cs b/src/OrchardCore.Modules/OrchardCore.ContentFields/Settings/LocalizationSetContentPickerFieldSettingsDriver.cs index c97990ec069..97a0e38f25b 100644 --- a/src/OrchardCore.Modules/OrchardCore.ContentFields/Settings/LocalizationSetContentPickerFieldSettingsDriver.cs +++ b/src/OrchardCore.Modules/OrchardCore.ContentFields/Settings/LocalizationSetContentPickerFieldSettingsDriver.cs @@ -15,7 +15,7 @@ public override IDisplayResult Edit(ContentPartFieldDefinition partFieldDefiniti { return Initialize("LocalizationSetContentPickerFieldSettings_Edit", model => { - var settings = partFieldDefinition.Settings.ToObject(); + var settings = partFieldDefinition.GetSettings(); model.Hint = settings.Hint; model.Required = settings.Required; diff --git a/src/OrchardCore.Modules/OrchardCore.ContentFields/Settings/NumericFieldSettingsDriver.cs b/src/OrchardCore.Modules/OrchardCore.ContentFields/Settings/NumericFieldSettingsDriver.cs index a1d5e6584bb..39a8bde5f18 100644 --- a/src/OrchardCore.Modules/OrchardCore.ContentFields/Settings/NumericFieldSettingsDriver.cs +++ b/src/OrchardCore.Modules/OrchardCore.ContentFields/Settings/NumericFieldSettingsDriver.cs @@ -13,7 +13,7 @@ public override IDisplayResult Edit(ContentPartFieldDefinition partFieldDefiniti { return Initialize("NumericFieldSettings_Edit", model => { - var settings = partFieldDefinition.Settings.ToObject(); + var settings = partFieldDefinition.GetSettings(); model.Hint = settings.Hint; model.Required = settings.Required; diff --git a/src/OrchardCore.Modules/OrchardCore.ContentFields/Settings/TextFieldSettingsDriver.cs b/src/OrchardCore.Modules/OrchardCore.ContentFields/Settings/TextFieldSettingsDriver.cs index 69a36e7c2fe..fdc4bb88390 100644 --- a/src/OrchardCore.Modules/OrchardCore.ContentFields/Settings/TextFieldSettingsDriver.cs +++ b/src/OrchardCore.Modules/OrchardCore.ContentFields/Settings/TextFieldSettingsDriver.cs @@ -13,7 +13,7 @@ public override IDisplayResult Edit(ContentPartFieldDefinition partFieldDefiniti { return Initialize("TextFieldSettings_Edit", model => { - var settings = partFieldDefinition.Settings.ToObject(); + var settings = partFieldDefinition.GetSettings(); model.Hint = settings.Hint; model.Required = settings.Required; diff --git a/src/OrchardCore.Modules/OrchardCore.ContentFields/Settings/TimeFieldSettingsDriver.cs b/src/OrchardCore.Modules/OrchardCore.ContentFields/Settings/TimeFieldSettingsDriver.cs index 7dc0ee25454..549e1c1bb6e 100644 --- a/src/OrchardCore.Modules/OrchardCore.ContentFields/Settings/TimeFieldSettingsDriver.cs +++ b/src/OrchardCore.Modules/OrchardCore.ContentFields/Settings/TimeFieldSettingsDriver.cs @@ -13,7 +13,7 @@ public override IDisplayResult Edit(ContentPartFieldDefinition partFieldDefiniti { return Initialize("TimeFieldSettings_Edit", model => { - var settings = partFieldDefinition.Settings.ToObject(); + var settings = partFieldDefinition.GetSettings(); model.Hint = settings.Hint; model.Required = settings.Required; diff --git a/src/OrchardCore.Modules/OrchardCore.ContentFields/Settings/YoutubeFieldSettingsDriver.cs b/src/OrchardCore.Modules/OrchardCore.ContentFields/Settings/YoutubeFieldSettingsDriver.cs index 587251dc49a..75cfe541449 100644 --- a/src/OrchardCore.Modules/OrchardCore.ContentFields/Settings/YoutubeFieldSettingsDriver.cs +++ b/src/OrchardCore.Modules/OrchardCore.ContentFields/Settings/YoutubeFieldSettingsDriver.cs @@ -13,10 +13,13 @@ public override IDisplayResult Edit(ContentPartFieldDefinition partFieldDefiniti { return Initialize("YoutubeFieldSetting_Edit", model => { - var settings = partFieldDefinition.Settings.ToObject(); + var settings = partFieldDefinition.GetSettings(); - model.Height = model.Height != default ? model.Height : 315; - model.Width = model.Width != default ? model.Width : 560; + model.Hint = settings.Hint; + model.Label = settings.Label; + model.Height = settings.Height != default ? settings.Height : 315; + model.Width = settings.Width != default ? settings.Width : 560; + model.Required = settings.Required; }).Location("Content"); } diff --git a/src/OrchardCore.Modules/OrchardCore.Search.Elasticsearch/Drivers/ContentPickerFieldElasticEditorSettingsDriver.cs b/src/OrchardCore.Modules/OrchardCore.Search.Elasticsearch/Drivers/ContentPickerFieldElasticEditorSettingsDriver.cs index 8435b7be6dd..8af8deab783 100644 --- a/src/OrchardCore.Modules/OrchardCore.Search.Elasticsearch/Drivers/ContentPickerFieldElasticEditorSettingsDriver.cs +++ b/src/OrchardCore.Modules/OrchardCore.Search.Elasticsearch/Drivers/ContentPickerFieldElasticEditorSettingsDriver.cs @@ -21,7 +21,7 @@ public override IDisplayResult Edit(ContentPartFieldDefinition partFieldDefiniti { return Initialize("ContentPickerFieldElasticEditorSettings_Edit", async model => { - var settings = partFieldDefinition.Settings.ToObject(); + var settings = partFieldDefinition.GetSettings(); model.Index = settings.Index; diff --git a/src/OrchardCore.Modules/OrchardCore.Search.Lucene/Settings/ContentPickerFieldLuceneEditorSettingsDriver.cs b/src/OrchardCore.Modules/OrchardCore.Search.Lucene/Settings/ContentPickerFieldLuceneEditorSettingsDriver.cs index b787c12d4a9..4bf6a278c5c 100644 --- a/src/OrchardCore.Modules/OrchardCore.Search.Lucene/Settings/ContentPickerFieldLuceneEditorSettingsDriver.cs +++ b/src/OrchardCore.Modules/OrchardCore.Search.Lucene/Settings/ContentPickerFieldLuceneEditorSettingsDriver.cs @@ -19,7 +19,7 @@ public override IDisplayResult Edit(ContentPartFieldDefinition partFieldDefiniti { return Initialize("ContentPickerFieldLuceneEditorSettings_Edit", async model => { - var settings = partFieldDefinition.Settings.ToObject(); + var settings = partFieldDefinition.GetSettings(); model.Index = settings.Index; model.Indices = (await _luceneIndexSettingsService.GetSettingsAsync()).Select(x => x.IndexName).ToArray(); diff --git a/src/OrchardCore.Modules/OrchardCore.Spatial/Drivers/GeoPointFieldSettingsDriver.cs b/src/OrchardCore.Modules/OrchardCore.Spatial/Drivers/GeoPointFieldSettingsDriver.cs index 35d4adf67fd..ff55a954b1b 100644 --- a/src/OrchardCore.Modules/OrchardCore.Spatial/Drivers/GeoPointFieldSettingsDriver.cs +++ b/src/OrchardCore.Modules/OrchardCore.Spatial/Drivers/GeoPointFieldSettingsDriver.cs @@ -14,7 +14,7 @@ public override IDisplayResult Edit(ContentPartFieldDefinition partFieldDefiniti { return Initialize("GeoPointFieldSettings_Edit", model => { - var settings = partFieldDefinition.Settings.ToObject(); + var settings = partFieldDefinition.GetSettings(); model.Hint = settings.Hint; model.Required = settings.Required; diff --git a/src/OrchardCore.Modules/OrchardCore.Taxonomies/Settings/TaxonomyFieldSettingsDriver.cs b/src/OrchardCore.Modules/OrchardCore.Taxonomies/Settings/TaxonomyFieldSettingsDriver.cs index cc4c5ee60c1..c2c33a99ec5 100644 --- a/src/OrchardCore.Modules/OrchardCore.Taxonomies/Settings/TaxonomyFieldSettingsDriver.cs +++ b/src/OrchardCore.Modules/OrchardCore.Taxonomies/Settings/TaxonomyFieldSettingsDriver.cs @@ -13,7 +13,7 @@ public override IDisplayResult Edit(ContentPartFieldDefinition partFieldDefiniti { return Initialize("TaxonomyFieldSettings_Edit", model => { - var settings = partFieldDefinition.Settings.ToObject(); + var settings = partFieldDefinition.GetSettings(); model.Hint = settings.Hint; model.Required = settings.Required; diff --git a/src/OrchardCore.Modules/OrchardCore.Taxonomies/Settings/TaxonomyFieldTagsEditorSettingsDriver.cs b/src/OrchardCore.Modules/OrchardCore.Taxonomies/Settings/TaxonomyFieldTagsEditorSettingsDriver.cs index 30f6fedec07..69526e3bb6a 100644 --- a/src/OrchardCore.Modules/OrchardCore.Taxonomies/Settings/TaxonomyFieldTagsEditorSettingsDriver.cs +++ b/src/OrchardCore.Modules/OrchardCore.Taxonomies/Settings/TaxonomyFieldTagsEditorSettingsDriver.cs @@ -13,7 +13,7 @@ public override IDisplayResult Edit(ContentPartFieldDefinition partFieldDefiniti { return Initialize("TaxonomyFieldTagsEditorSettings_Edit", model => { - var settings = partFieldDefinition.Settings.ToObject(); + var settings = partFieldDefinition.GetSettings(); model.Open = settings.Open; }).Location("Content"); diff --git a/src/docs/releases/2.0.0.md b/src/docs/releases/2.0.0.md index a3452087d71..a5621ae035a 100644 --- a/src/docs/releases/2.0.0.md +++ b/src/docs/releases/2.0.0.md @@ -801,7 +801,7 @@ services.AddJsonDerivedTypeInfo(); { return Initialize("NumericFieldSettings_Edit", model => { - var settings = partFieldDefinition.Settings.ToObject(); + var settings = partFieldDefinition.GetSettings(); model.Hint = settings.Hint; // ... }); diff --git a/test/OrchardCore.Tests/Modules/OrchardCore.ContentFields/Settings/DisplayDriverTestHelper.cs b/test/OrchardCore.Tests/Modules/OrchardCore.ContentFields/Settings/DisplayDriverTestHelper.cs new file mode 100644 index 00000000000..abc053b345c --- /dev/null +++ b/test/OrchardCore.Tests/Modules/OrchardCore.ContentFields/Settings/DisplayDriverTestHelper.cs @@ -0,0 +1,40 @@ +using OrchardCore.ContentManagement.Metadata.Builders; +using OrchardCore.ContentManagement.Metadata.Models; +using OrchardCore.ContentTypes.Editors; +using OrchardCore.DisplayManagement; +using OrchardCore.DisplayManagement.Handlers; +using OrchardCore.DisplayManagement.Views; +using OrchardCore.DisplayManagement.Zones; + +namespace OrchardCore.Tests.Modules.OrchardCore.ContentFields.Settings; + +public class DisplayDriverTestHelper +{ + public static ContentPartDefinition GetContentPartDefinition(Action configuration) + { + return new ContentPartDefinitionBuilder() + .Named("SomeContentPart") + .WithField("SomeField", configuration) + .Build(); + } + + public static Task GetShapeResultAsync(IShapeFactory factory, ContentPartDefinition contentDefinition) + where TDriver : IContentPartFieldDefinitionDisplayDriver, new() + => GetShapeResultAsync(factory, contentDefinition, new TDriver()); + + public static async Task GetShapeResultAsync(IShapeFactory factory, ContentPartDefinition contentDefinition, IContentPartFieldDefinitionDisplayDriver driver) + { + var partFieldDefinition = contentDefinition.Fields.First(); + + var partFieldDefinitionShape = await factory.CreateAsync("ContentPartFieldDefinition_Edit", () => + ValueTask.FromResult(new ZoneHolding(() => factory.CreateAsync("ContentZone")))); + partFieldDefinitionShape.Properties["ContentField"] = partFieldDefinition; + + var editorContext = new BuildEditorContext(partFieldDefinitionShape, "", false, "", factory, null, null); + + var result = await driver.BuildEditorAsync(partFieldDefinition, editorContext); + await result.ApplyAsync(editorContext); + + return (ShapeResult)result; + } +} diff --git a/test/OrchardCore.Tests/Modules/OrchardCore.ContentFields/Settings/SettingsDisplayDriverTests.cs b/test/OrchardCore.Tests/Modules/OrchardCore.ContentFields/Settings/SettingsDisplayDriverTests.cs new file mode 100644 index 00000000000..670d2c10c30 --- /dev/null +++ b/test/OrchardCore.Tests/Modules/OrchardCore.ContentFields/Settings/SettingsDisplayDriverTests.cs @@ -0,0 +1,251 @@ +using OrchardCore.ContentFields.Fields; +using OrchardCore.ContentFields.Settings; +using OrchardCore.DisplayManagement; +using OrchardCore.DisplayManagement.Descriptors; +using OrchardCore.DisplayManagement.Implementation; +using OrchardCore.DisplayManagement.Theming; +using OrchardCore.Environment.Extensions; +using OrchardCore.Tests.Stubs; + +namespace OrchardCore.Tests.Modules.OrchardCore.ContentFields.Settings; + +public class SettingsDisplayDriverTests +{ + private readonly IServiceProvider _serviceProvider; + private readonly IShapeFactory _shapeFactory; + private readonly ShapeTable _shapeTable; + + public SettingsDisplayDriverTests() + { + var serviceCollection = new ServiceCollection(); + + serviceCollection.AddScoped() + .AddScoped() + .AddScoped() + .AddScoped() + .AddScoped(); + + _shapeTable = new ShapeTable + ( + new Dictionary(StringComparer.OrdinalIgnoreCase), + new Dictionary(StringComparer.OrdinalIgnoreCase) + ); + + serviceCollection.AddSingleton(_shapeTable); + + _serviceProvider = serviceCollection.BuildServiceProvider(); + + _shapeFactory = _serviceProvider.GetRequiredService(); + } + + [Fact] + public async Task BooleanFieldSettingsShouldDeserialize() + { + var settings = new BooleanFieldSettings + { + DefaultValue = true, + Hint = "Test Hint", + Label = "Test Label", + }; + + var contentDefinition = DisplayDriverTestHelper.GetContentPartDefinition(field => field.WithSettings(settings)); + + var shapeResult = await DisplayDriverTestHelper.GetShapeResultAsync(_shapeFactory, contentDefinition); + + var shape = (BooleanFieldSettings)shapeResult.Shape; + + Assert.Equal(settings.Hint, shape.Hint); + Assert.Equal(settings.Label, shape.Label); + Assert.Equal(settings.DefaultValue, shape.DefaultValue); + } + + [Fact] + public async Task DateFieldSettingsShouldDeserialize() + { + var settings = new DateFieldSettings + { + Hint = "Test Hint", + Required = true, + }; + + var contentDefinition = DisplayDriverTestHelper.GetContentPartDefinition(field => field.WithSettings(settings)); + + var shapeResult = await DisplayDriverTestHelper.GetShapeResultAsync(_shapeFactory, contentDefinition); + + var shape = (DateFieldSettings)shapeResult.Shape; + + Assert.Equal(settings.Hint, shape.Hint); + Assert.Equal(settings.Required, shape.Required); + } + + [Fact] + public async Task DateTimeFieldSettingsShouldDeserialize() + { + var settings = new DateTimeFieldSettings + { + Hint = "Test Hint", + Required = true, + }; + + var contentDefinition = DisplayDriverTestHelper.GetContentPartDefinition(field => field.WithSettings(settings)); + + var shapeResult = await DisplayDriverTestHelper.GetShapeResultAsync(_shapeFactory, contentDefinition); + + var shape = (DateTimeFieldSettings)shapeResult.Shape; + + Assert.Equal(settings.Hint, shape.Hint); + Assert.Equal(settings.Required, shape.Required); + } + + [Fact] + public async Task LinkFieldSettingsShouldDeserialize() + { + var settings = new LinkFieldSettings + { + Hint = "Test Hint", + HintLinkText = "Test Hint Link Text", + Required = true, + LinkTextMode = LinkTextMode.Static, + UrlPlaceholder = "Test Url Placeholder", + TextPlaceholder = "Test Text Placeholder", + DefaultUrl = "https://www.orchardcore.net", + DefaultText = "Test Text", + DefaultTarget = "Test Target", + }; + + var contentDefinition = DisplayDriverTestHelper.GetContentPartDefinition(field => field.WithSettings(settings)); + + var shapeResult = await DisplayDriverTestHelper.GetShapeResultAsync(_shapeFactory, contentDefinition); + + var shape = (LinkFieldSettings)shapeResult.Shape; + + Assert.Equal(settings.Hint, shape.Hint); + Assert.Equal(settings.HintLinkText, shape.HintLinkText); + Assert.Equal(settings.Required, shape.Required); + Assert.Equal(settings.LinkTextMode, shape.LinkTextMode); + Assert.Equal(settings.UrlPlaceholder, shape.UrlPlaceholder); + Assert.Equal(settings.TextPlaceholder, shape.TextPlaceholder); + Assert.Equal(settings.DefaultUrl, shape.DefaultUrl); + Assert.Equal(settings.DefaultText, shape.DefaultText); + Assert.Equal(settings.DefaultTarget, shape.DefaultTarget); + } + + [Fact] + public async Task LocalizationSetContentPickerFieldSettingsShouldDeserialize() + { + var settings = new LocalizationSetContentPickerFieldSettings + { + Hint = "Test Hint", + Required = true, + Multiple = true, + DisplayedContentTypes = ["one", "two", "three"], + }; + + var contentDefinition = DisplayDriverTestHelper.GetContentPartDefinition(field => field.WithSettings(settings)); + + var shapeResult = await DisplayDriverTestHelper.GetShapeResultAsync(_shapeFactory, contentDefinition); + + var shape = (LocalizationSetContentPickerFieldSettings)shapeResult.Shape; + + Assert.Equal(settings.Hint, shape.Hint); + Assert.Equal(settings.Required, shape.Required); + Assert.Equal(settings.Multiple, shape.Multiple); + Assert.Equal(settings.DisplayedContentTypes, shape.DisplayedContentTypes); + } + + [Fact] + public async Task NumericFieldSettingsShouldDeserialize() + { + var settings = new NumericFieldSettings + { + Hint = "Test Hint", + Required = true, + Scale = 4, + Minimum = 3, + Maximum = 1000, + Placeholder = "Test Placeholder", + DefaultValue = "Test Default Value", + }; + + var contentDefinition = DisplayDriverTestHelper.GetContentPartDefinition(field => field.WithSettings(settings)); + + var shapeResult = await DisplayDriverTestHelper.GetShapeResultAsync(_shapeFactory, contentDefinition); + + var shape = (NumericFieldSettings)shapeResult.Shape; + + Assert.Equal(settings.Hint, shape.Hint); + Assert.Equal(settings.Required, shape.Required); + Assert.Equal(settings.Scale, shape.Scale); + Assert.Equal(settings.Minimum, shape.Minimum); + Assert.Equal(settings.Maximum, shape.Maximum); + Assert.Equal(settings.Placeholder, shape.Placeholder); + Assert.Equal(settings.DefaultValue, shape.DefaultValue); + } + + [Fact] + public async Task TextFieldSettingsShouldDeserialize() + { + var settings = new TextFieldSettings + { + DefaultValue = "Test Default", + Hint = "Test Hint", + Required = true, + }; + + var contentDefinition = DisplayDriverTestHelper.GetContentPartDefinition(field => field.WithSettings(settings)); + + var shapeResult = await DisplayDriverTestHelper.GetShapeResultAsync(_shapeFactory, contentDefinition); + + var shape = (TextFieldSettings)shapeResult.Shape; + + Assert.Equal(settings.Hint, shape.Hint); + Assert.Equal(settings.DefaultValue, shape.DefaultValue); + Assert.Equal(settings.Required, shape.Required); + } + + [Fact] + public async Task TimeFieldSettingsShouldDeserialize() + { + var settings = new TimeFieldSettings + { + Hint = "Test Hint", + Required = true, + Step = "Test Step", + }; + + var contentDefinition = DisplayDriverTestHelper.GetContentPartDefinition(field => field.WithSettings(settings)); + + var shapeResult = await DisplayDriverTestHelper.GetShapeResultAsync(_shapeFactory, contentDefinition); + + var shape = (TimeFieldSettings)shapeResult.Shape; + + Assert.Equal(settings.Hint, shape.Hint); + Assert.Equal(settings.Required, shape.Required); + Assert.Equal(settings.Step, shape.Step); + } + + [Fact] + public async Task YouTubeFieldSettingsShouldDeserialize() + { + var settings = new YoutubeFieldSettings + { + Hint = "Test Hint", + Label = "Test Label", + Width = 1024, + Height = 768, + Required = true, + }; + + var contentDefinition = DisplayDriverTestHelper.GetContentPartDefinition(field => field.WithSettings(settings)); + + var shapeResult = await DisplayDriverTestHelper.GetShapeResultAsync(_shapeFactory, contentDefinition); + + var shape = (YoutubeFieldSettings)shapeResult.Shape; + + Assert.Equal(settings.Hint, shape.Hint); + Assert.Equal(settings.Label, shape.Label); + Assert.Equal(settings.Width, shape.Width); + Assert.Equal(settings.Height, shape.Height); + Assert.Equal(settings.Required, shape.Required); + } +} diff --git a/test/OrchardCore.Tests/Modules/OrchardCore.Search.Elasticsearch/MockElasticIndexSettingsDocumentManager.cs b/test/OrchardCore.Tests/Modules/OrchardCore.Search.Elasticsearch/MockElasticIndexSettingsDocumentManager.cs new file mode 100644 index 00000000000..5bf3ccbe8d4 --- /dev/null +++ b/test/OrchardCore.Tests/Modules/OrchardCore.Search.Elasticsearch/MockElasticIndexSettingsDocumentManager.cs @@ -0,0 +1,30 @@ +using OrchardCore.Documents; +using OrchardCore.Search.Elasticsearch.Core.Models; + +namespace OrchardCore.Tests.Modules.OrchardCore.Search.Elasticsearch; + +public class MockElasticIndexSettingsDocumentManager : IDocumentManager +{ + public Task GetOrCreateImmutableAsync(Func> factoryAsync = null) + { + return Task.FromResult(new ElasticIndexSettingsDocument + { + ElasticIndexSettings = new Dictionary() + { + { "idx1", new ElasticIndexSettings { IndexName = "idx1" } }, + { "idx2", new ElasticIndexSettings { IndexName = "idx2" } }, + { "testIndex", new ElasticIndexSettings { IndexName = "testIndex" } }, + } + }); + } + + public Task GetOrCreateMutableAsync(Func> factoryAsync = null) + { + throw new NotImplementedException(); + } + + public Task UpdateAsync(ElasticIndexSettingsDocument document, Func afterUpdateAsync = null) + { + throw new NotImplementedException(); + } +} diff --git a/test/OrchardCore.Tests/Modules/OrchardCore.Search.Elasticsearch/SettingsDisplayDriverTests.cs b/test/OrchardCore.Tests/Modules/OrchardCore.Search.Elasticsearch/SettingsDisplayDriverTests.cs new file mode 100644 index 00000000000..1cc71ee4752 --- /dev/null +++ b/test/OrchardCore.Tests/Modules/OrchardCore.Search.Elasticsearch/SettingsDisplayDriverTests.cs @@ -0,0 +1,100 @@ +using OrchardCore.ContentFields.Fields; +using OrchardCore.DisplayManagement; +using OrchardCore.DisplayManagement.Descriptors; +using OrchardCore.DisplayManagement.Implementation; +using OrchardCore.DisplayManagement.Theming; +using OrchardCore.Documents; +using OrchardCore.Environment.Extensions; +using OrchardCore.Environment.Shell; +using OrchardCore.Environment.Shell.Builders; +using OrchardCore.Environment.Shell.Scope; +using OrchardCore.Locking; +using OrchardCore.Locking.Distributed; +using OrchardCore.Recipes.Services; +using OrchardCore.Scripting; +using OrchardCore.Search.Elasticsearch.Core.Models; +using OrchardCore.Search.Elasticsearch.Core.Services; +using OrchardCore.Search.Elasticsearch.Drivers; +using OrchardCore.Tests.Modules.OrchardCore.ContentFields.Settings; +using OrchardCore.Tests.Stubs; + +namespace OrchardCore.Tests.Modules.OrchardCore.Search.Elasticsearch; + +public partial class SettingsDisplayDriverTests +{ + private readonly IServiceProvider _serviceProvider; + private readonly IShapeFactory _shapeFactory; + private readonly ShapeTable _shapeTable; + + public SettingsDisplayDriverTests() + { + var serviceCollection = new ServiceCollection(); + + serviceCollection.AddScripting() + .AddLogging() + .AddScoped() + .AddScoped() + .AddScoped() + .AddScoped() + .AddScoped() + .AddScoped, MockElasticIndexSettingsDocumentManager>() + .AddSingleton(); + + _shapeTable = new ShapeTable + ( + new Dictionary(StringComparer.OrdinalIgnoreCase), + new Dictionary(StringComparer.OrdinalIgnoreCase) + ); + + serviceCollection.AddSingleton(_shapeTable); + + _serviceProvider = serviceCollection.BuildServiceProvider(); + + _shapeFactory = _serviceProvider.GetRequiredService(); + } + + [Fact] + public async Task ContentPickerFieldElasticEditorSettingsShouldDeserialize() + { + + await (await CreateShellContext().CreateScopeAsync()).UsingAsync(async scope => + { + // Arrange + var shellHostMock = new Mock(); + + shellHostMock.Setup(h => h.GetScopeAsync(It.IsAny())) + .Returns(GetScopeAsync); + + var loggerMock = new Mock>(); + var localizerMock = new Mock>(); + + localizerMock.Setup(localizer => localizer[It.IsAny()]) + .Returns((string name) => new LocalizedString(name, name)); + + var settings = new ContentPickerFieldElasticEditorSettings + { + Index = "testIndex", + Indices = ["idx1", "idx2", "testIndex"], + }; + + // Act + var contentDefinition = DisplayDriverTestHelper.GetContentPartDefinition(field => field.WithSettings(settings)); + var elasticService = new ElasticIndexSettingsService(); + var shapeResult = await DisplayDriverTestHelper.GetShapeResultAsync(_shapeFactory, contentDefinition, new ContentPickerFieldElasticEditorSettingsDriver(elasticService)); + var shape = (ContentPickerFieldElasticEditorSettings)shapeResult.Shape; + + // Assert + Assert.Equal(settings.Index, shape.Index); + Assert.Equal(settings.Indices, shape.Indices); + }); + } + + private ShellContext CreateShellContext() => new() + { + Settings = new ShellSettings().AsDefaultShell().AsRunning(), + ServiceProvider = _serviceProvider, + }; + + private static Task GetScopeAsync() + => ShellScope.Context.CreateScopeAsync(); +} diff --git a/test/OrchardCore.Tests/Modules/OrchardCore.Search.Lucene/MockLuceneIndexSettingsDocumentManager.cs b/test/OrchardCore.Tests/Modules/OrchardCore.Search.Lucene/MockLuceneIndexSettingsDocumentManager.cs new file mode 100644 index 00000000000..ccf17bbcbe7 --- /dev/null +++ b/test/OrchardCore.Tests/Modules/OrchardCore.Search.Lucene/MockLuceneIndexSettingsDocumentManager.cs @@ -0,0 +1,30 @@ +using OrchardCore.Documents; +using OrchardCore.Search.Lucene.Model; + +namespace OrchardCore.Tests.Modules.OrchardCore.Search.Lucene; + +public class MockLuceneIndexSettingsDocumentManager : IDocumentManager +{ + public Task GetOrCreateImmutableAsync(Func> factoryAsync = null) + { + return Task.FromResult(new LuceneIndexSettingsDocument + { + LuceneIndexSettings = new Dictionary() + { + { "idx1", new LuceneIndexSettings { IndexName = "idx1" } }, + { "idx2", new LuceneIndexSettings { IndexName = "idx2" } }, + { "testIndex", new LuceneIndexSettings { IndexName = "testIndex" } }, + } + }); + } + + public Task GetOrCreateMutableAsync(Func> factoryAsync = null) + { + throw new NotImplementedException(); + } + + public Task UpdateAsync(LuceneIndexSettingsDocument document, Func afterUpdateAsync = null) + { + throw new NotImplementedException(); + } +} diff --git a/test/OrchardCore.Tests/Modules/OrchardCore.Search.Lucene/SettingsDisplayDriverTests.cs b/test/OrchardCore.Tests/Modules/OrchardCore.Search.Lucene/SettingsDisplayDriverTests.cs new file mode 100644 index 00000000000..07078476074 --- /dev/null +++ b/test/OrchardCore.Tests/Modules/OrchardCore.Search.Lucene/SettingsDisplayDriverTests.cs @@ -0,0 +1,99 @@ +using OrchardCore.ContentFields.Fields; +using OrchardCore.DisplayManagement; +using OrchardCore.DisplayManagement.Descriptors; +using OrchardCore.DisplayManagement.Implementation; +using OrchardCore.DisplayManagement.Theming; +using OrchardCore.Documents; +using OrchardCore.Environment.Extensions; +using OrchardCore.Environment.Shell; +using OrchardCore.Environment.Shell.Builders; +using OrchardCore.Environment.Shell.Scope; +using OrchardCore.Locking; +using OrchardCore.Locking.Distributed; +using OrchardCore.Recipes.Services; +using OrchardCore.Scripting; +using OrchardCore.Search.Lucene; +using OrchardCore.Search.Lucene.Model; +using OrchardCore.Search.Lucene.Settings; +using OrchardCore.Tests.Modules.OrchardCore.ContentFields.Settings; +using OrchardCore.Tests.Stubs; + +namespace OrchardCore.Tests.Modules.OrchardCore.Search.Lucene; + +public partial class SettingsDisplayDriverTests +{ + private readonly IServiceProvider _serviceProvider; + private readonly IShapeFactory _shapeFactory; + private readonly ShapeTable _shapeTable; + + public SettingsDisplayDriverTests() + { + var serviceCollection = new ServiceCollection(); + + serviceCollection.AddScripting() + .AddLogging() + .AddScoped() + .AddScoped() + .AddScoped() + .AddScoped() + .AddScoped() + .AddScoped, MockLuceneIndexSettingsDocumentManager>() + .AddSingleton(); + + _shapeTable = new ShapeTable + ( + new Dictionary(StringComparer.OrdinalIgnoreCase), + new Dictionary(StringComparer.OrdinalIgnoreCase) + ); + + serviceCollection.AddSingleton(_shapeTable); + + _serviceProvider = serviceCollection.BuildServiceProvider(); + + _shapeFactory = _serviceProvider.GetRequiredService(); + } + + [Fact] + public async Task ContentPickerFieldLuceneEditorSettingsShouldDeserialize() + { + await (await CreateShellContext().CreateScopeAsync()).UsingAsync(async scope => + { + // Arrange + var shellHostMock = new Mock(); + + shellHostMock.Setup(h => h.GetScopeAsync(It.IsAny())) + .Returns(GetScopeAsync); + + var loggerMock = new Mock>(); + var localizerMock = new Mock>(); + + localizerMock.Setup(localizer => localizer[It.IsAny()]) + .Returns((string name) => new LocalizedString(name, name)); + + var settings = new ContentPickerFieldLuceneEditorSettings + { + Index = "testIndex", + Indices = ["idx1", "idx2", "testIndex"], + }; + + // Act + var contentDefinition = DisplayDriverTestHelper.GetContentPartDefinition(field => field.WithSettings(settings)); + var luceneService = new LuceneIndexSettingsService(new MockLuceneIndexSettingsDocumentManager()); + var shapeResult = await DisplayDriverTestHelper.GetShapeResultAsync(_shapeFactory, contentDefinition, new ContentPickerFieldLuceneEditorSettingsDriver(luceneService)); + var shape = (ContentPickerFieldLuceneEditorSettings)shapeResult.Shape; + + // Assert + Assert.Equal(settings.Index, shape.Index); + Assert.Equal(settings.Indices, shape.Indices); + }); + } + + private ShellContext CreateShellContext() => new() + { + Settings = new ShellSettings().AsDefaultShell().AsRunning(), + ServiceProvider = _serviceProvider, + }; + + private static Task GetScopeAsync() + => ShellScope.Context.CreateScopeAsync(); +} diff --git a/test/OrchardCore.Tests/Modules/OrchardCore.Spatial/SettingsDisplayDriverTests.cs b/test/OrchardCore.Tests/Modules/OrchardCore.Spatial/SettingsDisplayDriverTests.cs new file mode 100644 index 00000000000..b29f540029a --- /dev/null +++ b/test/OrchardCore.Tests/Modules/OrchardCore.Spatial/SettingsDisplayDriverTests.cs @@ -0,0 +1,61 @@ +using OrchardCore.DisplayManagement; +using OrchardCore.DisplayManagement.Descriptors; +using OrchardCore.DisplayManagement.Implementation; +using OrchardCore.DisplayManagement.Theming; +using OrchardCore.Environment.Extensions; +using OrchardCore.Spatial.Drivers; +using OrchardCore.Spatial.Fields; +using OrchardCore.Spatial.Settings; +using OrchardCore.Tests.Modules.OrchardCore.ContentFields.Settings; +using OrchardCore.Tests.Stubs; + +namespace OrchardCore.Tests.Modules.OrchardCore.Spatial; + +public class SettingsDisplayDriverTests +{ + private readonly IServiceProvider _serviceProvider; + private readonly IShapeFactory _shapeFactory; + private readonly ShapeTable _shapeTable; + + public SettingsDisplayDriverTests() + { + var serviceCollection = new ServiceCollection(); + + serviceCollection.AddScoped() + .AddScoped() + .AddScoped() + .AddScoped() + .AddScoped(); + + _shapeTable = new ShapeTable + ( + new Dictionary(StringComparer.OrdinalIgnoreCase), + new Dictionary(StringComparer.OrdinalIgnoreCase) + ); + + serviceCollection.AddSingleton(_shapeTable); + + _serviceProvider = serviceCollection.BuildServiceProvider(); + + _shapeFactory = _serviceProvider.GetRequiredService(); + } + + [Fact] + public async Task GeoPointFieldSettingsShouldDeserialize() + { + var settings = new GeoPointFieldSettings + { + Hint = "Test Hint", + Required = true, + }; + + var contentDefinition = DisplayDriverTestHelper.GetContentPartDefinition(field => field.WithSettings(settings)); + + var shapeResult = await DisplayDriverTestHelper.GetShapeResultAsync(_shapeFactory, contentDefinition); + + var shape = (GeoPointFieldSettings)shapeResult.Shape; + + Assert.Equal(settings.Hint, shape.Hint); + Assert.Equal(settings.Required, shape.Required); + } +} diff --git a/test/OrchardCore.Tests/Modules/OrchardCore.Taxonomies/SettingsDisplayDriverTests.cs b/test/OrchardCore.Tests/Modules/OrchardCore.Taxonomies/SettingsDisplayDriverTests.cs new file mode 100644 index 00000000000..f64f26be69f --- /dev/null +++ b/test/OrchardCore.Tests/Modules/OrchardCore.Taxonomies/SettingsDisplayDriverTests.cs @@ -0,0 +1,85 @@ +using OrchardCore.DisplayManagement; +using OrchardCore.DisplayManagement.Descriptors; +using OrchardCore.DisplayManagement.Implementation; +using OrchardCore.DisplayManagement.Theming; +using OrchardCore.Environment.Extensions; +using OrchardCore.Taxonomies.Fields; +using OrchardCore.Taxonomies.Settings; +using OrchardCore.Tests.Modules.OrchardCore.ContentFields.Settings; +using OrchardCore.Tests.Stubs; + +namespace OrchardCore.Tests.Modules.OrchardCore.Taxonomies; + +public class SettingsDisplayDriverTests +{ + private readonly IServiceProvider _serviceProvider; + private readonly IShapeFactory _shapeFactory; + private readonly ShapeTable _shapeTable; + + public SettingsDisplayDriverTests() + { + var serviceCollection = new ServiceCollection(); + + serviceCollection.AddScoped() + .AddScoped() + .AddScoped() + .AddScoped() + .AddScoped(); + + _shapeTable = new ShapeTable + ( + new Dictionary(StringComparer.OrdinalIgnoreCase), + new Dictionary(StringComparer.OrdinalIgnoreCase) + ); + + serviceCollection.AddSingleton(_shapeTable); + + _serviceProvider = serviceCollection.BuildServiceProvider(); + + _shapeFactory = _serviceProvider.GetRequiredService(); + } + + [Fact] + public async Task TaxonomyFieldSettingsShouldDeserialize() + { + var settings = new TaxonomyFieldSettings + { + Hint = "Test Hint", + Required = true, + TaxonomyContentItemId = "TestContentId", + Unique = true, + LeavesOnly = true, + Open = true, + }; + + var contentDefinition = DisplayDriverTestHelper.GetContentPartDefinition(field => field.WithSettings(settings)); + + var shapeResult = await DisplayDriverTestHelper.GetShapeResultAsync(_shapeFactory, contentDefinition); + + var shape = (TaxonomyFieldSettings)shapeResult.Shape; + + Assert.Equal(settings.Hint, shape.Hint); + Assert.Equal(settings.Required, shape.Required); + Assert.Equal(settings.TaxonomyContentItemId, shape.TaxonomyContentItemId); + Assert.Equal(settings.Unique, shape.Unique); + Assert.Equal(settings.LeavesOnly, shape.LeavesOnly); + Assert.Equal(settings.Open, shape.Open); + } + + [Fact] + public async Task TaxonomyFieldTagsEditorSettingsShouldDeserialize() + { + var settings = new TaxonomyFieldTagsEditorSettings + { + Open = true, + }; + + var contentDefinition = DisplayDriverTestHelper.GetContentPartDefinition(field => field.WithSettings(settings)); + + var shapeResult = await DisplayDriverTestHelper.GetShapeResultAsync(_shapeFactory, contentDefinition); + + var shape = (TaxonomyFieldTagsEditorSettings)shapeResult.Shape; + + Assert.Equal(settings.Open, shape.Open); + } +}