diff --git a/src/Umbraco.Tests/PublishedContent/PublishedContentDataTableTests.cs b/src/Umbraco.Tests/PublishedContent/PublishedContentDataTableTests.cs index 4e3c17403b..fa60b050cc 100644 --- a/src/Umbraco.Tests/PublishedContent/PublishedContentDataTableTests.cs +++ b/src/Umbraco.Tests/PublishedContent/PublishedContentDataTableTests.cs @@ -1,78 +1,78 @@ -//using System; -//using System.Collections.Generic; -//using System.Collections.ObjectModel; -//using System.Linq; -//using Moq; -//using NUnit.Framework; -//using Umbraco.Core; -//using Umbraco.Core.Composing; -//using Umbraco.Core.Logging; -//using Umbraco.Core.Models; -//using Umbraco.Core.Models.PublishedContent; -//using Umbraco.Core.PropertyEditors; -//using Umbraco.Core.Services; -//using Umbraco.Tests.TestHelpers; -//using Umbraco.Web; -// -//namespace Umbraco.Tests.PublishedContent -//{ -// /// -// /// Unit tests for IPublishedContent and extensions -// /// -// [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() -// { -// {"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() -// { -// {"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 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; -// } -// +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using Moq; +using NUnit.Framework; +using Umbraco.Core; +using Umbraco.Core.Composing; +using Umbraco.Core.Logging; +using Umbraco.Core.Models; +using Umbraco.Core.Models.PublishedContent; +using Umbraco.Core.PropertyEditors; +using Umbraco.Core.Services; +using Umbraco.Tests.TestHelpers; +using Umbraco.Web; + +namespace Umbraco.Tests.PublishedContent +{ + /// + /// Unit tests for IPublishedContent and extensions + /// + [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() + { + {"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() + { + {"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 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() // { @@ -91,7 +91,7 @@ // Assert.AreEqual("value11", dt.Rows[2]["Property 2"]); // Assert.AreEqual("value12", dt.Rows[2]["Property 4"]); // } -// + // [Test] // public void To_DataTable_With_Filter() // { @@ -111,138 +111,138 @@ // 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(Current.Services); -// //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 dataTypeService = new TestObjects.TestDataTypeService( -// new DataType(new VoidEditor(Mock.Of())) { Id = 1 }); -// -// var factory = new PublishedContentTypeFactory(Mock.Of(), new PropertyValueConverterCollection(Array.Empty()), dataTypeService); -// var contentTypeAlias = createChildren ? "Parent" : "Child"; -// var d = new TestPublishedContent -// { -// CreateDate = DateTime.Now, -// CreatorId = 1, -// CreatorName = "Shannon", -// Id = 3, -// SortOrder = 4, -// TemplateId = 5, -// UpdateDate = DateTime.Now, -// Path = "-1,3", -// UrlSegment = "home-page", -// Name = "Page" + Guid.NewGuid().ToString(), -// Version = Guid.NewGuid(), -// WriterId = 1, -// WriterName = "Shannon", -// Parent = null, -// Level = 1, -// Children = new List() -// }; -// d.Properties = new Collection(new List -// { -// 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() -// { -// 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) d.Properties).Add( -// new RawValueProperty(factory.CreatePropertyType("property4",1), d, "value" + (indexVals + 2))); -// } -// else -// { -// ((Collection) d.Properties).Add( -// new RawValueProperty(factory.CreatePropertyType("property3", 1), d, "value" + (indexVals + 2))); -// } -// -// d.ContentType = new PublishedContentType(22, contentTypeAlias, PublishedItemType.Content, Enumerable.Empty(), Enumerable.Empty(), ContentVariation.Nothing); -// return d; -// } -// -// // note - could probably rewrite those tests using SolidPublishedContentCache -// // l8tr... -// private class TestPublishedContent : IPublishedContent -// { -// public string Url { get; set; } -// public string GetUrl(string culture = null) => throw new NotSupportedException(); -// -// public PublishedItemType ItemType { get; set; } -// -// IPublishedContent IPublishedContent.Parent -// { -// get { return Parent; } -// } -// -// IEnumerable IPublishedContent.Children -// { -// get { return Children; } -// } -// -// public IPublishedContent Parent { get; set; } -// public int Id { get; set; } -// public Guid Key { get; set; } -// public int? TemplateId { get; set; } -// public int SortOrder { get; set; } -// public string Name { get; set; } -// public PublishedCultureInfo GetCulture(string culture = null) => throw new NotSupportedException(); -// public IReadOnlyDictionary Cultures => throw new NotSupportedException(); -// public string UrlSegment { 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; } -// public bool IsDraft(string culture = null) => false; -// -// public IEnumerable Properties { get; set; } -// -// public IEnumerable Children { get; set; } -// -// public IPublishedProperty GetProperty(string alias) -// { -// return Properties.FirstOrDefault(x => x.Alias.InvariantEquals(alias)); -// } -// -// public IPublishedProperty GetProperty(string alias, bool recurse) -// { -// var property = GetProperty(alias); -// if (recurse == false) return property; -// -// IPublishedContent content = this; -// while (content != null && (property == null || property.HasValue() == false)) -// { -// content = content.Parent; -// property = content == null ? null : content.GetProperty(alias); -// } -// -// return property; -// } -// -// public PublishedContentType ContentType { get; set; } -// } -// } -//} + + [Test] + public void To_DataTable_No_Rows() + { + var doc = GetContent(false, 1); + var dt = doc.ChildrenAsTable(Current.Services); + //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 dataTypeService = new TestObjects.TestDataTypeService( + new DataType(new VoidEditor(Mock.Of())) { Id = 1 }); + + var factory = new PublishedContentTypeFactory(Mock.Of(), new PropertyValueConverterCollection(Array.Empty()), dataTypeService); + var contentTypeAlias = createChildren ? "Parent" : "Child"; + var d = new TestPublishedContent + { + CreateDate = DateTime.Now, + CreatorId = 1, + CreatorName = "Shannon", + Id = 3, + SortOrder = 4, + TemplateId = 5, + UpdateDate = DateTime.Now, + Path = "-1,3", + UrlSegment = "home-page", + Name = "Page" + Guid.NewGuid().ToString(), + Version = Guid.NewGuid(), + WriterId = 1, + WriterName = "Shannon", + Parent = null, + Level = 1, + Children = new List() + }; + d.Properties = new Collection(new List + { + 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() + { + 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) d.Properties).Add( + new RawValueProperty(factory.CreatePropertyType("property4",1), d, "value" + (indexVals + 2))); + } + else + { + ((Collection) d.Properties).Add( + new RawValueProperty(factory.CreatePropertyType("property3", 1), d, "value" + (indexVals + 2))); + } + + d.ContentType = new PublishedContentType(22, contentTypeAlias, PublishedItemType.Content, Enumerable.Empty(), Enumerable.Empty(), ContentVariation.Nothing); + return d; + } + + // note - could probably rewrite those tests using SolidPublishedContentCache + // l8tr... + private class TestPublishedContent : IPublishedContent + { + public string Url { get; set; } + public string GetUrl(string culture = null) => throw new NotSupportedException(); + + public PublishedItemType ItemType { get; set; } + + IPublishedContent IPublishedContent.Parent + { + get { return Parent; } + } + + IEnumerable IPublishedContent.Children + { + get { return Children; } + } + + public IPublishedContent Parent { get; set; } + public int Id { get; set; } + public Guid Key { get; set; } + public int? TemplateId { get; set; } + public int SortOrder { get; set; } + public string Name { get; set; } + public PublishedCultureInfo GetCulture(string culture = null) => throw new NotSupportedException(); + public IReadOnlyDictionary Cultures => throw new NotSupportedException(); + public string UrlSegment { 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; } + public bool IsDraft(string culture = null) => false; + + public IEnumerable Properties { get; set; } + + public IEnumerable Children { get; set; } + + public IPublishedProperty GetProperty(string alias) + { + return Properties.FirstOrDefault(x => x.Alias.InvariantEquals(alias)); + } + + public IPublishedProperty GetProperty(string alias, bool recurse) + { + var property = GetProperty(alias); + if (recurse == false) return property; + + IPublishedContent content = this; + while (content != null && (property == null || property.HasValue() == false)) + { + content = content.Parent; + property = content == null ? null : content.GetProperty(alias); + } + + return property; + } + + public PublishedContentType ContentType { get; set; } + } + } +} diff --git a/src/Umbraco.Tests/PublishedContent/PublishedContentTests.cs b/src/Umbraco.Tests/PublishedContent/PublishedContentTests.cs index 603464e18b..855d864cc4 100644 --- a/src/Umbraco.Tests/PublishedContent/PublishedContentTests.cs +++ b/src/Umbraco.Tests/PublishedContent/PublishedContentTests.cs @@ -326,18 +326,18 @@ namespace Umbraco.Tests.PublishedContent } } - [Test] - public void Descendants_Ordered_Properly() - { - var doc = GetNode(1046); - - var expected = new[] { 1046, 1173, 1174, 117, 1177, 1178, 1179, 1176, 1175, 4444, 1172 }; - var exindex = 0; - - // must respect the XPath descendants-or-self axis! - foreach (var d in doc.DescendantsOrSelf()) - Assert.AreEqual(expected[exindex++], d.Id); - } +// [Test] +// public void Descendants_Ordered_Properly() +// { +// var doc = GetNode(1046); +// +// var expected = new[] { 1046, 1173, 1174, 117, 1177, 1178, 1179, 1176, 1175, 4444, 1172 }; +// var exindex = 0; +// +// // must respect the XPath descendants-or-self axis! +// foreach (var d in doc.DescendantsOrSelf()) +// Assert.AreEqual(expected[exindex++], d.Id); +// } [Test] public void Get_Property_Value_Recursive() @@ -367,18 +367,18 @@ namespace Umbraco.Tests.PublishedContent Assert.AreEqual("
This is some content
", propVal3.ToString()); } - [Test] - public void Complex_Linq() - { - var doc = GetNode(1173); - - var result = doc.Ancestors().OrderBy(x => x.Level) - .Single() - .Descendants() - .FirstOrDefault(x => x.Value("selectedNodes", defaultValue: "").Split(',').Contains("1173")); - - Assert.IsNotNull(result); - } +// [Test] +// public void Complex_Linq() +// { +// var doc = GetNode(1173); +// +// var result = doc.Ancestors().OrderBy(x => x.Level) +// .Single() +// .Descendants() +// .FirstOrDefault(x => x.Value("selectedNodes", defaultValue: "").Split(',').Contains("1173")); +// +// Assert.IsNotNull(result); +// } [Test] public void Children_GroupBy_DocumentTypeAlias() @@ -437,36 +437,36 @@ namespace Umbraco.Tests.PublishedContent } - [Test] - public void FirstChild() - { - var doc = GetNode(1173); // has child nodes - Assert.IsNotNull(doc.FirstChild()); - Assert.IsNotNull(doc.FirstChild(x => true)); - Assert.IsNotNull(doc.FirstChild()); +// [Test] +// public void FirstChild() +// { +// var doc = GetNode(1173); // has child nodes +// Assert.IsNotNull(doc.FirstChild()); +// Assert.IsNotNull(doc.FirstChild(x => true)); +// Assert.IsNotNull(doc.FirstChild()); +// +// doc = GetNode(1175); // does not have child nodes +// Assert.IsNull(doc.FirstChild()); +// Assert.IsNull(doc.FirstChild(x => true)); +// Assert.IsNull(doc.FirstChild()); +// } - doc = GetNode(1175); // does not have child nodes - Assert.IsNull(doc.FirstChild()); - Assert.IsNull(doc.FirstChild(x => true)); - Assert.IsNull(doc.FirstChild()); - } - - [Test] - public void FirstChildAsT() - { - var doc = GetNode(1046); // has child nodes - - var model = doc.FirstChild(x => true); // predicate - - Assert.IsNotNull(model); - Assert.IsTrue(model.Id == 1173); - Assert.IsInstanceOf(model); - Assert.IsInstanceOf(model); - - doc = GetNode(1175); // does not have child nodes - Assert.IsNull(doc.FirstChild()); - Assert.IsNull(doc.FirstChild(x => true)); - } +// [Test] +// public void FirstChildAsT() +// { +// var doc = GetNode(1046); // has child nodes +// +// var model = doc.FirstChild(x => true); // predicate +// +// Assert.IsNotNull(model); +// Assert.IsTrue(model.Id == 1173); +// Assert.IsInstanceOf(model); +// Assert.IsInstanceOf(model); +// +// doc = GetNode(1175); // does not have child nodes +// Assert.IsNull(doc.FirstChild()); +// Assert.IsNull(doc.FirstChild(x => true)); +// } [Test] public void IsComposedOf() @@ -668,31 +668,31 @@ namespace Umbraco.Tests.PublishedContent } - [Test] - public void Descendants_Or_Self() - { - var doc = GetNode(1046); - - var result = doc.DescendantsOrSelf().ToArray(); - - Assert.IsNotNull(result); - - Assert.AreEqual(10, result.Count()); - Assert.IsTrue(result.Select(x => ((dynamic)x).Id).ContainsAll(new dynamic[] { 1046, 1173, 1174, 1176, 1175 })); - } - - [Test] - public void Descendants() - { - var doc = GetNode(1046); - - var result = doc.Descendants().ToArray(); - - Assert.IsNotNull(result); - - Assert.AreEqual(9, result.Count()); - Assert.IsTrue(result.Select(x => ((dynamic)x).Id).ContainsAll(new dynamic[] { 1173, 1174, 1176, 1175, 4444 })); - } +// [Test] +// public void Descendants_Or_Self() +// { +// var doc = GetNode(1046); +// +// var result = doc.DescendantsOrSelf().ToArray(); +// +// Assert.IsNotNull(result); +// +// Assert.AreEqual(10, result.Count()); +// Assert.IsTrue(result.Select(x => ((dynamic)x).Id).ContainsAll(new dynamic[] { 1046, 1173, 1174, 1176, 1175 })); +// } +// +// [Test] +// public void Descendants() +// { +// var doc = GetNode(1046); +// +// var result = doc.Descendants().ToArray(); +// +// Assert.IsNotNull(result); +// +// Assert.AreEqual(9, result.Count()); +// Assert.IsTrue(result.Select(x => ((dynamic)x).Id).ContainsAll(new dynamic[] { 1173, 1174, 1176, 1175, 4444 })); +// } [Test] public void IsDescendant() diff --git a/src/Umbraco.Tests/PublishedContent/PublishedMediaTests.cs b/src/Umbraco.Tests/PublishedContent/PublishedMediaTests.cs index f88f37a972..7653b9c5f8 100644 --- a/src/Umbraco.Tests/PublishedContent/PublishedMediaTests.cs +++ b/src/Umbraco.Tests/PublishedContent/PublishedMediaTests.cs @@ -1,113 +1,113 @@ -//using System.Web; -//using System.Xml.Linq; -//using System.Xml.XPath; -//using NUnit.Framework; -//using Umbraco.Core; -//using Umbraco.Core.Models; -//using Umbraco.Tests.TestHelpers; -//using Umbraco.Tests.TestHelpers.Entities; -//using Umbraco.Tests.UmbracoExamine; -//using Umbraco.Web; -//using Umbraco.Web.PublishedCache.XmlPublishedCache; -//using System.Linq; -//using System.Threading; -//using System.Xml; -//using Examine; -//using Umbraco.Core.Cache; -//using Umbraco.Core.Models.PublishedContent; -//using Umbraco.Core.Strings; -//using Umbraco.Examine; -//using Current = Umbraco.Web.Composing.Current; -//using Umbraco.Tests.Testing; -//using Umbraco.Core.Composing; -//using Umbraco.Core.Models.Membership; -//using Umbraco.Core.PropertyEditors; -// -//namespace Umbraco.Tests.PublishedContent -//{ -// /// -// /// Tests the typed extension methods on IPublishedContent using the DefaultPublishedMediaStore -// /// -// [TestFixture] -// [Apartment(ApartmentState.STA)] -// [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest, WithApplication = true)] -// public class PublishedMediaTests : PublishedContentTestBase -// { -// /// -// /// sets up resolvers before resolution is frozen -// /// -// protected override void Compose() -// { -// base.Compose(); -// -// Composition.WithCollectionBuilder() -// .Clear() -// .Append(); -// } -// -// private IMediaType MakeNewMediaType(IUser user, string text, int parentId = -1) -// { -// var mt = new MediaType(parentId) { Name = text, Alias = text, Thumbnail = "icon-folder", Icon = "icon-folder" }; -// ServiceContext.MediaTypeService.Save(mt); -// return mt; -// } -// -// private IMedia MakeNewMedia(string name, IMediaType mediaType, IUser user, int parentId) -// { -// var m = ServiceContext.MediaService.CreateMediaWithIdentity(name, parentId, mediaType.Alias); -// return m; -// } -// -// /// -// /// Shared with PublishMediaStoreTests -// /// -// /// -// /// -// /// -// internal IPublishedContent GetNode(int id, UmbracoContext umbracoContext) -// { -// var cache = new PublishedMediaCache(new XmlStore((XmlDocument)null, null, null, null), Current.Services.MediaService, Current.Services.UserService, new StaticCacheProvider(), ContentTypesCache); -// var doc = cache.GetById(id); -// Assert.IsNotNull(doc); -// return doc; -// } -// -// private IPublishedContent GetNode(int id) -// { -// return GetNode(id, GetUmbracoContext("/test")); -// } -// -// [Test] -// public void Get_Property_Value_Uses_Converter() -// { -// var mType = MockedContentTypes.CreateImageMediaType("image2"); -// //lets add an RTE to this -// mType.PropertyGroups.First().PropertyTypes.Add( -// new PropertyType("test", ValueStorageType.Nvarchar, "content") -// { -// Name = "Rich Text", -// DataTypeId = -87 //tiny mce -// }); -// ServiceContext.MediaTypeService.Save(mType); -// var media = MockedMedia.CreateMediaImage(mType, -1); -// media.Properties["content"].SetValue("
This is some content
"); -// ServiceContext.MediaService.Save(media); -// -// var publishedMedia = GetNode(media.Id); -// -// var propVal = publishedMedia.Value("content"); -// Assert.IsInstanceOf(propVal); -// Assert.AreEqual("
This is some content
", propVal.ToString()); -// -// var propVal2 = publishedMedia.Value("content"); -// Assert.IsInstanceOf(propVal2); -// Assert.AreEqual("
This is some content
", propVal2.ToString()); -// -// var propVal3 = publishedMedia.Value("Content"); -// Assert.IsInstanceOf(propVal3); -// Assert.AreEqual("
This is some content
", propVal3.ToString()); -// } -// +using System.Web; +using System.Xml.Linq; +using System.Xml.XPath; +using NUnit.Framework; +using Umbraco.Core; +using Umbraco.Core.Models; +using Umbraco.Tests.TestHelpers; +using Umbraco.Tests.TestHelpers.Entities; +using Umbraco.Tests.UmbracoExamine; +using Umbraco.Web; +using Umbraco.Web.PublishedCache.XmlPublishedCache; +using System.Linq; +using System.Threading; +using System.Xml; +using Examine; +using Umbraco.Core.Cache; +using Umbraco.Core.Models.PublishedContent; +using Umbraco.Core.Strings; +using Umbraco.Examine; +using Current = Umbraco.Web.Composing.Current; +using Umbraco.Tests.Testing; +using Umbraco.Core.Composing; +using Umbraco.Core.Models.Membership; +using Umbraco.Core.PropertyEditors; + +namespace Umbraco.Tests.PublishedContent +{ + /// + /// Tests the typed extension methods on IPublishedContent using the DefaultPublishedMediaStore + /// + [TestFixture] + [Apartment(ApartmentState.STA)] + [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest, WithApplication = true)] + public class PublishedMediaTests : PublishedContentTestBase + { + /// + /// sets up resolvers before resolution is frozen + /// + protected override void Compose() + { + base.Compose(); + + Composition.WithCollectionBuilder() + .Clear() + .Append(); + } + + private IMediaType MakeNewMediaType(IUser user, string text, int parentId = -1) + { + var mt = new MediaType(parentId) { Name = text, Alias = text, Thumbnail = "icon-folder", Icon = "icon-folder" }; + ServiceContext.MediaTypeService.Save(mt); + return mt; + } + + private IMedia MakeNewMedia(string name, IMediaType mediaType, IUser user, int parentId) + { + var m = ServiceContext.MediaService.CreateMediaWithIdentity(name, parentId, mediaType.Alias); + return m; + } + + /// + /// Shared with PublishMediaStoreTests + /// + /// + /// + /// + internal IPublishedContent GetNode(int id, UmbracoContext umbracoContext) + { + var cache = new PublishedMediaCache(new XmlStore((XmlDocument)null, null, null, null), Current.Services.MediaService, Current.Services.UserService, new StaticCacheProvider(), ContentTypesCache); + var doc = cache.GetById(id); + Assert.IsNotNull(doc); + return doc; + } + + private IPublishedContent GetNode(int id) + { + return GetNode(id, GetUmbracoContext("/test")); + } + + [Test] + public void Get_Property_Value_Uses_Converter() + { + var mType = MockedContentTypes.CreateImageMediaType("image2"); + //lets add an RTE to this + mType.PropertyGroups.First().PropertyTypes.Add( + new PropertyType("test", ValueStorageType.Nvarchar, "content") + { + Name = "Rich Text", + DataTypeId = -87 //tiny mce + }); + ServiceContext.MediaTypeService.Save(mType); + var media = MockedMedia.CreateMediaImage(mType, -1); + media.Properties["content"].SetValue("
This is some content
"); + ServiceContext.MediaService.Save(media); + + var publishedMedia = GetNode(media.Id); + + var propVal = publishedMedia.Value("content"); + Assert.IsInstanceOf(propVal); + Assert.AreEqual("
This is some content
", propVal.ToString()); + + var propVal2 = publishedMedia.Value("content"); + Assert.IsInstanceOf(propVal2); + Assert.AreEqual("
This is some content
", propVal2.ToString()); + + var propVal3 = publishedMedia.Value("Content"); + Assert.IsInstanceOf(propVal3); + Assert.AreEqual("
This is some content
", propVal3.ToString()); + } + // [Test] // public void Ensure_Children_Sorted_With_Examine() // { @@ -136,55 +136,55 @@ // } // } // } -// -// [Test] -// public void Do_Not_Find_In_Recycle_Bin() -// { -// var rebuilder = IndexInitializer.GetMediaIndexRebuilder(Factory.GetInstance(), IndexInitializer.GetMockMediaService()); -// -// using (var luceneDir = new RandomIdRamDirectory()) -// using (var indexer = IndexInitializer.GetUmbracoIndexer(ProfilingLogger, luceneDir, -// //include unpublished content since this uses the 'internal' indexer, it's up to the media cache to filter -// validator: new ContentValueSetValidator(false))) -// using (indexer.ProcessNonAsync()) -// { -// rebuilder.RegisterIndex(indexer.Name); -// rebuilder.Populate(indexer); -// -// var searcher = indexer.GetSearcher(); -// var ctx = GetUmbracoContext("/test"); -// var cache = new PublishedMediaCache(ServiceContext.MediaService, ServiceContext.UserService, searcher, indexer, new StaticCacheProvider(), ContentTypesCache); -// -// //ensure it is found -// var publishedMedia = cache.GetById(3113); -// Assert.IsNotNull(publishedMedia); -// -// //move item to recycle bin -// var newXml = XElement.Parse(@" -// -// 115 -// 268 -// 10726 -// jpg -// "); -// indexer.IndexItems(new[]{ newXml.ConvertToValueSet("media") }); -// -// -// //ensure it still exists in the index (raw examine search) -// var criteria = searcher.CreateQuery(); -// var filter = criteria.Id(3113); -// var found = filter.Execute(); -// Assert.IsNotNull(found); -// Assert.AreEqual(1, found.TotalItemCount); -// -// //ensure it does not show up in the published media store -// var recycledMedia = cache.GetById(3113); -// Assert.IsNull(recycledMedia); -// -// } -// -// } -// + + [Test] + public void Do_Not_Find_In_Recycle_Bin() + { + var rebuilder = IndexInitializer.GetMediaIndexRebuilder(Factory.GetInstance(), IndexInitializer.GetMockMediaService()); + + using (var luceneDir = new RandomIdRamDirectory()) + using (var indexer = IndexInitializer.GetUmbracoIndexer(ProfilingLogger, luceneDir, + //include unpublished content since this uses the 'internal' indexer, it's up to the media cache to filter + validator: new ContentValueSetValidator(false))) + using (indexer.ProcessNonAsync()) + { + rebuilder.RegisterIndex(indexer.Name); + rebuilder.Populate(indexer); + + var searcher = indexer.GetSearcher(); + var ctx = GetUmbracoContext("/test"); + var cache = new PublishedMediaCache(ServiceContext.MediaService, ServiceContext.UserService, searcher, indexer, new StaticCacheProvider(), ContentTypesCache); + + //ensure it is found + var publishedMedia = cache.GetById(3113); + Assert.IsNotNull(publishedMedia); + + //move item to recycle bin + var newXml = XElement.Parse(@" + + 115 + 268 + 10726 + jpg + "); + indexer.IndexItems(new[]{ newXml.ConvertToValueSet("media") }); + + + //ensure it still exists in the index (raw examine search) + var criteria = searcher.CreateQuery(); + var filter = criteria.Id(3113); + var found = filter.Execute(); + Assert.IsNotNull(found); + Assert.AreEqual(1, found.TotalItemCount); + + //ensure it does not show up in the published media store + var recycledMedia = cache.GetById(3113); + Assert.IsNull(recycledMedia); + + } + + } + // [Test] // public void Children_With_Examine() // { @@ -212,7 +212,7 @@ // Assert.IsTrue(subChildren.Select(x => x.Id).ContainsAll(new[] { 2112 })); // } // } -// + // [Test] // public void Descendants_With_Examine() // { @@ -240,7 +240,7 @@ // Assert.IsTrue(subDescendants.Select(x => x.Id).ContainsAll(new[] { 2112, 3113 })); // } // } -// + // [Test] // public void DescendantsOrSelf_With_Examine() // { @@ -268,58 +268,58 @@ // Assert.IsTrue(subDescendants.Select(x => x.Id).ContainsAll(new[] { 2222, 2112, 3113 })); // } // } -// -// [Test] -// public void Ancestors_With_Examine() -// { -// var rebuilder = IndexInitializer.GetMediaIndexRebuilder(Factory.GetInstance(), IndexInitializer.GetMockMediaService()); -// -// -// using (var luceneDir = new RandomIdRamDirectory()) -// using (var indexer = IndexInitializer.GetUmbracoIndexer(ProfilingLogger, luceneDir, -// validator: new ContentValueSetValidator(true))) -// using (indexer.ProcessNonAsync()) -// { -// rebuilder.RegisterIndex(indexer.Name); -// rebuilder.Populate(indexer); -// -// var ctx = GetUmbracoContext("/test"); -// var searcher = indexer.GetSearcher(); -// var cache = new PublishedMediaCache(ServiceContext.MediaService, ServiceContext.UserService, searcher, indexer, new StaticCacheProvider(), ContentTypesCache); -// -// //we are using the media.xml media to test the examine results implementation, see the media.xml file in the ExamineHelpers namespace -// var publishedMedia = cache.GetById(3113); -// var ancestors = publishedMedia.Ancestors(); -// Assert.IsTrue(ancestors.Select(x => x.Id).ContainsAll(new[] { 2112, 2222, 1111 })); -// } -// -// } -// -// [Test] -// public void AncestorsOrSelf_With_Examine() -// { -// var rebuilder = IndexInitializer.GetMediaIndexRebuilder(Factory.GetInstance(), IndexInitializer.GetMockMediaService()); -// -// using (var luceneDir = new RandomIdRamDirectory()) -// using (var indexer = IndexInitializer.GetUmbracoIndexer(ProfilingLogger, luceneDir, -// validator: new ContentValueSetValidator(true))) -// using (indexer.ProcessNonAsync()) -// { -// rebuilder.RegisterIndex(indexer.Name); -// rebuilder.Populate(indexer); -// -// -// var ctx = GetUmbracoContext("/test"); -// var searcher = indexer.GetSearcher(); -// var cache = new PublishedMediaCache(ServiceContext.MediaService, ServiceContext.UserService, searcher, indexer, new StaticCacheProvider(), ContentTypesCache); -// -// //we are using the media.xml media to test the examine results implementation, see the media.xml file in the ExamineHelpers namespace -// var publishedMedia = cache.GetById(3113); -// var ancestors = publishedMedia.AncestorsOrSelf(); -// Assert.IsTrue(ancestors.Select(x => x.Id).ContainsAll(new[] { 3113, 2112, 2222, 1111 })); -// } -// } -// + + [Test] + public void Ancestors_With_Examine() + { + var rebuilder = IndexInitializer.GetMediaIndexRebuilder(Factory.GetInstance(), IndexInitializer.GetMockMediaService()); + + + using (var luceneDir = new RandomIdRamDirectory()) + using (var indexer = IndexInitializer.GetUmbracoIndexer(ProfilingLogger, luceneDir, + validator: new ContentValueSetValidator(true))) + using (indexer.ProcessNonAsync()) + { + rebuilder.RegisterIndex(indexer.Name); + rebuilder.Populate(indexer); + + var ctx = GetUmbracoContext("/test"); + var searcher = indexer.GetSearcher(); + var cache = new PublishedMediaCache(ServiceContext.MediaService, ServiceContext.UserService, searcher, indexer, new StaticCacheProvider(), ContentTypesCache); + + //we are using the media.xml media to test the examine results implementation, see the media.xml file in the ExamineHelpers namespace + var publishedMedia = cache.GetById(3113); + var ancestors = publishedMedia.Ancestors(); + Assert.IsTrue(ancestors.Select(x => x.Id).ContainsAll(new[] { 2112, 2222, 1111 })); + } + + } + + [Test] + public void AncestorsOrSelf_With_Examine() + { + var rebuilder = IndexInitializer.GetMediaIndexRebuilder(Factory.GetInstance(), IndexInitializer.GetMockMediaService()); + + using (var luceneDir = new RandomIdRamDirectory()) + using (var indexer = IndexInitializer.GetUmbracoIndexer(ProfilingLogger, luceneDir, + validator: new ContentValueSetValidator(true))) + using (indexer.ProcessNonAsync()) + { + rebuilder.RegisterIndex(indexer.Name); + rebuilder.Populate(indexer); + + + var ctx = GetUmbracoContext("/test"); + var searcher = indexer.GetSearcher(); + var cache = new PublishedMediaCache(ServiceContext.MediaService, ServiceContext.UserService, searcher, indexer, new StaticCacheProvider(), ContentTypesCache); + + //we are using the media.xml media to test the examine results implementation, see the media.xml file in the ExamineHelpers namespace + var publishedMedia = cache.GetById(3113); + var ancestors = publishedMedia.AncestorsOrSelf(); + Assert.IsTrue(ancestors.Select(x => x.Id).ContainsAll(new[] { 3113, 2112, 2222, 1111 })); + } + } + // [Test] // public void Children_Without_Examine() // { @@ -343,7 +343,7 @@ // var subChildren = publishedChild1.Children(); // Assert.IsTrue(subChildren.Select(x => x.Id).ContainsAll(new[] { mSubChild1.Id, mSubChild2.Id, mSubChild3.Id })); // } -// + // [Test] // public void Descendants_Without_Examine() // { @@ -367,7 +367,7 @@ // var subDescendants = publishedChild1.Descendants(); // Assert.IsTrue(subDescendants.Select(x => x.Id).ContainsAll(new[] { mSubChild1.Id, mSubChild2.Id, mSubChild3.Id })); // } -// + // [Test] // public void DescendantsOrSelf_Without_Examine() // { @@ -393,116 +393,116 @@ // Assert.IsTrue(subDescendantsOrSelf.Select(x => x.Id).ContainsAll( // new[] { mChild1.Id, mSubChild1.Id, mSubChild2.Id, mSubChild3.Id })); // } -// -// [Test] -// public void Parent_Without_Examine() -// { -// var user = ServiceContext.UserService.GetUserById(0); -// var mType = MakeNewMediaType(user, "TestMediaType"); -// var mRoot = MakeNewMedia("MediaRoot", mType, user, -1); -// -// var mChild1 = MakeNewMedia("Child1", mType, user, mRoot.Id); -// var mChild2 = MakeNewMedia("Child2", mType, user, mRoot.Id); -// var mChild3 = MakeNewMedia("Child3", mType, user, mRoot.Id); -// -// var mSubChild1 = MakeNewMedia("SubChild1", mType, user, mChild1.Id); -// var mSubChild2 = MakeNewMedia("SubChild2", mType, user, mChild1.Id); -// var mSubChild3 = MakeNewMedia("SubChild3", mType, user, mChild1.Id); -// -// var publishedRoot = GetNode(mRoot.Id); -// Assert.AreEqual(null, publishedRoot.Parent); -// -// var publishedChild1 = GetNode(mChild1.Id); -// Assert.AreEqual(mRoot.Id, publishedChild1.Parent.Id); -// -// var publishedSubChild1 = GetNode(mSubChild1.Id); -// Assert.AreEqual(mChild1.Id, publishedSubChild1.Parent.Id); -// } -// -// [Test] -// public void Ancestors_Without_Examine() -// { -// var user = ServiceContext.UserService.GetUserById(0); -// var mType = MakeNewMediaType(user, "TestMediaType"); -// var mRoot = MakeNewMedia("MediaRoot", mType, user, -1); -// -// var mChild1 = MakeNewMedia("Child1", mType, user, mRoot.Id); -// var mChild2 = MakeNewMedia("Child2", mType, user, mRoot.Id); -// var mChild3 = MakeNewMedia("Child3", mType, user, mRoot.Id); -// -// var mSubChild1 = MakeNewMedia("SubChild1", mType, user, mChild1.Id); -// var mSubChild2 = MakeNewMedia("SubChild2", mType, user, mChild1.Id); -// var mSubChild3 = MakeNewMedia("SubChild3", mType, user, mChild1.Id); -// -// var publishedSubChild1 = GetNode(mSubChild1.Id); -// Assert.IsTrue(publishedSubChild1.Ancestors().Select(x => x.Id).ContainsAll(new[] { mChild1.Id, mRoot.Id })); -// } -// -// [Test] -// public void AncestorsOrSelf_Without_Examine() -// { -// var user = ServiceContext.UserService.GetUserById(0); -// var mType = MakeNewMediaType(user, "TestMediaType"); -// var mRoot = MakeNewMedia("MediaRoot", mType, user, -1); -// -// var mChild1 = MakeNewMedia("Child1", mType, user, mRoot.Id); -// var mChild2 = MakeNewMedia("Child2", mType, user, mRoot.Id); -// var mChild3 = MakeNewMedia("Child3", mType, user, mRoot.Id); -// -// var mSubChild1 = MakeNewMedia("SubChild1", mType, user, mChild1.Id); -// var mSubChild2 = MakeNewMedia("SubChild2", mType, user, mChild1.Id); -// var mSubChild3 = MakeNewMedia("SubChild3", mType, user, mChild1.Id); -// -// var publishedSubChild1 = GetNode(mSubChild1.Id); -// Assert.IsTrue(publishedSubChild1.AncestorsOrSelf().Select(x => x.Id).ContainsAll( -// new[] { mSubChild1.Id, mChild1.Id, mRoot.Id })); -// } -// -// [Test] -// public void Convert_From_Standard_Xml() -// { -// var nodeId = 2112; -// -// var xml = XElement.Parse(@" -// -// 115 -// 268 -// 10726 -// jpg -// -// -// 115 -// 268 -// 10726 -// jpg -// -// "); -// var node = xml.DescendantsAndSelf("Image").Single(x => (int)x.Attribute("id") == nodeId); -// -// var publishedMedia = new PublishedMediaCache(new XmlStore((XmlDocument)null, null, null, null), ServiceContext.MediaService, ServiceContext.UserService, new StaticCacheProvider(), ContentTypesCache); -// -// var nav = node.CreateNavigator(); -// -// var converted = publishedMedia.CreateFromCacheValues( -// publishedMedia.ConvertFromXPathNodeIterator(nav.Select("/Image"), nodeId)); -// -// Assert.AreEqual(nodeId, converted.Id); -// Assert.AreEqual(3, converted.Level); -// Assert.AreEqual(1, converted.SortOrder); -// Assert.AreEqual("Sam's Umbraco Image", converted.Name); -// Assert.AreEqual("-1,1111,2222,2112", converted.Path); -// } -// -// [Test] -// public void Detects_Error_In_Xml() -// { -// var errorXml = new XElement("error", string.Format("No media is maching '{0}'", 1234)); -// var nav = errorXml.CreateNavigator(); -// -// var publishedMedia = new PublishedMediaCache(new XmlStore((XmlDocument)null, null, null, null), ServiceContext.MediaService, ServiceContext.UserService, new StaticCacheProvider(), ContentTypesCache); -// var converted = publishedMedia.ConvertFromXPathNodeIterator(nav.Select("/"), 1234); -// -// Assert.IsNull(converted); -// } -// } -//} + + [Test] + public void Parent_Without_Examine() + { + var user = ServiceContext.UserService.GetUserById(0); + var mType = MakeNewMediaType(user, "TestMediaType"); + var mRoot = MakeNewMedia("MediaRoot", mType, user, -1); + + var mChild1 = MakeNewMedia("Child1", mType, user, mRoot.Id); + var mChild2 = MakeNewMedia("Child2", mType, user, mRoot.Id); + var mChild3 = MakeNewMedia("Child3", mType, user, mRoot.Id); + + var mSubChild1 = MakeNewMedia("SubChild1", mType, user, mChild1.Id); + var mSubChild2 = MakeNewMedia("SubChild2", mType, user, mChild1.Id); + var mSubChild3 = MakeNewMedia("SubChild3", mType, user, mChild1.Id); + + var publishedRoot = GetNode(mRoot.Id); + Assert.AreEqual(null, publishedRoot.Parent); + + var publishedChild1 = GetNode(mChild1.Id); + Assert.AreEqual(mRoot.Id, publishedChild1.Parent.Id); + + var publishedSubChild1 = GetNode(mSubChild1.Id); + Assert.AreEqual(mChild1.Id, publishedSubChild1.Parent.Id); + } + + [Test] + public void Ancestors_Without_Examine() + { + var user = ServiceContext.UserService.GetUserById(0); + var mType = MakeNewMediaType(user, "TestMediaType"); + var mRoot = MakeNewMedia("MediaRoot", mType, user, -1); + + var mChild1 = MakeNewMedia("Child1", mType, user, mRoot.Id); + var mChild2 = MakeNewMedia("Child2", mType, user, mRoot.Id); + var mChild3 = MakeNewMedia("Child3", mType, user, mRoot.Id); + + var mSubChild1 = MakeNewMedia("SubChild1", mType, user, mChild1.Id); + var mSubChild2 = MakeNewMedia("SubChild2", mType, user, mChild1.Id); + var mSubChild3 = MakeNewMedia("SubChild3", mType, user, mChild1.Id); + + var publishedSubChild1 = GetNode(mSubChild1.Id); + Assert.IsTrue(publishedSubChild1.Ancestors().Select(x => x.Id).ContainsAll(new[] { mChild1.Id, mRoot.Id })); + } + + [Test] + public void AncestorsOrSelf_Without_Examine() + { + var user = ServiceContext.UserService.GetUserById(0); + var mType = MakeNewMediaType(user, "TestMediaType"); + var mRoot = MakeNewMedia("MediaRoot", mType, user, -1); + + var mChild1 = MakeNewMedia("Child1", mType, user, mRoot.Id); + var mChild2 = MakeNewMedia("Child2", mType, user, mRoot.Id); + var mChild3 = MakeNewMedia("Child3", mType, user, mRoot.Id); + + var mSubChild1 = MakeNewMedia("SubChild1", mType, user, mChild1.Id); + var mSubChild2 = MakeNewMedia("SubChild2", mType, user, mChild1.Id); + var mSubChild3 = MakeNewMedia("SubChild3", mType, user, mChild1.Id); + + var publishedSubChild1 = GetNode(mSubChild1.Id); + Assert.IsTrue(publishedSubChild1.AncestorsOrSelf().Select(x => x.Id).ContainsAll( + new[] { mSubChild1.Id, mChild1.Id, mRoot.Id })); + } + + [Test] + public void Convert_From_Standard_Xml() + { + var nodeId = 2112; + + var xml = XElement.Parse(@" + + 115 + 268 + 10726 + jpg + + + 115 + 268 + 10726 + jpg + + "); + var node = xml.DescendantsAndSelf("Image").Single(x => (int)x.Attribute("id") == nodeId); + + var publishedMedia = new PublishedMediaCache(new XmlStore((XmlDocument)null, null, null, null), ServiceContext.MediaService, ServiceContext.UserService, new StaticCacheProvider(), ContentTypesCache); + + var nav = node.CreateNavigator(); + + var converted = publishedMedia.CreateFromCacheValues( + publishedMedia.ConvertFromXPathNodeIterator(nav.Select("/Image"), nodeId)); + + Assert.AreEqual(nodeId, converted.Id); + Assert.AreEqual(3, converted.Level); + Assert.AreEqual(1, converted.SortOrder); + Assert.AreEqual("Sam's Umbraco Image", converted.Name); + Assert.AreEqual("-1,1111,2222,2112", converted.Path); + } + + [Test] + public void Detects_Error_In_Xml() + { + var errorXml = new XElement("error", string.Format("No media is maching '{0}'", 1234)); + var nav = errorXml.CreateNavigator(); + + var publishedMedia = new PublishedMediaCache(new XmlStore((XmlDocument)null, null, null, null), ServiceContext.MediaService, ServiceContext.UserService, new StaticCacheProvider(), ContentTypesCache); + var converted = publishedMedia.ConvertFromXPathNodeIterator(nav.Select("/"), 1234); + + Assert.IsNull(converted); + } + } +}