public void Prefixed_Properties_Can_Resolve() { var prop1 = new PublishedContentPropertyMock { Alias = "siteName", Value = "Name" }; var prop2 = new PublishedContentPropertyMock { Alias = "siteDescription", Value = "Description" }; var prop3 = new PublishedContentPropertyMock { Alias = "fallback", Value = "Fallback" }; var content = new PublishedContentMock { Properties = new[] { prop1, prop2, prop3 } }; var converted = content.As <MyModel>(); Assert.That(converted.Name, Is.EqualTo("Name")); Assert.That(converted.Description, Is.EqualTo("Description")); Assert.That(converted.Fallback, Is.EqualTo("Fallback")); }
public void Basic_Property_To_String_Exception() { // The source is an `IPublishedContent`, the target is a `string`, type mismatch exception var value = new PublishedContentMock(); var property = new PublishedContentPropertyMock { Alias = "myProperty", Value = value }; var content = new PublishedContentMock { Properties = new[] { property } }; TestDelegate code = () => { // We are passing an `IPublishedContent` object to a property (of type `string`), // so we know that internally Ditto will try calling `content.As<string>()`, // which will throw an `InvalidOperationException` exception. var model = content.As <BasicModelWithStringProperty>(); }; Assert.Throws <InvalidOperationException>(code); }
public void CurrentContent_Property_Mapped() { var metaTitle = new PublishedContentPropertyMock { Alias = "metaTitle", Value = "This is the meta title" }; var metaDescription = new PublishedContentPropertyMock { Alias = "metaDescription", Value = "This is the meta description" }; var metaKeywords = new PublishedContentPropertyMock { Alias = "metaKeywords", Value = "these,are,meta,keywords" }; var content = new PublishedContentMock { Properties = new[] { metaTitle, metaDescription, metaKeywords } }; var model = content.As <MyModel>(); Assert.That(model, Is.Not.Null); Assert.That(model.MetaData1, Is.Not.Null, "We expect the property to be populated."); Assert.That(model.MetaData1, Is.TypeOf <MyMetaDataModel>()); Assert.That(model.MetaData2, Is.Null, "We expect the property not to be populated."); }
public void Cache_Caches() { var cacheHelper = new CacheHelper( new ObjectCacheRuntimeCacheProvider(), new StaticCacheProvider(), new NullCacheProvider()); var appCtx = new ApplicationContext(cacheHelper); ApplicationContext.EnsureContext(appCtx, true); var prop1 = new PublishedContentPropertyMock { Alias = "myProperty1", Value = "Test1" }; var prop2 = new PublishedContentPropertyMock { Alias = "myProperty2", Value = "Test1" }; var prop3 = new PublishedContentPropertyMock { Alias = "myProperty3", Value = "Test1" }; var content = new PublishedContentMock { Id = 1, Properties = new[] { prop1, prop2, prop3 } }; var model1 = content.As <MyValueResolverModel1>(); var model2 = content.As <MyValueResolverModel2>(); Assert.That(model1.MyProperty1, Is.EqualTo("Test1")); Assert.That(model1.MyProperty2, Is.EqualTo("Test1")); Assert.That(model1.MyProperty3, Is.EqualTo("Test1")); Assert.That(model2.MyProperty1, Is.EqualTo("Test1")); Assert.That(model2.MyProperty2, Is.EqualTo("Test1")); Assert.That(model2.MyProperty3, Is.EqualTo("Test1")); prop1.Value = "Test2"; prop2.Value = "Test2"; prop3.Value = "Test2"; model1 = content.As <MyValueResolverModel1>(); model2 = content.As <MyValueResolverModel2>(); Assert.That(model1.MyProperty1, Is.EqualTo("Test1")); Assert.That(model1.MyProperty2, Is.EqualTo("Test1")); Assert.That(model1.MyProperty3, Is.EqualTo("Test2")); // This one doesn't cache Assert.That(model2.MyProperty1, Is.EqualTo("Test1")); Assert.That(model2.MyProperty2, Is.EqualTo("Test1")); Assert.That(model2.MyProperty3, Is.EqualTo("Test1")); }
public void Basic_PublishedContent_Property_IsMapped() { var value = new PublishedContentMock(); var property = new PublishedContentPropertyMock { Alias = "myProperty", Value = value }; var content = new PublishedContentMock { Properties = new[] { property } }; var model = content.As <BasicModelWithPublishedContentProperty>(); Assert.That(model.MyProperty, Is.EqualTo(value)); }
public void GenericDictionaryPropertyIsNotDetectedAsCastableEnumerable() { var property = new PublishedContentPropertyMock { Alias = "myProperty", Value = "myValue" }; var content = new PublishedContentMock { Properties = new[] { property } }; var result = content.As <MyModel>(); Assert.NotNull(result.MyProperty); Assert.True(result.MyProperty.Any()); Assert.AreEqual(result.MyProperty["hello"], "world"); }
public void CustomPublishedContent_Property_IsMapped() { var value = "myValue"; var property = new PublishedContentPropertyMock { Alias = "myProperty", Value = value }; var content = new CustomPublishedContentMock { Properties = new[] { property } }; var model = content.As<MyModel>(); Assert.That(model.MyProperty, Is.EqualTo(value)); }
public void Single_PublishedContent_Mapped_From_Collection() { var mock = new PublishedContentMock(); var items = Enumerable.Repeat <IPublishedContent>(mock, 3); var property = new PublishedContentPropertyMock() { Alias = "myProperty", Value = items }; var content = new PublishedContentMock() { Properties = new[] { property } }; var model = content.As <MyModel>(); Assert.That(model, Is.Not.Null); Assert.That(model.MyProperty, Is.Not.Null); }
public void CustomPublishedContent_Property_IsMapped() { var propertyValue = "foo"; var objectValue = "bar"; var property = new PublishedContentPropertyMock { Alias = "myProperty", Value = propertyValue }; var content = new CustomPublishedContentMock { Properties = new[] { property }, MyProperty = objectValue }; var model = content.As <MyModel>(); Assert.That(model.MyProperty, Is.Not.EqualTo(propertyValue)); Assert.That(model.MyProperty, Is.EqualTo(objectValue)); }
public void Existing_Inherited_Object_Mapped() { var propertyValue = "Foo Bar"; var property = new PublishedContentPropertyMock { Alias = "prop3", Value = propertyValue }; var content = new PublishedContentMock { Properties = new[] { property } }; var value = "Hello world"; var model = new MyInheritedModel() { MyProperty3 = value }; // run through Ditto, the expected behaviour is that the Umbraco property value will overwrite the previous value. content.As(instance: model); Assert.That(model.MyProperty3, Is.Not.EqualTo(value)); Assert.That(model.MyProperty3, Is.EqualTo(propertyValue)); }
public void Existing_Base_Object_Mapped() { var propertyValue = "Foo Bar"; var property = new PublishedContentPropertyMock { Alias = "prop3", Value = propertyValue }; var content = new PublishedContentMock { Properties = new[] { property } }; var value = "Hello world"; var model = new MyInheritedModel() { MyProperty3 = value }; // down-cast the model to the base type, so the inherited type properties should not be mapped. content.As <MyBaseModel>(instance: model); Assert.That(model.MyProperty3, Is.EqualTo(value)); Assert.That(model.MyProperty3, Is.Not.EqualTo(propertyValue)); }
public void Prefixed_Properties_Can_UmbracoPropertyAttribute_Override() { var prop1 = new PublishedContentPropertyMock { Alias = "siteUnprefixedProp", Value = "Site Unprefixed" }; var prop2 = new PublishedContentPropertyMock { Alias = "unprefixedProp", Value = "Unprefixed" }; var content = new PublishedContentMock { Properties = new[] { prop1, prop2 } }; var converted = content.As <MyModel>(); Assert.That(converted.UnprefixedProp, Is.EqualTo("Unprefixed")); }