removes module injector and other migrated code

This commit is contained in:
Shannon
2021-02-02 18:49:16 +11:00
parent 86fd473018
commit 2dc169457b
7 changed files with 1 additions and 550 deletions

View File

@@ -198,7 +198,6 @@
<Compile Include="Routing\RoutesCacheTests.cs" />
<Compile Include="Routing\UrlRoutingTestBase.cs" />
<Compile Include="Web\PublishedContentQueryTests.cs" />
<Compile Include="Web\Mvc\SurfaceControllerTests.cs" />
<Compile Include="Web\Mvc\MergeParentContextViewDataAttributeTests.cs" />
<Compile Include="Web\Mvc\ViewDataDictionaryExtensionTests.cs" />
<Compile Include="Persistence\Querying\ContentTypeSqlMappingTests.cs" />
@@ -217,7 +216,6 @@
<Compile Include="Routing\ContentFinderByAliasWithDomainsTests.cs" />
<Compile Include="Routing\DomainsAndCulturesTests.cs" />
<Compile Include="Routing\UrlsWithNestedDomains.cs" />
<Compile Include="Web\Controllers\PluginControllerAreaTests.cs" />
<Compile Include="TestHelpers\TestWithDatabaseBase.cs" />
<Compile Include="Routing\ContentFinderByAliasTests.cs" />
<Compile Include="Routing\ContentFinderByIdTests.cs" />

View File

@@ -1,91 +0,0 @@
using System;
using NUnit.Framework;
using Umbraco.Tests.TestHelpers;
using Umbraco.Web;
using Umbraco.Web.Mvc;
namespace Umbraco.Tests.Web.Controllers
{
[TestFixture]
public class PluginControllerAreaTests : BaseWebTest
{
[Test]
public void Ensure_Same_Area1()
{
Assert.Throws<InvalidOperationException>(() =>
new PluginControllerArea(TestObjects.GetGlobalSettings(), HostingEnvironment,
new PluginControllerMetadata[]
{
PluginController.GetMetadata(typeof(Plugin1Controller)),
PluginController.GetMetadata(typeof(Plugin2Controller)),
PluginController.GetMetadata(typeof(Plugin3Controller)) //not same area
}));
}
[Test]
public void Ensure_Same_Area3()
{
Assert.Throws<InvalidOperationException>(() =>
new PluginControllerArea(TestObjects.GetGlobalSettings(), HostingEnvironment,
new PluginControllerMetadata[]
{
PluginController.GetMetadata(typeof(Plugin1Controller)),
PluginController.GetMetadata(typeof(Plugin2Controller)),
PluginController.GetMetadata(typeof(Plugin4Controller)) //no area assigned
}));
}
[Test]
public void Ensure_Same_Area2()
{
var area = new PluginControllerArea(TestObjects.GetGlobalSettings(), HostingEnvironment,
new PluginControllerMetadata[]
{
PluginController.GetMetadata(typeof(Plugin1Controller)),
PluginController.GetMetadata(typeof(Plugin2Controller))
});
Assert.Pass();
}
#region Test classes
[PluginController("Area1")]
public class Plugin1Controller : PluginController
{
public Plugin1Controller(IUmbracoContextAccessor umbracoContextAccessor)
: base(umbracoContextAccessor, null, null, null, null)
{
}
}
[PluginController("Area1")]
public class Plugin2Controller : PluginController
{
public Plugin2Controller(IUmbracoContextAccessor umbracoContextAccessor)
: base(umbracoContextAccessor, null, null, null, null)
{
}
}
[PluginController("Area2")]
public class Plugin3Controller : PluginController
{
public Plugin3Controller(IUmbracoContextAccessor umbracoContextAccessor)
: base(umbracoContextAccessor, null, null, null, null)
{
}
}
public class Plugin4Controller : PluginController
{
public Plugin4Controller(IUmbracoContextAccessor umbracoContextAccessor)
: base(umbracoContextAccessor, null, null, null, null)
{
}
}
#endregion
}
}

View File

