From ea9d27c03881c04a952aaa0aa07e05185f4cbe7e Mon Sep 17 00:00:00 2001 From: Paul Johnson Date: Tue, 7 Jun 2022 10:25:05 +0100 Subject: [PATCH] Add depth property to ICoreScope (#12540) --- src/Umbraco.Core/Scoping/ICoreScope.cs | 9 +++++- src/Umbraco.Infrastructure/Scoping/Scope.cs | 13 +++++++++ .../Scoping/ScopeUnitTests.cs | 29 +++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/Umbraco.Core/Scoping/ICoreScope.cs b/src/Umbraco.Core/Scoping/ICoreScope.cs index 8bb85ca29d..e1382f0ec8 100644 --- a/src/Umbraco.Core/Scoping/ICoreScope.cs +++ b/src/Umbraco.Core/Scoping/ICoreScope.cs @@ -1,4 +1,3 @@ -using System; using Umbraco.Cms.Core.Cache; using Umbraco.Cms.Core.Events; @@ -9,6 +8,14 @@ namespace Umbraco.Cms.Core.Scoping; /// public interface ICoreScope : IDisposable, IInstanceIdentifiable { + /// + /// Gets the distance from the root scope. + /// + /// + /// A zero represents a root scope, any value greater than zero represents a child scope. + /// + public int Depth => -1; + /// /// Gets the scope notification publisher /// diff --git a/src/Umbraco.Infrastructure/Scoping/Scope.cs b/src/Umbraco.Infrastructure/Scoping/Scope.cs index 190261a808..e1ad1d2389 100644 --- a/src/Umbraco.Infrastructure/Scoping/Scope.cs +++ b/src/Umbraco.Infrastructure/Scoping/Scope.cs @@ -478,6 +478,19 @@ namespace Umbraco.Cms.Infrastructure.Scoping } } + public int Depth + { + get + { + if (ParentScope == null) + { + return 0; + } + + return ParentScope.Depth + 1; + } + } + public IScopedNotificationPublisher Notifications { get diff --git a/tests/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Scoping/ScopeUnitTests.cs b/tests/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Scoping/ScopeUnitTests.cs index f429f584e5..5734622e3e 100644 --- a/tests/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Scoping/ScopeUnitTests.cs +++ b/tests/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Scoping/ScopeUnitTests.cs @@ -579,5 +579,34 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Infrastructure.Scoping Assert.IsNull(realScope.GetReadLocks()); } } + + [Test] + public void Depth_WhenRootScope_ReturnsZero() + { + var scopeProvider = GetScopeProvider(out var syntaxProviderMock); + + using (var scope = scopeProvider.CreateScope()) + { + Assert.AreEqual(0,scope.Depth); + } + } + + + [Test] + public void Depth_WhenChildScope_ReturnsDepth() + { + var scopeProvider = GetScopeProvider(out var syntaxProviderMock); + + using (scopeProvider.CreateScope()) + { + using (scopeProvider.CreateScope()) + { + using (var c2 = scopeProvider.CreateScope()) + { + Assert.AreEqual(2, c2.Depth); + } + } + } + } } }