From b8575ef158dc0578499f7a73afab5d5d0456af00 Mon Sep 17 00:00:00 2001 From: Shannon Date: Tue, 17 Jun 2014 18:53:23 +1000 Subject: [PATCH] Started writing test implementation to mimic what developers might test in their controllers to expose the pitfalls of umbraco testing so we can make this simpler - identifying what needs to be public and how to re-strcuture some objects constructors, etc... to simplify and make this possible. --- .../Mvc/SurfaceControllerTests.cs | 151 ++++++++++++++++++ .../TestHelpers/DisposableUmbracoTest.cs | 52 ++++++ src/Umbraco.Tests/Umbraco.Tests.csproj | 4 +- src/Umbraco.Web/Properties/AssemblyInfo.cs | 2 + 4 files changed, 208 insertions(+), 1 deletion(-) create mode 100644 src/Umbraco.Tests/Mvc/SurfaceControllerTests.cs create mode 100644 src/Umbraco.Tests/TestHelpers/DisposableUmbracoTest.cs diff --git a/src/Umbraco.Tests/Mvc/SurfaceControllerTests.cs b/src/Umbraco.Tests/Mvc/SurfaceControllerTests.cs new file mode 100644 index 0000000000..572ff5aa92 --- /dev/null +++ b/src/Umbraco.Tests/Mvc/SurfaceControllerTests.cs @@ -0,0 +1,151 @@ +using System.CodeDom; +using System.Web; +using System.Web.Mvc; +using Moq; +using NUnit.Framework; +using Umbraco.Core; +using Umbraco.Core.Models; +using Umbraco.Core.ObjectResolution; +using Umbraco.Tests.TestHelpers; +using Umbraco.Web; +using Umbraco.Web.Mvc; +using Umbraco.Web.PublishedCache; + +namespace Umbraco.Tests.Mvc +{ + [TestFixture] + public class SurfaceControllerTests + { + [Test] + public void Can_Construct_And_Get_Result() + { + var appCtx = new ApplicationContext(CacheHelper.CreateDisabledCacheHelper()); + ApplicationContext.EnsureContext(appCtx, true); + + var umbCtx = UmbracoContext.EnsureContext( + new Mock().Object, + appCtx, + true); + + var ctrl = new TestSurfaceController(umbCtx); + + var result = ctrl.Index(); + + Assert.IsNotNull(result); + } + + [Test] + public void Umbraco_Context_Not_Null() + { + var appCtx = new ApplicationContext(CacheHelper.CreateDisabledCacheHelper()); + ApplicationContext.EnsureContext(appCtx, true); + + var umbCtx = UmbracoContext.EnsureContext( + new Mock().Object, + appCtx, + true); + + var ctrl = new TestSurfaceController(umbCtx); + + Assert.IsNotNull(ctrl.UmbracoContext); + } + + [Test] + public void Umbraco_Helper_Not_Null() + { + var appCtx = new ApplicationContext(CacheHelper.CreateDisabledCacheHelper()); + ApplicationContext.EnsureContext(appCtx, true); + + var umbCtx = UmbracoContext.EnsureContext( + new Mock().Object, + appCtx, + true); + + var ctrl = new TestSurfaceController(umbCtx); + + Assert.IsNotNull(ctrl.Umbraco); + } + + [Test] + public void Can_Lookup_Content() + { + //init app context + + var appCtx = new ApplicationContext(CacheHelper.CreateDisabledCacheHelper()); + + //TODO: Need to either make this public or make all methods on the UmbracoHelper or + // in v7 the PublishedContentQuery object virtual so we can just mock the methods + + var contentCaches = new Mock(); + + //init content resolver + //TODO: This is not public so people cannot actually do this! + + PublishedCachesResolver.Current = new PublishedCachesResolver(contentCaches.Object); + + //init umb context + + var umbCtx = UmbracoContext.EnsureContext( + new Mock().Object, + appCtx, + true); + + //setup the mock + + contentCaches.Setup(caches => caches.CreateContextualContentCache(It.IsAny())) + .Returns(new ContextualPublishedContentCache( + Mock.Of(cache => + cache.GetById(It.IsAny(), false, It.IsAny()) == + //return mock of IPublishedContent for any call to GetById + Mock.Of(content => content.Id == 2)), + umbCtx)); + + + + + + using (var uTest = new DisposableUmbracoTest(appCtx)) + { + var ctrl = new TestSurfaceController(uTest.UmbracoContext); + var result = ctrl.GetContent(2) as PublishedContentResult; + + Assert.IsNotNull(result); + Assert.AreEqual(2, result.Content.Id); + } + } + + public class TestSurfaceController : SurfaceController + { + public TestSurfaceController(UmbracoContext umbracoContext) : base(umbracoContext) + { + } + + public ActionResult Index() + { + return View(); + } + + public ActionResult GetContent(int id) + { + var content = Umbraco.TypedContent(id); + + 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) + { + } + + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Tests/TestHelpers/DisposableUmbracoTest.cs b/src/Umbraco.Tests/TestHelpers/DisposableUmbracoTest.cs new file mode 100644 index 0000000000..e6de8fb8f3 --- /dev/null +++ b/src/Umbraco.Tests/TestHelpers/DisposableUmbracoTest.cs @@ -0,0 +1,52 @@ +using System.Web; +using Moq; +using Umbraco.Core; +using Umbraco.Core.ObjectResolution; +using Umbraco.Web; + +namespace Umbraco.Tests.TestHelpers +{ + //NOTE: This is just a POC! Looking at the simplest way to expose some code so people can very easily test + // their Umbraco controllers, etc.... + public class DisposableUmbracoTest : DisposableObject + { + public ApplicationContext ApplicationContext { get; set; } + public UmbracoContext UmbracoContext { get; set; } + + public DisposableUmbracoTest(ApplicationContext applicationContext) + { + //init umb context + var umbctx = UmbracoContext.EnsureContext( + new Mock().Object, + applicationContext, + true); + + Init(applicationContext, umbctx); + } + + public DisposableUmbracoTest(ApplicationContext applicationContext, UmbracoContext umbracoContext) + { + Init(applicationContext, umbracoContext); + } + + private void Init(ApplicationContext applicationContext, UmbracoContext umbracoContext) + { + TestHelper.SetupLog4NetForTests(); + + ApplicationContext = applicationContext; + UmbracoContext = umbracoContext; + + ApplicationContext.Current = applicationContext; + UmbracoContext.Current = umbracoContext; + + Resolution.Freeze(); + } + + protected override void DisposeResources() + { + ApplicationContext.Current = null; + UmbracoContext.Current = null; + Resolution.Reset(); + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index 97e3d53b61..b7c5d9e9a1 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -1,4 +1,4 @@ - + Debug @@ -184,6 +184,7 @@ + @@ -417,6 +418,7 @@ + diff --git a/src/Umbraco.Web/Properties/AssemblyInfo.cs b/src/Umbraco.Web/Properties/AssemblyInfo.cs index 7dc3b796df..ad54647119 100644 --- a/src/Umbraco.Web/Properties/AssemblyInfo.cs +++ b/src/Umbraco.Web/Properties/AssemblyInfo.cs @@ -34,3 +34,5 @@ using System.Security; [assembly: InternalsVisibleTo("umbraco.webservices")] [assembly: InternalsVisibleTo("Concorde.Sync")] [assembly: InternalsVisibleTo("Umbraco.Belle")] + +[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")] \ No newline at end of file