@@ -1,221 +0,0 @@
using System;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Moq;
using NUnit.Framework;
using Umbraco.Core.Cache;
using Umbraco.Core.Configuration.Models;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.Services;
using Umbraco.Tests.Common;
using Umbraco.Tests.TestHelpers;
using Umbraco.Tests.Testing;
using Umbraco.Web;
using Umbraco.Web.Mvc;
using Umbraco.Web.PublishedCache;
using Current = Umbraco.Web.Composing.Current;
namespace Umbraco.Tests.Web.Mvc
{
[TestFixture]
[UmbracoTest(WithApplication = true)]
public class SurfaceControllerTests : UmbracoTestBase
{
public override void SetUp()
{
base.SetUp();
Current.UmbracoContextAccessor = new TestUmbracoContextAccessor();
}
[Test]
public void Can_Construct_And_Get_Result()
{
var globalSettings = TestObjects.GetGlobalSettings();
var httpContextAccessor = TestHelper.GetHttpContextAccessor();
var umbracoContextFactory = new UmbracoContextFactory(
Current.UmbracoContextAccessor,
Mock.Of<IPublishedSnapshotService>(),
new TestVariationContextAccessor(),
new TestDefaultCultureAccessor(),
globalSettings,
Mock.Of<IUserService>(),
HostingEnvironment,
UriUtility,
httpContextAccessor,
new AspNetCookieManager(httpContextAccessor));
var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext();
var umbracoContext = umbracoContextReference.UmbracoContext;
var umbracoContextAccessor = new TestUmbracoContextAccessor(umbracoContext);
var ctrl = new TestSurfaceController(umbracoContextAccessor, Mock.Of<IPublishedContentQuery>());
var result = ctrl.Index();
Assert.IsNotNull(result);
}
[Test]
public void Umbraco_Context_Not_Null()
{
var globalSettings = TestObjects.GetGlobalSettings();
var httpContextAccessor = TestHelper.GetHttpContextAccessor();
var umbracoContextFactory = new UmbracoContextFactory(
Current.UmbracoContextAccessor,
Mock.Of<IPublishedSnapshotService>(),
new TestVariationContextAccessor(),
new TestDefaultCultureAccessor(),
globalSettings,
Mock.Of<IUserService>(),
HostingEnvironment,
UriUtility,
httpContextAccessor,
new AspNetCookieManager(httpContextAccessor));
var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext();
var umbCtx = umbracoContextReference.UmbracoContext;
var umbracoContextAccessor = new TestUmbracoContextAccessor(umbCtx);
var ctrl = new TestSurfaceController(umbracoContextAccessor, Mock.Of<IPublishedContentQuery>());
Assert.IsNotNull(ctrl.UmbracoContext);
}
[Test]
public void Can_Lookup_Content()
{
var publishedSnapshot = new Mock<IPublishedSnapshot>();
publishedSnapshot.Setup(x => x.Members).Returns(Mock.Of<IPublishedMemberCache>());
var content = new Mock<IPublishedContent>();
content.Setup(x => x.Id).Returns(2);
var publishedSnapshotService = new Mock<IPublishedSnapshotService>();
var globalSettings = TestObjects.GetGlobalSettings();
var httpContextAccessor = TestHelper.GetHttpContextAccessor();
var umbracoContextFactory = new UmbracoContextFactory(
Current.UmbracoContextAccessor,
publishedSnapshotService.Object,
new TestVariationContextAccessor(),
new TestDefaultCultureAccessor(),
globalSettings,
Mock.Of<IUserService>(),
HostingEnvironment,
UriUtility,
httpContextAccessor,
new AspNetCookieManager(httpContextAccessor));
var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext();
var umbracoContext = umbracoContextReference.UmbracoContext;
var umbracoContextAccessor = new TestUmbracoContextAccessor(umbracoContext);
var publishedContentQuery = Mock.Of<IPublishedContentQuery>(query => query.Content(2) == content.Object);
var ctrl = new TestSurfaceController(umbracoContextAccessor,publishedContentQuery);
var result = ctrl.GetContent(2) as PublishedContentResult;
Assert.IsNotNull(result);
Assert.IsNotNull(result.Content);
Assert.AreEqual(2, result.Content.Id);
}
[Test]
public async Task Mock_Current_Page()
{
var globalSettings = TestObjects.GetGlobalSettings();
var httpContextAccessor = TestHelper.GetHttpContextAccessor();
var umbracoContextFactory = new UmbracoContextFactory(
Current.UmbracoContextAccessor,
Mock.Of<IPublishedSnapshotService>(),
new TestVariationContextAccessor(),
new TestDefaultCultureAccessor(),
globalSettings,
Mock.Of<IUserService>(),
HostingEnvironment,
UriUtility,
httpContextAccessor,
new AspNetCookieManager(httpContextAccessor));
var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext();
var umbracoContext = umbracoContextReference.UmbracoContext;
var umbracoContextAccessor = new TestUmbracoContextAccessor(umbracoContext);
var content = Mock.Of<IPublishedContent>(publishedContent => publishedContent.Id == 12345);
var webRoutingSettings = new WebRoutingSettings();
var publishedRouter = BaseWebTest.CreatePublishedRouter(umbracoContextAccessor, webRoutingSettings);
var frequest = await publishedRouter.CreateRequestAsync(new Uri("http://localhost/test"));
frequest.SetPublishedContent(content);
var routeDefinition = new RouteDefinition
{
PublishedRequest = frequest.Build()
};
var routeData = new RouteData();
routeData.Values.Add(Core.Constants.Web.UmbracoRouteDefinitionDataToken, routeDefinition);
var ctrl = new TestSurfaceController(umbracoContextAccessor, Mock.Of<IPublishedContentQuery>());
ctrl.ControllerContext = new ControllerContext(Mock.Of<HttpContextBase>(), routeData, ctrl);
var result = ctrl.GetContentFromCurrentPage() as PublishedContentResult;
Assert.AreEqual(12345, result.Content.Id);
}
public class TestSurfaceController : SurfaceController
{
private readonly IPublishedContentQuery _publishedContentQuery;
public TestSurfaceController(IUmbracoContextAccessor umbracoContextAccessor, IPublishedContentQuery publishedContentQuery)
: base(umbracoContextAccessor, null, ServiceContext.CreatePartial(), AppCaches.Disabled, null)
{
_publishedContentQuery = publishedContentQuery;
}
public ActionResult Index()
{
// ReSharper disable once Mvc.ViewNotResolved
return View();
}
public ActionResult GetContent(int id)
{
var content = _publishedContentQuery.Content(id);
return new PublishedContentResult(content);
}
public ActionResult GetContentFromCurrentPage()
{
var content = CurrentPage;
return new PublishedContentResult(content);
}
}
public class PublishedContentResult : ActionResult
{
public IPublishedContent Content { get; set; }
public PublishedContentResult(IPublishedContent content)
{
Content = content;
}
public override void ExecuteResult(ControllerContext context)
{
}
}
}
}

