Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor UmbracoContext #4577

Merged
merged 10 commits into from
Feb 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 0 additions & 64 deletions src/Umbraco.Core/DisposableObject.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Umbraco.Core/Logging/LogProfiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void Stop(bool discardResults = false)
}

// a lightweight disposable timer
private class LightDisposableTimer : DisposableObject
private class LightDisposableTimer : DisposableObjectSlim
{
private readonly Action<long> _callback;
private readonly Stopwatch _stopwatch = Stopwatch.StartNew();
Expand Down
2 changes: 1 addition & 1 deletion src/Umbraco.Core/Logging/VoidProfiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void Start()
public void Stop(bool discardResults = false)
{ }

private class VoidDisposable : DisposableObject
private class VoidDisposable : DisposableObjectSlim
{
protected override void DisposeResources()
{ }
Expand Down
2 changes: 1 addition & 1 deletion src/Umbraco.Core/Persistence/UmbracoDatabaseFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace Umbraco.Core.Persistence
/// </remarks>
// TODO: these comments are not true anymore
// TODO: this class needs not be disposable!
internal class UmbracoDatabaseFactory : DisposableObject, IUmbracoDatabaseFactory
internal class UmbracoDatabaseFactory : DisposableObjectSlim, IUmbracoDatabaseFactory
{
private readonly Lazy<IMapperCollection> _mappers;
private readonly ILogger _logger;
Expand Down
1 change: 0 additions & 1 deletion src/Umbraco.Core/Umbraco.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,6 @@
<Compile Include="DictionaryExtensions.cs" />
<Compile Include="Dictionary\ICultureDictionary.cs" />
<Compile Include="Dictionary\ICultureDictionaryFactory.cs" />
<Compile Include="DisposableObject.cs" />
<Compile Include="Logging\DisposableTimer.cs" />
<Compile Include="EmailSender.cs" />
<Compile Include="Enum.cs" />
Expand Down
15 changes: 14 additions & 1 deletion src/Umbraco.Tests/Cache/DistributedCacheBinderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
using Moq;
using NUnit.Framework;
using Umbraco.Core.Composing;
using Umbraco.Core.Configuration.UmbracoSettings;
using Umbraco.Core.Events;
using Umbraco.Core.Models;
using Umbraco.Core.Models.Membership;
using Umbraco.Core.Services;
using Umbraco.Tests.Testing;
using Umbraco.Tests.Testing.Objects.Accessors;
using Umbraco.Web;
using Umbraco.Web.Cache;
using Umbraco.Web.PublishedCache;
using Umbraco.Web.Routing;
Expand Down Expand Up @@ -148,8 +151,18 @@ public void CanHandleEvent()

};

var umbracoContextFactory = new UmbracoContextFactory(
new TestUmbracoContextAccessor(),
Mock.Of<IPublishedSnapshotService>(),
new TestVariationContextAccessor(),
new TestDefaultCultureAccessor(),
TestObjects.GetUmbracoSettings(),
TestObjects.GetGlobalSettings(),
Enumerable.Empty<IUrlProvider>(),
Mock.Of<IUserService>());

// just assert it does not throw
var refreshers = new DistributedCacheBinder(null, null);
var refreshers = new DistributedCacheBinder(null, umbracoContextFactory, null);
refreshers.HandleEvents(definitions);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Umbraco.Tests/IO/ShadowFileSystemTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ public void ShadowScopeComplete()
scope.Dispose();
scopedFileSystems = false;
Assert.IsTrue(phy.FileExists("sub/f5.txt"));
Assert.IsFalse(Directory.Exists(shadowfs + "/" + id));
TestHelper.TryAssert(() => Assert.IsFalse(Directory.Exists(shadowfs + "/" + id)));
}

[Test]
Expand Down
3 changes: 2 additions & 1 deletion src/Umbraco.Tests/Integration/ContentEventsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Umbraco.Tests.Services;
using Umbraco.Tests.TestHelpers.Entities;
using Umbraco.Tests.Testing;
using Umbraco.Web;
using Umbraco.Web.Cache;
using static Umbraco.Tests.Cache.DistributedCache.DistributedCacheTests;

Expand All @@ -32,7 +33,7 @@ public override void SetUp()
{
base.SetUp();

_h1 = new DistributedCacheBinder(new DistributedCache(), Mock.Of<ILogger>());
_h1 = new DistributedCacheBinder(new DistributedCache(), Mock.Of<IUmbracoContextFactory>(), Mock.Of<ILogger>());
_h1.BindEvents(true);

_events = new List<EventInstance>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@ public IDisposable ForcedPreview(bool preview, Action<bool> callback = null)
return new ForcedPreviewObject();
}

private class ForcedPreviewObject : DisposableObject
private class ForcedPreviewObject : DisposableObjectSlim
{
protected override void DisposeResources()
{ }
}

public void Dispose()
{ }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static ConcurrentDictionary<string, object> Current
{
get
{
var umbracoContext = UmbracoContext.Current;
var umbracoContext = Umbraco.Web.Composing.Current.UmbracoContext;

// will get or create a value
// a ConditionalWeakTable is thread-safe
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
using System.Linq;
using Moq;
using NUnit.Framework;
using Umbraco.Core.Composing;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.Services;
using Umbraco.Tests.Testing;
using Umbraco.Web;
using Current = Umbraco.Web.Composing.Current;

namespace Umbraco.Tests.PublishedContent
{
Expand Down Expand Up @@ -186,79 +186,79 @@ internal override void PopulateCache(PublishedContentTypeFactory factory, SolidP
[Test]
public void Can_Get_Content_For_Populated_Requested_Language()
{
var content = UmbracoContext.Current.ContentCache.GetAtRoot().First();
var content = Current.UmbracoContext.ContentCache.GetAtRoot().First();
var value = content.Value("welcomeText", "en-US");
Assert.AreEqual("Welcome", value);
}

[Test]
public void Can_Get_Content_For_Populated_Requested_Non_Default_Language()
{
var content = UmbracoContext.Current.ContentCache.GetAtRoot().First();
var content = Current.UmbracoContext.ContentCache.GetAtRoot().First();
var value = content.Value("welcomeText", "de");
Assert.AreEqual("Willkommen", value);
}

[Test]
public void Do_Not_Get_Content_For_Unpopulated_Requested_Language_Without_Fallback()
{
var content = UmbracoContext.Current.ContentCache.GetAtRoot().First();
var content = Current.UmbracoContext.ContentCache.GetAtRoot().First();
var value = content.Value("welcomeText", "fr");
Assert.IsNull(value);
}

[Test]
public void Do_Not_Get_Content_For_Unpopulated_Requested_Language_With_Fallback_Unless_Requested()
{
var content = UmbracoContext.Current.ContentCache.GetAtRoot().First();
var content = Current.UmbracoContext.ContentCache.GetAtRoot().First();
var value = content.Value("welcomeText", "es");
Assert.IsNull(value);
}

[Test]
public void Can_Get_Content_For_Unpopulated_Requested_Language_With_Fallback()
{
var content = UmbracoContext.Current.ContentCache.GetAtRoot().First();
var content = Current.UmbracoContext.ContentCache.GetAtRoot().First();
var value = content.Value("welcomeText", "es", fallback: Fallback.ToLanguage);
Assert.AreEqual("Welcome", value);
}

[Test]
public void Can_Get_Content_For_Unpopulated_Requested_Language_With_Fallback_Over_Two_Levels()
{
var content = UmbracoContext.Current.ContentCache.GetAtRoot().First();
var content = Current.UmbracoContext.ContentCache.GetAtRoot().First();
var value = content.Value("welcomeText", "it", fallback: Fallback.To(Fallback.Language, Fallback.Ancestors));
Assert.AreEqual("Welcome", value);
}

[Test]
public void Do_Not_GetContent_For_Unpopulated_Requested_Language_With_Fallback_Over_That_Loops()
{
var content = UmbracoContext.Current.ContentCache.GetAtRoot().First();
var content = Current.UmbracoContext.ContentCache.GetAtRoot().First();
var value = content.Value("welcomeText", "no", fallback: Fallback.ToLanguage);
Assert.IsNull(value);
}

[Test]
public void Do_Not_Get_Content_Recursively_Unless_Requested()
{
var content = UmbracoContext.Current.ContentCache.GetAtRoot().First().Children.First();
var content = Current.UmbracoContext.ContentCache.GetAtRoot().First().Children.First();
var value = content.Value("welcomeText2");
Assert.IsNull(value);
}

[Test]
public void Can_Get_Content_Recursively()
{
var content = UmbracoContext.Current.ContentCache.GetAtRoot().First().Children.First();
var content = Current.UmbracoContext.ContentCache.GetAtRoot().First().Children.First();
var value = content.Value("welcomeText2", fallback: Fallback.ToAncestors);
Assert.AreEqual("Welcome", value);
}

[Test]
public void Do_Not_Get_Content_Recursively_Unless_Requested2()
{
var content = UmbracoContext.Current.ContentCache.GetAtRoot().First().Children.First().Children().First();
var content = Current.UmbracoContext.ContentCache.GetAtRoot().First().Children.First().Children().First();
Assert.IsNull(content.GetProperty("welcomeText2"));
var value = content.Value("welcomeText2");
Assert.IsNull(value);
Expand All @@ -267,7 +267,7 @@ public void Do_Not_Get_Content_Recursively_Unless_Requested2()
[Test]
public void Can_Get_Content_Recursively2()
{
var content = UmbracoContext.Current.ContentCache.GetAtRoot().First().Children.First().Children().First();
var content = Current.UmbracoContext.ContentCache.GetAtRoot().First().Children.First().Children().First();
Assert.IsNull(content.GetProperty("welcomeText2"));
var value = content.Value("welcomeText2", fallback: Fallback.ToAncestors);
Assert.AreEqual("Welcome", value);
Expand All @@ -276,7 +276,7 @@ public void Can_Get_Content_Recursively2()
[Test]
public void Can_Get_Content_Recursively3()
{
var content = UmbracoContext.Current.ContentCache.GetAtRoot().First().Children.First().Children().First();
var content = Current.UmbracoContext.ContentCache.GetAtRoot().First().Children.First().Children().First();
Assert.IsNull(content.GetProperty("noprop"));
var value = content.Value("noprop", fallback: Fallback.ToAncestors);
// property has no value but we still get the value (ie, the converter would do something)
Expand All @@ -287,7 +287,7 @@ public void Can_Get_Content_Recursively3()
public void Can_Get_Content_With_Recursive_Priority()
{
Current.VariationContextAccessor.VariationContext = new VariationContext("nl");
var content = UmbracoContext.Current.ContentCache.GetAtRoot().First().Children.First();
var content = Current.UmbracoContext.ContentCache.GetAtRoot().First().Children.First();

var value = content.Value("welcomeText", "nl", fallback: Fallback.To(Fallback.Ancestors, Fallback.Language));

Expand All @@ -298,7 +298,7 @@ public void Can_Get_Content_With_Recursive_Priority()
[Test]
public void Can_Get_Content_With_Fallback_Language_Priority()
{
var content = UmbracoContext.Current.ContentCache.GetAtRoot().First().Children.First();
var content = Current.UmbracoContext.ContentCache.GetAtRoot().First().Children.First();
var value = content.Value("welcomeText", "nl", fallback: Fallback.ToLanguage);

// No Dutch value is directly assigned. Check has fallen back to English value from language variant.
Expand All @@ -308,14 +308,14 @@ public void Can_Get_Content_With_Fallback_Language_Priority()
[Test]
public void Throws_For_Non_Supported_Fallback()
{
var content = UmbracoContext.Current.ContentCache.GetAtRoot().First().Children.First();
var content = Current.UmbracoContext.ContentCache.GetAtRoot().First().Children.First();
Assert.Throws<NotSupportedException>(() => content.Value("welcomeText", "nl", fallback: Fallback.To(999)));
}

[Test]
public void Can_Fallback_To_Default_Value()
{
var content = UmbracoContext.Current.ContentCache.GetAtRoot().First().Children.First();
var content = Current.UmbracoContext.ContentCache.GetAtRoot().First().Children.First();

// no Dutch value is assigned, so getting null
var value = content.Value("welcomeText", "nl");
Expand All @@ -333,7 +333,7 @@ public void Can_Fallback_To_Default_Value()
[Test]
public void Can_Have_Custom_Default_Value()
{
var content = UmbracoContext.Current.ContentCache.GetAtRoot().First().Children.First();
var content = Current.UmbracoContext.ContentCache.GetAtRoot().First().Children.First();

// HACK: the value, pretend the converter would return something
var prop = content.GetProperty("welcomeText") as SolidPublishedPropertyWithLanguageVariants;
Expand Down
Loading