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 Prefixed_Properties_Can_Resolve_Recursive() { var childContent = new PublishedContentMock(); var parentContent = new PublishedContentMock { Properties = new[] { new PublishedContentPropertyMock { Alias = "siteDescription", Value = "Description" } }, Children = new[] { childContent } }; childContent.Parent = parentContent; var converted = childContent.As<MyModel>(); Assert.That(converted.Description, Is.EqualTo("Description")); }
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 CurrentContent_InfineLoop_Check() { var content = new PublishedContentMock(); TestDelegate code = () => { content.As<MyCircularReferenceModel>(); }; Assert.Throws<InvalidOperationException>(code); }
public void PocoConstructorParameterTest() { var content = new Mocks.PublishedContentMock(); TestDelegate code = () => { var model = content.As <MyModel2>(); }; Assert.Throws <InvalidOperationException>(code); }
public void PocoConstructorEmptyTest() { var content = new Mocks.PublishedContentMock(); var model = content.As <MyModel>(); Assert.That(model, Is.Not.Null); Assert.That(model, Is.InstanceOf <MyModel>()); }
public void AppSetting_Property_Returned() { var value = "MyAppSettingValue"; var content = new PublishedContentMock(); var model = content.As<MyAppSettingsModel>(); Assert.That(model.MyProperty, Is.EqualTo(value)); }
public void Custom_ValueResolver_Resolves() { var content = new PublishedContentMock() { Name = "MyName" }; var model = content.As<MyModel>(); Assert.That(model.Name, Is.EqualTo("MyName Test")); }
public void Castable_Interface_Can_Map() { var content = new PublishedContentMock { Id = 123, Name = "Test" }; var content2 = content.As<IPublishedContent>(); Assert.That(content2.Id, Is.EqualTo(content.Id)); Assert.That(content2.Name, Is.EqualTo(content.Name)); }
public void Basic_Name_IsMapped() { var name = "MyCustomName"; var content = new PublishedContentMock { Name = name }; var model = content.As<BasicModel>(); Assert.That(model.Name, Is.EqualTo(name)); }
public void Basic_Id_And_Property_IsMapped() { var id = 1234; var content = new PublishedContentMock { Id = id }; var model = content.As<BasicModelWithId>(); Assert.That(model.Id, Is.EqualTo(id)); Assert.That(model.MyProperty, Is.EqualTo(id)); }
public void Basic_Content_To_String_Exception() { var content = new PublishedContentMock(); TestDelegate code = () => { // Since a `string` does not have a parameterless constructor, // Ditto can not map the `IPublishedContent` object, // which will throw an `InvalidOperationException` exception. var model = content.As<string>(); }; Assert.Throws<InvalidOperationException>(code); }
public void InheritedClassWithPrefixedProperty_Mapping() { var value = "foo bar"; var property = new Mocks.PublishedContentPropertyMock("inherited_myProperty", value, true); var content = new Mocks.PublishedContentMock() { Properties = new[] { property } }; var model = content.As <InheritedModel>(); Assert.That(model, Is.Not.Null); Assert.That(model.MyProperty, Is.EqualTo(value)); }
public void Custom_TypeConverter_Converts() { var id = 1234; var content = new PublishedContentMock() { Id = id }; var model = content.As<MyModel>(); Assert.That(model.MyProperty, Is.InstanceOf<IPublishedContent>()); Assert.That(model.MyProperty.Id, Is.EqualTo(id)); }
public void Existing_Object_Mapped() { var content = new PublishedContentMock(); var value = "Hello world"; var model = new MyBaseModel() { MyProperty1 = value, MyProperty2 = value }; content.As(instance: model); Assert.That(model.MyProperty1, Is.Not.EqualTo(value)); Assert.That(model.MyProperty2, Is.EqualTo(value)); }
public void ClassLevel_TypeConverter_UmbracoProperty() { // In this test, the `MyProperty` property gets a `string` value // via the `UmbracoProperty`. The `string` type/value is passed // to the `MyCustomConverter` so to convert the `string` to a // `MyCustomModel` type/object. var content = new PublishedContentMock() { Name = "MyName" }; var model = content.As<MyModel1>(); Assert.IsNotNull(model); Assert.IsInstanceOf<MyModel1>(model); Assert.IsNotNull(model.MyProperty); Assert.IsInstanceOf<MyCustomModel>(model.MyProperty); Assert.That(model.MyProperty.Name, Is.EqualTo("MyName")); }
public void CaseSensitivePropertyName_Mapped() { var name = "MyCustomName"; var content = new PublishedContentMock { Name = name }; var model = content.As<MyModel>(); Assert.That(model.Name, Is.EqualTo(name)); Assert.That(model.Name_PascalCase, Is.EqualTo(name)); Assert.That(model.Name_UpperCase, Is.EqualTo(name)); Assert.That(model.Name_LowerCase, Is.EqualTo(name)); Assert.That(model.Name_MixedCase, Is.EqualTo(name)); }
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 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 PocoConstructorParameterOnPropertyTest() { // For this test, the value would have already been resolved by a PropertyValueConverter to its intended type. var value = new MyModel2("foo"); var property = new Mocks.PublishedContentPropertyMock("myProperty", value, true); var content = new Mocks.PublishedContentMock { Properties = new[] { property } }; var model = content.As <MyModel3>(); Assert.That(model, Is.Not.Null); Assert.That(model, Is.InstanceOf <MyModel3>()); Assert.That(model.MyProperty, Is.Not.Null); Assert.That(model.MyProperty, Is.InstanceOf <MyModel2>()); Assert.That(model.MyProperty, Is.EqualTo(value)); }
public void ClassLevel_TypeConverter_ValueResolver() { // In this test, the `MyProperty` property gets its value from // the `MyCustomValueResolver` (returning a `MyCustomModel`). // The `MyCustomConverter` is called, but fails the // `CanConvertFrom` check, so wouldn't try to convert it. // Since the value type is the same as the target property type, // the property value can be set. var content = new PublishedContentMock() { Name = "MyName" }; var model = content.As<MyModel2>(); Assert.IsNotNull(model); Assert.IsInstanceOf<MyModel2>(model); Assert.IsNotNull(model.MyProperty); Assert.IsInstanceOf<MyCustomModel>(model.MyProperty); Assert.That(model.MyProperty.Name, Is.EqualTo("MyCustomName")); }
public void Nested_Enumerable_PublishedContent_Property_Value() { var value = "My Inner Property Value"; var contentList = new List<IPublishedContent>(); for (var i = 0; i < 5; i++) { contentList.Add(new PublishedContentMock { Id = i, Name = "Node " + i, Properties = new[] { new PublishedContentPropertyMock { Alias = "myProperty", Value = value } } }); } var content2 = new PublishedContentMock { Properties = new[] { new PublishedContentPropertyMock { Alias = "myContentList", Value = contentList } } }; var model = content2.As<MyModel>(); Assert.That(model.MyContentList.Count(), Is.EqualTo(5)); Assert.That(model.MyContentList.All(x => x.MyProperty == value), Is.True); }
public void ImageCropDataSetMappedTest() { // JSON test data taken from Umbraco unit-test: // https://github.com/umbraco/Umbraco-CMS/blob/dev-v7/src/Umbraco.Tests/PropertyEditors/ImageCropperTest.cs var json = "{\"focalPoint\": {\"left\": 0.96,\"top\": 0.80827067669172936},\"src\": \"/media/1005/img_0671.jpg\",\"crops\": [{\"alias\":\"thumb\",\"width\": 100,\"height\": 100,\"coordinates\": {\"x1\": 0.58729977382575338,\"y1\": 0.055768992440203169,\"x2\": 0,\"y2\": 0.32457553600198386}}]}"; var value = JsonConvert.DeserializeObject <Models.ImageCropDataSet>(json); Assert.That(value, Is.Not.Null); var property = new Mocks.PublishedContentPropertyMock("myProperty", value, true); var content = new Mocks.PublishedContentMock { Properties = new[] { property } }; var model = content.As <MyModel>(); Assert.That(model, Is.Not.Null); Assert.That(model.MyProperty, Is.Not.Null); Assert.That(model.MyProperty, Is.EqualTo(value)); Assert.That(model.MyProperty.Crops, Is.Not.Null.Or.Empty); Assert.That(model.MyProperty.Src, Is.Not.Null.Or.Empty); }
public void Json_Serialize_Object_Mapping() { var txt = "foo bar"; var obj = new MyJsonModel { Name = txt }; var value = JObject.FromObject(obj); Assert.That(value, Is.Not.Null); var property = new Mocks.PublishedContentPropertyMock("myProperty", value, true); var content = new Mocks.PublishedContentMock { Properties = new[] { property } }; var model = content.As <MyModel>(); Assert.That(model, Is.Not.Null); Assert.That(model.MyProperty, Is.TypeOf <MyJsonModel>()); Assert.That(model.MyProperty, Is.Not.Null); Assert.That(model.MyProperty.Name, Is.EqualTo(txt)); }
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")); }
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 Nested_PublishedContent_Property_Value() { var value = "My Inner Property Value"; var content1 = new PublishedContentMock { Properties = new[] { new PublishedContentPropertyMock { Alias = "myProperty", Value = value } } }; var content2 = new PublishedContentMock { Properties = new[] { new PublishedContentPropertyMock { Alias = "myContent", Value = content1 } } }; var model = content2.As<MyModel>(); Assert.That(model.MyContent.MyProperty, Is.EqualTo(value)); }