public void UmbracoMapper_MapFromIPublishedContent_MapsNativePropertiesWithStringFormatter()
        {
            // Arrange
            var model = new SimpleViewModel();
            var content = new StubPublishedContent();
            var mapper = GetMapper();

            // Act
            mapper.Map(content, model, new Dictionary<string, PropertyMapping> { { "Name", new PropertyMapping { StringValueFormatter = x => { return x.ToUpper(); }, } } });

            // Assert
            Assert.AreEqual("TEST CONTENT", model.Name);
        }
        public void UmbracoMapper_MapFromIPublishedContent_MapsNativePropertiesWithMatchingNames()
        {
            // Arrange
            var model = new SimpleViewModel();
            var content = new StubPublishedContent();
            var mapper = GetMapper();

            // Act
            mapper.Map(content, model);

            // Assert
            Assert.AreEqual(1000, model.Id);
            Assert.AreEqual("Test content", model.Name);
        }
        public void UmbracoMapper_MapFromIPublishedContent_MapsNativePropertiesWithDifferentNamesUsingAttribute()
        {
            // Arrange
            var model = new SimpleViewModel2WithAttribute();
            var content = new StubPublishedContent();
            var mapper = GetMapper();

            // Act
            mapper.Map(content, model);

            // Assert
            Assert.AreEqual(1000, model.Id);
            Assert.AreEqual("Test content", model.Name);
            Assert.AreEqual("A.N. Editor", model.Author);
        }
        public void UmbracoMapper_MapFromIPublishedContent_MapsNativePropertiesWithDifferentNames()
        {
            // Arrange
            var model = new SimpleViewModel2();
            var content = new StubPublishedContent();
            var mapper = GetMapper();

            // Act
            mapper.Map(content, model, new Dictionary<string, PropertyMapping> { { "Author", new PropertyMapping { SourceProperty = "CreatorName", } } });

            // Assert
            Assert.AreEqual(1000, model.Id);
            Assert.AreEqual("Test content", model.Name);
            Assert.AreEqual("A.N. Editor", model.Author);
        }
        public void UmbracoMapper_MapFromIPublishedContent_MapsCustomPropertiesWithNonMatchingCondition()
        {
            // Using a shim of umbraco.dll
            using (ShimsContext.Create())
            {
                // Arrange
                var model = new SimpleViewModel3();
                var mapper = GetMapper();
                var content = new StubPublishedContent();

                // - shim GetPropertyValue (an extension method on IPublishedContent so can't be mocked)
                Umbraco.Web.Fakes.ShimPublishedContentExtensions.GetPropertyValueIPublishedContentStringBoolean =
                    (doc, alias, recursive) =>
                    {
                        switch (alias)
                        {
                            case "summaryText":
                                return "Another value";
                            case "bodyText":
                                return "This is the body text";
                            default:
                                return string.Empty;
                        }
                    };

                // Act
                mapper.Map(content, model, new Dictionary<string, PropertyMapping> 
                    { 
                        { 
                            "BodyText", 
                            new PropertyMapping 
                            { 
                                MapIfPropertyMatches = new KeyValuePair<string, string>("summaryText", "Test value")
                            } 
                        } 
                    });

                // Assert
                Assert.IsNull(model.BodyText);
            }
        }
        public void UmbracoMapper_MapFromIPublishedContent_MapsCustomPropertiesWithCoalescingAndFirstItemNotAvailableUsingAttribute()
        {
            // Using a shim of umbraco.dll
            using (ShimsContext.Create())
            {
                // Arrange
                var model = new SimpleViewModel5WithAttribute();
                var mapper = GetMapper();
                var content = new StubPublishedContent();

                // - shim GetPropertyValue (an extension method on IPublishedContent so can't be mocked)
                Umbraco.Web.Fakes.ShimPublishedContentExtensions.GetPropertyValueIPublishedContentStringBoolean =
                    (doc, alias, recursive) =>
                    {
                        switch (alias)
                        {
                            case "summaryText":
                                return string.Empty;
                            case "bodyText":
                                return "This is the body text";
                            default:
                                return string.Empty;
                        }
                    };

                // Act
                mapper.Map(content, model);

                // Assert
                Assert.AreEqual("This is the body text", model.SummaryText);
            }
        }
        public void UmbracoMapper_MapFromIPublishedContent_MapsWithIntegerDefaultValueUsingAttribute()
        {
            // Using a shim of umbraco.dll
            using (ShimsContext.Create())
            {
                // Arrange
                var model = new SimpleViewModel3WithAttribute();
                var mapper = GetMapper();
                var content = new StubPublishedContent();

                // - shim GetPropertyValue (an extension method on IPublishedContent so can't be mocked)
                Umbraco.Web.Fakes.ShimPublishedContentExtensions.GetPropertyValueIPublishedContentStringBoolean =
                    (doc, alias, recursive) =>
                    {
                        return string.Empty;
                    };

                // Act
                mapper.Map(content, model);

                // Assert
                Assert.AreEqual(99, model.NonMapped);
            }
        }
        public void UmbracoMapper_MapFromIPublishedContent_MapsOnlyCustomProperties()
        {
            // Using a shim of umbraco.dll
            using (ShimsContext.Create())
            {
                // Arrange
                var model = new SimpleViewModel3();
                var mapper = GetMapper();
                var content = new StubPublishedContent();

                // - shim GetPropertyValue (an extension method on IPublishedContent so can't be mocked)
                Umbraco.Web.Fakes.ShimPublishedContentExtensions.GetPropertyValueIPublishedContentStringBoolean =
                    (doc, alias, recursive) =>
                    {
                        switch (alias)
                        {
                            case "bodyText":
                                return "This is the body text";
                            default:
                                return string.Empty;
                        }
                    };

                // Act
                mapper.Map(content, model, propertySet: PropertySet.Custom);

                // Assert
                Assert.AreEqual(0, model.Id);
                Assert.IsNull(model.Name);
                Assert.AreEqual("This is the body text", model.BodyText);
            }
        }
        public void UmbracoMapper_MapFromIPublishedContent_MapsUsingCustomMappingWithNonMatchingPropertyCondition()
        {
            // Using a shim of umbraco.dll
            using (ShimsContext.Create())
            {
                // Arrange
                var model = new SimpleViewModel8();
                var content = new StubPublishedContent();
                var mapper = GetMapper();
                mapper.AddCustomMapping(typeof(GeoCoordinate).FullName, MapGeoCoordinate, "AnotherProperty");

                // - shim GetPropertyValue (an extension method on IPublishedContent so can't be mocked)
                Umbraco.Web.Fakes.ShimPublishedContentExtensions.GetPropertyValueIPublishedContentStringBoolean =
                    (doc, alias, recursive) =>
                    {
                        switch (alias)
                        {
                            case "geoCoordinate":
                                return "5.5,10.5,7";
                            default:
                                return string.Empty;
                        }
                    };

                // Act
                mapper.Map(content, model);

                // Assert
                Assert.IsNull(model.GeoCoordinate);
            }
        }
        public void UmbracoMapper_MapFromIPublishedContent_DoesNotMapIgnoredCustomPropertiesUsingDictionary()
        {
            // Using a shim of umbraco.dll
            using (ShimsContext.Create())
            {
                // Arrange
                var model = new SimpleViewModel3();
                var mapper = GetMapper();
                var content = new StubPublishedContent();

                // - shim GetPropertyValue (an extension method on IPublishedContent so can't be mocked)
                Umbraco.Web.Fakes.ShimPublishedContentExtensions.GetPropertyValueIPublishedContentStringBoolean =
                    (doc, alias, recursive) =>
                    {
                        switch (alias)
                        {
                            case "bodyText":
                                return "This is the body text";
                            default:
                                return string.Empty;
                        }
                    };

                // Act
                mapper.Map(content, model, new Dictionary<string, PropertyMapping> { { "BodyText", new PropertyMapping { Ignore = true, } } });

                // Assert
                Assert.AreEqual(1000, model.Id);
                Assert.AreEqual("Test content", model.Name);
                Assert.IsNull(model.BodyText);
            }
        }
        public void UmbracoMapper_MapFromIPublishedContent_MapsCustomPropertiesWithStringFormatter()
        {
            // Using a shim of umbraco.dll
            using (ShimsContext.Create())
            {
                // Arrange
                var model = new SimpleViewModel3();
                var mapper = GetMapper();
                var content = new StubPublishedContent();

                // - shim GetPropertyValue (an extension method on IPublishedContent so can't be mocked)
                Umbraco.Web.Fakes.ShimPublishedContentExtensions.GetPropertyValueIPublishedContentStringBoolean =
                    (doc, alias, recursive) =>
                    {
                        switch (alias)
                        {
                            case "bodyText":
                                return "This is the body text";
                            default:
                                return string.Empty;
                        }
                    };

                // Act
                mapper.Map(content, model, new Dictionary<string, PropertyMapping> { { "BodyText", new PropertyMapping { StringValueFormatter = x => { return x.ToUpper(); } } } });

                // Assert
                Assert.AreEqual("THIS IS THE BODY TEXT", model.BodyText);
            }
        }        
        public void UmbracoMapper_MapFromIPublishedContent_DoesNotMapIgnoredNativePropertiesUsingAttribute()
        {
            // Arrange
            var model = new SimpleViewModel1b();
            var content = new StubPublishedContent();
            var mapper = GetMapper();

            // Act
            mapper.Map(content, model);

            // Assert
            Assert.AreEqual(1000, model.Id);
            Assert.IsNull(model.Name);
        }
        public void UmbracoMapper_MapFromIPublishedContent_AutomapsParentIPublishedContent()
        {
            // Using a shim of umbraco.dll
            using (ShimsContext.Create())
            {
                // Arrange
                var model = new SimpleViewModel2bWithAttribute();
                var mapper = GetMapper();
                var content = new StubPublishedContent();

                // - shim GetPropertyValue (an extension method on IPublishedContent so can't be mocked)
                Umbraco.Web.Fakes.ShimPublishedContentExtensions.GetPropertyValueIPublishedContentStringBoolean =
                    (doc, alias, recursive) =>
                    {
                        switch (alias)
                        {
                            case "parent":
                                return new StubPublishedContent(1001);
                            default:
                                return string.Empty;
                        }
                    };

                // Act
                mapper.Map(content, model);

                // Assert
                Assert.AreEqual(1000, model.Id);
                Assert.AreEqual(1001, model.Parent.Id);
            }
        }
        public void UmbracoMapper_MapFromIPublishedContent_AutomapsRelatedIPublishedContent()
        {
            // Using a shim of umbraco.dll
            using (ShimsContext.Create())
            {
                // Arrange
                var model = new SimpleViewModel3WithAttribute();
                var mapper = GetMapper();
                var content = new StubPublishedContent();

                // - shim GetPropertyValue (an extension method on IPublishedContent so can't be mocked)
                Umbraco.Web.Fakes.ShimPublishedContentExtensions.GetPropertyValueIPublishedContentStringBoolean =
                    (doc, alias, recursive) =>
                    {
                        switch (alias)
                        {
                            case "subHeading":
                                return "This is the sub-heading";
                            case "bodyText":
                                return "This is the body text";
                            case "subModelValue":
                                return new StubPublishedContent();
                            case "subModelValues":
                                return new List<StubPublishedContent> { new StubPublishedContent(), new StubPublishedContent() };
                            default:
                                return string.Empty;
                        }
                    };

                // Act
                mapper.Map(content, model);

                // Assert
                Assert.AreEqual("This is the body text", model.BodyText);
                Assert.AreEqual("This is the sub-heading", model.SubModelValue.SubHeading);
                Assert.AreEqual(2, model.SubModelValues.Count());
                Assert.AreEqual("This is the sub-heading", model.SubModelValues.First().SubHeading);
            }
        }
        public void UmbracoMapper_MapFromIPublishedContent_MapsNullToNullableDateTimeWithNoValue()
        {
            // Using a shim of umbraco.dll
            using (ShimsContext.Create())
            {
                // Arrange
                var model = new SimpleViewModel4bWithAttribute();
                var mapper = GetMapper();
                var content = new StubPublishedContent();

                // - shim GetPropertyValue (an extension method on IPublishedContent so can't be mocked)
                Umbraco.Web.Fakes.ShimPublishedContentExtensions.GetPropertyValueIPublishedContentStringBoolean =
                    (doc, alias, recursive) =>
                    {
                        switch (alias)
                        {
                            case "dateTime":
                                return "1/1/0001 12:00:00 AM";
                            default:
                                return string.Empty;
                        }
                    };

                // Act
                mapper.Map(content, model);

                // Assert
                Assert.IsFalse(model.DateTime.HasValue);
            }
        }          
        public void UmbracoMapper_MapFromIPublishedContent_MapsNativePropertiesFromParentNode()
        {
            // Arrange
            var model = new SimpleViewModel7();
            var content = new StubPublishedContent();
            var mapper = GetMapper();

            // Act
            mapper.Map(content, model, new Dictionary<string, PropertyMapping> 
                { 
                    { 
                        "ParentId", 
                        new PropertyMapping 
                        { 
                            SourceProperty = "Id",
                            LevelsAbove = 1
                        } 
                    } 
                });

            // Assert
            Assert.AreEqual(1000, model.Id);
            Assert.AreEqual(1001, model.ParentId);
        }
        public void UmbracoMapper_MapFromIPublishedContent_MapsNativePropertiesFromParentNodeUsingAttribute()
        {
            // Arrange
            var model = new SimpleViewModel7WithAttribute();
            var content = new StubPublishedContent();
            var mapper = GetMapper();

            // Act
            mapper.Map(content, model);

            // Assert
            Assert.AreEqual(1000, model.Id);
            Assert.AreEqual(1001, model.ParentId);
        }
        public void UmbracoMapper_MapFromIPublishedContent_MapsCustomPropertiesWithDifferentNamesUsingAttributeAndRecursiveProperty()
        {
            // Using a shim of umbraco.dll
            using (ShimsContext.Create())
            {
                // Arrange
                var model = new SimpleViewModel4bWithAttribute();
                var mapper = GetMapper();
                var content = new StubPublishedContent();

                // - shim GetPropertyValue (an extension method on IPublishedContent so can't be mocked)
                Umbraco.Web.Fakes.ShimPublishedContentExtensions.GetPropertyValueIPublishedContentStringBoolean =
                    (doc, alias, recursive) =>
                    {
                        switch (alias)
                        {
                            case "bodyText":
                                if (recursive)
                                {
                                    return "This is the body text";
                                }

                                return string.Empty;
                            default:
                                return string.Empty;
                        }
                    };

                // Act
                mapper.Map(content, model);

                // Assert
                Assert.AreEqual(1000, model.Id);
                Assert.AreEqual("Test content", model.Name);
                Assert.AreEqual("This is the body text", model.BodyCopy);
            }
        }        
        public void UmbracoMapper_MapFromIPublishedContent_MapsUsingMapFromAttribute()
        {
            // Using a shim of umbraco.dll
            using (ShimsContext.Create())
            {
                // Arrange
                var model = new SimpleViewModel9();
                var content = new StubPublishedContent();
                var mapper = GetMapper();

                // - shim GetPropertyValue (an extension method on IPublishedContent so can't be mocked)
                Umbraco.Web.Fakes.ShimPublishedContentExtensions.GetPropertyValueIPublishedContentStringBoolean =
                    (doc, alias, recursive) =>
                    {
                        switch (alias)
                        {
                            case "child":
                                return 1001;
                            default:
                                return string.Empty;
                        }
                    };

                // Act
                mapper.Map(content, model);

                // Assert
                Assert.AreEqual("Test content", model.Name);
                Assert.AreEqual("Child item", model.Child.Name);
            }
        }
        public void UmbracoMapper_MapFromIPublishedContent_MapsCustomPropertiesWithConcatenation()
        {
            // Using a shim of umbraco.dll
            using (ShimsContext.Create())
            {
                // Arrange
                var model = new SimpleViewModel5();
                var mapper = GetMapper();
                var content = new StubPublishedContent();

                // - shim GetPropertyValue (an extension method on IPublishedContent so can't be mocked)
                Umbraco.Web.Fakes.ShimPublishedContentExtensions.GetPropertyValueIPublishedContentStringBoolean =
                    (doc, alias, recursive) =>
                    {
                        switch (alias)
                        {
                            case "bodyText":
                                return "This is the body text";
                            default:
                                return string.Empty;
                        }
                    };

                // Act
                mapper.Map(content, model, new Dictionary<string, PropertyMapping> 
                    { 
                        { 
                            "HeadingAndBodyText", 
                            new PropertyMapping 
                            { 
                                SourcePropertiesForConcatenation = new string[] { "Name", "bodyText" }, 
                                ConcatenationSeperator = ",",
                            } 
                        } 
                    });

                // Assert
                Assert.AreEqual("Test content,This is the body text", model.HeadingAndBodyText);
            }
        }
        public void UmbracoMapper_MapFromIPublishedContent_MapsWithStringDefaultValueUsingAttribute()
        {
            // Using a shim of umbraco.dll
            using (ShimsContext.Create())
            {
                // Arrange
                var model = new SimpleViewModel3WithAttribute();
                var mapper = GetMapper();
                var content = new StubPublishedContent();

                // - shim GetPropertyValue (an extension method on IPublishedContent so can't be mocked)
                Umbraco.Web.Fakes.ShimPublishedContentExtensions.GetPropertyValueIPublishedContentStringBoolean =
                    (doc, alias, recursive) =>
                    {
                        switch (alias)
                        {
                            case "bodyText":
                                return null;
                            case "bodyText2":
                                return string.Empty;
                            default:
                                return string.Empty;
                        }
                    };

                // Act
                mapper.Map(content, model);

                // Assert
                Assert.AreEqual("Default body text", model.BodyText);
                Assert.AreEqual("Default body text 2", model.BodyText2);
            }
        }
        public void UmbracoMapper_MapFromIPublishedContent_DoesNotMapIgnoredNativePropertiesUsingDictionary()
        {
            // Arrange
            var model = new SimpleViewModel();
            var content = new StubPublishedContent();
            var mapper = GetMapper();

            // Act
            mapper.Map(content, model, new Dictionary<string, PropertyMapping> { { "Name", new PropertyMapping { Ignore = true, } } });

            // Assert
            Assert.AreEqual(1000, model.Id);
            Assert.IsNull(model.Name);
        }