using System; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Routing; using System.Web.Security; using Moq; using NUnit.Framework; using Umbraco.Core.Cache; using Umbraco.Core.Composing; using Umbraco.Core.Configuration.UmbracoSettings; using Umbraco.Core.Dictionary; using Umbraco.Core.Logging; using Umbraco.Core.Models.PublishedContent; using Umbraco.Core.Services; using Umbraco.Tests.TestHelpers; using Umbraco.Tests.TestHelpers.Stubs; using Umbraco.Tests.Testing; using Umbraco.Tests.Testing.Objects.Accessors; using Umbraco.Web; using Umbraco.Web.Mvc; using Umbraco.Web.PublishedCache; using Umbraco.Web.Routing; using Umbraco.Web.Security; 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 umbracoContextFactory = new UmbracoContextFactory( Current.UmbracoContextAccessor, Mock.Of(), new TestVariationContextAccessor(), new TestDefaultCultureAccessor(), TestObjects.GetUmbracoSettings(), globalSettings, Enumerable.Empty(), Mock.Of()); var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of()); var umbracoContext = umbracoContextReference.UmbracoContext; var ctrl = new TestSurfaceController(umbracoContext); var result = ctrl.Index(); Assert.IsNotNull(result); } [Test] public void Umbraco_Context_Not_Null() { var globalSettings = TestObjects.GetGlobalSettings(); var umbracoContextFactory = new UmbracoContextFactory( Current.UmbracoContextAccessor, Mock.Of(), new TestVariationContextAccessor(), new TestDefaultCultureAccessor(), TestObjects.GetUmbracoSettings(), globalSettings, Enumerable.Empty(), Mock.Of()); var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of()); var umbCtx = umbracoContextReference.UmbracoContext; var ctrl = new TestSurfaceController(umbCtx); Assert.IsNotNull(ctrl.UmbracoContext); } [Test] public void Can_Lookup_Content() { var publishedSnapshot = new Mock(); publishedSnapshot.Setup(x => x.Members).Returns(Mock.Of()); var content = new Mock(); content.Setup(x => x.Id).Returns(2); var publishedSnapshotService = new Mock(); var globalSettings = TestObjects.GetGlobalSettings(); var umbracoContextFactory = new UmbracoContextFactory( Current.UmbracoContextAccessor, publishedSnapshotService.Object, new TestVariationContextAccessor(), new TestDefaultCultureAccessor(), Mock.Of(section => section.WebRouting == Mock.Of(routingSection => routingSection.UrlProviderMode == "Auto")), globalSettings, Enumerable.Empty(), Mock.Of()); var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of()); var umbracoContext = umbracoContextReference.UmbracoContext; var helper = new UmbracoHelper( umbracoContext, Mock.Of(), Mock.Of(), Mock.Of(), Mock.Of(query => query.Content(2) == content.Object), new MembershipHelper(new TestUmbracoContextAccessor(umbracoContext), Mock.Of(), Mock.Of(), Mock.Of(), Mock.Of(), Mock.Of(), Mock.Of(), Mock.Of(), Mock.Of())); var ctrl = new TestSurfaceController(umbracoContext, helper); var result = ctrl.GetContent(2) as PublishedContentResult; Assert.IsNotNull(result); Assert.IsNotNull(result.Content); Assert.AreEqual(2, result.Content.Id); } [Test] public void Mock_Current_Page() { var webRoutingSettings = Mock.Of(section => section.UrlProviderMode == "Auto"); var globalSettings = TestObjects.GetGlobalSettings(); var umbracoContextFactory = new UmbracoContextFactory( Current.UmbracoContextAccessor, Mock.Of(), new TestVariationContextAccessor(), new TestDefaultCultureAccessor(), Mock.Of(section => section.WebRouting == webRoutingSettings), globalSettings, Enumerable.Empty(), Mock.Of()); var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of()); var umbracoContext = umbracoContextReference.UmbracoContext; var content = Mock.Of(publishedContent => publishedContent.Id == 12345); var contextBase = umbracoContext.HttpContext; var publishedRouter = BaseWebTest.CreatePublishedRouter(TestObjects.GetUmbracoSettings().WebRouting); var frequest = publishedRouter.CreateRequest(umbracoContext, new Uri("http://localhost/test")); frequest.PublishedContent = content; var routeDefinition = new RouteDefinition { PublishedRequest = frequest }; var routeData = new RouteData(); routeData.DataTokens.Add(Core.Constants.Web.UmbracoRouteDefinitionDataToken, routeDefinition); var ctrl = new TestSurfaceController(umbracoContext, new UmbracoHelper()); ctrl.ControllerContext = new ControllerContext(contextBase, routeData, ctrl); var result = ctrl.GetContentFromCurrentPage() as PublishedContentResult; Assert.AreEqual(12345, result.Content.Id); } public class TestSurfaceController : SurfaceController { public TestSurfaceController(UmbracoContext ctx, UmbracoHelper helper = null) : base(ctx, null, ServiceContext.CreatePartial(), Mock.Of(), null, null, helper) { } public ActionResult Index() { return View(); } public ActionResult GetContent(int id) { var content = Umbraco.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) { } } } }