2021-01-12 16:28:00 +11:00
using System.Collections.Generic ;
using System.Linq ;
using System.Reflection ;
2021-02-04 13:09:28 +11:00
using Microsoft.AspNetCore.Http ;
2021-01-12 16:28:00 +11:00
using Microsoft.AspNetCore.Mvc ;
using Microsoft.AspNetCore.Mvc.Controllers ;
using Microsoft.AspNetCore.Mvc.Infrastructure ;
using Microsoft.AspNetCore.Mvc.ViewEngines ;
2021-02-05 13:14:24 +11:00
using Microsoft.AspNetCore.Routing ;
2021-01-12 16:28:00 +11:00
using Microsoft.Extensions.Logging ;
using Microsoft.Extensions.Logging.Abstractions ;
2021-02-04 13:09:28 +11:00
using Moq ;
2021-01-12 16:28:00 +11:00
using NUnit.Framework ;
2021-02-18 11:06:02 +01:00
using Umbraco.Cms.Core.Web ;
using Umbraco.Cms.Web.Common.Controllers ;
using Umbraco.Cms.Web.Website.Routing ;
2021-01-12 16:28:00 +11:00
using Umbraco.Extensions ;
2021-02-18 11:06:02 +01:00
using static Umbraco . Cms . Core . Constants . Web . Routing ;
2021-01-12 16:28:00 +11:00
2022-06-21 08:09:38 +02:00
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Website.Routing ;
2021-01-12 17:06:37 +11:00
2022-06-21 08:09:38 +02:00
[TestFixture]
public class ControllerActionSearcherTests
{
private ControllerActionDescriptor GetDescriptor < T > ( string action )
= > new ( )
2021-02-05 13:14:24 +11:00
{
2022-06-21 08:09:38 +02:00
ActionName = action ,
ControllerName = ControllerExtensions . GetControllerName < T > ( ) ,
ControllerTypeInfo = typeof ( RenderController ) . GetTypeInfo ( ) ,
DisplayName = $"{ControllerExtensions.GetControllerName<T>()}.{action}" ,
2021-02-05 13:14:24 +11:00
} ;
2021-01-12 16:28:00 +11:00
2022-06-21 08:09:38 +02:00
private IReadOnlyList < ControllerActionDescriptor > GetActionDescriptors ( ) = > new List < ControllerActionDescriptor >
{
GetDescriptor < RenderController > ( nameof ( RenderController . Index ) ) ,
GetDescriptor < Render1Controller > ( nameof ( Render1Controller . Index ) ) ,
GetDescriptor < Render1Controller > ( nameof ( Render1Controller . Custom ) ) ,
GetDescriptor < Render2Controller > ( nameof ( Render2Controller . Index ) ) ,
} ;
2021-01-12 17:06:37 +11:00
2022-06-21 08:09:38 +02:00
private class Render1Controller : ControllerBase , IRenderController
{
public IActionResult Index = > Content ( "hello world" ) ;
2021-01-12 16:28:00 +11:00
2022-06-21 08:09:38 +02:00
public IActionResult Custom = > Content ( "hello world" ) ;
}
private class Render2Controller : RenderController
{
public Render2Controller (
ILogger < Render2Controller > logger ,
ICompositeViewEngine compositeViewEngine ,
IUmbracoContextAccessor umbracoContextAccessor )
: base ( logger , compositeViewEngine , umbracoContextAccessor )
2021-01-12 16:28:00 +11:00
{
}
2022-06-21 08:09:38 +02:00
}
2021-01-12 16:28:00 +11:00
2022-06-21 08:09:38 +02:00
[TestCase("Index", "RenderNotFound", null, false)]
[TestCase("index", "Render", nameof(RenderController.Index), true)]
[TestCase("Index", "Render1", nameof(RenderController.Index), true)]
[TestCase("Index", "render2", nameof(Render2Controller.Index), true)]
[TestCase("NotFound", "Render", nameof(RenderController.Index), true)]
[TestCase("NotFound", "Render1", nameof(Render1Controller.Index), true)]
[TestCase("NotFound", "Render2", nameof(Render2Controller.Index), true)]
[TestCase("Custom", "Render1", nameof(Render1Controller.Custom), true)]
public void Matches_Controller ( string action , string controller , string resultAction , bool matches )
{
var descriptors = GetActionDescriptors ( ) ;
2021-02-04 13:09:28 +11:00
2022-06-21 08:09:38 +02:00
var actionSelector = new Mock < IActionSelector > ( ) ;
actionSelector . Setup ( x = > x . SelectCandidates ( It . IsAny < RouteContext > ( ) ) )
. Returns ( ( RouteContext r ) = >
{
// our own rudimentary search
var controller = r . RouteData . Values [ ControllerToken ] . ToString ( ) ;
var action = r . RouteData . Values [ ActionToken ] . ToString ( ) ;
return descriptors . Where ( x = >
x . ControllerName . InvariantEquals ( controller ) & & x . ActionName . InvariantEquals ( action ) ) . ToList ( ) ;
} ) ;
2021-02-04 13:09:28 +11:00
2022-06-21 08:09:38 +02:00
var query = new ControllerActionSearcher (
new NullLogger < ControllerActionSearcher > ( ) ,
actionSelector . Object ) ;
2021-02-04 13:09:28 +11:00
2022-06-21 08:09:38 +02:00
var httpContext = new DefaultHttpContext ( ) ;
2021-01-12 16:28:00 +11:00
2022-06-21 08:09:38 +02:00
var result = query . Find < IRenderController > ( httpContext , controller , action ) ;
Assert . IsTrue ( matches = = ( result ! = null ) ) ;
if ( matches )
{
Assert . IsTrue ( result . ActionName . InvariantEquals ( resultAction ) , "expected {0} does not match resulting action {1}" , resultAction , result . ActionName ) ;
Assert . IsTrue ( result . ControllerName . InvariantEquals ( controller ) , "expected {0} does not match resulting controller {1}" , controller , result . ControllerName ) ;
2021-01-12 16:28:00 +11:00
}
}
}