Skip to content

Commit e5d1c67

Browse files
authored
Fix logic with check for duplicate container name at level (#19803)
Fix logic with check for duplicate container name at level.
1 parent 046a3d9 commit e5d1c67

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

src/Umbraco.Core/Services/EntityTypeContainerService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public Task<IEnumerable<EntityContainer>> GetAllAsync()
107107
return EntityContainerOperationStatus.ParentNotFound;
108108
}
109109

110-
if (_entityContainerRepository.HasDuplicateName(container.ParentId, container.Name!))
110+
if (_entityContainerRepository.HasDuplicateName(parentContainer?.Id ?? Constants.System.Root, container.Name!))
111111
{
112112
return EntityContainerOperationStatus.DuplicateName;
113113
}

src/Umbraco.Infrastructure/Persistence/Repositories/Implement/EntityContainerRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public bool HasDuplicateName(int parentId, string name)
149149
{
150150
NodeDto nodeDto = Database.FirstOrDefault<NodeDto>(Sql().SelectAll()
151151
.From<NodeDto>()
152-
.Where<NodeDto>(dto => dto.Text == name && dto.NodeObjectType == NodeObjectTypeId && dto.ParentId == parentId));
152+
.Where<NodeDto>(dto => dto.Text == name && dto.NodeObjectType == NodeObjectTypeId && dto.ParentId == parentId));
153153

154154
return nodeDto is not null;
155155
}

tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/DataTypeContainerServiceTests.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using Umbraco.Cms.Core.Serialization;
66
using Umbraco.Cms.Core.Services;
77
using Umbraco.Cms.Core.Services.OperationStatus;
8-
using Umbraco.Cms.Infrastructure.Services;
98
using Umbraco.Cms.Tests.Common.Testing;
109
using Umbraco.Cms.Tests.Integration.Testing;
1110

@@ -54,6 +53,26 @@ public async Task Can_Create_Child_Container()
5453
Assert.AreEqual(root.Id, created.ParentId);
5554
}
5655

56+
[TestCase("Existing Child Container", false)]
57+
[TestCase("New Child Container", true)]
58+
[TestCase("Root Container", true)]
59+
public async Task Can_Create_Child_Container_Without_Duplicate_Name_At_Level(string containerName, bool expectSuccess)
60+
{
61+
EntityContainer root = (await DataTypeContainerService.CreateAsync(null, "Root Container", null, Constants.Security.SuperUserKey)).Result;
62+
EntityContainer existingChild = (await DataTypeContainerService.CreateAsync(null, "Existing Child Container", root.Key, Constants.Security.SuperUserKey)).Result;
63+
64+
var result = await DataTypeContainerService.CreateAsync(null, containerName, root.Key, Constants.Security.SuperUserKey);
65+
Assert.AreEqual(expectSuccess, result.Success);
66+
if (expectSuccess)
67+
{
68+
Assert.AreEqual(EntityContainerOperationStatus.Success, result.Status);
69+
}
70+
else
71+
{
72+
Assert.AreEqual(EntityContainerOperationStatus.DuplicateName, result.Status);
73+
}
74+
}
75+
5776
[Test]
5877
public async Task Can_Create_Container_With_Explicit_Key()
5978
{

0 commit comments

Comments
 (0)