public void GetBinder_FromAttribute_Binder_ValueNotPresent_ReturnsNull()
        {
            // Arrange
            ControllerContext             controllerContext = new ControllerContext();
            ExtensibleModelBindingContext bindingContext    = new ExtensibleModelBindingContext
            {
                ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(
                    null,
                    typeof(ModelWithProviderAttribute_Binder)
                    ),
                ModelName     = "foo",
                ValueProvider = new SimpleValueProvider {
                    { "bar", "barValue" }
                }
            };

            ModelBinderProviderCollection providers = new ModelBinderProviderCollection();

            providers.RegisterBinderForType(
                typeof(ModelWithProviderAttribute_Binder),
                new Mock <IExtensibleModelBinder>().Object,
                true /* suppressPrefix */
                );

            // Act
            IExtensibleModelBinder binder = providers.GetBinder(controllerContext, bindingContext);

            // Assert
            Assert.Null(binder);
        }
        public void BindModel_UnsuccessfulBind_BinderFails_ReturnsNull()
        {
            // Arrange
            ControllerContext             controllerContext = GetControllerContext();
            Mock <IExtensibleModelBinder> mockListBinder    = new Mock <IExtensibleModelBinder>();

            mockListBinder.Setup(o => o.BindModel(controllerContext, It.IsAny <ExtensibleModelBindingContext>())).Returns(false).Verifiable();

            ModelBinderProviderCollection binderProviders = new ModelBinderProviderCollection();

            binderProviders.RegisterBinderForType(typeof(List <int>), mockListBinder.Object, true /* suppressPrefixCheck */);
            ExtensibleModelBinderAdapter shimBinder = new ExtensibleModelBinderAdapter(binderProviders);

            ModelBindingContext bindingContext = new ModelBindingContext
            {
                FallbackToEmptyPrefix = false,
                ModelMetadata         = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(List <int>)),
                ModelState            = controllerContext.Controller.ViewData.ModelState
            };

            // Act
            object retVal = shimBinder.BindModel(controllerContext, bindingContext);

            // Assert
            Assert.Null(retVal);
            Assert.True(bindingContext.ModelState.IsValid);
            mockListBinder.Verify();
        }
        public void GetBinder_FromAttribute_Provider_ReturnsBinder()
        {
            // Arrange
            ControllerContext             controllerContext = new ControllerContext();
            ExtensibleModelBindingContext bindingContext    = new ExtensibleModelBindingContext
            {
                ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(
                    null,
                    typeof(ModelWithProviderAttribute_Provider)
                    )
            };

            ModelBinderProviderCollection providers = new ModelBinderProviderCollection();

            providers.RegisterBinderForType(
                typeof(ModelWithProviderAttribute_Provider),
                new Mock <IExtensibleModelBinder>().Object,
                true /* suppressPrefix */
                );

            // Act
            IExtensibleModelBinder binder = providers.GetBinder(controllerContext, bindingContext);

            // Assert
            Assert.IsType <CustomBinder>(binder);
        }
        public void BindModel_SuccessfulBind_ComplexTypeFallback_RunsValidationAndReturnsModel()
        {
            // Arrange
            ControllerContext controllerContext = GetControllerContext();

            bool       validationCalled = false;
            List <int> expectedModel    = new List <int> {
                1, 2, 3, 4, 5
            };

            ModelBindingContext bindingContext = new ModelBindingContext
            {
                FallbackToEmptyPrefix = true,
                ModelMetadata         = new DataAnnotationsModelMetadataProvider().GetMetadataForType(null, typeof(List <int>)),
                ModelName             = "someName",
                ModelState            = controllerContext.Controller.ViewData.ModelState,
                PropertyFilter        = _ => true,
                ValueProvider         = new SimpleValueProvider
                {
                    { "someOtherName", "dummyValue" }
                }
            };

            Mock <IExtensibleModelBinder> mockIntBinder = new Mock <IExtensibleModelBinder>();

            mockIntBinder
            .Setup(o => o.BindModel(controllerContext, It.IsAny <ExtensibleModelBindingContext>()))
            .Returns(
                delegate(ControllerContext cc, ExtensibleModelBindingContext mbc)
            {
                Assert.Same(bindingContext.ModelMetadata, mbc.ModelMetadata);
                Assert.Equal("", mbc.ModelName);
                Assert.Same(bindingContext.ValueProvider, mbc.ValueProvider);

                mbc.Model = expectedModel;
                mbc.ValidationNode.Validating += delegate { validationCalled = true; };
                return(true);
            });

            ModelBinderProviderCollection binderProviders = new ModelBinderProviderCollection();

            binderProviders.RegisterBinderForType(typeof(List <int>), mockIntBinder.Object, false /* suppressPrefixCheck */);
            ExtensibleModelBinderAdapter shimBinder = new ExtensibleModelBinderAdapter(binderProviders);

            // Act
            object retVal = shimBinder.BindModel(controllerContext, bindingContext);

            // Assert
            Assert.Equal(expectedModel, retVal);
            Assert.True(validationCalled);
            Assert.True(bindingContext.ModelState.IsValid);
        }
        public void BindModel_SuccessfulBind_RunsValidationAndReturnsModel()
        {
            // Arrange
            ControllerContext controllerContext = GetControllerContext();
            bool validationCalled = false;

            ModelBindingContext bindingContext = new ModelBindingContext()
            {
                FallbackToEmptyPrefix = true,
                ModelMetadata         = new DataAnnotationsModelMetadataProvider().GetMetadataForType(null, typeof(int)),
                ModelName             = "someName",
                ModelState            = controllerContext.Controller.ViewData.ModelState,
                PropertyFilter        = _ => true,
                ValueProvider         = new SimpleValueProvider()
                {
                    { "someName", "dummyValue" }
                }
            };

            Mock <IExtensibleModelBinder> mockIntBinder = new Mock <IExtensibleModelBinder>();

            mockIntBinder
            .Expect(o => o.BindModel(controllerContext, It.IsAny <ExtensibleModelBindingContext>()))
            .Returns(
                delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) {
                Assert.AreSame(bindingContext.ModelMetadata, mbc.ModelMetadata, "ModelMetadata wasn't correct.");
                Assert.AreEqual("someName", mbc.ModelName, "ModelName wasn't correct.");
                Assert.AreSame(bindingContext.ValueProvider, mbc.ValueProvider, "ValueProvider wasn't correct.");

                mbc.Model = 42;
                mbc.ValidationNode.Validating += delegate { validationCalled = true; };
                return(true);
            });

            ModelBinderProviderCollection binderProviders = new ModelBinderProviderCollection();

            binderProviders.RegisterBinderForType(typeof(int), mockIntBinder.Object, false /* suppressPrefixCheck */);
            ExtensibleModelBinderAdapter shimBinder = new ExtensibleModelBinderAdapter(binderProviders);

            // Act
            object retVal = shimBinder.BindModel(controllerContext, bindingContext);

            // Assert
            Assert.AreEqual(42, retVal);
            Assert.IsTrue(validationCalled);
            Assert.IsTrue(bindingContext.ModelState.IsValid, "No errors should have been added to ModelState.");
        }
        public void BindModel_SuccessfulBind_RunsValidationAndReturnsModel()
        {
            // Arrange
            ControllerContext controllerContext = GetControllerContext();
            bool validationCalled = false;

            ModelBindingContext bindingContext = new ModelBindingContext
            {
                FallbackToEmptyPrefix = true,
                ModelMetadata = new DataAnnotationsModelMetadataProvider().GetMetadataForType(null, typeof(int)),
                ModelName = "someName",
                ModelState = controllerContext.Controller.ViewData.ModelState,
                PropertyFilter = _ => true,
                ValueProvider = new SimpleValueProvider
                {
                    { "someName", "dummyValue" }
                }
            };

            Mock<IExtensibleModelBinder> mockIntBinder = new Mock<IExtensibleModelBinder>();
            mockIntBinder
                .Setup(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>()))
                .Returns(
                    delegate(ControllerContext cc, ExtensibleModelBindingContext mbc)
                    {
                        Assert.Same(bindingContext.ModelMetadata, mbc.ModelMetadata);
                        Assert.Equal("someName", mbc.ModelName);
                        Assert.Same(bindingContext.ValueProvider, mbc.ValueProvider);

                        mbc.Model = 42;
                        mbc.ValidationNode.Validating += delegate { validationCalled = true; };
                        return true;
                    });

            ModelBinderProviderCollection binderProviders = new ModelBinderProviderCollection();
            binderProviders.RegisterBinderForType(typeof(int), mockIntBinder.Object, false /* suppressPrefixCheck */);
            ExtensibleModelBinderAdapter shimBinder = new ExtensibleModelBinderAdapter(binderProviders);

            // Act
            object retVal = shimBinder.BindModel(controllerContext, bindingContext);

            // Assert
            Assert.Equal(42, retVal);
            Assert.True(validationCalled);
            Assert.True(bindingContext.ModelState.IsValid);
        }
        public void RegisterBinderForType_Instance_InsertsNewProviderBehindFrontOfListProviders()
        {
            // Arrange
            ModelBinderProvider    frontOfListProvider = new ProviderAtFront();
            IExtensibleModelBinder mockBinder          = new Mock <IExtensibleModelBinder>().Object;

            ModelBinderProviderCollection collection = new ModelBinderProviderCollection
            {
                frontOfListProvider
            };

            // Act
            collection.RegisterBinderForType(typeof(int), mockBinder);

            // Assert
            Assert.Equal(
                new[] { typeof(ProviderAtFront), typeof(SimpleModelBinderProvider) },
                collection.Select(o => o.GetType()).ToArray());
        }
        public void RegisterBinderForType_Instance()
        {
            // Arrange
            ModelBinderProvider    mockProvider = new Mock <ModelBinderProvider>().Object;
            IExtensibleModelBinder mockBinder   = new Mock <IExtensibleModelBinder>().Object;

            ModelBinderProviderCollection collection = new ModelBinderProviderCollection
            {
                mockProvider
            };

            // Act
            collection.RegisterBinderForType(typeof(int), mockBinder);

            // Assert
            var simpleProvider = Assert.IsType <SimpleModelBinderProvider>(collection[0]);

            Assert.Equal(typeof(int), simpleProvider.ModelType);
            Assert.Equal(mockProvider, collection[1]);
        }
