<commit_before>﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Orchard.DisplayManagement.Descriptors;
using Orchard.DisplayManagement.Extensions;
using Orchard.DisplayManagement.Implementation;
using Orchard.DisplayManagement.Shapes;
using Orchard.Environment.Extensions;
using Orchard.Environment.Extensions.Features;
using Orchard.Environment.Extensions.Loaders;
using Orchard.Environment.Extensions.Manifests;
using Orchard.Environment.Shell;
using Orchard.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Xunit;

namespace Orchard.Tests.DisplayManagement.Decriptors
{
    public class DefaultShapeTableManagerTests : IDisposable
    {
        IServiceProvider _serviceProvider;

        public class StubEventBus : IEventBus
        {
            public Task NotifyAsync(string message, IDictionary<string, object> arguments)
            {
                return null;
            }

            public Task NotifyAsync<TEventHandler>(Expression<Func<TEventHandler, Task>> eventNotifier) where TEventHandler : IEventHandler
            {
                return null;
            }

            public void Subscribe(string message, Func<IServiceProvider, IDictionary<string, object>, Task> action)
            {
            }
        }

        private class TestFeatureInfo : IFeatureInfo
        {
            public string[] Dependencies { get; set; } = new string[0];
            public IExtensionInfo Extension { get; set; }
            public string Id { get; set; }
            public string Name { get; set; }
            public double Priority { get; set; }
            public string Category { get; set; }
            public string Description { get; set; }
            public bool DependencyOn(IFeatureInfo feature)
            {
                return false;
            }
        }

        private class TestModuleExtensionInfo : IExtensionInfo
        {
            public TestModuleExtensionInfo(string name)
            {
                var dic1 = new Dictionary<string, string>()
                {
                    {"name", name},
                    {"desciption", name},
                    {"type", "module"},
                };

                var memConfigSrc1 = new MemoryConfigurationSource { InitialData = dic1 };
                var configurationBuilder = new ConfigurationBuilder();
                configurationBuilder.Add(memConfigSrc1);

                Manifest = new ManifestInfo(configurationBuilder.Build(), "module");

                var features =
                    new List<IFeatureInfo>()
                    {
                        {new FeatureInfo(name, name, 0D, "", "", this, new string[0])}
                    };

                Features = features;
            }

            public IFileInfo ExtensionFileInfo { get; set; }
            public IEnumerable<IFeatureInfo> Features { get; set; }
            public string Id { get; set; }
            public IManifestInfo Manifest { get; set; }
            public string SubPath { get; set; }
            public bool Exists => true;
        }

        private class TestThemeExtensionInfo : IExtensionInfo
        {
            public TestThemeExtensionInfo(string name)
            {
                var dic1 = new Dictionary<string, string>()
                {
                    {"name", name},
                    {"desciption", name},
                    {"type", "theme"},
                };

                var memConfigSrc1 = new MemoryConfigurationSource { InitialData = dic1 };
                var configurationBuilder = new ConfigurationBuilder();
                configurationBuilder.Add(memConfigSrc1);

                Manifest = new ManifestInfo(configurationBuilder.Build(), "theme");

                var features =
                    new List<IFeatureInfo>()
                    {
                        {new FeatureInfo(name, name, 0D, "", "", this, new string[0])}
                    };

                Features = features;

                Id = name;
            }

            public TestThemeExtensionInfo(string name, IFeatureInfo baseTheme)
            {
                var dic1 = new Dictionary<string, string>()
                {
                    {"name", name},
                    {"desciption", name},
                    {"type", "theme"},
                    {"basetheme", baseTheme.Id }
                };

                var memConfigSrc1 = new MemoryConfigurationSource { InitialData = dic1 };
                var configurationBuilder = new ConfigurationBuilder();
                configurationBuilder.Add(memConfigSrc1);

                Manifest = new ManifestInfo(configurationBuilder.Build(), "theme");

                Features =
                    new List<IFeatureInfo>()
                    {
                        {new FeatureInfo(name, name, 0D, "", "", this, new string[] { baseTheme.Id })}
                    };

                Id = name;
            }

            public IFileInfo ExtensionFileInfo { get; set; }
            public IEnumerable<IFeatureInfo> Features { get; set; }
            public string Id { get; set; }
            public IManifestInfo Manifest { get; set; }
            public string SubPath { get; set; }
            public bool Exists => true;
        }

