2020-12-20 08:36:11 +01:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
2016-02-17 10:59:48 +01:00
|
|
|
using System.Linq;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using NUnit.Framework;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Cms.Core.Models.ContentEditing;
|
|
|
|
|
using Umbraco.Extensions;
|
2016-02-17 10:59:48 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common.AngularIntegration;
|
|
|
|
|
|
|
|
|
|
[TestFixture]
|
|
|
|
|
public class ContentModelSerializationTests
|
2016-02-17 10:59:48 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void Content_Display_To_Json()
|
2016-02-17 10:59:48 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
// create 3 tabs with 3 properties each
|
|
|
|
|
var tabs = new List<Tab<ContentPropertyDisplay>>();
|
|
|
|
|
for (var tabIndex = 0; tabIndex < 3; tabIndex++)
|
2018-07-17 11:53:46 +10:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
var props = new List<ContentPropertyDisplay>();
|
|
|
|
|
for (var propertyIndex = 0; propertyIndex < 3; propertyIndex++)
|
2018-07-17 11:53:46 +10:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
props.Add(new ContentPropertyDisplay
|
2018-07-17 11:53:46 +10:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
Alias = "property" + propertyIndex,
|
|
|
|
|
Label = "Property " + propertyIndex,
|
|
|
|
|
Id = propertyIndex,
|
|
|
|
|
Value = "value" + propertyIndex,
|
|
|
|
|
Config = new Dictionary<string, object> { { propertyIndex.ToInvariantString(), "value" } },
|
|
|
|
|
Description = "Description " + propertyIndex,
|
2018-07-17 11:53:46 +10:00
|
|
|
});
|
|
|
|
|
}
|
2016-02-17 10:59:48 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
tabs.Add(new Tab<ContentPropertyDisplay>
|
2018-07-17 11:53:46 +10:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
Alias = "Tab" + tabIndex,
|
|
|
|
|
Label = "Tab" + tabIndex,
|
|
|
|
|
Properties = props,
|
|
|
|
|
});
|
|
|
|
|
}
|
2016-02-17 10:59:48 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var displayModel = new ContentItemDisplay
|
|
|
|
|
{
|
|
|
|
|
Id = 1234,
|
|
|
|
|
Variants = new List<ContentVariantDisplay> { new() { Name = "Test", Tabs = tabs } },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var json = JsonConvert.SerializeObject(displayModel);
|
2016-02-17 10:59:48 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var jObject = JObject.Parse(json);
|
2016-02-17 10:59:48 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.AreEqual("1234", jObject["id"].ToString());
|
|
|
|
|
Assert.AreEqual("Test", jObject["variants"][0]["name"].ToString());
|
2020-12-20 08:36:11 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var jsonTabs = jObject["variants"][0]["tabs"];
|
|
|
|
|
Assert.AreEqual(3, jsonTabs.Count());
|
|
|
|
|
for (var tab = 0; tab < jsonTabs.Count(); tab++)
|
|
|
|
|
{
|
|
|
|
|
Assert.AreEqual("Tab" + tab, jsonTabs[tab]["alias"].ToString());
|
|
|
|
|
Assert.AreEqual("Tab" + tab, jsonTabs[tab]["label"].ToString());
|
|
|
|
|
Assert.AreEqual(3, jsonTabs[tab]["properties"].Count());
|
|
|
|
|
for (var prop = 0; prop < jsonTabs[tab]["properties"].Count(); prop++)
|
2018-07-17 11:53:46 +10:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.AreEqual("property" + prop, jsonTabs[tab]["properties"][prop]["alias"].ToString());
|
|
|
|
|
Assert.AreEqual("Property " + prop, jsonTabs[tab]["properties"][prop]["label"].ToString());
|
|
|
|
|
Assert.AreEqual(prop, jsonTabs[tab]["properties"][prop]["id"].Value<int>());
|
|
|
|
|
Assert.AreEqual("value" + prop, jsonTabs[tab]["properties"][prop]["value"].ToString());
|
|
|
|
|
Assert.AreEqual("{\"" + prop + "\":\"value\"}", jsonTabs[tab]["properties"][prop]["config"].ToString(Formatting.None));
|
|
|
|
|
Assert.AreEqual("Description " + prop, jsonTabs[tab]["properties"][prop]["description"].ToString());
|
2018-07-17 11:53:46 +10:00
|
|
|
}
|
|
|
|
|
}
|
2016-02-17 10:59:48 +01:00
|
|
|
}
|
|
|
|
|
}
|