diff --git a/src/Umbraco.Tests/ContentStores/PublishMediaStoreTests.cs b/src/Umbraco.Tests/ContentStores/PublishMediaStoreTests.cs index 214931138c..0595923589 100644 --- a/src/Umbraco.Tests/ContentStores/PublishMediaStoreTests.cs +++ b/src/Umbraco.Tests/ContentStores/PublishMediaStoreTests.cs @@ -29,6 +29,24 @@ namespace Umbraco.Tests.ContentStores PublishedMediaTests.DoTearDown(); } + [Test] + public void Get_Root_Docs() + { + var user = new User(0); + var mType = global::umbraco.cms.businesslogic.media.MediaType.MakeNew(user, "TestMediaType"); + var mRoot1 = global::umbraco.cms.businesslogic.media.Media.MakeNew("MediaRoot1", mType, user, -1); + var mRoot2 = global::umbraco.cms.businesslogic.media.Media.MakeNew("MediaRoot2", mType, user, -1); + var mChild1 = global::umbraco.cms.businesslogic.media.Media.MakeNew("Child1", mType, user, mRoot1.Id); + var mChild2 = global::umbraco.cms.businesslogic.media.Media.MakeNew("Child2", mType, user, mRoot2.Id); + + var ctx = GetUmbracoContext("/test", 1234); + var mediaStore = new DefaultPublishedMediaStore(); + var roots = mediaStore.GetRootDocuments(ctx); + Assert.AreEqual(2, roots.Count()); + Assert.IsTrue(roots.Select(x => x.Id).ContainsAll(new[] {mRoot1.Id, mRoot2.Id})); + + } + [Test] public void Get_Item_Without_Examine() { diff --git a/src/Umbraco.Tests/PublishedContent/PublishedMediaTests.cs b/src/Umbraco.Tests/PublishedContent/PublishedMediaTests.cs index a5a33db6a2..973655b9c1 100644 --- a/src/Umbraco.Tests/PublishedContent/PublishedMediaTests.cs +++ b/src/Umbraco.Tests/PublishedContent/PublishedMediaTests.cs @@ -118,5 +118,119 @@ namespace Umbraco.Tests.PublishedContent 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() + { + var user = new User(0); + var mType = global::umbraco.cms.businesslogic.media.MediaType.MakeNew(user, "TestMediaType"); + var mRoot = global::umbraco.cms.businesslogic.media.Media.MakeNew("MediaRoot", mType, user, -1); + + var mChild1 = global::umbraco.cms.businesslogic.media.Media.MakeNew("Child1", mType, user, mRoot.Id); + var mChild2 = global::umbraco.cms.businesslogic.media.Media.MakeNew("Child2", mType, user, mRoot.Id); + var mChild3 = global::umbraco.cms.businesslogic.media.Media.MakeNew("Child3", mType, user, mRoot.Id); + + var mSubChild1 = global::umbraco.cms.businesslogic.media.Media.MakeNew("SubChild1", mType, user, mChild1.Id); + var mSubChild2 = global::umbraco.cms.businesslogic.media.Media.MakeNew("SubChild2", mType, user, mChild1.Id); + var mSubChild3 = global::umbraco.cms.businesslogic.media.Media.MakeNew("SubChild3", mType, user, mChild1.Id); + + var publishedMedia = GetNode(mRoot.Id); + var rootDescendants = publishedMedia.Descendants(); + Assert.IsTrue(rootDescendants.Select(x => x.Id).ContainsAll(new[] { mChild1.Id, mChild2.Id, mChild3.Id, mSubChild1.Id, mSubChild2.Id, mSubChild3.Id })); + + var publishedChild1 = GetNode(mChild1.Id); + 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() + { + var user = new User(0); + var mType = global::umbraco.cms.businesslogic.media.MediaType.MakeNew(user, "TestMediaType"); + var mRoot = global::umbraco.cms.businesslogic.media.Media.MakeNew("MediaRoot", mType, user, -1); + + var mChild1 = global::umbraco.cms.businesslogic.media.Media.MakeNew("Child1", mType, user, mRoot.Id); + var mChild2 = global::umbraco.cms.businesslogic.media.Media.MakeNew("Child2", mType, user, mRoot.Id); + var mChild3 = global::umbraco.cms.businesslogic.media.Media.MakeNew("Child3", mType, user, mRoot.Id); + + var mSubChild1 = global::umbraco.cms.businesslogic.media.Media.MakeNew("SubChild1", mType, user, mChild1.Id); + var mSubChild2 = global::umbraco.cms.businesslogic.media.Media.MakeNew("SubChild2", mType, user, mChild1.Id); + var mSubChild3 = global::umbraco.cms.businesslogic.media.Media.MakeNew("SubChild3", mType, user, mChild1.Id); + + var publishedMedia = GetNode(mRoot.Id); + var rootDescendantsOrSelf = publishedMedia.DescendantsOrSelf(); + Assert.IsTrue(rootDescendantsOrSelf.Select(x => x.Id).ContainsAll( + new[] { mRoot.Id, mChild1.Id, mChild2.Id, mChild3.Id, mSubChild1.Id, mSubChild2.Id, mSubChild3.Id })); + + var publishedChild1 = GetNode(mChild1.Id); + var subDescendantsOrSelf = publishedChild1.DescendantsOrSelf(); + 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 = new User(0); + var mType = global::umbraco.cms.businesslogic.media.MediaType.MakeNew(user, "TestMediaType"); + var mRoot = global::umbraco.cms.businesslogic.media.Media.MakeNew("MediaRoot", mType, user, -1); + + var mChild1 = global::umbraco.cms.businesslogic.media.Media.MakeNew("Child1", mType, user, mRoot.Id); + var mChild2 = global::umbraco.cms.businesslogic.media.Media.MakeNew("Child2", mType, user, mRoot.Id); + var mChild3 = global::umbraco.cms.businesslogic.media.Media.MakeNew("Child3", mType, user, mRoot.Id); + + var mSubChild1 = global::umbraco.cms.businesslogic.media.Media.MakeNew("SubChild1", mType, user, mChild1.Id); + var mSubChild2 = global::umbraco.cms.businesslogic.media.Media.MakeNew("SubChild2", mType, user, mChild1.Id); + var mSubChild3 = global::umbraco.cms.businesslogic.media.Media.MakeNew("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 = new User(0); + var mType = global::umbraco.cms.businesslogic.media.MediaType.MakeNew(user, "TestMediaType"); + var mRoot = global::umbraco.cms.businesslogic.media.Media.MakeNew("MediaRoot", mType, user, -1); + + var mChild1 = global::umbraco.cms.businesslogic.media.Media.MakeNew("Child1", mType, user, mRoot.Id); + var mChild2 = global::umbraco.cms.businesslogic.media.Media.MakeNew("Child2", mType, user, mRoot.Id); + var mChild3 = global::umbraco.cms.businesslogic.media.Media.MakeNew("Child3", mType, user, mRoot.Id); + + var mSubChild1 = global::umbraco.cms.businesslogic.media.Media.MakeNew("SubChild1", mType, user, mChild1.Id); + var mSubChild2 = global::umbraco.cms.businesslogic.media.Media.MakeNew("SubChild2", mType, user, mChild1.Id); + var mSubChild3 = global::umbraco.cms.businesslogic.media.Media.MakeNew("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 = new User(0); + var mType = global::umbraco.cms.businesslogic.media.MediaType.MakeNew(user, "TestMediaType"); + var mRoot = global::umbraco.cms.businesslogic.media.Media.MakeNew("MediaRoot", mType, user, -1); + + var mChild1 = global::umbraco.cms.businesslogic.media.Media.MakeNew("Child1", mType, user, mRoot.Id); + var mChild2 = global::umbraco.cms.businesslogic.media.Media.MakeNew("Child2", mType, user, mRoot.Id); + var mChild3 = global::umbraco.cms.businesslogic.media.Media.MakeNew("Child3", mType, user, mRoot.Id); + + var mSubChild1 = global::umbraco.cms.businesslogic.media.Media.MakeNew("SubChild1", mType, user, mChild1.Id); + var mSubChild2 = global::umbraco.cms.businesslogic.media.Media.MakeNew("SubChild2", mType, user, mChild1.Id); + var mSubChild3 = global::umbraco.cms.businesslogic.media.Media.MakeNew("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 })); + } } } \ No newline at end of file diff --git a/src/Umbraco.Web/DefaultPublishedMediaStore.cs b/src/Umbraco.Web/DefaultPublishedMediaStore.cs index 562bfd91e5..b6675e01fa 100644 --- a/src/Umbraco.Web/DefaultPublishedMediaStore.cs +++ b/src/Umbraco.Web/DefaultPublishedMediaStore.cs @@ -85,17 +85,15 @@ namespace Umbraco.Web var media = global::umbraco.library.GetMedia(id, true); if (media != null && media.Current != null) { - //if (media.MoveNext()) + media.MoveNext(); + ////error check + //if (media.Current.MoveToFirstChild() && media.Current.Name.InvariantEquals("error")) //{ - var current = media.Current; - //error check - if (media.Current.MoveToFirstChild() && media.Current.Name.InvariantEquals("error")) - { - return null; - } + // return null; + //} + //var current = media.Current; - return ConvertFromXPathNavigator(current); - //} + return ConvertFromXPathNavigator(media.Current); } return null; @@ -195,7 +193,8 @@ namespace Umbraco.Web ? GetUmbracoMedia(d.ParentId) : null, //callback to return the children of the current node based on the xml structure already found - d => GetChildrenMedia(d.ParentId, xpath), + //d => GetChildrenMedia(d.ParentId, xpath), + d => GetChildrenMedia(d.Id, xpath), GetProperty); } @@ -285,32 +284,36 @@ namespace Umbraco.Web var media = library.GetMedia(parentId, true); if (media != null && media.Current != null) { - if (!media.MoveNext()) - return null; xpath = media.Current; } + else + { + return null; + } } - var children = xpath.SelectChildren(XPathNodeType.Element); - var mediaList = new List(); - while (children.Current != null) + //The xpath might be the whole xpath including the current ones ancestors so we need to select the current node + var item = xpath.Select("//*[@id='" + parentId + "']"); + if (item.Current == null) { - if (!children.MoveNext()) - { - break; - } + return null; + } + var children = item.Current.SelectChildren(XPathNodeType.Element); + var mediaList = new List(); + foreach(XPathNavigator x in children) + { //NOTE: I'm not sure why this is here, it is from legacy code of ExamineBackedMedia, but // will leave it here as it must have done something! - if (children.Current.Name != "contents") + if (x.Name != "contents") { //make sure it's actually a node, not a property - if (!string.IsNullOrEmpty(children.Current.GetAttribute("path", "")) && - !string.IsNullOrEmpty(children.Current.GetAttribute("id", ""))) + if (!string.IsNullOrEmpty(x.GetAttribute("path", "")) && + !string.IsNullOrEmpty(x.GetAttribute("id", ""))) { - mediaList.Add(ConvertFromXPathNavigator(children.Current)); + mediaList.Add(ConvertFromXPathNavigator(x)); } - } + } } return mediaList; }