• Published
  • 6 min

EPiServer content types hygiene unit tests

Some developers are lazy or just not paying attention of the attributes set on content types and properties. I believe that developers should set the correct information as soon as possible and not wait until the end of the project go through and set data to the needed attributes for content types and properties.

If you do it during the development of a feature, I think you have the best mindset to describe what the content type/properties are for.

I also want to raise the standard and hygiene on the deliverables, so I created these unit tests that uses reflection go through all content types and their properties and look on what attributes are set and that they are correct. The tests are written to be reusable so that you have the option to turn off a test without deleting or uncomment the code/test for any specific project.

Features

Content types

Check that developers have set a "Guid" attribute.
Check that developers have not set the same Guid value on different content types.
Check that developers have set a "DisplayName" attribute.
Check that developers have set a "Description" attribute.
Check that developers have set a "GroupName" attribute.
Check that developers have set a "ImageUrl" attribute.
Check that developers have set a "Order" attribute.
Check that developers have not set the same Order value on different content types.

Content type properties

Check that developers have set a "Name" attribute.
Check that developers have set a "Description" attribute.
Check that developers have set a "ShortName" attribute.
Check that developers have set a "GroupName" attribute.
Check that developers have set a "Order" attribute.
Check that developers have not set the same Order value on different properties in the same content type.
Check that developers have set a "Prompt" attribute on string properties.

How to get started

  1. Clone the project from GitHub. https://github.com/ovelartelius/EpiserverHygieneDemo/
  2. In your unittest project/structure. Create a new class with the name “ContentTypesHygieneTest.cs”.
  3. Copy the code (from your clone or directly from GitHub) and paste into the new created class.
  4. Change the namespace in the copied code to fit your location in your project.
  5. Set the correct namespace to your website assembly. You simply check the website project settings and use “[Assembly name].Global”.

  6. Set which tests you want to run on your project. The properties that are set to true will be run. We strongly recommend that you run them all. Always!
  7. Now you should be ready to compile and run the tests.

How to set tests on/off

Some projects use special custom functionality to generate translation for names, descriptions or other attributes. So, I added some logic that could turn on and off tests in the class. The test is implemented on a template project that will be used when new projects are created. Then it is nice that each project, can decide them self, what kind of tests that should want to run.
In the beginning of the "ContentTypesHygieneTests" class there are a couple of properties that you can set to true/false. The list below describes in more detail what the test does.

The following properties can be turned on/off:

What does the tests do?

Check_ContentTypeGuid

If true: Two test will be executed.
1. Checks that all content types has the "GUID" attribute set. 

2. Checks if there is any duplicate GUID specified on content types.

If any content types missing the attribute, the test will fail, and report which content types that does not contain the "GUID" attribute.

If any duplicate GUID´s will be found, the test will fail, and report which content types that are using the same GUID.

Check_ContentTypeDisplayName

If true: Checks that all content types has the "DisplayName" attribute set.

If any content types missing the attribute or are less then 3 characters, the test will fail, and report which content types that does not contain the "DisplayName" attribute.

Check_ContentTypeDescription

If true: Check if all content types has the "Description" attribute set.

If any content types missing the attribute or are less then 3 characters, the test will fail, and report which content types that does not contain the "Description" attribute.

Check_ContentTypeGroupName

If true: Check if all content types has the "GroupName" attribute set.

If any content types missing the attribute, the test will fail, and report which content types that does not contain the "GroupName" attribute.

Check_ContentTypeImageUrl

If true: Check if all content types has the "ImageUrl" attribute set.

If any content types missing the attribute, the test will fail, and report which content types that does not contain the "ImageUrl" attribute.

Check_ContentTypeOrder

If true: Check if all content types has the "Order" attribute set.

If any content types missing the attribute, the test will fail, and report which content types that does not contain the "Order" attribute.

Check_ContentTypeDuplicateOrder

If true: Check if all content types has unique order index.
[BILD]
If any content types have the same order index, the test will fail, and report which content types that use the same order index number.

Check_PropertyName

If true: Check if all properties has the "Name" attribute set.

If any properties missing the attribute or are less then 2 characters, the test will fail, and report which [ContentType].[Property] that does not contain the "Name" attribute.

Check_PropertiesDescription

If true: Check if all properties has the "Description" attribute set.

If any properties missing the attribute or are less then 2 characters, the test will fail, and report which [ContentType].[Property] that does not contain the "Description" attribute.

Check_PropertiesOrder

If true: Check if all properties has the "Order" attribute set.

If any properties missing the attribute, the test will fail, and report which [ContentType].[Property] that does not contain the "Order" attribute.

Check_PropertiesShortName

If true: Check if all properties has the "ShortName" attribute set.

If any properties missing the attribute or are less then 2 characters, the test will fail, and report which [ContentType].[Property] that does not contain the "ShortName" attribute.

Check_PropertiesDuplicateOrder

If true: Check if all properties with in a content type has unique order index.

If any properties has the same order index, the test will fail, and report which [ContentType].[Property] that use the same order index number.

Check_PropertiesGroupName

If true: Check if all properties has the "GroupName" attribute set.

If any properties missing the attribute or are less then 2 characters, the test will fail, and report which [ContentType].[Property] that does not contain the "GroupName" attribute.

Check_PropertiesStringPrompt

If true: Check if all string properties has the "Prompt" attribute set.

If any string properties missing the attribute or are less then 2 characters, the test will fail, and report which [ContentType].[Property] that does not contain the "Prompt" attribute.