Skip to content

Commit 21e3518

Browse files
Migaroezbergmania
andauthored
Fix copy document with descendants onto self (#17661)
* Filter self out of descedants for self onto self copy * Optimized code --------- Co-authored-by: Bjarke Berg <[email protected]>
1 parent 28756d4 commit 21e3518

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/Umbraco.Core/Services/ContentService.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2699,6 +2699,13 @@ public bool RecycleBinSmells()
26992699
GetPagedDescendants(content.Id, page++, pageSize, out total);
27002700
foreach (IContent descendant in descendants)
27012701
{
2702+
// when copying a branch into itself, the copy of a root would be seen as a descendant
2703+
// and would be copied again => filter it out.
2704+
if (descendant.Id == copy.Id)
2705+
{
2706+
continue;
2707+
}
2708+
27022709
// if parent has not been copied, skip, else gets its copy id
27032710
if (idmap.TryGetValue(descendant.ParentId, out parentId) == false)
27042711
{

tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ContentEditingServiceTests.Copy.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,48 @@ void VerifyCopy(IContent? copiedRoot)
198198
}
199199
}
200200

201+
[TestCase(true)]
202+
[TestCase(false)]
203+
public async Task Can_Copy_Onto_Self(bool includeDescendants)
204+
{
205+
var contentType = await CreateTextPageContentTypeAsync();
206+
(IContent root, IContent child) = await CreateRootAndChildAsync(contentType);
207+
208+
var result = await ContentEditingService.CopyAsync(root.Key, root.Key, false, includeDescendants, Constants.Security.SuperUserKey);
209+
Assert.IsTrue(result.Success);
210+
Assert.AreEqual(ContentEditingOperationStatus.Success, result.Status);
211+
212+
VerifyCopy(result.Result);
213+
214+
// re-get and re-test
215+
VerifyCopy(await ContentEditingService.GetAsync(result.Result!.Key));
216+
217+
void VerifyCopy(IContent? copiedRoot)
218+
{
219+
Assert.IsNotNull(copiedRoot);
220+
Assert.AreEqual(root.Id, copiedRoot.ParentId);
221+
Assert.IsTrue(copiedRoot.HasIdentity);
222+
Assert.AreNotEqual(root.Key, copiedRoot.Key);
223+
Assert.AreEqual(root.Name, copiedRoot.Name);
224+
var copiedChildren = ContentService.GetPagedChildren(copiedRoot.Id, 0, 100, out var total).ToArray();
225+
226+
if (includeDescendants)
227+
{
228+
Assert.AreEqual(1, copiedChildren.Length);
229+
Assert.AreEqual(1, total);
230+
var copiedChild = copiedChildren.First();
231+
Assert.AreNotEqual(child.Id, copiedChild.Id);
232+
Assert.AreNotEqual(child.Key, copiedChild.Key);
233+
Assert.AreEqual(child.Name, copiedChild.Name);
234+
}
235+
else
236+
{
237+
Assert.AreEqual(0, copiedChildren.Length);
238+
Assert.AreEqual(0, total);
239+
}
240+
}
241+
}
242+
201243
[Test]
202244
public async Task Can_Relate_Copy_To_Original()
203245
{

0 commit comments

Comments
 (0)