Skip to main content

Devops

How to Test PageMode.IsExperienceEditorEditing in Sitecore

Office@1x.jpg

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!

Thoughts on “How to Test PageMode.IsExperienceEditorEditing in Sitecore”

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Brian Beckham

As a Sitecore MVP, Brian spends most of his time consulting and architecting software solutions for enterprise-level Sitecore projects.

More from this Author

Follow Us
TwitterLinkedinFacebookYoutubeInstagram