Beispiel #9
0
        public void RegisterBinderForType_Instance_InsertsNewProviderBehindFrontOfListProviders()
        {
            // Arrange
            ModelBinderProvider    frontOfListProvider = new ProviderAtFront();
            IExtensibleModelBinder mockBinder          = new Mock <IExtensibleModelBinder>().Object;

            ModelBinderProviderCollection collection = new ModelBinderProviderCollection()
            {
                frontOfListProvider
            };

            // Act
            collection.RegisterBinderForType(typeof(int), mockBinder);

            // Assert
            CollectionAssert.AreEqual(
                new Type[] { typeof(ProviderAtFront), typeof(SimpleModelBinderProvider) },
                collection.Select(o => o.GetType()).ToArray(),
                "New provider should be inserted after any marked 'front of list'.");
        }
Beispiel #10
0
        public void RegisterBinderForType_Instance()
        {
            // Arrange
            ModelBinderProvider    mockProvider = new Mock <ModelBinderProvider>().Object;
            IExtensibleModelBinder mockBinder   = new Mock <IExtensibleModelBinder>().Object;

            ModelBinderProviderCollection collection = new ModelBinderProviderCollection()
            {
                mockProvider
            };

            // Act
            collection.RegisterBinderForType(typeof(int), mockBinder);

            // Assert
            SimpleModelBinderProvider simpleProvider = collection[0] as SimpleModelBinderProvider;

            Assert.IsNotNull(simpleProvider, "Simple provider should've been inserted at the front of the list.");
            Assert.AreEqual(typeof(int), simpleProvider.ModelType);
            Assert.AreEqual(mockProvider, collection[1]);
        }
