Help shape the future of CMS PaaS release notes! Take this quick survey and share your feedback. 

Minesh Shah (Netcel)
Mar 27, 2023
  1792
(1 votes)

How to Write an xUnit Test to Verify Unique Content Type Guids in Content Management

When developing an Optimizely CMS solution, it is important to ensure that each content type has a unique GUID. If two or more content types share the same GUID, the CMS can have unexpected behavior and issues. In this blog post, we will explore how to write an xUnit test to verify that all Content Type Guids are unique in an Content Management solution.

The Problem

When developing an Optimizely CMS solution, it is common to define content types using C# classes that implement the IContentData interface. Each content type is typically decorated with the ContentType attribute, which defines properties such as the name and GUID of the content type.

When multiple content types share the same GUID, it can cause unexpected behavior and issues in the CMS. For example, if two content types have the same GUID, it can cause an exception to be thrown when attempting to create a new instance of one of the content types.

The Solution

To verify that all Content Type Guids are unique in an Optimizely Content Management solution, we can write an xUnit test that iterates through all the content types in the solution and checks that each content type has a unique GUID. Here is the code for the test:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using Xunit;

namespace YourProject.Tests
{
    public class ContentTypesGuidTest
    {
        [Fact]
        public void VerifyContentTypesGuidsAreUnique()
        {
             // Get the assembly that contains the Project.Cms.Domain project
             var domainAssembly = Assembly.Load("Project.Cms.Domain");

            // Get all content types in the solution, this covers Pages, Blocks and Assets
            var contentTypes = typeof(PageData).Assembly.GetTypes()
                .Where(x => typeof(IContentData).IsAssignableFrom(x) && !x.IsAbstract);

            // Create a dictionary to hold the Guids and their counts
            var guidDictionary = new Dictionary<Guid, int>();

            // Iterate through all the content types
            foreach (var contentType in contentTypes)
            {
                // Get the SiteContentType attribute of the content type
                var siteContentTypeAttribute = contentType.GetCustomAttribute<SiteContentTypeAttribute>();

                // Get the value of the Guid property in the SiteContentType attribute
                var guid = siteContentTypeAttribute.GUID;

                // Check if the Guid already exists in the dictionary
                if (guidDictionary.ContainsKey(guid))
                {
                    // Increment the count for the Guid if it already exists
                    guidDictionary[guid]++;
                }
                else
                {
                    // Add the Guid to the dictionary if it doesn't exist
                    guidDictionary.Add(guid, 1);
                }
            }

            // Iterate through the dictionary to check if there are any Guids with a count greater than 1
            foreach (var guid in guidDictionary)
            {
                Assert.Equal(1, guid.Value); // Ensure the count for each Guid is equal to 1
            }
        }
    }
}

This test code uses reflection to get all the content types in the solution that implement the IContentData interface and are not abstract. It then iterates through all the content types and checks that each content type has a unique GUID. The test fails if any content type has a GUID that is not unique.

By writing an xUnit test to verify that all Content Type Guids are unique in an Optimizely solution, we can ensure that the CMS has expected behavior and avoid potential issues caused by duplicate GUIDs. 

Mar 27, 2023

Comments

valdis
valdis Mar 27, 2023 02:53 PM

This is nice way to ensure and remind myself not to use the same GUID :)

btw, this check is also available in code analyzer - https://github.com/Stekeblad/stekeblad.optimizely.analyzers/blob/master/doc/Analyzers/SOA1009.md

Karol Berezicki
Karol Berezicki Mar 28, 2023 09:48 AM

There was also a blog post from Ove Lartelius on Epinova regariding content type GUIDs and much more :) 
https://www.epinova.no/en/folg-med/blog/2019/episerver-content-types-hygiene-unit-tests/

Minesh Shah (Netcel)
Minesh Shah (Netcel) Apr 3, 2023 10:33 AM

Thank you Valdis and Karol, extremely helpful information which I was not aware of previously. 

Please login to comment.
Latest blogs
How to: set access right to folders

Today I stumped upon this question Solution for Handling File Upload Permissions in Episerver CMS 12, and there is a simple solution for that Using...

Quan Mai | Feb 7, 2025 | Syndicated blog

Poking around in the new Visual Builder and the SaaS CMS

Early findings from using a SaaS CMS instance and the new Visual Builder grids.

Johan Kronberg | Feb 7, 2025 | Syndicated blog

Be careful with your (order) notes

This happened a quite ago but only now I have had time to write about it. Once upon a time, I was asked to look into a customer database (in a big...

Quan Mai | Feb 5, 2025 | Syndicated blog

Imagevault download picker

This open source extension enables you to download images as ImageData with ContentReference from the ImageVault picker. It serves as an alternativ...

Luc Gosso (MVP) | Feb 4, 2025 | Syndicated blog

Optimizely SaaS vs PaaS: A Comparison from Client and Developer Perspectives

Optimizely, one of the leading digital experience platform. Offering both Software-as-a-Service (SaaS) and Platform-as-a-Service (PaaS) solutions....

Praful Jangid | Feb 3, 2025

Returning to Optimizely After Many Years

Returning to Optimizely After Many Years: A Journey Through Its New Features After several years away from Optimizely’s Content Management … More

Jose Neto | Feb 2, 2025 | Syndicated blog