View File

@@ -1,55 +0,0 @@
using System.Web;
using Microsoft.Extensions.DependencyInjection;
using Umbraco.Core;
using Umbraco.Core.Composing;
using Umbraco.Core.Exceptions;
namespace Umbraco.Web.Composing
{
/// <summary>
/// Provides a base class for module injectors.
/// </summary>
/// <typeparam name="TModule">The type of the injected module.</typeparam>
public abstract class ModuleInjector<TModule> : IHttpModule
where TModule : class, IHttpModule
{
protected TModule Module { get; private set; }
/// <inheritdoc />
public void Init(HttpApplication context)
{
try
{
// using the service locator here - no other way, really
Module = Current.Factory.GetRequiredService<TModule>();
}
catch
{
// if GetInstance fails, it may be because of a boot error, in
// which case that is the error we actually want to report
IRuntimeState runtimeState = null;
try
{
runtimeState = Current.Factory.GetRequiredService<IRuntimeState>();
}
catch { /* don't make it worse */ }
if (runtimeState?.BootFailedException != null)
BootFailedException.Rethrow(runtimeState.BootFailedException);
// else... throw what we have
throw;
}
// initialize
Module.Init(context);
}
/// <inheritdoc />
public void Dispose()
{
Module?.Dispose();
}
}
}

View File

