Recursive PublishedContentExtensions IsDocumentType

Optional recursive parameter defaulted to false for backwards compatibility and no performance hit.
Settings recursive = true fetches content type for content and checks ancestor content types for the same alias.
This commit is contained in:
Lars-Erik Aabech
2013-09-27 13:12:16 +02:00
parent 099c1ff2ea
commit b6777e4635
3 changed files with 131 additions and 2 deletions

View File

@@ -0,0 +1,110 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Umbraco.Core.Models;
using Umbraco.Tests.TestHelpers;
using Umbraco.Web;
namespace Umbraco.Tests.PublishedContent
{
[TestFixture]
public class PublishedContentExtensionTests : PublishedContentTestBase
{
private UmbracoContext ctx;
private string xmlContent = "";
private bool createContentTypes = true;
protected override DatabaseBehavior DatabaseTestBehavior
{
get { return DatabaseBehavior.NewDbFileAndSchemaPerFixture; }
}
[SetUp]
public override void Initialize()
{
base.Initialize();
}
[TearDown]
public override void TearDown()
{
base.TearDown();
}
protected override string GetXmlContent(int templateId)
{
return xmlContent;
}
[Test]
public void IsDocumentType_NonRecursive_ActualType_ReturnsTrue()
{
InitializeInheritedContentTypes();
var publishedContent = ctx.ContentCache.GetById(1100);
Assert.That(publishedContent.IsDocumentType("inherited"));
}
[Test]
public void IsDocumentType_NonRecursive_BaseType_ReturnsFalse()
{
InitializeInheritedContentTypes();
var publishedContent = ctx.ContentCache.GetById(1100);
Assert.That(publishedContent.IsDocumentType("base"), Is.False);
}
[Test]
public void IsDocumentType_Recursive_ActualType_ReturnsTrue()
{
InitializeInheritedContentTypes();
var publishedContent = ctx.ContentCache.GetById(1100);
Assert.That(publishedContent.IsDocumentType("inherited", true));
}
[Test]
public void IsDocumentType_Recursive_BaseType_ReturnsTrue()
{
InitializeInheritedContentTypes();
var publishedContent = ctx.ContentCache.GetById(1100);
Assert.That(publishedContent.IsDocumentType("base", true));
}
[Test]
public void IsDocumentType_Recursive_InvalidBaseType_ReturnsFalse()
{
InitializeInheritedContentTypes();
var publishedContent = ctx.ContentCache.GetById(1100);
Assert.That(publishedContent.IsDocumentType("invalidbase", true), Is.False);
}
private void InitializeInheritedContentTypes()
{
ctx = GetUmbracoContext("/", 1, null, true);
if (createContentTypes)
{
var contentTypeService = ctx.Application.Services.ContentTypeService;
var baseType = new ContentType(-1) {Alias = "base", Name = "Base"};
var inheritedType = new ContentType(baseType) {Alias = "inherited", Name = "Inherited"};
contentTypeService.Save(baseType);
contentTypeService.Save(inheritedType);
createContentTypes = false;
}
#region setup xml content
xmlContent = @"<?xml version=""1.0"" encoding=""utf-8""?>
<!DOCTYPE root[
<!ELEMENT inherited ANY>
<!ATTLIST inherited id ID #REQUIRED>
]>
<root id=""-1"">
<inherited id=""1100"" parentID=""-1"" level=""1"" writerID=""0"" creatorID=""0"" nodeType=""1044"" template=""1"" sortOrder=""1"" createDate=""2012-06-12T14:13:17"" updateDate=""2012-07-20T18:50:43"" nodeName=""Home"" urlName=""home"" writerName=""admin"" creatorName=""admin"" path=""-1,1046"" isDoc=""""/>
</root>";
#endregion
}
}
}

View File

@@ -196,6 +196,7 @@
<Compile Include="Persistence\Repositories\MemberTypeRepositoryTest.cs" />
<Compile Include="Persistence\Repositories\UserRepositoryTest.cs" />
<Compile Include="Persistence\Repositories\UserTypeRepositoryTest.cs" />
<Compile Include="PublishedContent\PublishedContentExtensionTests.cs" />
<Compile Include="PublishedContent\StronglyTypedModels\CallingMethodTests.cs" />
<Compile Include="PublishedContent\StronglyTypedModels\Subpage.cs" />
<Compile Include="PublishedContent\StronglyTypedModels\Textpage.cs" />

View File

@@ -537,9 +537,27 @@ namespace Umbraco.Web
#region Is Helpers
public static bool IsDocumentType(this IPublishedContent content, string docTypeAlias)
public static bool IsDocumentType(this IPublishedContent content, string docTypeAlias, bool recursive = false)
{
return content.DocumentTypeAlias == docTypeAlias;
if (content.DocumentTypeAlias == docTypeAlias)
return true;
if (recursive)
return IsDocumentTypeRecursive(content, docTypeAlias);
return false;
}
private static bool IsDocumentTypeRecursive(IPublishedContent content, string docTypeAlias)
{
var contentTypeService = UmbracoContext.Current.Application.Services.ContentTypeService;
var type = contentTypeService.GetContentType(content.DocumentTypeAlias);
while (type.ParentId > 0)
{
type = contentTypeService.GetContentType(type.ParentId);
if (type.Alias == docTypeAlias)
return true;
}
return false;
}
public static bool IsNull(this IPublishedContent content, string alias, bool recursive)