private static DnnUrlHelper ArrangeHelper(ModuleInstanceContext expectedContext, string url = null, bool isViewContext = true)
        {
            var mockController    = new Mock <ControllerBase>();
            var mockDnnController = mockController.As <IDnnController>();

            var routeData = new RouteData();

            routeData.Values["controller"] = "bar";
            routeData.Values["action"]     = "foo";
            var context = MockHelper.CreateMockControllerContext(url != null? MockHelper.CreateMockHttpContext(url):null, routeData);

            mockDnnController.Setup(c => c.ModuleContext).Returns(expectedContext);
            mockDnnController.Setup(c => c.ControllerContext).Returns(context);

            if (isViewContext)
            {
                var viewContext = new ViewContext {
                    Controller = mockController.Object
                };

                if (!string.IsNullOrEmpty(url))
                {
                    viewContext.RequestContext = new RequestContext(MockHelper.CreateMockHttpContext(url), routeData);
                }

                return(new DnnUrlHelper(viewContext));
            }

            var requestContext = new RequestContext(MockHelper.CreateMockHttpContext(url ?? "http://base/"), routeData);

            return(new DnnUrlHelper(requestContext, mockDnnController.Object));
        }
        public void HasModuleRequestResult_Returns_False_If_Not_Present()
        {
            // Arrange
            HttpContextBase httpContext = MockHelper.CreateMockHttpContext();

            // Act and Assert
            Assert.IsFalse(httpContext.HasModuleRequestResult());
        }
        public void GetModuleRequestResult_Returns_Null_If_Not_Present()
        {
            //Arrange
            var httpContext = MockHelper.CreateMockHttpContext();

            //Act
            var result = httpContext.GetModuleRequestResult();

            //Assert
            Assert.IsNull(result);
        }
        public void HasModuleRequestResult_Returns_True_If_Present()
        {
            // Arrange
            HttpContextBase httpContext    = MockHelper.CreateMockHttpContext();
            var             expectedResult = new ModuleRequestResult();

            httpContext.Items[HttpContextExtensions.ModuleRequestResultKey] = expectedResult;

            //Act and Assert
            Assert.IsTrue(httpContext.HasModuleRequestResult());
        }
        public void PortalSettings_Property_Is_Null_If_Not_Set_In_Context()
        {
            //Arrange
            HttpContextBase context = MockHelper.CreateMockHttpContext();

            //Act
            var controller = SetupController(context);

            //Assert
            Assert.IsNull(controller.PortalSettings);
        }
        public void User_Property_Is_Null_If_PortalSettings_Not_Set_In_Context()
        {
            //Arrange
            HttpContextBase httpContextBase = MockHelper.CreateMockHttpContext();

            //Act
            var controller = SetupController(httpContextBase);

            //Assert
            Assert.IsNull(controller.User);
        }
        public void View_Returns_DnnViewResult()
        {
            //Arrange
            HttpContextBase httpContextBase = MockHelper.CreateMockHttpContext();

            //Act
            var controller = SetupController(httpContextBase);
            var viewResult = controller.Action1();

            //Assert
            Assert.IsInstanceOf <DnnViewResult>(viewResult);
        }
        public void Initialize_CreatesInstance_Of_DnnUrlHelper()
        {
            // Arrange
            HttpContextBase httpContextBase = MockHelper.CreateMockHttpContext();

            // Act
            var controller = this.SetupController(httpContextBase);

            controller.MockInitialize(httpContextBase.Request.RequestContext);

            // Assert
            Assert.NotNull(controller.Url);
        }
        public void ActionInvoker_InvokeOnExecutedFilters()
        {
            //Arrange
            var httpContextBase = MockHelper.CreateMockHttpContext();
            var controller      = SetupController(httpContextBase);
            var expectedResult  = FakeRedirectAttribute.Result;

            //Act
            _actionInvoker.InvokeAction(controller.ControllerContext, nameof(FakeDnnController.ActionWithOnExecutedFilter));

            //Assert
            Assert.AreEqual(expectedResult, _actionInvoker.ResultOfLastInvoke);
        }
        public void GetModuleRequestResult_Returns_ModuleRequestResult_If_Present()
        {
            //Arrange
            var httpContext    = MockHelper.CreateMockHttpContext();
            var expectedResult = new ModuleRequestResult();

            httpContext.Items[HttpContextExtensions.ModuleRequestResultKey] = expectedResult;

            //Act
            var result = httpContext.GetModuleRequestResult();

            //Assert
            Assert.AreSame(expectedResult, result);
        }
        public void ResultOfLastExecute_Returns_ViewResult()
        {
            //Arrange
            HttpContextBase httpContextBase = MockHelper.CreateMockHttpContext();

            //Act
            var controller = SetupController(httpContextBase);

            controller.ActionInvoker.InvokeAction(controller.ControllerContext, "Action1");

            //Assert
            Assert.NotNull(controller.ResultOfLastExecute);
            Assert.IsInstanceOf <DnnViewResult>(controller.ResultOfLastExecute);
        }
        public void SetModuleRequestResult_Sets_ModuleRequestResult()
        {
            // Arrange
            HttpContextBase context        = MockHelper.CreateMockHttpContext();
            var             expectedResult = new ModuleRequestResult();

            // Act
            context.SetModuleRequestResult(expectedResult);

            // Assert
            var actual = context.Items[HttpContextExtensions.ModuleRequestResultKey];

            Assert.IsNotNull(actual);
            Assert.IsInstanceOf <ModuleRequestResult>(actual);
        }
        public void ActionInvoker_InvokeExceptionFilters_IsExceptionHandled_False()
        {
            //Arrange
            var httpContextBase = MockHelper.CreateMockHttpContext();
            var controller      = SetupController(httpContextBase);

            FakeHandleExceptionRedirectAttribute.IsExceptionHandled = false;
            var expectedResult = FakeRedirectAttribute.Result;

            //Act
            Assert.Throws <Exception>(() => _actionInvoker.InvokeAction(controller.ControllerContext, nameof(FakeDnnController.ActionWithExceptionFilter)));

            //Assert
            Assert.AreEqual(expectedResult, _actionInvoker.ResultOfLastInvoke);
        }
        public void View_Returns_DnnViewResult_With_Correct_ViewName()
        {
            //Arrange
            HttpContextBase httpContextBase = MockHelper.CreateMockHttpContext();

            //Act
            var controller = SetupController(httpContextBase);
            var viewResult = controller.Action1();

            //Assert
            var dnnViewResult = viewResult as DnnViewResult;

            Assert.NotNull(dnnViewResult);
            Assert.AreEqual("Action1", dnnViewResult.ViewName);
        }
        public void InvokeActionResult_Sets_ResultOfLastInvoke()
        {
            //Arrange
            HttpContextBase context = MockHelper.CreateMockHttpContext();

            var controller = new FakeController();

            controller.ControllerContext = new ControllerContext(context, new RouteData(), controller);

            //Act
            _actionInvoker.InvokeAction(controller.ControllerContext, "Index");

            //Assert
            Assert.IsNotNull(_actionInvoker.ResultOfLastInvoke);
            Assert.IsInstanceOf <ViewResult>(_actionInvoker.ResultOfLastInvoke);
        }
        public void View_Returns_DnnViewResult_With_Correct_ViewData()
        {
            //Arrange
            HttpContextBase httpContextBase = MockHelper.CreateMockHttpContext();

            //Act
            var controller = SetupController(httpContextBase);

            controller.ViewData.Add("key", "value");
            var viewResult = controller.Action2();

            //Assert
            var dnnViewResult = viewResult as DnnViewResult;

            Assert.NotNull(dnnViewResult);
            Assert.AreEqual("value", dnnViewResult.ViewData["key"]);
        }
        public void View_Returns_DnnViewResult_With_Correct_ViewEngines()
        {
            //Arrange
            var dog = new Dog()
            {
                Name = "Fluffy"
            };
            HttpContextBase httpContextBase = MockHelper.CreateMockHttpContext();

            //Act
            var controller = SetupController(httpContextBase);
            var viewResult = controller.Action3(dog);

            //Assert
            var dnnViewResult = viewResult as DnnViewResult;

            Assert.NotNull(dnnViewResult);
            Assert.AreEqual(controller.ViewEngineCollection, dnnViewResult.ViewEngineCollection);
        }
        private static ModuleRequestContext CreateModuleContext(string controllerName, string actionName)
        {
            var routeData = new RouteData();

            routeData.Values.Add("controller", controllerName);
            routeData.Values.Add("action", actionName);

            var moduleContext = new ModuleInstanceContext {
                Configuration = new ModuleInfo {
                    ModuleID = 42
                }
            };

            return(new ModuleRequestContext
            {
                HttpContext = MockHelper.CreateMockHttpContext("http://localhost/Portal/Page/ModuleRoute"),
                RouteData = routeData,
                ModuleContext = moduleContext
            });
        }