Exemplo n.º 1
0
        private (IPublishedContentType, IPublishedContentType) CreateContentTypes()
        {
            Current.Reset();

            var logger   = Mock.Of <ILogger>();
            var profiler = Mock.Of <IProfiler>();
            var proflog  = new ProfilingLogger(logger, profiler);

            PropertyEditorCollection editors = null;
            var editor = new NestedContentPropertyEditor(logger, new Lazy <PropertyEditorCollection>(() => editors));

            editors = new PropertyEditorCollection(new DataEditorCollection(new DataEditor[] { editor }));

            var dataType1 = new DataType(editor)
            {
                Id            = 1,
                Configuration = new NestedContentConfiguration
                {
                    MinItems     = 1,
                    MaxItems     = 1,
                    ContentTypes = new[]
                    {
                        new NestedContentConfiguration.ContentType {
                            Alias = "contentN1"
                        }
                    }
                }
            };

            var dataType2 = new DataType(editor)
            {
                Id            = 2,
                Configuration = new NestedContentConfiguration
                {
                    MinItems     = 1,
                    MaxItems     = 99,
                    ContentTypes = new[]
                    {
                        new NestedContentConfiguration.ContentType {
                            Alias = "contentN1"
                        }
                    }
                }
            };

            var dataType3 = new DataType(new TextboxPropertyEditor(logger))
            {
                Id = 3
            };

            // mocked dataservice returns nested content preValues
            var dataTypeService = new TestObjects.TestDataTypeService(dataType1, dataType2, dataType3);

            var publishedModelFactory = new Mock <IPublishedModelFactory>();

            // mocked model factory returns model type
            var modelTypes = new Dictionary <string, Type>
            {
                { "contentN1", typeof(TestElementModel) }
            };

            publishedModelFactory
            .Setup(x => x.MapModelType(It.IsAny <Type>()))
            .Returns((Type type) => ModelType.Map(type, modelTypes));

            // mocked model factory creates models
            publishedModelFactory
            .Setup(x => x.CreateModel(It.IsAny <IPublishedElement>()))
            .Returns((IPublishedElement element) =>
            {
                if (element.ContentType.Alias.InvariantEquals("contentN1"))
                {
                    return(new TestElementModel(element));
                }
                return(element);
            });

            // mocked model factory creates model lists
            publishedModelFactory
            .Setup(x => x.CreateModelList(It.IsAny <string>()))
            .Returns((string alias) =>
            {
                return(alias == "contentN1"
                        ? (IList) new List <TestElementModel>()
                        : (IList) new List <IPublishedElement>());
            });

            var contentCache      = new Mock <IPublishedContentCache>();
            var publishedSnapshot = new Mock <IPublishedSnapshot>();

            // mocked published snapshot returns a content cache
            publishedSnapshot
            .Setup(x => x.Content)
            .Returns(contentCache.Object);

            var publishedSnapshotAccessor = new Mock <IPublishedSnapshotAccessor>();

            // mocked published snapshot accessor returns a facade
            publishedSnapshotAccessor
            .Setup(x => x.PublishedSnapshot)
            .Returns(publishedSnapshot.Object);

            var converters = new PropertyValueConverterCollection(new IPropertyValueConverter[]
            {
                new NestedContentSingleValueConverter(publishedSnapshotAccessor.Object, publishedModelFactory.Object, proflog),
                new NestedContentManyValueConverter(publishedSnapshotAccessor.Object, publishedModelFactory.Object, proflog),
            });

            var factory = new PublishedContentTypeFactory(publishedModelFactory.Object, converters, dataTypeService);

            IEnumerable <IPublishedPropertyType> CreatePropertyTypes1(IPublishedContentType contentType)
            {
                yield return(factory.CreatePropertyType(contentType, "property1", 1));
            }

            IEnumerable <IPublishedPropertyType> CreatePropertyTypes2(IPublishedContentType contentType)
            {
                yield return(factory.CreatePropertyType(contentType, "property2", 2));
            }

            IEnumerable <IPublishedPropertyType> CreatePropertyTypesN1(IPublishedContentType contentType)
            {
                yield return(factory.CreatePropertyType(contentType, "propertyN1", 3));
            }

            var contentType1  = factory.CreateContentType(1, "content1", CreatePropertyTypes1);
            var contentType2  = factory.CreateContentType(2, "content2", CreatePropertyTypes2);
            var contentTypeN1 = factory.CreateContentType(2, "contentN1", CreatePropertyTypesN1, isElement: true);

            // mocked content cache returns content types
            contentCache
            .Setup(x => x.GetContentType(It.IsAny <string>()))
            .Returns((string alias) =>
            {
                if (alias.InvariantEquals("contentN1"))
                {
                    return(contentTypeN1);
                }
                return(null);
            });

            return(contentType1, contentType2);
        }
