Files
Umbraco-CMS/src/Umbraco.Tests/PublishedContent/PublishedContentDataTableTests.cs
Elitsa Marinovska dd5f400cf3 Netcore: Migration of Model classes from Umbraco.Infrastructure to Core (#9404)
* Migrating more model, mapping and tree classes

* Migrating files from Mapping dir without Newtonsoft dependency

* Migrating files from PublishedContent and Editors dirs without Newtonsoft dependency + some more of the same kind

* Migrating DataType class without the usage of Newtonsoft.Json and making the corresponding changes to all classes affected

* Combining 3 ContentExtensions files into 1

* Refactoring from migrating ContentExtensions

* Migrating more classes

* Migrating ContentRepositoryExtensions - combining it with existing file in Umbraco.Core

* removing Newtonsoft json dependency & migrating file. Adding partial migration of ConfigurationEditor, so PropertyTagsExtensions can be migrated

* Migrating ContentTagsExtensions, and refactoring from changes in PropertyTagsExtensions

* Changes that should be reverted once ConfigurationEditor class is fully migrated

* VS couldn't find Composing, so build was failing. Removing the using solves the problem

* Handling a single case for deserializing a subset of an input

* Small changes and added tests to JsonNetSerializer

Signed-off-by: Bjarke Berg <mail@bergmania.dk>

* Migrated ConfigurationEditor

Signed-off-by: Bjarke Berg <mail@bergmania.dk>

Co-authored-by: Bjarke Berg <mail@bergmania.dk>
2020-11-17 20:27:10 +01:00

186 lines
7.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using NUnit.Framework;
using Umbraco.Web.Composing;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Serialization;
using Umbraco.Core.Services;
using Umbraco.Core.Strings;
using Umbraco.Tests.TestHelpers;
using Umbraco.Tests.Testing;
using Umbraco.Web;
using PublishedContentExtensions = Umbraco.Web.PublishedContentExtensions;
namespace Umbraco.Tests.PublishedContent
{
/// <summary>
/// Unit tests for IPublishedContent and extensions
/// </summary>
[TestFixture]
public class PublishedContentDataTableTests : BaseWebTest
{
public override void SetUp()
{
base.SetUp();
// need to specify a different callback for testing
PublishedContentExtensions.GetPropertyAliasesAndNames = (services, alias) =>
{
var userFields = new Dictionary<string, string>()
{
{"property1", "Property 1"},
{"property2", "Property 2"}
};
if (alias == "Child")
{
userFields.Add("property4", "Property 4");
}
else
{
userFields.Add("property3", "Property 3");
}
//ensure the standard fields are there
var allFields = new Dictionary<string, string>()
{
{"Id", "Id"},
{"NodeName", "NodeName"},
{"NodeTypeAlias", "NodeTypeAlias"},
{"CreateDate", "CreateDate"},
{"UpdateDate", "UpdateDate"},
{"CreatorId", "CreatorId"},
{"WriterId", "WriterId"},
{"Url", "Url"}
};
foreach (var f in userFields.Where(f => !allFields.ContainsKey(f.Key)))
{
allFields.Add(f.Key, f.Value);
}
return allFields;
};
var umbracoContext = GetUmbracoContext("/test");
//set the UmbracoContext.Current since the extension methods rely on it
Umbraco.Web.Composing.Current.UmbracoContextAccessor.UmbracoContext = umbracoContext;
}
public override void TearDown()
{
base.TearDown();
Umbraco.Web.PublishedContentExtensions.GetPropertyAliasesAndNames = null;
}
[Test]
public void To_DataTable()
{
var doc = GetContent(true, 1);
var dt = doc.ChildrenAsTable(ServiceContext);
Assert.AreEqual(11, dt.Columns.Count);
Assert.AreEqual(3, dt.Rows.Count);
Assert.AreEqual("value4", dt.Rows[0]["Property 1"]);
Assert.AreEqual("value5", dt.Rows[0]["Property 2"]);
Assert.AreEqual("value6", dt.Rows[0]["Property 4"]);
Assert.AreEqual("value7", dt.Rows[1]["Property 1"]);
Assert.AreEqual("value8", dt.Rows[1]["Property 2"]);
Assert.AreEqual("value9", dt.Rows[1]["Property 4"]);
Assert.AreEqual("value10", dt.Rows[2]["Property 1"]);
Assert.AreEqual("value11", dt.Rows[2]["Property 2"]);
Assert.AreEqual("value12", dt.Rows[2]["Property 4"]);
}
[Test]
public void To_DataTable_With_Filter()
{
var doc = GetContent(true, 1);
//change a doc type alias
var c = (SolidPublishedContent)doc.Children.ElementAt(0);
c.ContentType = new PublishedContentType(Guid.NewGuid(), 22, "DontMatch", PublishedItemType.Content, Enumerable.Empty<string>(), Enumerable.Empty<PublishedPropertyType>(), ContentVariation.Nothing);
var dt = doc.ChildrenAsTable(ServiceContext, "Child");
Assert.AreEqual(11, dt.Columns.Count);
Assert.AreEqual(2, dt.Rows.Count);
Assert.AreEqual("value7", dt.Rows[0]["Property 1"]);
Assert.AreEqual("value8", dt.Rows[0]["Property 2"]);
Assert.AreEqual("value9", dt.Rows[0]["Property 4"]);
Assert.AreEqual("value10", dt.Rows[1]["Property 1"]);
Assert.AreEqual("value11", dt.Rows[1]["Property 2"]);
Assert.AreEqual("value12", dt.Rows[1]["Property 4"]);
}
[Test]
public void To_DataTable_No_Rows()
{
var doc = GetContent(false, 1);
var dt = doc.ChildrenAsTable(ServiceContext);
//will return an empty data table
Assert.AreEqual(0, dt.Columns.Count);
Assert.AreEqual(0, dt.Rows.Count);
}
private IPublishedContent GetContent(bool createChildren, int indexVals)
{
var serializer = new ConfigurationEditorJsonSerializer();
var dataTypeService = new TestObjects.TestDataTypeService(
new DataType(new VoidEditor(NullLoggerFactory.Instance, Mock.Of<IDataTypeService>(), Mock.Of<ILocalizationService>(), Mock.Of<ILocalizedTextService>(), Mock.Of<IShortStringHelper>()), serializer) { Id = 1 });
var factory = new PublishedContentTypeFactory(Mock.Of<IPublishedModelFactory>(), new PropertyValueConverterCollection(Array.Empty<IPropertyValueConverter>()), dataTypeService);
var contentTypeAlias = createChildren ? "Parent" : "Child";
var contentType = new PublishedContentType(Guid.NewGuid(), 22, contentTypeAlias, PublishedItemType.Content, Enumerable.Empty<string>(), Enumerable.Empty<PublishedPropertyType>(), ContentVariation.Nothing);
var d = new SolidPublishedContent(contentType)
{
CreateDate = DateTime.Now,
CreatorId = 1,
Id = 3,
SortOrder = 4,
TemplateId = 5,
UpdateDate = DateTime.Now,
Path = "-1,3",
UrlSegment = "home-page",
Name = "Page" + Guid.NewGuid(),
Version = Guid.NewGuid(),
WriterId = 1,
Parent = null,
Level = 1,
Children = new List<IPublishedContent>()
};
d.Properties = new Collection<IPublishedProperty>(new List<IPublishedProperty>
{
new RawValueProperty(factory.CreatePropertyType("property1", 1), d, "value" + indexVals),
new RawValueProperty(factory.CreatePropertyType("property2", 1), d, "value" + (indexVals + 1))
});
if (createChildren)
{
d.Children = new List<IPublishedContent>()
{
GetContent(false, indexVals + 3),
GetContent(false, indexVals + 6),
GetContent(false, indexVals + 9)
};
}
if (!createChildren)
{
//create additional columns, used to test the different columns for child nodes
((Collection<IPublishedProperty>) d.Properties).Add(
new RawValueProperty(factory.CreatePropertyType("property4",1), d, "value" + (indexVals + 2)));
}
else
{
((Collection<IPublishedProperty>) d.Properties).Add(
new RawValueProperty(factory.CreatePropertyType("property3", 1), d, "value" + (indexVals + 2)));
}
return d;
}
}
}