Home > C#, Sitecore 6, Sitecore Fakes, Unit Testing > Sitecore Fakes Media Items LinkFields and a challange.

Sitecore Fakes Media Items LinkFields and a challange.

When I  first started building the Sitecore Fakes Isolation framework I was given a challenge by Per Bering. The challenge was  to make the following unit test pass.


Passing this test would require for implementation of some more fake objects in existing Sitecore Fakes Framework,  a implementation for easily adding LinkField XML to an field “General Link Field in this case” ,  make the mapping to the TargetItem And create it. Creating a fake media Item, and replacing the default Media Provider used by the MediaManager.

The Fake Intenal Link Field, builds the XML required for casting in to the real LinkField, so it derives from a FakeField which build the XML allmost like Sitecore Does it.

Here is the FakeField and FakeLinkField no getters is placed on the FakeLinkField since I expected values to be fetch through the regular LinkField.


public class FakeField
 public FakeField(string xmlString)
 XMLDocument = XmlUtil.LoadXml(xmlString);

 public void SetAttribute(string name, string value)
 XmlUtil.SetAttribute(name, "", value, XMLDocument.FirstChild);

public XmlDocument XMLDocument;


public class FakeInternalLinkField : FakeLinkField
 public FakeInternalLinkField(Item itemToLinkTo,string target="",string url = "", string text="", string anchor="",string querystring="" ,string title="" ,string cssclass="")
 Target = target;
 Url = url;
 Text = text;
 Anchor = anchor;
 QueryString = querystring;
 Title = title;
 Class = cssclass;
 LinkType = "internal";

private void SetLinkToItem(Item itemToLinkTo)
 ((FakeDatabase) Factory.GetDatabase(itemToLinkTo.Database.Name)).FakeAddItem(itemToLinkTo);
 TargetItem = itemToLinkTo;

public override string ToString()
 return XMLDocument.InnerXml;

When adding the  FakeLinkField to an field on a FakeItem simply call the .ToString() and build the XML for a regular LinkField from Sitecore. See Example below.

 public void Field_AddingLinkFieldWithLinkFromOneItemToAnother_TargetItemShouldReturnLinkedToItem()
 Item linkedToItem = new FakeItem();
 FakeInternalLinkField fakeLinkField = new FakeInternalLinkField(linkedToItem);

 ID fieldId = ID.NewID;
 FieldList fieldCollection = new FieldList();

Item itemToLinkFrom = new FakeItem(fieldCollection);

LinkField sitecoreLinkField = (LinkField) itemToLinkFrom.Fields[fieldId];


With that in place the inner part of challenges is now passing.

Next was the TypeCast to a MedaiItem, again creating a FAkeMedia Item ad mapping all the field passed in all most does the job. BUT sitecore asks for field values using their names so an additional mapping from id to field named is required, Sitecore Fakes now mappes most of the standard field on an imageItem from Sitecore using the MediaField setting file se both below.

public class FakeMediaItem : MediaItem
 private readonly MediaStandardFields _mediaStandardFields;
 public FakeMediaItem(string name = "" ,string filepath="", string alt="", string width="",string height="",string title="",string keywords="",string extension="",string mimetype="",string size="" ) :
 _mediaStandardFields = new MediaStandardFields();

private static Item CreateDataItem(string name,string filepath, string alt, string width, string height, string title, string keywords, string extension, string mimetype, string size)
 FieldList fieldList = CreateFieldList(filepath, alt, width, height, title, keywords, extension, mimetype, size);
 FakeItem dataItem = new FakeItem(fieldList,name);


 return dataItem;

private static FieldList CreateFieldList(string filepath, string alt, string width, string height, string title,
 string keywords, string extension, string mimetype, string size)
 FieldList fieldList = new FieldList();

fieldList.Add(MediaStandardFields.FilePathId, filepath);
 fieldList.Add(MediaStandardFields.AltId, alt);
 fieldList.Add(MediaStandardFields.WidthId, width);
 fieldList.Add(MediaStandardFields.HeightId, height);
 fieldList.Add(MediaStandardFields.TitleId, title);
 fieldList.Add(MediaStandardFields.KeywordsId, keywords);
 fieldList.Add(MediaStandardFields.ExtensionId, extension);
 fieldList.Add(MediaStandardFields.MimetypeId, mimetype);
 fieldList.Add(MediaStandardFields.SizeId, size);
 return fieldList;

And the FieldMappings

 public class MediaStandardFields
 public static ID FilePathId = ID.NewID;
 public static ID AltId = ID.NewID;
 public static ID WidthId = ID.NewID;
 public static ID HeightId = ID.NewID;
 public static ID TitleId = ID.NewID;
 public static ID KeywordsId = ID.NewID;
 public static ID ExtensionId = ID.NewID;
 public static ID MimetypeId = ID.NewID;
 public static ID SizeId = ID.NewID;
 public static string FilePath = "file path";
 public static string Alt = "Alt";
 public static string Width = "Width";
 public static string Height = "Height";
 public static string Title = "Title";
 public static string Keywords = "Keywords";
 public static string Extension = "Extension";
 public static string Mimetype = "Mime type";
 public static string Size = "size";

public MediaStandardFields()

Now missing is the replacement for the fake media provide switching is fairly easy but changing in the app.config file

 <mediaProvider type="Sitecore.Fakes.FakeMediaProvider, Sitecore.Fakes" />

Since this is a first release and to make the test pass with out writting to much code I’m gonna give the premise that and media url is build from the item name and the type extesion , prefixed with “/~/media library/”. Here is is rather simple implementation of the Fake Media Provider.

 public class FakeMediaProvider : MediaProvider
 public override string GetMediaUrl(MediaItem item)
 Item sourceItem = item;
 return String.Format("/~/media/{0}.{1}", sourceItem.Name, sourceItem[MediaStandardFields.ExtensionId]);

And Now Lets See the “unit test” / challenge pass.

 public void The_Per_Bering_Challnange()
 MediaItem mediaItem = new FakeMediaItem("hello",extension:"png");

ID linkFieldId = ID.NewID;
 FakeInternalLinkField fakeLinkField = new FakeInternalLinkField(mediaItem);

FieldList fieldList = new FieldList();
 fieldList.Add(linkFieldId, fakeLinkField.ToString());

 FakeItem contenItem= new FakeItem(fieldList);


13-06-2013 13-14-53

Remember unlike other Sitecore unit testing examples this is all done i memory.  So this is pure unit testing no integration testing. And hopefully  Sitecore Fakes will develope over timer to support more features. Let me know which feature I should work on next.

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: