public async Task AboutPost_SetAccreditingProviderToEmpty()
        {
            var existingEnrichment = new UcasProviderEnrichmentGetModel()
            {
                EnrichmentModel = new ProviderEnrichmentModel()
                {
                    AccreditingProviderEnrichments = new List <AccreditingProviderEnrichment>
                    {
                        new AccreditingProviderEnrichment
                        {
                            UcasProviderCode = "ACC",
                            Description      = "foo"
                        }
                    }
                }
            };

            var apiMock = new Mock <IManageApi>();

            apiMock.Setup(x => x.GetProviderEnrichment("ABC")).ReturnsAsync(existingEnrichment);

            UcasProviderEnrichmentPostModel result = null;

            apiMock.Setup(x => x.SaveProviderEnrichment("ABC", It.IsAny <UcasProviderEnrichmentPostModel>()))
            .Callback((string a, UcasProviderEnrichmentPostModel b) => result = b)
            .Returns(Task.CompletedTask);

            apiMock.Setup(x => x.GetCoursesOfProvider("ABC")).ReturnsAsync(new List <Course> {
                new Course {
                    AccreditingProvider = new GovUk.Education.ManageCourses.Domain.Models.Provider {
                        ProviderCode = "ACC"
                    }
                }
            });

            var objectValidator = new Mock <IObjectModelValidator>();

            objectValidator.Setup(o => o.Validate(It.IsAny <ActionContext>(),
                                                  It.IsAny <ValidationStateDictionary>(),
                                                  It.IsAny <string>(),
                                                  It.IsAny <Object>()));

            var frontendUrlMock = new Mock <IFrontendUrlService>();

            var controller = new OrganisationController(apiMock.Object, frontendUrlMock.Object);

            controller.ObjectValidator = objectValidator.Object;
            controller.TempData        = new Mock <ITempDataDictionary>().Object;

            var res = await controller.AboutPost("ABC", new OrganisationViewModelForAbout {
                AboutTrainingProviders = new List <TrainingProviderViewModel>
                {
                    new TrainingProviderViewModel
                    {
                        ProviderCode = "ACC",
                        Description  = null // not an empty string... this is how MVC model binding behaves
                    }
                }
            });


            result.Should().NotBeNull();
            result.EnrichmentModel.AccreditingProviderEnrichments[0].UcasProviderCode.Should().Be("ACC");
            result.EnrichmentModel.AccreditingProviderEnrichments[0].Description.Should().BeNullOrEmpty();
        }
        public void AboutPost_ModelState_WordCount()
        {
            var providerCode   = "PROVIDERCODE";
            var exceed100Words = "";

            for (int i = 0; i < 101; i++)
            {
                exceed100Words += i + " ";
            }
            var providerName = "ProviderName";

            var viewModel = new OrganisationViewModelForAbout
            {
                AboutTrainingProviders = new List <TrainingProviderViewModel>()
                {
                    new TrainingProviderViewModel {
                        Description  = exceed100Words,
                        ProviderName = providerName,
                        ProviderCode = providerCode + 1
                    }
                }
            };

            var providerCourses = new List <Course>
            {
                new Course {
                },
                new Course {
                    AccreditingProvider = new GovUk.Education.ManageCourses.Domain.Models.Provider {
                        ProviderCode = providerCode.ToUpperInvariant()
                    }
                },
                new Course {
                    AccreditingProvider = new GovUk.Education.ManageCourses.Domain.Models.Provider {
                        ProviderCode = providerCode.ToLowerInvariant()
                    }
                },
                new Course {
                    AccreditingProvider = new GovUk.Education.ManageCourses.Domain.Models.Provider {
                        ProviderCode = providerCode
                    }
                },
                new Course {
                    AccreditingProvider = new GovUk.Education.ManageCourses.Domain.Models.Provider {
                        ProviderCode = providerCode + 1, ProviderName = providerName
                    }
                },
                new Course {
                    AccreditingProvider = new GovUk.Education.ManageCourses.Domain.Models.Provider {
                        ProviderCode = providerCode + 2
                    }
                },
            };

            var apiMock = new Mock <IManageApi>();

            apiMock.Setup(x => x.GetCoursesOfProvider(providerCode))
            .ReturnsAsync(providerCourses);


            var enrichmentModel = new ProviderEnrichmentModel {
                AccreditingProviderEnrichments = new List <AccreditingProviderEnrichment> {
                }
            };

            var ucasProviderEnrichmentGetModel = new UcasProviderEnrichmentGetModel()
            {
                EnrichmentModel = enrichmentModel
            };

            apiMock.Setup(x => x.GetProviderSummaries())
            .ReturnsAsync(new List <ProviderSummary> {
                new ProviderSummary()
            });

            apiMock.Setup(x => x.GetProviderEnrichment(providerCode))
            .ReturnsAsync(ucasProviderEnrichmentGetModel);

            var objectValidator = new Mock <IObjectModelValidator>();

            objectValidator.Setup(o => o.Validate(It.IsAny <ActionContext>(),
                                                  It.IsAny <ValidationStateDictionary>(),
                                                  It.IsAny <string>(),
                                                  It.IsAny <Object>()));

            var frontendUrlMock = new Mock <IFrontendUrlService>();

            var controller = new OrganisationController(apiMock.Object, frontendUrlMock.Object);

            controller.ObjectValidator = objectValidator.Object;

            controller.TempData = new Mock <ITempDataDictionary>().Object;

            Assert.IsFalse(controller.ModelState.Any());
            Assert.IsTrue(controller.ModelState.IsValid);
            var result = controller.AboutPost(providerCode, viewModel).Result;


            Assert.IsTrue(controller.ModelState.Any());
            Assert.AreEqual($"Reduce word count for {providerName}", controller.ModelState["AboutTrainingProviders_0__Description"].Errors.First().ErrorMessage);
            Assert.IsFalse(controller.ModelState.IsValid);

            var viewResult = result as ViewResult;

            var organisationViewModel = viewResult.ViewData.Model as OrganisationViewModelForAbout;

            Assert.IsNotNull(viewResult);
            Assert.AreEqual(exceed100Words, organisationViewModel.AboutTrainingProviders.First(x => x.ProviderCode == providerCode + 1).Description);
            Assert.AreEqual(providerName, organisationViewModel.AboutTrainingProviders.First(x => x.ProviderCode == providerCode + 1).ProviderName);
        }
        public async Task AboutPost_SaveOrganisation()
        {
            var providerCode = "PROVIDERCODE";
            var viewModel    = new OrganisationViewModelForAbout
            {
                AboutTrainingProviders = new List <TrainingProviderViewModel>()
            };

            var providerName = "ProviderName";

            var providerCourses = new List <Course>
            {
                new Course {
                },
                new Course {
                    AccreditingProvider = new GovUk.Education.ManageCourses.Domain.Models.Provider {
                        ProviderCode = providerCode.ToUpperInvariant()
                    }
                },
                new Course {
                    AccreditingProvider = new GovUk.Education.ManageCourses.Domain.Models.Provider {
                        ProviderCode = providerCode.ToLowerInvariant()
                    }
                },
                new Course {
                    AccreditingProvider = new GovUk.Education.ManageCourses.Domain.Models.Provider {
                        ProviderCode = providerCode
                    }
                },
                new Course {
                    AccreditingProvider = new GovUk.Education.ManageCourses.Domain.Models.Provider {
                        ProviderCode = providerCode + 1, ProviderName = providerName
                    }
                },
                new Course {
                    AccreditingProvider = new GovUk.Education.ManageCourses.Domain.Models.Provider {
                        ProviderCode = providerCode + 2
                    }
                },
            };

            var apiMock = new Mock <IManageApi>();

            apiMock.Setup(x => x.GetCoursesOfProvider(providerCode))
            .ReturnsAsync(providerCourses);

            var enrichmentModel = new ProviderEnrichmentModel {
                AccreditingProviderEnrichments = new List <AccreditingProviderEnrichment> {
                }
            };

            var ucasProviderEnrichmentGetModel = new UcasProviderEnrichmentGetModel()
            {
                EnrichmentModel = enrichmentModel
            };

            apiMock.Setup(x => x.GetProviderEnrichment(providerCode))
            .ReturnsAsync(ucasProviderEnrichmentGetModel);

            var objectValidator = new Mock <IObjectModelValidator>();

            objectValidator.Setup(o => o.Validate(It.IsAny <ActionContext>(),
                                                  It.IsAny <ValidationStateDictionary>(),
                                                  It.IsAny <string>(),
                                                  It.IsAny <Object>()));

            var frontendUrlMock = new Mock <IFrontendUrlService>();

            var controller = new OrganisationController(apiMock.Object, frontendUrlMock.Object);

            controller.ObjectValidator = objectValidator.Object;

            controller.TempData = new Mock <ITempDataDictionary>().Object;

            var result = await controller.AboutPost(providerCode, viewModel);

            apiMock.Verify(x => x.SaveProviderEnrichment(providerCode, It.IsAny <UcasProviderEnrichmentPostModel>()), Times.Once);

            var actionResult = result as RedirectToActionResult;

            Assert.IsNotNull(actionResult);
            Assert.AreEqual("Details", actionResult.ActionName);
            Assert.AreEqual("Organisation", actionResult.ControllerName);
            Assert.AreEqual(providerCode, actionResult.RouteValues[providerCode]);
        }