@@ -1,104 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;
using Umbraco.Core;
using Umbraco.Core.Configuration;
using Umbraco.Core.Composing;
using Umbraco.Core.Configuration.Models;
using Umbraco.Core.Hosting;
using Umbraco.Core.IO;
using Umbraco.Web.WebApi;
namespace Umbraco.Web.Mvc
{
/// <summary>
/// A custom area for controllers that are plugins
/// </summary>
internal class PluginControllerArea : AreaRegistration
{
private readonly GlobalSettings _globalSettings;
private readonly IHostingEnvironment _hostingEnvironment;
private readonly IEnumerable<PluginControllerMetadata> _surfaceControllers;
private readonly IEnumerable<PluginControllerMetadata> _apiControllers;
private readonly string _areaName;
/// <summary>
/// The constructor accepts all types of plugin controllers and will verify that ALL of them have the same areaName assigned to them
/// based on their PluginControllerAttribute. If they are not the same an exception will be thrown.
/// </summary>
/// <param name="globalSettings"></param>
/// <param name="hostingEnvironment"></param>
/// <param name="pluginControllers"></param>
public PluginControllerArea(GlobalSettings globalSettings, IHostingEnvironment hostingEnvironment, IEnumerable<PluginControllerMetadata> pluginControllers)
{
_globalSettings = globalSettings;
_hostingEnvironment = hostingEnvironment;
var controllers = pluginControllers.ToArray();
if (controllers.Any(x => x.AreaName.IsNullOrWhiteSpace()))
{
throw new InvalidOperationException("Cannot create a PluginControllerArea unless all plugin controllers assigned have a PluginControllerAttribute assigned");
}
_areaName = controllers.First().AreaName;
foreach(var c in controllers)
{
if (c.AreaName != _areaName)
{
throw new InvalidOperationException("Cannot create a PluginControllerArea unless all plugin controllers assigned have the same AreaName. The first AreaName found was " + _areaName + " however, the controller of type " + c.GetType().FullName + " has an AreaName of " + c.AreaName);
}
}
//get the controllers
_surfaceControllers = controllers.Where(x => TypeHelper.IsTypeAssignableFrom<SurfaceController>(x.ControllerType));
_apiControllers = controllers.Where(x => TypeHelper.IsTypeAssignableFrom<UmbracoApiController>(x.ControllerType));
}
public override void RegisterArea(AreaRegistrationContext context)
{
MapRouteSurfaceControllers(context.Routes, _surfaceControllers);
MapRouteApiControllers(context.Routes, _apiControllers);
}
public override string AreaName
{
get { return _areaName; }
}
/// <summary>
/// Registers all surface controller routes
/// </summary>
/// <param name="routes"></param>
/// <param name="surfaceControllers"></param>
/// <remarks>
/// The routes will be:
///
/// /Umbraco/[AreaName]/[ControllerName]/[Action]/[Id]
/// </remarks>
private void MapRouteSurfaceControllers(RouteCollection routes, IEnumerable<PluginControllerMetadata> surfaceControllers)
{
foreach (var s in surfaceControllers)
{
var route = this.RouteControllerPlugin(_globalSettings, _hostingEnvironment, s.ControllerName, s.ControllerType, routes, "", "Index", UrlParameter.Optional, "surface");
//set the route handler to our SurfaceRouteHandler
route.RouteHandler = new SurfaceRouteHandler();
}
}
/// <summary>
/// Registers all api controller routes
/// </summary>
/// <param name="routes"></param>
/// <param name="apiControllers"></param>
private void MapRouteApiControllers(RouteCollection routes, IEnumerable<PluginControllerMetadata> apiControllers)
{
foreach (var s in apiControllers)
{
this.RouteControllerPlugin(_globalSettings, _hostingEnvironment, s.ControllerName, s.ControllerType, routes, "", "", UrlParameter.Optional, "api",
isMvc: false,
areaPathPrefix: s.IsBackOffice ? "backoffice" : null);
}
}
}
}

View File

