Files
Umbraco-CMS/tests/Umbraco.Tests.UnitTests/Umbraco.Core/Published/NestedContentTests.cs

286 lines
12 KiB
C#
Raw Normal View History

// Copyright (c) Umbraco.
// See LICENSE for more details.
using System;
2017-10-17 17:43:15 +02:00
using System.Collections;
2017-09-26 14:57:50 +02:00
using System.Collections.Generic;
using System.Linq;
2020-09-28 08:26:21 +02:00
using Microsoft.Extensions.Logging;
2020-09-18 14:37:19 +02:00
using Microsoft.Extensions.Logging.Abstractions;
2017-09-26 14:57:50 +02:00
using Moq;
using NUnit.Framework;
using Umbraco.Cms.Core.IO;
using Umbraco.Cms.Core.Logging;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.PropertyEditors;
using Umbraco.Cms.Core.PropertyEditors.ValueConverters;
using Umbraco.Cms.Core.PublishedCache;
using Umbraco.Cms.Core.PublishedCache.Internal;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Strings;
using Umbraco.Cms.Infrastructure.Serialization;
using Umbraco.Cms.Tests.Common.TestHelpers;
using Umbraco.Extensions;
2017-09-26 14:57:50 +02:00
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Published
2017-09-26 14:57:50 +02:00
{
[TestFixture]
public class NestedContentTests
2017-09-26 14:57:50 +02:00
{
2019-04-15 13:04:14 +02:00
private (IPublishedContentType, IPublishedContentType) CreateContentTypes()
2017-09-26 14:57:50 +02:00
{
ILogger<ProfilingLogger> logger = Mock.Of<ILogger<ProfilingLogger>>();
NullLoggerFactory loggerFactory = NullLoggerFactory.Instance;
IProfiler profiler = Mock.Of<IProfiler>();
2017-09-26 14:57:50 +02:00
var proflog = new ProfilingLogger(logger, profiler);
ILocalizationService localizationService = Mock.Of<ILocalizationService>();
2017-09-26 14:57:50 +02:00
2018-02-07 13:35:59 +01:00
PropertyEditorCollection editors = null;
var editor = new NestedContentPropertyEditor(Mock.Of<IDataValueEditorFactory>(),Mock.Of<IIOHelper>(), Mock.Of<IEditorConfigurationParser>());
editors = new PropertyEditorCollection(new DataEditorCollection(() => new DataEditor[] { editor }));
2018-02-07 13:35:59 +01:00
var serializer = new ConfigurationEditorJsonSerializer();
var dataType1 = new DataType(editor, serializer)
2018-01-26 17:55:20 +01:00
{
2018-02-07 13:35:59 +01:00
Id = 1,
2018-01-26 17:55:20 +01:00
Configuration = new NestedContentConfiguration
{
MinItems = 1,
MaxItems = 1,
2018-01-26 17:55:20 +01:00
ContentTypes = new[]
{
new NestedContentConfiguration.ContentType { Alias = "contentN1" }
}
}
};
var dataType2 = new DataType(editor, serializer)
2018-01-26 17:55:20 +01:00
{
2018-02-07 13:35:59 +01:00
Id = 2,
2018-01-26 17:55:20 +01:00
Configuration = new NestedContentConfiguration
{
MinItems = 1,
MaxItems = 99,
ContentTypes = new[]
{
new NestedContentConfiguration.ContentType { Alias = "contentN1" }
}
}
};
2017-09-26 14:57:50 +02:00
var dataType3 = new DataType(new TextboxPropertyEditor(Mock.Of<IDataValueEditorFactory>(),Mock.Of<IIOHelper>(), Mock.Of<IEditorConfigurationParser>()), serializer)
{
Id = 3
};
2018-01-26 17:55:20 +01:00
// mocked dataservice returns nested content preValues
var dataTypeServiceMock = new Mock<IDataTypeService>();
dataTypeServiceMock.Setup(x => x.GetAll()).Returns(new[] { dataType1, dataType2, dataType3 });
2017-09-26 14:57:50 +02:00
var publishedModelFactory = new Mock<IPublishedModelFactory>();
// mocked model factory returns model type
var modelTypes = new Dictionary<string, Type>
{
{ "contentN1", typeof(TestElementModel) }
};
2017-09-26 14:57:50 +02:00
publishedModelFactory
.Setup(x => x.MapModelType(It.IsAny<Type>()))
.Returns((Type type) => ModelType.Map(type, modelTypes));
2017-09-26 14:57:50 +02:00
// 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>());
}
2017-09-26 14:57:50 +02:00
return element;
});
2017-10-17 17:43:15 +02:00
// mocked model factory creates model lists
publishedModelFactory
.Setup(x => x.CreateModelList(It.IsAny<string>()))
.Returns((string alias) =>
alias == "contentN1"
? (IList)new List<TestElementModel>()
: (IList)new List<IPublishedElement>());
2017-10-17 17:43:15 +02:00
2017-09-26 14:57:50 +02:00
var contentCache = new Mock<IPublishedContentCache>();
2018-04-27 11:38:50 +10:00
var publishedSnapshot = new Mock<IPublishedSnapshot>();
2017-09-26 14:57:50 +02:00
2017-10-31 12:48:24 +01:00
// mocked published snapshot returns a content cache
publishedSnapshot
2017-10-31 12:50:30 +01:00
.Setup(x => x.Content)
2017-09-26 14:57:50 +02:00
.Returns(contentCache.Object);
2017-10-31 12:48:24 +01:00
var publishedSnapshotAccessor = new Mock<IPublishedSnapshotAccessor>();
2017-09-26 14:57:50 +02:00
2017-10-31 12:48:24 +01:00
// mocked published snapshot accessor returns a facade
var localPublishedSnapshot = publishedSnapshot.Object;
2017-10-31 12:48:24 +01:00
publishedSnapshotAccessor
.Setup(x => x.TryGetPublishedSnapshot(out localPublishedSnapshot))
.Returns(true);
2017-09-26 14:57:50 +02:00
var converters = new PropertyValueConverterCollection(() => new IPropertyValueConverter[]
2017-09-26 14:57:50 +02:00
{
2017-10-31 12:48:24 +01:00
new NestedContentSingleValueConverter(publishedSnapshotAccessor.Object, publishedModelFactory.Object, proflog),
new NestedContentManyValueConverter(publishedSnapshotAccessor.Object, publishedModelFactory.Object, proflog),
2017-09-26 14:57:50 +02:00
});
var factory = new PublishedContentTypeFactory(publishedModelFactory.Object, converters, dataTypeServiceMock.Object);
2017-10-17 17:43:15 +02:00
2019-04-15 17:14:45 +02:00
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);
}
2017-09-26 14:57:50 +02:00
IPublishedContentType contentType1 = factory.CreateContentType(Guid.NewGuid(), 1, "content1", CreatePropertyTypes1);
IPublishedContentType contentType2 = factory.CreateContentType(Guid.NewGuid(), 2, "content2", CreatePropertyTypes2);
IPublishedContentType contentTypeN1 = factory.CreateContentType(Guid.NewGuid(), 2, "contentN1", CreatePropertyTypesN1, isElement: true);
2017-09-26 14:57:50 +02:00
// mocked content cache returns content types
contentCache
.Setup(x => x.GetContentType(It.IsAny<string>()))
.Returns((string alias) =>
{
if (alias.InvariantEquals("contentN1"))
{
return contentTypeN1;
}
2017-09-26 14:57:50 +02:00
return null;
});
return (contentType1, contentType2);
}
[Test]
public void SingleNestedTest()
{
(IPublishedContentType contentType1, _) = CreateContentTypes();
2017-09-26 14:57:50 +02:00
// nested single converter returns the proper value clr type TestModel, and cache level
Assert.AreEqual(typeof(TestElementModel), contentType1.GetPropertyType("property1").ClrType);
2017-10-31 12:48:24 +01:00
Assert.AreEqual(PropertyCacheLevel.Element, contentType1.GetPropertyType("property1").CacheLevel);
2017-09-26 14:57:50 +02:00
var key = Guid.NewGuid();
var keyA = Guid.NewGuid();
var content = new InternalPublishedContent(contentType1)
2017-09-26 14:57:50 +02:00
{
2019-06-07 11:15:58 +02:00
Key = key,
Properties = new[]
2019-06-07 11:15:58 +02:00
{
new TestPublishedProperty(
contentType1.GetPropertyType("property1"), $@"[
2017-09-26 14:57:50 +02:00
{{ ""key"": ""{keyA}"", ""propertyN1"": ""foo"", ""ncContentTypeAlias"": ""contentN1"" }}
]")
2019-06-07 11:15:58 +02:00
}
};
var value = content.Value(Mock.Of<IPublishedValueFallback>(), "property1");
2017-09-26 14:57:50 +02:00
// nested single converter returns proper TestModel value
2017-09-29 15:51:33 +02:00
Assert.IsInstanceOf<TestElementModel>(value);
var valueM = (TestElementModel)value;
2017-09-26 14:57:50 +02:00
Assert.AreEqual("foo", valueM.PropValue);
Assert.AreEqual(keyA, valueM.Key);
}
[Test]
public void ManyNestedTest()
{
(_, IPublishedContentType contentType2) = CreateContentTypes();
2017-09-26 14:57:50 +02:00
// nested many converter returns the proper value clr type IEnumerable<TestModel>, and cache level
Assert.AreEqual(typeof(IEnumerable<TestElementModel>), contentType2.GetPropertyType("property2").ClrType);
2017-10-31 12:48:24 +01:00
Assert.AreEqual(PropertyCacheLevel.Element, contentType2.GetPropertyType("property2").CacheLevel);
2017-09-26 14:57:50 +02:00
var key = Guid.NewGuid();
var keyA = Guid.NewGuid();
var keyB = Guid.NewGuid();
var content = new InternalPublishedContent(contentType2)
2017-09-26 14:57:50 +02:00
{
2019-06-07 11:15:58 +02:00
Key = key,
Properties = new[]
{
new TestPublishedProperty(contentType2.GetPropertyType("property2"), $@"[
2017-09-26 14:57:50 +02:00
{{ ""key"": ""{keyA}"", ""propertyN1"": ""foo"", ""ncContentTypeAlias"": ""contentN1"" }},
{{ ""key"": ""{keyB}"", ""propertyN1"": ""bar"", ""ncContentTypeAlias"": ""contentN1"" }}
]")
2019-06-07 11:15:58 +02:00
}
};
var value = content.Value(Mock.Of<IPublishedValueFallback>(), "property2");
2017-09-26 14:57:50 +02:00
// nested many converter returns proper IEnumerable<TestModel> value
Assert.IsInstanceOf<IEnumerable<IPublishedElement>>(value);
2017-09-29 15:51:33 +02:00
Assert.IsInstanceOf<IEnumerable<TestElementModel>>(value);
var valueM = ((IEnumerable<TestElementModel>)value).ToArray();
2017-09-26 14:57:50 +02:00
Assert.AreEqual("foo", valueM[0].PropValue);
Assert.AreEqual(keyA, valueM[0].Key);
Assert.AreEqual("bar", valueM[1].PropValue);
Assert.AreEqual(keyB, valueM[1].Key);
}
2017-09-29 15:51:33 +02:00
public class TestElementModel : PublishedElementModel
2017-09-26 14:57:50 +02:00
{
public TestElementModel(IPublishedElement content, IPublishedValueFallback fallback)
: base(content, fallback)
{
}
2017-09-26 14:57:50 +02:00
public string PropValue => this.Value<string>(Mock.Of<IPublishedValueFallback>(), "propertyN1");
2017-09-26 14:57:50 +02:00
}
public class TestPublishedProperty : PublishedPropertyBase
2017-09-26 14:57:50 +02:00
{
private readonly bool _preview;
2017-12-06 11:51:35 +01:00
private readonly object _sourceValue;
private readonly bool _hasValue;
2017-09-26 14:57:50 +02:00
private IPublishedElement _owner;
2019-04-15 17:14:45 +02:00
public TestPublishedProperty(IPublishedPropertyType propertyType, object source)
2017-10-31 12:48:24 +01:00
: base(propertyType, PropertyCacheLevel.Element) // initial reference cache level always is .Content
2017-09-26 14:57:50 +02:00
{
2017-12-06 11:51:35 +01:00
_sourceValue = source;
_hasValue = source != null && (!(source is string ssource) || !string.IsNullOrWhiteSpace(ssource));
2017-09-26 14:57:50 +02:00
}
2019-04-15 17:14:45 +02:00
public TestPublishedProperty(IPublishedPropertyType propertyType, IPublishedElement element, bool preview, PropertyCacheLevel referenceCacheLevel, object source)
2017-09-26 14:57:50 +02:00
: base(propertyType, referenceCacheLevel)
{
2017-12-06 11:51:35 +01:00
_sourceValue = source;
_hasValue = source != null && (!(source is string ssource) || !string.IsNullOrWhiteSpace(ssource));
2017-09-26 14:57:50 +02:00
_owner = element;
_preview = preview;
}
2017-12-06 11:51:35 +01:00
private object InterValue => PropertyType.ConvertSourceToInter(null, _sourceValue, false);
2017-09-26 14:57:50 +02:00
internal void SetOwner(IPublishedElement owner) => _owner = owner;
2017-09-26 14:57:50 +02:00
public override bool HasValue(string culture = null, string? segment = null) => _hasValue;
public override object GetSourceValue(string culture = null, string? segment = null) => _sourceValue;
public override object GetValue(string culture = null, string? segment = null) => PropertyType.ConvertInterToObject(_owner, ReferenceCacheLevel, InterValue, _preview);
public override object GetXPathValue(string culture = null, string? segment = null) => throw new InvalidOperationException("This method won't be implemented.");
2017-09-26 14:57:50 +02:00
}
}
}