Beispiel #11
0
        public void GetBinder_FromAttribute_Binder_ValuePresent_ReturnsBinder()
        {
            // Arrange
            ControllerContext             controllerContext = new ControllerContext();
            ExtensibleModelBindingContext bindingContext    = new ExtensibleModelBindingContext()
            {
                ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(ModelWithProviderAttribute_Binder)),
                ModelName     = "foo",
                ValueProvider = new SimpleValueProvider()
                {
                    { "foo", "fooValue" }
                }
            };

            ModelBinderProviderCollection providers = new ModelBinderProviderCollection();

            providers.RegisterBinderForType(typeof(ModelWithProviderAttribute_Binder), new Mock <IExtensibleModelBinder>().Object, true /* suppressPrefix */);

            // Act
            IExtensibleModelBinder binder = providers.GetBinder(controllerContext, bindingContext);

            // Assert
            Assert.AreEqual(typeof(CustomBinder), binder.GetType(), "Binder should've come from attribute rather than collection.");
        }
        public void BindModel() {
            // Arrange
            ControllerContext controllerContext = new ControllerContext();
            MyModel model = new MyModel();
            ModelMetadata modelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(() => model, typeof(MyModel));
            ComplexModelDto dto = new ComplexModelDto(modelMetadata, modelMetadata.Properties);

            Mock<IExtensibleModelBinder> mockStringBinder = new Mock<IExtensibleModelBinder>();
            mockStringBinder
                .Expect(b => b.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>()))
                .Returns(
                    delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) {
                        Assert.AreEqual(typeof(string), mbc.ModelType);
                        Assert.AreEqual("theModel.StringProperty", mbc.ModelName);
                        mbc.ValidationNode = new ModelValidationNode(mbc.ModelMetadata, "theModel.StringProperty");
                        mbc.Model = "someStringValue";
                        return true;
                    });

            Mock<IExtensibleModelBinder> mockIntBinder = new Mock<IExtensibleModelBinder>();
            mockIntBinder
                .Expect(b => b.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>()))
                .Returns(
                    delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) {
                        Assert.AreEqual(typeof(int), mbc.ModelType);
                        Assert.AreEqual("theModel.IntProperty", mbc.ModelName);
                        mbc.ValidationNode = new ModelValidationNode(mbc.ModelMetadata, "theModel.IntProperty");
                        mbc.Model = 42;
                        return true;
                    });

            Mock<IExtensibleModelBinder> mockDateTimeBinder = new Mock<IExtensibleModelBinder>();
            mockDateTimeBinder
                .Expect(b => b.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>()))
                .Returns(
                    delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) {
                        Assert.AreEqual(typeof(DateTime), mbc.ModelType);
                        Assert.AreEqual("theModel.DateTimeProperty", mbc.ModelName);
                        return false;
                    });

            ModelBinderProviderCollection binders = new ModelBinderProviderCollection();
            binders.RegisterBinderForType(typeof(string), mockStringBinder.Object, true /* suppressPrefixCheck */);
            binders.RegisterBinderForType(typeof(int), mockIntBinder.Object, true /* suppressPrefixCheck */);
            binders.RegisterBinderForType(typeof(DateTime), mockDateTimeBinder.Object, true /* suppressPrefixCheck */);

            ExtensibleModelBindingContext parentBindingContext = new ExtensibleModelBindingContext() {
                ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(() => dto, typeof(ComplexModelDto)),
                ModelName = "theModel",
                ModelBinderProviders = binders
            };

            ComplexModelDtoModelBinder binder = new ComplexModelDtoModelBinder();

            // Act
            bool retVal = binder.BindModel(controllerContext, parentBindingContext);

            // Assert
            Assert.IsTrue(retVal);
            Assert.AreEqual(dto, parentBindingContext.Model, "The return model should have been the original DTO.");

            ComplexModelDtoResult stringDtoResult = dto.Results[dto.PropertyMetadata.Where(m => m.ModelType == typeof(string)).First()];
            Assert.AreEqual("someStringValue", stringDtoResult.Model);
            Assert.AreEqual("theModel.StringProperty", stringDtoResult.ValidationNode.ModelStateKey);

            ComplexModelDtoResult intDtoResult = dto.Results[dto.PropertyMetadata.Where(m => m.ModelType == typeof(int)).First()];
            Assert.AreEqual(42, intDtoResult.Model);
            Assert.AreEqual("theModel.IntProperty", intDtoResult.ValidationNode.ModelStateKey);

            ComplexModelDtoResult dateTimeDtoResult = dto.Results[dto.PropertyMetadata.Where(m => m.ModelType == typeof(DateTime)).First()];
            Assert.IsNull(dateTimeDtoResult);
        }
        public void BindModel_SuccessfulBind_ComplexTypeFallback_RunsValidationAndReturnsModel() {
            // Arrange
            ControllerContext controllerContext = GetControllerContext();

            bool validationCalled = false;
            List<int> expectedModel = new List<int>() { 1, 2, 3, 4, 5 };

            ModelBindingContext bindingContext = new ModelBindingContext() {
                FallbackToEmptyPrefix = true,
                ModelMetadata = new DataAnnotationsModelMetadataProvider().GetMetadataForType(null, typeof(List<int>)),
                ModelName = "someName",
                ModelState = controllerContext.Controller.ViewData.ModelState,
                PropertyFilter = _ => true,
                ValueProvider = new SimpleValueProvider() {
                    { "someOtherName", "dummyValue" }
                }
            };

            Mock<IExtensibleModelBinder> mockIntBinder = new Mock<IExtensibleModelBinder>();
            mockIntBinder
                .Expect(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>()))
                .Returns(
                    delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) {
                        Assert.AreSame(bindingContext.ModelMetadata, mbc.ModelMetadata, "ModelMetadata wasn't correct.");
                        Assert.AreEqual("", mbc.ModelName, "ModelName wasn't correct.");
                        Assert.AreSame(bindingContext.ValueProvider, mbc.ValueProvider, "ValueProvider wasn't correct.");

                        mbc.Model = expectedModel;
                        mbc.ValidationNode.Validating += delegate { validationCalled = true; };
                        return true;
                    });

            ModelBinderProviderCollection binderProviders = new ModelBinderProviderCollection();
            binderProviders.RegisterBinderForType(typeof(List<int>), mockIntBinder.Object, false /* suppressPrefixCheck */);
            ExtensibleModelBinderAdapter shimBinder = new ExtensibleModelBinderAdapter(binderProviders);

            // Act
            object retVal = shimBinder.BindModel(controllerContext, bindingContext);

            // Assert
            Assert.AreEqual(expectedModel, retVal);
            Assert.IsTrue(validationCalled);
            Assert.IsTrue(bindingContext.ModelState.IsValid, "No errors should have been added to ModelState.");
        }
        public void BindModel_UnsuccessfulBind_BinderFails_ReturnsNull() {
            // Arrange
            ControllerContext controllerContext = GetControllerContext();
            Mock<IExtensibleModelBinder> mockListBinder = new Mock<IExtensibleModelBinder>();
            mockListBinder.Expect(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>())).Returns(false).Verifiable();

            ModelBinderProviderCollection binderProviders = new ModelBinderProviderCollection();
            binderProviders.RegisterBinderForType(typeof(List<int>), mockListBinder.Object, true /* suppressPrefixCheck */);
            ExtensibleModelBinderAdapter shimBinder = new ExtensibleModelBinderAdapter(binderProviders);

            ModelBindingContext bindingContext = new ModelBindingContext() {
                FallbackToEmptyPrefix = false,
                ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(List<int>)),
                ModelState = controllerContext.Controller.ViewData.ModelState
            };

            // Act
            object retVal = shimBinder.BindModel(controllerContext, bindingContext);

            // Assert
            Assert.IsNull(retVal);
            Assert.IsTrue(bindingContext.ModelState.IsValid, "No errors should have been added to ModelState.");
            mockListBinder.Verify();
        }
        public void BindModel()
        {
            // Arrange
            ControllerContext controllerContext = new ControllerContext();
            MyModel           model             = new MyModel();
            ModelMetadata     modelMetadata     = new EmptyModelMetadataProvider().GetMetadataForType(() => model, typeof(MyModel));
            ComplexModelDto   dto = new ComplexModelDto(modelMetadata, modelMetadata.Properties);

            Mock <IExtensibleModelBinder> mockStringBinder = new Mock <IExtensibleModelBinder>();

            mockStringBinder
            .Setup(b => b.BindModel(controllerContext, It.IsAny <ExtensibleModelBindingContext>()))
            .Returns(
                delegate(ControllerContext cc, ExtensibleModelBindingContext mbc)
            {
                Assert.Equal(typeof(string), mbc.ModelType);
                Assert.Equal("theModel.StringProperty", mbc.ModelName);
                mbc.ValidationNode = new ModelValidationNode(mbc.ModelMetadata, "theModel.StringProperty");
                mbc.Model          = "someStringValue";
                return(true);
            });

            Mock <IExtensibleModelBinder> mockIntBinder = new Mock <IExtensibleModelBinder>();

            mockIntBinder
            .Setup(b => b.BindModel(controllerContext, It.IsAny <ExtensibleModelBindingContext>()))
            .Returns(
                delegate(ControllerContext cc, ExtensibleModelBindingContext mbc)
            {
                Assert.Equal(typeof(int), mbc.ModelType);
                Assert.Equal("theModel.IntProperty", mbc.ModelName);
                mbc.ValidationNode = new ModelValidationNode(mbc.ModelMetadata, "theModel.IntProperty");
                mbc.Model          = 42;
                return(true);
            });

            Mock <IExtensibleModelBinder> mockDateTimeBinder = new Mock <IExtensibleModelBinder>();

            mockDateTimeBinder
            .Setup(b => b.BindModel(controllerContext, It.IsAny <ExtensibleModelBindingContext>()))
            .Returns(
                delegate(ControllerContext cc, ExtensibleModelBindingContext mbc)
            {
                Assert.Equal(typeof(DateTime), mbc.ModelType);
                Assert.Equal("theModel.DateTimeProperty", mbc.ModelName);
                return(false);
            });

            ModelBinderProviderCollection binders = new ModelBinderProviderCollection();

            binders.RegisterBinderForType(typeof(string), mockStringBinder.Object, true /* suppressPrefixCheck */);
            binders.RegisterBinderForType(typeof(int), mockIntBinder.Object, true /* suppressPrefixCheck */);
            binders.RegisterBinderForType(typeof(DateTime), mockDateTimeBinder.Object, true /* suppressPrefixCheck */);

            ExtensibleModelBindingContext parentBindingContext = new ExtensibleModelBindingContext
            {
                ModelMetadata        = new EmptyModelMetadataProvider().GetMetadataForType(() => dto, typeof(ComplexModelDto)),
                ModelName            = "theModel",
                ModelBinderProviders = binders
            };

            ComplexModelDtoModelBinder binder = new ComplexModelDtoModelBinder();

            // Act
            bool retVal = binder.BindModel(controllerContext, parentBindingContext);

            // Assert
            Assert.True(retVal);
            Assert.Equal(dto, parentBindingContext.Model);

            ComplexModelDtoResult stringDtoResult = dto.Results[dto.PropertyMetadata.Where(m => m.ModelType == typeof(string)).First()];

            Assert.Equal("someStringValue", stringDtoResult.Model);
            Assert.Equal("theModel.StringProperty", stringDtoResult.ValidationNode.ModelStateKey);

            ComplexModelDtoResult intDtoResult = dto.Results[dto.PropertyMetadata.Where(m => m.ModelType == typeof(int)).First()];

            Assert.Equal(42, intDtoResult.Model);
            Assert.Equal("theModel.IntProperty", intDtoResult.ValidationNode.ModelStateKey);

            ComplexModelDtoResult dateTimeDtoResult = dto.Results[dto.PropertyMetadata.Where(m => m.ModelType == typeof(DateTime)).First()];

            Assert.Null(dateTimeDtoResult);
        }