Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed copy preserving sort order #10091

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/Umbraco.Core/Services/Implement/ContentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1415,7 +1415,7 @@ private void PerformScheduledPublishingExpiration(DateTime date, List<PublishRes

var result = CommitDocumentChangesInternal(scope, d, saveEventArgs, allLangs.Value, d.WriterId);
if (result.Success == false)
Logger.Error<ContentService,int,PublishResultType>(null, "Failed to publish document id={DocumentId}, reason={Reason}.", d.Id, result.Result);
Logger.Error<ContentService, int, PublishResultType>(null, "Failed to publish document id={DocumentId}, reason={Reason}.", d.Id, result.Result);
results.Add(result);

}
Expand Down Expand Up @@ -2201,7 +2201,7 @@ public IContent Copy(IContent content, int parentId, bool relateToOriginal, bool
while (page * pageSize < total)
{
var descendants = GetPagedDescendants(content.Id, page++, pageSize, out total);
foreach (var descendant in descendants)
foreach (var descendant in descendants.OrderBy(x => x.Level).ThenBy(y => y.SortOrder))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be sorted via SQL/Database, this will not perform well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried passing "level" to the GetPagedDescendants method:

var descendants = GetPagedDescendants(content.Id, page++, pageSize, out total, ordering: Ordering.By("level"));

But that doesn't work because we end up in ContentRepositoryBase.ApplySystemOrdering and level is not a supported field there. I don't know if it can "just" be added but even if we could then Umbraco.Core.Services.Ordering does not seem to support ordering on multiple columns so we wouldn't be able to construct the same ordering that being doing in Linq here.

{
// if parent has not been copied, skip, else gets its copy id
if (idmap.TryGetValue(descendant.ParentId, out parentId) == false) continue;
Expand Down Expand Up @@ -2420,7 +2420,7 @@ public ContentDataIntegrityReport CheckDataIntegrity(ContentDataIntegrityReportO
if (report.FixedIssues.Count > 0)
{
//The event args needs a content item so we'll make a fake one with enough properties to not cause a null ref
var root = new Content("root", -1, new ContentType(-1)) {Id = -1, Key = Guid.Empty};
var root = new Content("root", -1, new ContentType(-1)) { Id = -1, Key = Guid.Empty };
scope.Events.Dispatch(TreeChanged, this, new TreeChange<IContent>.EventArgs(new TreeChange<IContent>(root, TreeChangeTypes.RefreshAll)));
}

Expand Down Expand Up @@ -3169,7 +3169,7 @@ public OperationResult Rollback(int id, int versionId, string culture = "*", int
if (rollbackSaveResult.Success == false)
{
//Log the error/warning
Logger.Error<ContentService,int,int,int>("User '{UserId}' was unable to rollback content '{ContentId}' to version '{VersionId}'", userId, id, versionId);
Logger.Error<ContentService, int, int, int>("User '{UserId}' was unable to rollback content '{ContentId}' to version '{VersionId}'", userId, id, versionId);
}
else
{
Expand All @@ -3178,7 +3178,7 @@ public OperationResult Rollback(int id, int versionId, string culture = "*", int
scope.Events.Dispatch(RolledBack, this, rollbackEventArgs);

//Logging & Audit message
Logger.Info<ContentService,int,int,int>("User '{UserId}' rolled back content '{ContentId}' to version '{VersionId}'", userId, id, versionId);
Logger.Info<ContentService, int, int, int>("User '{UserId}' rolled back content '{ContentId}' to version '{VersionId}'", userId, id, versionId);
Audit(AuditType.RollBack, userId, id, $"Content '{content.Name}' was rolled back to version '{versionId}'");
}

Expand Down
26 changes: 26 additions & 0 deletions src/Umbraco.Tests/Services/ContentServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2082,6 +2082,32 @@ public void Can_Copy_Content_With_Tags()
Assert.AreEqual("world", copiedTags[1].Text);
}

[Test]
public void Copy_Recursive_Preserves_Sort_Order()
{
// Arrange
var contentService = ServiceContext.ContentService;
var temp = contentService.GetById(NodeDto.NodeIdSeed + 2);
Assert.AreEqual("Home", temp.Name);
Assert.AreEqual(3, contentService.CountChildren(temp.Id));
var reversedChildren = contentService.GetPagedChildren(temp.Id, 0, 10, out var total1).Reverse().ToArray();
contentService.Sort(reversedChildren);

// Act
var copy = contentService.Copy(temp, temp.ParentId, false, true, Constants.Security.SuperUserId);
var content = contentService.GetById(NodeDto.NodeIdSeed + 2);

// Assert
Assert.That(copy, Is.Not.Null);
Assert.That(copy.Id, Is.Not.EqualTo(content.Id));
Assert.AreNotSame(content, copy);
Assert.AreEqual(3, contentService.CountChildren(copy.Id));

var copiedChildren = contentService.GetPagedChildren(copy.Id, 0, 10, out var total2).OrderBy(c => c.SortOrder).ToArray();
Assert.AreEqual(reversedChildren.First().Name, copiedChildren.First().Name);
Assert.AreEqual(reversedChildren.Last().Name, copiedChildren.Last().Name);
}

[Test]
public void Can_Rollback_Version_On_Content()
{
Expand Down