Reinoud van Dalen

October 07, 2016

Sitecore Approve with Test picks the wrong version

I’m blogging about this just in case someone else runs into this problem and for future reference in case I run into it again. If you have more than 2 versions of a page in a language other than the default ‘en’ and you select the ‘Approve with Test’ option, then the wrong version to compare to is selected.

Come again? What seems to be the problem here?

Sitecore has a nifty feature which allows you to easily test new page versions against the current one. When you apply the default workflow then it’s not only a matter of Approving or Rejecting a new version, you can also ‘Approve with Test’. That’s really handy! But sadly we found out this feature is bugged in a certain scenario.

It happens when you are working with a language other than the default 'en' and have more than 2 versions but it’s probably the easiest to show you how you can reproduce this problem. So here’s what I did: First I created a fresh install of Sitecore 8.2-Initial and added the Dutch language.

add the Dutch language

Create Sample Item with 2 Dutch versions

Next I inserted a page under the default Home item in the Dutch language, opened it in the Experience Editor and set the title as follows:

Create a Sample Item

Then submitted and approved the item which makes is visible for visitors. After that I created another Dutch version in the Content Editor and opened it up in the Experience Editor again. I changed the title to v2 and repeated the steps to submit and approve it.

Add a version

Change v2 title

Add a Page test against a new page version

Still having version 2 open in the Experience Editor I clicked the Page test button in the Optimization tab which starts a new Page Test. The current item is used as the original and you can select one or more pages to test against (as shown in the picture below). In this case I opted to test against a New page version and opened the new version in the page editor afterwards.

Start Page Test

Test against new version

I then changed the title to v3, saved it and pressed Submit in the workflow message. By now we have created 3 versions of the page, version 2 is published and we want to test it against version 3. But if we click ‘Approve with Test’ (which should result in the desired test) then you’ll see that the first version is selected.

It’s not really clear on the picture, but if you open the zoomed version then you’ll see that version 1 is selected to Test against.

Preview and Start Test

For some reason the info box doesn’t work. Might be my installation, might be a bug in 8.2-initial. But my point is proven. The wrong item to test against is selected.

The VersionHelper doesn't help

In my mission to find the source of the problem I found out, or actually an ex-colleague of mine (Arjen van Veen), that a Sitecore has a VersionHelper class with GetLatestPublishdVersion method which doesn’t take the current language into account, whoops.

How to fix?

This method is used in several locations but one is of particular interest to us: The GetPageVersionTestCandidates processor. This processor is responsible for selecting the right item to test against and it uses the bugged VersionHelper. The fix requires 2 steps.

Create a CustomVersionHelper

Code is not much different from the original. The only thing we added was getting the item language and using it to get it from other databases.

public class CustomVersionHelper
{
    public static int GetLatestPublishedVersion(Item item)
    {
        Assert.ArgumentNotNull((object)item, "item");
        Database database1 = Context.ContentDatabase ?? Context.Database;
        int num = -1;
        ID id = item.ID;
        Language language = item.Language;
        foreach (Database database2 in Factory.GetDatabases())
        {
            if (database1 != null && database2.Name != database1.Name)
            {
                Item obj = database2.GetItem(id, language);
                if (obj != null && obj.Version.Number > num)
                    num = obj.Version.Number;
            }
        }
        return num;
    }
}

Create a CustomGetPageVersionTestCandidates and override the original in config

I actually really like the fact that the original processor has many virtual methods, only to just public! This makes this class really small and only affects the part we need to:

public class CustomGetPageVersionTestCandidates : GetPageVersionTestCandidates
{
    protected override int GetLatestPublishedVersion(Item item)
    {
        Assert.ArgumentNotNull(item, "item");
        return CustomVersionHelper.GetLatestPublishedVersion(item);
    }
}

Next the only thing left to do is add to include a patch config:

Fixed!

Now if you select the ‘Approve with Test’ option the right version will be found and used!

Hope this saves anyone else’s day.

TAGS: sitecore xdb contenttesting


Comments
Oliver Raduner

Thank you, Reinoud, for sharing this bug! Have you addressed it to Sitecore Support and is there a Hotfix available already or in production? Can you keep posted this article, in case in a future Release they officially fix the current faulty behaviour? Thanks, Oliver


Reinoud van Dalen

Hi Oliver, yes I've addressed it to Sitecore Support. There is no hotfix, but it is filed as bug and is on their backlog.