2012-09-20 14:17:40 +07:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using Umbraco.Core;
|
|
|
|
|
using Umbraco.Core.Dynamics;
|
|
|
|
|
using Umbraco.Core.Models;
|
|
|
|
|
using Umbraco.Tests.TestHelpers;
|
|
|
|
|
using Umbraco.Web;
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Tests.DynamicDocument
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2012-10-02 01:35:39 +05:00
|
|
|
/// Unit tests for IPublishedContent and extensions
|
2012-09-20 14:17:40 +07:00
|
|
|
/// </summary>
|
|
|
|
|
[TestFixture]
|
2012-10-06 23:41:42 +05:00
|
|
|
public class PublishedContentDataTableTests : BaseRoutingTest
|
2012-09-20 14:17:40 +07:00
|
|
|
{
|
|
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
base.Initialize();
|
|
|
|
|
//need to specify a different callback for testing
|
2012-10-02 22:51:53 +05:00
|
|
|
Umbraco.Web.PublishedContentExtensions.GetPropertyAliasesAndNames = s =>
|
2012-09-20 14:17:40 +07:00
|
|
|
{
|
|
|
|
|
var userFields = new Dictionary<string, string>()
|
|
|
|
|
{
|
|
|
|
|
{"property1", "Property 1"},
|
|
|
|
|
{"property2", "Property 2"}
|
|
|
|
|
};
|
|
|
|
|
if (s == "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"},
|
|
|
|
|
{"CreatorName", "CreatorName"},
|
|
|
|
|
{"WriterName", "WriterName"},
|
|
|
|
|
{"Url", "Url"}
|
|
|
|
|
};
|
|
|
|
|
foreach (var f in userFields.Where(f => !allFields.ContainsKey(f.Key)))
|
|
|
|
|
{
|
|
|
|
|
allFields.Add(f.Key, f.Value);
|
|
|
|
|
}
|
|
|
|
|
return allFields;
|
|
|
|
|
};
|
|
|
|
|
var routingContext = GetRoutingContext("/test");
|
|
|
|
|
|
|
|
|
|
//set the UmbracoContext.Current since the extension methods rely on it
|
|
|
|
|
UmbracoContext.Current = routingContext.UmbracoContext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void TearDown()
|
|
|
|
|
{
|
|
|
|
|
base.TearDown();
|
2012-10-02 22:51:53 +05:00
|
|
|
Umbraco.Web.PublishedContentExtensions.GetPropertyAliasesAndNames = null;
|
2012-09-20 14:17:40 +07:00
|
|
|
UmbracoContext.Current = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void To_DataTable()
|
|
|
|
|
{
|
2012-10-06 23:41:42 +05:00
|
|
|
var doc = GetContent(true, 1);
|
2012-09-20 14:17:40 +07:00
|
|
|
var dt = doc.ChildrenAsTable();
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
{
|
2012-10-06 23:41:42 +05:00
|
|
|
var doc = GetContent(true, 1);
|
2012-09-20 14:17:40 +07:00
|
|
|
//change a doc type alias
|
2012-10-02 01:35:39 +05:00
|
|
|
((TestPublishedContent) doc.Children.ElementAt(0)).DocumentTypeAlias = "DontMatch";
|
2012-09-20 14:17:40 +07:00
|
|
|
|
|
|
|
|
var dt = doc.ChildrenAsTable("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()
|
|
|
|
|
{
|
2012-10-06 23:41:42 +05:00
|
|
|
var doc = GetContent(false, 1);
|
2012-09-20 14:17:40 +07:00
|
|
|
var dt = doc.ChildrenAsTable();
|
|
|
|
|
//will return an empty data table
|
|
|
|
|
Assert.AreEqual(0, dt.Columns.Count);
|
|
|
|
|
Assert.AreEqual(0, dt.Rows.Count);
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-06 23:41:42 +05:00
|
|
|
private IPublishedContent GetContent(bool createChildren, int indexVals)
|
2012-09-20 14:17:40 +07:00
|
|
|
{
|
2012-10-02 01:35:39 +05:00
|
|
|
var d = new TestPublishedContent
|
2012-09-20 14:17:40 +07:00
|
|
|
{
|
|
|
|
|
CreateDate = DateTime.Now,
|
|
|
|
|
CreatorId = 1,
|
|
|
|
|
CreatorName = "Shannon",
|
|
|
|
|
DocumentTypeAlias = createChildren? "Parent" : "Child",
|
|
|
|
|
DocumentTypeId = 2,
|
|
|
|
|
Id = 3,
|
|
|
|
|
SortOrder = 4,
|
|
|
|
|
TemplateId = 5,
|
|
|
|
|
UpdateDate = DateTime.Now,
|
|
|
|
|
Path = "-1,3",
|
|
|
|
|
UrlName = "home-page",
|
|
|
|
|
Name = "Page" + Guid.NewGuid().ToString(),
|
|
|
|
|
Version = Guid.NewGuid(),
|
|
|
|
|
WriterId = 1,
|
|
|
|
|
WriterName = "Shannon",
|
|
|
|
|
Parent = null,
|
|
|
|
|
Level = 1,
|
2012-10-04 03:26:56 +05:00
|
|
|
Properties = new Collection<IPublishedContentProperty>(
|
|
|
|
|
new List<IPublishedContentProperty>()
|
2012-09-20 14:17:40 +07:00
|
|
|
{
|
|
|
|
|
new PropertyResult("property1", "value" + indexVals, Guid.NewGuid(), PropertyResultType.UserProperty),
|
|
|
|
|
new PropertyResult("property2", "value" + (indexVals + 1), Guid.NewGuid(), PropertyResultType.UserProperty)
|
|
|
|
|
}),
|
2012-10-02 01:35:39 +05:00
|
|
|
Children = new List<IPublishedContent>()
|
2012-09-20 14:17:40 +07:00
|
|
|
};
|
|
|
|
|
if (createChildren)
|
|
|
|
|
{
|
2012-10-02 01:35:39 +05:00
|
|
|
d.Children = new List<IPublishedContent>()
|
2012-09-20 14:17:40 +07:00
|
|
|
{
|
2012-10-06 23:41:42 +05:00
|
|
|
GetContent(false, indexVals + 3),
|
|
|
|
|
GetContent(false, indexVals + 6),
|
|
|
|
|
GetContent(false, indexVals + 9)
|
2012-09-20 14:17:40 +07:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
if (!createChildren)
|
|
|
|
|
{
|
|
|
|
|
//create additional columns, used to test the different columns for child nodes
|
|
|
|
|
d.Properties.Add(new PropertyResult("property4", "value" + (indexVals + 2), Guid.NewGuid(), PropertyResultType.UserProperty));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
d.Properties.Add(new PropertyResult("property3", "value" + (indexVals + 2), Guid.NewGuid(), PropertyResultType.UserProperty));
|
|
|
|
|
}
|
|
|
|
|
return d;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-10-02 01:35:39 +05:00
|
|
|
private class TestPublishedContent : IPublishedContent
|
2012-09-20 14:17:40 +07:00
|
|
|
{
|
2012-10-02 01:35:39 +05:00
|
|
|
public IPublishedContent Parent { get; set; }
|
2012-09-20 14:17:40 +07:00
|
|
|
public int Id { get; set; }
|
|
|
|
|
public int TemplateId { get; set; }
|
|
|
|
|
public int SortOrder { get; set; }
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
public string UrlName { get; set; }
|
|
|
|
|
public string DocumentTypeAlias { get; set; }
|
|
|
|
|
public int DocumentTypeId { get; set; }
|
|
|
|
|
public string WriterName { get; set; }
|
|
|
|
|
public string CreatorName { get; set; }
|
|
|
|
|
public int WriterId { get; set; }
|
|
|
|
|
public int CreatorId { get; set; }
|
|
|
|
|
public string Path { get; set; }
|
|
|
|
|
public DateTime CreateDate { get; set; }
|
|
|
|
|
public DateTime UpdateDate { get; set; }
|
|
|
|
|
public Guid Version { get; set; }
|
|
|
|
|
public int Level { get; set; }
|
2012-10-04 03:26:56 +05:00
|
|
|
public Collection<IPublishedContentProperty> Properties { get; set; }
|
2012-10-02 01:35:39 +05:00
|
|
|
public IEnumerable<IPublishedContent> Children { get; set; }
|
2012-10-04 03:26:56 +05:00
|
|
|
public IPublishedContentProperty GetProperty(string alias)
|
2012-09-20 14:17:40 +07:00
|
|
|
{
|
|
|
|
|
return Properties.FirstOrDefault(x => x.Alias.InvariantEquals(alias));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|