@@ -116,82 +116,8 @@ namespace Umbraco.Web.Runtime
umbracoPath + "/RenderMvc/{action}/{id}",
new { controller = "RenderMvc", action = "Index", id = UrlParameter.Optional }
);
defaultRoute.RouteHandler = new RenderRouteHandler(umbracoContextAccessor, ControllerBuilder.Current.GetControllerFactory(), shortStringHelper);
// register install routes
// RouteTable.Routes.RegisterArea<UmbracoInstallArea>();
// register all back office routes
// RouteTable.Routes.RegisterArea(new BackOfficeArea(globalSettings, hostingEnvironment));
// plugin controllers must come first because the next route will catch many things
RoutePluginControllers(globalSettings, apiControllerTypes, hostingEnvironment);
}
private static void RoutePluginControllers(
GlobalSettings globalSettings,
UmbracoApiControllerTypeCollection apiControllerTypes,
IHostingEnvironment hostingEnvironment)
{
var umbracoPath = globalSettings.GetUmbracoMvcArea(hostingEnvironment);
// need to find the plugin controllers and route them
var pluginControllers = apiControllerTypes; //TODO was: surfaceControllerTypes.Concat(apiControllerTypes).ToArray();
// local controllers do not contain the attribute
var localControllers = pluginControllers.Where(x => PluginController.GetMetadata(x).AreaName.IsNullOrWhiteSpace());
foreach (var s in localControllers)
{
if (TypeHelper.IsTypeAssignableFrom<SurfaceController>(s))
RouteLocalSurfaceController(s, umbracoPath);
else if (TypeHelper.IsTypeAssignableFrom<UmbracoApiController>(s))
RouteLocalApiController(s, umbracoPath);
}
// get the plugin controllers that are unique to each area (group by)
var pluginSurfaceControlleres = pluginControllers.Where(x => PluginController.GetMetadata(x).AreaName.IsNullOrWhiteSpace() == false);
var groupedAreas = pluginSurfaceControlleres.GroupBy(controller => PluginController.GetMetadata(controller).AreaName);
// loop through each area defined amongst the controllers
foreach (var g in groupedAreas)
{
// create & register an area for the controllers (this will throw an exception if all controllers are not in the same area)
var pluginControllerArea = new PluginControllerArea(globalSettings, hostingEnvironment, g.Select(PluginController.GetMetadata));
RouteTable.Routes.RegisterArea(pluginControllerArea);
}
}
private static void RouteLocalApiController(Type controller, string umbracoPath)
{
var meta = PluginController.GetMetadata(controller);
var url = umbracoPath + (meta.IsBackOffice ? "/BackOffice" : "") + "/Api/" + meta.ControllerName + "/{action}/{id}";
var route = RouteTable.Routes.MapHttpRoute(
$"umbraco-api-{meta.ControllerName}",
url, // URL to match
new { controller = meta.ControllerName, id = UrlParameter.Optional },
new[] { meta.ControllerNamespace });
if (route.DataTokens == null) // web api routes don't set the data tokens object
route.DataTokens = new RouteValueDictionary();
// TODO: Pretty sure this is not necessary, we'll see
//route.DataTokens.Add(Core.Constants.Web.UmbracoDataToken, "api"); //ensure the umbraco token is set
}
private static void RouteLocalSurfaceController(Type controller, string umbracoPath)
{
var meta = PluginController.GetMetadata(controller);
var url = umbracoPath + "/Surface/" + meta.ControllerName + "/{action}/{id}";
var route = RouteTable.Routes.MapRoute(
$"umbraco-surface-{meta.ControllerName}",
url, // URL to match
new { controller = meta.ControllerName, action = "Index", id = UrlParameter.Optional },
new[] { meta.ControllerNamespace }); // look in this namespace to create the controller
// TODO: Pretty sure this is not necessary, we'll see
//route.DataTokens.Add(Core.Constants.Web.UmbracoDataToken, "surface"); // ensure the umbraco token is set
route.DataTokens.Add("UseNamespaceFallback", false); // don't look anywhere else except this namespace!
// make it use our custom/special SurfaceMvcHandler
route.RouteHandler = new SurfaceRouteHandler();
}
}
}

View File

@@ -175,7 +175,6 @@
<Compile Include="WebApi\ParameterSwapControllerActionSelector.cs" />
<Compile Include="HttpContextUmbracoContextAccessor.cs" />
<Compile Include="IHttpContextAccessor.cs" />
<Compile Include="Composing\ModuleInjector.cs" />
<Compile Include="WebApi\EnableDetailedErrorsAttribute.cs" />
<Compile Include="WebApi\Filters\FeatureAuthorizeAttribute.cs" />
<Compile Include="WebApi\SessionHttpControllerRouteHandler.cs" />
@@ -224,7 +223,6 @@
<DependentUpon>Strings.resx</DependentUpon>
</Compile>
<Compile Include="Mvc\SurfaceController.cs" />
<Compile Include="Mvc\PluginControllerArea.cs" />
<Compile Include="Mvc\PluginControllerAttribute.cs" />
<Compile Include="Mvc\UmbracoPageResult.cs" />
<Compile Include="RouteCollectionExtensions.cs" />