PublishedContent - implement a model factory

This commit is contained in:
Stephan
2013-09-13 21:15:40 +02:00
parent ac19ac7a6b
commit 85cb7fadfc
8 changed files with 288 additions and 2 deletions

View File

@@ -1,6 +1,5 @@
using System.Linq;
using System.Collections.ObjectModel;
using Lucene.Net.Documents;
using NUnit.Framework;
using Umbraco.Core;
using Umbraco.Core.Models;
@@ -36,7 +35,7 @@ namespace Umbraco.Tests.PublishedContent
PropertyValueConvertersResolver.Current =
new PropertyValueConvertersResolver();
PublishedContentModelFactoryResolver.Current =
new PublishedContentModelFactoryResolver();
new PublishedContentModelFactoryResolver(new PublishedContentModelFactoryImpl());
Resolution.Freeze();
var caches = CreatePublishedContent();
@@ -122,6 +121,44 @@ namespace Umbraco.Tests.PublishedContent
Assert.IsTrue(content.IsLast());
}
[Test]
public void OfType1()
{
var content = UmbracoContext.Current.ContentCache.GetAtRoot()
.OfType<ContentType2>()
.Distinct()
.ToArray();
Assert.AreEqual(2, content.Count());
Assert.IsInstanceOf<ContentType2>(content.First());
var set = content.ToContentSet();
Assert.IsInstanceOf<ContentType2>(set.First());
Assert.AreSame(set, set.First().ContentSet);
Assert.IsInstanceOf<ContentType2Sub>(set.First().Next());
}
[Test]
public void OfType2()
{
var content = UmbracoContext.Current.ContentCache.GetAtRoot()
.OfType<ContentType2Sub>()
.Distinct()
.ToArray();
Assert.AreEqual(1, content.Count());
Assert.IsInstanceOf<ContentType2Sub>(content.First());
var set = content.ToContentSet();
Assert.IsInstanceOf<ContentType2Sub>(set.First());
}
[Test]
public void OfType()
{
var content = UmbracoContext.Current.ContentCache.GetAtRoot()
.OfType<ContentType2>()
.First(x => x.Prop1 == 1234);
Assert.AreEqual("Content 2", content.Name);
Assert.AreEqual(1234, content.Prop1);
}
[Test]
public void Position()
{
@@ -138,6 +175,28 @@ namespace Umbraco.Tests.PublishedContent
Assert.IsTrue(content.First().Next().Next().IsLast());
}
[Test]
public void Issue()
{
var content = UmbracoContext.Current.ContentCache.GetAtRoot()
.Distinct()
.OfType<ContentType2>();
var where = content.Where(x => x.Prop1 == 1234);
var first = where.First();
Assert.AreEqual(1234, first.Prop1);
var content2 = UmbracoContext.Current.ContentCache.GetAtRoot()
.OfType<ContentType2>()
.First(x => x.Prop1 == 1234);
Assert.AreEqual(1234, content2.Prop1);
var content3 = UmbracoContext.Current.ContentCache.GetAtRoot()
.OfType<ContentType2>()
.First();
Assert.AreEqual(1234, content3.Prop1);
}
static SolidPublishedCaches CreatePublishedContent()
{
var caches = new SolidPublishedCaches();

View File

@@ -227,6 +227,36 @@ namespace Umbraco.Tests.PublishedContent
public object XPathValue { get; set; }
}
[PublishedContentModel("ContentType2")]
public class ContentType2 : PublishedContentModel
{
#region Plumbing
public ContentType2(IPublishedContent content)
: base(content)
{ }
#endregion
// fast, if you know that the appropriate IPropertyEditorValueConverter is wired
public int Prop1 { get { return (int)this["prop1"]; } }
// almost as fast, not sure I like it as much, though
//public int Prop1 { get { return this.GetPropertyValue<int>("prop1"); } }
}
[PublishedContentModel("ContentType2Sub")]
public class ContentType2Sub : ContentType2
{
#region Plumbing
public ContentType2Sub(IPublishedContent content)
: base(content)
{ }
#endregion
}
class PublishedContentStrong1 : PublishedContentExtended
{
public PublishedContentStrong1(IPublishedContent content)

View File

@@ -23,6 +23,8 @@ namespace Umbraco.Tests.PublishedContent
get { return DatabaseBehavior.NoDatabasePerFixture; }
}
private PluginManager _pluginManager;
public override void Initialize()
{
// required so we can access property.Value
@@ -30,6 +32,16 @@ namespace Umbraco.Tests.PublishedContent
base.Initialize();
// this is so the model factory looks into the test assembly
_pluginManager = PluginManager.Current;
PluginManager.Current = new PluginManager(false)
{
AssembliesToScan = _pluginManager.AssembliesToScan
.Union(new[] { typeof(PublishedContentTests).Assembly })
};
ApplicationContext.Current = new ApplicationContext(false) { IsReady = true };
// need to specify a custom callback for unit tests
// AutoPublishedContentTypes generates properties automatically
// when they are requested, but we must declare those that we
@@ -48,6 +60,20 @@ namespace Umbraco.Tests.PublishedContent
PublishedContentType.GetPublishedContentTypeCallback = (alias) => type;
}
public override void TearDown()
{
PluginManager.Current = _pluginManager;
ApplicationContext.Current.DisposeIfDisposable();
ApplicationContext.Current = null;
}
protected override void FreezeResolution()
{
PublishedContentModelFactoryResolver.Current = new PublishedContentModelFactoryResolver(
new PublishedContentModelFactoryImpl());
base.FreezeResolution();
}
protected override string GetXmlContent(int templateId)
{
return @"<?xml version=""1.0"" encoding=""utf-8""?>
@@ -186,6 +212,47 @@ namespace Umbraco.Tests.PublishedContent
}
}
[PublishedContentModel("Home")]
public class Home : PublishedContentModel
{
public Home(IPublishedContent content)
: base(content)
{}
}
[Test]
public void Is_Last_From_Where_Filter2()
{
var doc = GetNode(1173);
var items = doc.Children
.Select(PublishedContentModelFactory.CreateModel) // linq, returns IEnumerable<IPublishedContent>
// only way around this is to make sure every IEnumerable<T> extension
// explicitely returns a PublishedContentSet, not an IEnumerable<T>
.OfType<Home>() // ours, return IEnumerable<Home> (actually a PublishedContentSet<Home>)
.Where(x => x.IsVisible()) // so, here it's linq again :-(
.ToContentSet() // so, we need that one for the test to pass
.ToArray();
Assert.AreEqual(1, items.Count());
foreach (var d in items)
{
switch (d.Id)
{
case 1174:
Assert.IsTrue(d.IsFirst());
Assert.IsTrue(d.IsLast());
break;
default:
Assert.Fail("Invalid id.");
break;
}
}
}
[Test]
public void Is_Last_From_Take()
{