Click here to Skip to main content
15,885,674 members
Articles / Web Development / ASP.NET

Unit Testing a DotNetNuke Private Assembly Module

Rate me:
Please Sign up or sign in to vote.
4.69/5 (10 votes)
18 Jun 2007CPOL18 min read 83.1K   709   35  
This article describes how to create unit tests that will test out code for a DotNetNuke custom module.
/*
 * 
 * iFinity Smart Business Solutions - http://www.ifinity.com.au
 * Copyright (c) 2007
  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 * documentation files (the "Software"), the rights to use, copy, modify, merge, publish, distribute, sublicense
 * copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the 
 * following conditions:

 * 1. The above copyright notice and this permission notice shall be included in all copies or substantial portions 
 *    of the Software.  
 * 2. The software may not be claimed as the original work and may not be sold as a stand alone product

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 * 
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using DotNetNuke.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace iFinity.DNN.Utilities.Testing
{
    public class DnnUnitTest
    {
        SqlDataProvider _sqlProvider;
        TestContext testContextInstance;
        int _portalId;
        public TestContext TestContext
        {
            get { return testContextInstance; }
            set { testContextInstance = value; }
        }
        public int PortalId
        {
            get
            {
                return _portalId;
            }
        }
        public DnnUnitTest(int portalID)
        {
            DnnUnitTestSection dnnUnitTest = (DnnUnitTestSection)ConfigurationManager.GetSection("iFinity/dnnUnitTest");
            if (dnnUnitTest == null)
                throw new System.Configuration.ConfigurationErrorsException("The iFinity/dnnUnitTest section was not found in the config file");
            UnitTestHelper.SetHttpContextWithSimulatedRequest(dnnUnitTest.SimulatedServer, dnnUnitTest.AppName, dnnUnitTest.AppPath, dnnUnitTest.SimulatedPage);
            DotNetNuke.Common.Globals.ServerName = dnnUnitTest.SimulatedServer;
            DotNetNuke.Common.Globals.HostMapPath = dnnUnitTest.HostMapPath;
            DotNetNuke.Common.Globals.ApplicationPath = dnnUnitTest.AppPath;
            DotNetNuke.Entities.Portals.PortalAliasInfo objPortalAliasInfo = new DotNetNuke.Entities.Portals.PortalAliasInfo();
            objPortalAliasInfo.PortalID = portalID;
            DotNetNuke.Entities.Portals.PortalSettings ps = new DotNetNuke.Entities.Portals.PortalSettings(-1, objPortalAliasInfo);
            System.Web.HttpContext.Current.Items.Add("PortalSettings", ps);
            _sqlProvider = new SqlDataProvider();
            _portalId = portalID;
        }
    }
    public class DnnUnitTestSection : ConfigurationSection
    {
        [ConfigurationProperty("hostMapPath", DefaultValue = "C:\\DotNetNuke", IsRequired = true)]
        public string HostMapPath
        {
            get { return (string)this["hostMapPath"]; }
            set { this["hostMapPath"] = value; }
        }
        [ConfigurationProperty("appName", DefaultValue = "DotNetNuke", IsRequired = true)]
        public string AppName
        {
            get { return (string)this["appName"]; }
            set { this["appName"] = value; }
        }
        [ConfigurationProperty("appPath", DefaultValue = "C:\\DotNetNuke", IsRequired = true)]
        public string AppPath
        {
            get { return (string)this["appPath"]; }
            set { this["appPath"] = value; }
        }
        [ConfigurationProperty("simulatedServer", DefaultValue = "localhost", IsRequired = true)]
        public string SimulatedServer
        {
            get { return (string)this["simulatedServer"]; }
            set { this["simulatedServer"] = value; }
        }
        [ConfigurationProperty("simulatedPage", DefaultValue = "Default.aspx", IsRequired = true)]
        public string SimulatedPage
        {
            get { return (string)this["simulatedPage"]; }
            set { this["simulatedPage"] = value; }
        }

    }

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Product Manager DNN Corp
Australia Australia
Bruce Chapman is the Product Manager for Cloud Services at DNN. He’s been an active member of the DNN Community since 2006 as a contributor, vendor and now employee of DNN Corp.

You can read his blog at http://dnnsoftware.com/blog or follow him on Twitter @brucerchapman

Comments and Discussions