How to Test PageMode.IsExperienceEditorEditing in Sitecore

If you are passionate about testing automation in Sitecore, sooner or later you will want to mock Context.PageMode.IsExperienceEditorEditing (or Context.PageMode.IsPageEditorEditing in pre-Sitecore 8) properties. This post will give you step-by-step instructions on how to do that using Sitecore.FakeDb.

First of all, you need to define a “shell” site in the app.config file.

<sites>
    <site name="shell" domain="sitecore"></site>
</sites>

SiteContextFactory.Sites.Add() method has no use here. Sitecore uses the lower level API SiteManager.GetSite(“shell”), so the “shell” site has to be set up in app.config or you need to use a custom provider in SiteManager.

After finishing with the “shell” site, you need to take care of the current context site. It can be set up using standard Sitecore.FakeDb SiteContextSwitcher helper. There are only two essential attributes to set up – enableWebEdit and masterDatabase.

var fakeSiteContext = new FakeSiteContext(
new StringDictionary
{
    {"enableWebEdit", "true"},
    {"masterDatabase", "master"}
});
var siteContextSwitcher = new SiteContextSwitcher(fakeSiteContext);

Now you are ready for the last step – switch site mode. You need to call Context.Site.SetDisplayMode method for that:

Context.Site.SetDisplayMode(DisplayMode.Edit, DisplayModeDuration.Remember);

Now you have everything you need and can assert that. Below is a full test listing using NUnit and FluentAssertions:

[Test]
public void TestIsPageEditorEditing()
{
    //arrange
    var fakeSiteContext = new Sitecore.FakeDb.Sites.FakeSiteContext(
        new Sitecore.Collections.StringDictionary
        {
            {"enableWebEdit", "true"},
            {"masterDatabase", "master"},
        });

    using (new Sitecore.Sites.SiteContextSwitcher(fakeSiteContext))
    {
        //act
        Sitecore.Context.Site.SetDisplayMode(DisplayMode.Edit, DisplayModeDuration.Remember);

        //assert
        Assert.IsTrue(Sitecore.Context.PageMode.IsPageEditorEditing);

        //Available only in Sitecore 8+
        Assert.IsTrue(Sitecore.Context.PageMode.IsExperienceEditorEditing);
    }
}

You also can find full code for the solution in GitHub. Happy testing!

Dmitry Harnitski

I am an Architect and Software Developer with over 15 years of experience building solutions for the web using .NET, JavaScript and Sitecore. I joined BrainJocks in 2013 to deliver custom Sitecore solutions and, since then, I’ve had the opportunity to design and build enterprise platforms for several major clients. I’m an active contributor to BrainJocks SCORE and an early adopter of cutting edge technologies. I am a loyal fan of continuous integration, unit testing and Algolia. Learn more about Dmitry.

1 comment on How to Test PageMode.IsExperienceEditorEditing in Sitecore

Sergey IvanovJanuary 30, 2017 - Reply

Thanks Dmitry a lot!
It helped me in unit testing not once.

Add a Comment

Your email address will not be published. Required fields are marked *

Or request call back