Tested: Sitecore Wildcards and DMS statistics

With this test we want to check what Sitecore DMS does with Wildcards. How are Wildcard URL’s and shown on the Analytics Reports?

Wildcard items in Sitecore are a convenient way to handle dynamic URLs. They let you pass data through the URL instead of relying on query string values that are appended to the URL. More information about the Wildcards and a introduction to the Wildcard Shared Source Module can be found in this article (written by Adam Conn).

Creating a testing environment

For this testing environment I’ve installed the shared source Wildcard module. After installing the module I opened the Content Editor and created a ‘products’ item, underneath the products item I created a wildcard item (*).

image

Configure the Wildcard Module

Go to /sitecore/system/Modules/Wildcards/. Create a new token called ‘Product Detail’ in the Tokens folder. And create the a new route called ‘Product Route’ within the Routes folder. In the Product Route item add the Wildcard item (*) to the Items field and define the rule in the Rules field.

image

The Wildcard module comes with two Sublayouts; DisplayDynamicUrls and DisplayTokenValues. For this test we are going to use the DisplayDynamicUrls sublayout. We need to alter the GetSampleData() method in DisplayDynamicUrls.ascx.cs. Open the DisplayDynamicUrls.ascx.cs and navigate to the GetSampleData() method and change the code so it will look like the following code:



//The following constants must match tokens defined in Sitecore
const string TOKEN_PRODUCT_DETAIL = "%Product Detail%";

private List GetSampleData()
{
var list = new List();
list.Add(new NameValueCollection { { TOKEN_PRODUCT_DETAIL, "product1" } });
list.Add(new NameValueCollection { { TOKEN_PRODUCT_DETAIL, "product2" } });
list.Add(new NameValueCollection { { TOKEN_PRODUCT_DETAIL, "product3" } });
return list;
}


Now add the DisplayDynamicUrls sublayout to the Layout Details of the products item.

The test

Generate analytics data

Open a new browser, clear all cookies (I use Chrome Incognito Window) and visit the products item. You can see that the DisplayDynamicUrls sublayout generates Sample links based on the Sample data you configured in the GetSampleData() method.

image

Click the Sample links and close the browser.

View the Latest Visit Report

Open the Latest Visits report in the Engagement Analytics and select the latest visit. In the bottom of report you can find the visited pages. You can see that Sitecore has logged the wildcard page visits.

image

The verdict

At this point there is no indication that Wildcards are causing any problems with Sitecore DMS statistics. This is a straight forward simple test without any other complex systems involved, implementing wildcard in combination with DMS on complex systems needs thorough testing before deployment. Consider creating a Proof of Concept before starting to developt your sollution.

Please leave a comment if you have experience with Wildcards and DMS in a more complex scenario.

TypeMock: Mock Unittest examples

In this example I will show how to create a Unit Test with TypeMock.

First I have created some basic dummy classes for the example


public sealed class ServiceFactory
{
    public static ExpireDateService CreateExpireDateService()
    {
        ExpireDateService expireDateService = new ExpireDateService();
        expireDateService.administration = new SqlAdministration("connectionstring");
        return expireDateService;
    }
}

/// <summary>
/// SqlAdministrion creates connection and managed queries with SQL
/// </summary>

public class SqlAdministration
{
    private static DateTime LoadParameterPrivate()
    {
        return DateTime.Parse("01-01-1980");

    }

    public SqlAdministration(string connectionString)
    {
        //Create connection to sql�
    }

    public static DateTime LoadParameter(string expireDateType)
    {
        //GET expireDate from Database:
         SqlAdministratie.LoadParameter("expireDateType");
        // Load Date from Private method for other mocking examples

        return LoadParameterPrivate();
    }
}

public class ExpireDateService
{
    public SqlAdministration administration;

    public DateTime GetDate(string expireDateType)
    {
        DateTime expireDate = SqlAdministration.LoadParameter(expireDateType);

        return expireDate;
    }
}

public class CheckDate
{
    public static bool CheckExpireDateBooking()
    {
        ExpireDateService expireDateService = ServiceFactory.CreateExpireDateService();
        DateTime expireDate = expireDateService.GetDate("expireDateDateFirst");
        return DateTime.Parse("01-01-2000") &lt; expireDate;
    }
}


With the following Unit test I will test the code written above. I don't want to change my code or add code for testing purpose. That's where Mocking comes in. With TypeMock I will mock the outcome of specified methods.

The first example is a standard unit test. No Mocking there.

[code language='c#']
///

/// Checks the expiredate from ‘database’ 01-01-1980
/// with a hardcoded date 01-01-2000
///

[TestMethod()]
public void TestMethodWithoutTypeMock()
{
    Assert.IsFalse(CheckDate.CheckExpireDateBooking());

[/code]

 Now I want to mock the method GetDate to return a date specified by me (01-01-2010).


/// <summary>
/// Checks the expiredate from database (01-01-1980)
/// with a hardcoded date provided by TypeMock (01-01-2010)
/// </summary>

[Isolated]
[TestMethod()]
public void TestMethodWithTypeMockIsolate()
{
    //Create a dummy version of the ExpireDateService object to use for Mocking

    ExpireDateService expireDateService = new ExpireDateService();
    expireDateService.administration = new SqlAdministration("dummyConnectionString");

    //Return the declared expireDateService when method CreateExpireDateService is called

    Isolate.WhenCalled(() =&gt; ServiceFactory.CreateExpireDateService())
        .WillReturn(expireDateService);

    //Isolate the call to method expireDateService.GetDate with parameter 'expireDateDateFirst' and return 01-01-2010

    Isolate.WhenCalled(() =&gt; expireDateService.GetDate("expireDateDateFirst"))
        .WillReturn(DateTime.Parse("01-01-2010"));

    Assert.IsTrue(CheckDate.CheckExpireDateBooking());
}


For the latest example I wanted to Mock a Private method.


/// <summary>
/// Checks the expiredate from database (01-01-1980)
/// with a hardcoded date provided by TypeMock on privatemethod(01-01-2010)
/// </summary>

[Isolated]
[TestMethod()]
public void TestMethodWithTypeMockIsolatePrivate()
{
    Isolate.NonPublic.WhenCalled(typeof(SqlAdministration), "LoadParameterPrivate")
        .WillReturn(DateTime.Parse("01-01-2010"));

    Assert.IsTrue(CheckDate.CheckExpireDateBooking());
} 


You can download the source here:
MockingWithTypeMockExampleSource.zip (44.47 kb)

Hope it helps.

Cheers,

Pieter