Framework/Resources: Fixing ResourceManager code styling and failing ResourceManagerTests

PR #8177 (fix for #8176) added new dependencies to ResourceManager, whose stubs need to be registered for unit tests.
This commit is contained in:
Lombiq
2019-08-19 12:59:53 +02:00
committed by Benedek Farkas
parent 04ae8096de
commit 8c5d3b6fc7
2 changed files with 25 additions and 31 deletions

View File

@@ -1,11 +1,9 @@
using System;
using System.Linq;
using System.Collections.Generic;
using Autofac;
using NUnit.Framework;
using Orchard.DisplayManagement.Implementation;
using Orchard.Mvc;
using Orchard.Tests.Stubs;
using Orchard.UI.Admin;
using Orchard.UI.Resources;
namespace Orchard.Tests.UI.Resources {
@@ -20,7 +18,7 @@ namespace Orchard.Tests.UI.Resources {
public Action<ResourceManifest> DefineManifest { get; set; }
public TestManifestProvider() {
}
public void BuildManifests(ResourceManifestBuilder builder) {
var manifest = builder.Add();
@@ -40,6 +38,8 @@ namespace Orchard.Tests.UI.Resources {
[SetUp]
public void Init() {
var builder = new ContainerBuilder();
builder.RegisterType<StubWorkContextAccessor>().As<IWorkContextAccessor>();
builder.RegisterType<StubHttpContextAccessor>().As<IHttpContextAccessor>();
builder.RegisterType<ResourceManager>().As<IResourceManager>();
builder.RegisterType<TestManifestProvider>().As<IResourceManifestProvider>().SingleInstance();
_container = builder.Build();

View File

@@ -2,14 +2,12 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Autofac.Features.Metadata;
using Orchard.Environment;
using Orchard.Environment.Extensions.Models;
using Orchard.Mvc;
using Orchard.Settings;
@@ -23,8 +21,8 @@ namespace Orchard.UI.Resources {
};
private readonly Dictionary<string, IList<ResourceRequiredContext>> _builtResources = new Dictionary<string, IList<ResourceRequiredContext>>(StringComparer.OrdinalIgnoreCase);
private readonly IEnumerable<Meta<IResourceManifestProvider>> _providers;
private readonly Work<WorkContext> _workContext;
private readonly Work<IHttpContextAccessor> _httpContextAccessor;
private readonly Lazy<IWorkContextAccessor> _wcaLazy;
private readonly Lazy<IHttpContextAccessor> _hcaLazy;
private ResourceManifest _dynamicManifest;
private List<String> _headScripts;
private List<String> _footScripts;
@@ -32,6 +30,17 @@ namespace Orchard.UI.Resources {
private const string NotIE = "!IE";
public ResourceManager(
IEnumerable<Meta<IResourceManifestProvider>> resourceProviders,
Lazy<IWorkContextAccessor> wcaLazy,
Lazy<IHttpContextAccessor> hcaLazy) {
_providers = resourceProviders;
_wcaLazy = wcaLazy;
_hcaLazy = hcaLazy;
}
private static string ToAppRelativePath(string resourcePath) {
if (!String.IsNullOrEmpty(resourcePath) && !Uri.IsWellFormedUriString(resourcePath, UriKind.Absolute)) {
resourcePath = VirtualPathUtility.ToAppRelative(resourcePath);
@@ -75,14 +84,14 @@ namespace Orchard.UI.Resources {
}
var tagBuilder = GetTagBuilder(resource, url);
if (attributes != null) {
// todo: try null value
tagBuilder.MergeAttributes(attributes, true);
}
writer.WriteLine(tagBuilder.ToString(resource.TagRenderMode));
if (!string.IsNullOrEmpty(condition)) {
if (condition == NotIE) {
writer.WriteLine("<!--<![endif]-->");
@@ -93,15 +102,6 @@ namespace Orchard.UI.Resources {
}
}
public ResourceManager(IEnumerable<Meta<IResourceManifestProvider>> resourceProviders,
Work<WorkContext> workContext,
Work<IHttpContextAccessor> httpContextAccessor
) {
_providers = resourceProviders;
_workContext = workContext;
_httpContextAccessor = httpContextAccessor;
}
public IEnumerable<IResourceManifest> ResourceProviders {
get {
if (_manifests == null) {
@@ -328,7 +328,7 @@ namespace Orchard.UI.Resources {
}
var index = meta.Name ?? meta.HttpEquiv;
if (String.IsNullOrEmpty(index)) {
return;
}
@@ -342,8 +342,8 @@ namespace Orchard.UI.Resources {
private string GetResourceKey(string releasePath, string debugPath) {
bool debugMode;
var site = _workContext.Value.CurrentSite;
switch (site.ResourceDebugMode) {
switch (_wcaLazy.Value.GetContext().CurrentSite.ResourceDebugMode) {
case ResourceDebugMode.Enabled:
debugMode = true;
break;
@@ -351,17 +351,11 @@ namespace Orchard.UI.Resources {
debugMode = false;
break;
default:
Debug.Assert(site.ResourceDebugMode == ResourceDebugMode.FromAppSetting, "Unknown ResourceDebugMode value.");
var context = _httpContextAccessor.Value.Current();
debugMode = context != null && context.IsDebuggingEnabled;
debugMode = _hcaLazy.Value.Current()?.IsDebuggingEnabled ?? false;
break;
}
if (debugMode && !string.IsNullOrWhiteSpace(debugPath)) {
return debugPath;
}
else {
return releasePath;
}
return debugMode && !string.IsNullOrWhiteSpace(debugPath) ? debugPath : releasePath;
}
}
}