Skip to content

Commit 54c45f3

Browse files
committed
Added integration tests for PropertyTypeUsageService and adjusted assert in management API permissions test.
1 parent 04df265 commit 54c45f3

File tree

7 files changed

+66
-28
lines changed

7 files changed

+66
-28
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
namespace Umbraco.Cms.Core.Persistence.Repositories;
22

3+
/// <summary>
4+
/// Defines repository methods for querying property type usage.
5+
/// </summary>
36
public interface IPropertyTypeUsageRepository
47
{
8+
/// <summary>
9+
/// Determines whether there are any saved property values for the specified content type and property alias.
10+
/// </summary>
511
Task<bool> HasSavedPropertyValuesAsync(Guid contentTypeKey, string propertyAlias);
12+
13+
/// <summary>
14+
/// Determines whether a content type with the specified unique identifier exists.
15+
/// </summary>
616
Task<bool> ContentTypeExistAsync(Guid contentTypeKey);
717
}

src/Umbraco.Core/Services/IPropertyTypeUsageService.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Umbraco.Cms.Core.Services;
44

5+
/// <summary>
6+
/// Defines service methods for querying property type usage.
7+
/// </summary>
58
public interface IPropertyTypeUsageService
69
{
710
/// <summary>

src/Umbraco.Core/Services/PropertyTypeUsageService.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,25 @@
44

55
namespace Umbraco.Cms.Core.Services;
66

7+
/// <inheritdoc/>
78
public class PropertyTypeUsageService : IPropertyTypeUsageService
89
{
910
private readonly IPropertyTypeUsageRepository _propertyTypeUsageRepository;
10-
private readonly IContentTypeService _contentTypeService;
1111
private readonly ICoreScopeProvider _scopeProvider;
1212

13+
// TODO (V18): Remove IContentTypeService parameter from constructor.
14+
15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="PropertyTypeUsageService"/> class.
17+
/// </summary>
1318
public PropertyTypeUsageService(
1419
IPropertyTypeUsageRepository propertyTypeUsageRepository,
20+
#pragma warning disable IDE0060 // Remove unused parameter
1521
IContentTypeService contentTypeService,
22+
#pragma warning restore IDE0060 // Remove unused parameter
1623
ICoreScopeProvider scopeProvider)
1724
{
1825
_propertyTypeUsageRepository = propertyTypeUsageRepository;
19-
_contentTypeService = contentTypeService;
2026
_scopeProvider = scopeProvider;
2127
}
2228

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
using NPoco;
32
using Umbraco.Cms.Core;
43
using Umbraco.Cms.Core.Persistence.Repositories;
@@ -8,28 +7,26 @@
87

98
namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement;
109

10+
/// <inheritdoc/>
1111
internal sealed class PropertyTypeUsageRepository : IPropertyTypeUsageRepository
1212
{
13-
private static readonly Guid?[] NodeObjectTypes = new Guid?[]
14-
{
13+
private static readonly List<Guid> _nodeObjectTypes =
14+
[
1515
Constants.ObjectTypes.DocumentType, Constants.ObjectTypes.MediaType, Constants.ObjectTypes.MemberType,
16-
};
16+
];
1717

1818
private readonly IScopeAccessor _scopeAccessor;
1919

20-
public PropertyTypeUsageRepository(IScopeAccessor scopeAccessor)
21-
{
22-
_scopeAccessor = scopeAccessor;
23-
}
20+
/// <summary>
21+
/// Initializes a new instance of the <see cref="PropertyTypeUsageRepository"/> class.
22+
/// </summary>
23+
public PropertyTypeUsageRepository(IScopeAccessor scopeAccessor) => _scopeAccessor = scopeAccessor;
2424

25+
/// <inheritdoc/>
2526
public Task<bool> HasSavedPropertyValuesAsync(Guid contentTypeKey, string propertyAlias)
2627
{
27-
IUmbracoDatabase? database = _scopeAccessor.AmbientScope?.Database;
28-
29-
if (database is null)
30-
{
31-
throw new InvalidOperationException("A scope is required to query the database");
32-
}
28+
IUmbracoDatabase? database = _scopeAccessor.AmbientScope?.Database
29+
?? throw new InvalidOperationException("A scope is required to query the database");
3330

3431
Sql<ISqlContext> selectQuery = database.SqlContext.Sql()
3532
.SelectAll()
@@ -47,26 +44,21 @@ public Task<bool> HasSavedPropertyValuesAsync(Guid contentTypeKey, string proper
4744
return Task.FromResult(database.ExecuteScalar<bool>(hasValuesQuery));
4845
}
4946

47+
/// <inheritdoc/>
5048
public Task<bool> ContentTypeExistAsync(Guid contentTypeKey)
5149
{
52-
IUmbracoDatabase? database = _scopeAccessor.AmbientScope?.Database;
53-
54-
if (database is null)
55-
{
56-
throw new InvalidOperationException("A scope is required to query the database");
57-
}
50+
IUmbracoDatabase? database = _scopeAccessor.AmbientScope?.Database
51+
?? throw new InvalidOperationException("A scope is required to query the database");
5852

5953
Sql<ISqlContext> selectQuery = database.SqlContext.Sql()
6054
.SelectAll()
6155
.From<NodeDto>("n")
6256
.Where<NodeDto>(n => n.UniqueId == contentTypeKey, "n")
63-
.Where<NodeDto>(n => NodeObjectTypes.Contains(n.NodeObjectType), "n");
57+
.WhereIn<NodeDto>(n => n.NodeObjectType, _nodeObjectTypes, "n");
6458

6559
Sql<ISqlContext> hasValuesQuery = database.SqlContext.Sql()
6660
.SelectAnyIfExists(selectQuery);
6761

6862
return Task.FromResult(database.ExecuteScalar<bool>(hasValuesQuery));
6963
}
70-
71-
7264
}

tests/Umbraco.Tests.Integration/ManagementApi/PropertyType/IsUsedPropertyTypeControllerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public async Task Setup()
3636

3737
protected override UserGroupAssertionModel AdminUserGroupAssertionModel => new()
3838
{
39-
ExpectedStatusCode = HttpStatusCode.InternalServerError
39+
ExpectedStatusCode = HttpStatusCode.OK
4040
};
4141

4242
protected override UserGroupAssertionModel EditorUserGroupAssertionModel => new()

tests/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTestWithContent.cs

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

4-
using System;
54
using NUnit.Framework;
65
using Umbraco.Cms.Core.Models;
76
using Umbraco.Cms.Core.Services;
@@ -11,6 +10,8 @@ namespace Umbraco.Cms.Tests.Integration.Testing;
1110

1211
public abstract class UmbracoIntegrationTestWithContent : UmbracoIntegrationTest
1312
{
13+
protected const string TextpageContentTypeKey = "1D3A8E6E-2EA9-4CC1-B229-1AEE19821522";
14+
1415
protected const string TextpageKey = "B58B3AD4-62C2-4E27-B1BE-837BD7C533E0";
1516
protected const string SubPageKey = "07EABF4A-5C62-4662-9F2A-15BBB488BCA5";
1617
protected const string SubPage2Key = "0EED78FC-A6A8-4587-AB18-D3AFE212B1C4";
@@ -48,7 +49,7 @@ public virtual void CreateTestData()
4849
// Create and Save ContentType "umbTextpage" -> 1051 (template), 1052 (content type)
4950
ContentType =
5051
ContentTypeBuilder.CreateSimpleContentType("umbTextpage", "Textpage", defaultTemplateId: template.Id);
51-
ContentType.Key = new Guid("1D3A8E6E-2EA9-4CC1-B229-1AEE19821522");
52+
ContentType.Key = new Guid(TextpageContentTypeKey);
5253
ContentTypeService.Save(ContentType);
5354

5455
// Create and Save Content "Homepage" based on "umbTextpage" -> 1053
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using NUnit.Framework;
2+
using Umbraco.Cms.Core;
3+
using Umbraco.Cms.Core.Services;
4+
using Umbraco.Cms.Core.Services.OperationStatus;
5+
using Umbraco.Cms.Tests.Common.Testing;
6+
using Umbraco.Cms.Tests.Integration.Testing;
7+
8+
namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Services;
9+
10+
[TestFixture]
11+
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
12+
internal sealed class PropertyTypeUsageServiceTests : UmbracoIntegrationTestWithContent
13+
{
14+
private IPropertyTypeUsageService PropertyTypeUsageService => GetRequiredService<IPropertyTypeUsageService>();
15+
16+
[TestCase(TextpageContentTypeKey, "title", true, true, PropertyTypeOperationStatus.Success)]
17+
[TestCase("1D3A8E6E-2EA9-4CC1-B229-1AEE19821523", "title", false, false, PropertyTypeOperationStatus.ContentTypeNotFound)]
18+
[TestCase(TextpageContentTypeKey, "missingProperty", true, false, PropertyTypeOperationStatus.Success)]
19+
public async Task Can_Check_For_Saved_Property_Values(Guid contentTypeKey, string propertyAlias, bool expectedSuccess, bool expectedResult, PropertyTypeOperationStatus expectedOperationStatus)
20+
{
21+
Attempt<bool, PropertyTypeOperationStatus> resultAttempt = await PropertyTypeUsageService.HasSavedPropertyValuesAsync(contentTypeKey, propertyAlias);
22+
Assert.AreEqual(expectedSuccess, resultAttempt.Success);
23+
Assert.AreEqual(expectedResult, resultAttempt.Result);
24+
Assert.AreEqual(expectedOperationStatus, resultAttempt.Status);
25+
}
26+
}

0 commit comments

Comments
 (0)