Skip to content

Commit

Permalink
Updated installation instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
sshushliapin committed Mar 15, 2014
1 parent d55a3e9 commit fc6c4a5
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 46 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,6 @@ $RECYCLE.BIN/

# Mac desktop service store files
.DS_Store

# License
license.xml
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
0.7.1
* [FIX] Updated installation instructions
* [FIX] Updated Getting Started code samples
* [FIX] Changed default license.xml file path. Now it is set to the root of a test project

0.7.0
* [NEW] Added possibility to configure settings in memory

Expand Down
69 changes: 48 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
Sitecore FakeDb
===============

A unit testing framework for Sitecore that enables creation and manipulation of Sitecore content in memory. Designed to minimize efforts for the test content initialization keeping focus on the minimal test data rather than comprehensive content tree representation.
A unit testing framework for Sitecore that enables creation and manipulation of Sitecore content in memory.
Designed to minimize efforts for the test content initialization keeping focus on the minimal test data rather
than comprehensive content tree representation.

### How do I install Sitecore FakeDb
## Installation

The package is available on NuGet. To install the package, run the following command in the Package Manager Console:
Sitecore FakeDb is available on NuGet.
To install the framework:

Install-Package Sitecore.FakeDb
When the package installation is done, go to the App.config file of the project you have the package installed and set path to the license.xml file in LicenseFile setting.
1. Create a new Class Library project.
2. Add references to Sitecore.Kernel and Sitecore.Nexus assemblies.
3. Run the following command in the NuGet Package Manager Console:

`Install-Package Sitecore.FakeDb`
4. Open App.config file added by the package and update path to the license.xml file using LicenseFile setting if necessary. By default the license file path is set to the root folder of the project:

`<setting name="LicenseFile" value="..\..\license.xml" />`

## How do I...
### How do I create a simple item

The code below creates a fake in-memory database with a single item Home that contains field Title with value 'Welcome!' ([xUnit](http://xunit.codeplex.com/) and [FluentAssertions](https://github.com/dennisdoomen/FluentAssertions) are used):
The code below creates a fake in-memory database with a single item Home that contains field Title with value 'Welcome!'
([xUnit](http://xunit.codeplex.com/) unit testing framework is used):

[Fact]
public void HowDoICreateASimpleItem()
Expand All @@ -24,7 +34,7 @@ The code below creates a fake in-memory database with a single item Home that co
})
{
Sitecore.Data.Items.Item homeItem = db.GetItem("/sitecore/content/home");
homeItem["Title"].Should().Be("Welcome!");
Assert.Equal("Welcome!", homeItem["Title"]);
}
}

Expand All @@ -45,8 +55,8 @@ This code creates a root item Articles and two child items Getting Started and T
})
{
Sitecore.Data.Items.Item articles = db.GetItem("/sitecore/content/Articles");
articles["Getting Started"].Should().NotBeNull();
articles["Troubleshooting"].Should().NotBeNull();
Assert.NotNull(articles["Getting Started"]);
Assert.NotNull(articles["Troubleshooting"]);
}
}

Expand All @@ -66,15 +76,18 @@ The next example demonstrates how to configure field values for different langua
})
{
Sitecore.Data.Items.Item homeEn = db.GetItem("/sitecore/content/home", "en");
homeEn["Title"].Should().Be("Hello!");
Assert.Equal("Hello!", homeEn["Title"]);

Sitecore.Data.Items.Item homeDa = db.GetItem("/sitecore/content/home", "da");
homeDa["Title"].Should().Be("Hej!");
Assert.Equal("Hej!", homeDa["Title"]);
}
}

### How do I create an item of specific template

In some cases you may want to create an item template first and only then add items based on this template.
It can be acheived using the next sample:

[Fact]
public void HowDoICreateAnItemOfSpecificTemplate()
{
Expand All @@ -87,14 +100,16 @@ The next example demonstrates how to configure field values for different langua
})
{
Sitecore.Data.Items.Item item = db.GetItem("/sitecore/content/apple");
item.TemplateID.Should().Be(templateId);
item.Fields["Name"].Should().NotBeNull();
Assert.Equal(templateId, item.TemplateID);
Assert.NotNull(item.Fields["Name"]);
}
}

## Security
### How do I configure item access

The code below denies item read, so that GetItem() method returns null:

[Fact]
public void HowDoIConfigureItemAccess()
{
Expand All @@ -104,7 +119,9 @@ The next example demonstrates how to configure field values for different langua
})
{
Sitecore.Data.Items.Item item = db.GetItem("/sitecore/content/home");
item.Should().BeNull();

// item is null because read is denied
Assert.Null(item);
}
}

