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 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 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 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 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 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 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 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 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));
        }