        public DefaultShapeTableManagerTests()
        {
            IServiceCollection serviceCollection = new ServiceCollection();

            serviceCollection.AddLogging();
            serviceCollection.AddMemoryCache();
            serviceCollection.AddScoped<IShellFeaturesManager, TestShellFeaturesManager>();
            serviceCollection.AddScoped<IShapeTableManager, DefaultShapeTableManager>();
            serviceCollection.AddScoped<IEventBus, StubEventBus>();
            serviceCollection.AddSingleton<ITypeFeatureProvider, TypeFeatureProvider>();
            serviceCollection.AddSingleton<IExtensionOrderingStrategy, ThemeExtensionOrderingStrategy>();

            var testFeatureExtensionInfo = new TestModuleExtensionInfo("Testing");
            var theme1FeatureExtensionInfo = new TestThemeExtensionInfo("Theme1");
            var baseThemeFeatureExtensionInfo = new TestThemeExtensionInfo("BaseTheme");
            var derivedThemeFeatureExtensionInfo = new TestThemeExtensionInfo("DerivedTheme", baseThemeFeatureExtensionInfo.Features.First());

            var features = new[] {
                testFeatureExtensionInfo.Features.First(),
                theme1FeatureExtensionInfo.Features.First(),
                baseThemeFeatureExtensionInfo.Features.First(),
                derivedThemeFeatureExtensionInfo.Features.First()
            };

            serviceCollection.AddSingleton<IExtensionManager>(new TestExtensionManager(features));

            TestShapeProvider.FeatureShapes = new Dictionary<IFeatureInfo, IEnumerable<string>> {
                { TestFeature(), new [] {"Hello"} },
                { features[1], new [] {"Theme1Shape"} },
                { features[2], new [] {"DerivedShape", "OverriddenShape"} },
                { features[3], new [] {"BaseShape", "OverriddenShape"} }
            };

            serviceCollection.AddScoped<IShapeTableProvider, TestShapeProvider>();
            serviceCollection.AddScoped<TestShapeProvider>((x => (TestShapeProvider)x.GetService<IShapeTableProvider>()));

            _serviceProvider = serviceCollection.BuildServiceProvider();
        }

        static IFeatureInfo TestFeature()
        {
            return new TestFeatureInfo
            {
                Id = "Testing",
                Dependencies = new string[0],
                Extension = new TestModuleExtensionInfo("Testing")
            };
        }

        public class TestShellFeaturesManager : IShellFeaturesManager
        {
            private readonly IExtensionManager _extensionManager;

            public TestShellFeaturesManager(IExtensionManager extensionManager)
            {
                _extensionManager = extensionManager;
            }

