Skip to content

Commit ce3d60b

Browse files
committed
Added integration tests to verify behaviour.
1 parent cea45dc commit ce3d60b

File tree

2 files changed

+86
-4
lines changed

2 files changed

+86
-4
lines changed

tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Persistence/Repositories/ContentTypeRepositoryTest.cs

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
// Copyright (c) Umbraco.
22
// See LICENSE for more details.
33

4-
using System.Collections.Generic;
5-
using System.Linq;
64
using Microsoft.Extensions.Logging;
75
using Microsoft.Extensions.Options;
86
using Moq;
97
using NUnit.Framework;
8+
using Umbraco.Cms.Api.Management.Mapping.Permissions;
109
using Umbraco.Cms.Core;
1110
using Umbraco.Cms.Core.Cache;
1211
using Umbraco.Cms.Core.Configuration.Models;
1312
using Umbraco.Cms.Core.IO;
1413
using Umbraco.Cms.Core.Mapping;
1514
using Umbraco.Cms.Core.Models;
16-
using Umbraco.Cms.Core.Models.ContentEditing;
15+
using Umbraco.Cms.Core.Models.Membership;
16+
using Umbraco.Cms.Core.Models.Membership.Permissions;
1717
using Umbraco.Cms.Core.Persistence;
1818
using Umbraco.Cms.Core.Persistence.Repositories;
1919
using Umbraco.Cms.Core.Services;
@@ -52,8 +52,11 @@ internal sealed class ContentTypeRepositoryTest : UmbracoIntegrationTest
5252
private IMediaTypeRepository MediaTypeRepository => GetRequiredService<IMediaTypeRepository>();
5353

5454
private IDocumentRepository DocumentRepository => GetRequiredService<IDocumentRepository>();
55+
5556
private IContentService ContentService => GetRequiredService<IContentService>();
5657

58+
private IUserGroupRepository UserGroupRepository => GetRequiredService<IUserGroupRepository>();
59+
5760
private ContentTypeRepository ContentTypeRepository =>
5861
(ContentTypeRepository)GetRequiredService<IContentTypeRepository>();
5962

@@ -918,4 +921,83 @@ public void Can_Update_Variation_Of_Element_Type_Property()
918921
Assert.That(hasCulture, Is.True);
919922
}
920923
}
924+
925+
[Test]
926+
public void Can_Remove_Property_Value_Permissions_On_Removal_Of_Property_Types()
927+
{
928+
var provider = ScopeProvider;
929+
using (var scope = provider.CreateScope())
930+
{
931+
// Create, save and re-retrieve a content type and user group.
932+
IContentType contentType = ContentTypeBuilder.CreateSimpleContentType(defaultTemplateId: 0);
933+
ContentTypeRepository.Save(contentType);
934+
contentType = ContentTypeRepository.Get(contentType.Id);
935+
936+
var userGroup = CreateUserGroupWithGranularPermissions(contentType);
937+
938+
// Remove property types and verify that the permission is removed from the user group.
939+
contentType.RemovePropertyType("author");
940+
ContentTypeRepository.Save(contentType);
941+
userGroup = UserGroupRepository.Get(userGroup.Id);
942+
Assert.AreEqual(3, userGroup.GranularPermissions.Count);
943+
944+
contentType.RemovePropertyType("bodyText");
945+
ContentTypeRepository.Save(contentType);
946+
userGroup = UserGroupRepository.Get(userGroup.Id);
947+
Assert.AreEqual(2, userGroup.GranularPermissions.Count);
948+
949+
contentType.RemovePropertyType("title");
950+
ContentTypeRepository.Save(contentType);
951+
userGroup = UserGroupRepository.Get(userGroup.Id);
952+
Assert.AreEqual(0, userGroup.GranularPermissions.Count);
953+
}
954+
}
955+
956+
[Test]
957+
public void Can_Remove_Property_Value_Permissions_On_Removal_Of_Content_Type()
958+
{
959+
var provider = ScopeProvider;
960+
using (var scope = provider.CreateScope())
961+
{
962+
// Create, save and re-retrieve a content type and user group.
963+
IContentType contentType = ContentTypeBuilder.CreateSimpleContentType(defaultTemplateId: 0);
964+
ContentTypeRepository.Save(contentType);
965+
contentType = ContentTypeRepository.Get(contentType.Id);
966+
967+
var userGroup = CreateUserGroupWithGranularPermissions(contentType);
968+
969+
// Remove the content type and verify all permissions are removed from the user group.
970+
ContentTypeRepository.Delete(contentType);
971+
userGroup = UserGroupRepository.Get(userGroup.Id);
972+
Assert.AreEqual(0, userGroup.GranularPermissions.Count);
973+
}
974+
}
975+
976+
private IUserGroup CreateUserGroupWithGranularPermissions(IContentType contentType)
977+
{
978+
DocumentPropertyValueGranularPermission CreatePermission(IPropertyType propertyType, string permission = "")
979+
=> new()
980+
{
981+
Key = contentType.Key,
982+
Permission = propertyType.Key.ToString().ToLowerInvariant() + "|" + permission,
983+
};
984+
985+
var titlePropertyType = contentType.PropertyTypes.Single(x => x.Alias == "title");
986+
var bodyTextPropertyType = contentType.PropertyTypes.Single(x => x.Alias == "bodyText");
987+
var authorPropertyType = contentType.PropertyTypes.Single(x => x.Alias == "author");
988+
989+
var userGroup = new UserGroupBuilder()
990+
.WithGranularPermissions([
991+
CreatePermission(titlePropertyType, "Umb.Document.PropertyValue.Read"),
992+
CreatePermission(titlePropertyType, "Umb.Document.PropertyValue.Write"),
993+
CreatePermission(bodyTextPropertyType, "Umb.Document.PropertyValue.Read"),
994+
CreatePermission(authorPropertyType)
995+
])
996+
.Build();
997+
UserGroupRepository.Save(userGroup);
998+
userGroup = UserGroupRepository.Get(userGroup.Id);
999+
1000+
Assert.AreEqual(4, userGroup.GranularPermissions.Count);
1001+
return userGroup;
1002+
}
9211003
}

tests/Umbraco.Tests.Integration/appsettings.Tests.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
},
1212
"Tests": {
1313
"Database": {
14-
"DatabaseType": "SQLite",
14+
"DatabaseType": "SQLite", // "SQLite", "LocalDb"
1515
"PrepareThreadCount": 4,
1616
"SchemaDatabaseCount": 4,
1717
"EmptyDatabasesCount": 2,

0 commit comments

Comments
 (0)