Exemplo n.º 2
0
    private (IPublishedContentType, IPublishedContentType) CreateContentTypes()
    {
        var logger              = Mock.Of <ILogger <ProfilingLogger> >();
        var loggerFactory       = NullLoggerFactory.Instance;
        var profiler            = Mock.Of <IProfiler>();
        var proflog             = new ProfilingLogger(logger, profiler);
        var localizationService = Mock.Of <ILocalizationService>();

        PropertyEditorCollection editors = null;
        var editor = new NestedContentPropertyEditor(Mock.Of <IDataValueEditorFactory>(), Mock.Of <IIOHelper>(), Mock.Of <IEditorConfigurationParser>());

        editors = new PropertyEditorCollection(new DataEditorCollection(() => new DataEditor[] { editor }));

        var serializer = new ConfigurationEditorJsonSerializer();

        var dataType1 = new DataType(editor, serializer)
        {
            Id            = 1,
            Configuration = new NestedContentConfiguration
            {
                MinItems     = 1,
                MaxItems     = 1,
                ContentTypes = new[] { new NestedContentConfiguration.ContentType {
                                           Alias = "contentN1"
                                       } },
            },
        };

        var dataType2 = new DataType(editor, serializer)
        {
            Id            = 2,
            Configuration = new NestedContentConfiguration
            {
                MinItems     = 1,
                MaxItems     = 99,
                ContentTypes = new[] { new NestedContentConfiguration.ContentType {
                                           Alias = "contentN1"
                                       } },
            },
        };

        var dataType3 =
            new DataType(
                new TextboxPropertyEditor(Mock.Of <IDataValueEditorFactory>(), Mock.Of <IIOHelper>(), Mock.Of <IEditorConfigurationParser>()), serializer)
        {
            Id = 3
        };

        // mocked dataservice returns nested content preValues
        var dataTypeServiceMock = new Mock <IDataTypeService>();

        dataTypeServiceMock.Setup(x => x.GetAll()).Returns(new[] { dataType1, dataType2, dataType3 });

        var publishedModelFactory = new Mock <IPublishedModelFactory>();

        // mocked model factory returns model type
        var modelTypes = new Dictionary <string, Type> {
            { "contentN1", typeof(TestElementModel) }
        };

        publishedModelFactory
        .Setup(x => x.MapModelType(It.IsAny <Type>()))
        .Returns((Type type) => ModelType.Map(type, modelTypes));

        // mocked model factory creates models
        publishedModelFactory
        .Setup(x => x.CreateModel(It.IsAny <IPublishedElement>()))
        .Returns((IPublishedElement element) =>
        {
            if (element.ContentType.Alias.InvariantEquals("contentN1"))
            {
                return(new TestElementModel(element, Mock.Of <IPublishedValueFallback>()));
            }

            return(element);
        });

        // mocked model factory creates model lists
        publishedModelFactory
        .Setup(x => x.CreateModelList(It.IsAny <string>()))
        .Returns((string alias) =>
                 alias == "contentN1"
                    ? new List <TestElementModel>()
                    : new List <IPublishedElement>());

        var contentCache      = new Mock <IPublishedContentCache>();
        var publishedSnapshot = new Mock <IPublishedSnapshot>();

        // mocked published snapshot returns a content cache
        publishedSnapshot
        .Setup(x => x.Content)
        .Returns(contentCache.Object);

        var publishedSnapshotAccessor = new Mock <IPublishedSnapshotAccessor>();

        // mocked published snapshot accessor returns a facade
        var localPublishedSnapshot = publishedSnapshot.Object;

        publishedSnapshotAccessor
        .Setup(x => x.TryGetPublishedSnapshot(out localPublishedSnapshot))
        .Returns(true);

        var converters = new PropertyValueConverterCollection(() => new IPropertyValueConverter[]
        {
            new NestedContentSingleValueConverter(publishedSnapshotAccessor.Object, publishedModelFactory.Object, proflog),
            new NestedContentManyValueConverter(publishedSnapshotAccessor.Object, publishedModelFactory.Object, proflog),
        });

        var factory =
            new PublishedContentTypeFactory(publishedModelFactory.Object, converters, dataTypeServiceMock.Object);

        IEnumerable <IPublishedPropertyType> CreatePropertyTypes1(IPublishedContentType contentType)
        {
            yield return(factory.CreatePropertyType(contentType, "property1", 1));
        }

        IEnumerable <IPublishedPropertyType> CreatePropertyTypes2(IPublishedContentType contentType)
        {
            yield return(factory.CreatePropertyType(contentType, "property2", 2));
        }

        IEnumerable <IPublishedPropertyType> CreatePropertyTypesN1(IPublishedContentType contentType)
        {
            yield return(factory.CreatePropertyType(contentType, "propertyN1", 3));
        }

        var contentType1  = factory.CreateContentType(Guid.NewGuid(), 1, "content1", CreatePropertyTypes1);
        var contentType2  = factory.CreateContentType(Guid.NewGuid(), 2, "content2", CreatePropertyTypes2);
        var contentTypeN1 =
            factory.CreateContentType(Guid.NewGuid(), 2, "contentN1", CreatePropertyTypesN1, isElement: true);

        // mocked content cache returns content types
        contentCache
        .Setup(x => x.GetContentType(It.IsAny <string>()))
        .Returns((string alias) =>
        {
            if (alias.InvariantEquals("contentN1"))
            {
                return(contentTypeN1);
            }

            return(null);
        });

        return(contentType1, contentType2);
    }