Expand All @@ -123,7 +140,8 @@ The next example mocks the authentication provider and substitutes it so that au
using (new Sitecore.Common.Switcher<Sitecore.Security.Authentication.AuthenticationProvider>(provider))
{
// use authentication manager in your code
Sitecore.Security.Authentication.AuthenticationManager.Login("John", false).Should().BeTrue();
bool isLoggedIn = Sitecore.Security.Authentication.AuthenticationManager.Login("John", false);
Assert.True(isLoggedIn);
}
}

Expand All @@ -140,13 +158,18 @@ and can be used in unit tests:
{
using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db())
{
// set the setting value in unit test using db instance
db.Configuration.Settings["MySetting"] = "1234";
Sitecore.Configuration.Settings.GetSetting("MySetting").Should().Be("1234");

// get the setting value in your code using regular Sitecore API
var value = Sitecore.Configuration.Settings.GetSetting("MySetting");
Assert.Equal("1234", value);
}
}

## Miscellaneous
### How do I mock the content search logic

The example below creates and configure a content search index mock so that it returns Home item:

[Fact]
Expand All @@ -165,7 +188,7 @@ The example below creates and configure a content search index mock so that it r

Sitecore.Data.Items.Item result = index.CreateSearchContext().GetQueryable<Sitecore.ContentSearch.SearchTypes.SearchResultItem>().Single().GetItem();

result.Paths.FullPath.Should().Be("/sitecore/content/home");
Assert.Equal("/sitecore/content/home", result.Paths.FullPath);
}
}
finally
Expand All @@ -179,10 +202,14 @@ The example below creates and configure a content search index mock so that it r
[Fact]
public void HowDoIMockTrackerVisitor()
{
var visitor = Substitute.For<Sitecore.Analytics.Data.DataAccess.Visitor>(Guid.NewGuid());
// create a visitor mock
var visitorMock = Substitute.For<Sitecore.Analytics.Data.DataAccess.Visitor>(Guid.NewGuid());

using (new Sitecore.Common.Switcher<Sitecore.Analytics.Data.DataAccess.Visitor>(visitor))
// inject the visitor mock into Analytics Tracker
using (new Sitecore.Common.Switcher<Sitecore.Analytics.Data.DataAccess.Visitor>(visitorMock))
{
Sitecore.Analytics.Tracker.Visitor.Should().Be(visitor);
// the mocked visitor instance is now available via Analytics.Tracker.Visitor property
var currentVisitor = Sitecore.Analytics.Tracker.Visitor;
Assert.Equal(visitorMock, currentVisitor);
}
}
9 changes: 5 additions & 4 deletions Source/Sitecore.FakeDb.Tests/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, Sitecore.Logging" />
</configSections>
<sitecore database="Fake">
<!-- SETTINGS -->
<settings>
<setting name="LicenseFile" value="..\..\license.xml" />
<setting name="Caching.Enabled" value="false" />
</settings>
<!-- PIPELINES -->
<pipelines>
<!-- FakeDb -->
Expand Down Expand Up @@ -150,10 +155,6 @@
<add name="fake" type="Sitecore.FakeDb.Security.AccessControl.FakeAccessRightProvider, Sitecore.FakeDb" />
</providers>
</accessRights>
<settings>
<setting name="LicenseFile" value="..\..\..\packages\Sitecore\license.xml" />
<setting name="Caching.Enabled" value="false" />
</settings>
<!-- SEARCH -->
<contentSearch>
<configuration type="Sitecore.ContentSearch.ProviderIndexSearchConfiguration, Sitecore.ContentSearch" />
Expand Down
40 changes: 25 additions & 15 deletions Source/Sitecore.FakeDb.Tests/GettingStarted.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
{
using System;
using System.Linq;
using FluentAssertions;
using NSubstitute;
using Xunit;

Expand All @@ -19,7 +18,7 @@ public void HowDoICreateASimpleItem()
})
{
Sitecore.Data.Items.Item homeItem = db.GetItem("/sitecore/content/home");
homeItem["Title"].Should().Be("Welcome!");
Assert.Equal("Welcome!", homeItem["Title"]);
}
}

Expand All @@ -36,8 +35,8 @@ public void HowDoICreateAnItemHierarchy()
})
{
Sitecore.Data.Items.Item articles = db.GetItem("/sitecore/content/Articles");
articles["Getting Started"].Should().NotBeNull();
articles["Troubleshooting"].Should().NotBeNull();
Assert.NotNull(articles["Getting Started"]);
Assert.NotNull(articles["Troubleshooting"]);
}
}