            Task<IEnumerable<IFeatureInfo>> IShellFeaturesManager.GetEnabledFeaturesAsync()
            {
                return Task.FromResult(_extensionManager.GetFeatures());
            }
<commit_msg>Fixes shapes unit tests on last master. (#492)<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Orchard.DisplayManagement.Descriptors;
using Orchard.DisplayManagement.Extensions;
using Orchard.DisplayManagement.Implementation;
using Orchard.Environment.Extensions;
using Orchard.Environment.Extensions.Features;
using Orchard.Environment.Extensions.Loaders;
using Orchard.Environment.Extensions.Manifests;
using Orchard.Environment.Shell;
using Orchard.Events;
using Xunit;

namespace Orchard.Tests.DisplayManagement.Decriptors
{
    public class DefaultShapeTableManagerTests : IDisposable
    {
        IServiceProvider _serviceProvider;

        public class StubEventBus : IEventBus
        {
            public Task NotifyAsync(string message, IDictionary<string, object> arguments)
            {
                return null;
            }

            public Task NotifyAsync<TEventHandler>(Expression<Func<TEventHandler, Task>> eventNotifier) where TEventHandler : IEventHandler
            {
                return null;
            }

            public void Subscribe(string message, Func<IServiceProvider, IDictionary<string, object>, Task> action)
            {
            }
        }

        private class TestFeatureInfo : IFeatureInfo
        {
            public string[] Dependencies { get; set; } = new string[0];
            public IExtensionInfo Extension { get; set; }
            public string Id { get; set; }
            public string Name { get; set; }
            public double Priority { get; set; }
            public string Category { get; set; }
            public string Description { get; set; }
            public bool DependencyOn(IFeatureInfo feature)
            {
                return false;
            }
        }

        private class TestModuleExtensionInfo : IExtensionInfo
        {
            public TestModuleExtensionInfo(string name)
            {
                var dic1 = new Dictionary<string, string>()
                {
                    {"name", name},
                    {"desciption", name},
                    {"type", "module"},
                };

                var memConfigSrc1 = new MemoryConfigurationSource { InitialData = dic1 };
                var configurationBuilder = new ConfigurationBuilder();
                configurationBuilder.Add(memConfigSrc1);

                Manifest = new ManifestInfo(configurationBuilder.Build(), "module");

                var features =
                    new List<IFeatureInfo>()
                    {
                        {new FeatureInfo(name, name, 0D, "", "", this, new string[0])}
                    };

                Features = features;
            }

            public IFileInfo ExtensionFileInfo { get; set; }
            public IEnumerable<IFeatureInfo> Features { get; set; }
            public string Id { get; set; }
            public IManifestInfo Manifest { get; set; }
            public string SubPath { get; set; }
            public bool Exists => true;
        }

        private class TestThemeExtensionInfo : IExtensionInfo
        {
            public TestThemeExtensionInfo(string name)
            {
                var dic1 = new Dictionary<string, string>()
                {
                    {"name", name},
                    {"desciption", name},
                    {"type", "theme"},
                };

                var memConfigSrc1 = new MemoryConfigurationSource { InitialData = dic1 };
                var configurationBuilder = new ConfigurationBuilder();
                configurationBuilder.Add(memConfigSrc1);

                Manifest = new ManifestInfo(configurationBuilder.Build(), "theme");

                var features =
                    new List<IFeatureInfo>()
                    {
                        {new FeatureInfo(name, name, 0D, "", "", this, new string[0])}
                    };

                Features = features;

                Id = name;
            }

            public TestThemeExtensionInfo(string name, IFeatureInfo baseTheme)
            {
                var dic1 = new Dictionary<string, string>()
                {
                    {"name", name},
                    {"desciption", name},
                    {"type", "theme"},
                    {"basetheme", baseTheme.Id }
                };

                var memConfigSrc1 = new MemoryConfigurationSource { InitialData = dic1 };
                var configurationBuilder = new ConfigurationBuilder();
                configurationBuilder.Add(memConfigSrc1);

                Manifest = new ManifestInfo(configurationBuilder.Build(), "theme");

                Features =
                    new List<IFeatureInfo>()
                    {
                        {new FeatureInfo(name, name, 0D, "", "", this, new string[] { baseTheme.Id })}
                    };

                Id = name;
            }

            public IFileInfo ExtensionFileInfo { get; set; }
            public IEnumerable<IFeatureInfo> Features { get; set; }
            public string Id { get; set; }
            public IManifestInfo Manifest { get; set; }
            public string SubPath { get; set; }
            public bool Exists => true;
        }

        public DefaultShapeTableManagerTests()
        {
            IServiceCollection serviceCollection = new ServiceCollection();

            serviceCollection.AddLogging();
            serviceCollection.AddMemoryCache();
            serviceCollection.AddScoped<IShellFeaturesManager, TestShellFeaturesManager>();
            serviceCollection.AddScoped<IShapeTableManager, DefaultShapeTableManager>();
            serviceCollection.AddScoped<IEventBus, StubEventBus>();
            serviceCollection.AddSingleton<ITypeFeatureProvider, TypeFeatureProvider>();
            serviceCollection.AddSingleton<IExtensionOrderingStrategy, ThemeExtensionOrderingStrategy>();

            var testFeatureExtensionInfo = new TestModuleExtensionInfo("Testing");
            var theme1FeatureExtensionInfo = new TestThemeExtensionInfo("Theme1");
            var baseThemeFeatureExtensionInfo = new TestThemeExtensionInfo("BaseTheme");
            var derivedThemeFeatureExtensionInfo = new TestThemeExtensionInfo("DerivedTheme", baseThemeFeatureExtensionInfo.Features.First());

            var features = new[] {
                testFeatureExtensionInfo.Features.First(),
                theme1FeatureExtensionInfo.Features.First(),
                baseThemeFeatureExtensionInfo.Features.First(),
                derivedThemeFeatureExtensionInfo.Features.First()
            };

            serviceCollection.AddSingleton<IExtensionManager>(new TestExtensionManager(features));

            TestShapeProvider.FeatureShapes = new Dictionary<IFeatureInfo, IEnumerable<string>> {
                { TestFeature(), new [] {"Hello"} },
                { features[1], new [] {"Theme1Shape"} },
                { features[2], new [] {"DerivedShape", "OverriddenShape"} },
                { features[3], new [] {"BaseShape", "OverriddenShape"} }
            };

            serviceCollection.AddScoped<IShapeTableProvider, TestShapeProvider>();
            serviceCollection.AddScoped<TestShapeProvider>((x => (TestShapeProvider)x.GetService<IShapeTableProvider>()));

            _serviceProvider = serviceCollection.BuildServiceProvider();

            var typeFeatureProvider = _serviceProvider.GetService<ITypeFeatureProvider>();
            typeFeatureProvider.TryAdd(typeof(TestShapeProvider), new InternalFeatureInfo("Core", new InternalExtensionInfo("Core")));
        }

        static IFeatureInfo TestFeature()
        {
            return new TestFeatureInfo
            {
                Id = "Testing",
                Dependencies = new string[0],
                Extension = new TestModuleExtensionInfo("Testing")
            };
        }

        public class TestShellFeaturesManager : IShellFeaturesManager
        {
            private readonly IExtensionManager _extensionManager;

            public TestShellFeaturesManager(IExtensionManager extensionManager)
            {
                _extensionManager = extensionManager;
            }

            Task<IEnumerable<IFeatureInfo>> IShellFeaturesManager.GetEnabledFeaturesAsync()
            {
                return Task.FromResult(_extensionManager.GetFeatures());
            }

{'fraction_non_alphanumeric': 0.08495658296174607, 'fraction_numerical': 0.003226942032386764, 'mean_word_length': 1.9556095023409052, 'pattern_counts': {'":': 0, '<': 58, '<?xml version=': 0, '>': 64, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4157346', 'n_tokens_mistral': 3954, 'n_tokens_neox': 3724, 'n_words': 861}