Skip to content

Commit 46e0b08

Browse files
Merge remote-tracking branch 'origin/v8/bugfix/fix-atroot' into v8/dev
2 parents 80ffd02 + 7a178be commit 46e0b08

2 files changed

Lines changed: 101 additions & 2 deletions

File tree

src/Umbraco.Tests/PublishedContent/NuCacheChildrenTests.cs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,74 @@ ContentNodeKit CreateKit(int id, int parentId, int sortOrder)
264264
yield return CreateKit(12, 4, 2);
265265
}
266266

267+
private IEnumerable<ContentNodeKit> GetVariantWithDraftKits()
268+
{
269+
var paths = new Dictionary<int, string> { { -1, "-1" } };
270+
271+
Dictionary<string, CultureVariation> GetCultureInfos(int id, DateTime now)
272+
{
273+
var en = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
274+
var fr = new[] { 1, 3, 4, 6, 7, 9, 10, 12 };
275+
276+
var infos = new Dictionary<string, CultureVariation>();
277+
if (en.Contains(id))
278+
infos["en-US"] = new CultureVariation { Name = "N" + id + "-" + "en-US", Date = now, IsDraft = false };
279+
if (fr.Contains(id))
280+
infos["fr-FR"] = new CultureVariation { Name = "N" + id + "-" + "fr-FR", Date = now, IsDraft = false };
281+
return infos;
282+
}
283+
284+
ContentNodeKit CreateKit(int id, int parentId, int sortOrder)
285+
{
286+
if (!paths.TryGetValue(parentId, out var parentPath))
287+
throw new Exception("Unknown parent.");
288+
289+
var path = paths[id] = parentPath + "," + id;
290+
var level = path.Count(x => x == ',');
291+
var now = DateTime.Now;
292+
293+
ContentData CreateContentData(bool published) => new ContentData
294+
{
295+
Name = "N" + id,
296+
Published = published,
297+
TemplateId = 0,
298+
VersionId = 1,
299+
VersionDate = now,
300+
WriterId = 0,
301+
Properties = new Dictionary<string, PropertyData[]>(),
302+
CultureInfos = GetCultureInfos(id, now)
303+
};
304+
305+
var withDraft = id%2==0;
306+
var withPublished = !withDraft;
307+
308+
return new ContentNodeKit
309+
{
310+
ContentTypeId = _contentTypeVariant.Id,
311+
Node = new ContentNode(id, Guid.NewGuid(), level, path, sortOrder, parentId, DateTime.Now, 0),
312+
DraftData = withDraft ? CreateContentData(false) : null,
313+
PublishedData = withPublished ? CreateContentData(true) : null
314+
};
315+
}
316+
317+
yield return CreateKit(1, -1, 1);
318+
yield return CreateKit(2, -1, 2);
319+
yield return CreateKit(3, -1, 3);
320+
321+
yield return CreateKit(4, 1, 1);
322+
yield return CreateKit(5, 1, 2);
323+
yield return CreateKit(6, 1, 3);
324+
325+
yield return CreateKit(7, 2, 3);
326+
yield return CreateKit(8, 2, 2);
327+
yield return CreateKit(9, 2, 1);
328+
329+
yield return CreateKit(10, 3, 1);
330+
331+
yield return CreateKit(11, 4, 1);
332+
yield return CreateKit(12, 4, 2);
333+
}
334+
267335
[Test]
268336
public void EmptyTest()
269337
{
@@ -747,6 +815,25 @@ public void UpdateTest()
747815
AssertDocuments(documents, "N9", "N8", "N7");
748816
}
749817

818+
[Test]
819+
public void AtRootTest()
820+
{
821+
Init(GetVariantWithDraftKits());
822+
823+
var snapshot = _snapshotService.CreatePublishedSnapshot(previewToken: null);
824+
_snapshotAccessor.PublishedSnapshot = snapshot;
825+
826+
_variationAccesor.VariationContext = new VariationContext("en-US");
827+
828+
// N2 is draft only
829+
830+
var documents = snapshot.Content.GetAtRoot().ToArray();
831+
AssertDocuments(documents, "N1-en-US", /*"N2-en-US",*/ "N3-en-US");
832+
833+
documents = snapshot.Content.GetAtRoot(true).ToArray();
834+
AssertDocuments(documents, "N1-en-US", "N2-en-US", "N3-en-US");
835+
}
836+
750837
private void AssertDocuments(IPublishedContent[] documents, params string[] names)
751838
{
752839
Assert.AreEqual(names.Length, documents.Length);

src/Umbraco.Web/PublishedCache/NuCache/ContentCache.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,22 @@ public override IEnumerable<IPublishedContent> GetAtRoot(bool preview, string cu
261261
if (culture == null)
262262
culture = _variationContextAccessor?.VariationContext?.Culture ?? "";
263263

264+
// _snapshot.GetAtRoot() returns all ContentNode at root
264265
// both .Draft and .Published cannot be null at the same time
265266
// root is already sorted by sortOrder, and does not contain nulls
266-
var atRoot = _snapshot.GetAtRoot().Select(n => GetNodePublishedContent(n, preview));
267-
return culture == "*" ? atRoot : atRoot.Where(x => x.IsInvariantOrHasCulture(culture));
267+
//
268+
// GetNodePublishedContent may return null if !preview and there is no
269+
// published model, so we need to filter these nulls out
270+
271+
var atRoot = _snapshot.GetAtRoot()
272+
.Select(n => GetNodePublishedContent(n, preview))
273+
.WhereNotNull();
274+
275+
// if a culture is specified, we must ensure that it is avail/published
276+
if (culture != "*")
277+
atRoot = atRoot.Where(x => x.IsInvariantOrHasCulture(culture));
278+
279+
return atRoot;
268280
}
269281

270282
private static IPublishedContent GetNodePublishedContent(ContentNode node, bool preview)

0 commit comments

Comments
 (0)