Expand All @@ -53,10 +52,10 @@ public void HowDoICreateAMultilingualItem()
})
{
Sitecore.Data.Items.Item homeEn = db.GetItem("/sitecore/content/home", "en");
homeEn["Title"].Should().Be("Hello!");
Assert.Equal("Hello!", homeEn["Title"]);

Sitecore.Data.Items.Item homeDa = db.GetItem("/sitecore/content/home", "da");
homeDa["Title"].Should().Be("Hej!");
Assert.Equal("Hej!", homeDa["Title"]);
}
}

Expand All @@ -72,8 +71,8 @@ public void HowDoICreateAnItemOfSpecificTemplate()
})
{
Sitecore.Data.Items.Item item = db.GetItem("/sitecore/content/apple");
item.TemplateID.Should().Be(templateId);
item.Fields["Name"].Should().NotBeNull();
Assert.Equal(templateId, item.TemplateID);
Assert.NotNull(item.Fields["Name"]);
}
}

Expand All @@ -90,7 +89,9 @@ public void HowDoIConfigureItemAccess()
})
{
Sitecore.Data.Items.Item item = db.GetItem("/sitecore/content/home");
item.Should().BeNull();

// item is null because read is denied
Assert.Null(item);
}
}

Expand All @@ -105,7 +106,8 @@ public void HowDoIMockAuthenticationProvider()
using (new Sitecore.Common.Switcher<Sitecore.Security.Authentication.AuthenticationProvider>(provider))
{
// use authentication manager in your code
Sitecore.Security.Authentication.AuthenticationManager.Login("John", false).Should().BeTrue();
bool isLoggedIn = Sitecore.Security.Authentication.AuthenticationManager.Login("John", false);
Assert.True(isLoggedIn);
}
}

Expand All @@ -118,8 +120,12 @@ public void HowDoIConfigureSettings()
{
using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db())
{
// set the setting value in unit test using db instance
db.Configuration.Settings["MySetting"] = "1234";
Sitecore.Configuration.Settings.GetSetting("MySetting").Should().Be("1234");

// get the setting value in your code using regular Sitecore API
var value = Sitecore.Configuration.Settings.GetSetting("MySetting");
Assert.Equal("1234", value);
}
}

Expand All @@ -143,7 +149,7 @@ public void HowDoIMockContentSearchLogic()

Sitecore.Data.Items.Item result = index.CreateSearchContext().GetQueryable<Sitecore.ContentSearch.SearchTypes.SearchResultItem>().Single().GetItem();

result.Paths.FullPath.Should().Be("/sitecore/content/home");
Assert.Equal("/sitecore/content/home", result.Paths.FullPath);
}
}
finally
Expand All @@ -155,11 +161,15 @@ public void HowDoIMockContentSearchLogic()
[Fact]
public void HowDoIMockTrackerVisitor()
{
var visitor = Substitute.For<Sitecore.Analytics.Data.DataAccess.Visitor>(Guid.NewGuid());
// create a visitor mock
var visitorMock = Substitute.For<Sitecore.Analytics.Data.DataAccess.Visitor>(Guid.NewGuid());

using (new Sitecore.Common.Switcher<Sitecore.Analytics.Data.DataAccess.Visitor>(visitor))
// inject the visitor mock into Analytics Tracker
using (new Sitecore.Common.Switcher<Sitecore.Analytics.Data.DataAccess.Visitor>(visitorMock))
{
Sitecore.Analytics.Tracker.Visitor.Should().Be(visitor);
// the mocked visitor instance is now available via Analytics.Tracker.Visitor property
var currentVisitor = Sitecore.Analytics.Tracker.Visitor;
Assert.Equal(visitorMock, currentVisitor);
}
}

Expand Down
6 changes: 3 additions & 3 deletions Source/Sitecore.FakeDb.Tests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.7.0.0")]
[assembly: AssemblyFileVersion("0.7.0.0")]
[assembly: AssemblyInformationalVersion("0.7.0")]
[assembly: AssemblyVersion("0.7.1.0")]
[assembly: AssemblyFileVersion("0.7.1.0")]
[assembly: AssemblyInformationalVersion("0.7.1")]
6 changes: 3 additions & 3 deletions Source/Sitecore.FakeDb/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.7.0.0")]
[assembly: AssemblyFileVersion("0.7.0.0")]
[assembly: AssemblyInformationalVersion("0.7.0")]
[assembly: AssemblyVersion("0.7.1.0")]
[assembly: AssemblyFileVersion("0.7.1.0")]
[assembly: AssemblyInformationalVersion("0.7.1")]

[assembly: InternalsVisibleTo("Sitecore.FakeDb.Tests")]

0 comments on commit fc6c4a5

Please sign in to comment.